Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: webrtc/modules/desktop_capture/fallback_desktop_capturer_wrapper.cc

Issue 2697453002: [DesktopCapturer] FallbackDesktopCapturerWrapper and its tests (Closed)
Patch Set: Resolve review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 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/fallback_desktop_capturer_wrapper.h"
12
13 #include <utility>
14
15 #include "webrtc/base/checks.h"
16
17 namespace webrtc {
18
19 namespace {
20
21 // Implementation to share a SharedMemoryFactory between DesktopCapturer
22 // instances. This class is designed for synchronized DesktopCapturer
23 // implementations only.
24 class SharedMemoryFactoryProxy : public SharedMemoryFactory {
25 public:
26 // Users should maintain the lifetime of |factory| to ensure it overlives
27 // current instance.
28 static std::unique_ptr<SharedMemoryFactory> Create(
29 SharedMemoryFactory* factory);
30 ~SharedMemoryFactoryProxy() override;
31
32 // Forwards CreateSharedMemory() calls to |factory_|. Users should always call
33 // this function in one thread. Users should not call this function after the
34 // SharedMemoryFactory which current instance created from has been destroyed.
35 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) override;
36
37 private:
38 explicit SharedMemoryFactoryProxy(SharedMemoryFactory* factory);
39
40 SharedMemoryFactory* factory_ = nullptr;
41 rtc::ThreadChecker thread_checker_;
42 };
43
44 } // namespace
45
46 SharedMemoryFactoryProxy::SharedMemoryFactoryProxy(
47 SharedMemoryFactory* factory) {
48 RTC_DCHECK(factory);
49 factory_ = factory;
50 }
51
52 // static
53 std::unique_ptr<SharedMemoryFactory>
54 SharedMemoryFactoryProxy::Create(SharedMemoryFactory* factory) {
55 return std::unique_ptr<SharedMemoryFactory>(
56 new SharedMemoryFactoryProxy(factory));
57 }
58
59 SharedMemoryFactoryProxy::~SharedMemoryFactoryProxy() = default;
60
61 std::unique_ptr<SharedMemory>
62 SharedMemoryFactoryProxy::CreateSharedMemory(size_t size) {
63 RTC_DCHECK(thread_checker_.CalledOnValidThread());
64 return factory_->CreateSharedMemory(size);
65 }
66
67 FallbackDesktopCapturerWrapper::FallbackDesktopCapturerWrapper(
68 std::unique_ptr<DesktopCapturer> main_capturer,
69 std::unique_ptr<DesktopCapturer> secondary_capturer)
70 : main_capturer_(std::move(main_capturer)),
71 secondary_capturer_(std::move(secondary_capturer)) {
72 RTC_DCHECK(main_capturer_);
73 RTC_DCHECK(secondary_capturer_);
74 }
75
76 FallbackDesktopCapturerWrapper::~FallbackDesktopCapturerWrapper() = default;
77
78 void FallbackDesktopCapturerWrapper::Start(
79 DesktopCapturer::Callback* callback) {
80 // FallbackDesktopCapturerWrapper catchs the callback of the main capturer,
81 // and checks its return value to decide whether the secondary capturer should
82 // be involved.
83 main_capturer_->Start(this);
84 // For the secondary capturer, we do not have a backup plan anymore, so
85 // FallbackDesktopCapturerWrapper won't check its return value any more. It
86 // will directly return to the input |callback|.
87 secondary_capturer_->Start(callback);
88 callback_ = callback;
89 }
90
91 void FallbackDesktopCapturerWrapper::SetSharedMemoryFactory(
92 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
93 shared_memory_factory_ = std::move(shared_memory_factory);
94 if (shared_memory_factory_) {
95 main_capturer_->SetSharedMemoryFactory(
96 SharedMemoryFactoryProxy::Create(shared_memory_factory_.get()));
97 secondary_capturer_->SetSharedMemoryFactory(
98 SharedMemoryFactoryProxy::Create(shared_memory_factory_.get()));
99 } else {
100 main_capturer_->SetSharedMemoryFactory(
101 std::unique_ptr<SharedMemoryFactory>());
102 secondary_capturer_->SetSharedMemoryFactory(
103 std::unique_ptr<SharedMemoryFactory>());
104 }
105 }
106
107 void FallbackDesktopCapturerWrapper::CaptureFrame() {
108 RTC_DCHECK(callback_);
109 if (main_capturer_permanent_error_) {
110 secondary_capturer_->CaptureFrame();
111 } else {
112 main_capturer_->CaptureFrame();
113 }
114 }
115
116 void FallbackDesktopCapturerWrapper::SetExcludedWindow(WindowId window) {
117 main_capturer_->SetExcludedWindow(window);
118 secondary_capturer_->SetExcludedWindow(window);
119 }
120
121 bool FallbackDesktopCapturerWrapper::GetSourceList(SourceList* sources) {
122 if (main_capturer_permanent_error_) {
123 return secondary_capturer_->GetSourceList(sources);
124 }
125 return main_capturer_->GetSourceList(sources);
126 }
127
128 bool FallbackDesktopCapturerWrapper::SelectSource(SourceId id) {
129 if (main_capturer_permanent_error_) {
130 return secondary_capturer_->SelectSource(id);
131 }
132 return main_capturer_->SelectSource(id) &&
133 secondary_capturer_->SelectSource(id);
134 }
135
136 bool FallbackDesktopCapturerWrapper::FocusOnSelectedSource() {
137 if (main_capturer_permanent_error_) {
138 return secondary_capturer_->FocusOnSelectedSource();
139 }
140 return main_capturer_->FocusOnSelectedSource() ||
141 secondary_capturer_->FocusOnSelectedSource();
142 }
143
144 void FallbackDesktopCapturerWrapper::OnCaptureResult(
145 Result result,
146 std::unique_ptr<DesktopFrame> frame) {
147 RTC_DCHECK(callback_);
148 if (result == Result::SUCCESS) {
149 callback_->OnCaptureResult(result, std::move(frame));
150 return;
151 }
152
153 if (result == Result::ERROR_PERMANENT) {
154 main_capturer_permanent_error_ = true;
155 }
156 secondary_capturer_->CaptureFrame();
157 }
158
159 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698