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