OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <string.h> | |
12 | |
13 #include <algorithm> | |
14 #include <initializer_list> | |
15 #include <memory> | 11 #include <memory> |
16 #include <utility> | 12 #include <utility> |
17 | 13 |
18 #include "webrtc/modules/desktop_capture/screen_capturer.h" | 14 #include "webrtc/modules/desktop_capture/screen_capturer.h" |
19 | 15 |
20 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "webrtc/base/checks.h" | |
23 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
24 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
25 #include "webrtc/modules/desktop_capture/rgba_color.h" | |
26 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" | 20 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
27 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 21 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
28 #include "webrtc/modules/desktop_capture/desktop_region.h" | 22 #include "webrtc/modules/desktop_capture/desktop_region.h" |
29 #include "webrtc/modules/desktop_capture/screen_capturer_mock_objects.h" | 23 #include "webrtc/modules/desktop_capture/screen_capturer_mock_objects.h" |
30 #include "webrtc/modules/desktop_capture/screen_drawer.h" | |
31 #include "webrtc/system_wrappers/include/sleep.h" | |
32 | 24 |
33 #if defined(WEBRTC_WIN) | 25 #if defined(WEBRTC_WIN) |
34 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" | 26 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
35 #endif // defined(WEBRTC_WIN) | 27 #endif // defined(WEBRTC_WIN) |
36 | 28 |
37 using ::testing::_; | 29 using ::testing::_; |
38 using ::testing::AnyNumber; | 30 using ::testing::AnyNumber; |
39 using ::testing::Return; | 31 using ::testing::Return; |
40 | 32 |
41 const int kTestSharedMemoryId = 123; | 33 const int kTestSharedMemoryId = 123; |
42 | 34 |
43 namespace webrtc { | 35 namespace webrtc { |
44 | 36 |
45 namespace { | |
46 | |
47 ACTION_P(SaveUniquePtrArg, dest) { | |
48 *dest = std::move(*arg1); | |
49 } | |
50 | |
51 // Expects |capturer| to successfully capture a frame, and returns it. | |
52 std::unique_ptr<DesktopFrame> CaptureFrame( | |
53 ScreenCapturer* capturer, | |
54 MockScreenCapturerCallback* callback) { | |
55 std::unique_ptr<DesktopFrame> frame; | |
56 EXPECT_CALL(*callback, | |
57 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | |
58 .WillOnce(SaveUniquePtrArg(&frame)); | |
59 capturer->Capture(DesktopRegion()); | |
60 EXPECT_TRUE(frame); | |
61 return frame; | |
62 } | |
63 | |
64 // Expects color in |rect| of |frame| is |color|. | |
65 void ExpectPixelsAreColoredBy(const DesktopFrame& frame, | |
66 DesktopRect rect, | |
67 RgbaColor color) { | |
68 // updated_region() should cover the painted area. | |
69 DesktopRegion updated_region(frame.updated_region()); | |
70 updated_region.IntersectWith(rect); | |
71 ASSERT_TRUE(updated_region.Equals(DesktopRegion(rect))); | |
72 | |
73 // Color in the |rect| should be |color|. | |
74 uint8_t* row = frame.GetFrameDataAtPos(rect.top_left()); | |
75 for (int i = 0; i < rect.height(); i++) { | |
76 uint8_t* column = row; | |
77 for (int j = 0; j < rect.width(); j++) { | |
78 ASSERT_EQ(color, RgbaColor(column)); | |
79 column += DesktopFrame::kBytesPerPixel; | |
80 } | |
81 row += frame.stride(); | |
82 } | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 class ScreenCapturerTest : public testing::Test { | 37 class ScreenCapturerTest : public testing::Test { |
88 public: | 38 public: |
89 void SetUp() override { | 39 void SetUp() override { |
90 capturer_.reset( | 40 capturer_.reset( |
91 ScreenCapturer::Create(DesktopCaptureOptions::CreateDefault())); | 41 ScreenCapturer::Create(DesktopCaptureOptions::CreateDefault())); |
92 } | 42 } |
93 | 43 |
94 protected: | 44 protected: |
95 void TestCaptureUpdatedRegion( | |
96 std::initializer_list<ScreenCapturer*> capturers) { | |
97 // A large enough area for the tests, which should be able to fulfill by | |
98 // most of systems. | |
99 const int kTestArea = 512; | |
100 const int kRectSize = 32; | |
101 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create(); | |
102 if (!drawer || drawer->DrawableRegion().is_empty()) { | |
103 LOG(LS_WARNING) << "No ScreenDrawer implementation for current platform."; | |
104 return; | |
105 } | |
106 if (drawer->DrawableRegion().width() < kTestArea || | |
107 drawer->DrawableRegion().height() < kTestArea) { | |
108 LOG(LS_WARNING) << "ScreenDrawer::DrawableRegion() is too small for the " | |
109 "CaptureUpdatedRegion tests."; | |
110 return; | |
111 } | |
112 | |
113 for (ScreenCapturer* capturer : capturers) { | |
114 capturer->Start(&callback_); | |
115 } | |
116 | |
117 #if defined(WEBRTC_LINUX) | |
118 // TODO(zijiehe): ScreenCapturerX11 won't be able to capture correct images | |
119 // in the first several capture attempts. | |
120 for (int i = 0; i < 10; i++) { | |
121 for (ScreenCapturer* capturer : capturers) { | |
122 std::unique_ptr<DesktopFrame> frame = | |
123 CaptureFrame(capturer, &callback_); | |
124 if (!frame) { | |
125 return; | |
126 } | |
127 } | |
128 } | |
129 #endif | |
130 | |
131 for (int c = 0; c < 3; c++) { | |
132 for (int i = 0; i < kTestArea - kRectSize; i += 16) { | |
133 DesktopRect rect = DesktopRect::MakeXYWH(i, i, kRectSize, kRectSize); | |
134 rect.Translate(drawer->DrawableRegion().top_left()); | |
135 RgbaColor color((c == 0 ? (i & 0xff) : 0x7f), | |
136 (c == 1 ? (i & 0xff) : 0x7f), | |
137 (c == 2 ? (i & 0xff) : 0x7f)); | |
138 drawer->Clear(); | |
139 drawer->DrawRectangle(rect, color); | |
140 drawer->WaitForPendingDraws(); | |
141 | |
142 for (ScreenCapturer* capturer : capturers) { | |
143 std::unique_ptr<DesktopFrame> frame = | |
144 CaptureFrame(capturer, &callback_); | |
145 if (!frame) { | |
146 return; | |
147 } | |
148 | |
149 ExpectPixelsAreColoredBy(*frame, rect, color); | |
150 } | |
151 } | |
152 } | |
153 } | |
154 | |
155 void TestCaptureUpdatedRegion() { | |
156 TestCaptureUpdatedRegion({capturer_.get()}); | |
157 } | |
158 | |
159 #if defined(WEBRTC_WIN) | |
160 bool SetDirectxCapturerMode() { | |
161 if (!ScreenCapturerWinDirectx::IsSupported()) { | |
162 LOG(LS_WARNING) << "Directx capturer is not supported"; | |
163 return false; | |
164 } | |
165 | |
166 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); | |
167 options.set_allow_directx_capturer(true); | |
168 capturer_.reset(ScreenCapturer::Create(options)); | |
169 return true; | |
170 } | |
171 #endif // defined(WEBRTC_WIN) | |
172 | |
173 std::unique_ptr<ScreenCapturer> capturer_; | 45 std::unique_ptr<ScreenCapturer> capturer_; |
174 MockScreenCapturerCallback callback_; | 46 MockScreenCapturerCallback callback_; |
175 }; | 47 }; |
176 | 48 |
177 class FakeSharedMemory : public SharedMemory { | 49 class FakeSharedMemory : public SharedMemory { |
178 public: | 50 public: |
179 FakeSharedMemory(char* buffer, size_t size) | 51 FakeSharedMemory(char* buffer, size_t size) |
180 : SharedMemory(buffer, size, 0, kTestSharedMemoryId), | 52 : SharedMemory(buffer, size, 0, kTestSharedMemoryId), |
181 buffer_(buffer) { | 53 buffer_(buffer) { |
182 } | 54 } |
(...skipping 12 matching lines...) Expand all Loading... |
195 | 67 |
196 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) override { | 68 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) override { |
197 return std::unique_ptr<SharedMemory>( | 69 return std::unique_ptr<SharedMemory>( |
198 new FakeSharedMemory(new char[size], size)); | 70 new FakeSharedMemory(new char[size], size)); |
199 } | 71 } |
200 | 72 |
201 private: | 73 private: |
202 RTC_DISALLOW_COPY_AND_ASSIGN(FakeSharedMemoryFactory); | 74 RTC_DISALLOW_COPY_AND_ASSIGN(FakeSharedMemoryFactory); |
203 }; | 75 }; |
204 | 76 |
| 77 ACTION_P(SaveUniquePtrArg, dest) { |
| 78 *dest = std::move(*arg1); |
| 79 } |
| 80 |
205 TEST_F(ScreenCapturerTest, GetScreenListAndSelectScreen) { | 81 TEST_F(ScreenCapturerTest, GetScreenListAndSelectScreen) { |
206 webrtc::ScreenCapturer::ScreenList screens; | 82 webrtc::ScreenCapturer::ScreenList screens; |
207 EXPECT_TRUE(capturer_->GetScreenList(&screens)); | 83 EXPECT_TRUE(capturer_->GetScreenList(&screens)); |
208 for (webrtc::ScreenCapturer::ScreenList::iterator it = screens.begin(); | 84 for (webrtc::ScreenCapturer::ScreenList::iterator it = screens.begin(); |
209 it != screens.end(); ++it) { | 85 it != screens.end(); ++it) { |
210 EXPECT_TRUE(capturer_->SelectScreen(it->id)); | 86 EXPECT_TRUE(capturer_->SelectScreen(it->id)); |
211 } | 87 } |
212 } | 88 } |
213 | 89 |
214 TEST_F(ScreenCapturerTest, StartCapturer) { | 90 TEST_F(ScreenCapturerTest, StartCapturer) { |
(...skipping 19 matching lines...) Expand all Loading... |
234 | 110 |
235 // Verify that the region contains whole screen. | 111 // Verify that the region contains whole screen. |
236 EXPECT_FALSE(frame->updated_region().is_empty()); | 112 EXPECT_FALSE(frame->updated_region().is_empty()); |
237 DesktopRegion::Iterator it(frame->updated_region()); | 113 DesktopRegion::Iterator it(frame->updated_region()); |
238 ASSERT_TRUE(!it.IsAtEnd()); | 114 ASSERT_TRUE(!it.IsAtEnd()); |
239 EXPECT_TRUE(it.rect().equals(DesktopRect::MakeSize(frame->size()))); | 115 EXPECT_TRUE(it.rect().equals(DesktopRect::MakeSize(frame->size()))); |
240 it.Advance(); | 116 it.Advance(); |
241 EXPECT_TRUE(it.IsAtEnd()); | 117 EXPECT_TRUE(it.IsAtEnd()); |
242 } | 118 } |
243 | 119 |
244 TEST_F(ScreenCapturerTest, CaptureUpdatedRegion) { | |
245 TestCaptureUpdatedRegion(); | |
246 } | |
247 | |
248 #if defined(WEBRTC_WIN) | 120 #if defined(WEBRTC_WIN) |
249 | 121 |
250 TEST_F(ScreenCapturerTest, UseSharedBuffers) { | 122 TEST_F(ScreenCapturerTest, UseSharedBuffers) { |
251 std::unique_ptr<DesktopFrame> frame; | 123 std::unique_ptr<DesktopFrame> frame; |
252 EXPECT_CALL(callback_, | 124 EXPECT_CALL(callback_, |
253 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | 125 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) |
254 .WillOnce(SaveUniquePtrArg(&frame)); | 126 .WillOnce(SaveUniquePtrArg(&frame)); |
255 | 127 |
256 capturer_->Start(&callback_); | 128 capturer_->Start(&callback_); |
257 capturer_->SetSharedMemoryFactory( | 129 capturer_->SetSharedMemoryFactory( |
(...skipping 14 matching lines...) Expand all Loading... |
272 EXPECT_CALL(callback_, | 144 EXPECT_CALL(callback_, |
273 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | 145 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) |
274 .WillOnce(SaveUniquePtrArg(&frame)); | 146 .WillOnce(SaveUniquePtrArg(&frame)); |
275 | 147 |
276 capturer_->Start(&callback_); | 148 capturer_->Start(&callback_); |
277 capturer_->Capture(DesktopRegion()); | 149 capturer_->Capture(DesktopRegion()); |
278 ASSERT_TRUE(frame); | 150 ASSERT_TRUE(frame); |
279 } | 151 } |
280 | 152 |
281 TEST_F(ScreenCapturerTest, UseDirectxCapturer) { | 153 TEST_F(ScreenCapturerTest, UseDirectxCapturer) { |
282 if (!SetDirectxCapturerMode()) { | 154 if (!ScreenCapturerWinDirectx::IsSupported()) { |
| 155 LOG(LS_WARNING) << "Directx capturer is not supported"; |
283 return; | 156 return; |
284 } | 157 } |
285 | 158 |
| 159 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); |
| 160 options.set_allow_directx_capturer(true); |
| 161 capturer_.reset(ScreenCapturer::Create(options)); |
| 162 |
286 std::unique_ptr<DesktopFrame> frame; | 163 std::unique_ptr<DesktopFrame> frame; |
287 EXPECT_CALL(callback_, | 164 EXPECT_CALL(callback_, |
288 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | 165 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) |
289 .WillOnce(SaveUniquePtrArg(&frame)); | 166 .WillOnce(SaveUniquePtrArg(&frame)); |
290 | 167 |
291 capturer_->Start(&callback_); | 168 capturer_->Start(&callback_); |
292 capturer_->Capture(DesktopRegion()); | 169 capturer_->Capture(DesktopRegion()); |
293 ASSERT_TRUE(frame); | 170 ASSERT_TRUE(frame); |
294 } | 171 } |
295 | 172 |
296 TEST_F(ScreenCapturerTest, UseDirectxCapturerWithSharedBuffers) { | 173 TEST_F(ScreenCapturerTest, UseDirectxCapturerWithSharedBuffers) { |
297 if (!SetDirectxCapturerMode()) { | 174 if (!ScreenCapturerWinDirectx::IsSupported()) { |
| 175 LOG(LS_WARNING) << "Directx capturer is not supported"; |
298 return; | 176 return; |
299 } | 177 } |
300 | 178 |
| 179 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); |
| 180 options.set_allow_directx_capturer(true); |
| 181 capturer_.reset(ScreenCapturer::Create(options)); |
| 182 |
301 std::unique_ptr<DesktopFrame> frame; | 183 std::unique_ptr<DesktopFrame> frame; |
302 EXPECT_CALL(callback_, | 184 EXPECT_CALL(callback_, |
303 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | 185 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) |
304 .WillOnce(SaveUniquePtrArg(&frame)); | 186 .WillOnce(SaveUniquePtrArg(&frame)); |
305 | 187 |
306 capturer_->Start(&callback_); | 188 capturer_->Start(&callback_); |
307 capturer_->SetSharedMemoryFactory( | 189 capturer_->SetSharedMemoryFactory( |
308 std::unique_ptr<SharedMemoryFactory>(new FakeSharedMemoryFactory())); | 190 std::unique_ptr<SharedMemoryFactory>(new FakeSharedMemoryFactory())); |
309 capturer_->Capture(DesktopRegion()); | 191 capturer_->Capture(DesktopRegion()); |
310 ASSERT_TRUE(frame); | 192 ASSERT_TRUE(frame); |
311 ASSERT_TRUE(frame->shared_memory()); | 193 ASSERT_TRUE(frame->shared_memory()); |
312 EXPECT_EQ(frame->shared_memory()->id(), kTestSharedMemoryId); | 194 EXPECT_EQ(frame->shared_memory()->id(), kTestSharedMemoryId); |
313 } | 195 } |
314 | 196 |
315 TEST_F(ScreenCapturerTest, CaptureUpdatedRegionWithDirectxCapturer) { | |
316 if (!SetDirectxCapturerMode()) { | |
317 return; | |
318 } | |
319 | |
320 TestCaptureUpdatedRegion(); | |
321 } | |
322 | |
323 // TODO(zijiehe): Enable this test after CL 2299663003 has been submitted. | |
324 TEST_F(ScreenCapturerTest, DISABLED_TwoDirectxCapturers) { | |
325 if (!SetDirectxCapturerMode()) { | |
326 return; | |
327 } | |
328 | |
329 std::unique_ptr<ScreenCapturer> capturer2(capturer_.release()); | |
330 RTC_CHECK(SetDirectxCapturerMode()); | |
331 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); | |
332 } | |
333 | |
334 #endif // defined(WEBRTC_WIN) | 197 #endif // defined(WEBRTC_WIN) |
335 | 198 |
336 } // namespace webrtc | 199 } // namespace webrtc |
OLD | NEW |