blob: b261a030281a71bef1b637824d9fa7def8e6bbb8 [file] [log] [blame]
zijiehe8fefe982017-02-17 14:32:04 -08001/*
zijiehee352dbe2017-02-21 15:00:07 -08002 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
zijiehe8fefe982017-02-17 14:32:04 -08003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/desktop_capture/fallback_desktop_capturer_wrapper.h"
zijiehe8fefe982017-02-17 14:32:04 -080012
13#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
16#include "system_wrappers/include/metrics.h"
zijiehe8fefe982017-02-17 14:32:04 -080017
18namespace webrtc {
19
20namespace {
21
22// Implementation to share a SharedMemoryFactory between DesktopCapturer
23// instances. This class is designed for synchronized DesktopCapturer
24// implementations only.
25class 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
47SharedMemoryFactoryProxy::SharedMemoryFactoryProxy(
48 SharedMemoryFactory* factory) {
49 RTC_DCHECK(factory);
50 factory_ = factory;
51}
52
53// static
54std::unique_ptr<SharedMemoryFactory>
55SharedMemoryFactoryProxy::Create(SharedMemoryFactory* factory) {
56 return std::unique_ptr<SharedMemoryFactory>(
57 new SharedMemoryFactoryProxy(factory));
58}
59
60SharedMemoryFactoryProxy::~SharedMemoryFactoryProxy() = default;
61
62std::unique_ptr<SharedMemory>
63SharedMemoryFactoryProxy::CreateSharedMemory(size_t size) {
64 RTC_DCHECK(thread_checker_.CalledOnValidThread());
65 return factory_->CreateSharedMemory(size);
66}
67
68FallbackDesktopCapturerWrapper::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
77FallbackDesktopCapturerWrapper::~FallbackDesktopCapturerWrapper() = default;
78
79void 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
92void 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
108void 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
117void FallbackDesktopCapturerWrapper::SetExcludedWindow(WindowId window) {
118 main_capturer_->SetExcludedWindow(window);
119 secondary_capturer_->SetExcludedWindow(window);
120}
121
122bool 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
129bool FallbackDesktopCapturerWrapper::SelectSource(SourceId id) {
130 if (main_capturer_permanent_error_) {
131 return secondary_capturer_->SelectSource(id);
132 }
Zijie He6564ea32017-09-14 08:28:58 -0700133 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);
zijiehe8fefe982017-02-17 14:32:04 -0800142}
143
144bool 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 He9cad5012017-09-14 08:32:46 -0700152bool 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
zijiehe8fefe982017-02-17 14:32:04 -0800161void FallbackDesktopCapturerWrapper::OnCaptureResult(
162 Result result,
163 std::unique_ptr<DesktopFrame> frame) {
164 RTC_DCHECK(callback_);
Zijie He565d0462017-08-15 15:31:31 -0700165 RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.PrimaryCapturerError",
166 result != Result::SUCCESS);
167 RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.PrimaryCapturerPermanentError",
168 result == Result::ERROR_PERMANENT);
zijiehe8fefe982017-02-17 14:32:04 -0800169 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