desktop_capturer.h (8668B)
1 /* 2 * Copyright (c) 2013 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_DESKTOP_CAPTURER_H_ 12 #define MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <memory> 17 #include <string> 18 #include <type_traits> 19 #include <vector> 20 21 // TODO(alcooper): Update include usage in downstream consumers and then change 22 // this to a forward declaration. 23 #include "modules/desktop_capture/delegated_source_list_controller.h" 24 #include "modules/desktop_capture/desktop_capture_metadata.h" 25 #include "modules/desktop_capture/desktop_capture_types.h" 26 #include "modules/desktop_capture/shared_memory.h" 27 #include "rtc_base/system/rtc_export.h" 28 29 namespace webrtc { 30 31 void RTC_EXPORT LogDesktopCapturerFullscreenDetectorUsage(); 32 33 class DesktopCaptureOptions; 34 class DesktopFrame; 35 class DesktopVector; 36 37 // Abstract interface for screen and window capturers. 38 class RTC_EXPORT DesktopCapturer { 39 public: 40 enum class Result { 41 // The frame was captured successfully. 42 SUCCESS, 43 44 // There was a temporary error. The caller should continue calling 45 // CaptureFrame(), in the expectation that it will eventually recover. 46 ERROR_TEMPORARY, 47 48 // Capture has failed and will keep failing if the caller tries calling 49 // CaptureFrame() again. 50 ERROR_PERMANENT, 51 52 MAX_VALUE = ERROR_PERMANENT 53 }; 54 55 // Interface that must be implemented by the DesktopCapturer consumers. 56 class Callback { 57 public: 58 // Called before a frame capture is started. 59 virtual void OnFrameCaptureStart() {} 60 61 // Called after a frame has been captured. `frame` is not nullptr if and 62 // only if `result` is SUCCESS. 63 virtual void OnCaptureResult(Result result, 64 std::unique_ptr<DesktopFrame> frame) = 0; 65 66 protected: 67 virtual ~Callback() {} 68 }; 69 70 #if defined(CHROMEOS) 71 typedef int64_t SourceId; 72 #else 73 typedef intptr_t SourceId; 74 #endif 75 76 static_assert(std::is_same<SourceId, ScreenId>::value, 77 "SourceId should be a same type as ScreenId."); 78 79 struct Source { 80 // The unique id to represent a Source of current DesktopCapturer. 81 SourceId id; 82 pid_t pid = 0; 83 84 // Title of the window or screen in UTF-8 encoding, maybe empty. This field 85 // should not be used to identify a source. 86 std::string title; 87 88 #if defined(CHROMEOS) 89 // TODO(https://crbug.com/1369162): Remove or refactor this value. 90 WindowId in_process_id = kNullWindowId; 91 #endif 92 93 // The display's unique ID. If no ID is defined, it will hold the value 94 // kInvalidDisplayId. 95 int64_t display_id = kInvalidDisplayId; 96 }; 97 98 typedef std::vector<Source> SourceList; 99 100 virtual ~DesktopCapturer(); 101 102 // Called at the beginning of a capturing session. `callback` must remain 103 // valid until capturer is destroyed. 104 virtual void Start(Callback* callback) = 0; 105 106 // Sets max frame rate for the capturer. This is best effort and may not be 107 // supported by all capturers. This will only affect the frequency at which 108 // new frames are available, not the frequency at which you are allowed to 109 // capture the frames. 110 virtual void SetMaxFrameRate(uint32_t /* max_frame_rate */) {} 111 112 // Returns a valid pointer if the capturer requires the user to make a 113 // selection from a source list provided by the capturer. 114 // Returns nullptr if the capturer does not provide a UI for the user to make 115 // a selection. 116 // 117 // Callers should not take ownership of the returned pointer, but it is 118 // guaranteed to be valid as long as the desktop_capturer is valid. 119 // Note that consumers should still use GetSourceList and SelectSource, but 120 // their behavior may be modified if this returns a value. See those methods 121 // for a more in-depth discussion of those potential modifications. 122 virtual DelegatedSourceListController* GetDelegatedSourceListController(); 123 124 // Sets SharedMemoryFactory that will be used to create buffers for the 125 // captured frames. The factory can be invoked on a thread other than the one 126 // where CaptureFrame() is called. It will be destroyed on the same thread. 127 // Shared memory is currently supported only by some DesktopCapturer 128 // implementations. 129 virtual void SetSharedMemoryFactory( 130 std::unique_ptr<SharedMemoryFactory> shared_memory_factory); 131 132 // Captures next frame, and involve callback provided by Start() function. 133 // Pending capture requests are canceled when DesktopCapturer is deleted. 134 virtual void CaptureFrame() = 0; 135 136 // Sets the window to be excluded from the captured image in the future 137 // Capture calls. Used to exclude the screenshare notification window for 138 // screen capturing. 139 virtual void SetExcludedWindow(WindowId window); 140 141 // TODO(zijiehe): Following functions should be pure virtual. The default 142 // implementations are for backward compatibility only. Remove default 143 // implementations once all DesktopCapturer implementations in Chromium have 144 // implemented these functions. 145 146 // Gets a list of sources current capturer supports. Returns false in case of 147 // a failure. 148 // For DesktopCapturer implementations to capture screens, this function 149 // should return monitors. 150 // For DesktopCapturer implementations to capture windows, this function 151 // should only return root windows owned by applications. 152 // 153 // Note that capturers who use a delegated source list will return a 154 // SourceList with exactly one value, but it may not be viable for capture 155 // (e.g. CaptureFrame will return ERROR_TEMPORARY) until a selection has been 156 // made. 157 virtual bool GetSourceList(SourceList* sources); 158 159 // Selects a source to be captured. Returns false in case of a failure (e.g. 160 // if there is no source with the specified type and id.) 161 // 162 // Note that some capturers with delegated source lists may also support 163 // selecting a SourceID that is not in the returned source list as a form of 164 // restore token. 165 virtual bool SelectSource(SourceId id); 166 167 // Brings the selected source to the front and sets the input focus on it. 168 // Returns false in case of a failure or no source has been selected or the 169 // implementation does not support this functionality. 170 virtual bool FocusOnSelectedSource(); 171 172 // Returns true if the `pos` on the selected source is covered by other 173 // elements on the display, and is not visible to the users. 174 // `pos` is in full desktop coordinates, i.e. the top-left monitor always 175 // starts from (0, 0). 176 // The return value if `pos` is out of the scope of the source is undefined. 177 virtual bool IsOccluded(const DesktopVector& pos); 178 179 // Creates a DesktopCapturer instance which targets to capture windows. 180 static std::unique_ptr<DesktopCapturer> CreateWindowCapturer( 181 const DesktopCaptureOptions& options); 182 183 // Creates a DesktopCapturer instance which targets to capture screens. 184 static std::unique_ptr<DesktopCapturer> CreateScreenCapturer( 185 const DesktopCaptureOptions& options); 186 187 // Creates a DesktopCapturer instance which targets to capture windows and 188 // screens. 189 static std::unique_ptr<DesktopCapturer> CreateGenericCapturer( 190 const DesktopCaptureOptions& options); 191 192 #if defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11) 193 static bool IsRunningUnderWayland(); 194 195 virtual void UpdateResolution(uint32_t width, uint32_t height) {} 196 #endif // defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11) 197 198 #if defined(WEBRTC_USE_GIO) 199 // Populates implementation specific metadata into the passed in pointer. 200 // Classes can choose to override it or use the default no-op implementation. 201 virtual DesktopCaptureMetadata GetMetadata() { return {}; } 202 #endif // defined(WEBRTC_USE_GIO) 203 204 protected: 205 // CroppingWindowCapturer needs to create raw capturers without wrappers, so 206 // the following two functions are protected. 207 208 // Creates a platform specific DesktopCapturer instance which targets to 209 // capture windows. 210 static std::unique_ptr<DesktopCapturer> CreateRawWindowCapturer( 211 const DesktopCaptureOptions& options); 212 213 // Creates a platform specific DesktopCapturer instance which targets to 214 // capture screens. 215 static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer( 216 const DesktopCaptureOptions& options); 217 }; 218 219 } // namespace webrtc 220 221 #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_