OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/video_scheduler.h" | 5 #include "remoting/host/video_scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "remoting/base/auto_thread.h" | 11 #include "remoting/base/auto_thread.h" |
12 #include "remoting/base/auto_thread_task_runner.h" | 12 #include "remoting/base/auto_thread_task_runner.h" |
| 13 #include "remoting/codec/video_encoder.h" |
13 #include "remoting/codec/video_encoder_verbatim.h" | 14 #include "remoting/codec/video_encoder_verbatim.h" |
| 15 #include "remoting/host/fake_mouse_cursor_monitor.h" |
14 #include "remoting/host/fake_screen_capturer.h" | 16 #include "remoting/host/fake_screen_capturer.h" |
| 17 #include "remoting/host/host_mock_objects.h" |
| 18 #include "remoting/proto/control.pb.h" |
15 #include "remoting/proto/video.pb.h" | 19 #include "remoting/proto/video.pb.h" |
16 #include "remoting/protocol/protocol_mock_objects.h" | 20 #include "remoting/protocol/protocol_mock_objects.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 23 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 24 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
20 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object
s.h" | 25 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object
s.h" |
21 | 26 |
22 using ::remoting::protocol::MockClientStub; | 27 using ::remoting::protocol::MockClientStub; |
23 using ::remoting::protocol::MockVideoStub; | 28 using ::remoting::protocol::MockVideoStub; |
24 | 29 |
25 using ::testing::_; | 30 using ::testing::_; |
26 using ::testing::AtLeast; | 31 using ::testing::AtLeast; |
27 using ::testing::AnyNumber; | 32 using ::testing::AnyNumber; |
28 using ::testing::DeleteArg; | 33 using ::testing::DeleteArg; |
29 using ::testing::DoAll; | 34 using ::testing::DoAll; |
(...skipping 14 matching lines...) Expand all Loading... |
44 } | 49 } |
45 | 50 |
46 ACTION(FinishSend) { | 51 ACTION(FinishSend) { |
47 arg1.Run(); | 52 arg1.Run(); |
48 } | 53 } |
49 | 54 |
50 } // namespace | 55 } // namespace |
51 | 56 |
52 static const int kWidth = 640; | 57 static const int kWidth = 640; |
53 static const int kHeight = 480; | 58 static const int kHeight = 480; |
| 59 static const int kCursorWidth = 64; |
| 60 static const int kCursorHeight = 32; |
| 61 static const int kHotspotX = 11; |
| 62 static const int kHotspotY = 12; |
54 | 63 |
55 class MockVideoEncoder : public VideoEncoder { | 64 class MockVideoEncoder : public VideoEncoder { |
56 public: | 65 public: |
57 MockVideoEncoder() {} | 66 MockVideoEncoder() {} |
58 virtual ~MockVideoEncoder() {} | 67 virtual ~MockVideoEncoder() {} |
59 | 68 |
60 scoped_ptr<VideoPacket> Encode( | 69 scoped_ptr<VideoPacket> Encode( |
61 const webrtc::DesktopFrame& frame) { | 70 const webrtc::DesktopFrame& frame) { |
62 return scoped_ptr<VideoPacket>(EncodePtr(frame)); | 71 return scoped_ptr<VideoPacket>(EncodePtr(frame)); |
63 } | 72 } |
(...skipping 28 matching lines...) Expand all Loading... |
92 virtual ~ThreadCheckScreenCapturer() { | 101 virtual ~ThreadCheckScreenCapturer() { |
93 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 102 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
94 } | 103 } |
95 | 104 |
96 private: | 105 private: |
97 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 106 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
98 | 107 |
99 DISALLOW_COPY_AND_ASSIGN(ThreadCheckScreenCapturer); | 108 DISALLOW_COPY_AND_ASSIGN(ThreadCheckScreenCapturer); |
100 }; | 109 }; |
101 | 110 |
| 111 class ThreadCheckMouseCursorMonitor : public FakeMouseCursorMonitor { |
| 112 public: |
| 113 ThreadCheckMouseCursorMonitor( |
| 114 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 115 : task_runner_(task_runner) { |
| 116 } |
| 117 virtual ~ThreadCheckMouseCursorMonitor() { |
| 118 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 119 } |
| 120 |
| 121 private: |
| 122 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor); |
| 125 }; |
| 126 |
102 class VideoSchedulerTest : public testing::Test { | 127 class VideoSchedulerTest : public testing::Test { |
103 public: | 128 public: |
104 VideoSchedulerTest(); | 129 VideoSchedulerTest(); |
105 | 130 |
106 virtual void SetUp() OVERRIDE; | 131 virtual void SetUp() OVERRIDE; |
107 virtual void TearDown() OVERRIDE; | 132 virtual void TearDown() OVERRIDE; |
108 | 133 |
109 void StartVideoScheduler( | 134 void StartVideoScheduler( |
110 scoped_ptr<webrtc::ScreenCapturer> capturer, | 135 scoped_ptr<webrtc::ScreenCapturer> capturer, |
111 scoped_ptr<VideoEncoder> encoder); | 136 scoped_ptr<VideoEncoder> encoder, |
| 137 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); |
112 void StopVideoScheduler(); | 138 void StopVideoScheduler(); |
113 | 139 |
114 // webrtc::ScreenCapturer mocks. | 140 // webrtc::ScreenCapturer mocks. |
115 void OnCapturerStart(webrtc::ScreenCapturer::Callback* callback); | 141 void OnCapturerStart(webrtc::ScreenCapturer::Callback* callback); |
116 void OnCaptureFrame(const webrtc::DesktopRegion& region); | 142 void OnCaptureFrame(const webrtc::DesktopRegion& region); |
| 143 void OnMouseCursorMonitorInit( |
| 144 webrtc::MouseCursorMonitor::Callback* callback, |
| 145 webrtc::MouseCursorMonitor::Mode mode); |
| 146 void OnCaptureMouse(); |
| 147 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); |
117 | 148 |
118 protected: | 149 protected: |
119 base::MessageLoop message_loop_; | 150 base::MessageLoop message_loop_; |
120 base::RunLoop run_loop_; | 151 base::RunLoop run_loop_; |
121 scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; | 152 scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; |
122 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; | 153 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; |
123 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; | 154 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
124 scoped_refptr<VideoScheduler> scheduler_; | 155 scoped_refptr<VideoScheduler> scheduler_; |
125 | 156 |
126 MockClientStub client_stub_; | 157 MockClientStub client_stub_; |
127 MockVideoStub video_stub_; | 158 MockVideoStub video_stub_; |
128 | 159 |
129 scoped_ptr<webrtc::DesktopFrame> frame_; | |
130 | |
131 // Points to the callback passed to webrtc::ScreenCapturer::Start(). | 160 // Points to the callback passed to webrtc::ScreenCapturer::Start(). |
132 webrtc::ScreenCapturer::Callback* capturer_callback_; | 161 webrtc::ScreenCapturer::Callback* capturer_callback_; |
133 | 162 |
| 163 // Points to the callback passed to webrtc::MouseCursor::Init(). |
| 164 webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; |
| 165 |
134 private: | 166 private: |
135 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 167 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); |
136 }; | 168 }; |
137 | 169 |
138 VideoSchedulerTest::VideoSchedulerTest() | 170 VideoSchedulerTest::VideoSchedulerTest() |
139 : capturer_callback_(NULL) { | 171 : capturer_callback_(NULL), |
| 172 mouse_monitor_callback_(NULL) { |
140 } | 173 } |
141 | 174 |
142 void VideoSchedulerTest::SetUp() { | 175 void VideoSchedulerTest::SetUp() { |
143 main_task_runner_ = new AutoThreadTaskRunner( | 176 main_task_runner_ = new AutoThreadTaskRunner( |
144 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | 177 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
145 capture_task_runner_ = main_task_runner_; | 178 capture_task_runner_ = main_task_runner_; |
146 encode_task_runner_ = main_task_runner_; | 179 encode_task_runner_ = main_task_runner_; |
147 } | 180 } |
148 | 181 |
149 void VideoSchedulerTest::TearDown() { | 182 void VideoSchedulerTest::TearDown() { |
150 // Release the task runners, so that the test can quit. | 183 // Release the task runners, so that the test can quit. |
151 capture_task_runner_ = NULL; | 184 capture_task_runner_ = NULL; |
152 encode_task_runner_ = NULL; | 185 encode_task_runner_ = NULL; |
153 main_task_runner_ = NULL; | 186 main_task_runner_ = NULL; |
154 | 187 |
155 // Run the MessageLoop until everything has torn down. | 188 // Run the MessageLoop until everything has torn down. |
156 run_loop_.Run(); | 189 run_loop_.Run(); |
157 } | 190 } |
158 | 191 |
159 void VideoSchedulerTest::StartVideoScheduler( | 192 void VideoSchedulerTest::StartVideoScheduler( |
160 scoped_ptr<webrtc::ScreenCapturer> capturer, | 193 scoped_ptr<webrtc::ScreenCapturer> capturer, |
161 scoped_ptr<VideoEncoder> encoder) { | 194 scoped_ptr<VideoEncoder> encoder, |
| 195 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { |
162 scheduler_ = new VideoScheduler( | 196 scheduler_ = new VideoScheduler( |
163 capture_task_runner_, | 197 capture_task_runner_, |
164 encode_task_runner_, | 198 encode_task_runner_, |
165 main_task_runner_, | 199 main_task_runner_, |
166 capturer.Pass(), | 200 capturer.Pass(), |
| 201 mouse_monitor.Pass(), |
167 encoder.Pass(), | 202 encoder.Pass(), |
168 &client_stub_, | 203 &client_stub_, |
169 &video_stub_); | 204 &video_stub_); |
170 scheduler_->Start(); | 205 scheduler_->Start(); |
171 } | 206 } |
172 | 207 |
173 void VideoSchedulerTest::StopVideoScheduler() { | 208 void VideoSchedulerTest::StopVideoScheduler() { |
174 scheduler_->Stop(); | 209 scheduler_->Stop(); |
175 scheduler_ = NULL; | 210 scheduler_ = NULL; |
176 } | 211 } |
177 | 212 |
178 void VideoSchedulerTest::OnCapturerStart( | 213 void VideoSchedulerTest::OnCapturerStart( |
179 webrtc::ScreenCapturer::Callback* callback) { | 214 webrtc::ScreenCapturer::Callback* callback) { |
180 EXPECT_FALSE(capturer_callback_); | 215 EXPECT_FALSE(capturer_callback_); |
181 EXPECT_TRUE(callback); | 216 EXPECT_TRUE(callback); |
182 | 217 |
183 capturer_callback_ = callback; | 218 capturer_callback_ = callback; |
184 } | 219 } |
185 | 220 |
186 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { | 221 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { |
187 frame_->mutable_updated_region()->SetRect( | 222 scoped_ptr<webrtc::DesktopFrame> frame( |
| 223 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); |
| 224 frame->mutable_updated_region()->SetRect( |
188 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); | 225 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
189 capturer_callback_->OnCaptureCompleted(frame_.release()); | 226 capturer_callback_->OnCaptureCompleted(frame.release()); |
| 227 } |
| 228 |
| 229 void VideoSchedulerTest::OnCaptureMouse() { |
| 230 EXPECT_TRUE(mouse_monitor_callback_); |
| 231 |
| 232 scoped_ptr<webrtc::MouseCursor> mouse_cursor( |
| 233 new webrtc::MouseCursor( |
| 234 new webrtc::BasicDesktopFrame( |
| 235 webrtc::DesktopSize(kCursorWidth, kCursorHeight)), |
| 236 webrtc::DesktopVector(kHotspotX, kHotspotY))); |
| 237 |
| 238 mouse_monitor_callback_->OnMouseCursor(mouse_cursor.release()); |
| 239 } |
| 240 |
| 241 void VideoSchedulerTest::OnMouseCursorMonitorInit( |
| 242 webrtc::MouseCursorMonitor::Callback* callback, |
| 243 webrtc::MouseCursorMonitor::Mode mode) { |
| 244 EXPECT_FALSE(mouse_monitor_callback_); |
| 245 EXPECT_TRUE(callback); |
| 246 |
| 247 mouse_monitor_callback_ = callback; |
| 248 } |
| 249 |
| 250 void VideoSchedulerTest::SetCursorShape( |
| 251 const protocol::CursorShapeInfo& cursor_shape) { |
| 252 EXPECT_TRUE(cursor_shape.has_width()); |
| 253 EXPECT_EQ(kCursorWidth, cursor_shape.width()); |
| 254 EXPECT_TRUE(cursor_shape.has_height()); |
| 255 EXPECT_EQ(kCursorHeight, cursor_shape.height()); |
| 256 EXPECT_TRUE(cursor_shape.has_hotspot_x()); |
| 257 EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); |
| 258 EXPECT_TRUE(cursor_shape.has_hotspot_y()); |
| 259 EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); |
| 260 EXPECT_TRUE(cursor_shape.has_data()); |
| 261 EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, |
| 262 static_cast<int>(cursor_shape.data().size())); |
190 } | 263 } |
191 | 264 |
192 // This test mocks capturer, encoder and network layer to simulate one capture | 265 // This test mocks capturer, encoder and network layer to simulate one capture |
193 // cycle. When the first encoded packet is submitted to the network | 266 // cycle. When the first encoded packet is submitted to the network |
194 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 267 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
195 // sequence to be executed successfully. | 268 // sequence to be executed successfully. |
196 TEST_F(VideoSchedulerTest, StartAndStop) { | 269 TEST_F(VideoSchedulerTest, StartAndStop) { |
197 scoped_ptr<webrtc::MockScreenCapturer> capturer( | 270 scoped_ptr<webrtc::MockScreenCapturer> capturer( |
198 new webrtc::MockScreenCapturer()); | 271 new webrtc::MockScreenCapturer()); |
| 272 scoped_ptr<MockMouseCursorMonitor> cursor_monitor( |
| 273 new MockMouseCursorMonitor()); |
| 274 |
| 275 { |
| 276 InSequence s; |
| 277 |
| 278 EXPECT_CALL(*cursor_monitor, Init(_, _)) |
| 279 .WillOnce( |
| 280 Invoke(this, &VideoSchedulerTest::OnMouseCursorMonitorInit)); |
| 281 |
| 282 EXPECT_CALL(*cursor_monitor, Capture()) |
| 283 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureMouse)); |
| 284 } |
| 285 |
199 Expectation capturer_start = | 286 Expectation capturer_start = |
200 EXPECT_CALL(*capturer, Start(_)) | 287 EXPECT_CALL(*capturer, Start(_)) |
201 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 288 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); |
202 | 289 |
203 frame_.reset(new webrtc::BasicDesktopFrame( | |
204 webrtc::DesktopSize(kWidth, kHeight))); | |
205 | |
206 // First the capturer is called. | 290 // First the capturer is called. |
207 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) | 291 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) |
208 .After(capturer_start) | 292 .After(capturer_start) |
209 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 293 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); |
210 | 294 |
211 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder()); | 295 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder()); |
212 | 296 |
213 // Expect the encoder be called. | 297 // Expect the encoder be called. |
214 EXPECT_CALL(*encoder, EncodePtr(_)) | 298 EXPECT_CALL(*encoder, EncodePtr(_)) |
215 .WillRepeatedly(FinishEncode()); | 299 .WillRepeatedly(FinishEncode()); |
216 | 300 |
217 // By default delete the arguments when ProcessVideoPacket is received. | 301 // By default delete the arguments when ProcessVideoPacket is received. |
218 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 302 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
219 .WillRepeatedly(FinishSend()); | 303 .WillRepeatedly(FinishSend()); |
220 | 304 |
221 // When the first ProcessVideoPacket is received we stop the VideoScheduler. | 305 // When the first ProcessVideoPacket is received we stop the VideoScheduler. |
222 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 306 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
223 .WillOnce(DoAll( | 307 .WillOnce(DoAll( |
224 FinishSend(), | 308 FinishSend(), |
225 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 309 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
226 .RetiresOnSaturation(); | 310 .RetiresOnSaturation(); |
227 | 311 |
| 312 EXPECT_CALL(client_stub_, SetCursorShape(_)) |
| 313 .WillOnce(Invoke(this, &VideoSchedulerTest::SetCursorShape)); |
| 314 |
228 // Start video frame capture. | 315 // Start video frame capture. |
| 316 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( |
| 317 new FakeMouseCursorMonitor()); |
229 StartVideoScheduler(capturer.PassAs<webrtc::ScreenCapturer>(), | 318 StartVideoScheduler(capturer.PassAs<webrtc::ScreenCapturer>(), |
230 encoder.PassAs<VideoEncoder>()); | 319 encoder.PassAs<VideoEncoder>(), |
| 320 cursor_monitor.PassAs<webrtc::MouseCursorMonitor>()); |
| 321 |
| 322 // Run until there are no more pending tasks from the VideoScheduler. |
| 323 // Otherwise, a lingering frame capture might attempt to trigger a capturer |
| 324 // expectation action and crash. |
| 325 base::RunLoop().RunUntilIdle(); |
231 } | 326 } |
232 | 327 |
233 // Verify that the capturer and encoder are torn down on the correct threads. | 328 // Verify that the capturer, encoder and mouse monitor are torn down on the |
| 329 // correct threads. |
234 TEST_F(VideoSchedulerTest, DeleteOnThreads) { | 330 TEST_F(VideoSchedulerTest, DeleteOnThreads) { |
235 capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); | 331 capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); |
236 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); | 332 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); |
237 | 333 |
238 scoped_ptr<webrtc::ScreenCapturer> capturer( | 334 scoped_ptr<webrtc::ScreenCapturer> capturer( |
239 new ThreadCheckScreenCapturer(capture_task_runner_)); | 335 new ThreadCheckScreenCapturer(capture_task_runner_)); |
240 scoped_ptr<VideoEncoder> encoder( | 336 scoped_ptr<VideoEncoder> encoder( |
241 new ThreadCheckVideoEncoder(encode_task_runner_)); | 337 new ThreadCheckVideoEncoder(encode_task_runner_)); |
| 338 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( |
| 339 new ThreadCheckMouseCursorMonitor(capture_task_runner_)); |
242 | 340 |
243 // Start and stop the scheduler, so it will tear down the screen capturer and | 341 // Start and stop the scheduler, so it will tear down the screen capturer, |
244 // video encoder. | 342 // video encoder and mouse monitor. |
245 StartVideoScheduler(capturer.Pass(), encoder.Pass()); | 343 StartVideoScheduler(capturer.Pass(), encoder.Pass(), |
| 344 mouse_cursor_monitor.Pass()); |
246 StopVideoScheduler(); | 345 StopVideoScheduler(); |
247 } | 346 } |
248 | 347 |
249 } // namespace remoting | 348 } // namespace remoting |
OLD | NEW |