| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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> | 11 #include <string.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <initializer_list> | 14 #include <initializer_list> |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <utility> | 16 #include <utility> |
| 17 | 17 |
| 18 #include "webrtc/modules/desktop_capture/screen_capturer.h" | 18 #include "webrtc/modules/desktop_capture/screen_capturer.h" |
| 19 | 19 |
| 20 #include "webrtc/test/gmock.h" | 20 #include "webrtc/test/gmock.h" |
| 21 #include "webrtc/test/gtest.h" | 21 #include "webrtc/test/gtest.h" |
| 22 #include "webrtc/base/checks.h" | 22 #include "webrtc/base/checks.h" |
| 23 #include "webrtc/base/constructormagic.h" | 23 #include "webrtc/base/constructormagic.h" |
| 24 #include "webrtc/base/logging.h" | 24 #include "webrtc/base/logging.h" |
| 25 #include "webrtc/modules/desktop_capture/rgba_color.h" | 25 #include "webrtc/modules/desktop_capture/rgba_color.h" |
| 26 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" | 26 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 27 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 27 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 28 #include "webrtc/modules/desktop_capture/desktop_region.h" | 28 #include "webrtc/modules/desktop_capture/desktop_region.h" |
| 29 #include "webrtc/modules/desktop_capture/mock_desktop_capturer_callback.h" | 29 #include "webrtc/modules/desktop_capture/screen_capturer_mock_objects.h" |
| 30 #include "webrtc/modules/desktop_capture/screen_drawer.h" | 30 #include "webrtc/modules/desktop_capture/screen_drawer.h" |
| 31 | 31 |
| 32 #if defined(WEBRTC_WIN) | 32 #if defined(WEBRTC_WIN) |
| 33 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" | 33 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
| 34 #endif // defined(WEBRTC_WIN) | 34 #endif // defined(WEBRTC_WIN) |
| 35 | 35 |
| 36 using ::testing::_; | 36 using ::testing::_; |
| 37 | 37 |
| 38 namespace webrtc { | 38 namespace webrtc { |
| 39 | 39 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 70 row += frame.stride(); | 70 row += frame.stride(); |
| 71 } | 71 } |
| 72 return true; | 72 return true; |
| 73 } | 73 } |
| 74 | 74 |
| 75 } // namespace | 75 } // namespace |
| 76 | 76 |
| 77 class ScreenCapturerIntegrationTest : public testing::Test { | 77 class ScreenCapturerIntegrationTest : public testing::Test { |
| 78 public: | 78 public: |
| 79 void SetUp() override { | 79 void SetUp() override { |
| 80 capturer_ = DesktopCapturer::CreateScreenCapturer( | 80 capturer_.reset( |
| 81 DesktopCaptureOptions::CreateDefault()); | 81 ScreenCapturer::Create(DesktopCaptureOptions::CreateDefault())); |
| 82 } | 82 } |
| 83 | 83 |
| 84 protected: | 84 protected: |
| 85 void TestCaptureUpdatedRegion( | 85 void TestCaptureUpdatedRegion( |
| 86 std::initializer_list<DesktopCapturer*> capturers) { | 86 std::initializer_list<ScreenCapturer*> capturers) { |
| 87 RTC_DCHECK(capturers.size() > 0); | 87 RTC_DCHECK(capturers.size() > 0); |
| 88 // A large enough area for the tests, which should be able to be fulfilled | 88 // A large enough area for the tests, which should be able to fulfill by |
| 89 // by most systems. | 89 // most of systems. |
| 90 const int kTestArea = 512; | 90 const int kTestArea = 512; |
| 91 const int kRectSize = 32; | 91 const int kRectSize = 32; |
| 92 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create(); | 92 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create(); |
| 93 if (!drawer || drawer->DrawableRegion().is_empty()) { | 93 if (!drawer || drawer->DrawableRegion().is_empty()) { |
| 94 LOG(LS_WARNING) << "No ScreenDrawer implementation for current platform."; | 94 LOG(LS_WARNING) << "No ScreenDrawer implementation for current platform."; |
| 95 return; | 95 return; |
| 96 } | 96 } |
| 97 if (drawer->DrawableRegion().width() < kTestArea || | 97 if (drawer->DrawableRegion().width() < kTestArea || |
| 98 drawer->DrawableRegion().height() < kTestArea) { | 98 drawer->DrawableRegion().height() < kTestArea) { |
| 99 LOG(LS_WARNING) << "ScreenDrawer::DrawableRegion() is too small for the " | 99 LOG(LS_WARNING) << "ScreenDrawer::DrawableRegion() is too small for the " |
| 100 "CaptureUpdatedRegion tests."; | 100 "CaptureUpdatedRegion tests."; |
| 101 return; | 101 return; |
| 102 } | 102 } |
| 103 | 103 |
| 104 for (DesktopCapturer* capturer : capturers) { | 104 for (ScreenCapturer* capturer : capturers) { |
| 105 capturer->Start(&callback_); | 105 capturer->Start(&callback_); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Draw a set of |kRectSize| by |kRectSize| rectangles at (|i|, |i|), or | 108 // Draw a set of |kRectSize| by |kRectSize| rectangles at (|i|, |i|), or |
| 109 // |i| by |i| rectangles at (|kRectSize|, |kRectSize|). One of (controlled | 109 // |i| by |i| rectangles at (|kRectSize|, |kRectSize|). One of (controlled |
| 110 // by |c|) its primary colors is |i|, and the other two are 0x7f. So we | 110 // by |c|) its primary colors is |i|, and the other two are 0x7f. So we |
| 111 // won't draw a black or white rectangle. | 111 // won't draw a black or white rectangle. |
| 112 for (int c = 0; c < 3; c++) { | 112 for (int c = 0; c < 3; c++) { |
| 113 // A fixed size rectangle. | 113 // A fixed size rectangle. |
| 114 for (int i = 0; i < kTestArea - kRectSize; i += 16) { | 114 for (int i = 0; i < kTestArea - kRectSize; i += 16) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 void TestCaptureUpdatedRegion() { | 135 void TestCaptureUpdatedRegion() { |
| 136 TestCaptureUpdatedRegion({capturer_.get()}); | 136 TestCaptureUpdatedRegion({capturer_.get()}); |
| 137 } | 137 } |
| 138 | 138 |
| 139 #if defined(WEBRTC_WIN) | 139 #if defined(WEBRTC_WIN) |
| 140 // Enable allow_directx_capturer in DesktopCaptureOptions, but let | 140 // Enable allow_directx_capturer in DesktopCaptureOptions, but let |
| 141 // DesktopCapturer::CreateScreenCapturer() to decide whether a DirectX | 141 // ScreenCapturer::Create to decide whether a DirectX capturer should be used. |
| 142 // capturer should be used. | |
| 143 void MaybeCreateDirectxCapturer() { | 142 void MaybeCreateDirectxCapturer() { |
| 144 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); | 143 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); |
| 145 options.set_allow_directx_capturer(true); | 144 options.set_allow_directx_capturer(true); |
| 146 capturer_ = DesktopCapturer::CreateScreenCapturer(options); | 145 capturer_.reset(ScreenCapturer::Create(options)); |
| 147 } | 146 } |
| 148 | 147 |
| 149 bool CreateDirectxCapturer() { | 148 bool CreateDirectxCapturer() { |
| 150 if (!ScreenCapturerWinDirectx::IsSupported()) { | 149 if (!ScreenCapturerWinDirectx::IsSupported()) { |
| 151 LOG(LS_WARNING) << "Directx capturer is not supported"; | 150 LOG(LS_WARNING) << "Directx capturer is not supported"; |
| 152 return false; | 151 return false; |
| 153 } | 152 } |
| 154 | 153 |
| 155 MaybeCreateDirectxCapturer(); | 154 MaybeCreateDirectxCapturer(); |
| 156 return true; | 155 return true; |
| 157 } | 156 } |
| 158 | 157 |
| 159 void CreateMagnifierCapturer() { | 158 void CreateMagnifierCapturer() { |
| 160 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); | 159 DesktopCaptureOptions options(DesktopCaptureOptions::CreateDefault()); |
| 161 options.set_allow_use_magnification_api(true); | 160 options.set_allow_use_magnification_api(true); |
| 162 capturer_ = DesktopCapturer::CreateScreenCapturer(options); | 161 capturer_.reset(ScreenCapturer::Create(options)); |
| 163 } | 162 } |
| 164 #endif // defined(WEBRTC_WIN) | 163 #endif // defined(WEBRTC_WIN) |
| 165 | 164 |
| 166 std::unique_ptr<DesktopCapturer> capturer_; | 165 std::unique_ptr<ScreenCapturer> capturer_; |
| 167 MockDesktopCapturerCallback callback_; | 166 MockScreenCapturerCallback callback_; |
| 168 | 167 |
| 169 private: | 168 private: |
| 170 // Repeats capturing the frame by using |capturers| one-by-one for 600 times, | 169 // Repeats capturing the frame by using |capturers| one-by-one for 600 times, |
| 171 // typically 30 seconds, until they succeeded captured a |color| rectangle at | 170 // typically 30 seconds, until they succeeded captured a |color| rectangle at |
| 172 // |rect|. This function uses |drawer|->WaitForPendingDraws() between two | 171 // |rect|. This function uses |drawer|->WaitForPendingDraws() between two |
| 173 // attempts to wait for the screen to update. | 172 // attempts to wait for the screen to update. |
| 174 void TestCaptureOneFrame(std::vector<DesktopCapturer*> capturers, | 173 void TestCaptureOneFrame(std::vector<ScreenCapturer*> capturers, |
| 175 ScreenDrawer* drawer, | 174 ScreenDrawer* drawer, |
| 176 DesktopRect rect, | 175 DesktopRect rect, |
| 177 RgbaColor color) { | 176 RgbaColor color) { |
| 178 const int wait_capture_round = 600; | 177 const int wait_capture_round = 600; |
| 179 drawer->Clear(); | 178 drawer->Clear(); |
| 180 size_t succeeded_capturers = 0; | 179 size_t succeeded_capturers = 0; |
| 181 for (int i = 0; i < wait_capture_round; i++) { | 180 for (int i = 0; i < wait_capture_round; i++) { |
| 182 drawer->DrawRectangle(rect, color); | 181 drawer->DrawRectangle(rect, color); |
| 183 drawer->WaitForPendingDraws(); | 182 drawer->WaitForPendingDraws(); |
| 184 for (size_t j = 0; j < capturers.size(); j++) { | 183 for (size_t j = 0; j < capturers.size(); j++) { |
| 185 if (capturers[j] == nullptr) { | 184 if (capturers[j] == nullptr) { |
| 186 // DesktopCapturer should return an empty updated_region() if no | 185 // ScreenCapturer should return an empty updated_region() if no |
| 187 // update detected. So we won't test it again if it has captured | 186 // update detected. So we won't test it again if it has captured |
| 188 // the rectangle we drew. | 187 // the rectangle we drew. |
| 189 continue; | 188 continue; |
| 190 } | 189 } |
| 191 std::unique_ptr<DesktopFrame> frame = CaptureFrame(capturers[j]); | 190 std::unique_ptr<DesktopFrame> frame = CaptureFrame(capturers[j]); |
| 192 if (!frame) { | 191 if (!frame) { |
| 193 // CaptureFrame() has triggered an assertion failure already, we | 192 // CaptureFrame() has triggered an assertion failure already, we |
| 194 // only need to return here. | 193 // only need to return here. |
| 195 return; | 194 return; |
| 196 } | 195 } |
| 197 | 196 |
| 198 if (ArePixelsColoredBy( | 197 if (ArePixelsColoredBy( |
| 199 *frame, rect, color, drawer->MayDrawIncompleteShapes())) { | 198 *frame, rect, color, drawer->MayDrawIncompleteShapes())) { |
| 200 capturers[j] = nullptr; | 199 capturers[j] = nullptr; |
| 201 succeeded_capturers++; | 200 succeeded_capturers++; |
| 202 } | 201 } |
| 203 } | 202 } |
| 204 | 203 |
| 205 if (succeeded_capturers == capturers.size()) { | 204 if (succeeded_capturers == capturers.size()) { |
| 206 break; | 205 break; |
| 207 } | 206 } |
| 208 } | 207 } |
| 209 | 208 |
| 210 ASSERT_EQ(succeeded_capturers, capturers.size()); | 209 ASSERT_EQ(succeeded_capturers, capturers.size()); |
| 211 } | 210 } |
| 212 | 211 |
| 213 // Expects |capturer| to successfully capture a frame, and returns it. | 212 // Expects |capturer| to successfully capture a frame, and returns it. |
| 214 std::unique_ptr<DesktopFrame> CaptureFrame(DesktopCapturer* capturer) { | 213 std::unique_ptr<DesktopFrame> CaptureFrame(ScreenCapturer* capturer) { |
| 215 std::unique_ptr<DesktopFrame> frame; | 214 std::unique_ptr<DesktopFrame> frame; |
| 216 EXPECT_CALL(callback_, | 215 EXPECT_CALL(callback_, |
| 217 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) | 216 OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) |
| 218 .WillOnce(SaveUniquePtrArg(&frame)); | 217 .WillOnce(SaveUniquePtrArg(&frame)); |
| 219 capturer->CaptureFrame(); | 218 capturer->CaptureFrame(); |
| 220 EXPECT_TRUE(frame); | 219 EXPECT_TRUE(frame); |
| 221 return frame; | 220 return frame; |
| 222 } | 221 } |
| 223 }; | 222 }; |
| 224 | 223 |
| 225 TEST_F(ScreenCapturerIntegrationTest, CaptureUpdatedRegion) { | 224 TEST_F(ScreenCapturerIntegrationTest, CaptureUpdatedRegion) { |
| 226 TestCaptureUpdatedRegion(); | 225 TestCaptureUpdatedRegion(); |
| 227 } | 226 } |
| 228 | 227 |
| 229 TEST_F(ScreenCapturerIntegrationTest, TwoCapturers) { | 228 TEST_F(ScreenCapturerIntegrationTest, TwoCapturers) { |
| 230 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_); | 229 std::unique_ptr<ScreenCapturer> capturer2 = std::move(capturer_); |
| 231 SetUp(); | 230 SetUp(); |
| 232 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); | 231 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); |
| 233 } | 232 } |
| 234 | 233 |
| 235 #if defined(WEBRTC_WIN) | 234 #if defined(WEBRTC_WIN) |
| 236 | 235 |
| 237 TEST_F(ScreenCapturerIntegrationTest, CaptureUpdatedRegionWithDirectxCapturer) { | 236 TEST_F(ScreenCapturerIntegrationTest, CaptureUpdatedRegionWithDirectxCapturer) { |
| 238 if (!CreateDirectxCapturer()) { | 237 if (!CreateDirectxCapturer()) { |
| 239 return; | 238 return; |
| 240 } | 239 } |
| 241 | 240 |
| 242 TestCaptureUpdatedRegion(); | 241 TestCaptureUpdatedRegion(); |
| 243 } | 242 } |
| 244 | 243 |
| 245 TEST_F(ScreenCapturerIntegrationTest, TwoDirectxCapturers) { | 244 TEST_F(ScreenCapturerIntegrationTest, TwoDirectxCapturers) { |
| 246 if (!CreateDirectxCapturer()) { | 245 if (!CreateDirectxCapturer()) { |
| 247 return; | 246 return; |
| 248 } | 247 } |
| 249 | 248 |
| 250 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_); | 249 std::unique_ptr<ScreenCapturer> capturer2 = std::move(capturer_); |
| 251 RTC_CHECK(CreateDirectxCapturer()); | 250 RTC_CHECK(CreateDirectxCapturer()); |
| 252 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); | 251 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); |
| 253 } | 252 } |
| 254 | 253 |
| 255 TEST_F(ScreenCapturerIntegrationTest, | 254 TEST_F(ScreenCapturerIntegrationTest, |
| 256 CaptureUpdatedRegionWithMagnifierCapturer) { | 255 CaptureUpdatedRegionWithMagnifierCapturer) { |
| 257 CreateMagnifierCapturer(); | 256 CreateMagnifierCapturer(); |
| 258 TestCaptureUpdatedRegion(); | 257 TestCaptureUpdatedRegion(); |
| 259 } | 258 } |
| 260 | 259 |
| 261 TEST_F(ScreenCapturerIntegrationTest, TwoMagnifierCapturers) { | 260 TEST_F(ScreenCapturerIntegrationTest, TwoMagnifierCapturers) { |
| 262 CreateMagnifierCapturer(); | 261 CreateMagnifierCapturer(); |
| 263 std::unique_ptr<DesktopCapturer> capturer2 = std::move(capturer_); | 262 std::unique_ptr<ScreenCapturer> capturer2 = std::move(capturer_); |
| 264 CreateMagnifierCapturer(); | 263 CreateMagnifierCapturer(); |
| 265 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); | 264 TestCaptureUpdatedRegion({capturer_.get(), capturer2.get()}); |
| 266 } | 265 } |
| 267 | 266 |
| 268 TEST_F(ScreenCapturerIntegrationTest, | 267 TEST_F(ScreenCapturerIntegrationTest, |
| 269 MaybeCaptureUpdatedRegionWithDirectxCapturer) { | 268 MaybeCaptureUpdatedRegionWithDirectxCapturer) { |
| 270 // Even DirectX capturer is not supported in current system, we should be able | 269 // Even DirectX capturer is not supported in current system, we should be able |
| 271 // to select a usable capturer. | 270 // to select a usable capturer. |
| 272 MaybeCreateDirectxCapturer(); | 271 MaybeCreateDirectxCapturer(); |
| 273 TestCaptureUpdatedRegion(); | 272 TestCaptureUpdatedRegion(); |
| 274 } | 273 } |
| 275 | 274 |
| 276 #endif // defined(WEBRTC_WIN) | 275 #endif // defined(WEBRTC_WIN) |
| 277 | 276 |
| 278 } // namespace webrtc | 277 } // namespace webrtc |
| OLD | NEW |