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