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 #include <utility> | 17 #include <utility> |
18 | 18 |
19 #include <X11/extensions/Xdamage.h> | 19 #include <X11/extensions/Xdamage.h> |
20 #include <X11/extensions/Xfixes.h> | 20 #include <X11/extensions/Xfixes.h> |
21 #include <X11/Xlib.h> | 21 #include <X11/Xlib.h> |
22 #include <X11/Xutil.h> | 22 #include <X11/Xutil.h> |
23 | 23 |
24 #include "webrtc/base/checks.h" | 24 #include "webrtc/base/checks.h" |
25 #include "webrtc/base/constructormagic.h" | 25 #include "webrtc/base/constructormagic.h" |
26 #include "webrtc/base/timeutils.h" | 26 #include "webrtc/base/timeutils.h" |
27 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" | 27 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
28 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 28 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
29 #include "webrtc/modules/desktop_capture/differ.h" | |
30 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" | 29 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" |
31 #include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" | 30 #include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" |
32 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" | 31 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" |
33 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" | 32 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" |
34 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" | 33 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" |
35 #include "webrtc/system_wrappers/include/logging.h" | 34 #include "webrtc/system_wrappers/include/logging.h" |
36 | 35 |
37 namespace webrtc { | 36 namespace webrtc { |
38 namespace { | 37 namespace { |
39 | 38 |
40 // A class to perform video frame capturing for Linux. | 39 // A class to perform video frame capturing for Linux. |
| 40 // |
| 41 // If XDamage is used, this class sets DesktopFrame::updated_region() according |
| 42 // to the areas reported by XDamage. Otherwise this class does not detect |
| 43 // DesktopFrame::updated_region(), the field is always set to the entire frame |
| 44 // rectangle. ScreenCapturerDifferWrapper should be used if that functionality |
| 45 // is necessary. |
41 class ScreenCapturerLinux : public ScreenCapturer, | 46 class ScreenCapturerLinux : public ScreenCapturer, |
42 public SharedXDisplay::XEventHandler { | 47 public SharedXDisplay::XEventHandler { |
43 public: | 48 public: |
44 ScreenCapturerLinux(); | 49 ScreenCapturerLinux(); |
45 virtual ~ScreenCapturerLinux(); | 50 virtual ~ScreenCapturerLinux(); |
46 | 51 |
47 // TODO(ajwong): Do we really want this to be synchronous? | 52 // TODO(ajwong): Do we really want this to be synchronous? |
48 bool Init(const DesktopCaptureOptions& options); | 53 bool Init(const DesktopCaptureOptions& options); |
49 | 54 |
50 // DesktopCapturer interface. | 55 // DesktopCapturer interface. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // recently captured screen. | 114 // recently captured screen. |
110 ScreenCapturerHelper helper_; | 115 ScreenCapturerHelper helper_; |
111 | 116 |
112 // Queue of the frames buffers. | 117 // Queue of the frames buffers. |
113 ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; | 118 ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; |
114 | 119 |
115 // Invalid region from the previous capture. This is used to synchronize the | 120 // Invalid region from the previous capture. This is used to synchronize the |
116 // current with the last buffer used. | 121 // current with the last buffer used. |
117 DesktopRegion last_invalid_region_; | 122 DesktopRegion last_invalid_region_; |
118 | 123 |
119 // |Differ| for use when polling for changes. | |
120 std::unique_ptr<Differ> differ_; | |
121 | |
122 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); | 124 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); |
123 }; | 125 }; |
124 | 126 |
125 ScreenCapturerLinux::ScreenCapturerLinux() { | 127 ScreenCapturerLinux::ScreenCapturerLinux() { |
126 helper_.SetLogGridSize(4); | 128 helper_.SetLogGridSize(4); |
127 } | 129 } |
128 | 130 |
129 ScreenCapturerLinux::~ScreenCapturerLinux() { | 131 ScreenCapturerLinux::~ScreenCapturerLinux() { |
130 options_.x_display()->RemoveEventHandler(ConfigureNotify, this); | 132 options_.x_display()->RemoveEventHandler(ConfigureNotify, this); |
131 if (use_damage_) { | 133 if (use_damage_) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 248 |
247 // If the current frame is from an older generation then allocate a new one. | 249 // If the current frame is from an older generation then allocate a new one. |
248 // Note that we can't reallocate other buffers at this point, since the caller | 250 // Note that we can't reallocate other buffers at this point, since the caller |
249 // may still be reading from them. | 251 // may still be reading from them. |
250 if (!queue_.current_frame()) { | 252 if (!queue_.current_frame()) { |
251 queue_.ReplaceCurrentFrame( | 253 queue_.ReplaceCurrentFrame( |
252 SharedDesktopFrame::Wrap(std::unique_ptr<DesktopFrame>( | 254 SharedDesktopFrame::Wrap(std::unique_ptr<DesktopFrame>( |
253 new BasicDesktopFrame(x_server_pixel_buffer_.window_size())))); | 255 new BasicDesktopFrame(x_server_pixel_buffer_.window_size())))); |
254 } | 256 } |
255 | 257 |
256 // Refresh the Differ helper used by CaptureFrame(), if needed. | |
257 DesktopFrame* frame = queue_.current_frame(); | |
258 if (!use_damage_ && | |
259 (!differ_ || (differ_->width() != frame->size().width()) || | |
260 (differ_->height() != frame->size().height()) || | |
261 (differ_->bytes_per_row() != frame->stride()))) { | |
262 differ_.reset(new Differ(frame->size().width(), frame->size().height(), | |
263 DesktopFrame::kBytesPerPixel, frame->stride())); | |
264 } | |
265 | |
266 std::unique_ptr<DesktopFrame> result = CaptureScreen(); | 258 std::unique_ptr<DesktopFrame> result = CaptureScreen(); |
267 last_invalid_region_ = result->updated_region(); | 259 last_invalid_region_ = result->updated_region(); |
268 result->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) / | 260 result->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) / |
269 rtc::kNumNanosecsPerMillisec); | 261 rtc::kNumNanosecsPerMillisec); |
270 callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); | 262 callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); |
271 } | 263 } |
272 | 264 |
273 bool ScreenCapturerLinux::GetScreenList(ScreenList* screens) { | 265 bool ScreenCapturerLinux::GetScreenList(ScreenList* screens) { |
274 RTC_DCHECK(screens->size() == 0); | 266 RTC_DCHECK(screens->size() == 0); |
275 // TODO(jiayl): implement screen enumeration. | 267 // TODO(jiayl): implement screen enumeration. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 334 |
343 for (DesktopRegion::Iterator it(*updated_region); | 335 for (DesktopRegion::Iterator it(*updated_region); |
344 !it.IsAtEnd(); it.Advance()) { | 336 !it.IsAtEnd(); it.Advance()) { |
345 x_server_pixel_buffer_.CaptureRect(it.rect(), frame.get()); | 337 x_server_pixel_buffer_.CaptureRect(it.rect(), frame.get()); |
346 } | 338 } |
347 } else { | 339 } else { |
348 // Doing full-screen polling, or this is the first capture after a | 340 // Doing full-screen polling, or this is the first capture after a |
349 // screen-resolution change. In either case, need a full-screen capture. | 341 // screen-resolution change. In either case, need a full-screen capture. |
350 DesktopRect screen_rect = DesktopRect::MakeSize(frame->size()); | 342 DesktopRect screen_rect = DesktopRect::MakeSize(frame->size()); |
351 x_server_pixel_buffer_.CaptureRect(screen_rect, frame.get()); | 343 x_server_pixel_buffer_.CaptureRect(screen_rect, frame.get()); |
352 | 344 updated_region->SetRect(screen_rect); |
353 if (queue_.previous_frame()) { | |
354 // Full-screen polling, so calculate the invalid rects here, based on the | |
355 // changed pixels between current and previous buffers. | |
356 RTC_DCHECK(differ_); | |
357 RTC_DCHECK(queue_.previous_frame()->data()); | |
358 differ_->CalcDirtyRegion(queue_.previous_frame()->data(), | |
359 frame->data(), updated_region); | |
360 } else { | |
361 // No previous buffer, so always invalidate the whole screen, whether | |
362 // or not DAMAGE is being used. DAMAGE doesn't necessarily send a | |
363 // full-screen notification after a screen-resolution change, so | |
364 // this is done here. | |
365 updated_region->SetRect(screen_rect); | |
366 } | |
367 } | 345 } |
368 | 346 |
369 return std::move(frame); | 347 return std::move(frame); |
370 } | 348 } |
371 | 349 |
372 void ScreenCapturerLinux::ScreenConfigurationChanged() { | 350 void ScreenCapturerLinux::ScreenConfigurationChanged() { |
373 // Make sure the frame buffers will be reallocated. | 351 // Make sure the frame buffers will be reallocated. |
374 queue_.Reset(); | 352 queue_.Reset(); |
375 | 353 |
376 helper_.ClearInvalidRegion(); | 354 helper_.ClearInvalidRegion(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 } | 412 } |
435 | 413 |
436 if (options.detect_updated_region()) { | 414 if (options.detect_updated_region()) { |
437 capturer.reset(new ScreenCapturerDifferWrapper(std::move(capturer))); | 415 capturer.reset(new ScreenCapturerDifferWrapper(std::move(capturer))); |
438 } | 416 } |
439 | 417 |
440 return capturer.release(); | 418 return capturer.release(); |
441 } | 419 } |
442 | 420 |
443 } // namespace webrtc | 421 } // namespace webrtc |
OLD | NEW |