wgc_capture_source.h (4799B)
1 /* 2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_DESKTOP_CAPTURE_WIN_WGC_CAPTURE_SOURCE_H_ 12 #define MODULES_DESKTOP_CAPTURE_WIN_WGC_CAPTURE_SOURCE_H_ 13 14 #include <windows.graphics.capture.h> 15 #include <windows.graphics.h> 16 #include <wrl/client.h> 17 18 #include <memory> 19 #include <optional> 20 21 #include "modules/desktop_capture/desktop_capturer.h" 22 #include "modules/desktop_capture/desktop_geometry.h" 23 24 namespace webrtc { 25 26 // Abstract class to represent the source that WGC-based capturers capture 27 // from. Could represent an application window or a screen. Consumers should use 28 // the appropriate Wgc*SourceFactory class to create WgcCaptureSource objects 29 // of the appropriate type. 30 class WgcCaptureSource { 31 public: 32 explicit WgcCaptureSource(DesktopCapturer::SourceId source_id); 33 virtual ~WgcCaptureSource(); 34 35 virtual DesktopVector GetTopLeft() = 0; 36 // Lightweight version of IsCapturable which avoids allocating/deallocating 37 // COM objects for each call. As such may return a different value than 38 // IsCapturable. 39 virtual bool ShouldBeCapturable(); 40 virtual bool IsCapturable(); 41 virtual bool FocusOnSource(); 42 virtual ABI::Windows::Graphics::SizeInt32 GetSize(); 43 HRESULT GetCaptureItem( 44 Microsoft::WRL::ComPtr< 45 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result); 46 DesktopCapturer::SourceId GetSourceId() { return source_id_; } 47 48 protected: 49 virtual HRESULT CreateCaptureItem( 50 Microsoft::WRL::ComPtr< 51 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) = 0; 52 53 private: 54 Microsoft::WRL::ComPtr<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem> 55 item_; 56 const DesktopCapturer::SourceId source_id_; 57 }; 58 59 class WgcCaptureSourceFactory { 60 public: 61 virtual ~WgcCaptureSourceFactory(); 62 63 virtual std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 64 DesktopCapturer::SourceId) = 0; 65 }; 66 67 class WgcWindowSourceFactory final : public WgcCaptureSourceFactory { 68 public: 69 WgcWindowSourceFactory(); 70 71 // Disallow copy and assign. 72 WgcWindowSourceFactory(const WgcWindowSourceFactory&) = delete; 73 WgcWindowSourceFactory& operator=(const WgcWindowSourceFactory&) = delete; 74 75 ~WgcWindowSourceFactory() override; 76 77 std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 78 DesktopCapturer::SourceId) override; 79 }; 80 81 class WgcScreenSourceFactory final : public WgcCaptureSourceFactory { 82 public: 83 WgcScreenSourceFactory(); 84 85 WgcScreenSourceFactory(const WgcScreenSourceFactory&) = delete; 86 WgcScreenSourceFactory& operator=(const WgcScreenSourceFactory&) = delete; 87 88 ~WgcScreenSourceFactory() override; 89 90 std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 91 DesktopCapturer::SourceId) override; 92 }; 93 94 // Class for capturing application windows. 95 class WgcWindowSource final : public WgcCaptureSource { 96 public: 97 explicit WgcWindowSource(DesktopCapturer::SourceId source_id); 98 99 WgcWindowSource(const WgcWindowSource&) = delete; 100 WgcWindowSource& operator=(const WgcWindowSource&) = delete; 101 102 ~WgcWindowSource() override; 103 104 DesktopVector GetTopLeft() override; 105 ABI::Windows::Graphics::SizeInt32 GetSize() override; 106 bool ShouldBeCapturable() override; 107 bool IsCapturable() override; 108 bool FocusOnSource() override; 109 110 private: 111 HRESULT CreateCaptureItem( 112 Microsoft::WRL::ComPtr< 113 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) 114 override; 115 }; 116 117 // Class for capturing screens/monitors/displays. 118 class WgcScreenSource final : public WgcCaptureSource { 119 public: 120 explicit WgcScreenSource(DesktopCapturer::SourceId source_id); 121 122 WgcScreenSource(const WgcScreenSource&) = delete; 123 WgcScreenSource& operator=(const WgcScreenSource&) = delete; 124 125 ~WgcScreenSource() override; 126 127 DesktopVector GetTopLeft() override; 128 ABI::Windows::Graphics::SizeInt32 GetSize() override; 129 bool IsCapturable() override; 130 131 private: 132 HRESULT CreateCaptureItem( 133 Microsoft::WRL::ComPtr< 134 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) 135 override; 136 137 // To maintain compatibility with other capturers, this class accepts a 138 // device index as it's SourceId. However, WGC requires we use an HMONITOR to 139 // describe which screen to capture. So, we internally convert the supplied 140 // device index into an HMONITOR when `IsCapturable()` is called. 141 std::optional<HMONITOR> hmonitor_; 142 }; 143 144 } // namespace webrtc 145 146 #endif // MODULES_DESKTOP_CAPTURE_WIN_WGC_CAPTURE_SOURCE_H_