| OLD | NEW |
| (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/desktop_capturer_differ_wrapper.h" | |
| 12 | |
| 13 #include <initializer_list> | |
| 14 #include <memory> | |
| 15 #include <utility> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "webrtc/base/random.h" | |
| 19 #include "webrtc/base/timeutils.h" | |
| 20 #include "webrtc/modules/desktop_capture/desktop_geometry.h" | |
| 21 #include "webrtc/modules/desktop_capture/desktop_region.h" | |
| 22 #include "webrtc/modules/desktop_capture/differ_block.h" | |
| 23 #include "webrtc/modules/desktop_capture/fake_screen_capturer.h" | |
| 24 #include "webrtc/modules/desktop_capture/mock_desktop_capturer_callback.h" | |
| 25 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" | |
| 26 #include "webrtc/test/gtest.h" | |
| 27 #include "webrtc/typedefs.h" | |
| 28 | |
| 29 namespace webrtc { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Compares and asserts |frame|.updated_region() equals to |rects|. This | |
| 34 // function does not care about the order of the |rects| and it does not expect | |
| 35 // DesktopRegion to return an exact area for each rectangle in |rects|. | |
| 36 template <template <typename, typename...> class T = std::initializer_list, | |
| 37 typename... Rect> | |
| 38 void AssertUpdatedRegionIs(const DesktopFrame& frame, | |
| 39 const T<DesktopRect, Rect...>& rects) { | |
| 40 DesktopRegion region; | |
| 41 for (const auto& rect : rects) { | |
| 42 region.AddRect(rect); | |
| 43 } | |
| 44 ASSERT_TRUE(frame.updated_region().Equals(region)); | |
| 45 } | |
| 46 | |
| 47 // Compares and asserts |frame|.updated_region() covers all rectangles in | |
| 48 // |rects|, but does not cover areas other than a kBlockSize expansion. This | |
| 49 // function does not care about the order of the |rects|, and it does not expect | |
| 50 // DesktopRegion to return an exact area of each rectangle in |rects|. | |
| 51 template <template <typename, typename...> class T = std::initializer_list, | |
| 52 typename... Rect> | |
| 53 void AssertUpdatedRegionCovers(const DesktopFrame& frame, | |
| 54 const T<DesktopRect, Rect...>& rects) { | |
| 55 DesktopRegion region; | |
| 56 for (const auto& rect : rects) { | |
| 57 region.AddRect(rect); | |
| 58 } | |
| 59 | |
| 60 // Intersect of |rects| and |frame|.updated_region() should be |rects|. i.e. | |
| 61 // |frame|.updated_region() should be a superset of |rects|. | |
| 62 DesktopRegion intersect(region); | |
| 63 intersect.IntersectWith(frame.updated_region()); | |
| 64 ASSERT_TRUE(region.Equals(intersect)); | |
| 65 | |
| 66 // Difference between |rects| and |frame|.updated_region() should not cover | |
| 67 // areas which have larger than twice of kBlockSize width and height. | |
| 68 // | |
| 69 // Explanation of the 'twice' of kBlockSize (indeed kBlockSize * 2 - 2) is | |
| 70 // following, | |
| 71 // (Each block in the following grid is a 8 x 8 pixels area. X means the real | |
| 72 // updated area, m means the updated area marked by | |
| 73 // DesktopCapturerDifferWrapper.) | |
| 74 // +---+---+---+---+---+---+---+---+ | |
| 75 // | X | m | m | m | m | m | m | m | | |
| 76 // +---+---+---+---+---+---+---+---+ | |
| 77 // | m | m | m | m | m | m | m | m | | |
| 78 // +---+---+---+---+---+---+---+---+ | |
| 79 // | m | m | m | m | m | m | m | m | | |
| 80 // +---+---+---+---+---+---+---+---+ | |
| 81 // | m | m | m | m | m | m | m | X | | |
| 82 // +---+---+---+---+---+---+---+---+ | |
| 83 // The top left [0, 0] - [8, 8] and right bottom [56, 24] - [64, 32] blocks of | |
| 84 // this area are updated. But since DesktopCapturerDifferWrapper compares | |
| 85 // 32 x 32 blocks by default, this entire area is marked as updated. So the | |
| 86 // [8, 8] - [56, 32] is expected to be covered in the difference. | |
| 87 // | |
| 88 // But if [0, 0] - [8, 8] and [64, 24] - [72, 32] blocks are updated, | |
| 89 // +---+---+---+---+---+---+---+---+---+---+---+---+ | |
| 90 // | X | m | m | m | | | | | m | m | m | m | | |
| 91 // +---+---+---+---+---+---+---+---+---+---+---+---+ | |
| 92 // | m | m | m | m | | | | | m | m | m | m | | |
| 93 // +---+---+---+---+---+---+---+---+---+---+---+---+ | |
| 94 // | m | m | m | m | | | | | m | m | m | m | | |
| 95 // +---+---+---+---+---+---+---+---+---+---+---+---+ | |
| 96 // | m | m | m | m | | | | | X | m | m | m | | |
| 97 // +---+---+---+---+---+---+---+---+---+---+---+---+ | |
| 98 // the [8, 8] - [64, 32] is not expected to be covered in the difference. As | |
| 99 // DesktopCapturerDifferWrapper should only mark [0, 0] - [32, 32] and | |
| 100 // [64, 0] - [96, 32] as updated. | |
| 101 DesktopRegion differ(frame.updated_region()); | |
| 102 differ.Subtract(region); | |
| 103 for (DesktopRegion::Iterator it(differ); !it.IsAtEnd(); it.Advance()) { | |
| 104 ASSERT_TRUE(it.rect().width() <= kBlockSize * 2 - 2 || | |
| 105 it.rect().height() <= kBlockSize * 2 - 2); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Executes a DesktopCapturerDifferWrapper::Capture() and compares its output | |
| 110 // DesktopFrame::updated_region() with |updated_region| if |check_result| is | |
| 111 // true. If |exactly_match| is true, AssertUpdatedRegionIs() will be used, | |
| 112 // otherwise AssertUpdatedRegionCovers() will be used. | |
| 113 template <template <typename, typename...> class T = std::initializer_list, | |
| 114 typename... Rect> | |
| 115 void ExecuteDifferWrapperCase(BlackWhiteDesktopFramePainter* frame_painter, | |
| 116 DesktopCapturerDifferWrapper* capturer, | |
| 117 MockDesktopCapturerCallback* callback, | |
| 118 const T<DesktopRect, Rect...>& updated_region, | |
| 119 bool check_result, | |
| 120 bool exactly_match) { | |
| 121 EXPECT_CALL(*callback, | |
| 122 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) | |
| 123 .Times(1) | |
| 124 .WillOnce(testing::Invoke([&updated_region, check_result, exactly_match]( | |
| 125 DesktopCapturer::Result result, | |
| 126 std::unique_ptr<DesktopFrame>* frame) { | |
| 127 ASSERT_EQ(result, DesktopCapturer::Result::SUCCESS); | |
| 128 if (check_result) { | |
| 129 if (exactly_match) { | |
| 130 AssertUpdatedRegionIs(**frame, updated_region); | |
| 131 } else { | |
| 132 AssertUpdatedRegionCovers(**frame, updated_region); | |
| 133 } | |
| 134 } | |
| 135 })); | |
| 136 for (const auto& rect : updated_region) { | |
| 137 frame_painter->updated_region()->AddRect(rect); | |
| 138 } | |
| 139 capturer->CaptureFrame(); | |
| 140 } | |
| 141 | |
| 142 // Executes a DesktopCapturerDifferWrapper::Capture(), if updated_region() is | |
| 143 // not set, this function will reset DesktopCapturerDifferWrapper internal | |
| 144 // DesktopFrame into black. | |
| 145 void ExecuteCapturer(DesktopCapturerDifferWrapper* capturer, | |
| 146 MockDesktopCapturerCallback* callback) { | |
| 147 EXPECT_CALL(*callback, | |
| 148 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) | |
| 149 .Times(1); | |
| 150 capturer->CaptureFrame(); | |
| 151 } | |
| 152 | |
| 153 void ExecuteDifferWrapperTest(bool with_hints, | |
| 154 bool enlarge_updated_region, | |
| 155 bool random_updated_region, | |
| 156 bool check_result) { | |
| 157 const bool updated_region_should_exactly_match = | |
| 158 with_hints && !enlarge_updated_region && !random_updated_region; | |
| 159 BlackWhiteDesktopFramePainter frame_painter; | |
| 160 PainterDesktopFrameGenerator frame_generator; | |
| 161 frame_generator.set_desktop_frame_painter(&frame_painter); | |
| 162 std::unique_ptr<FakeDesktopCapturer<>> fake(new FakeDesktopCapturer<>()); | |
| 163 fake->set_frame_generator(&frame_generator); | |
| 164 DesktopCapturerDifferWrapper capturer(std::move(fake)); | |
| 165 MockDesktopCapturerCallback callback; | |
| 166 frame_generator.set_provide_updated_region_hints(with_hints); | |
| 167 frame_generator.set_enlarge_updated_region(enlarge_updated_region); | |
| 168 frame_generator.set_add_random_updated_region(random_updated_region); | |
| 169 | |
| 170 capturer.Start(&callback); | |
| 171 | |
| 172 EXPECT_CALL(callback, | |
| 173 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) | |
| 174 .Times(1) | |
| 175 .WillOnce(testing::Invoke([](DesktopCapturer::Result result, | |
| 176 std::unique_ptr<DesktopFrame>* frame) { | |
| 177 ASSERT_EQ(result, DesktopCapturer::Result::SUCCESS); | |
| 178 AssertUpdatedRegionIs(**frame, | |
| 179 {DesktopRect::MakeSize((*frame)->size())}); | |
| 180 })); | |
| 181 capturer.CaptureFrame(); | |
| 182 | |
| 183 ExecuteDifferWrapperCase(&frame_painter, &capturer, &callback, | |
| 184 {DesktopRect::MakeLTRB(100, 100, 200, 200), | |
| 185 DesktopRect::MakeLTRB(300, 300, 400, 400)}, | |
| 186 check_result, updated_region_should_exactly_match); | |
| 187 ExecuteCapturer(&capturer, &callback); | |
| 188 | |
| 189 ExecuteDifferWrapperCase( | |
| 190 &frame_painter, &capturer, &callback, | |
| 191 {DesktopRect::MakeLTRB(0, 0, 40, 40), | |
| 192 DesktopRect::MakeLTRB(0, frame_generator.size()->height() - 40, 40, | |
| 193 frame_generator.size()->height()), | |
| 194 DesktopRect::MakeLTRB(frame_generator.size()->width() - 40, 0, | |
| 195 frame_generator.size()->width(), 40), | |
| 196 DesktopRect::MakeLTRB(frame_generator.size()->width() - 40, | |
| 197 frame_generator.size()->height() - 40, | |
| 198 frame_generator.size()->width(), | |
| 199 frame_generator.size()->height())}, | |
| 200 check_result, updated_region_should_exactly_match); | |
| 201 | |
| 202 Random random(rtc::TimeMillis()); | |
| 203 // Fuzzing tests. | |
| 204 for (int i = 0; i < 1000; i++) { | |
| 205 if (enlarge_updated_region) { | |
| 206 frame_generator.set_enlarge_range(random.Rand(1, 50)); | |
| 207 } | |
| 208 frame_generator.size()->set(random.Rand(500, 2000), random.Rand(500, 2000)); | |
| 209 ExecuteCapturer(&capturer, &callback); | |
| 210 std::vector<DesktopRect> updated_region; | |
| 211 for (int j = random.Rand(50); j >= 0; j--) { | |
| 212 // At least a 1 x 1 updated region. | |
| 213 const int left = random.Rand(0, frame_generator.size()->width() - 2); | |
| 214 const int top = random.Rand(0, frame_generator.size()->height() - 2); | |
| 215 const int right = random.Rand(left + 1, frame_generator.size()->width()); | |
| 216 const int bottom = random.Rand(top + 1, frame_generator.size()->height()); | |
| 217 updated_region.push_back(DesktopRect::MakeLTRB(left, top, right, bottom)); | |
| 218 } | |
| 219 ExecuteDifferWrapperCase(&frame_painter, &capturer, &callback, | |
| 220 updated_region, check_result, | |
| 221 updated_region_should_exactly_match); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 } // namespace | |
| 226 | |
| 227 TEST(DesktopCapturerDifferWrapperTest, CaptureWithoutHints) { | |
| 228 ExecuteDifferWrapperTest(false, false, false, true); | |
| 229 } | |
| 230 | |
| 231 TEST(DesktopCapturerDifferWrapperTest, CaptureWithHints) { | |
| 232 ExecuteDifferWrapperTest(true, false, false, true); | |
| 233 } | |
| 234 | |
| 235 TEST(DesktopCapturerDifferWrapperTest, CaptureWithEnlargedHints) { | |
| 236 ExecuteDifferWrapperTest(true, true, false, true); | |
| 237 } | |
| 238 | |
| 239 TEST(DesktopCapturerDifferWrapperTest, CaptureWithRandomHints) { | |
| 240 ExecuteDifferWrapperTest(true, false, true, true); | |
| 241 } | |
| 242 | |
| 243 TEST(DesktopCapturerDifferWrapperTest, CaptureWithEnlargedAndRandomHints) { | |
| 244 ExecuteDifferWrapperTest(true, true, true, true); | |
| 245 } | |
| 246 | |
| 247 // When hints are provided, DesktopCapturerDifferWrapper has a slightly better | |
| 248 // performance in current configuration, but not so significant. Following is | |
| 249 // one run result. | |
| 250 // [ RUN ] DISABLED_CaptureWithoutHintsPerf | |
| 251 // [ OK ] DISABLED_CaptureWithoutHintsPerf (7118 ms) | |
| 252 // [ RUN ] DISABLED_CaptureWithHintsPerf | |
| 253 // [ OK ] DISABLED_CaptureWithHintsPerf (5580 ms) | |
| 254 // [ RUN ] DISABLED_CaptureWithEnlargedHintsPerf | |
| 255 // [ OK ] DISABLED_CaptureWithEnlargedHintsPerf (5974 ms) | |
| 256 // [ RUN ] DISABLED_CaptureWithRandomHintsPerf | |
| 257 // [ OK ] DISABLED_CaptureWithRandomHintsPerf (6184 ms) | |
| 258 // [ RUN ] DISABLED_CaptureWithEnlargedAndRandomHintsPerf | |
| 259 // [ OK ] DISABLED_CaptureWithEnlargedAndRandomHintsPerf (6347 ms) | |
| 260 TEST(DesktopCapturerDifferWrapperTest, DISABLED_CaptureWithoutHintsPerf) { | |
| 261 int64_t started = rtc::TimeMillis(); | |
| 262 ExecuteDifferWrapperTest(false, false, false, false); | |
| 263 ASSERT_LE(rtc::TimeMillis() - started, 15000); | |
| 264 } | |
| 265 | |
| 266 TEST(DesktopCapturerDifferWrapperTest, DISABLED_CaptureWithHintsPerf) { | |
| 267 int64_t started = rtc::TimeMillis(); | |
| 268 ExecuteDifferWrapperTest(true, false, false, false); | |
| 269 ASSERT_LE(rtc::TimeMillis() - started, 15000); | |
| 270 } | |
| 271 | |
| 272 TEST(DesktopCapturerDifferWrapperTest, DISABLED_CaptureWithEnlargedHintsPerf) { | |
| 273 int64_t started = rtc::TimeMillis(); | |
| 274 ExecuteDifferWrapperTest(true, true, false, false); | |
| 275 ASSERT_LE(rtc::TimeMillis() - started, 15000); | |
| 276 } | |
| 277 | |
| 278 TEST(DesktopCapturerDifferWrapperTest, DISABLED_CaptureWithRandomHintsPerf) { | |
| 279 int64_t started = rtc::TimeMillis(); | |
| 280 ExecuteDifferWrapperTest(true, false, true, false); | |
| 281 ASSERT_LE(rtc::TimeMillis() - started, 15000); | |
| 282 } | |
| 283 | |
| 284 TEST(DesktopCapturerDifferWrapperTest, | |
| 285 DISABLED_CaptureWithEnlargedAndRandomHintsPerf) { | |
| 286 int64_t started = rtc::TimeMillis(); | |
| 287 ExecuteDifferWrapperTest(true, true, true, false); | |
| 288 ASSERT_LE(rtc::TimeMillis() - started, 15000); | |
| 289 } | |
| 290 | |
| 291 } // namespace webrtc | |
| OLD | NEW |