Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: remoting/host/video_scheduler.cc

Issue 872433005: Move capture scheduling logic from VideoScheduler to CaptureScheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/host/capture_scheduler.h"
17 #include "remoting/proto/control.pb.h" 18 #include "remoting/proto/control.pb.h"
18 #include "remoting/proto/internal.pb.h" 19 #include "remoting/proto/internal.pb.h"
19 #include "remoting/proto/video.pb.h" 20 #include "remoting/proto/video.pb.h"
20 #include "remoting/protocol/cursor_shape_stub.h" 21 #include "remoting/protocol/cursor_shape_stub.h"
21 #include "remoting/protocol/message_decoder.h" 22 #include "remoting/protocol/message_decoder.h"
22 #include "remoting/protocol/video_stub.h" 23 #include "remoting/protocol/video_stub.h"
23 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" 24 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
24 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" 25 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
25 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" 26 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
26 27
27 namespace remoting { 28 namespace remoting {
28 29
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 // Interval between empty keep-alive frames. These frames are sent only when the 30 // 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 31 // 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 32 // capturer). To prevent PseudoTCP from resetting congestion window this value
36 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. 33 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms.
37 static const int kKeepAlivePacketIntervalMs = 200; 34 static const int kKeepAlivePacketIntervalMs = 200;
38 35
39 static bool g_enable_timestamps = false; 36 static bool g_enable_timestamps = false;
40 37
41 // static 38 // static
42 void VideoScheduler::EnableTimestampsForTests() { 39 void VideoScheduler::EnableTimestampsForTests() {
(...skipping 10 matching lines...) Expand all
53 protocol::CursorShapeStub* cursor_stub, 50 protocol::CursorShapeStub* cursor_stub,
54 protocol::VideoStub* video_stub) 51 protocol::VideoStub* video_stub)
55 : capture_task_runner_(capture_task_runner), 52 : capture_task_runner_(capture_task_runner),
56 encode_task_runner_(encode_task_runner), 53 encode_task_runner_(encode_task_runner),
57 network_task_runner_(network_task_runner), 54 network_task_runner_(network_task_runner),
58 capturer_(capturer.Pass()), 55 capturer_(capturer.Pass()),
59 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), 56 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()),
60 encoder_(encoder.Pass()), 57 encoder_(encoder.Pass()),
61 cursor_stub_(cursor_stub), 58 cursor_stub_(cursor_stub),
62 video_stub_(video_stub), 59 video_stub_(video_stub),
63 pending_frames_(0),
64 capture_pending_(false),
65 did_skip_frame_(false),
66 is_paused_(false),
67 latest_event_timestamp_(0) { 60 latest_event_timestamp_(0) {
68 DCHECK(network_task_runner_->BelongsToCurrentThread()); 61 DCHECK(network_task_runner_->BelongsToCurrentThread());
69 DCHECK(capturer_); 62 DCHECK(capturer_);
70 DCHECK(mouse_cursor_monitor_); 63 DCHECK(mouse_cursor_monitor_);
71 DCHECK(encoder_); 64 DCHECK(encoder_);
72 DCHECK(cursor_stub_); 65 DCHECK(cursor_stub_);
73 DCHECK(video_stub_); 66 DCHECK(video_stub_);
74 } 67 }
75 68
76 // Public methods -------------------------------------------------------------- 69 // Public methods --------------------------------------------------------------
77 70
78 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) { 71 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) {
79 return nullptr; 72 return nullptr;
80 } 73 }
81 74
82 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
83 DCHECK(capture_task_runner_->BelongsToCurrentThread());
84
85 capture_pending_ = false;
86
87 scoped_ptr<webrtc::DesktopFrame> owned_frame(frame);
88
89 if (owned_frame) {
90 scheduler_.RecordCaptureTime(
91 base::TimeDelta::FromMilliseconds(owned_frame->capture_time_ms()));
92 }
93
94 // Even when |frame| is nullptr we still need to post it to the encode thread
95 // to make sure frames are freed in the same order they are received and
96 // that we don't start capturing frame n+2 before frame n is freed.
97 encode_task_runner_->PostTask(
98 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this,
99 base::Passed(&owned_frame), latest_event_timestamp_,
100 base::TimeTicks::Now()));
101
102 // If a frame was skipped, try to capture it again.
103 if (did_skip_frame_) {
104 capture_task_runner_->PostTask(
105 FROM_HERE, base::Bind(&VideoScheduler::CaptureNextFrame, this));
106 }
107 }
108
109 void VideoScheduler::OnMouseCursor(webrtc::MouseCursor* cursor) {
110 DCHECK(capture_task_runner_->BelongsToCurrentThread());
111
112 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor);
113
114 // Do nothing if the scheduler is being stopped.
115 if (!capturer_)
116 return;
117
118 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
119 new protocol::CursorShapeInfo());
120 cursor_proto->set_width(cursor->image()->size().width());
121 cursor_proto->set_height(cursor->image()->size().height());
122 cursor_proto->set_hotspot_x(cursor->hotspot().x());
123 cursor_proto->set_hotspot_y(cursor->hotspot().y());
124
125 cursor_proto->set_data(std::string());
126 uint8_t* current_row = cursor->image()->data();
127 for (int y = 0; y < cursor->image()->size().height(); ++y) {
128 cursor_proto->mutable_data()->append(
129 current_row,
130 current_row + cursor->image()->size().width() *
131 webrtc::DesktopFrame::kBytesPerPixel);
132 current_row += cursor->image()->stride();
133 }
134
135 network_task_runner_->PostTask(
136 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
137 base::Passed(&cursor_proto)));
138 }
139
140 void VideoScheduler::OnMouseCursorPosition(
141 webrtc::MouseCursorMonitor::CursorState state,
142 const webrtc::DesktopVector& position) {
143 // We're not subscribing to mouse position changes.
144 NOTREACHED();
145 }
146
147 void VideoScheduler::Start() { 75 void VideoScheduler::Start() {
148 DCHECK(network_task_runner_->BelongsToCurrentThread()); 76 DCHECK(network_task_runner_->BelongsToCurrentThread());
149 77
78 keep_alive_timer_.reset(new base::DelayTimer<VideoScheduler>(
79 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs),
80 this, &VideoScheduler::SendKeepAlivePacket));
81
82 capture_scheduler_.reset(new CaptureScheduler(
83 base::Bind(&VideoScheduler::CaptureNextFrame, this)));
84 capture_scheduler_->Start();
85
150 capture_task_runner_->PostTask( 86 capture_task_runner_->PostTask(
151 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); 87 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this));
152 } 88 }
153 89
154 void VideoScheduler::Stop() { 90 void VideoScheduler::Stop() {
155 DCHECK(network_task_runner_->BelongsToCurrentThread()); 91 DCHECK(network_task_runner_->BelongsToCurrentThread());
156 92
157 // Clear stubs to prevent further updates reaching the client. 93 // Clear stubs to prevent further updates reaching the client.
158 cursor_stub_ = nullptr; 94 cursor_stub_ = nullptr;
159 video_stub_ = nullptr; 95 video_stub_ = nullptr;
160 96
161 keep_alive_timer_.reset(); 97 keep_alive_timer_.reset();
162 98
163 capture_task_runner_->PostTask( 99 capture_task_runner_->PostTask(
164 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this)); 100 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this));
165 } 101 }
166 102
167 void VideoScheduler::Pause(bool pause) { 103 void VideoScheduler::Pause(bool pause) {
168 if (!capture_task_runner_->BelongsToCurrentThread()) { 104 if (!capture_task_runner_->BelongsToCurrentThread()) {
169 DCHECK(network_task_runner_->BelongsToCurrentThread()); 105 DCHECK(network_task_runner_->BelongsToCurrentThread());
170 capture_task_runner_->PostTask( 106 capture_task_runner_->PostTask(
171 FROM_HERE, base::Bind(&VideoScheduler::Pause, this, pause)); 107 FROM_HERE, base::Bind(&VideoScheduler::Pause, this, pause));
172 return; 108 return;
173 } 109 }
174 110
175 if (is_paused_ != pause) { 111 if (capture_scheduler_)
176 is_paused_ = pause; 112 capture_scheduler_->Pause(pause);
177
178 // Restart captures if we're resuming and there are none scheduled.
179 if (!is_paused_ && capture_timer_ && !capture_timer_->IsRunning())
180 CaptureNextFrame();
181 }
182 } 113 }
183 114
184 void VideoScheduler::SetLatestEventTimestamp(int64 latest_event_timestamp) { 115 void VideoScheduler::SetLatestEventTimestamp(int64 latest_event_timestamp) {
185 if (!capture_task_runner_->BelongsToCurrentThread()) { 116 if (!capture_task_runner_->BelongsToCurrentThread()) {
186 DCHECK(network_task_runner_->BelongsToCurrentThread()); 117 DCHECK(network_task_runner_->BelongsToCurrentThread());
187 capture_task_runner_->PostTask( 118 capture_task_runner_->PostTask(
188 FROM_HERE, base::Bind(&VideoScheduler::SetLatestEventTimestamp, 119 FROM_HERE, base::Bind(&VideoScheduler::SetLatestEventTimestamp,
189 this, latest_event_timestamp)); 120 this, latest_event_timestamp));
190 return; 121 return;
191 } 122 }
(...skipping 29 matching lines...) Expand all
221 152
222 VideoScheduler::~VideoScheduler() { 153 VideoScheduler::~VideoScheduler() {
223 // Destroy the capturer and encoder on their respective threads. 154 // Destroy the capturer and encoder on their respective threads.
224 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release()); 155 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release());
225 capture_task_runner_->DeleteSoon(FROM_HERE, mouse_cursor_monitor_.release()); 156 capture_task_runner_->DeleteSoon(FROM_HERE, mouse_cursor_monitor_.release());
226 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release()); 157 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release());
227 } 158 }
228 159
229 // Capturer thread ------------------------------------------------------------- 160 // Capturer thread -------------------------------------------------------------
230 161
162 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
163 DCHECK(capture_task_runner_->BelongsToCurrentThread());
164
165 // Even when |frame| is nullptr we still need to post it to the encode thread
166 // to make sure frames are freed in the same order they are received and
167 // that we don't start capturing frame n+2 before frame n is freed.
168 encode_task_runner_->PostTask(
169 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this,
170 base::Passed(make_scoped_ptr(frame)),
171 latest_event_timestamp_, base::TimeTicks::Now()));
172
173 capture_scheduler_->OnCaptureCompleted();
174 }
175
176 void VideoScheduler::OnMouseCursor(webrtc::MouseCursor* cursor) {
177 DCHECK(capture_task_runner_->BelongsToCurrentThread());
178
179 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor);
180
181 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
182 new protocol::CursorShapeInfo());
183 cursor_proto->set_width(cursor->image()->size().width());
184 cursor_proto->set_height(cursor->image()->size().height());
185 cursor_proto->set_hotspot_x(cursor->hotspot().x());
186 cursor_proto->set_hotspot_y(cursor->hotspot().y());
187
188 cursor_proto->set_data(std::string());
189 uint8_t* current_row = cursor->image()->data();
190 for (int y = 0; y < cursor->image()->size().height(); ++y) {
191 cursor_proto->mutable_data()->append(
192 current_row,
193 current_row + cursor->image()->size().width() *
194 webrtc::DesktopFrame::kBytesPerPixel);
195 current_row += cursor->image()->stride();
196 }
197
198 network_task_runner_->PostTask(
199 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
200 base::Passed(&cursor_proto)));
201 }
202
203 void VideoScheduler::OnMouseCursorPosition(
204 webrtc::MouseCursorMonitor::CursorState state,
205 const webrtc::DesktopVector& position) {
206 // We're not subscribing to mouse position changes.
207 NOTREACHED();
208 }
209
231 void VideoScheduler::StartOnCaptureThread() { 210 void VideoScheduler::StartOnCaptureThread() {
232 DCHECK(capture_task_runner_->BelongsToCurrentThread()); 211 DCHECK(capture_task_runner_->BelongsToCurrentThread());
233 DCHECK(!capture_timer_); 212 DCHECK(!capture_scheduler_);
234 213
235 // Start mouse cursor monitor.
236 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); 214 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY);
237
238 // Start the capturer.
239 capturer_->Start(this); 215 capturer_->Start(this);
240
241 capture_timer_.reset(new base::OneShotTimer<VideoScheduler>());
242 keep_alive_timer_.reset(new base::DelayTimer<VideoScheduler>(
243 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs),
244 this, &VideoScheduler::SendKeepAlivePacket));
245
246 // Capture first frame immediately.
247 CaptureNextFrame();
248 } 216 }
249 217
250 void VideoScheduler::StopOnCaptureThread() { 218 void VideoScheduler::StopOnCaptureThread() {
251 DCHECK(capture_task_runner_->BelongsToCurrentThread()); 219 DCHECK(capture_task_runner_->BelongsToCurrentThread());
252 220
253 // This doesn't deleted already captured frames, so encoder can keep using the 221 // This doesn't deleted already captured frames, so encoder can keep using the
254 // frames that were captured previously. 222 // frames that were captured previously.
255 capturer_.reset(); 223 capturer_.reset();
256 224
257 // |capture_timer_| must be destroyed on the thread on which it is used. 225 mouse_cursor_monitor_.reset();
258 capture_timer_.reset(); 226 capture_scheduler_.reset();
259 }
260
261 void VideoScheduler::ScheduleNextCapture() {
262 DCHECK(capture_task_runner_->BelongsToCurrentThread());
263
264 capture_timer_->Start(FROM_HERE,
265 scheduler_.NextCaptureDelay(),
266 this,
267 &VideoScheduler::CaptureNextFrame);
268 } 227 }
269 228
270 void VideoScheduler::CaptureNextFrame() { 229 void VideoScheduler::CaptureNextFrame() {
271 DCHECK(capture_task_runner_->BelongsToCurrentThread()); 230 DCHECK(capture_task_runner_->BelongsToCurrentThread());
272 231
273 // If we are stopping (|capturer_| is nullptr), or paused, then don't capture. 232 // Capture mouse shape first and then screen content.
274 if (!capturer_ || is_paused_)
275 return;
276
277 // 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
279 // end of an encode operation.
280 if (pending_frames_ >= kMaxPendingFrames || capture_pending_) {
281 did_skip_frame_ = true;
282 return;
283 }
284
285 did_skip_frame_ = false;
286
287 // At this point we are going to perform one capture so save the current time.
288 pending_frames_++;
289 DCHECK_LE(pending_frames_, kMaxPendingFrames);
290
291 // Before doing a capture schedule for the next one.
292 ScheduleNextCapture();
293
294 capture_pending_ = true;
295
296 // Capture the mouse shape.
297 mouse_cursor_monitor_->Capture(); 233 mouse_cursor_monitor_->Capture();
298
299 // And finally perform one capture.
300 capturer_->Capture(webrtc::DesktopRegion()); 234 capturer_->Capture(webrtc::DesktopRegion());
301 } 235 }
302 236
303 void VideoScheduler::FrameCaptureCompleted() { 237 void VideoScheduler::ProcessFrameEncodedOnCaptureThread(
238 base::TimeDelta encode_time) {
304 DCHECK(capture_task_runner_->BelongsToCurrentThread()); 239 DCHECK(capture_task_runner_->BelongsToCurrentThread());
305 240
306 // Decrement the pending capture count. 241 capture_scheduler_->OnFrameEncoded(encode_time);
307 pending_frames_--; 242 }
308 DCHECK_GE(pending_frames_, 0);
309 243
310 // If we've skipped a frame capture because too we had too many captures 244 void VideoScheduler::ProcessFrameSentOnCaptureThread() {
311 // pending then schedule one now. 245 DCHECK(capture_task_runner_->BelongsToCurrentThread());
312 if (did_skip_frame_) 246
313 CaptureNextFrame(); 247 capture_scheduler_->OnFrameSent();
314 } 248 }
315 249
316 // Network thread -------------------------------------------------------------- 250 // Network thread --------------------------------------------------------------
317 251
318 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) { 252 void VideoScheduler::SendVideoPacket(scoped_ptr<VideoPacket> packet) {
319 DCHECK(network_task_runner_->BelongsToCurrentThread()); 253 DCHECK(network_task_runner_->BelongsToCurrentThread());
320 254
321 if (!video_stub_) 255 if (!video_stub_)
322 return; 256 return;
323 257
324 video_stub_->ProcessVideoPacket( 258 video_stub_->ProcessVideoPacket(
325 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this)); 259 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this));
326 } 260 }
327 261
328 void VideoScheduler::OnVideoPacketSent() { 262 void VideoScheduler::OnVideoPacketSent() {
329 DCHECK(network_task_runner_->BelongsToCurrentThread()); 263 DCHECK(network_task_runner_->BelongsToCurrentThread());
330 264
331 if (!video_stub_) 265 if (!video_stub_)
332 return; 266 return;
333 267
334 keep_alive_timer_->Reset(); 268 keep_alive_timer_->Reset();
335 269
336 capture_task_runner_->PostTask( 270 capture_task_runner_->PostTask(
337 FROM_HERE, base::Bind(&VideoScheduler::FrameCaptureCompleted, this)); 271 FROM_HERE,
272 base::Bind(&VideoScheduler::ProcessFrameSentOnCaptureThread, this));
338 } 273 }
339 274
340 void VideoScheduler::SendKeepAlivePacket() { 275 void VideoScheduler::SendKeepAlivePacket() {
341 DCHECK(network_task_runner_->BelongsToCurrentThread()); 276 DCHECK(network_task_runner_->BelongsToCurrentThread());
342 277
343 if (!video_stub_) 278 if (!video_stub_)
344 return; 279 return;
345 280
346 video_stub_->ProcessVideoPacket( 281 video_stub_->ProcessVideoPacket(
347 make_scoped_ptr(new VideoPacket()), 282 make_scoped_ptr(new VideoPacket()),
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 325
391 if (g_enable_timestamps) { 326 if (g_enable_timestamps) {
392 packet->set_timestamp(timestamp.ToInternalValue()); 327 packet->set_timestamp(timestamp.ToInternalValue());
393 } 328 }
394 329
395 // Destroy the frame before sending |packet| because SendVideoPacket() may 330 // Destroy the frame before sending |packet| because SendVideoPacket() may
396 // trigger another frame to be captured, and the screen capturer expects the 331 // trigger another frame to be captured, and the screen capturer expects the
397 // old frame to be freed by then. 332 // old frame to be freed by then.
398 frame.reset(); 333 frame.reset();
399 334
400 scheduler_.RecordEncodeTime( 335 capture_task_runner_->PostTask(
401 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); 336 FROM_HERE,
337 base::Bind(&VideoScheduler::ProcessFrameEncodedOnCaptureThread, this,
338 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())));
402 network_task_runner_->PostTask( 339 network_task_runner_->PostTask(
403 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this, 340 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this,
404 base::Passed(&packet))); 341 base::Passed(&packet)));
405 } 342 }
406 343
407 } // namespace remoting 344 } // namespace remoting
OLDNEW
« remoting/host/capture_scheduler.h ('K') | « remoting/host/video_scheduler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698