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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "remoting/proto/control.pb.h" | 17 #include "remoting/proto/control.pb.h" |
18 #include "remoting/proto/internal.pb.h" | 18 #include "remoting/proto/internal.pb.h" |
19 #include "remoting/proto/video.pb.h" | 19 #include "remoting/proto/video.pb.h" |
20 #include "remoting/protocol/cursor_shape_stub.h" | 20 #include "remoting/protocol/cursor_shape_stub.h" |
21 #include "remoting/protocol/message_decoder.h" | 21 #include "remoting/protocol/message_decoder.h" |
22 #include "remoting/protocol/video_stub.h" | 22 #include "remoting/protocol/video_sender.h" |
23 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | 23 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" |
24 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 24 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
25 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" | 25 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
26 | 26 |
27 namespace remoting { | 27 namespace remoting { |
28 | 28 |
29 // Maximum number of frames that can be processed simultaneously. | 29 // Maximum number of frames that can be processed simultaneously. In case the |
30 // video channel has Ack messages enabled the VideoSender acknowledges frames | |
31 // only after the corresponding Ack message is received. The values are chosen | |
32 // experimentally to maximize frame rate, while keeping round-trip latency low. | |
30 // TODO(hclam): Move this value to CaptureScheduler. | 33 // TODO(hclam): Move this value to CaptureScheduler. |
31 static const int kMaxPendingFrames = 2; | 34 static const int kMaxPendingFrames = 4; |
Wez
2015/01/21 01:35:38
kMaxPendingFrames controls the number of screen-ca
Sergey Ulanov
2015/01/29 01:33:27
Fixed it now. CaptureScheduler now separately chec
| |
35 static const int kMaxPendingFramesWithoutAcks = 2; | |
32 | 36 |
33 // Interval between empty keep-alive frames. These frames are sent only when the | 37 // Interval between empty keep-alive frames. These frames are sent only when the |
34 // stream is paused or inactive for some other reason (e.g. when blocked on | 38 // stream is paused or inactive for some other reason (e.g. when blocked on |
35 // capturer). To prevent PseudoTCP from resetting congestion window this value | 39 // capturer). To prevent PseudoTCP from resetting congestion window this value |
36 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. | 40 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. |
37 static const int kKeepAlivePacketIntervalMs = 200; | 41 static const int kKeepAlivePacketIntervalMs = 200; |
38 | 42 |
39 static bool g_enable_timestamps = false; | 43 static bool g_enable_timestamps = false; |
40 | 44 |
41 // static | 45 // static |
42 void VideoScheduler::EnableTimestampsForTests() { | 46 void VideoScheduler::EnableTimestampsForTests() { |
43 g_enable_timestamps = true; | 47 g_enable_timestamps = true; |
44 } | 48 } |
45 | 49 |
46 VideoScheduler::VideoScheduler( | 50 VideoScheduler::VideoScheduler( |
47 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
48 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 52 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
49 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | 53 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
50 scoped_ptr<webrtc::DesktopCapturer> capturer, | 54 scoped_ptr<webrtc::DesktopCapturer> capturer, |
51 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | 55 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, |
52 scoped_ptr<VideoEncoder> encoder, | 56 scoped_ptr<VideoEncoder> encoder, |
53 protocol::CursorShapeStub* cursor_stub, | 57 protocol::CursorShapeStub* cursor_stub, |
54 protocol::VideoStub* video_stub) | 58 protocol::VideoSender* video_sender) |
55 : capture_task_runner_(capture_task_runner), | 59 : capture_task_runner_(capture_task_runner), |
56 encode_task_runner_(encode_task_runner), | 60 encode_task_runner_(encode_task_runner), |
57 network_task_runner_(network_task_runner), | 61 network_task_runner_(network_task_runner), |
58 capturer_(capturer.Pass()), | 62 capturer_(capturer.Pass()), |
59 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), | 63 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), |
60 encoder_(encoder.Pass()), | 64 encoder_(encoder.Pass()), |
61 cursor_stub_(cursor_stub), | 65 cursor_stub_(cursor_stub), |
62 video_stub_(video_stub), | 66 video_sender_(video_sender), |
63 pending_frames_(0), | 67 pending_frames_(0), |
64 capture_pending_(false), | 68 capture_pending_(false), |
65 did_skip_frame_(false), | 69 did_skip_frame_(false), |
66 is_paused_(false), | 70 is_paused_(false), |
67 latest_event_timestamp_(0) { | 71 latest_event_timestamp_(0) { |
68 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 72 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
69 DCHECK(capturer_); | 73 DCHECK(capturer_); |
70 DCHECK(mouse_cursor_monitor_); | 74 DCHECK(mouse_cursor_monitor_); |
71 DCHECK(encoder_); | 75 DCHECK(encoder_); |
72 DCHECK(cursor_stub_); | 76 DCHECK(cursor_stub_); |
73 DCHECK(video_stub_); | 77 DCHECK(video_sender_); |
74 } | 78 } |
75 | 79 |
76 // Public methods -------------------------------------------------------------- | 80 // Public methods -------------------------------------------------------------- |
77 | 81 |
78 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) { | 82 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) { |
79 return nullptr; | 83 return nullptr; |
80 } | 84 } |
81 | 85 |
82 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) { | 86 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) { |
83 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 87 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 | 153 |
150 capture_task_runner_->PostTask( | 154 capture_task_runner_->PostTask( |
151 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); | 155 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); |
152 } | 156 } |
153 | 157 |
154 void VideoScheduler::Stop() { | 158 void VideoScheduler::Stop() { |
155 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 159 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
156 | 160 |
157 // Clear stubs to prevent further updates reaching the client. | 161 // Clear stubs to prevent further updates reaching the client. |
158 cursor_stub_ = nullptr; | 162 cursor_stub_ = nullptr; |
159 video_stub_ = nullptr; | 163 video_sender_ = nullptr; |
160 | 164 |
161 keep_alive_timer_.reset(); | 165 keep_alive_timer_.reset(); |
162 | 166 |
163 capture_task_runner_->PostTask( | 167 capture_task_runner_->PostTask( |
164 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this)); | 168 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this)); |
165 } | 169 } |
166 | 170 |
167 void VideoScheduler::Pause(bool pause) { | 171 void VideoScheduler::Pause(bool pause) { |
168 if (!capture_task_runner_->BelongsToCurrentThread()) { | 172 if (!capture_task_runner_->BelongsToCurrentThread()) { |
169 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 173 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 &VideoScheduler::CaptureNextFrame); | 271 &VideoScheduler::CaptureNextFrame); |
268 } | 272 } |
269 | 273 |
270 void VideoScheduler::CaptureNextFrame() { | 274 void VideoScheduler::CaptureNextFrame() { |
271 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 275 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
272 | 276 |
273 // If we are stopping (|capturer_| is nullptr), or paused, then don't capture. | 277 // If we are stopping (|capturer_| is nullptr), or paused, then don't capture. |
274 if (!capturer_ || is_paused_) | 278 if (!capturer_ || is_paused_) |
275 return; | 279 return; |
276 | 280 |
281 int max_pending_frames = video_sender_->SupportsAcks() | |
282 ? kMaxPendingFrames | |
283 : kMaxPendingFramesWithoutAcks; | |
284 | |
277 // Make sure we have at most two outstanding recordings. We can simply return | 285 // Make sure we have at most two outstanding recordings. We can simply return |
278 // if we can't make a capture now, the next capture will be started by the | 286 // if we can't make a capture now, the next capture will be started by the |
279 // end of an encode operation. | 287 // end of an encode operation. |
280 if (pending_frames_ >= kMaxPendingFrames || capture_pending_) { | 288 if (pending_frames_ >= max_pending_frames || capture_pending_) { |
281 did_skip_frame_ = true; | 289 did_skip_frame_ = true; |
282 return; | 290 return; |
283 } | 291 } |
284 | 292 |
285 did_skip_frame_ = false; | 293 did_skip_frame_ = false; |
286 | 294 |
287 // At this point we are going to perform one capture so save the current time. | 295 // At this point we are going to perform one capture so save the current time. |
288 pending_frames_++; | 296 pending_frames_++; |
289 DCHECK_LE(pending_frames_, kMaxPendingFrames); | 297 DCHECK_LE(pending_frames_, max_pending_frames); |
290 | 298 |
291 // Before doing a capture schedule for the next one. | 299 // Before doing a capture schedule for the next one. |
292 ScheduleNextCapture(); | 300 ScheduleNextCapture(); |
293 | 301 |
294 capture_pending_ = true; | 302 capture_pending_ = true; |
295 | 303 |
296 // Capture the mouse shape. | 304 // Capture the mouse shape. |
297 mouse_cursor_monitor_->Capture(); | 305 mouse_cursor_monitor_->Capture(); |
298 | 306 |
299 // And finally perform one capture. | 307 // And finally perform one capture. |
(...skipping 11 matching lines...) Expand all Loading... | |
311 // pending then schedule one now. | 319 // pending then schedule one now. |
312 if (did_skip_frame_) | 320 if (did_skip_frame_) |
313 CaptureNextFrame(); | 321 CaptureNextFrame(); |
314 } | 322 } |
315 | 323 |
316 // Network thread -------------------------------------------------------------- | 324 // Network thread -------------------------------------------------------------- |
317 | 325 |
318 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) { | 326 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) { |
319 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 327 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
320 | 328 |
321 if (!video_stub_) | 329 if (!video_sender_) |
322 return; | 330 return; |
323 | 331 |
324 video_stub_->ProcessVideoPacket( | 332 video_sender_->ProcessVideoPacket( |
325 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this)); | 333 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this)); |
326 } | 334 } |
327 | 335 |
328 void VideoScheduler::OnVideoPacketSent() { | 336 void VideoScheduler::OnVideoPacketSent() { |
329 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 337 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
330 | 338 |
331 if (!video_stub_) | 339 if (!video_sender_) |
332 return; | 340 return; |
333 | 341 |
334 keep_alive_timer_->Reset(); | 342 keep_alive_timer_->Reset(); |
335 | 343 |
336 capture_task_runner_->PostTask( | 344 capture_task_runner_->PostTask( |
337 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this)); | 345 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this)); |
338 } | 346 } |
339 | 347 |
340 void VideoScheduler::SendKeepAlivePacket() { | 348 void VideoScheduler::SendKeepAlivePacket() { |
341 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 349 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
342 | 350 |
343 if (!video_stub_) | 351 if (!video_sender_) |
344 return; | 352 return; |
345 | 353 |
346 video_stub_->ProcessVideoPacket( | 354 video_sender_->ProcessVideoPacket( |
347 make_scoped_ptr(new VideoPacket()), | 355 make_scoped_ptr(new VideoPacket()), |
348 base::Bind(&VideoScheduler::OnKeepAlivePacketSent, this)); | 356 base::Bind(&VideoScheduler::OnKeepAlivePacketSent, this)); |
349 } | 357 } |
350 | 358 |
351 void VideoScheduler::OnKeepAlivePacketSent() { | 359 void VideoScheduler::OnKeepAlivePacketSent() { |
352 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 360 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
353 | 361 |
354 if (keep_alive_timer_) | 362 if (keep_alive_timer_) |
355 keep_alive_timer_->Reset(); | 363 keep_alive_timer_->Reset(); |
356 } | 364 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 frame.reset(); | 406 frame.reset(); |
399 | 407 |
400 scheduler_.RecordEncodeTime( | 408 scheduler_.RecordEncodeTime( |
401 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); | 409 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); |
402 network_task_runner_->PostTask( | 410 network_task_runner_->PostTask( |
403 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this, | 411 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this, |
404 base::Passed(&packet))); | 412 base::Passed(&packet))); |
405 } | 413 } |
406 | 414 |
407 } // namespace remoting | 415 } // namespace remoting |
OLD | NEW |