Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Side by Side Diff: webrtc/modules/desktop_capture/screen_capturer_x11.cc

Issue 1861893002: Fix screen capturers to initialize on the same thread on which Start() is called. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/desktop_capture/screen_capturer.h" 11 #include "webrtc/modules/desktop_capture/screen_capturer.h"
12 12
13 #include <string.h> 13 #include <string.h>
14 14
15 #include <memory> 15 #include <memory>
16 #include <set> 16 #include <set>
17 17
18 #include <X11/extensions/Xdamage.h> 18 #include <X11/extensions/Xdamage.h>
19 #include <X11/extensions/Xfixes.h> 19 #include <X11/extensions/Xfixes.h>
20 #include <X11/Xlib.h> 20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h> 21 #include <X11/Xutil.h>
22 22
23 #include "webrtc/base/checks.h" 23 #include "webrtc/base/checks.h"
24 #include "webrtc/base/thread_checker.h"
24 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" 25 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
25 #include "webrtc/modules/desktop_capture/desktop_frame.h" 26 #include "webrtc/modules/desktop_capture/desktop_frame.h"
26 #include "webrtc/modules/desktop_capture/differ.h" 27 #include "webrtc/modules/desktop_capture/differ.h"
27 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" 28 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
28 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" 29 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
29 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" 30 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
30 #include "webrtc/system_wrappers/include/logging.h" 31 #include "webrtc/system_wrappers/include/logging.h"
31 #include "webrtc/system_wrappers/include/tick_util.h" 32 #include "webrtc/system_wrappers/include/tick_util.h"
32 33
33 namespace webrtc { 34 namespace webrtc {
34 namespace { 35 namespace {
35 36
36 // A class to perform video frame capturing for Linux. 37 // A class to perform video frame capturing for Linux.
37 class ScreenCapturerLinux : public ScreenCapturer, 38 class ScreenCapturerLinux : public ScreenCapturer,
38 public SharedXDisplay::XEventHandler { 39 public SharedXDisplay::XEventHandler {
39 public: 40 public:
40 ScreenCapturerLinux(); 41 explicit ScreenCapturerLinux(const DesktopCaptureOptions& options);
41 virtual ~ScreenCapturerLinux(); 42 virtual ~ScreenCapturerLinux();
42 43
43 // TODO(ajwong): Do we really want this to be synchronous?
44 bool Init(const DesktopCaptureOptions& options);
45
46 // DesktopCapturer interface. 44 // DesktopCapturer interface.
47 void Start(Callback* delegate) override; 45 void Start(Callback* delegate) override;
48 void Capture(const DesktopRegion& region) override; 46 void Capture(const DesktopRegion& region) override;
49 47
50 // ScreenCapturer interface. 48 // ScreenCapturer interface.
51 bool GetScreenList(ScreenList* screens) override; 49 bool GetScreenList(ScreenList* screens) override;
52 bool SelectScreen(ScreenId id) override; 50 bool SelectScreen(ScreenId id) override;
53 51
54 private: 52 private:
55 Display* display() { return options_.x_display()->display(); } 53 Display* display() { return options_.x_display()->display(); }
56 54
55 bool Initialize();
56
57 void InitXDamage();
58
57 // SharedXDisplay::XEventHandler interface. 59 // SharedXDisplay::XEventHandler interface.
58 bool HandleXEvent(const XEvent& event) override; 60 bool HandleXEvent(const XEvent& event) override;
59 61
60 void InitXDamage();
61
62 // Capture screen pixels to the current buffer in the queue. In the DAMAGE 62 // Capture screen pixels to the current buffer in the queue. In the DAMAGE
63 // case, the ScreenCapturerHelper already holds the list of invalid rectangles 63 // case, the ScreenCapturerHelper already holds the list of invalid rectangles
64 // from HandleXEvent(). In the non-DAMAGE case, this captures the 64 // from HandleXEvent(). In the non-DAMAGE case, this captures the
65 // whole screen, then calculates some invalid rectangles that include any 65 // whole screen, then calculates some invalid rectangles that include any
66 // differences between this and the previous capture. 66 // differences between this and the previous capture.
67 DesktopFrame* CaptureScreen(); 67 DesktopFrame* CaptureScreen();
68 68
69 // Called when the screen configuration is changed. 69 // Called when the screen configuration is changed.
70 void ScreenConfigurationChanged(); 70 void ScreenConfigurationChanged();
71 71
72 // Synchronize the current buffer with |last_buffer_|, by copying pixels from 72 // Synchronize the current buffer with |last_buffer_|, by copying pixels from
73 // the area of |last_invalid_rects|. 73 // the area of |last_invalid_rects|.
74 // Note this only works on the assumption that kNumBuffers == 2, as 74 // Note this only works on the assumption that kNumBuffers == 2, as
75 // |last_invalid_rects| holds the differences from the previous buffer and 75 // |last_invalid_rects| holds the differences from the previous buffer and
76 // the one prior to that (which will then be the current buffer). 76 // the one prior to that (which will then be the current buffer).
77 void SynchronizeFrame(); 77 void SynchronizeFrame();
78 78
79 void DeinitXlib(); 79 void DeinitXlib();
80 80
81 rtc::ThreadChecker thread_checker_;
82
81 DesktopCaptureOptions options_; 83 DesktopCaptureOptions options_;
82 84
83 Callback* callback_; 85 Callback* callback_ = nullptr;
84 86
85 // X11 graphics context. 87 // X11 graphics context.
86 GC gc_; 88 GC gc_ = nullptr;
87 Window root_window_; 89 Window root_window_ = BadValue;
88 90
89 // XFixes. 91 // XFixes.
90 bool has_xfixes_; 92 bool has_xfixes_ = false;
91 int xfixes_event_base_; 93 int xfixes_event_base_ = -1;
92 int xfixes_error_base_; 94 int xfixes_error_base_ = -1;
93 95
94 // XDamage information. 96 // XDamage information.
95 bool use_damage_; 97 bool use_damage_ = false;
96 Damage damage_handle_; 98 Damage damage_handle_ = 0;
97 int damage_event_base_; 99 int damage_event_base_ = -1;
98 int damage_error_base_; 100 int damage_error_base_ = -1;
99 XserverRegion damage_region_; 101 XserverRegion damage_region_ = 0;
100 102
101 // Access to the X Server's pixel buffer. 103 // Access to the X Server's pixel buffer.
102 XServerPixelBuffer x_server_pixel_buffer_; 104 XServerPixelBuffer x_server_pixel_buffer_;
103 105
104 // A thread-safe list of invalid rectangles, and the size of the most 106 // A thread-safe list of invalid rectangles, and the size of the most
105 // recently captured screen. 107 // recently captured screen.
106 ScreenCapturerHelper helper_; 108 ScreenCapturerHelper helper_;
107 109
108 // Queue of the frames buffers. 110 // Queue of the frames buffers.
109 ScreenCaptureFrameQueue queue_; 111 ScreenCaptureFrameQueue queue_;
110 112
111 // Invalid region from the previous capture. This is used to synchronize the 113 // Invalid region from the previous capture. This is used to synchronize the
112 // current with the last buffer used. 114 // current with the last buffer used.
113 DesktopRegion last_invalid_region_; 115 DesktopRegion last_invalid_region_;
114 116
115 // |Differ| for use when polling for changes. 117 // |Differ| for use when polling for changes.
116 std::unique_ptr<Differ> differ_; 118 std::unique_ptr<Differ> differ_;
117 119
118 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); 120 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux);
119 }; 121 };
120 122
121 ScreenCapturerLinux::ScreenCapturerLinux() 123 ScreenCapturerLinux::ScreenCapturerLinux(const DesktopCaptureOptions& options)
122 : callback_(NULL), 124 : options_(options) {
123 gc_(NULL), 125 // ScreenCapturer can be used on a thread different from the thread on which
124 root_window_(BadValue), 126 // it's created.
125 has_xfixes_(false), 127 thread_checker_.DetachFromThread();
126 xfixes_event_base_(-1),
127 xfixes_error_base_(-1),
128 use_damage_(false),
129 damage_handle_(0),
130 damage_event_base_(-1),
131 damage_error_base_(-1),
132 damage_region_(0) {
133 helper_.SetLogGridSize(4);
134 } 128 }
135 129
136 ScreenCapturerLinux::~ScreenCapturerLinux() { 130 ScreenCapturerLinux::~ScreenCapturerLinux() {
137 options_.x_display()->RemoveEventHandler(ConfigureNotify, this); 131 options_.x_display()->RemoveEventHandler(ConfigureNotify, this);
138 if (use_damage_) { 132 if (use_damage_) {
139 options_.x_display()->RemoveEventHandler( 133 options_.x_display()->RemoveEventHandler(
140 damage_event_base_ + XDamageNotify, this); 134 damage_event_base_ + XDamageNotify, this);
141 } 135 }
142 DeinitXlib(); 136 DeinitXlib();
143 } 137 }
144 138
145 bool ScreenCapturerLinux::Init(const DesktopCaptureOptions& options) { 139 bool ScreenCapturerLinux::Initialize() {
146 options_ = options; 140 helper_.SetLogGridSize(4);
147 141
148 root_window_ = RootWindow(display(), DefaultScreen(display())); 142 root_window_ = RootWindow(display(), DefaultScreen(display()));
149 if (root_window_ == BadValue) { 143 if (root_window_ == BadValue) {
150 LOG(LS_ERROR) << "Unable to get the root window"; 144 LOG(LS_ERROR) << "Unable to get the root window";
151 DeinitXlib(); 145 DeinitXlib();
152 return false; 146 return false;
153 } 147 }
154 148
155 gc_ = XCreateGC(display(), root_window_, 0, NULL); 149 gc_ = XCreateGC(display(), root_window_, 0, NULL);
156 if (gc_ == NULL) { 150 if (gc_ == NULL) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 return; 185 return;
192 } 186 }
193 187
194 // Check for XDamage extension. 188 // Check for XDamage extension.
195 if (!XDamageQueryExtension(display(), &damage_event_base_, 189 if (!XDamageQueryExtension(display(), &damage_event_base_,
196 &damage_error_base_)) { 190 &damage_error_base_)) {
197 LOG(LS_INFO) << "X server does not support XDamage."; 191 LOG(LS_INFO) << "X server does not support XDamage.";
198 return; 192 return;
199 } 193 }
200 194
201 // TODO(lambroslambrou): Disable DAMAGE in situations where it is known
202 // to fail, such as when Desktop Effects are enabled, with graphics
203 // drivers (nVidia, ATI) that fail to report DAMAGE notifications
204 // properly.
205
206 // Request notifications every time the screen becomes damaged. 195 // Request notifications every time the screen becomes damaged.
207 damage_handle_ = XDamageCreate(display(), root_window_, 196 damage_handle_ = XDamageCreate(display(), root_window_,
208 XDamageReportNonEmpty); 197 XDamageReportNonEmpty);
209 if (!damage_handle_) { 198 if (!damage_handle_) {
210 LOG(LS_ERROR) << "Unable to initialize XDamage."; 199 LOG(LS_ERROR) << "Unable to initialize XDamage.";
211 return; 200 return;
212 } 201 }
213 202
214 // Create an XFixes server-side region to collate damage into. 203 // Create an XFixes server-side region to collate damage into.
215 damage_region_ = XFixesCreateRegion(display(), 0, 0); 204 damage_region_ = XFixesCreateRegion(display(), 0, 0);
216 if (!damage_region_) { 205 if (!damage_region_) {
217 XDamageDestroy(display(), damage_handle_); 206 XDamageDestroy(display(), damage_handle_);
218 LOG(LS_ERROR) << "Unable to create XFixes region."; 207 LOG(LS_ERROR) << "Unable to create XFixes region.";
219 return; 208 return;
220 } 209 }
221 210
222 options_.x_display()->AddEventHandler( 211 options_.x_display()->AddEventHandler(
223 damage_event_base_ + XDamageNotify, this); 212 damage_event_base_ + XDamageNotify, this);
224 213
225 use_damage_ = true; 214 use_damage_ = true;
226 LOG(LS_INFO) << "Using XDamage extension."; 215 LOG(LS_INFO) << "Using XDamage extension.";
227 } 216 }
228 217
229 void ScreenCapturerLinux::Start(Callback* callback) { 218 void ScreenCapturerLinux::Start(Callback* callback) {
219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
230 RTC_DCHECK(!callback_); 220 RTC_DCHECK(!callback_);
231 RTC_DCHECK(callback); 221 RTC_DCHECK(callback);
232 222
233 callback_ = callback; 223 callback_ = callback;
224
225 if (!Initialize()) {
226 callback_->OnInitializationFailed();
227 }
234 } 228 }
235 229
236 void ScreenCapturerLinux::Capture(const DesktopRegion& region) { 230 void ScreenCapturerLinux::Capture(const DesktopRegion& region) {
231 RTC_DCHECK(thread_checker_.CalledOnValidThread());
232
237 TickTime capture_start_time = TickTime::Now(); 233 TickTime capture_start_time = TickTime::Now();
238 234
239 queue_.MoveToNextFrame(); 235 queue_.MoveToNextFrame();
240 236
241 // Process XEvents for XDamage and cursor shape tracking. 237 // Process XEvents for XDamage and cursor shape tracking.
242 options_.x_display()->ProcessPendingXEvents(); 238 options_.x_display()->ProcessPendingXEvents();
243 239
244 // ProcessPendingXEvents() may call ScreenConfigurationChanged() which 240 // ProcessPendingXEvents() may call ScreenConfigurationChanged() which
245 // reinitializes |x_server_pixel_buffer_|. Check if the pixel buffer is still 241 // reinitializes |x_server_pixel_buffer_|. Check if the pixel buffer is still
246 // in a good shape. 242 // in a good shape.
(...skipping 25 matching lines...) Expand all
272 } 268 }
273 269
274 DesktopFrame* result = CaptureScreen(); 270 DesktopFrame* result = CaptureScreen();
275 last_invalid_region_ = result->updated_region(); 271 last_invalid_region_ = result->updated_region();
276 result->set_capture_time_ms( 272 result->set_capture_time_ms(
277 (TickTime::Now() - capture_start_time).Milliseconds()); 273 (TickTime::Now() - capture_start_time).Milliseconds());
278 callback_->OnCaptureCompleted(result); 274 callback_->OnCaptureCompleted(result);
279 } 275 }
280 276
281 bool ScreenCapturerLinux::GetScreenList(ScreenList* screens) { 277 bool ScreenCapturerLinux::GetScreenList(ScreenList* screens) {
278 RTC_DCHECK(thread_checker_.CalledOnValidThread());
282 RTC_DCHECK(screens->size() == 0); 279 RTC_DCHECK(screens->size() == 0);
280
283 // TODO(jiayl): implement screen enumeration. 281 // TODO(jiayl): implement screen enumeration.
284 Screen default_screen; 282 Screen default_screen;
285 default_screen.id = 0; 283 default_screen.id = 0;
286 screens->push_back(default_screen); 284 screens->push_back(default_screen);
287 return true; 285 return true;
288 } 286 }
289 287
290 bool ScreenCapturerLinux::SelectScreen(ScreenId id) { 288 bool ScreenCapturerLinux::SelectScreen(ScreenId id) {
289 RTC_DCHECK(thread_checker_.CalledOnValidThread());
291 // TODO(jiayl): implement screen selection. 290 // TODO(jiayl): implement screen selection.
292 return true; 291 return true;
293 } 292 }
294 293
295 bool ScreenCapturerLinux::HandleXEvent(const XEvent& event) { 294 bool ScreenCapturerLinux::HandleXEvent(const XEvent& event) {
295 RTC_DCHECK(thread_checker_.CalledOnValidThread());
296
296 if (use_damage_ && (event.type == damage_event_base_ + XDamageNotify)) { 297 if (use_damage_ && (event.type == damage_event_base_ + XDamageNotify)) {
297 const XDamageNotifyEvent* damage_event = 298 const XDamageNotifyEvent* damage_event =
298 reinterpret_cast<const XDamageNotifyEvent*>(&event); 299 reinterpret_cast<const XDamageNotifyEvent*>(&event);
299 if (damage_event->damage != damage_handle_) 300 if (damage_event->damage != damage_handle_)
300 return false; 301 return false;
301 RTC_DCHECK(damage_event->level == XDamageReportNonEmpty); 302 RTC_DCHECK(damage_event->level == XDamageReportNonEmpty);
302 return true; 303 return true;
303 } else if (event.type == ConfigureNotify) { 304 } else if (event.type == ConfigureNotify) {
304 ScreenConfigurationChanged(); 305 ScreenConfigurationChanged();
305 return true; 306 return true;
306 } 307 }
307 return false; 308 return false;
308 } 309 }
309 310
310 DesktopFrame* ScreenCapturerLinux::CaptureScreen() { 311 DesktopFrame* ScreenCapturerLinux::CaptureScreen() {
311 DesktopFrame* frame = queue_.current_frame()->Share(); 312 DesktopFrame* frame = queue_.current_frame()->Share();
312 assert(x_server_pixel_buffer_.window_size().equals(frame->size())); 313 RTC_DCHECK(x_server_pixel_buffer_.window_size().equals(frame->size()));
313 314
314 // Pass the screen size to the helper, so it can clip the invalid region if it 315 // Pass the screen size to the helper, so it can clip the invalid region if it
315 // expands that region to a grid. 316 // expands that region to a grid.
316 helper_.set_size_most_recent(frame->size()); 317 helper_.set_size_most_recent(frame->size());
317 318
318 // In the DAMAGE case, ensure the frame is up-to-date with the previous frame 319 // In the DAMAGE case, ensure the frame is up-to-date with the previous frame
319 // if any. If there isn't a previous frame, that means a screen-resolution 320 // if any. If there isn't a previous frame, that means a screen-resolution
320 // change occurred, and |invalid_rects| will be updated to include the whole 321 // change occurred, and |invalid_rects| will be updated to include the whole
321 // screen. 322 // screen.
322 if (use_damage_ && queue_.previous_frame()) 323 if (use_damage_ && queue_.previous_frame())
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 } 430 }
430 } 431 }
431 432
432 } // namespace 433 } // namespace
433 434
434 // static 435 // static
435 ScreenCapturer* ScreenCapturer::Create(const DesktopCaptureOptions& options) { 436 ScreenCapturer* ScreenCapturer::Create(const DesktopCaptureOptions& options) {
436 if (!options.x_display()) 437 if (!options.x_display())
437 return NULL; 438 return NULL;
438 439
439 std::unique_ptr<ScreenCapturerLinux> capturer(new ScreenCapturerLinux()); 440 return new ScreenCapturerLinux(options);
440 if (!capturer->Init(options))
441 capturer.reset();
442 return capturer.release();
443 } 441 }
444 442
445 } // namespace webrtc 443 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698