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 20 #include "absl/types/optional.h" 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 virtual bool IsCapturable(); 37 virtual bool FocusOnSource(); 38 virtual ABI::Windows::Graphics::SizeInt32 GetSize(); 39 HRESULT GetCaptureItem( 40 Microsoft::WRL::ComPtr< 41 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result); GetSourceId()42 DesktopCapturer::SourceId GetSourceId() { return source_id_; } 43 44 protected: 45 virtual HRESULT CreateCaptureItem( 46 Microsoft::WRL::ComPtr< 47 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) = 0; 48 49 private: 50 Microsoft::WRL::ComPtr<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem> 51 item_; 52 const DesktopCapturer::SourceId source_id_; 53 }; 54 55 class WgcCaptureSourceFactory { 56 public: 57 virtual ~WgcCaptureSourceFactory(); 58 59 virtual std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 60 DesktopCapturer::SourceId) = 0; 61 }; 62 63 class WgcWindowSourceFactory final : public WgcCaptureSourceFactory { 64 public: 65 WgcWindowSourceFactory(); 66 67 // Disallow copy and assign. 68 WgcWindowSourceFactory(const WgcWindowSourceFactory&) = delete; 69 WgcWindowSourceFactory& operator=(const WgcWindowSourceFactory&) = delete; 70 71 ~WgcWindowSourceFactory() override; 72 73 std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 74 DesktopCapturer::SourceId) override; 75 }; 76 77 class WgcScreenSourceFactory final : public WgcCaptureSourceFactory { 78 public: 79 WgcScreenSourceFactory(); 80 81 WgcScreenSourceFactory(const WgcScreenSourceFactory&) = delete; 82 WgcScreenSourceFactory& operator=(const WgcScreenSourceFactory&) = delete; 83 84 ~WgcScreenSourceFactory() override; 85 86 std::unique_ptr<WgcCaptureSource> CreateCaptureSource( 87 DesktopCapturer::SourceId) override; 88 }; 89 90 // Class for capturing application windows. 91 class WgcWindowSource final : public WgcCaptureSource { 92 public: 93 explicit WgcWindowSource(DesktopCapturer::SourceId source_id); 94 95 WgcWindowSource(const WgcWindowSource&) = delete; 96 WgcWindowSource& operator=(const WgcWindowSource&) = delete; 97 98 ~WgcWindowSource() override; 99 100 DesktopVector GetTopLeft() override; 101 ABI::Windows::Graphics::SizeInt32 GetSize() override; 102 bool IsCapturable() override; 103 bool FocusOnSource() override; 104 105 private: 106 HRESULT CreateCaptureItem( 107 Microsoft::WRL::ComPtr< 108 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) 109 override; 110 }; 111 112 // Class for capturing screens/monitors/displays. 113 class WgcScreenSource final : public WgcCaptureSource { 114 public: 115 explicit WgcScreenSource(DesktopCapturer::SourceId source_id); 116 117 WgcScreenSource(const WgcScreenSource&) = delete; 118 WgcScreenSource& operator=(const WgcScreenSource&) = delete; 119 120 ~WgcScreenSource() override; 121 122 DesktopVector GetTopLeft() override; 123 ABI::Windows::Graphics::SizeInt32 GetSize() override; 124 bool IsCapturable() override; 125 126 private: 127 HRESULT CreateCaptureItem( 128 Microsoft::WRL::ComPtr< 129 ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>* result) 130 override; 131 132 // To maintain compatibility with other capturers, this class accepts a 133 // device index as it's SourceId. However, WGC requires we use an HMONITOR to 134 // describe which screen to capture. So, we internally convert the supplied 135 // device index into an HMONITOR when `IsCapturable()` is called. 136 absl::optional<HMONITOR> hmonitor_; 137 }; 138 139 } // namespace webrtc 140 141 #endif // MODULES_DESKTOP_CAPTURE_WIN_WGC_CAPTURE_SOURCE_H_ 142