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
3 // found in the LICENSE file.
5 #include "remoting/host/video_scheduler.h"
10 #include "base/callback.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/stl_util.h"
15 #include "base/sys_info.h"
16 #include "base/time/time.h"
17 #include "remoting/proto/control.pb.h"
18 #include "remoting/proto/internal.pb.h"
19 #include "remoting/proto/video.pb.h"
20 #include "remoting/protocol/cursor_shape_stub.h"
21 #include "remoting/protocol/message_decoder.h"
22 #include "remoting/protocol/util.h"
23 #include "remoting/protocol/video_stub.h"
24 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
25 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_shape.h"
26 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
30 // Maximum number of frames that can be processed simultaneously.
31 // TODO(hclam): Move this value to CaptureScheduler.
32 static const int kMaxPendingFrames
= 2;
34 VideoScheduler::VideoScheduler(
35 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
36 scoped_refptr
<base::SingleThreadTaskRunner
> encode_task_runner
,
37 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner
,
38 scoped_ptr
<webrtc::ScreenCapturer
> capturer
,
39 scoped_ptr
<VideoEncoder
> encoder
,
40 protocol::CursorShapeStub
* cursor_stub
,
41 protocol::VideoStub
* video_stub
)
42 : capture_task_runner_(capture_task_runner
),
43 encode_task_runner_(encode_task_runner
),
44 network_task_runner_(network_task_runner
),
45 capturer_(capturer
.Pass()),
46 encoder_(encoder
.Pass()),
47 cursor_stub_(cursor_stub
),
48 video_stub_(video_stub
),
50 capture_pending_(false),
51 did_skip_frame_(false),
54 DCHECK(network_task_runner_
->BelongsToCurrentThread());
61 // Public methods --------------------------------------------------------------
63 webrtc::SharedMemory
* VideoScheduler::CreateSharedMemory(size_t size
) {
67 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame
* frame
) {
68 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
70 capture_pending_
= false;
72 scoped_ptr
<webrtc::DesktopFrame
> owned_frame(frame
);
75 scheduler_
.RecordCaptureTime(
76 base::TimeDelta::FromMilliseconds(frame
->capture_time_ms()));
79 encode_task_runner_
->PostTask(
80 FROM_HERE
, base::Bind(&VideoScheduler::EncodeFrame
, this,
81 base::Passed(&owned_frame
), sequence_number_
));
83 // If a frame was skipped, try to capture it again.
84 if (did_skip_frame_
) {
85 capture_task_runner_
->PostTask(
86 FROM_HERE
, base::Bind(&VideoScheduler::CaptureNextFrame
, this));
90 void VideoScheduler::OnCursorShapeChanged(
91 webrtc::MouseCursorShape
* cursor_shape
) {
92 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
94 scoped_ptr
<webrtc::MouseCursorShape
> owned_cursor(cursor_shape
);
96 // Do nothing if the scheduler is being stopped.
100 scoped_ptr
<protocol::CursorShapeInfo
> cursor_proto(
101 new protocol::CursorShapeInfo());
102 cursor_proto
->set_width(cursor_shape
->size
.width());
103 cursor_proto
->set_height(cursor_shape
->size
.height());
104 cursor_proto
->set_hotspot_x(cursor_shape
->hotspot
.x());
105 cursor_proto
->set_hotspot_y(cursor_shape
->hotspot
.y());
106 cursor_proto
->set_data(cursor_shape
->data
);
108 network_task_runner_
->PostTask(
109 FROM_HERE
, base::Bind(&VideoScheduler::SendCursorShape
, this,
110 base::Passed(&cursor_proto
)));
113 void VideoScheduler::Start() {
114 DCHECK(network_task_runner_
->BelongsToCurrentThread());
116 capture_task_runner_
->PostTask(
117 FROM_HERE
, base::Bind(&VideoScheduler::StartOnCaptureThread
, this));
120 void VideoScheduler::Stop() {
121 DCHECK(network_task_runner_
->BelongsToCurrentThread());
123 // Clear stubs to prevent further updates reaching the client.
127 capture_task_runner_
->PostTask(FROM_HERE
,
128 base::Bind(&VideoScheduler::StopOnCaptureThread
, this));
131 void VideoScheduler::Pause(bool pause
) {
132 if (!capture_task_runner_
->BelongsToCurrentThread()) {
133 DCHECK(network_task_runner_
->BelongsToCurrentThread());
134 capture_task_runner_
->PostTask(
135 FROM_HERE
, base::Bind(&VideoScheduler::Pause
, this, pause
));
139 if (is_paused_
!= pause
) {
142 // Restart captures if we're resuming and there are none scheduled.
143 if (!is_paused_
&& capture_timer_
&& !capture_timer_
->IsRunning())
148 void VideoScheduler::UpdateSequenceNumber(int64 sequence_number
) {
149 if (!capture_task_runner_
->BelongsToCurrentThread()) {
150 DCHECK(network_task_runner_
->BelongsToCurrentThread());
151 capture_task_runner_
->PostTask(
152 FROM_HERE
, base::Bind(&VideoScheduler::UpdateSequenceNumber
,
153 this, sequence_number
));
157 sequence_number_
= sequence_number
;
160 // Private methods -----------------------------------------------------------
162 VideoScheduler::~VideoScheduler() {
165 // Capturer thread -------------------------------------------------------------
167 void VideoScheduler::StartOnCaptureThread() {
168 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
169 DCHECK(!capture_timer_
);
171 // Start the capturer and let it notify us if cursor shape changes.
172 capturer_
->SetMouseShapeObserver(this);
173 capturer_
->Start(this);
175 capture_timer_
.reset(new base::OneShotTimer
<VideoScheduler
>());
177 // Capture first frame immedately.
181 void VideoScheduler::StopOnCaptureThread() {
182 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
184 // This doesn't deleted already captured frames, so encoder can keep using the
185 // frames that were captured previously.
188 // |capture_timer_| must be destroyed on the thread on which it is used.
189 capture_timer_
.reset();
192 void VideoScheduler::ScheduleNextCapture() {
193 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
195 capture_timer_
->Start(FROM_HERE
,
196 scheduler_
.NextCaptureDelay(),
198 &VideoScheduler::CaptureNextFrame
);
201 void VideoScheduler::CaptureNextFrame() {
202 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
204 // If we are stopping (|capturer_| is NULL), or paused, then don't capture.
205 if (!capturer_
|| is_paused_
)
208 // Make sure we have at most two outstanding recordings. We can simply return
209 // if we can't make a capture now, the next capture will be started by the
210 // end of an encode operation.
211 if (pending_frames_
>= kMaxPendingFrames
|| capture_pending_
) {
212 did_skip_frame_
= true;
216 did_skip_frame_
= false;
218 // At this point we are going to perform one capture so save the current time.
220 DCHECK_LE(pending_frames_
, kMaxPendingFrames
);
222 // Before doing a capture schedule for the next one.
223 ScheduleNextCapture();
225 capture_pending_
= true;
227 // And finally perform one capture.
228 capturer_
->Capture(webrtc::DesktopRegion());
231 void VideoScheduler::FrameCaptureCompleted() {
232 DCHECK(capture_task_runner_
->BelongsToCurrentThread());
234 // Decrement the pending capture count.
236 DCHECK_GE(pending_frames_
, 0);
238 // If we've skipped a frame capture because too we had too many captures
239 // pending then schedule one now.
244 // Network thread --------------------------------------------------------------
246 void VideoScheduler::SendVideoPacket(scoped_ptr
<VideoPacket
> packet
) {
247 DCHECK(network_task_runner_
->BelongsToCurrentThread());
252 video_stub_
->ProcessVideoPacket(
253 packet
.Pass(), base::Bind(&VideoScheduler::VideoFrameSentCallback
, this));
256 void VideoScheduler::VideoFrameSentCallback() {
257 DCHECK(network_task_runner_
->BelongsToCurrentThread());
262 capture_task_runner_
->PostTask(
263 FROM_HERE
, base::Bind(&VideoScheduler::FrameCaptureCompleted
, this));
266 void VideoScheduler::SendCursorShape(
267 scoped_ptr
<protocol::CursorShapeInfo
> cursor_shape
) {
268 DCHECK(network_task_runner_
->BelongsToCurrentThread());
273 cursor_stub_
->SetCursorShape(*cursor_shape
);
276 // Encoder thread --------------------------------------------------------------
278 void VideoScheduler::EncodeFrame(
279 scoped_ptr
<webrtc::DesktopFrame
> frame
,
280 int64 sequence_number
) {
281 DCHECK(encode_task_runner_
->BelongsToCurrentThread());
283 // If there is nothing to encode then send an empty keep-alive packet.
284 if (!frame
|| frame
->updated_region().is_empty()) {
285 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
286 packet
->set_client_sequence_number(sequence_number
);
287 network_task_runner_
->PostTask(
288 FROM_HERE
, base::Bind(&VideoScheduler::SendVideoPacket
, this,
289 base::Passed(&packet
)));
290 capture_task_runner_
->DeleteSoon(FROM_HERE
, frame
.release());
294 scoped_ptr
<VideoPacket
> packet
= encoder_
->Encode(*frame
);
295 packet
->set_client_sequence_number(sequence_number
);
297 // Destroy the frame before sending |packet| because SendVideoPacket() may
298 // trigger another frame to be captured, and the screen capturer expects the
299 // old frame to be freed by then.
302 scheduler_
.RecordEncodeTime(
303 base::TimeDelta::FromMilliseconds(packet
->encode_time_ms()));
304 network_task_runner_
->PostTask(
305 FROM_HERE
, base::Bind(&VideoScheduler::SendVideoPacket
, this,
306 base::Passed(&packet
)));
309 } // namespace remoting