jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | #include "webrtc/modules/desktop_capture/cropping_window_capturer.h" |
| 12 | |
| 13 | #include "webrtc/modules/desktop_capture/cropped_desktop_frame.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/include/logging.h" |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | CroppingWindowCapturer::CroppingWindowCapturer( |
| 19 | const DesktopCaptureOptions& options) |
| 20 | : options_(options), |
| 21 | callback_(NULL), |
| 22 | window_capturer_(WindowCapturer::Create(options)), |
| 23 | selected_window_(kNullWindowId), |
| 24 | excluded_window_(kNullWindowId) { |
| 25 | } |
| 26 | |
| 27 | CroppingWindowCapturer::~CroppingWindowCapturer() {} |
| 28 | |
| 29 | void CroppingWindowCapturer::Start(DesktopCapturer::Callback* callback) { |
| 30 | callback_ = callback; |
| 31 | window_capturer_->Start(callback); |
| 32 | } |
| 33 | |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame^] | 34 | void CroppingWindowCapturer::SetSharedMemoryFactory( |
| 35 | rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) { |
| 36 | window_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); |
| 37 | } |
| 38 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 39 | void CroppingWindowCapturer::Capture(const DesktopRegion& region) { |
| 40 | if (ShouldUseScreenCapturer()) { |
| 41 | if (!screen_capturer_.get()) { |
| 42 | screen_capturer_.reset(ScreenCapturer::Create(options_)); |
| 43 | if (excluded_window_) { |
| 44 | screen_capturer_->SetExcludedWindow(excluded_window_); |
| 45 | } |
| 46 | screen_capturer_->Start(this); |
| 47 | } |
| 48 | screen_capturer_->Capture(region); |
| 49 | } else { |
| 50 | window_capturer_->Capture(region); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void CroppingWindowCapturer::SetExcludedWindow(WindowId window) { |
| 55 | excluded_window_ = window; |
| 56 | if (screen_capturer_.get()) { |
| 57 | screen_capturer_->SetExcludedWindow(window); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool CroppingWindowCapturer::GetWindowList(WindowList* windows) { |
| 62 | return window_capturer_->GetWindowList(windows); |
| 63 | } |
| 64 | |
| 65 | bool CroppingWindowCapturer::SelectWindow(WindowId id) { |
| 66 | if (window_capturer_->SelectWindow(id)) { |
| 67 | selected_window_ = id; |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool CroppingWindowCapturer::BringSelectedWindowToFront() { |
| 74 | return window_capturer_->BringSelectedWindowToFront(); |
| 75 | } |
| 76 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 77 | void CroppingWindowCapturer::OnCaptureCompleted(DesktopFrame* frame) { |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 78 | rtc::scoped_ptr<DesktopFrame> screen_frame(frame); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 79 | |
| 80 | if (!ShouldUseScreenCapturer()) { |
| 81 | LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes"; |
| 82 | window_capturer_->Capture(DesktopRegion()); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (!frame) { |
| 87 | LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame"; |
| 88 | callback_->OnCaptureCompleted(NULL); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | DesktopRect window_rect = GetWindowRectInVirtualScreen(); |
| 93 | if (window_rect.is_empty()) { |
| 94 | LOG(LS_WARNING) << "Window rect is empty"; |
| 95 | callback_->OnCaptureCompleted(NULL); |
| 96 | return; |
| 97 | } |
| 98 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 99 | rtc::scoped_ptr<DesktopFrame> window_frame( |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 100 | CreateCroppedDesktopFrame(screen_frame.release(), window_rect)); |
| 101 | callback_->OnCaptureCompleted(window_frame.release()); |
| 102 | } |
| 103 | |
jiayl@webrtc.org | 90b9b08 | 2014-11-12 20:53:00 +0000 | [diff] [blame] | 104 | #if !defined(WEBRTC_WIN) |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 105 | // static |
| 106 | WindowCapturer* |
| 107 | CroppingWindowCapturer::Create(const DesktopCaptureOptions& options) { |
| 108 | return WindowCapturer::Create(options); |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | } // namespace webrtc |