Chromium Code Reviews| Index: webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper.cc |
| diff --git a/webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper.cc b/webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ae3ba67f500d18441a4411016f407ac99cc09d6e |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper.cc |
| @@ -0,0 +1,147 @@ |
| +/* |
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/desktop_capture/blank_detector_desktop_capturer_wrapper.h" |
| + |
| +#include <algorithm> |
| +#include <utility> |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/modules/desktop_capture/desktop_geometry.h" |
| +#include "webrtc/system_wrappers/include/metrics.h" |
| + |
| +namespace webrtc { |
| + |
| +BlankDetectorDesktopCapturerWrapper::BlankDetectorDesktopCapturerWrapper( |
| + std::unique_ptr<DesktopCapturer> capturer, |
| + RgbaColor blank_pixel, |
| + int permanent_failure_threshold) |
| + : capturer_(std::move(capturer)), |
| + blank_pixel_(blank_pixel), |
| + permanent_failure_threshold_(permanent_failure_threshold) { |
| + RTC_DCHECK(capturer_); |
| +} |
| + |
| +BlankDetectorDesktopCapturerWrapper::~BlankDetectorDesktopCapturerWrapper() = |
| + default; |
| + |
| +void BlankDetectorDesktopCapturerWrapper::Start( |
| + DesktopCapturer::Callback* callback) { |
| + capturer_->Start(this); |
| + callback_ = callback; |
| +} |
| + |
| +void BlankDetectorDesktopCapturerWrapper::SetSharedMemoryFactory( |
| + std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { |
| + capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); |
| +} |
| + |
| +void BlankDetectorDesktopCapturerWrapper::CaptureFrame() { |
| + RTC_DCHECK(callback_); |
| + if (permanent_failure_reached()) { |
| + ReturnPermanentError(); |
| + } else { |
| + capturer_->CaptureFrame(); |
| + } |
| +} |
| + |
| +void BlankDetectorDesktopCapturerWrapper::SetExcludedWindow(WindowId window) { |
| + capturer_->SetExcludedWindow(window); |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::GetSourceList(SourceList* sources) { |
| + return capturer_->GetSourceList(sources); |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::SelectSource(SourceId id) { |
| + return capturer_->SelectSource(id); |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::FocusOnSelectedSource() { |
| + return capturer_->FocusOnSelectedSource(); |
| +} |
| + |
| +void BlankDetectorDesktopCapturerWrapper::OnCaptureResult( |
| + Result result, |
| + std::unique_ptr<DesktopFrame> frame) { |
| + RTC_DCHECK(callback_); |
| + if (result != Result::SUCCESS || non_blank_frame_received_) { |
| + callback_->OnCaptureResult(result, std::move(frame)); |
| + return; |
| + } |
| + |
| + RTC_DCHECK(frame); |
| + RTC_DCHECK(!permanent_failure_reached()); |
| + |
| + // If nothing has been changed in current frame, we do not need to check it |
| + // again. |
| + if (!frame->updated_region().is_empty() || is_first_frame_) { |
| + last_frame_is_blank_ = IsBlankFrame(*frame); |
| + is_first_frame_ = false; |
| + } |
| + RTC_HISTOGRAM_BOOLEAN("WebRTC.DesktopCapture.BlankFrameDetected", |
| + last_frame_is_blank_); |
| + if (!last_frame_is_blank_) { |
| + non_blank_frame_received_ = true; |
| + callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); |
| + return; |
| + } |
| + |
| + if (permanent_failure_enabled()) { |
| + failure_count_++; |
| + if (permanent_failure_reached()) { |
| + ReturnPermanentError(); |
| + return; |
| + } |
| + } |
| + |
| + callback_->OnCaptureResult(Result::ERROR_TEMPORARY, |
| + std::unique_ptr<DesktopFrame>()); |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::IsBlankFrame( |
| + const DesktopFrame& frame) const { |
| + // 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.
|
| + for (int i = 0; i < frame.size().width() * frame.size().height(); i += 105) { |
| + const int x = i % frame.size().width(); |
| + const int y = i / frame.size().width(); |
| + if (!IsBlankPixel(frame, x, y)) { |
| + return false; |
| + } |
| + } |
| + |
| + // We are verifying the pixel in the center as well. |
| + return IsBlankPixel(frame, frame.size().width() / 2, |
| + frame.size().height() / 2); |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::IsBlankPixel( |
| + const DesktopFrame& frame, |
| + int x, |
| + int y) const { |
| + uint8_t* pixel_data = frame.GetFrameDataAtPos(DesktopVector(x, y)); |
| + return RgbaColor(pixel_data) == blank_pixel_; |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::permanent_failure_enabled() const { |
| + return permanent_failure_threshold_ >= 0; |
| +} |
| + |
| +bool BlankDetectorDesktopCapturerWrapper::permanent_failure_reached() const { |
| + return permanent_failure_enabled() && |
| + failure_count_ >= permanent_failure_threshold_; |
| +} |
| + |
| +void BlankDetectorDesktopCapturerWrapper::ReturnPermanentError() { |
| + callback_->OnCaptureResult(Result::ERROR_PERMANENT, |
| + std::unique_ptr<DesktopFrame>()); |
| +} |
| + |
| +} // namespace webrtc |