1 /*
2 * Copyright (c) 2021 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
11 #include "modules/desktop_capture/win/wgc_capture_source.h"
12
13 #include <windows.graphics.capture.h>
14 #include <wrl/client.h>
15
16 #include <utility>
17
18 #include "modules/desktop_capture/desktop_capture_types.h"
19 #include "modules/desktop_capture/desktop_geometry.h"
20 #include "modules/desktop_capture/win/screen_capture_utils.h"
21 #include "modules/desktop_capture/win/test_support/test_window.h"
22 #include "modules/desktop_capture/win/wgc_capturer_win.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/logging.h"
25 #include "rtc_base/win/scoped_com_initializer.h"
26 #include "test/gtest.h"
27
28 namespace webrtc {
29 namespace {
30
31 const WCHAR kWindowTitle[] = L"WGC Capture Source Test Window";
32
33 const int kFirstXCoord = 25;
34 const int kFirstYCoord = 50;
35 const int kSecondXCoord = 50;
36 const int kSecondYCoord = 75;
37
38 } // namespace
39
40 class WgcCaptureSourceTest : public ::testing::TestWithParam<CaptureType> {
41 public:
SetUp()42 void SetUp() override {
43 com_initializer_ =
44 std::make_unique<ScopedCOMInitializer>(ScopedCOMInitializer::kMTA);
45 ASSERT_TRUE(com_initializer_->Succeeded());
46 }
47
TearDown()48 void TearDown() override {
49 if (window_open_) {
50 DestroyTestWindow(window_info_);
51 }
52 }
53
SetUpForWindowSource()54 void SetUpForWindowSource() {
55 window_info_ = CreateTestWindow(kWindowTitle);
56 window_open_ = true;
57 source_id_ = reinterpret_cast<DesktopCapturer::SourceId>(window_info_.hwnd);
58 source_factory_ = std::make_unique<WgcWindowSourceFactory>();
59 }
60
SetUpForScreenSource()61 void SetUpForScreenSource() {
62 source_id_ = kFullDesktopScreenId;
63 source_factory_ = std::make_unique<WgcScreenSourceFactory>();
64 }
65
66 protected:
67 std::unique_ptr<ScopedCOMInitializer> com_initializer_;
68 std::unique_ptr<WgcCaptureSourceFactory> source_factory_;
69 std::unique_ptr<WgcCaptureSource> source_;
70 DesktopCapturer::SourceId source_id_;
71 WindowInfo window_info_;
72 bool window_open_ = false;
73 };
74
75 // Window specific test
TEST_F(WgcCaptureSourceTest,WindowPosition)76 TEST_F(WgcCaptureSourceTest, WindowPosition) {
77 if (!IsWgcSupported(CaptureType::kWindow)) {
78 RTC_LOG(LS_INFO)
79 << "Skipping WgcCapturerWinTests on unsupported platforms.";
80 GTEST_SKIP();
81 }
82
83 SetUpForWindowSource();
84 source_ = source_factory_->CreateCaptureSource(source_id_);
85 ASSERT_TRUE(source_);
86 EXPECT_EQ(source_->GetSourceId(), source_id_);
87
88 MoveTestWindow(window_info_.hwnd, kFirstXCoord, kFirstYCoord);
89 DesktopVector source_vector = source_->GetTopLeft();
90 EXPECT_EQ(source_vector.x(), kFirstXCoord);
91 EXPECT_EQ(source_vector.y(), kFirstYCoord);
92
93 MoveTestWindow(window_info_.hwnd, kSecondXCoord, kSecondYCoord);
94 source_vector = source_->GetTopLeft();
95 EXPECT_EQ(source_vector.x(), kSecondXCoord);
96 EXPECT_EQ(source_vector.y(), kSecondYCoord);
97 }
98
99 // Screen specific test
TEST_F(WgcCaptureSourceTest,ScreenPosition)100 TEST_F(WgcCaptureSourceTest, ScreenPosition) {
101 if (!IsWgcSupported(CaptureType::kScreen)) {
102 RTC_LOG(LS_INFO)
103 << "Skipping WgcCapturerWinTests on unsupported platforms.";
104 GTEST_SKIP();
105 }
106
107 SetUpForScreenSource();
108 source_ = source_factory_->CreateCaptureSource(source_id_);
109 ASSERT_TRUE(source_);
110 EXPECT_EQ(source_id_, source_->GetSourceId());
111
112 DesktopRect screen_rect = GetFullscreenRect();
113 DesktopVector source_vector = source_->GetTopLeft();
114 EXPECT_EQ(source_vector.x(), screen_rect.left());
115 EXPECT_EQ(source_vector.y(), screen_rect.top());
116 }
117
118 // Source agnostic test
TEST_P(WgcCaptureSourceTest,CreateSource)119 TEST_P(WgcCaptureSourceTest, CreateSource) {
120 if (!IsWgcSupported(GetParam())) {
121 RTC_LOG(LS_INFO)
122 << "Skipping WgcCapturerWinTests on unsupported platforms.";
123 GTEST_SKIP();
124 }
125
126 if (GetParam() == CaptureType::kWindow) {
127 SetUpForWindowSource();
128 } else {
129 SetUpForScreenSource();
130 }
131
132 source_ = source_factory_->CreateCaptureSource(source_id_);
133 ASSERT_TRUE(source_);
134 EXPECT_EQ(source_id_, source_->GetSourceId());
135 EXPECT_TRUE(source_->IsCapturable());
136
137 Microsoft::WRL::ComPtr<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>
138 item;
139 EXPECT_TRUE(SUCCEEDED(source_->GetCaptureItem(&item)));
140 EXPECT_TRUE(item);
141 }
142
143 INSTANTIATE_TEST_SUITE_P(SourceAgnostic,
144 WgcCaptureSourceTest,
145 ::testing::Values(CaptureType::kWindow,
146 CaptureType::kScreen));
147
148 } // namespace webrtc
149