Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / host / video_scheduler.cc
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.
4
5 #include "remoting/host/video_scheduler.h"
6
7 #include <algorithm>
8
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"
26
27 namespace remoting {
28
29 // Maximum number of frames that can be processed simultaneously.
30 // TODO(hclam): Move this value to CaptureScheduler.
31 static const int kMaxPendingFrames = 2;
32
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_);
58 }
59
60 // Public methods --------------------------------------------------------------
61
62 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) {
63   return NULL;
64 }
65
66 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
67   DCHECK(capture_task_runner_->BelongsToCurrentThread());
68
69   capture_pending_ = false;
70
71   scoped_ptr<webrtc::DesktopFrame> owned_frame(frame);
72
73   if (frame) {
74     scheduler_.RecordCaptureTime(
75         base::TimeDelta::FromMilliseconds(frame->capture_time_ms()));
76   }
77
78   encode_task_runner_->PostTask(
79       FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this,
80                             base::Passed(&owned_frame), sequence_number_));
81
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));
86   }
87 }
88
89 void VideoScheduler::OnCursorShapeChanged(
90     webrtc::MouseCursorShape* cursor_shape) {
91   DCHECK(capture_task_runner_->BelongsToCurrentThread());
92
93   scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape);
94
95   // Do nothing if the scheduler is being stopped.
96   if (!capturer_)
97     return;
98
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);
106
107   network_task_runner_->PostTask(
108       FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
109                             base::Passed(&cursor_proto)));
110 }
111
112 void VideoScheduler::Start() {
113   DCHECK(network_task_runner_->BelongsToCurrentThread());
114
115   capture_task_runner_->PostTask(
116       FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this));
117 }
118
119 void VideoScheduler::Stop() {
120   DCHECK(network_task_runner_->BelongsToCurrentThread());
121
122   // Clear stubs to prevent further updates reaching the client.
123   cursor_stub_ = NULL;
124   video_stub_ = NULL;
125
126   capture_task_runner_->PostTask(FROM_HERE,
127       base::Bind(&VideoScheduler::StopOnCaptureThread, this));
128 }
129
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;
136   }
137
138   if (is_paused_ != pause) {
139     is_paused_ = pause;
140
141     // Restart captures if we're resuming and there are none scheduled.
142     if (!is_paused_ && capture_timer_ && !capture_timer_->IsRunning())
143       CaptureNextFrame();
144   }
145 }
146
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;
154   }
155
156   sequence_number_ = sequence_number;
157 }
158
159 // Private methods -----------------------------------------------------------
160
161 VideoScheduler::~VideoScheduler() {
162 }
163
164 // Capturer thread -------------------------------------------------------------
165
166 void VideoScheduler::StartOnCaptureThread() {
167   DCHECK(capture_task_runner_->BelongsToCurrentThread());
168   DCHECK(!capture_timer_);
169
170   // Start the capturer and let it notify us if cursor shape changes.
171   capturer_->SetMouseShapeObserver(this);
172   capturer_->Start(this);
173
174   capture_timer_.reset(new base::OneShotTimer<VideoScheduler>());
175
176   // Capture first frame immedately.
177   CaptureNextFrame();
178 }
179
180 void VideoScheduler::StopOnCaptureThread() {
181   DCHECK(capture_task_runner_->BelongsToCurrentThread());
182
183   // This doesn't deleted already captured frames, so encoder can keep using the
184   // frames that were captured previously.
185   capturer_.reset();
186
187   // |capture_timer_| must be destroyed on the thread on which it is used.
188   capture_timer_.reset();
189 }
190
191 void VideoScheduler::ScheduleNextCapture() {
192   DCHECK(capture_task_runner_->BelongsToCurrentThread());
193
194   capture_timer_->Start(FROM_HERE,
195                         scheduler_.NextCaptureDelay(),
196                         this,
197                         &VideoScheduler::CaptureNextFrame);
198 }
199
200 void VideoScheduler::CaptureNextFrame() {
201   DCHECK(capture_task_runner_->BelongsToCurrentThread());
202
203   // If we are stopping (|capturer_| is NULL), or paused, then don't capture.
204   if (!capturer_ || is_paused_)
205     return;
206
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;
213   }
214
215   did_skip_frame_ = false;
216
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);
220
221   // Before doing a capture schedule for the next one.
222   ScheduleNextCapture();
223
224   capture_pending_ = true;
225
226   // And finally perform one capture.
227   capturer_->Capture(webrtc::DesktopRegion());
228 }
229
230 void VideoScheduler::FrameCaptureCompleted() {
231   DCHECK(capture_task_runner_->BelongsToCurrentThread());
232
233   // Decrement the pending capture count.
234   pending_frames_--;
235   DCHECK_GE(pending_frames_, 0);
236
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();
241 }
242
243 // Network thread --------------------------------------------------------------
244
245 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) {
246   DCHECK(network_task_runner_->BelongsToCurrentThread());
247
248   if (!video_stub_)
249     return;
250
251   video_stub_->ProcessVideoPacket(
252       packet.Pass(), base::Bind(&VideoScheduler::VideoFrameSentCallback, this));
253 }
254
255 void VideoScheduler::VideoFrameSentCallback() {
256   DCHECK(network_task_runner_->BelongsToCurrentThread());
257
258   if (!video_stub_)
259     return;
260
261   capture_task_runner_->PostTask(
262       FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this));
263 }
264
265 void VideoScheduler::SendCursorShape(
266     scoped_ptr<protocol::CursorShapeInfo> cursor_shape) {
267   DCHECK(network_task_runner_->BelongsToCurrentThread());
268
269   if (!cursor_stub_)
270     return;
271
272   cursor_stub_->SetCursorShape(*cursor_shape);
273 }
274
275 // Encoder thread --------------------------------------------------------------
276
277 void VideoScheduler::EncodeFrame(
278     scoped_ptr<webrtc::DesktopFrame> frame,
279     int64 sequence_number) {
280   DCHECK(encode_task_runner_->BelongsToCurrentThread());
281
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;
291   }
292
293   scoped_ptr<VideoPacket> packet = encoder_->Encode(*frame);
294   packet->set_client_sequence_number(sequence_number);
295
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();
300
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)));
306 }
307
308 }  // namespace remoting