cropping_window_capturer.cc (4396B)
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 "modules/desktop_capture/cropping_window_capturer.h" 12 13 #include <cstddef> 14 #include <memory> 15 #include <utility> 16 17 #include "modules/desktop_capture/cropped_desktop_frame.h" 18 #include "modules/desktop_capture/desktop_capture_options.h" 19 #include "modules/desktop_capture/desktop_capture_types.h" 20 #include "modules/desktop_capture/desktop_capturer.h" 21 #include "modules/desktop_capture/desktop_frame.h" 22 #include "modules/desktop_capture/desktop_geometry.h" 23 #include "modules/desktop_capture/shared_memory.h" 24 #include "rtc_base/logging.h" 25 26 namespace webrtc { 27 28 CroppingWindowCapturer::CroppingWindowCapturer( 29 const DesktopCaptureOptions& options) 30 : options_(options), 31 callback_(nullptr), 32 window_capturer_(DesktopCapturer::CreateRawWindowCapturer(options)), 33 selected_window_(kNullWindowId), 34 excluded_window_(kNullWindowId) {} 35 36 CroppingWindowCapturer::~CroppingWindowCapturer() {} 37 38 void CroppingWindowCapturer::Start(DesktopCapturer::Callback* callback) { 39 callback_ = callback; 40 window_capturer_->Start(callback); 41 } 42 43 void CroppingWindowCapturer::SetSharedMemoryFactory( 44 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { 45 window_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); 46 } 47 48 void CroppingWindowCapturer::CaptureFrame() { 49 if (ShouldUseScreenCapturer()) { 50 if (!screen_capturer_) { 51 screen_capturer_ = DesktopCapturer::CreateRawScreenCapturer(options_); 52 if (excluded_window_) { 53 screen_capturer_->SetExcludedWindow(excluded_window_); 54 } 55 screen_capturer_->Start(this); 56 } 57 screen_capturer_->CaptureFrame(); 58 } else { 59 window_capturer_->CaptureFrame(); 60 } 61 } 62 63 void CroppingWindowCapturer::SetExcludedWindow(WindowId window) { 64 excluded_window_ = window; 65 if (screen_capturer_) { 66 screen_capturer_->SetExcludedWindow(window); 67 } 68 } 69 70 bool CroppingWindowCapturer::GetSourceList(SourceList* sources) { 71 return window_capturer_->GetSourceList(sources); 72 } 73 74 bool CroppingWindowCapturer::SelectSource(SourceId id) { 75 if (window_capturer_->SelectSource(id)) { 76 selected_window_ = id; 77 return true; 78 } 79 return false; 80 } 81 82 bool CroppingWindowCapturer::FocusOnSelectedSource() { 83 return window_capturer_->FocusOnSelectedSource(); 84 } 85 86 void CroppingWindowCapturer::OnCaptureResult( 87 DesktopCapturer::Result result, 88 std::unique_ptr<DesktopFrame> screen_frame) { 89 if (!ShouldUseScreenCapturer()) { 90 RTC_LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes"; 91 window_capturer_->CaptureFrame(); 92 return; 93 } 94 95 if (result != Result::SUCCESS) { 96 RTC_LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame"; 97 callback_->OnCaptureResult(result, nullptr); 98 return; 99 } 100 101 DesktopRect window_rect = GetWindowRectInVirtualScreen(); 102 if (window_rect.is_empty()) { 103 RTC_LOG(LS_WARNING) << "Window rect is empty"; 104 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); 105 return; 106 } 107 108 std::unique_ptr<DesktopFrame> cropped_frame = 109 CreateCroppedDesktopFrame(std::move(screen_frame), window_rect); 110 111 if (!cropped_frame) { 112 RTC_LOG(LS_WARNING) << "Window is outside of the captured display"; 113 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); 114 return; 115 } 116 117 callback_->OnCaptureResult(Result::SUCCESS, std::move(cropped_frame)); 118 } 119 120 bool CroppingWindowCapturer::IsOccluded(const DesktopVector& pos) { 121 // Returns true if either capturer returns true. 122 if (window_capturer_->IsOccluded(pos)) { 123 return true; 124 } 125 if (screen_capturer_ != nullptr && screen_capturer_->IsOccluded(pos)) { 126 return true; 127 } 128 return false; 129 } 130 131 #if !defined(WEBRTC_WIN) 132 // CroppingWindowCapturer is implemented only for windows. On other platforms 133 // the regular window capturer is used. 134 // static 135 std::unique_ptr<DesktopCapturer> CroppingWindowCapturer::CreateCapturer( 136 const DesktopCaptureOptions& options) { 137 return DesktopCapturer::CreateWindowCapturer(options); 138 } 139 #endif 140 141 } // namespace webrtc