blob: 9c7ecc78f46ea2e2c173416132318f0526714265 [file] [log] [blame]
sergeyu@chromium.org15e32cc2013-04-29 20:10:571/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_
12#define MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_
sergeyu@chromium.org15e32cc2013-04-29 20:10:5713
sergeyu@chromium.org3d34f662013-06-04 18:51:2314#include <stddef.h>
zijieheb68d6552016-10-29 00:35:1115#include <stdint.h>
sergeyu@chromium.org3d34f662013-06-04 18:51:2316
kwiberg84be5112016-04-27 08:19:5817#include <memory>
zijieheb68d6552016-10-29 00:35:1118#include <string>
zijiehefce49052016-11-07 23:25:1819#include <type_traits>
zijieheb68d6552016-10-29 00:35:1120#include <vector>
kwiberg84be5112016-04-27 08:19:5821
Alex Cooperb11586f2022-08-31 22:01:5122// 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"
Salman Malik24927782022-03-31 14:32:2425#if defined(WEBRTC_USE_GIO)
26#include "modules/desktop_capture/desktop_capture_metadata.h"
27#endif // defined(WEBRTC_USE_GIO)
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "modules/desktop_capture/desktop_capture_types.h"
Yves Gerey665174f2018-06-19 13:03:0529#include "modules/desktop_capture/desktop_frame.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "modules/desktop_capture/shared_memory.h"
Mirko Bonadei3d255302018-10-11 08:50:4531#include "rtc_base/system/rtc_export.h"
jiayl@webrtc.org7ee0c162014-03-26 15:57:4332
sergeyu@chromium.org15e32cc2013-04-29 20:10:5733namespace webrtc {
34
Johannes Kronb311f6a2023-02-03 11:01:5235void RTC_EXPORT LogDesktopCapturerFullscreenDetectorUsage();
36
zijieheb68d6552016-10-29 00:35:1137class DesktopCaptureOptions;
sergeyu@chromium.org15e32cc2013-04-29 20:10:5738class DesktopFrame;
sergeyu@chromium.org15e32cc2013-04-29 20:10:5739
40// Abstract interface for screen and window capturers.
Mirko Bonadei3d255302018-10-11 08:50:4541class RTC_EXPORT DesktopCapturer {
sergeyu@chromium.org15e32cc2013-04-29 20:10:5742 public:
sergeyu5d910282016-06-07 23:41:5843 enum class Result {
44 // The frame was captured successfully.
45 SUCCESS,
46
47 // There was a temporary error. The caller should continue calling
zijiehe249beee2016-10-19 06:13:2948 // CaptureFrame(), in the expectation that it will eventually recover.
sergeyu5d910282016-06-07 23:41:5849 ERROR_TEMPORARY,
50
51 // Capture has failed and will keep failing if the caller tries calling
zijiehe249beee2016-10-19 06:13:2952 // CaptureFrame() again.
sergeyu5d910282016-06-07 23:41:5853 ERROR_PERMANENT,
Sergey Ulanov4c17abe2016-06-15 20:55:4454
55 MAX_VALUE = ERROR_PERMANENT
sergeyu5d910282016-06-07 23:41:5856 };
57
sergeyu@chromium.org15e32cc2013-04-29 20:10:5758 // Interface that must be implemented by the DesktopCapturer consumers.
59 class Callback {
60 public:
Salman6ff77132023-01-27 22:58:3561 // Called before a frame capture is started.
62 virtual void OnFrameCaptureStart() {}
63
Artem Titovcec43432021-07-28 21:35:3964 // Called after a frame has been captured. `frame` is not nullptr if and
65 // only if `result` is SUCCESS.
sergeyu5d910282016-06-07 23:41:5866 virtual void OnCaptureResult(Result result,
sergeyu6ef36e72016-06-21 23:50:0067 std::unique_ptr<DesktopFrame> frame) = 0;
sergeyu@chromium.org15e32cc2013-04-29 20:10:5768
69 protected:
70 virtual ~Callback() {}
71 };
72
Joe Downing1d99f492022-01-13 15:10:1673#if defined(CHROMEOS)
74 typedef int64_t SourceId;
75#else
Brave Yaoef77ef32018-12-21 23:22:5376 typedef intptr_t SourceId;
Joe Downing1d99f492022-01-13 15:10:1677#endif
Brave Yaoef77ef32018-12-21 23:22:5378
zijiehefce49052016-11-07 23:25:1879 static_assert(std::is_same<SourceId, ScreenId>::value,
80 "SourceId should be a same type as ScreenId.");
81
zijieheb68d6552016-10-29 00:35:1182 struct Source {
83 // The unique id to represent a Source of current DesktopCapturer.
84 SourceId id;
85
86 // Title of the window or screen in UTF-8 encoding, maybe empty. This field
87 // should not be used to identify a source.
88 std::string title;
Alex Cooper0d43caa2022-09-28 16:43:5389
90#if defined(CHROMEOS)
91 // TODO(https://crbug.com/1369162): Remove or refactor this value.
92 WindowId in_process_id = kNullWindowId;
93#endif
Simon Hanglcdc769d2022-11-10 14:06:3994
95 // The display's unique ID. If no ID is defined, it will hold the value
96 // kInvalidDisplayId.
97 int64_t display_id = kInvalidDisplayId;
zijieheb68d6552016-10-29 00:35:1198 };
99
100 typedef std::vector<Source> SourceList;
101
102 virtual ~DesktopCapturer();
sergeyu@chromium.org15e32cc2013-04-29 20:10:57103
Artem Titovcec43432021-07-28 21:35:39104 // Called at the beginning of a capturing session. `callback` must remain
sergeyu@chromium.org15e32cc2013-04-29 20:10:57105 // valid until capturer is destroyed.
106 virtual void Start(Callback* callback) = 0;
107
Salman26340b02023-01-27 18:26:05108 // Sets max frame rate for the capturer. This is best effort and may not be
109 // supported by all capturers. This will only affect the frequency at which
110 // new frames are available, not the frequency at which you are allowed to
111 // capture the frames.
112 virtual void SetMaxFrameRate(uint32_t max_frame_rate) {}
113
Alex Cooper86015a62022-08-31 17:31:11114 // Returns a valid pointer if the capturer requires the user to make a
115 // selection from a source list provided by the capturer.
116 // Returns nullptr if the capturer does not provide a UI for the user to make
117 // a selection.
118 //
119 // Callers should not take ownership of the returned pointer, but it is
120 // guaranteed to be valid as long as the desktop_capturer is valid.
121 // Note that consumers should still use GetSourceList and SelectSource, but
122 // their behavior may be modified if this returns a value. See those methods
123 // for a more in-depth discussion of those potential modifications.
124 virtual DelegatedSourceListController* GetDelegatedSourceListController();
125
sergeyucc9669c2016-02-09 23:13:26126 // Sets SharedMemoryFactory that will be used to create buffers for the
127 // captured frames. The factory can be invoked on a thread other than the one
zijiehe249beee2016-10-19 06:13:29128 // where CaptureFrame() is called. It will be destroyed on the same thread.
129 // Shared memory is currently supported only by some DesktopCapturer
130 // implementations.
sergeyucc9669c2016-02-09 23:13:26131 virtual void SetSharedMemoryFactory(
zijieheb68d6552016-10-29 00:35:11132 std::unique_ptr<SharedMemoryFactory> shared_memory_factory);
sergeyucc9669c2016-02-09 23:13:26133
zijiehe91902cb2016-10-13 23:47:49134 // Captures next frame, and involve callback provided by Start() function.
135 // Pending capture requests are canceled when DesktopCapturer is deleted.
zijiehe249beee2016-10-19 06:13:29136 virtual void CaptureFrame() = 0;
jiayl@webrtc.org7ee0c162014-03-26 15:57:43137
138 // Sets the window to be excluded from the captured image in the future
139 // Capture calls. Used to exclude the screenshare notification window for
140 // screen capturing.
zijieheb68d6552016-10-29 00:35:11141 virtual void SetExcludedWindow(WindowId window);
142
143 // TODO(zijiehe): Following functions should be pure virtual. The default
144 // implementations are for backward compatibility only. Remove default
145 // implementations once all DesktopCapturer implementations in Chromium have
146 // implemented these functions.
147
148 // Gets a list of sources current capturer supports. Returns false in case of
149 // a failure.
Zijie Heb010a322017-08-07 22:25:01150 // For DesktopCapturer implementations to capture screens, this function
151 // should return monitors.
152 // For DesktopCapturer implementations to capture windows, this function
153 // should only return root windows owned by applications.
Alex Cooper86015a62022-08-31 17:31:11154 //
155 // Note that capturers who use a delegated source list will return a
156 // SourceList with exactly one value, but it may not be viable for capture
157 // (e.g. CaptureFrame will return ERROR_TEMPORARY) until a selection has been
158 // made.
zijieheb68d6552016-10-29 00:35:11159 virtual bool GetSourceList(SourceList* sources);
160
161 // Selects a source to be captured. Returns false in case of a failure (e.g.
162 // if there is no source with the specified type and id.)
Alex Cooper86015a62022-08-31 17:31:11163 //
164 // Note that some capturers with delegated source lists may also support
165 // selecting a SourceID that is not in the returned source list as a form of
166 // restore token.
zijieheb68d6552016-10-29 00:35:11167 virtual bool SelectSource(SourceId id);
168
169 // Brings the selected source to the front and sets the input focus on it.
170 // Returns false in case of a failure or no source has been selected or the
171 // implementation does not support this functionality.
172 virtual bool FocusOnSelectedSource();
zijiehe54fd5792016-11-02 21:49:35173
Artem Titovcec43432021-07-28 21:35:39174 // Returns true if the `pos` on the selected source is covered by other
Zijie He12827112017-08-29 18:19:13175 // elements on the display, and is not visible to the users.
Artem Titovcec43432021-07-28 21:35:39176 // `pos` is in full desktop coordinates, i.e. the top-left monitor always
Zijie He12827112017-08-29 18:19:13177 // starts from (0, 0).
Artem Titovcec43432021-07-28 21:35:39178 // The return value if `pos` is out of the scope of the source is undefined.
Zijie He12827112017-08-29 18:19:13179 virtual bool IsOccluded(const DesktopVector& pos);
180
zijiehe54fd5792016-11-02 21:49:35181 // Creates a DesktopCapturer instance which targets to capture windows.
182 static std::unique_ptr<DesktopCapturer> CreateWindowCapturer(
183 const DesktopCaptureOptions& options);
184
185 // Creates a DesktopCapturer instance which targets to capture screens.
186 static std::unique_ptr<DesktopCapturer> CreateScreenCapturer(
187 const DesktopCaptureOptions& options);
188
Jan Grulich0e9556a2023-07-12 11:15:40189 // Creates a DesktopCapturer instance which targets to capture windows and
190 // screens.
191 static std::unique_ptr<DesktopCapturer> CreateGenericCapturer(
192 const DesktopCaptureOptions& options);
193
Patrik Höglund0808a8c2019-12-12 10:20:07194#if defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11)
Tomas Popela318da512018-11-13 05:32:23195 static bool IsRunningUnderWayland();
Salman Malik45a22ff2022-05-26 17:50:15196
Jonas Oreland865d9c52022-05-31 08:10:49197 virtual void UpdateResolution(uint32_t width, uint32_t height) {}
Patrik Höglund0808a8c2019-12-12 10:20:07198#endif // defined(WEBRTC_USE_PIPEWIRE) || defined(WEBRTC_USE_X11)
Tomas Popela318da512018-11-13 05:32:23199
Salman Malik24927782022-03-31 14:32:24200#if defined(WEBRTC_USE_GIO)
201 // Populates implementation specific metadata into the passed in pointer.
202 // Classes can choose to override it or use the default no-op implementation.
203 virtual DesktopCaptureMetadata GetMetadata() { return {}; }
204#endif // defined(WEBRTC_USE_GIO)
205
zijiehe54fd5792016-11-02 21:49:35206 protected:
207 // CroppingWindowCapturer needs to create raw capturers without wrappers, so
208 // the following two functions are protected.
209
210 // Creates a platform specific DesktopCapturer instance which targets to
211 // capture windows.
212 static std::unique_ptr<DesktopCapturer> CreateRawWindowCapturer(
213 const DesktopCaptureOptions& options);
214
215 // Creates a platform specific DesktopCapturer instance which targets to
216 // capture screens.
217 static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer(
218 const DesktopCaptureOptions& options);
sergeyu@chromium.org15e32cc2013-04-29 20:10:57219};
220
221} // namespace webrtc
222
Mirko Bonadei92ea95e2017-09-15 04:47:31223#endif // MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_