wgc_session_impl.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include <mutex>
  3. #include <thread>
  4. namespace am {
  5. class wgc_session_impl : public wgc_session {
  6. struct __declspec(uuid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1"))
  7. IDirect3DDxgiInterfaceAccess : ::IUnknown {
  8. virtual HRESULT __stdcall GetInterface(GUID const &id, void **object) = 0;
  9. };
  10. struct {
  11. union {
  12. HWND hwnd;
  13. HMONITOR hmonitor;
  14. };
  15. bool is_window;
  16. } target_{0};
  17. public:
  18. wgc_session_impl();
  19. ~wgc_session_impl() override;
  20. public:
  21. void release() override;
  22. int initialize(HWND hwnd) override;
  23. int initialize(HMONITOR hmonitor) override;
  24. void register_observer(wgc_session_observer *observer) override;
  25. int start() override;
  26. int stop() override;
  27. int pause() override;
  28. int resume() override;
  29. private:
  30. auto create_d3d11_device();
  31. auto create_capture_item(HWND hwnd);
  32. auto create_capture_item(HMONITOR hmonitor);
  33. template <typename T>
  34. auto
  35. get_dxgi_interface(winrt::Windows::Foundation::IInspectable const &object);
  36. HRESULT create_mapped_texture(winrt::com_ptr<ID3D11Texture2D> src_texture,
  37. unsigned int width = 0,
  38. unsigned int height = 0);
  39. void
  40. on_frame(winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const
  41. &sender,
  42. winrt::Windows::Foundation::IInspectable const &args);
  43. void on_closed(winrt::Windows::Graphics::Capture::GraphicsCaptureItem const &,
  44. winrt::Windows::Foundation::IInspectable const &);
  45. int initialize();
  46. void cleanup();
  47. void message_func();
  48. private:
  49. std::mutex lock_;
  50. bool is_initialized_ = false;
  51. bool is_running_ = false;
  52. bool is_paused_ = false;
  53. wgc_session_observer *observer_ = nullptr;
  54. // wgc
  55. winrt::Windows::Graphics::Capture::GraphicsCaptureItem capture_item_{nullptr};
  56. winrt::Windows::Graphics::Capture::GraphicsCaptureSession capture_session_{
  57. nullptr};
  58. winrt::Windows::Graphics::SizeInt32 capture_frame_size_;
  59. winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
  60. d3d11_direct_device_{nullptr};
  61. winrt::com_ptr<ID3D11DeviceContext> d3d11_device_context_{nullptr};
  62. winrt::com_ptr<ID3D11Texture2D> d3d11_texture_mapped_{nullptr};
  63. std::atomic<bool> cleaned_ = false;
  64. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
  65. capture_framepool_{nullptr};
  66. winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::
  67. FrameArrived_revoker capture_framepool_trigger_;
  68. winrt::Windows::Graphics::Capture::GraphicsCaptureItem::Closed_revoker
  69. capture_close_trigger_;
  70. // message loop
  71. std::thread loop_;
  72. HWND hwnd_ = nullptr;
  73. };
  74. template <typename T>
  75. inline auto wgc_session_impl::get_dxgi_interface(
  76. winrt::Windows::Foundation::IInspectable const &object) {
  77. auto access = object.as<IDirect3DDxgiInterfaceAccess>();
  78. winrt::com_ptr<T> result;
  79. winrt::check_hresult(
  80. access->GetInterface(winrt::guid_of<T>(), result.put_void()));
  81. return result;
  82. }
  83. } // namespace am