OLD | NEW |
---|---|
(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 "webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper .h" | |
12 | |
13 #include <algorithm> | |
14 #include <utility> | |
15 | |
16 #include "webrtc/base/checks.h" | |
17 #include "webrtc/modules/desktop_capture/desktop_geometry.h" | |
18 #include "webrtc/system_wrappers/include/metrics.h" | |
19 | |
20 namespace webrtc { | |
21 | |
22 BlankDetectorDesktopCapturerWrapper::BlankDetectorDesktopCapturerWrapper( | |
23 std::unique_ptr<DesktopCapturer> capturer, | |
24 RgbaColor blank_pixel, | |
25 int permanent_failure_threshold) | |
26 : capturer_(std::move(capturer)), | |
27 blank_pixel_(blank_pixel), | |
28 permanent_failure_threshold_(permanent_failure_threshold) { | |
29 RTC_DCHECK(capturer_); | |
30 } | |
31 | |
32 BlankDetectorDesktopCapturerWrapper::~BlankDetectorDesktopCapturerWrapper() = | |
33 default; | |
34 | |
35 void BlankDetectorDesktopCapturerWrapper::Start( | |
36 DesktopCapturer::Callback* callback) { | |
37 capturer_->Start(this); | |
38 callback_ = callback; | |
39 } | |
40 | |
41 void BlankDetectorDesktopCapturerWrapper::SetSharedMemoryFactory( | |
42 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { | |
43 capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); | |
44 } | |
45 | |
46 void BlankDetectorDesktopCapturerWrapper::CaptureFrame() { | |
47 RTC_DCHECK(callback_); | |
48 if (permanent_failure_reached()) { | |
49 ReturnPermanentError(); | |
50 } else { | |
51 capturer_->CaptureFrame(); | |
52 } | |
53 } | |
54 | |
55 void BlankDetectorDesktopCapturerWrapper::SetExcludedWindow(WindowId window) { | |
56 capturer_->SetExcludedWindow(window); | |
57 } | |
58 | |
59 bool BlankDetectorDesktopCapturerWrapper::GetSourceList(SourceList* sources) { | |
60 return capturer_->GetSourceList(sources); | |
61 } | |
62 | |
63 bool BlankDetectorDesktopCapturerWrapper::SelectSource(SourceId id) { | |
64 return capturer_->SelectSource(id); | |
65 } | |
66 | |
67 bool BlankDetectorDesktopCapturerWrapper::FocusOnSelectedSource() { | |
68 return capturer_->FocusOnSelectedSource(); | |
69 } | |
70 | |
71 void BlankDetectorDesktopCapturerWrapper::OnCaptureResult( | |
72 Result result, | |
73 std::unique_ptr<DesktopFrame> frame) { | |
74 RTC_DCHECK(callback_); | |
75 if (result != Result::SUCCESS || non_blank_frame_received_) { | |
76 callback_->OnCaptureResult(result, std::move(frame)); | |
77 return; | |
78 } | |
79 | |
80 RTC_DCHECK(frame); | |
81 RTC_DCHECK(!permanent_failure_reached()); | |
82 | |
83 // If nothing has been changed in current frame, we do not need to check it | |
84 // again. | |
85 if (!frame->updated_region().is_empty() || is_first_frame_) { | |
86 last_frame_is_blank_ = IsBlankFrame(*frame); | |
87 is_first_frame_ = false; | |
88 } | |
89 RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.BlankFrameDetected", | |
90 last_frame_is_blank_); | |
91 if (!last_frame_is_blank_) { | |
92 non_blank_frame_received_ = true; | |
93 callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); | |
94 return; | |
95 } | |
96 | |
97 if (permanent_failure_enabled()) { | |
98 failure_count_++; | |
99 if (permanent_failure_reached()) { | |
100 ReturnPermanentError(); | |
101 return; | |
102 } | |
103 } | |
104 | |
105 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, | |
106 std::unique_ptr<DesktopFrame>()); | |
107 } | |
108 | |
109 bool BlankDetectorDesktopCapturerWrapper::IsBlankFrame( | |
110 const DesktopFrame& frame) const { | |
111 // We will check 768 pixels for a frame with 1024 x 768 resolution. | |
Sergey Ulanov
2017/03/02 23:45:51
update this comment
Hzj_jie
2017/03/03 02:29:41
Done.
| |
112 for (int i = 0; i < frame.size().width() * frame.size().height(); i += 105) { | |
113 const int x = i % frame.size().width(); | |
114 const int y = i / frame.size().width(); | |
115 if (!IsBlankPixel(frame, x, y)) { | |
116 return false; | |
117 } | |
118 } | |
119 | |
120 // We are verifying the pixel in the center as well. | |
121 return IsBlankPixel(frame, frame.size().width() / 2, | |
122 frame.size().height() / 2); | |
123 } | |
124 | |
125 bool BlankDetectorDesktopCapturerWrapper::IsBlankPixel( | |
126 const DesktopFrame& frame, | |
127 int x, | |
128 int y) const { | |
129 uint8_t* pixel_data = frame.GetFrameDataAtPos(DesktopVector(x, y)); | |
130 return RgbaColor(pixel_data) == blank_pixel_; | |
131 } | |
132 | |
133 bool BlankDetectorDesktopCapturerWrapper::permanent_failure_enabled() const { | |
134 return permanent_failure_threshold_ >= 0; | |
135 } | |
136 | |
137 bool BlankDetectorDesktopCapturerWrapper::permanent_failure_reached() const { | |
138 return permanent_failure_enabled() && | |
139 failure_count_ >= permanent_failure_threshold_; | |
140 } | |
141 | |
142 void BlankDetectorDesktopCapturerWrapper::ReturnPermanentError() { | |
143 callback_->OnCaptureResult(Result::ERROR_PERMANENT, | |
144 std::unique_ptr<DesktopFrame>()); | |
145 } | |
146 | |
147 } // namespace webrtc | |
OLD | NEW |