Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / host / video_scheduler.cc
blob7fc1983128d8d0c36604e502b64e08fbd2a88135
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"
7 #include <algorithm>
9 #include "base/bind.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/video_stub.h"
23 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
24 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_shape.h"
25 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
27 namespace remoting {
29 // Maximum number of frames that can be processed simultaneously.
30 // TODO(hclam): Move this value to CaptureScheduler.
31 static const int kMaxPendingFrames = 2;
33 VideoScheduler::VideoScheduler(
34 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner,
35 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner,
36 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
37 scoped_ptr<webrtc::ScreenCapturer> capturer,
38 scoped_ptr<VideoEncoder> encoder,
39 protocol::CursorShapeStub* cursor_stub,
40 protocol::VideoStub* video_stub)
41 : capture_task_runner_(capture_task_runner),
42 encode_task_runner_(encode_task_runner),
43 network_task_runner_(network_task_runner),
44 capturer_(capturer.Pass()),
45 encoder_(encoder.Pass()),
46 cursor_stub_(cursor_stub),
47 video_stub_(video_stub),
48 pending_frames_(0),
49 capture_pending_(false),
50 did_skip_frame_(false),
51 is_paused_(false),
52 sequence_number_(0) {
53 DCHECK(network_task_runner_->BelongsToCurrentThread());
54 DCHECK(capturer_);
55 DCHECK(encoder_);
56 DCHECK(cursor_stub_);
57 DCHECK(video_stub_);
60 // Public methods --------------------------------------------------------------
62 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) {
63 return NULL;
66 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
67 DCHECK(capture_task_runner_->BelongsToCurrentThread());
69 capture_pending_ = false;
71 scoped_ptr<webrtc::DesktopFrame> owned_frame(frame);
73 if (frame) {
74 scheduler_.RecordCaptureTime(
75 base::TimeDelta::FromMilliseconds(frame->capture_time_ms()));
78 encode_task_runner_->PostTask(
79 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this,
80 base::Passed(&owned_frame), sequence_number_));
82 // If a frame was skipped, try to capture it again.
83 if (did_skip_frame_) {
84 capture_task_runner_->PostTask(
85 FROM_HERE, base::Bind(&VideoScheduler::CaptureNextFrame, this));
89 void VideoScheduler::OnCursorShapeChanged(
90 webrtc::MouseCursorShape* cursor_shape) {
91 DCHECK(capture_task_runner_->BelongsToCurrentThread());
93 scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape);
95 // Do nothing if the scheduler is being stopped.
96 if (!capturer_)
97 return;
99 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
100 new protocol::CursorShapeInfo());
101 cursor_proto->set_width(cursor_shape->size.width());
102 cursor_proto->set_height(cursor_shape->size.height());
103 cursor_proto->set_hotspot_x(cursor_shape->hotspot.x());
104 cursor_proto->set_hotspot_y(cursor_shape->hotspot.y());
105 cursor_proto->set_data(cursor_shape->data);
107 network_task_runner_->PostTask(
108 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
109 base::Passed(&cursor_proto)));
112 void VideoScheduler::Start() {
113 DCHECK(network_task_runner_->BelongsToCurrentThread());
115 capture_task_runner_->PostTask(
116 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this));
119 void VideoScheduler::Stop() {
120 DCHECK(network_task_runner_->BelongsToCurrentThread());
122 // Clear stubs to prevent further updates reaching the client.
123 cursor_stub_ = NULL;
124 video_stub_ = NULL;
126 capture_task_runner_->PostTask(FROM_HERE,
127 base::Bind(&VideoScheduler::StopOnCaptureThread, this));
130 void VideoScheduler::Pause(bool pause) {
131 if (!capture_task_runner_->BelongsToCurrentThread()) {
132 DCHECK(network_task_runner_->BelongsToCurrentThread());
133 capture_task_runner_->PostTask(
134 FROM_HERE, base::Bind(&VideoScheduler::Pause, this, pause));
135 return;
138 if (is_paused_ != pause) {
139 is_paused_ = pause;
141 // Restart captures if we're resuming and there are none scheduled.
142 if (!is_paused_ && capture_timer_ && !capture_timer_->IsRunning())
143 CaptureNextFrame();
147 void VideoScheduler::UpdateSequenceNumber(int64 sequence_number) {
148 if (!capture_task_runner_->BelongsToCurrentThread()) {
149 DCHECK(network_task_runner_->BelongsToCurrentThread());
150 capture_task_runner_->PostTask(
151 FROM_HERE, base::Bind(&VideoScheduler::UpdateSequenceNumber,
152 this, sequence_number));
153 return;
156 sequence_number_ = sequence_number;
159 // Private methods -----------------------------------------------------------
161 VideoScheduler::~VideoScheduler() {
164 // Capturer thread -------------------------------------------------------------
166 void VideoScheduler::StartOnCaptureThread() {
167 DCHECK(capture_task_runner_->BelongsToCurrentThread());
168 DCHECK(!capture_timer_);
170 // Start the capturer and let it notify us if cursor shape changes.
171 capturer_->SetMouseShapeObserver(this);
172 capturer_->Start(this);
174 capture_timer_.reset(new base::OneShotTimer<VideoScheduler>());
176 // Capture first frame immedately.
177 CaptureNextFrame();
180 void VideoScheduler::StopOnCaptureThread() {
181 DCHECK(capture_task_runner_->BelongsToCurrentThread());
183 // This doesn't deleted already captured frames, so encoder can keep using the
184 // frames that were captured previously.
185 capturer_.reset();
187 // |capture_timer_| must be destroyed on the thread on which it is used.
188 capture_timer_.reset();
191 void VideoScheduler::ScheduleNextCapture() {
192 DCHECK(capture_task_runner_->BelongsToCurrentThread());
194 capture_timer_->Start(FROM_HERE,
195 scheduler_.NextCaptureDelay(),
196 this,
197 &VideoScheduler::CaptureNextFrame);
200 void VideoScheduler::CaptureNextFrame() {
201 DCHECK(capture_task_runner_->BelongsToCurrentThread());
203 // If we are stopping (|capturer_| is NULL), or paused, then don't capture.
204 if (!capturer_ || is_paused_)
205 return;
207 // Make sure we have at most two outstanding recordings. We can simply return
208 // if we can't make a capture now, the next capture will be started by the
209 // end of an encode operation.
210 if (pending_frames_ >= kMaxPendingFrames || capture_pending_) {
211 did_skip_frame_ = true;
212 return;
215 did_skip_frame_ = false;
217 // At this point we are going to perform one capture so save the current time.
218 pending_frames_++;
219 DCHECK_LE(pending_frames_, kMaxPendingFrames);
221 // Before doing a capture schedule for the next one.
222 ScheduleNextCapture();
224 capture_pending_ = true;
226 // And finally perform one capture.
227 capturer_->Capture(webrtc::DesktopRegion());
230 void VideoScheduler::FrameCaptureCompleted() {
231 DCHECK(capture_task_runner_->BelongsToCurrentThread());
233 // Decrement the pending capture count.
234 pending_frames_--;
235 DCHECK_GE(pending_frames_, 0);
237 // If we've skipped a frame capture because too we had too many captures
238 // pending then schedule one now.
239 if (did_skip_frame_)
240 CaptureNextFrame();
243 // Network thread --------------------------------------------------------------
245 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) {
246 DCHECK(network_task_runner_->BelongsToCurrentThread());
248 if (!video_stub_)
249 return;
251 video_stub_->ProcessVideoPacket(
252 packet.Pass(), base::Bind(&VideoScheduler::VideoFrameSentCallback, this));
255 void VideoScheduler::VideoFrameSentCallback() {
256 DCHECK(network_task_runner_->BelongsToCurrentThread());
258 if (!video_stub_)
259 return;
261 capture_task_runner_->PostTask(
262 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this));
265 void VideoScheduler::SendCursorShape(
266 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) {
267 DCHECK(network_task_runner_->BelongsToCurrentThread());
269 if (!cursor_stub_)
270 return;
272 cursor_stub_->SetCursorShape(*cursor_shape);
275 // Encoder thread --------------------------------------------------------------
277 void VideoScheduler::EncodeFrame(
278 scoped_ptr<webrtc::DesktopFrame> frame,
279 int64 sequence_number) {
280 DCHECK(encode_task_runner_->BelongsToCurrentThread());
282 // If there is nothing to encode then send an empty keep-alive packet.
283 if (!frame || frame->updated_region().is_empty()) {
284 scoped_ptr<VideoPacket> packet(new VideoPacket());
285 packet->set_client_sequence_number(sequence_number);
286 network_task_runner_->PostTask(
287 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this,
288 base::Passed(&packet)));
289 capture_task_runner_->DeleteSoon(FROM_HERE, frame.release());
290 return;
293 scoped_ptr<VideoPacket> packet = encoder_->Encode(*frame);
294 packet->set_client_sequence_number(sequence_number);
296 // Destroy the frame before sending |packet| because SendVideoPacket() may
297 // trigger another frame to be captured, and the screen capturer expects the
298 // old frame to be freed by then.
299 frame.reset();
301 scheduler_.RecordEncodeTime(
302 base::TimeDelta::FromMilliseconds(packet->encode_time_ms()));
303 network_task_runner_->PostTask(
304 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this,
305 base::Passed(&packet)));
308 } // namespace remoting