Update parsing of dumpsys batterystats
[chromium-blink-merge.git] / remoting / host / video_scheduler.cc
blobfab4136e918fa2b86d0db8cd0fa141b13b1bf659
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 // Interval between empty keep-alive frames. These frames are sent only
34 // when there are no real video frames being sent. To prevent PseudoTCP from
35 // resetting congestion window this value must be smaller than the minimum
36 // RTO used in PseudoTCP, which is 250ms.
37 static const int kKeepAlivePacketIntervalMs = 200;
39 VideoScheduler::VideoScheduler(
40 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner,
42 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
43 scoped_ptr<webrtc::ScreenCapturer> capturer,
44 scoped_ptr<VideoEncoder> encoder,
45 protocol::CursorShapeStub* cursor_stub,
46 protocol::VideoStub* video_stub)
47 : capture_task_runner_(capture_task_runner),
48 encode_task_runner_(encode_task_runner),
49 network_task_runner_(network_task_runner),
50 capturer_(capturer.Pass()),
51 encoder_(encoder.Pass()),
52 cursor_stub_(cursor_stub),
53 video_stub_(video_stub),
54 pending_frames_(0),
55 capture_pending_(false),
56 did_skip_frame_(false),
57 is_paused_(false),
58 sequence_number_(0) {
59 DCHECK(network_task_runner_->BelongsToCurrentThread());
60 DCHECK(capturer_);
61 DCHECK(encoder_);
62 DCHECK(cursor_stub_);
63 DCHECK(video_stub_);
66 // Public methods --------------------------------------------------------------
68 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) {
69 return NULL;
72 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
73 DCHECK(capture_task_runner_->BelongsToCurrentThread());
75 capture_pending_ = false;
77 scoped_ptr<webrtc::DesktopFrame> owned_frame(frame);
79 if (owned_frame) {
80 scheduler_.RecordCaptureTime(
81 base::TimeDelta::FromMilliseconds(owned_frame->capture_time_ms()));
84 // Even when |frame| is NULL we still need to post it to the encode thread
85 // to make sure frames are freed in the same order they are received and
86 // that we don't start capturing frame n+2 before frame n is freed.
87 encode_task_runner_->PostTask(
88 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this,
89 base::Passed(&owned_frame), sequence_number_));
91 // If a frame was skipped, try to capture it again.
92 if (did_skip_frame_) {
93 capture_task_runner_->PostTask(
94 FROM_HERE, base::Bind(&VideoScheduler::CaptureNextFrame, this));
98 void VideoScheduler::OnCursorShapeChanged(
99 webrtc::MouseCursorShape* cursor_shape) {
100 DCHECK(capture_task_runner_->BelongsToCurrentThread());
102 scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape);
104 // Do nothing if the scheduler is being stopped.
105 if (!capturer_)
106 return;
108 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
109 new protocol::CursorShapeInfo());
110 cursor_proto->set_width(cursor_shape->size.width());
111 cursor_proto->set_height(cursor_shape->size.height());
112 cursor_proto->set_hotspot_x(cursor_shape->hotspot.x());
113 cursor_proto->set_hotspot_y(cursor_shape->hotspot.y());
114 cursor_proto->set_data(cursor_shape->data);
116 network_task_runner_->PostTask(
117 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
118 base::Passed(&cursor_proto)));
121 void VideoScheduler::Start() {
122 DCHECK(network_task_runner_->BelongsToCurrentThread());
124 capture_task_runner_->PostTask(
125 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this));
128 void VideoScheduler::Stop() {
129 DCHECK(network_task_runner_->BelongsToCurrentThread());
131 // Clear stubs to prevent further updates reaching the client.
132 cursor_stub_ = NULL;
133 video_stub_ = NULL;
135 keep_alive_timer_.reset();
137 capture_task_runner_->PostTask(
138 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this));
141 void VideoScheduler::Pause(bool pause) {
142 if (!capture_task_runner_->BelongsToCurrentThread()) {
143 DCHECK(network_task_runner_->BelongsToCurrentThread());
144 capture_task_runner_->PostTask(
145 FROM_HERE, base::Bind(&VideoScheduler::Pause, this, pause));
146 return;
149 if (is_paused_ != pause) {
150 is_paused_ = pause;
152 // Restart captures if we're resuming and there are none scheduled.
153 if (!is_paused_ && capture_timer_ && !capture_timer_->IsRunning())
154 CaptureNextFrame();
158 void VideoScheduler::UpdateSequenceNumber(int64 sequence_number) {
159 if (!capture_task_runner_->BelongsToCurrentThread()) {
160 DCHECK(network_task_runner_->BelongsToCurrentThread());
161 capture_task_runner_->PostTask(
162 FROM_HERE, base::Bind(&VideoScheduler::UpdateSequenceNumber,
163 this, sequence_number));
164 return;
167 sequence_number_ = sequence_number;
170 void VideoScheduler::SetLosslessEncode(bool want_lossless) {
171 if (!encode_task_runner_->BelongsToCurrentThread()) {
172 DCHECK(network_task_runner_->BelongsToCurrentThread());
173 encode_task_runner_->PostTask(
174 FROM_HERE, base::Bind(&VideoScheduler::SetLosslessEncode,
175 this, want_lossless));
176 return;
179 encoder_->SetLosslessEncode(want_lossless);
182 void VideoScheduler::SetLosslessColor(bool want_lossless) {
183 if (!encode_task_runner_->BelongsToCurrentThread()) {
184 DCHECK(network_task_runner_->BelongsToCurrentThread());
185 encode_task_runner_->PostTask(
186 FROM_HERE, base::Bind(&VideoScheduler::SetLosslessColor,
187 this, want_lossless));
188 return;
191 encoder_->SetLosslessColor(want_lossless);
194 // Private methods -----------------------------------------------------------
196 VideoScheduler::~VideoScheduler() {
197 // Destroy the capturer and encoder on their respective threads.
198 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release());
199 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release());
202 // Capturer thread -------------------------------------------------------------
204 void VideoScheduler::StartOnCaptureThread() {
205 DCHECK(capture_task_runner_->BelongsToCurrentThread());
206 DCHECK(!capture_timer_);
208 // Start the capturer and let it notify us if cursor shape changes.
209 capturer_->SetMouseShapeObserver(this);
210 capturer_->Start(this);
212 capture_timer_.reset(new base::OneShotTimer<VideoScheduler>());
213 keep_alive_timer_.reset(new base::DelayTimer<VideoScheduler>(
214 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs),
215 this, &VideoScheduler::SendKeepAlivePacket));
217 // Capture first frame immedately.
218 CaptureNextFrame();
221 void VideoScheduler::StopOnCaptureThread() {
222 DCHECK(capture_task_runner_->BelongsToCurrentThread());
224 // This doesn't deleted already captured frames, so encoder can keep using the
225 // frames that were captured previously.
226 capturer_.reset();
228 // |capture_timer_| must be destroyed on the thread on which it is used.
229 capture_timer_.reset();
232 void VideoScheduler::ScheduleNextCapture() {
233 DCHECK(capture_task_runner_->BelongsToCurrentThread());
235 capture_timer_->Start(FROM_HERE,
236 scheduler_.NextCaptureDelay(),
237 this,
238 &VideoScheduler::CaptureNextFrame);
241 void VideoScheduler::CaptureNextFrame() {
242 DCHECK(capture_task_runner_->BelongsToCurrentThread());
244 // If we are stopping (|capturer_| is NULL), or paused, then don't capture.
245 if (!capturer_ || is_paused_)
246 return;
248 // Make sure we have at most two outstanding recordings. We can simply return
249 // if we can't make a capture now, the next capture will be started by the
250 // end of an encode operation.
251 if (pending_frames_ >= kMaxPendingFrames || capture_pending_) {
252 did_skip_frame_ = true;
253 return;
256 did_skip_frame_ = false;
258 // At this point we are going to perform one capture so save the current time.
259 pending_frames_++;
260 DCHECK_LE(pending_frames_, kMaxPendingFrames);
262 // Before doing a capture schedule for the next one.
263 ScheduleNextCapture();
265 capture_pending_ = true;
267 // And finally perform one capture.
268 capturer_->Capture(webrtc::DesktopRegion());
271 void VideoScheduler::FrameCaptureCompleted() {
272 DCHECK(capture_task_runner_->BelongsToCurrentThread());
274 // Decrement the pending capture count.
275 pending_frames_--;
276 DCHECK_GE(pending_frames_, 0);
278 // If we've skipped a frame capture because too we had too many captures
279 // pending then schedule one now.
280 if (did_skip_frame_)
281 CaptureNextFrame();
284 // Network thread --------------------------------------------------------------
286 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) {
287 DCHECK(network_task_runner_->BelongsToCurrentThread());
289 if (!video_stub_)
290 return;
292 video_stub_->ProcessVideoPacket(
293 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this));
296 void VideoScheduler::OnVideoPacketSent() {
297 DCHECK(network_task_runner_->BelongsToCurrentThread());
299 if (!video_stub_)
300 return;
302 keep_alive_timer_->Reset();
304 capture_task_runner_->PostTask(
305 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this));
308 void VideoScheduler::SendKeepAlivePacket() {
309 DCHECK(network_task_runner_->BelongsToCurrentThread());
311 if (!video_stub_)
312 return;
314 video_stub_->ProcessVideoPacket(
315 scoped_ptr<VideoPacket>(new VideoPacket()),
316 base::Bind(&VideoScheduler::OnKeepAlivePacketSent, this));
319 void VideoScheduler::OnKeepAlivePacketSent() {
320 DCHECK(network_task_runner_->BelongsToCurrentThread());
322 if (keep_alive_timer_)
323 keep_alive_timer_->Reset();
326 void VideoScheduler::SendCursorShape(
327 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) {
328 DCHECK(network_task_runner_->BelongsToCurrentThread());
330 if (!cursor_stub_)
331 return;
333 cursor_stub_->SetCursorShape(*cursor_shape);
336 // Encoder thread --------------------------------------------------------------
338 void VideoScheduler::EncodeFrame(
339 scoped_ptr<webrtc::DesktopFrame> frame,
340 int64 sequence_number) {
341 DCHECK(encode_task_runner_->BelongsToCurrentThread());
343 // Drop the frame if there were no changes.
344 if (!frame || frame->updated_region().is_empty()) {
345 capture_task_runner_->DeleteSoon(FROM_HERE, frame.release());
346 capture_task_runner_->PostTask(
347 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this));
348 return;
351 scoped_ptr<VideoPacket> packet = encoder_->Encode(*frame);
352 packet->set_client_sequence_number(sequence_number);
354 // Destroy the frame before sending |packet| because SendVideoPacket() may
355 // trigger another frame to be captured, and the screen capturer expects the
356 // old frame to be freed by then.
357 frame.reset();
359 scheduler_.RecordEncodeTime(
360 base::TimeDelta::FromMilliseconds(packet->encode_time_ms()));
361 network_task_runner_->PostTask(
362 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this,
363 base::Passed(&packet)));
366 } // namespace remoting