zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/desktop_capture/screen_drawer.h" |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 12 | |
| 13 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <atomic> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 16 | #include <memory> |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 17 | |
Artem Titov | 741daaf | 2019-03-21 14:37:36 +0100 | [diff] [blame] | 18 | #include "api/function_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/platform_thread.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 22 | #include "rtc_base/random.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "system_wrappers/include/sleep.h" |
| 25 | #include "test/gtest.h" |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 26 | |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 27 | #if defined(WEBRTC_POSIX) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/desktop_capture/screen_drawer_lock_posix.h" |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 31 | namespace webrtc { |
| 32 | |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | void TestScreenDrawerLock( |
| 36 | rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor) { |
| 37 | constexpr int kLockDurationMs = 100; |
| 38 | |
| 39 | RTC_DCHECK(ctor); |
| 40 | |
| 41 | std::atomic<bool> created(false); |
| 42 | std::atomic<bool> ready(false); |
| 43 | |
| 44 | class Task { |
| 45 | public: |
| 46 | Task(std::atomic<bool>* created, |
| 47 | const std::atomic<bool>& ready, |
| 48 | rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | : created_(created), ready_(ready), ctor_(ctor) {} |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 50 | |
| 51 | ~Task() = default; |
| 52 | |
| 53 | static void RunTask(void* me) { |
| 54 | Task* task = static_cast<Task*>(me); |
| 55 | std::unique_ptr<ScreenDrawerLock> lock = task->ctor_(); |
| 56 | ASSERT_TRUE(!!lock); |
| 57 | task->created_->store(true); |
| 58 | // Wait for the main thread to get the signal of created_. |
| 59 | while (!task->ready_.load()) { |
| 60 | SleepMs(1); |
| 61 | } |
| 62 | // At this point, main thread should begin to create a second lock. Though |
| 63 | // it's still possible the second lock won't be created before the |
| 64 | // following sleep has been finished, the possibility will be |
| 65 | // significantly reduced. |
| 66 | const int64_t current_ms = rtc::TimeMillis(); |
| 67 | // SleepMs() may return early. See |
| 68 | // https://cs.chromium.org/chromium/src/third_party/webrtc/system_wrappers/include/sleep.h?rcl=4a604c80cecce18aff6fc5e16296d04675312d83&l=20 |
| 69 | // But we need to ensure at least 100 ms has been passed before unlocking |
| 70 | // |lock|. |
| 71 | while (rtc::TimeMillis() - current_ms < kLockDurationMs) { |
| 72 | SleepMs(kLockDurationMs - (rtc::TimeMillis() - current_ms)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::atomic<bool>* const created_; |
| 78 | const std::atomic<bool>& ready_; |
| 79 | const rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor_; |
| 80 | } task(&created, ready, ctor); |
| 81 | |
| 82 | rtc::PlatformThread lock_thread(&Task::RunTask, &task, "lock_thread"); |
| 83 | lock_thread.Start(); |
| 84 | |
| 85 | // Wait for the first lock in Task::RunTask() to be created. |
| 86 | // TODO(zijiehe): Find a better solution to wait for the creation of the first |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 87 | // lock. See |
| 88 | // https://chromium-review.googlesource.com/c/607688/13/webrtc/modules/desktop_capture/screen_drawer_unittest.cc |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 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 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 105 | // 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.*. |
| 109 | TEST(ScreenDrawerTest, DISABLED_DrawRectangles) { |
| 110 | std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create(); |
| 111 | if (!drawer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_WARNING) |
| 113 | << "No ScreenDrawer implementation for current platform."; |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 117 | if (drawer->DrawableRegion().is_empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 118 | RTC_LOG(LS_WARNING) |
| 119 | << "ScreenDrawer of current platform does not provide a " |
| 120 | "non-empty DrawableRegion()."; |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 124 | 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())), |
zijiehe | 0f49dac | 2016-09-07 11:52:25 -0700 | [diff] [blame] | 133 | RgbaColor(random.Rand<uint8_t>(), random.Rand<uint8_t>(), |
| 134 | random.Rand<uint8_t>(), random.Rand<uint8_t>())); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 135 | |
| 136 | if (i == 50) { |
| 137 | SleepMs(10000); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | SleepMs(10000); |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Alex Loiko | 2b5b0e9 | 2018-11-21 10:50:48 +0100 | [diff] [blame] | 144 | #if defined(THREAD_SANITIZER) // bugs.webrtc.org/10019 |
| 145 | #define MAYBE_TwoScreenDrawerLocks DISABLED_TwoScreenDrawerLocks |
| 146 | #else |
| 147 | #define MAYBE_TwoScreenDrawerLocks TwoScreenDrawerLocks |
| 148 | #endif |
| 149 | TEST(ScreenDrawerTest, MAYBE_TwoScreenDrawerLocks) { |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 150 | #if defined(WEBRTC_POSIX) |
| 151 | // ScreenDrawerLockPosix won't be able to unlink the named semaphore. So use a |
| 152 | // different semaphore name here to avoid deadlock. |
| 153 | const char* semaphore_name = "GSDL8784541a812011e788ff67427b"; |
| 154 | ScreenDrawerLockPosix::Unlink(semaphore_name); |
| 155 | |
| 156 | TestScreenDrawerLock([semaphore_name]() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 157 | return std::make_unique<ScreenDrawerLockPosix>(semaphore_name); |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 158 | }); |
| 159 | #elif defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | TestScreenDrawerLock([]() { return ScreenDrawerLock::Create(); }); |
Zijie He | 825f65e | 2017-08-16 14:56:42 -0700 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
zijiehe | 49c01d7 | 2016-08-16 17:33:55 -0700 | [diff] [blame] | 164 | } // namespace webrtc |