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 <memory> |
| 14 #include <utility> |
| 15 |
| 16 #include "webrtc/modules/desktop_capture/desktop_capturer.h" |
| 17 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 #include "webrtc/modules/desktop_capture/desktop_frame_generator.h" |
| 19 #include "webrtc/modules/desktop_capture/fake_desktop_capturer.h" |
| 20 #include "webrtc/test/gtest.h" |
| 21 |
| 22 namespace webrtc { |
| 23 |
| 24 class BlankDetectorDesktopCapturerWrapperTest |
| 25 : public testing::Test, |
| 26 public DesktopCapturer::Callback { |
| 27 public: |
| 28 BlankDetectorDesktopCapturerWrapperTest(); |
| 29 ~BlankDetectorDesktopCapturerWrapperTest() override; |
| 30 |
| 31 protected: |
| 32 void PerfTest(DesktopCapturer* capturer); |
| 33 |
| 34 const int frame_width_ = 1024; |
| 35 const int frame_height_ = 768; |
| 36 const int permanent_failure_threshold_ = 100; |
| 37 std::unique_ptr<BlankDetectorDesktopCapturerWrapper> wrapper_; |
| 38 DesktopCapturer* capturer_ = nullptr; |
| 39 BlackWhiteDesktopFramePainter painter_; |
| 40 int num_frames_captured_ = 0; |
| 41 DesktopCapturer::Result last_result_ = DesktopCapturer::Result::SUCCESS; |
| 42 std::unique_ptr<DesktopFrame> last_frame_; |
| 43 |
| 44 private: |
| 45 // DesktopCapturer::Callback interface. |
| 46 void OnCaptureResult(DesktopCapturer::Result result, |
| 47 std::unique_ptr<DesktopFrame> frame) override; |
| 48 |
| 49 PainterDesktopFrameGenerator frame_generator_; |
| 50 }; |
| 51 |
| 52 BlankDetectorDesktopCapturerWrapperTest:: |
| 53 BlankDetectorDesktopCapturerWrapperTest() { |
| 54 frame_generator_.size()->set(frame_width_, frame_height_); |
| 55 frame_generator_.set_desktop_frame_painter(&painter_); |
| 56 std::unique_ptr<DesktopCapturer> capturer(new FakeDesktopCapturer()); |
| 57 FakeDesktopCapturer* fake_capturer = |
| 58 static_cast<FakeDesktopCapturer*>(capturer.get()); |
| 59 fake_capturer->set_frame_generator(&frame_generator_); |
| 60 capturer_ = fake_capturer; |
| 61 wrapper_.reset(new BlankDetectorDesktopCapturerWrapper( |
| 62 std::move(capturer), RgbaColor(0, 0, 0, 0), |
| 63 permanent_failure_threshold_)); |
| 64 wrapper_->Start(this); |
| 65 } |
| 66 |
| 67 BlankDetectorDesktopCapturerWrapperTest:: |
| 68 ~BlankDetectorDesktopCapturerWrapperTest() = default; |
| 69 |
| 70 void BlankDetectorDesktopCapturerWrapperTest::OnCaptureResult( |
| 71 DesktopCapturer::Result result, |
| 72 std::unique_ptr<DesktopFrame> frame) { |
| 73 last_result_ = result; |
| 74 last_frame_ = std::move(frame); |
| 75 num_frames_captured_++; |
| 76 } |
| 77 |
| 78 void BlankDetectorDesktopCapturerWrapperTest::PerfTest( |
| 79 DesktopCapturer* capturer) { |
| 80 for (int i = 0; i < 10000; i++) { |
| 81 // According to the implementation detail, this pixel is checked at last. |
| 82 painter_.updated_region()->AddRect( |
| 83 DesktopRect::MakeXYWH(frame_width_ >> 1, frame_height_ >> 1, 1, 1)); |
| 84 capturer->CaptureFrame(); |
| 85 ASSERT_EQ(num_frames_captured_, i + 1); |
| 86 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 87 ASSERT_TRUE(last_frame_); |
| 88 } |
| 89 } |
| 90 |
| 91 TEST_F(BlankDetectorDesktopCapturerWrapperTest, ShouldDetectBlankFrame) { |
| 92 wrapper_->CaptureFrame(); |
| 93 ASSERT_EQ(num_frames_captured_, 1); |
| 94 ASSERT_EQ(last_result_, DesktopCapturer::Result::ERROR_TEMPORARY); |
| 95 ASSERT_FALSE(last_frame_); |
| 96 } |
| 97 |
| 98 TEST_F(BlankDetectorDesktopCapturerWrapperTest, ShouldPassBlankDetection) { |
| 99 painter_.updated_region()->AddRect(DesktopRect::MakeXYWH(0, 0, 10, 10)); |
| 100 wrapper_->CaptureFrame(); |
| 101 ASSERT_EQ(num_frames_captured_, 1); |
| 102 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 103 ASSERT_TRUE(last_frame_); |
| 104 |
| 105 painter_.updated_region()->AddRect( |
| 106 DesktopRect::MakeXYWH(frame_width_ - 10, frame_height_ - 10, 10, 10)); |
| 107 wrapper_->CaptureFrame(); |
| 108 ASSERT_EQ(num_frames_captured_, 2); |
| 109 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 110 ASSERT_TRUE(last_frame_); |
| 111 |
| 112 painter_.updated_region()->AddRect( |
| 113 DesktopRect::MakeXYWH(0, frame_height_ - 10, 10, 10)); |
| 114 wrapper_->CaptureFrame(); |
| 115 ASSERT_EQ(num_frames_captured_, 3); |
| 116 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 117 ASSERT_TRUE(last_frame_); |
| 118 |
| 119 painter_.updated_region()->AddRect( |
| 120 DesktopRect::MakeXYWH(frame_width_ - 10, 0, 10, 10)); |
| 121 wrapper_->CaptureFrame(); |
| 122 ASSERT_EQ(num_frames_captured_, 4); |
| 123 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 124 ASSERT_TRUE(last_frame_); |
| 125 |
| 126 painter_.updated_region()->AddRect(DesktopRect::MakeXYWH( |
| 127 (frame_width_ >> 1) - 5, (frame_height_ >> 1) - 5, 10, 10)); |
| 128 wrapper_->CaptureFrame(); |
| 129 ASSERT_EQ(num_frames_captured_, 5); |
| 130 ASSERT_EQ(last_result_, DesktopCapturer::Result::SUCCESS); |
| 131 ASSERT_TRUE(last_frame_); |
| 132 } |
| 133 |
| 134 TEST_F(BlankDetectorDesktopCapturerWrapperTest, ShouldReturnPermanentError) { |
| 135 for (int i = 0; i < permanent_failure_threshold_ - 1; i++) { |
| 136 wrapper_->CaptureFrame(); |
| 137 ASSERT_EQ(num_frames_captured_, i + 1); |
| 138 ASSERT_EQ(last_result_, DesktopCapturer::Result::ERROR_TEMPORARY); |
| 139 ASSERT_FALSE(last_frame_); |
| 140 } |
| 141 wrapper_->CaptureFrame(); |
| 142 ASSERT_EQ(num_frames_captured_, permanent_failure_threshold_); |
| 143 ASSERT_EQ(last_result_, DesktopCapturer::Result::ERROR_PERMANENT); |
| 144 ASSERT_FALSE(last_frame_); |
| 145 } |
| 146 |
| 147 // There is no perceptible impact by using BlankDetectorDesktopCapturerWrapper. |
| 148 // i.e. less than 0.01ms per frame. |
| 149 // [ OK ] DISABLED_Performance (8555 ms) |
| 150 // [ OK ] DISABLED_PerformanceComparison (8456 ms) |
| 151 TEST_F(BlankDetectorDesktopCapturerWrapperTest, DISABLED_Performance) { |
| 152 PerfTest(wrapper_.get()); |
| 153 } |
| 154 |
| 155 TEST_F(BlankDetectorDesktopCapturerWrapperTest, |
| 156 DISABLED_PerformanceComparison) { |
| 157 capturer_->Start(this); |
| 158 PerfTest(capturer_); |
| 159 } |
| 160 |
| 161 } // namespace webrtc |
OLD | NEW |