| #include "api/video/video_frame.h" |
| 6 | #include "api/video/video_source_interface.h" |
| 7 | #include "media/base/video_adapter.h" |
| 8 | #include "modules/video_capture/video_capture.h" |
| 9 |
| 10 | #include "VideoCaptureInterface.h" |
| 11 |
| 12 | #include <memory> |
| 13 | #include <vector> |
| 14 | #include <stddef.h> |
| 15 |
| 16 | #include <d3d11.h> |
| 17 | #include <windows.graphics.directx.direct3d11.interop.h> |
| 18 | #include <windows.graphics.capture.h> |
| 19 |
| 20 | #include <winrt/Windows.Foundation.h> |
| 21 | #include <winrt/Windows.Graphics.Capture.h> |
| 22 | #include <winrt/Windows.System.h> |
| 23 |
| 24 | using namespace winrt::Windows::Graphics; |
| 25 | using namespace winrt::Windows::Graphics::Capture; |
| 26 | using namespace winrt::Windows::System; |
| 27 |
| 28 | namespace tgcalls { |
| 29 |
| 30 | class UwpScreenCapturer { |
| 31 | public: |
| 32 | explicit UwpScreenCapturer( |
| 33 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink, GraphicsCaptureItem item); |
| 34 | ~UwpScreenCapturer(); |
| 35 |
| 36 | void setState(VideoState state); |
| 37 | void setPreferredCaptureAspectRatio(float aspectRatio); |
| 38 | void setOnFatalError(std::function<void ()> error); |
| 39 | void setOnPause(std::function<void(bool)> pause); |
| 40 |
| 41 | std::pair<int, int> resolution() const; |
| 42 |
| 43 | private: |
| 44 | void create(); |
| 45 | void destroy(); |
| 46 |
| 47 | void onFatalError(); |
| 48 |
| 49 | void OnFrame(std::vector<uint8_t> bytes, int width, int height); |
| 50 |
| 51 | bool item_closed_; |
| 52 | bool is_capture_started_; |
| 53 | SizeInt32 previous_size_; |
| 54 | Direct3D11CaptureFramePool frame_pool_ = nullptr; |
| 55 | GraphicsCaptureItem item_; |
| 56 | GraphicsCaptureSession session_ = nullptr; |
| 57 | winrt::com_ptr<ID3D11Device> d3d11_device_; |
| 58 | winrt::com_ptr<IInspectable> direct3d_device_; |
| 59 | winrt::com_ptr<ID3D11Texture2D> mapped_texture_ = nullptr; |
| 60 | winrt::slim_mutex lock_; |
| 61 | DispatcherQueue queue_= nullptr; |
| 62 | DispatcherQueueController queueController_= nullptr; |
| 63 | DispatcherQueueTimer repeatingTimer_= nullptr; |
| 64 | HRESULT CreateMappedTexture(winrt::com_ptr<ID3D11Texture2D> src_texture, UINT width = 0, UINT height = 0); |
| 65 |
| 66 | //void OnFrameArrived(Direct3D11CaptureFramePool const& sender, winrt::Windows::Foundation::IInspectable const& args); |
| 67 | void OnFrameArrived(DispatcherQueueTimer const& sender, winrt::Windows::Foundation::IInspectable const& args); |
| 68 | void OnClosed(GraphicsCaptureItem const& sender, winrt::Windows::Foundation::IInspectable const& args); |
| 69 |
| 70 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _sink; |
| 71 |
| 72 | VideoState _state = VideoState::Inactive; |
| 73 | std::pair<int, int> _dimensions; |
| 74 | std::function<void()> _onFatalError; |
| 75 | bool _fatalError = false; |
| 76 | std::function<void(bool)> _onPause; |
| 77 | bool _paused = false; |
| 78 | float _aspectRatio = 0.; |
| 79 | }; |
| 80 |
| 81 | } // namespace tgcalls |
| 82 |
| 83 | #endif |
| 84 |