zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 1 | /* |
zijiehe | e352dbe | 2017-02-21 15:00:07 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 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/fallback_desktop_capturer_wrapper.h" |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 12 | |
| 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
| 16 | #include "system_wrappers/include/metrics.h" |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Implementation to share a SharedMemoryFactory between DesktopCapturer |
| 23 | // instances. This class is designed for synchronized DesktopCapturer |
| 24 | // implementations only. |
| 25 | class SharedMemoryFactoryProxy : public SharedMemoryFactory { |
| 26 | public: |
| 27 | // Users should maintain the lifetime of |factory| to ensure it overlives |
| 28 | // current instance. |
| 29 | static std::unique_ptr<SharedMemoryFactory> Create( |
| 30 | SharedMemoryFactory* factory); |
| 31 | ~SharedMemoryFactoryProxy() override; |
| 32 | |
| 33 | // Forwards CreateSharedMemory() calls to |factory_|. Users should always call |
| 34 | // this function in one thread. Users should not call this function after the |
| 35 | // SharedMemoryFactory which current instance created from has been destroyed. |
| 36 | std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) override; |
| 37 | |
| 38 | private: |
| 39 | explicit SharedMemoryFactoryProxy(SharedMemoryFactory* factory); |
| 40 | |
| 41 | SharedMemoryFactory* factory_ = nullptr; |
| 42 | rtc::ThreadChecker thread_checker_; |
| 43 | }; |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | SharedMemoryFactoryProxy::SharedMemoryFactoryProxy( |
| 48 | SharedMemoryFactory* factory) { |
| 49 | RTC_DCHECK(factory); |
| 50 | factory_ = factory; |
| 51 | } |
| 52 | |
| 53 | // static |
| 54 | std::unique_ptr<SharedMemoryFactory> |
| 55 | SharedMemoryFactoryProxy::Create(SharedMemoryFactory* factory) { |
| 56 | return std::unique_ptr<SharedMemoryFactory>( |
| 57 | new SharedMemoryFactoryProxy(factory)); |
| 58 | } |
| 59 | |
| 60 | SharedMemoryFactoryProxy::~SharedMemoryFactoryProxy() = default; |
| 61 | |
| 62 | std::unique_ptr<SharedMemory> |
| 63 | SharedMemoryFactoryProxy::CreateSharedMemory(size_t size) { |
| 64 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 | return factory_->CreateSharedMemory(size); |
| 66 | } |
| 67 | |
| 68 | FallbackDesktopCapturerWrapper::FallbackDesktopCapturerWrapper( |
| 69 | std::unique_ptr<DesktopCapturer> main_capturer, |
| 70 | std::unique_ptr<DesktopCapturer> secondary_capturer) |
| 71 | : main_capturer_(std::move(main_capturer)), |
| 72 | secondary_capturer_(std::move(secondary_capturer)) { |
| 73 | RTC_DCHECK(main_capturer_); |
| 74 | RTC_DCHECK(secondary_capturer_); |
| 75 | } |
| 76 | |
| 77 | FallbackDesktopCapturerWrapper::~FallbackDesktopCapturerWrapper() = default; |
| 78 | |
| 79 | void FallbackDesktopCapturerWrapper::Start( |
| 80 | DesktopCapturer::Callback* callback) { |
| 81 | // FallbackDesktopCapturerWrapper catchs the callback of the main capturer, |
| 82 | // and checks its return value to decide whether the secondary capturer should |
| 83 | // be involved. |
| 84 | main_capturer_->Start(this); |
| 85 | // For the secondary capturer, we do not have a backup plan anymore, so |
| 86 | // FallbackDesktopCapturerWrapper won't check its return value any more. It |
| 87 | // will directly return to the input |callback|. |
| 88 | secondary_capturer_->Start(callback); |
| 89 | callback_ = callback; |
| 90 | } |
| 91 | |
| 92 | void FallbackDesktopCapturerWrapper::SetSharedMemoryFactory( |
| 93 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { |
| 94 | shared_memory_factory_ = std::move(shared_memory_factory); |
| 95 | if (shared_memory_factory_) { |
| 96 | main_capturer_->SetSharedMemoryFactory( |
| 97 | SharedMemoryFactoryProxy::Create(shared_memory_factory_.get())); |
| 98 | secondary_capturer_->SetSharedMemoryFactory( |
| 99 | SharedMemoryFactoryProxy::Create(shared_memory_factory_.get())); |
| 100 | } else { |
| 101 | main_capturer_->SetSharedMemoryFactory( |
| 102 | std::unique_ptr<SharedMemoryFactory>()); |
| 103 | secondary_capturer_->SetSharedMemoryFactory( |
| 104 | std::unique_ptr<SharedMemoryFactory>()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void FallbackDesktopCapturerWrapper::CaptureFrame() { |
| 109 | RTC_DCHECK(callback_); |
| 110 | if (main_capturer_permanent_error_) { |
| 111 | secondary_capturer_->CaptureFrame(); |
| 112 | } else { |
| 113 | main_capturer_->CaptureFrame(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void FallbackDesktopCapturerWrapper::SetExcludedWindow(WindowId window) { |
| 118 | main_capturer_->SetExcludedWindow(window); |
| 119 | secondary_capturer_->SetExcludedWindow(window); |
| 120 | } |
| 121 | |
| 122 | bool FallbackDesktopCapturerWrapper::GetSourceList(SourceList* sources) { |
| 123 | if (main_capturer_permanent_error_) { |
| 124 | return secondary_capturer_->GetSourceList(sources); |
| 125 | } |
| 126 | return main_capturer_->GetSourceList(sources); |
| 127 | } |
| 128 | |
| 129 | bool FallbackDesktopCapturerWrapper::SelectSource(SourceId id) { |
| 130 | if (main_capturer_permanent_error_) { |
| 131 | return secondary_capturer_->SelectSource(id); |
| 132 | } |
Zijie He | 6564ea3 | 2017-09-14 08:28:58 -0700 | [diff] [blame] | 133 | const bool main_capturer_result = main_capturer_->SelectSource(id); |
| 134 | RTC_HISTOGRAM_BOOLEAN( |
| 135 | "WebRTC.DesktopCapture.PrimaryCapturerSelectSourceError", |
| 136 | main_capturer_result); |
| 137 | if (!main_capturer_result) { |
| 138 | main_capturer_permanent_error_ = true; |
| 139 | } |
| 140 | |
| 141 | return secondary_capturer_->SelectSource(id); |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool FallbackDesktopCapturerWrapper::FocusOnSelectedSource() { |
| 145 | if (main_capturer_permanent_error_) { |
| 146 | return secondary_capturer_->FocusOnSelectedSource(); |
| 147 | } |
| 148 | return main_capturer_->FocusOnSelectedSource() || |
| 149 | secondary_capturer_->FocusOnSelectedSource(); |
| 150 | } |
| 151 | |
Zijie He | 9cad501 | 2017-09-14 08:32:46 -0700 | [diff] [blame] | 152 | bool FallbackDesktopCapturerWrapper::IsOccluded(const DesktopVector& pos) { |
| 153 | // Returns true if either capturer returns true. |
| 154 | if (main_capturer_permanent_error_) { |
| 155 | return secondary_capturer_->IsOccluded(pos); |
| 156 | } |
| 157 | return main_capturer_->IsOccluded(pos) || |
| 158 | secondary_capturer_->IsOccluded(pos); |
| 159 | } |
| 160 | |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 161 | void FallbackDesktopCapturerWrapper::OnCaptureResult( |
| 162 | Result result, |
| 163 | std::unique_ptr<DesktopFrame> frame) { |
| 164 | RTC_DCHECK(callback_); |
Zijie He | 565d046 | 2017-08-15 15:31:31 -0700 | [diff] [blame] | 165 | RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.PrimaryCapturerError", |
| 166 | result != Result::SUCCESS); |
| 167 | RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.PrimaryCapturerPermanentError", |
| 168 | result == Result::ERROR_PERMANENT); |
zijiehe | 8fefe98 | 2017-02-17 14:32:04 -0800 | [diff] [blame] | 169 | if (result == Result::SUCCESS) { |
| 170 | callback_->OnCaptureResult(result, std::move(frame)); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if (result == Result::ERROR_PERMANENT) { |
| 175 | main_capturer_permanent_error_ = true; |
| 176 | } |
| 177 | secondary_capturer_->CaptureFrame(); |
| 178 | } |
| 179 | |
| 180 | } // namespace webrtc |