blob: 198e764b55e83aba37f740dd013e5c81f714cc50 [file] [log] [blame]
zijiehe6be0a652016-10-27 16:50:35 -07001/*
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 <string.h>
12
13#include <algorithm>
14#include <initializer_list>
zijiehe21841552016-11-18 20:31:02 -080015#include <iostream> // TODO(zijiehe): Remove once flaky has been resolved.
zijiehe6be0a652016-10-27 16:50:35 -070016#include <memory>
17#include <utility>
18
zijiehe21841552016-11-18 20:31:02 -080019// TODO(zijiehe): Remove once flaky has been resolved.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/desktop_capture/desktop_capture_options.h"
21#include "modules/desktop_capture/desktop_capturer.h"
22#include "modules/desktop_capture/desktop_frame.h"
23#include "modules/desktop_capture/desktop_region.h"
24#include "modules/desktop_capture/mock_desktop_capturer_callback.h"
25#include "modules/desktop_capture/rgba_color.h"
26#include "modules/desktop_capture/screen_drawer.h"
27#include "rtc_base/base64.h"
28#include "rtc_base/checks.h"
29#include "rtc_base/constructormagic.h"
30#include "rtc_base/logging.h"
31#include "test/gmock.h"
32#include "test/gtest.h"
zijiehe6be0a652016-10-27 16:50:35 -070033
34#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "modules/desktop_capture/win/screen_capturer_win_directx.h"
36#include "rtc_base/win32.h"
zijiehe6be0a652016-10-27 16:50:35 -070037#endif // defined(WEBRTC_WIN)
38
39using ::testing::_;
40
41namespace webrtc {
42
43namespace {
44
zijiehee83f4b32016-12-08 11:47:01 -080045ACTION_P2(SaveCaptureResult, result, dest) {
46 *result = arg0;
zijiehe6be0a652016-10-27 16:50:35 -070047 *dest = std::move(*arg1);
48}
49
50// Returns true if color in |rect| of |frame| is |color|.
51bool ArePixelsColoredBy(const DesktopFrame& frame,
52 DesktopRect rect,
53 RgbaColor color,
54 bool may_partially_draw) {
55 if (!may_partially_draw) {
56 // updated_region() should cover the painted area.
57 DesktopRegion updated_region(frame.updated_region());
58 updated_region.IntersectWith(rect);
59 if (!updated_region.Equals(DesktopRegion(rect))) {
60 return false;
61 }
62 }
63
64 // Color in the |rect| should be |color|.
65 uint8_t* row = frame.GetFrameDataAtPos(rect.top_left());
66 for (int i = 0; i < rect.height(); i++) {
67 uint8_t* column = row;
68 for (int j = 0; j < rect.width(); j++) {
69 if (color != RgbaColor(column)) {
70 return false;
71 }
72 column += DesktopFrame::kBytesPerPixel;
73 }
74 row += frame.stride();
75 }
76 return true;
77}
78
79} // namespace
80
81class ScreenCapturerIntegrationTest : public testing::Test {
82 public:
83 void SetUp() override {
zijiehe54fd5792016-11-02 14:49:35 -070084 capturer_ = DesktopCapturer::CreateScreenCapturer(
85 DesktopCaptureOptions::CreateDefault());
zijiehe6be0a652016-10-27 16:50:35 -070086 }
87
88 protected:
89 void TestCaptureUpdatedRegion(
zijiehe54fd5792016-11-02 14:49:35 -070090 std::initializer_list<DesktopCapturer*> capturers) {
zijiehe6be0a652016-10-27 16:50:35 -070091 RTC_DCHECK(capturers.size() > 0);
zijiehe54fd5792016-11-02 14:49:35 -070092 // A large enough area for the tests, which should be able to be fulfilled
93 // by most systems.
zijiehe166e59a2016-11-29 14:46:46 -080094#if defined(WEBRTC_WIN)
95 // On Windows, an interesting warning window may pop up randomly. The root
96 // cause is still under investigation, so reduce the test area to work
97 // around. Bug https://bugs.chromium.org/p/webrtc/issues/detail?id=6666.
98 const int kTestArea = 416;
99#else
zijiehe6be0a652016-10-27 16:50:35 -0700100 const int kTestArea = 512;
zijiehe166e59a2016-11-29 14:46:46 -0800101#endif
zijiehe6be0a652016-10-27 16:50:35 -0700102 const int kRectSize = 32;
103 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create();
104 if (!drawer || drawer->DrawableRegion().is_empty()) {
105 LOG(LS_WARNING) << "No ScreenDrawer implementation for current platform.";
106 return;
107 }
108 if (drawer->DrawableRegion().width() < kTestArea ||
109 drawer->DrawableRegion().height() < kTestArea) {
110 LOG(LS_WARNING) << "ScreenDrawer::DrawableRegion() is too small for the "
111 "CaptureUpdatedRegion tests.";
112 return;
113 }
114
zijiehe54fd5792016-11-02 14:49:35 -0700115 for (DesktopCapturer* capturer : capturers) {
zijiehe6be0a652016-10-27 16:50:35 -0700116 capturer->Start(&callback_);
117 }
118
119 // Draw a set of |kRectSize| by |kRectSize| rectangles at (|i|, |i|), or
120 // |i| by |i| rectangles at (|kRectSize|, |kRectSize|). One of (controlled
121 // by |c|) its primary colors is |i|, and the other two are 0x7f. So we
122 // won't draw a black or white rectangle.
123 for (int c = 0; c < 3; c++) {
124 // A fixed size rectangle.
125 for (int i = 0; i < kTestArea - kRectSize; i += 16) {
126 DesktopRect rect = DesktopRect::MakeXYWH(i, i, kRectSize, kRectSize);
127 rect.Translate(drawer->DrawableRegion().top_left());
128 RgbaColor color((c == 0 ? (i & 0xff) : 0x7f),
129 (c == 1 ? (i & 0xff) : 0x7f),
130 (c == 2 ? (i & 0xff) : 0x7f));
zijiehe21841552016-11-18 20:31:02 -0800131 // Fail fast.
132 ASSERT_NO_FATAL_FAILURE(
133 TestCaptureOneFrame(capturers, drawer.get(), rect, color));
zijiehe6be0a652016-10-27 16:50:35 -0700134 }
135
136 // A variable-size rectangle.
137 for (int i = 0; i < kTestArea - kRectSize; i += 16) {
138 DesktopRect rect = DesktopRect::MakeXYWH(kRectSize, kRectSize, i, i);
139 rect.Translate(drawer->DrawableRegion().top_left());
140 RgbaColor color((c == 0 ? (i & 0xff) : 0x7f),
141 (c == 1 ? (i & 0xff) : 0x7f),
142 (c == 2 ? (i & 0xff) : 0x7f));
zijiehe21841552016-11-18 20:31:02 -0800143 // Fail fast.
144 ASSERT_NO_FATAL_FAILURE(
145 TestCaptureOneFrame(capturers, drawer.get(), rect, color));
zijiehe6be0a652016-10-27 16:50:35 -0700146 }
147 }
148 }
149
150 void TestCaptureUpdatedRegion() {
151 TestCaptureUpdatedRegion({capturer_.get()});
152 }
153
154#if defined(WEBRTC_WIN)
155 // Enable allow_directx_capturer in DesktopCaptureOptions, but let
zijiehe54fd5792016-11-02 14:49:35 -0700156 // DesktopCapturer::CreateScreenCapturer() to decide whether a DirectX
157 // capturer should be used.
zijiehe6be0a652016-10-27 16:50:35 -0700158 void MaybeCreateDirectxCapturer() {
159 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault());
160 options.set_allow_directx_capturer(true);
zijiehe54fd5792016-11-02 14:49:35 -0700161 capturer_ = DesktopCapturer::CreateScreenCapturer(options);
zijiehe6be0a652016-10-27 16:50:35 -0700162 }
163
164 bool CreateDirectxCapturer() {
165 if (!ScreenCapturerWinDirectx::IsSupported()) {
166 LOG(LS_WARNING) << "Directx capturer is not supported";
167 return false;
168 }
169
170 MaybeCreateDirectxCapturer();
171 return true;
172 }
173
174 void CreateMagnifierCapturer() {
175 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault());
176 options.set_allow_use_magnification_api(true);
zijiehe54fd5792016-11-02 14:49:35 -0700177 capturer_ = DesktopCapturer::CreateScreenCapturer(options);
zijiehe6be0a652016-10-27 16:50:35 -0700178 }
179#endif // defined(WEBRTC_WIN)
180
zijiehe54fd5792016-11-02 14:49:35 -0700181 std::unique_ptr<DesktopCapturer> capturer_;
182 MockDesktopCapturerCallback callback_;
zijiehe6be0a652016-10-27 16:50:35 -0700183
184 private:
185 // Repeats capturing the frame by using |capturers| one-by-one for 600 times,
186 // typically 30 seconds, until they succeeded captured a |color| rectangle at
187 // |rect|. This function uses |drawer|->WaitForPendingDraws() between two
188 // attempts to wait for the screen to update.
zijiehe54fd5792016-11-02 14:49:35 -0700189 void TestCaptureOneFrame(std::vector<DesktopCapturer*> capturers,
zijiehe6be0a652016-10-27 16:50:35 -0700190 ScreenDrawer* drawer,
191 DesktopRect rect,
192 RgbaColor color) {
193 const int wait_capture_round = 600;
194 drawer->Clear();
195 size_t succeeded_capturers = 0;
196 for (int i = 0; i < wait_capture_round; i++) {
197 drawer->DrawRectangle(rect, color);
198 drawer->WaitForPendingDraws();
199 for (size_t j = 0; j < capturers.size(); j++) {
200 if (capturers[j] == nullptr) {
zijiehe54fd5792016-11-02 14:49:35 -0700201 // DesktopCapturer should return an empty updated_region() if no
zijiehe21841552016-11-18 20:31:02 -0800202 // update detected. So we won't test it again if it has captured the
203 // rectangle we drew.
zijiehe6be0a652016-10-27 16:50:35 -0700204 continue;
205 }
206 std::unique_ptr<DesktopFrame> frame = CaptureFrame(capturers[j]);
207 if (!frame) {
zijiehe21841552016-11-18 20:31:02 -0800208 // CaptureFrame() has triggered an assertion failure already, we only
209 // need to return here.
zijiehe6be0a652016-10-27 16:50:35 -0700210 return;
211 }
212
213 if (ArePixelsColoredBy(
214 *frame, rect, color, drawer->MayDrawIncompleteShapes())) {
215 capturers[j] = nullptr;
216 succeeded_capturers++;
217 }
zijiehe21841552016-11-18 20:31:02 -0800218 // The following else if statement is for debugging purpose only, which
219 // should be removed after flaky of ScreenCapturerIntegrationTest has
220 // been resolved.
221 else if (i == wait_capture_round - 1) {
222 std::string result;
223 rtc::Base64::EncodeFromArray(frame->data(),
224 frame->size().height() * frame->stride(),
225 &result);
226 std::cout << frame->size().width() << " x " << frame->size().height()
227 << std::endl;
228 // Split the entire string (can be over 4M) into several lines to
zijiehe166e59a2016-11-29 14:46:46 -0800229 // avoid browser from sticking.
zijiehe21841552016-11-18 20:31:02 -0800230 static const size_t kLineLength = 32768;
231 const char* result_end = result.c_str() + result.length();
232 for (const char* it = result.c_str();
233 it < result_end;
234 it += kLineLength) {
235 const size_t max_length = result_end - it;
236 std::cout << std::string(it, std::min(kLineLength, max_length))
237 << std::endl;
238 }
zijiehe166e59a2016-11-29 14:46:46 -0800239 std::cout << "Failed to capture rectangle " << rect.left() << " x "
240 << rect.top() << " - " << rect.right() << " x "
241 << rect.bottom() << " with color ("
242 << static_cast<int>(color.red) << ", "
243 << static_cast<int>(color.green) << ", "
244 << static_cast<int>(color.blue) << ", "
245 << static_cast<int>(color.alpha) << ")" << std::endl;
zijiehe21841552016-11-18 20:31:02 -0800246 ASSERT_TRUE(false) << "ScreenCapturerIntegrationTest may be flaky. "
zijiehee83f4b32016-12-08 11:47:01 -0800247 "Please kindly FYI the broken link to "
248 "zijiehe@chromium.org for investigation. If "
249 "the failure continually happens, but I have "
250 "not responded as quick as expected, disable "
251 "*all* tests in "
zijiehe21841552016-11-18 20:31:02 -0800252 "screen_capturer_integration_test.cc to "
253 "unblock other developers.";
254 }
zijiehe6be0a652016-10-27 16:50:35 -0700255 }
256
257 if (succeeded_capturers == capturers.size()) {
258 break;
259 }
260 }
261
262 ASSERT_EQ(succeeded_capturers, capturers.size());
263 }
264
265 // Expects |capturer| to successfully capture a frame, and returns it.
zijiehe54fd5792016-11-02 14:49:35 -0700266 std::unique_ptr<DesktopFrame> CaptureFrame(DesktopCapturer* capturer) {
zijiehee83f4b32016-12-08 11:47:01 -0800267 for (int i = 0; i < 10; i++) {
268 std::unique_ptr<DesktopFrame> frame;
269 DesktopCapturer::Result result;
270 EXPECT_CALL(callback_, OnCaptureResultPtr(_, _))
271 .WillOnce(SaveCaptureResult(&result, &frame));
272 capturer->CaptureFrame();
273 testing::Mock::VerifyAndClearExpectations(&callback_);
274 if (result == DesktopCapturer::Result::SUCCESS) {
275 EXPECT_TRUE(frame);
276 return frame;
277 } else {
278 EXPECT_FALSE(frame);
279 }
280 }
281
282 EXPECT_TRUE(false);
283 return nullptr;
zijiehe6be0a652016-10-27 16:50:35 -0700284 }
285};
286
zijiehe8d1649d2016-12-09 16:00:00 -0800287#if defined(WEBRTC_WIN)
288// ScreenCapturerWinGdi randomly returns blank screen, the root cause is still
289// unknown. Bug, https://bugs.chromium.org/p/webrtc/issues/detail?id=6843.
Henrik Kjellander99f7bfd2016-12-12 08:29:55 +0100290#define MAYBE_CaptureUpdatedRegion DISABLED_CaptureUpdatedRegion
zijiehe8d1649d2016-12-09 16:00:00 -0800291#else
292#define MAYBE_CaptureUpdatedRegion CaptureUpdatedRegion
zijiehee83f4b32016-12-08 11:47:01 -0800293#endif
zijiehe8d1649d2016-12-09 16:00:00 -0800294TEST_F(ScreenCapturerIntegrationTest, MAYBE_CaptureUpdatedRegion) {
295 TestCaptureUpdatedRegion();
zijiehe6be0a652016-10-27 16:50:35 -0700296}
297
zijiehe8d1649d2016-12-09 16:00:00 -0800298#if defined(WEBRTC_WIN)
299// ScreenCapturerWinGdi randomly returns blank screen, the root cause is still
300// unknown. Bug, https://bugs.chromium.org/p/webrtc/issues/detail?id=6843.
Henrik Kjellander99f7bfd2016-12-12 08:29:55 +0100301#define MAYBE_TwoCapturers DISABLED_TwoCapturers
zijiehe8d1649d2016-12-09 16:00:00 -0800302#else
303#define MAYBE_TwoCapturers TwoCapturers
304#endif
305TEST_F(ScreenCapturerIntegrationTest, MAYBE_TwoCapturers) {
zijiehe54fd5792016-11-02 14:49:35 -0700306 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_);
zijiehe6be0a652016-10-27 16:50:35 -0700307 SetUp();
308 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()});
309}
310
311#if defined(WEBRTC_WIN)
312
Zijie He0cab0852017-08-24 11:30:02 -0700313// Windows cannot capture contents on VMs hosted in GCE. See bug
314// https://bugs.chromium.org/p/webrtc/issues/detail?id=8153.
315TEST_F(ScreenCapturerIntegrationTest,
316 DISABLED_CaptureUpdatedRegionWithDirectxCapturer) {
zijiehe6be0a652016-10-27 16:50:35 -0700317 if (!CreateDirectxCapturer()) {
318 return;
319 }
320
321 TestCaptureUpdatedRegion();
322}
323
Zijie He0cab0852017-08-24 11:30:02 -0700324TEST_F(ScreenCapturerIntegrationTest, DISABLED_TwoDirectxCapturers) {
zijiehe6be0a652016-10-27 16:50:35 -0700325 if (!CreateDirectxCapturer()) {
326 return;
327 }
328
zijiehe54fd5792016-11-02 14:49:35 -0700329 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_);
zijiehe6be0a652016-10-27 16:50:35 -0700330 RTC_CHECK(CreateDirectxCapturer());
331 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()});
332}
333
334TEST_F(ScreenCapturerIntegrationTest,
Zijie He0cab0852017-08-24 11:30:02 -0700335 DISABLED_CaptureUpdatedRegionWithMagnifierCapturer) {
zijiehe166e59a2016-11-29 14:46:46 -0800336 // On Windows 8 or later, magnifier APIs return a frame with a border on test
337 // environment, so disable these tests.
zijiehee83f4b32016-12-08 11:47:01 -0800338 // Bug https://bugs.chromium.org/p/webrtc/issues/detail?id=6844
zijiehe166e59a2016-11-29 14:46:46 -0800339 // TODO(zijiehe): Find the root cause of the border and failure, which cannot
340 // reproduce on my dev machine.
341 if (rtc::IsWindows8OrLater()) {
342 return;
343 }
zijiehe6be0a652016-10-27 16:50:35 -0700344 CreateMagnifierCapturer();
345 TestCaptureUpdatedRegion();
346}
347
Zijie He0cab0852017-08-24 11:30:02 -0700348TEST_F(ScreenCapturerIntegrationTest, DISABLED_TwoMagnifierCapturers) {
zijiehe166e59a2016-11-29 14:46:46 -0800349 // On Windows 8 or later, magnifier APIs return a frame with a border on test
350 // environment, so disable these tests.
zijiehee83f4b32016-12-08 11:47:01 -0800351 // Bug https://bugs.chromium.org/p/webrtc/issues/detail?id=6844
zijiehe166e59a2016-11-29 14:46:46 -0800352 // TODO(zijiehe): Find the root cause of the border and failure, which cannot
353 // reproduce on my dev machine.
354 if (rtc::IsWindows8OrLater()) {
355 return;
356 }
zijiehe6be0a652016-10-27 16:50:35 -0700357 CreateMagnifierCapturer();
zijiehe54fd5792016-11-02 14:49:35 -0700358 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_);
zijiehe6be0a652016-10-27 16:50:35 -0700359 CreateMagnifierCapturer();
360 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()});
361}
362
363TEST_F(ScreenCapturerIntegrationTest,
Zijie He0cab0852017-08-24 11:30:02 -0700364 DISABLED_MaybeCaptureUpdatedRegionWithDirectxCapturer) {
zijiehee83f4b32016-12-08 11:47:01 -0800365 if (!rtc::IsWindows8OrLater()) {
366 // ScreenCapturerWinGdi randomly returns blank screen, the root cause is
367 // still unknown. Bug,
368 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6843.
369 // On Windows 7 or early version, MaybeCreateDirectxCapturer() always
370 // creates GDI capturer.
371 return;
372 }
zijiehe6be0a652016-10-27 16:50:35 -0700373 // Even DirectX capturer is not supported in current system, we should be able
374 // to select a usable capturer.
375 MaybeCreateDirectxCapturer();
376 TestCaptureUpdatedRegion();
377}
378
379#endif // defined(WEBRTC_WIN)
380
381} // namespace webrtc