OLD | NEW |
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/modules/desktop_capture/desktop_capture_options.h" | 24 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
25 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 25 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
26 #include "webrtc/modules/desktop_capture/differ.h" | 26 #include "webrtc/modules/desktop_capture/differ.h" |
27 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" | 27 #include "webrtc/modules/desktop_capture/screen_capture_queue.h" |
28 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" | 28 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" |
| 29 #include "webrtc/modules/desktop_capture/shared_desktop_frame.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 { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 XserverRegion damage_region_; | 100 XserverRegion damage_region_; |
100 | 101 |
101 // Access to the X Server's pixel buffer. | 102 // Access to the X Server's pixel buffer. |
102 XServerPixelBuffer x_server_pixel_buffer_; | 103 XServerPixelBuffer x_server_pixel_buffer_; |
103 | 104 |
104 // A thread-safe list of invalid rectangles, and the size of the most | 105 // A thread-safe list of invalid rectangles, and the size of the most |
105 // recently captured screen. | 106 // recently captured screen. |
106 ScreenCapturerHelper helper_; | 107 ScreenCapturerHelper helper_; |
107 | 108 |
108 // Queue of the frames buffers. | 109 // Queue of the frames buffers. |
109 ScreenCaptureFrameQueue queue_; | 110 ScreenCaptureQueue<SharedDesktopFrame> queue_; |
110 | 111 |
111 // Invalid region from the previous capture. This is used to synchronize the | 112 // Invalid region from the previous capture. This is used to synchronize the |
112 // current with the last buffer used. | 113 // current with the last buffer used. |
113 DesktopRegion last_invalid_region_; | 114 DesktopRegion last_invalid_region_; |
114 | 115 |
115 // |Differ| for use when polling for changes. | 116 // |Differ| for use when polling for changes. |
116 std::unique_ptr<Differ> differ_; | 117 std::unique_ptr<Differ> differ_; |
117 | 118 |
118 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); | 119 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); |
119 }; | 120 }; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 void ScreenCapturerLinux::Start(Callback* callback) { | 230 void ScreenCapturerLinux::Start(Callback* callback) { |
230 RTC_DCHECK(!callback_); | 231 RTC_DCHECK(!callback_); |
231 RTC_DCHECK(callback); | 232 RTC_DCHECK(callback); |
232 | 233 |
233 callback_ = callback; | 234 callback_ = callback; |
234 } | 235 } |
235 | 236 |
236 void ScreenCapturerLinux::Capture(const DesktopRegion& region) { | 237 void ScreenCapturerLinux::Capture(const DesktopRegion& region) { |
237 TickTime capture_start_time = TickTime::Now(); | 238 TickTime capture_start_time = TickTime::Now(); |
238 | 239 |
239 queue_.MoveToNextFrame(); | 240 queue_.MoveToNext(); |
| 241 RTC_DCHECK(!queue_.current() || !queue_.current()->IsShared()); |
240 | 242 |
241 // Process XEvents for XDamage and cursor shape tracking. | 243 // Process XEvents for XDamage and cursor shape tracking. |
242 options_.x_display()->ProcessPendingXEvents(); | 244 options_.x_display()->ProcessPendingXEvents(); |
243 | 245 |
244 // ProcessPendingXEvents() may call ScreenConfigurationChanged() which | 246 // ProcessPendingXEvents() may call ScreenConfigurationChanged() which |
245 // reinitializes |x_server_pixel_buffer_|. Check if the pixel buffer is still | 247 // reinitializes |x_server_pixel_buffer_|. Check if the pixel buffer is still |
246 // in a good shape. | 248 // in a good shape. |
247 if (!x_server_pixel_buffer_.is_initialized()) { | 249 if (!x_server_pixel_buffer_.is_initialized()) { |
248 // We failed to initialize pixel buffer. | 250 // We failed to initialize pixel buffer. |
249 callback_->OnCaptureCompleted(NULL); | 251 callback_->OnCaptureCompleted(NULL); |
250 return; | 252 return; |
251 } | 253 } |
252 | 254 |
253 // If the current frame is from an older generation then allocate a new one. | 255 // If the current frame is from an older generation then allocate a new one. |
254 // Note that we can't reallocate other buffers at this point, since the caller | 256 // Note that we can't reallocate other buffers at this point, since the caller |
255 // may still be reading from them. | 257 // may still be reading from them. |
256 if (!queue_.current_frame()) { | 258 if (!queue_.current()) { |
257 std::unique_ptr<DesktopFrame> frame( | 259 std::unique_ptr<DesktopFrame> frame( |
258 new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); | 260 new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); |
259 queue_.ReplaceCurrentFrame(frame.release()); | 261 queue_.ReplaceCurrent(SharedDesktopFrame::Wrap(std::move(frame))); |
260 } | 262 } |
261 | 263 |
262 // Refresh the Differ helper used by CaptureFrame(), if needed. | 264 // Refresh the Differ helper used by CaptureFrame(), if needed. |
263 DesktopFrame* frame = queue_.current_frame(); | 265 DesktopFrame* frame = queue_.current(); |
264 if (!use_damage_ && ( | 266 if (!use_damage_ && ( |
265 !differ_.get() || | 267 !differ_.get() || |
266 (differ_->width() != frame->size().width()) || | 268 (differ_->width() != frame->size().width()) || |
267 (differ_->height() != frame->size().height()) || | 269 (differ_->height() != frame->size().height()) || |
268 (differ_->bytes_per_row() != frame->stride()))) { | 270 (differ_->bytes_per_row() != frame->stride()))) { |
269 differ_.reset(new Differ(frame->size().width(), frame->size().height(), | 271 differ_.reset(new Differ(frame->size().width(), frame->size().height(), |
270 DesktopFrame::kBytesPerPixel, | 272 DesktopFrame::kBytesPerPixel, |
271 frame->stride())); | 273 frame->stride())); |
272 } | 274 } |
273 | 275 |
(...skipping 27 matching lines...) Expand all Loading... |
301 RTC_DCHECK(damage_event->level == XDamageReportNonEmpty); | 303 RTC_DCHECK(damage_event->level == XDamageReportNonEmpty); |
302 return true; | 304 return true; |
303 } else if (event.type == ConfigureNotify) { | 305 } else if (event.type == ConfigureNotify) { |
304 ScreenConfigurationChanged(); | 306 ScreenConfigurationChanged(); |
305 return true; | 307 return true; |
306 } | 308 } |
307 return false; | 309 return false; |
308 } | 310 } |
309 | 311 |
310 DesktopFrame* ScreenCapturerLinux::CaptureScreen() { | 312 DesktopFrame* ScreenCapturerLinux::CaptureScreen() { |
311 DesktopFrame* frame = queue_.current_frame()->Share(); | 313 DesktopFrame* frame = queue_.current()->Share(); |
312 assert(x_server_pixel_buffer_.window_size().equals(frame->size())); | 314 assert(x_server_pixel_buffer_.window_size().equals(frame->size())); |
313 | 315 |
314 // Pass the screen size to the helper, so it can clip the invalid region if it | 316 // Pass the screen size to the helper, so it can clip the invalid region if it |
315 // expands that region to a grid. | 317 // expands that region to a grid. |
316 helper_.set_size_most_recent(frame->size()); | 318 helper_.set_size_most_recent(frame->size()); |
317 | 319 |
318 // In the DAMAGE case, ensure the frame is up-to-date with the previous frame | 320 // 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 | 321 // 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 | 322 // change occurred, and |invalid_rects| will be updated to include the whole |
321 // screen. | 323 // screen. |
322 if (use_damage_ && queue_.previous_frame()) | 324 if (use_damage_ && queue_.previous()) |
323 SynchronizeFrame(); | 325 SynchronizeFrame(); |
324 | 326 |
325 DesktopRegion* updated_region = frame->mutable_updated_region(); | 327 DesktopRegion* updated_region = frame->mutable_updated_region(); |
326 | 328 |
327 x_server_pixel_buffer_.Synchronize(); | 329 x_server_pixel_buffer_.Synchronize(); |
328 if (use_damage_ && queue_.previous_frame()) { | 330 if (use_damage_ && queue_.previous()) { |
329 // Atomically fetch and clear the damage region. | 331 // Atomically fetch and clear the damage region. |
330 XDamageSubtract(display(), damage_handle_, None, damage_region_); | 332 XDamageSubtract(display(), damage_handle_, None, damage_region_); |
331 int rects_num = 0; | 333 int rects_num = 0; |
332 XRectangle bounds; | 334 XRectangle bounds; |
333 XRectangle* rects = XFixesFetchRegionAndBounds(display(), damage_region_, | 335 XRectangle* rects = XFixesFetchRegionAndBounds(display(), damage_region_, |
334 &rects_num, &bounds); | 336 &rects_num, &bounds); |
335 for (int i = 0; i < rects_num; ++i) { | 337 for (int i = 0; i < rects_num; ++i) { |
336 updated_region->AddRect(DesktopRect::MakeXYWH( | 338 updated_region->AddRect(DesktopRect::MakeXYWH( |
337 rects[i].x, rects[i].y, rects[i].width, rects[i].height)); | 339 rects[i].x, rects[i].y, rects[i].width, rects[i].height)); |
338 } | 340 } |
(...skipping 12 matching lines...) Expand all Loading... |
351 for (DesktopRegion::Iterator it(*updated_region); | 353 for (DesktopRegion::Iterator it(*updated_region); |
352 !it.IsAtEnd(); it.Advance()) { | 354 !it.IsAtEnd(); it.Advance()) { |
353 x_server_pixel_buffer_.CaptureRect(it.rect(), frame); | 355 x_server_pixel_buffer_.CaptureRect(it.rect(), frame); |
354 } | 356 } |
355 } else { | 357 } else { |
356 // Doing full-screen polling, or this is the first capture after a | 358 // Doing full-screen polling, or this is the first capture after a |
357 // screen-resolution change. In either case, need a full-screen capture. | 359 // screen-resolution change. In either case, need a full-screen capture. |
358 DesktopRect screen_rect = DesktopRect::MakeSize(frame->size()); | 360 DesktopRect screen_rect = DesktopRect::MakeSize(frame->size()); |
359 x_server_pixel_buffer_.CaptureRect(screen_rect, frame); | 361 x_server_pixel_buffer_.CaptureRect(screen_rect, frame); |
360 | 362 |
361 if (queue_.previous_frame()) { | 363 if (queue_.previous()) { |
362 // Full-screen polling, so calculate the invalid rects here, based on the | 364 // Full-screen polling, so calculate the invalid rects here, based on the |
363 // changed pixels between current and previous buffers. | 365 // changed pixels between current and previous buffers. |
364 RTC_DCHECK(differ_.get() != NULL); | 366 RTC_DCHECK(differ_.get() != NULL); |
365 RTC_DCHECK(queue_.previous_frame()->data()); | 367 RTC_DCHECK(queue_.previous()->data()); |
366 differ_->CalcDirtyRegion(queue_.previous_frame()->data(), | 368 differ_->CalcDirtyRegion(queue_.previous()->data(), |
367 frame->data(), updated_region); | 369 frame->data(), updated_region); |
368 } else { | 370 } else { |
369 // No previous buffer, so always invalidate the whole screen, whether | 371 // No previous buffer, so always invalidate the whole screen, whether |
370 // or not DAMAGE is being used. DAMAGE doesn't necessarily send a | 372 // or not DAMAGE is being used. DAMAGE doesn't necessarily send a |
371 // full-screen notification after a screen-resolution change, so | 373 // full-screen notification after a screen-resolution change, so |
372 // this is done here. | 374 // this is done here. |
373 updated_region->SetRect(screen_rect); | 375 updated_region->SetRect(screen_rect); |
374 } | 376 } |
375 } | 377 } |
376 | 378 |
(...skipping 13 matching lines...) Expand all Loading... |
390 | 392 |
391 void ScreenCapturerLinux::SynchronizeFrame() { | 393 void ScreenCapturerLinux::SynchronizeFrame() { |
392 // Synchronize the current buffer with the previous one since we do not | 394 // Synchronize the current buffer with the previous one since we do not |
393 // capture the entire desktop. Note that encoder may be reading from the | 395 // capture the entire desktop. Note that encoder may be reading from the |
394 // previous buffer at this time so thread access complaints are false | 396 // previous buffer at this time so thread access complaints are false |
395 // positives. | 397 // positives. |
396 | 398 |
397 // TODO(hclam): We can reduce the amount of copying here by subtracting | 399 // TODO(hclam): We can reduce the amount of copying here by subtracting |
398 // |capturer_helper_|s region from |last_invalid_region_|. | 400 // |capturer_helper_|s region from |last_invalid_region_|. |
399 // http://crbug.com/92354 | 401 // http://crbug.com/92354 |
400 RTC_DCHECK(queue_.previous_frame()); | 402 RTC_DCHECK(queue_.previous()); |
401 | 403 |
402 DesktopFrame* current = queue_.current_frame(); | 404 DesktopFrame* current = queue_.current(); |
403 DesktopFrame* last = queue_.previous_frame(); | 405 DesktopFrame* last = queue_.previous(); |
404 RTC_DCHECK(current != last); | 406 RTC_DCHECK(current != last); |
405 for (DesktopRegion::Iterator it(last_invalid_region_); | 407 for (DesktopRegion::Iterator it(last_invalid_region_); |
406 !it.IsAtEnd(); it.Advance()) { | 408 !it.IsAtEnd(); it.Advance()) { |
407 current->CopyPixelsFrom(*last, it.rect().top_left(), it.rect()); | 409 current->CopyPixelsFrom(*last, it.rect().top_left(), it.rect()); |
408 } | 410 } |
409 } | 411 } |
410 | 412 |
411 void ScreenCapturerLinux::DeinitXlib() { | 413 void ScreenCapturerLinux::DeinitXlib() { |
412 if (gc_) { | 414 if (gc_) { |
413 XFreeGC(display(), gc_); | 415 XFreeGC(display(), gc_); |
(...skipping 22 matching lines...) Expand all Loading... |
436 if (!options.x_display()) | 438 if (!options.x_display()) |
437 return NULL; | 439 return NULL; |
438 | 440 |
439 std::unique_ptr<ScreenCapturerLinux> capturer(new ScreenCapturerLinux()); | 441 std::unique_ptr<ScreenCapturerLinux> capturer(new ScreenCapturerLinux()); |
440 if (!capturer->Init(options)) | 442 if (!capturer->Init(options)) |
441 capturer.reset(); | 443 capturer.reset(); |
442 return capturer.release(); | 444 return capturer.release(); |
443 } | 445 } |
444 | 446 |
445 } // namespace webrtc | 447 } // namespace webrtc |
OLD | NEW |