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" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 14 | #include "webrtc/rtc_base/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), |
zijiehe | 3045589 | 2016-11-09 16:37:48 -0800 | [diff] [blame] | 22 | window_capturer_(DesktopCapturer::CreateRawWindowCapturer(options)), |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 23 | selected_window_(kNullWindowId), |
zijiehe | 98903d2 | 2016-11-10 21:57:10 -0800 | [diff] [blame] | 24 | excluded_window_(kNullWindowId) {} |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 25 | |
| 26 | CroppingWindowCapturer::~CroppingWindowCapturer() {} |
| 27 | |
| 28 | void CroppingWindowCapturer::Start(DesktopCapturer::Callback* callback) { |
| 29 | callback_ = callback; |
| 30 | window_capturer_->Start(callback); |
| 31 | } |
| 32 | |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame] | 33 | void CroppingWindowCapturer::SetSharedMemoryFactory( |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 34 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame] | 35 | window_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); |
| 36 | } |
| 37 | |
zijiehe | 91902cb | 2016-10-13 16:47:49 -0700 | [diff] [blame] | 38 | void CroppingWindowCapturer::CaptureFrame() { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 39 | if (ShouldUseScreenCapturer()) { |
| 40 | if (!screen_capturer_.get()) { |
zijiehe | 3045589 | 2016-11-09 16:37:48 -0800 | [diff] [blame] | 41 | screen_capturer_ = DesktopCapturer::CreateRawScreenCapturer(options_); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 42 | if (excluded_window_) { |
| 43 | screen_capturer_->SetExcludedWindow(excluded_window_); |
| 44 | } |
| 45 | screen_capturer_->Start(this); |
| 46 | } |
zijiehe | 91902cb | 2016-10-13 16:47:49 -0700 | [diff] [blame] | 47 | screen_capturer_->CaptureFrame(); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 48 | } else { |
zijiehe | 91902cb | 2016-10-13 16:47:49 -0700 | [diff] [blame] | 49 | window_capturer_->CaptureFrame(); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void CroppingWindowCapturer::SetExcludedWindow(WindowId window) { |
| 54 | excluded_window_ = window; |
| 55 | if (screen_capturer_.get()) { |
| 56 | screen_capturer_->SetExcludedWindow(window); |
| 57 | } |
| 58 | } |
| 59 | |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 60 | bool CroppingWindowCapturer::GetSourceList(SourceList* sources) { |
| 61 | return window_capturer_->GetSourceList(sources); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 62 | } |
| 63 | |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 64 | bool CroppingWindowCapturer::SelectSource(SourceId id) { |
| 65 | if (window_capturer_->SelectSource(id)) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 66 | selected_window_ = id; |
| 67 | return true; |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 72 | bool CroppingWindowCapturer::FocusOnSelectedSource() { |
| 73 | return window_capturer_->FocusOnSelectedSource(); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 76 | void CroppingWindowCapturer::OnCaptureResult( |
| 77 | DesktopCapturer::Result result, |
| 78 | std::unique_ptr<DesktopFrame> screen_frame) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 79 | if (!ShouldUseScreenCapturer()) { |
| 80 | LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes"; |
zijiehe | 249beee | 2016-10-18 23:13:29 -0700 | [diff] [blame] | 81 | window_capturer_->CaptureFrame(); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 85 | if (result != Result::SUCCESS) { |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 86 | LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame"; |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 87 | callback_->OnCaptureResult(result, nullptr); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | DesktopRect window_rect = GetWindowRectInVirtualScreen(); |
| 92 | if (window_rect.is_empty()) { |
| 93 | LOG(LS_WARNING) << "Window rect is empty"; |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 94 | callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 98 | callback_->OnCaptureResult( |
| 99 | Result::SUCCESS, |
| 100 | CreateCroppedDesktopFrame(std::move(screen_frame), window_rect)); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Zijie He | 9cad501 | 2017-09-14 08:32:46 -0700 | [diff] [blame] | 103 | bool CroppingWindowCapturer::IsOccluded(const DesktopVector& pos) { |
| 104 | // Returns true if either capturer returns true. |
| 105 | if (window_capturer_->IsOccluded(pos)) { |
| 106 | return true; |
| 107 | } |
| 108 | if (screen_capturer_ != nullptr && screen_capturer_->IsOccluded(pos)) { |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
jiayl@webrtc.org | 90b9b08 | 2014-11-12 20:53:00 +0000 | [diff] [blame] | 114 | #if !defined(WEBRTC_WIN) |
zijiehe | 1b0e3aa | 2016-11-21 13:54:26 -0800 | [diff] [blame] | 115 | // CroppingWindowCapturer is implemented only for windows. On other platforms |
| 116 | // the regular window capturer is used. |
zijiehe | c9a6e4a | 2016-11-11 15:13:32 -0800 | [diff] [blame] | 117 | // static |
| 118 | std::unique_ptr<DesktopCapturer> CroppingWindowCapturer::CreateCapturer( |
| 119 | const DesktopCaptureOptions& options) { |
| 120 | return DesktopCapturer::CreateWindowCapturer(options); |
| 121 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | } // namespace webrtc |