blob: 349edbc58b457b3818e769a4244d966ede7f923c [file] [log] [blame]
zijiehe49c01d72016-08-16 17:33:55 -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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/desktop_capture/screen_drawer.h"
zijiehe49c01d72016-08-16 17:33:55 -070012
Zijie He825f65e2017-08-16 14:56:42 -070013#include <atomic>
zijiehe49c01d72016-08-16 17:33:55 -070014#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/function_view.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/random.h"
20#include "rtc_base/platform_thread.h"
21#include "rtc_base/ptr_util.h"
22#include "rtc_base/timeutils.h"
23#include "system_wrappers/include/sleep.h"
24#include "test/gtest.h"
zijiehe49c01d72016-08-16 17:33:55 -070025
Zijie He825f65e2017-08-16 14:56:42 -070026#if defined(WEBRTC_POSIX)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/desktop_capture/screen_drawer_lock_posix.h"
Zijie He825f65e2017-08-16 14:56:42 -070028#endif
29
zijiehe49c01d72016-08-16 17:33:55 -070030namespace webrtc {
31
Zijie He825f65e2017-08-16 14:56:42 -070032namespace {
33
34void TestScreenDrawerLock(
35 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor) {
36 constexpr int kLockDurationMs = 100;
37
38 RTC_DCHECK(ctor);
39
40 std::atomic<bool> created(false);
41 std::atomic<bool> ready(false);
42
43 class Task {
44 public:
45 Task(std::atomic<bool>* created,
46 const std::atomic<bool>& ready,
47 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor)
48 : created_(created),
49 ready_(ready),
50 ctor_(ctor) {}
51
52 ~Task() = default;
53
54 static void RunTask(void* me) {
55 Task* task = static_cast<Task*>(me);
56 std::unique_ptr<ScreenDrawerLock> lock = task->ctor_();
57 ASSERT_TRUE(!!lock);
58 task->created_->store(true);
59 // Wait for the main thread to get the signal of created_.
60 while (!task->ready_.load()) {
61 SleepMs(1);
62 }
63 // At this point, main thread should begin to create a second lock. Though
64 // it's still possible the second lock won't be created before the
65 // following sleep has been finished, the possibility will be
66 // significantly reduced.
67 const int64_t current_ms = rtc::TimeMillis();
68 // SleepMs() may return early. See
69 // https://cs.chromium.org/chromium/src/third_party/webrtc/system_wrappers/include/sleep.h?rcl=4a604c80cecce18aff6fc5e16296d04675312d83&l=20
70 // But we need to ensure at least 100 ms has been passed before unlocking
71 // |lock|.
72 while (rtc::TimeMillis() - current_ms < kLockDurationMs) {
73 SleepMs(kLockDurationMs - (rtc::TimeMillis() - current_ms));
74 }
75 }
76
77 private:
78 std::atomic<bool>* const created_;
79 const std::atomic<bool>& ready_;
80 const rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor_;
81 } task(&created, ready, ctor);
82
83 rtc::PlatformThread lock_thread(&Task::RunTask, &task, "lock_thread");
84 lock_thread.Start();
85
86 // Wait for the first lock in Task::RunTask() to be created.
87 // TODO(zijiehe): Find a better solution to wait for the creation of the first
88 // lock. See https://chromium-review.googlesource.com/c/607688/13/webrtc/modules/desktop_capture/screen_drawer_unittest.cc
89 while (!created.load()) {
90 SleepMs(1);
91 }
92
93 const int64_t start_ms = rtc::TimeMillis();
94 ready.store(true);
95 // This is unlikely to fail, but just in case current thread is too laggy and
96 // cause the SleepMs() in RunTask() to finish before we creating another lock.
97 ASSERT_GT(kLockDurationMs, rtc::TimeMillis() - start_ms);
98 ctor();
99 ASSERT_LE(kLockDurationMs, rtc::TimeMillis() - start_ms);
100 lock_thread.Stop();
101}
102
103} // namespace
104
zijiehe49c01d72016-08-16 17:33:55 -0700105// These are a set of manual test cases, as we do not have an automatical way to
106// detect whether a ScreenDrawer on a certain platform works well without
107// ScreenCapturer(s). So you may execute these test cases with
108// --gtest_also_run_disabled_tests --gtest_filter=ScreenDrawerTest.*.
109TEST(ScreenDrawerTest, DISABLED_DrawRectangles) {
110 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create();
111 if (!drawer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_WARNING)
113 << "No ScreenDrawer implementation for current platform.";
zijiehe49c01d72016-08-16 17:33:55 -0700114 return;
115 }
116
zijiehe0f49dac2016-09-07 11:52:25 -0700117 if (drawer->DrawableRegion().is_empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100118 RTC_LOG(LS_WARNING)
119 << "ScreenDrawer of current platform does not provide a "
120 "non-empty DrawableRegion().";
zijiehe0f49dac2016-09-07 11:52:25 -0700121 return;
122 }
123
zijiehe49c01d72016-08-16 17:33:55 -0700124 DesktopRect rect = drawer->DrawableRegion();
125 Random random(rtc::TimeMicros());
126 for (int i = 0; i < 100; i++) {
127 // Make sure we at least draw one pixel.
128 int left = random.Rand(rect.left(), rect.right() - 2);
129 int top = random.Rand(rect.top(), rect.bottom() - 2);
130 drawer->DrawRectangle(
131 DesktopRect::MakeLTRB(left, top, random.Rand(left + 1, rect.right()),
132 random.Rand(top + 1, rect.bottom())),
zijiehe0f49dac2016-09-07 11:52:25 -0700133 RgbaColor(random.Rand<uint8_t>(), random.Rand<uint8_t>(),
134 random.Rand<uint8_t>(), random.Rand<uint8_t>()));
zijiehe49c01d72016-08-16 17:33:55 -0700135
136 if (i == 50) {
137 SleepMs(10000);
zijiehe49c01d72016-08-16 17:33:55 -0700138 }
139 }
140
141 SleepMs(10000);
zijiehe49c01d72016-08-16 17:33:55 -0700142}
143
Zijie He825f65e2017-08-16 14:56:42 -0700144TEST(ScreenDrawerTest, TwoScreenDrawerLocks) {
145#if defined(WEBRTC_POSIX)
146 // ScreenDrawerLockPosix won't be able to unlink the named semaphore. So use a
147 // different semaphore name here to avoid deadlock.
148 const char* semaphore_name = "GSDL8784541a812011e788ff67427b";
149 ScreenDrawerLockPosix::Unlink(semaphore_name);
150
151 TestScreenDrawerLock([semaphore_name]() {
152 return rtc::MakeUnique<ScreenDrawerLockPosix>(semaphore_name);
153 });
154#elif defined(WEBRTC_WIN)
155 TestScreenDrawerLock([]() {
156 return ScreenDrawerLock::Create();
157 });
158#endif
159}
160
zijiehe49c01d72016-08-16 17:33:55 -0700161} // namespace webrtc