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

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

Issue 2709523003: BlankDetectorDesktopCapturerWrapper to detect a blank DesktopFrame (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) 2017 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 <utility>
12
13 #include "webrtc/base/checks.h"
14 #include "webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper .h"
Sergey Ulanov 2017/02/25 00:05:28 This should be the first include in this file.
Hzj_jie 2017/02/25 02:06:38 Done.
15 #include "webrtc/modules/desktop_capture/desktop_geometry.h"
16
17 namespace webrtc {
18
19 BlankDetectorDesktopCapturerWrapper::BlankDetectorDesktopCapturerWrapper(
20 std::unique_ptr<DesktopCapturer> capturer,
21 RgbaColor blank_pixel,
22 int permanent_failure_threshold)
23 : capturer_(std::move(capturer)),
24 blank_pixel_(blank_pixel),
25 permanent_failure_threshold_(permanent_failure_threshold) {
26 RTC_DCHECK(capturer_);
27 }
28
29 BlankDetectorDesktopCapturerWrapper::~BlankDetectorDesktopCapturerWrapper() =
30 default;
31
32 void BlankDetectorDesktopCapturerWrapper::Start(
33 DesktopCapturer::Callback* callback) {
34 capturer_->Start(this);
35 callback_ = callback;
36 }
37
38 void BlankDetectorDesktopCapturerWrapper::SetSharedMemoryFactory(
39 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
40 capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
41 }
42
43 void BlankDetectorDesktopCapturerWrapper::CaptureFrame() {
44 RTC_DCHECK(callback_);
45 capturer_->CaptureFrame();
46 }
47
48 void BlankDetectorDesktopCapturerWrapper::SetExcludedWindow(WindowId window) {
49 capturer_->SetExcludedWindow(window);
50 }
51
52 bool BlankDetectorDesktopCapturerWrapper::GetSourceList(SourceList* sources) {
53 return capturer_->GetSourceList(sources);
54 }
55
56 bool BlankDetectorDesktopCapturerWrapper::SelectSource(SourceId id) {
57 return capturer_->SelectSource(id);
58 }
59
60 bool BlankDetectorDesktopCapturerWrapper::FocusOnSelectedSource() {
61 return capturer_->FocusOnSelectedSource();
62 }
63
64 void BlankDetectorDesktopCapturerWrapper::OnCaptureResult(
65 Result result,
66 std::unique_ptr<DesktopFrame> frame) {
67 RTC_DCHECK(callback_);
68 if (result != Result::SUCCESS) {
69 callback_->OnCaptureResult(result, std::move(frame));
70 return;
71 }
72
73 if (!frame) {
74 ReturnTemporaryError();
75 return;
76 }
77
78 if (permanent_failure_reached()) {
79 ReturnPermanentError();
80 return;
81 }
82
83 if (!IsBlankFrame(*frame)) {
Sergey Ulanov 2017/02/25 00:05:28 Can we avoid calling it for every frame? Particula
Hzj_jie 2017/02/25 02:06:38 Done.
Sergey Ulanov 2017/02/27 23:26:25 I don't think we should be doing this unless we kn
Hzj_jie 2017/02/28 00:13:17 Done.
84 failure_count_ = 0;
85 callback_->OnCaptureResult(Result::SUCCESS, std::move(frame));
86 return;
87 }
88
89 if (permanent_failure_enabled()) {
90 failure_count_++;
91 if (permanent_failure_reached()) {
92 ReturnPermanentError();
93 return;
94 }
95 }
96 ReturnTemporaryError();
97 }
98
99 bool BlankDetectorDesktopCapturerWrapper::IsBlankFrame(
100 const DesktopFrame& frame) const {
101 const int size =
102 (frame.size().width() < frame.size().height() ? frame.size().width()
Sergey Ulanov 2017/02/25 00:05:29 std::min(frame.size().width(), frame.size().height
Hzj_jie 2017/02/25 02:06:38 Done.
103 : frame.size().height());
104 // We will check 303 pixels for a frame with 1024 x 768 resolution.
105 for (int i = 0; i < size; i += 10) {
106 const int x1 = i;
107 const int x2 = frame.size().width() - i - 1;
108 const int y1 = i;
109 const int y2 = frame.size().height() - i - 1;
Sergey Ulanov 2017/02/25 00:05:28 As far as I can tell this always ignores center of
Hzj_jie 2017/02/25 02:06:38 The center pixel has been checked at the end of th
110 if (!IsBlankPixel(frame, x1, y1) || !IsBlankPixel(frame, x1, y2) ||
111 !IsBlankPixel(frame, x2, y1) || !IsBlankPixel(frame, x2, y2)) {
Sergey Ulanov 2017/02/25 00:05:29 I would expect performance of this code to be high
Hzj_jie 2017/02/25 02:06:38 If we check pixels sequentially (Say, one per 1000
112 return false;
113 }
114 }
115
116 return IsBlankPixel(frame, frame.size().width() >> 1,
117 frame.size().height() >> 1);
118 }
119
120 bool BlankDetectorDesktopCapturerWrapper::IsBlankPixel(
121 const DesktopFrame& frame,
122 int x,
123 int y) const {
124 uint8_t* pixel_data = frame.GetFrameDataAtPos(DesktopVector(x, y));
125 return RgbaColor(pixel_data) == blank_pixel_;
126 }
127
128 bool BlankDetectorDesktopCapturerWrapper::permanent_failure_enabled() const {
129 return permanent_failure_threshold_ >= 0;
130 }
131
132 bool BlankDetectorDesktopCapturerWrapper::permanent_failure_reached() const {
133 return permanent_failure_enabled() &&
134 failure_count_ >= permanent_failure_threshold_;
135 }
136
137 void BlankDetectorDesktopCapturerWrapper::ReturnPermanentError() {
138 callback_->OnCaptureResult(Result::ERROR_PERMANENT,
139 std::unique_ptr<DesktopFrame>());
140 }
141
142 void BlankDetectorDesktopCapturerWrapper::ReturnTemporaryError() {
143 callback_->OnCaptureResult(Result::ERROR_TEMPORARY,
144 std::unique_ptr<DesktopFrame>());
145 }
146
147 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698