OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_frame_pump.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.h" |
14 #include "remoting/codec/video_encoder_verbatim.h" | 14 #include "remoting/codec/video_encoder_verbatim.h" |
15 #include "remoting/host/fake_desktop_capturer.h" | 15 #include "remoting/host/fake_desktop_capturer.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 ~ThreadCheckMouseCursorMonitor() override { | 116 ~ThreadCheckMouseCursorMonitor() override { |
117 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 117 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
118 } | 118 } |
119 | 119 |
120 private: | 120 private: |
121 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 121 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
122 | 122 |
123 DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor); | 123 DISALLOW_COPY_AND_ASSIGN(ThreadCheckMouseCursorMonitor); |
124 }; | 124 }; |
125 | 125 |
126 class VideoSchedulerTest : public testing::Test { | 126 class VideoFramePumpTest : public testing::Test { |
127 public: | 127 public: |
128 VideoSchedulerTest(); | 128 VideoFramePumpTest(); |
129 | 129 |
130 void SetUp() override; | 130 void SetUp() override; |
131 void TearDown() override; | 131 void TearDown() override; |
132 | 132 |
133 void StartVideoScheduler( | 133 void StartVideoFramePump( |
134 scoped_ptr<webrtc::DesktopCapturer> capturer, | 134 scoped_ptr<webrtc::DesktopCapturer> capturer, |
135 scoped_ptr<VideoEncoder> encoder, | 135 scoped_ptr<VideoEncoder> encoder, |
136 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); | 136 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); |
137 void StopVideoScheduler(); | 137 void StopVideoFramePump(); |
138 | 138 |
139 // webrtc::DesktopCapturer mocks. | 139 // webrtc::DesktopCapturer mocks. |
140 void OnCapturerStart(webrtc::DesktopCapturer::Callback* callback); | 140 void OnCapturerStart(webrtc::DesktopCapturer::Callback* callback); |
141 void OnCaptureFrame(const webrtc::DesktopRegion& region); | 141 void OnCaptureFrame(const webrtc::DesktopRegion& region); |
142 | 142 |
143 // webrtc::MouseCursorMonitor mocks. | 143 // webrtc::MouseCursorMonitor mocks. |
144 void OnMouseCursorMonitorInit( | 144 void OnMouseCursorMonitorInit( |
145 webrtc::MouseCursorMonitor::Callback* callback, | 145 webrtc::MouseCursorMonitor::Callback* callback, |
146 webrtc::MouseCursorMonitor::Mode mode); | 146 webrtc::MouseCursorMonitor::Mode mode); |
147 void OnCaptureMouse(); | 147 void OnCaptureMouse(); |
148 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); | 148 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); |
149 | 149 |
150 protected: | 150 protected: |
151 base::MessageLoop message_loop_; | 151 base::MessageLoop message_loop_; |
152 base::RunLoop run_loop_; | 152 base::RunLoop run_loop_; |
153 scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; | 153 scoped_refptr<AutoThreadTaskRunner> capture_task_runner_; |
154 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; | 154 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; |
155 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; | 155 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
156 scoped_refptr<VideoScheduler> scheduler_; | 156 scoped_refptr<VideoFramePump> scheduler_; |
157 | 157 |
158 MockClientStub client_stub_; | 158 MockClientStub client_stub_; |
159 MockVideoStub video_stub_; | 159 MockVideoStub video_stub_; |
160 | 160 |
161 // Points to the callback passed to webrtc::DesktopCapturer::Start(). | 161 // Points to the callback passed to webrtc::DesktopCapturer::Start(). |
162 webrtc::DesktopCapturer::Callback* capturer_callback_; | 162 webrtc::DesktopCapturer::Callback* capturer_callback_; |
163 | 163 |
164 // Points to the callback passed to webrtc::MouseCursor::Init(). | 164 // Points to the callback passed to webrtc::MouseCursor::Init(). |
165 webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; | 165 webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; |
166 | 166 |
167 private: | 167 private: |
168 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 168 DISALLOW_COPY_AND_ASSIGN(VideoFramePumpTest); |
169 }; | 169 }; |
170 | 170 |
171 VideoSchedulerTest::VideoSchedulerTest() | 171 VideoFramePumpTest::VideoFramePumpTest() |
172 : capturer_callback_(nullptr), | 172 : capturer_callback_(nullptr), |
173 mouse_monitor_callback_(nullptr) { | 173 mouse_monitor_callback_(nullptr) { |
174 } | 174 } |
175 | 175 |
176 void VideoSchedulerTest::SetUp() { | 176 void VideoFramePumpTest::SetUp() { |
177 main_task_runner_ = new AutoThreadTaskRunner( | 177 main_task_runner_ = new AutoThreadTaskRunner( |
178 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | 178 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
179 capture_task_runner_ = main_task_runner_; | 179 capture_task_runner_ = main_task_runner_; |
180 encode_task_runner_ = main_task_runner_; | 180 encode_task_runner_ = main_task_runner_; |
181 } | 181 } |
182 | 182 |
183 void VideoSchedulerTest::TearDown() { | 183 void VideoFramePumpTest::TearDown() { |
184 // Release the task runners, so that the test can quit. | 184 // Release the task runners, so that the test can quit. |
185 capture_task_runner_ = nullptr; | 185 capture_task_runner_ = nullptr; |
186 encode_task_runner_ = nullptr; | 186 encode_task_runner_ = nullptr; |
187 main_task_runner_ = nullptr; | 187 main_task_runner_ = nullptr; |
188 | 188 |
189 // Run the MessageLoop until everything has torn down. | 189 // Run the MessageLoop until everything has torn down. |
190 run_loop_.Run(); | 190 run_loop_.Run(); |
191 } | 191 } |
192 | 192 |
193 void VideoSchedulerTest::StartVideoScheduler( | 193 void VideoFramePumpTest::StartVideoFramePump( |
194 scoped_ptr<webrtc::DesktopCapturer> capturer, | 194 scoped_ptr<webrtc::DesktopCapturer> capturer, |
195 scoped_ptr<VideoEncoder> encoder, | 195 scoped_ptr<VideoEncoder> encoder, |
196 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { | 196 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { |
197 scheduler_ = new VideoScheduler( | 197 scheduler_ = new VideoFramePump( |
198 capture_task_runner_, | 198 capture_task_runner_, |
199 encode_task_runner_, | 199 encode_task_runner_, |
200 main_task_runner_, | 200 main_task_runner_, |
201 capturer.Pass(), | 201 capturer.Pass(), |
202 mouse_monitor.Pass(), | 202 mouse_monitor.Pass(), |
203 encoder.Pass(), | 203 encoder.Pass(), |
204 &client_stub_, | 204 &client_stub_, |
205 &video_stub_); | 205 &video_stub_); |
206 scheduler_->Start(); | 206 scheduler_->Start(); |
207 } | 207 } |
208 | 208 |
209 void VideoSchedulerTest::StopVideoScheduler() { | 209 void VideoFramePumpTest::StopVideoFramePump() { |
210 scheduler_->Stop(); | 210 scheduler_->Stop(); |
211 scheduler_ = nullptr; | 211 scheduler_ = nullptr; |
212 } | 212 } |
213 | 213 |
214 void VideoSchedulerTest::OnCapturerStart( | 214 void VideoFramePumpTest::OnCapturerStart( |
215 webrtc::DesktopCapturer::Callback* callback) { | 215 webrtc::DesktopCapturer::Callback* callback) { |
216 EXPECT_FALSE(capturer_callback_); | 216 EXPECT_FALSE(capturer_callback_); |
217 EXPECT_TRUE(callback); | 217 EXPECT_TRUE(callback); |
218 | 218 |
219 capturer_callback_ = callback; | 219 capturer_callback_ = callback; |
220 } | 220 } |
221 | 221 |
222 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { | 222 void VideoFramePumpTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { |
223 scoped_ptr<webrtc::DesktopFrame> frame( | 223 scoped_ptr<webrtc::DesktopFrame> frame( |
224 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); | 224 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); |
225 frame->mutable_updated_region()->SetRect( | 225 frame->mutable_updated_region()->SetRect( |
226 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); | 226 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
227 capturer_callback_->OnCaptureCompleted(frame.release()); | 227 capturer_callback_->OnCaptureCompleted(frame.release()); |
228 } | 228 } |
229 | 229 |
230 void VideoSchedulerTest::OnCaptureMouse() { | 230 void VideoFramePumpTest::OnCaptureMouse() { |
231 EXPECT_TRUE(mouse_monitor_callback_); | 231 EXPECT_TRUE(mouse_monitor_callback_); |
232 | 232 |
233 scoped_ptr<webrtc::MouseCursor> mouse_cursor( | 233 scoped_ptr<webrtc::MouseCursor> mouse_cursor( |
234 new webrtc::MouseCursor( | 234 new webrtc::MouseCursor( |
235 new webrtc::BasicDesktopFrame( | 235 new webrtc::BasicDesktopFrame( |
236 webrtc::DesktopSize(kCursorWidth, kCursorHeight)), | 236 webrtc::DesktopSize(kCursorWidth, kCursorHeight)), |
237 webrtc::DesktopVector(kHotspotX, kHotspotY))); | 237 webrtc::DesktopVector(kHotspotX, kHotspotY))); |
238 | 238 |
239 mouse_monitor_callback_->OnMouseCursor(mouse_cursor.release()); | 239 mouse_monitor_callback_->OnMouseCursor(mouse_cursor.release()); |
240 } | 240 } |
241 | 241 |
242 void VideoSchedulerTest::OnMouseCursorMonitorInit( | 242 void VideoFramePumpTest::OnMouseCursorMonitorInit( |
243 webrtc::MouseCursorMonitor::Callback* callback, | 243 webrtc::MouseCursorMonitor::Callback* callback, |
244 webrtc::MouseCursorMonitor::Mode mode) { | 244 webrtc::MouseCursorMonitor::Mode mode) { |
245 EXPECT_FALSE(mouse_monitor_callback_); | 245 EXPECT_FALSE(mouse_monitor_callback_); |
246 EXPECT_TRUE(callback); | 246 EXPECT_TRUE(callback); |
247 | 247 |
248 mouse_monitor_callback_ = callback; | 248 mouse_monitor_callback_ = callback; |
249 } | 249 } |
250 | 250 |
251 void VideoSchedulerTest::SetCursorShape( | 251 void VideoFramePumpTest::SetCursorShape( |
252 const protocol::CursorShapeInfo& cursor_shape) { | 252 const protocol::CursorShapeInfo& cursor_shape) { |
253 EXPECT_TRUE(cursor_shape.has_width()); | 253 EXPECT_TRUE(cursor_shape.has_width()); |
254 EXPECT_EQ(kCursorWidth, cursor_shape.width()); | 254 EXPECT_EQ(kCursorWidth, cursor_shape.width()); |
255 EXPECT_TRUE(cursor_shape.has_height()); | 255 EXPECT_TRUE(cursor_shape.has_height()); |
256 EXPECT_EQ(kCursorHeight, cursor_shape.height()); | 256 EXPECT_EQ(kCursorHeight, cursor_shape.height()); |
257 EXPECT_TRUE(cursor_shape.has_hotspot_x()); | 257 EXPECT_TRUE(cursor_shape.has_hotspot_x()); |
258 EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); | 258 EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); |
259 EXPECT_TRUE(cursor_shape.has_hotspot_y()); | 259 EXPECT_TRUE(cursor_shape.has_hotspot_y()); |
260 EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); | 260 EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); |
261 EXPECT_TRUE(cursor_shape.has_data()); | 261 EXPECT_TRUE(cursor_shape.has_data()); |
262 EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, | 262 EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, |
263 static_cast<int>(cursor_shape.data().size())); | 263 static_cast<int>(cursor_shape.data().size())); |
264 } | 264 } |
265 | 265 |
266 // This test mocks capturer, encoder and network layer to simulate one capture | 266 // This test mocks capturer, encoder and network layer to simulate one capture |
267 // cycle. When the first encoded packet is submitted to the network | 267 // cycle. When the first encoded packet is submitted to the network |
268 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 268 // VideoFramePump is instructed to come to a complete stop. We expect the stop |
269 // sequence to be executed successfully. | 269 // sequence to be executed successfully. |
270 TEST_F(VideoSchedulerTest, StartAndStop) { | 270 TEST_F(VideoFramePumpTest, StartAndStop) { |
271 scoped_ptr<webrtc::MockScreenCapturer> capturer( | 271 scoped_ptr<webrtc::MockScreenCapturer> capturer( |
272 new webrtc::MockScreenCapturer()); | 272 new webrtc::MockScreenCapturer()); |
273 scoped_ptr<MockMouseCursorMonitor> cursor_monitor( | 273 scoped_ptr<MockMouseCursorMonitor> cursor_monitor( |
274 new MockMouseCursorMonitor()); | 274 new MockMouseCursorMonitor()); |
275 | 275 |
276 { | 276 { |
277 InSequence s; | 277 InSequence s; |
278 | 278 |
279 EXPECT_CALL(*cursor_monitor, Init(_, _)) | 279 EXPECT_CALL(*cursor_monitor, Init(_, _)) |
280 .WillOnce( | 280 .WillOnce( |
281 Invoke(this, &VideoSchedulerTest::OnMouseCursorMonitorInit)); | 281 Invoke(this, &VideoFramePumpTest::OnMouseCursorMonitorInit)); |
282 | 282 |
283 EXPECT_CALL(*cursor_monitor, Capture()) | 283 EXPECT_CALL(*cursor_monitor, Capture()) |
284 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureMouse)); | 284 .WillRepeatedly(Invoke(this, &VideoFramePumpTest::OnCaptureMouse)); |
285 } | 285 } |
286 | 286 |
287 Expectation capturer_start = | 287 Expectation capturer_start = |
288 EXPECT_CALL(*capturer, Start(_)) | 288 EXPECT_CALL(*capturer, Start(_)) |
289 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 289 .WillOnce(Invoke(this, &VideoFramePumpTest::OnCapturerStart)); |
290 | 290 |
291 // First the capturer is called. | 291 // First the capturer is called. |
292 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) | 292 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) |
293 .After(capturer_start) | 293 .After(capturer_start) |
294 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 294 .WillRepeatedly(Invoke(this, &VideoFramePumpTest::OnCaptureFrame)); |
295 | 295 |
296 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder()); | 296 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder()); |
297 | 297 |
298 // Expect the encoder be called. | 298 // Expect the encoder be called. |
299 EXPECT_CALL(*encoder, EncodePtr(_)) | 299 EXPECT_CALL(*encoder, EncodePtr(_)) |
300 .WillRepeatedly(FinishEncode()); | 300 .WillRepeatedly(FinishEncode()); |
301 | 301 |
302 // By default delete the arguments when ProcessVideoPacket is received. | 302 // By default delete the arguments when ProcessVideoPacket is received. |
303 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 303 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
304 .WillRepeatedly(FinishSend()); | 304 .WillRepeatedly(FinishSend()); |
305 | 305 |
306 // When the first ProcessVideoPacket is received we stop the VideoScheduler. | 306 // When the first ProcessVideoPacket is received we stop the VideoFramePump. |
307 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 307 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
308 .WillOnce(DoAll( | 308 .WillOnce(DoAll( |
309 FinishSend(), | 309 FinishSend(), |
310 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 310 InvokeWithoutArgs(this, &VideoFramePumpTest::StopVideoFramePump))) |
311 .RetiresOnSaturation(); | 311 .RetiresOnSaturation(); |
312 | 312 |
313 EXPECT_CALL(client_stub_, SetCursorShape(_)) | 313 EXPECT_CALL(client_stub_, SetCursorShape(_)) |
314 .WillOnce(Invoke(this, &VideoSchedulerTest::SetCursorShape)); | 314 .WillOnce(Invoke(this, &VideoFramePumpTest::SetCursorShape)); |
315 | 315 |
316 // Start video frame capture. | 316 // Start video frame capture. |
317 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( | 317 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( |
318 new FakeMouseCursorMonitor()); | 318 new FakeMouseCursorMonitor()); |
319 StartVideoScheduler(capturer.Pass(), encoder.Pass(), cursor_monitor.Pass()); | 319 StartVideoFramePump(capturer.Pass(), encoder.Pass(), cursor_monitor.Pass()); |
320 | 320 |
321 // Run until there are no more pending tasks from the VideoScheduler. | 321 // Run until there are no more pending tasks from the VideoFramePump. |
322 // Otherwise, a lingering frame capture might attempt to trigger a capturer | 322 // Otherwise, a lingering frame capture might attempt to trigger a capturer |
323 // expectation action and crash. | 323 // expectation action and crash. |
324 base::RunLoop().RunUntilIdle(); | 324 base::RunLoop().RunUntilIdle(); |
325 } | 325 } |
326 | 326 |
327 // Verify that the capturer, encoder and mouse monitor are torn down on the | 327 // Verify that the capturer, encoder and mouse monitor are torn down on the |
328 // correct threads. | 328 // correct threads. |
329 TEST_F(VideoSchedulerTest, DeleteOnThreads) { | 329 TEST_F(VideoFramePumpTest, DeleteOnThreads) { |
330 capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); | 330 capture_task_runner_ = AutoThread::Create("capture", main_task_runner_); |
331 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); | 331 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); |
332 | 332 |
333 scoped_ptr<webrtc::DesktopCapturer> capturer( | 333 scoped_ptr<webrtc::DesktopCapturer> capturer( |
334 new ThreadCheckDesktopCapturer(capture_task_runner_)); | 334 new ThreadCheckDesktopCapturer(capture_task_runner_)); |
335 scoped_ptr<VideoEncoder> encoder( | 335 scoped_ptr<VideoEncoder> encoder( |
336 new ThreadCheckVideoEncoder(encode_task_runner_)); | 336 new ThreadCheckVideoEncoder(encode_task_runner_)); |
337 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( | 337 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor( |
338 new ThreadCheckMouseCursorMonitor(capture_task_runner_)); | 338 new ThreadCheckMouseCursorMonitor(capture_task_runner_)); |
339 | 339 |
340 // Start and stop the scheduler, so it will tear down the screen capturer, | 340 // Start and stop the scheduler, so it will tear down the screen capturer, |
341 // video encoder and mouse monitor. | 341 // video encoder and mouse monitor. |
342 StartVideoScheduler(capturer.Pass(), encoder.Pass(), | 342 StartVideoFramePump(capturer.Pass(), encoder.Pass(), |
343 mouse_cursor_monitor.Pass()); | 343 mouse_cursor_monitor.Pass()); |
344 StopVideoScheduler(); | 344 StopVideoFramePump(); |
345 } | 345 } |
346 | 346 |
347 } // namespace remoting | 347 } // namespace remoting |
OLD | NEW |