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