blob: 584770dbf8a53475ac1ad69776f19eece015f940 [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
13#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <atomic>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016#include <memory>
zijiehe49c01d72016-08-16 17:33:55 -070017
Artem Titov741daaf2019-03-21 14:37:36 +010018#include "api/function_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/platform_thread.h"
Yves Gerey665174f2018-06-19 15:03:05 +020022#include "rtc_base/random.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "system_wrappers/include/sleep.h"
25#include "test/gtest.h"
zijiehe49c01d72016-08-16 17:33:55 -070026
Zijie He825f65e2017-08-16 14:56:42 -070027#if defined(WEBRTC_POSIX)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/desktop_capture/screen_drawer_lock_posix.h"
Zijie He825f65e2017-08-16 14:56:42 -070029#endif
30
zijiehe49c01d72016-08-16 17:33:55 -070031namespace webrtc {
32
Zijie He825f65e2017-08-16 14:56:42 -070033namespace {
34
35void TestScreenDrawerLock(
36 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor) {
37 constexpr int kLockDurationMs = 100;
38
Zijie He825f65e2017-08-16 14:56:42 -070039 std::atomic<bool> created(false);
40 std::atomic<bool> ready(false);
41
42 class Task {
43 public:
44 Task(std::atomic<bool>* created,
45 const std::atomic<bool>& ready,
46 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor)
Yves Gerey665174f2018-06-19 15:03:05 +020047 : created_(created), ready_(ready), ctor_(ctor) {}
Zijie He825f65e2017-08-16 14:56:42 -070048
49 ~Task() = default;
50
Markus Handellad5037b2021-05-07 15:02:36 +020051 void RunTask() {
52 std::unique_ptr<ScreenDrawerLock> lock = ctor_();
Zijie He825f65e2017-08-16 14:56:42 -070053 ASSERT_TRUE(!!lock);
Markus Handellad5037b2021-05-07 15:02:36 +020054 created_->store(true);
Zijie He825f65e2017-08-16 14:56:42 -070055 // Wait for the main thread to get the signal of created_.
Markus Handellad5037b2021-05-07 15:02:36 +020056 while (!ready_.load()) {
Zijie He825f65e2017-08-16 14:56:42 -070057 SleepMs(1);
58 }
59 // At this point, main thread should begin to create a second lock. Though
60 // it's still possible the second lock won't be created before the
61 // following sleep has been finished, the possibility will be
62 // significantly reduced.
63 const int64_t current_ms = rtc::TimeMillis();
64 // SleepMs() may return early. See
65 // https://cs.chromium.org/chromium/src/third_party/webrtc/system_wrappers/include/sleep.h?rcl=4a604c80cecce18aff6fc5e16296d04675312d83&l=20
66 // But we need to ensure at least 100 ms has been passed before unlocking
Artem Titovcec43432021-07-28 23:35:39 +020067 // `lock`.
Zijie He825f65e2017-08-16 14:56:42 -070068 while (rtc::TimeMillis() - current_ms < kLockDurationMs) {
69 SleepMs(kLockDurationMs - (rtc::TimeMillis() - current_ms));
70 }
71 }
72
73 private:
74 std::atomic<bool>* const created_;
75 const std::atomic<bool>& ready_;
76 const rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor_;
77 } task(&created, ready, ctor);
78
Markus Handellad5037b2021-05-07 15:02:36 +020079 auto lock_thread = rtc::PlatformThread::SpawnJoinable(
80 [&task] { task.RunTask(); }, "lock_thread");
Zijie He825f65e2017-08-16 14:56:42 -070081
82 // Wait for the first lock in Task::RunTask() to be created.
83 // TODO(zijiehe): Find a better solution to wait for the creation of the first
Yves Gerey665174f2018-06-19 15:03:05 +020084 // lock. See
85 // https://chromium-review.googlesource.com/c/607688/13/webrtc/modules/desktop_capture/screen_drawer_unittest.cc
Zijie He825f65e2017-08-16 14:56:42 -070086 while (!created.load()) {
87 SleepMs(1);
88 }
89
90 const int64_t start_ms = rtc::TimeMillis();
91 ready.store(true);
92 // This is unlikely to fail, but just in case current thread is too laggy and
93 // cause the SleepMs() in RunTask() to finish before we creating another lock.
94 ASSERT_GT(kLockDurationMs, rtc::TimeMillis() - start_ms);
95 ctor();
96 ASSERT_LE(kLockDurationMs, rtc::TimeMillis() - start_ms);
Zijie He825f65e2017-08-16 14:56:42 -070097}
98
99} // namespace
100
zijiehe49c01d72016-08-16 17:33:55 -0700101// These are a set of manual test cases, as we do not have an automatical way to
102// detect whether a ScreenDrawer on a certain platform works well without
103// ScreenCapturer(s). So you may execute these test cases with
104// --gtest_also_run_disabled_tests --gtest_filter=ScreenDrawerTest.*.
105TEST(ScreenDrawerTest, DISABLED_DrawRectangles) {
106 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create();
107 if (!drawer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_WARNING)
109 << "No ScreenDrawer implementation for current platform.";
zijiehe49c01d72016-08-16 17:33:55 -0700110 return;
111 }
112
zijiehe0f49dac2016-09-07 11:52:25 -0700113 if (drawer->DrawableRegion().is_empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_WARNING)
115 << "ScreenDrawer of current platform does not provide a "
116 "non-empty DrawableRegion().";
zijiehe0f49dac2016-09-07 11:52:25 -0700117 return;
118 }
119
zijiehe49c01d72016-08-16 17:33:55 -0700120 DesktopRect rect = drawer->DrawableRegion();
121 Random random(rtc::TimeMicros());
122 for (int i = 0; i < 100; i++) {
123 // Make sure we at least draw one pixel.
124 int left = random.Rand(rect.left(), rect.right() - 2);
125 int top = random.Rand(rect.top(), rect.bottom() - 2);
126 drawer->DrawRectangle(
127 DesktopRect::MakeLTRB(left, top, random.Rand(left + 1, rect.right()),
128 random.Rand(top + 1, rect.bottom())),
zijiehe0f49dac2016-09-07 11:52:25 -0700129 RgbaColor(random.Rand<uint8_t>(), random.Rand<uint8_t>(),
130 random.Rand<uint8_t>(), random.Rand<uint8_t>()));
zijiehe49c01d72016-08-16 17:33:55 -0700131
132 if (i == 50) {
133 SleepMs(10000);
zijiehe49c01d72016-08-16 17:33:55 -0700134 }
135 }
136
137 SleepMs(10000);
zijiehe49c01d72016-08-16 17:33:55 -0700138}
139
Alex Loiko2b5b0e92018-11-21 10:50:48 +0100140#if defined(THREAD_SANITIZER) // bugs.webrtc.org/10019
141#define MAYBE_TwoScreenDrawerLocks DISABLED_TwoScreenDrawerLocks
142#else
143#define MAYBE_TwoScreenDrawerLocks TwoScreenDrawerLocks
144#endif
145TEST(ScreenDrawerTest, MAYBE_TwoScreenDrawerLocks) {
Zijie He825f65e2017-08-16 14:56:42 -0700146#if defined(WEBRTC_POSIX)
147 // ScreenDrawerLockPosix won't be able to unlink the named semaphore. So use a
148 // different semaphore name here to avoid deadlock.
149 const char* semaphore_name = "GSDL8784541a812011e788ff67427b";
150 ScreenDrawerLockPosix::Unlink(semaphore_name);
151
152 TestScreenDrawerLock([semaphore_name]() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200153 return std::make_unique<ScreenDrawerLockPosix>(semaphore_name);
Zijie He825f65e2017-08-16 14:56:42 -0700154 });
155#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200156 TestScreenDrawerLock([]() { return ScreenDrawerLock::Create(); });
Zijie He825f65e2017-08-16 14:56:42 -0700157#endif
158}
159
zijiehe49c01d72016-08-16 17:33:55 -0700160} // namespace webrtc