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

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

Issue 893353002: Rename VideoScheduler->VideoFramePipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@scheduler_cleanup
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
« no previous file with comments | « remoting/host/video_scheduler.h ('k') | remoting/host/video_scheduler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/task_runner_util.h"
15 #include "base/time/time.h"
16 #include "remoting/host/capture_scheduler.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/video_stub.h"
22 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
23 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
24 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
25
26 namespace remoting {
27
28 namespace {
29
30 // Helper used to encode frames on the encode thread.
31 //
32 // TODO(sergeyu): This functions doesn't do much beside calling
33 // VideoEncoder::Encode(). It's only needed to handle empty frames properly and
34 // that logic can be moved to VideoEncoder implementations.
35 scoped_ptr<VideoPacket> EncodeFrame(VideoEncoder* encoder,
36 scoped_ptr<webrtc::DesktopFrame> frame) {
37 // If there is nothing to encode then send an empty packet.
38 if (!frame || frame->updated_region().is_empty())
39 return make_scoped_ptr(new VideoPacket());
40
41 return encoder->Encode(*frame);
42 }
43
44 } // namespace
45
46 // Interval between empty keep-alive frames. These frames are sent only when the
47 // stream is paused or inactive for some other reason (e.g. when blocked on
48 // capturer). To prevent PseudoTCP from resetting congestion window this value
49 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms.
50 static const int kKeepAlivePacketIntervalMs = 200;
51
52 static bool g_enable_timestamps = false;
53
54 // static
55 void VideoScheduler::EnableTimestampsForTests() {
56 g_enable_timestamps = true;
57 }
58
59 VideoScheduler::VideoScheduler(
60 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner,
61 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner,
62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
63 scoped_ptr<webrtc::DesktopCapturer> capturer,
64 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor,
65 scoped_ptr<VideoEncoder> encoder,
66 protocol::CursorShapeStub* cursor_stub,
67 protocol::VideoStub* video_stub)
68 : capture_task_runner_(capture_task_runner),
69 encode_task_runner_(encode_task_runner),
70 network_task_runner_(network_task_runner),
71 capturer_(capturer.Pass()),
72 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()),
73 encoder_(encoder.Pass()),
74 cursor_stub_(cursor_stub),
75 video_stub_(video_stub),
76 latest_event_timestamp_(0) {
77 DCHECK(network_task_runner_->BelongsToCurrentThread());
78 DCHECK(capturer_);
79 DCHECK(mouse_cursor_monitor_);
80 DCHECK(encoder_);
81 DCHECK(cursor_stub_);
82 DCHECK(video_stub_);
83 }
84
85 // Public methods --------------------------------------------------------------
86
87 void VideoScheduler::Start() {
88 DCHECK(network_task_runner_->BelongsToCurrentThread());
89
90 keep_alive_timer_.reset(new base::DelayTimer<VideoScheduler>(
91 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs),
92 this, &VideoScheduler::SendKeepAlivePacket));
93
94 capture_scheduler_.reset(new CaptureScheduler(
95 base::Bind(&VideoScheduler::CaptureNextFrame, this)));
96 capture_scheduler_->Start();
97
98 capture_task_runner_->PostTask(
99 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this));
100 }
101
102 void VideoScheduler::Stop() {
103 DCHECK(network_task_runner_->BelongsToCurrentThread());
104
105 // Clear stubs to prevent further updates reaching the client.
106 cursor_stub_ = nullptr;
107 video_stub_ = nullptr;
108
109 capture_scheduler_.reset();
110 keep_alive_timer_.reset();
111
112 capture_task_runner_->PostTask(
113 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this));
114 }
115
116 void VideoScheduler::Pause(bool pause) {
117 DCHECK(network_task_runner_->BelongsToCurrentThread());
118
119 capture_scheduler_->Pause(pause);
120 }
121
122 void VideoScheduler::SetLatestEventTimestamp(int64 latest_event_timestamp) {
123 DCHECK(network_task_runner_->BelongsToCurrentThread());
124
125 latest_event_timestamp_ = latest_event_timestamp;
126 }
127
128 void VideoScheduler::SetLosslessEncode(bool want_lossless) {
129 DCHECK(network_task_runner_->BelongsToCurrentThread());
130
131 encode_task_runner_->PostTask(
132 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessEncode,
133 base::Unretained(encoder_.get()), want_lossless));
134 }
135
136 void VideoScheduler::SetLosslessColor(bool want_lossless) {
137 DCHECK(network_task_runner_->BelongsToCurrentThread());
138
139 encode_task_runner_->PostTask(
140 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessColor,
141 base::Unretained(encoder_.get()), want_lossless));
142 }
143
144 // Private methods -----------------------------------------------------------
145
146 VideoScheduler::~VideoScheduler() {
147 // Destroy the capturer and encoder on their respective threads.
148 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release());
149 capture_task_runner_->DeleteSoon(FROM_HERE, mouse_cursor_monitor_.release());
150 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release());
151 }
152
153 // Capturer thread -------------------------------------------------------------
154
155 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) {
156 return nullptr;
157 }
158
159 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) {
160 DCHECK(capture_task_runner_->BelongsToCurrentThread());
161
162 network_task_runner_->PostTask(
163 FROM_HERE, base::Bind(&VideoScheduler::EncodeAndSendFrame, this,
164 base::Passed(make_scoped_ptr(frame))));
165 }
166
167 void VideoScheduler::OnMouseCursor(webrtc::MouseCursor* cursor) {
168 DCHECK(capture_task_runner_->BelongsToCurrentThread());
169
170 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor);
171
172 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
173 new protocol::CursorShapeInfo());
174 cursor_proto->set_width(cursor->image()->size().width());
175 cursor_proto->set_height(cursor->image()->size().height());
176 cursor_proto->set_hotspot_x(cursor->hotspot().x());
177 cursor_proto->set_hotspot_y(cursor->hotspot().y());
178
179 cursor_proto->set_data(std::string());
180 uint8_t* current_row = cursor->image()->data();
181 for (int y = 0; y < cursor->image()->size().height(); ++y) {
182 cursor_proto->mutable_data()->append(
183 current_row,
184 current_row + cursor->image()->size().width() *
185 webrtc::DesktopFrame::kBytesPerPixel);
186 current_row += cursor->image()->stride();
187 }
188
189 network_task_runner_->PostTask(
190 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this,
191 base::Passed(&cursor_proto)));
192 }
193
194 void VideoScheduler::OnMouseCursorPosition(
195 webrtc::MouseCursorMonitor::CursorState state,
196 const webrtc::DesktopVector& position) {
197 // We're not subscribing to mouse position changes.
198 NOTREACHED();
199 }
200
201 void VideoScheduler::StartOnCaptureThread() {
202 DCHECK(capture_task_runner_->BelongsToCurrentThread());
203
204 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY);
205 capturer_->Start(this);
206 }
207
208 void VideoScheduler::StopOnCaptureThread() {
209 DCHECK(capture_task_runner_->BelongsToCurrentThread());
210
211 // This doesn't deleted already captured frames, so encoder can keep using the
212 // frames that were captured previously.
213 capturer_.reset();
214
215 mouse_cursor_monitor_.reset();
216 }
217
218 void VideoScheduler::CaptureNextFrameOnCaptureThread() {
219 DCHECK(capture_task_runner_->BelongsToCurrentThread());
220
221 // Capture mouse shape first and then screen content.
222 mouse_cursor_monitor_->Capture();
223 capturer_->Capture(webrtc::DesktopRegion());
224 }
225
226 // Network thread --------------------------------------------------------------
227
228 void VideoScheduler::CaptureNextFrame() {
229 DCHECK(network_task_runner_->BelongsToCurrentThread());
230
231 capture_task_runner_->PostTask(
232 FROM_HERE,
233 base::Bind(&VideoScheduler::CaptureNextFrameOnCaptureThread, this));
234 }
235
236 void VideoScheduler::EncodeAndSendFrame(
237 scoped_ptr<webrtc::DesktopFrame> frame) {
238 DCHECK(network_task_runner_->BelongsToCurrentThread());
239
240 if (!video_stub_)
241 return;
242
243 capture_scheduler_->OnCaptureCompleted();
244
245 // Even when |frame| is nullptr we still need to post it to the encode thread
246 // to make sure frames are freed in the same order they are received and
247 // that we don't start capturing frame n+2 before frame n is freed.
248 base::PostTaskAndReplyWithResult(
249 encode_task_runner_.get(), FROM_HERE,
250 base::Bind(&EncodeFrame, encoder_.get(), base::Passed(&frame)),
251 base::Bind(&VideoScheduler::SendEncodedFrame, this,
252 latest_event_timestamp_, base::TimeTicks::Now()));
253 }
254
255 void VideoScheduler::SendEncodedFrame(int64 latest_event_timestamp,
256 base::TimeTicks timestamp,
257 scoped_ptr<VideoPacket> packet) {
258 DCHECK(network_task_runner_->BelongsToCurrentThread());
259
260 if (!video_stub_)
261 return;
262
263 if (g_enable_timestamps)
264 packet->set_timestamp(timestamp.ToInternalValue());
265
266 packet->set_latest_event_timestamp(latest_event_timestamp);
267
268 capture_scheduler_->OnFrameEncoded(
269 base::TimeDelta::FromMilliseconds(packet->encode_time_ms()));
270
271 video_stub_->ProcessVideoPacket(
272 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this));
273 }
274
275 void VideoScheduler::OnVideoPacketSent() {
276 DCHECK(network_task_runner_->BelongsToCurrentThread());
277
278 if (!video_stub_)
279 return;
280
281 capture_scheduler_->OnFrameSent();
282 keep_alive_timer_->Reset();
283 }
284
285 void VideoScheduler::SendKeepAlivePacket() {
286 DCHECK(network_task_runner_->BelongsToCurrentThread());
287
288 video_stub_->ProcessVideoPacket(
289 make_scoped_ptr(new VideoPacket()),
290 base::Bind(&VideoScheduler::OnKeepAlivePacketSent, this));
291 }
292
293 void VideoScheduler::OnKeepAlivePacketSent() {
294 DCHECK(network_task_runner_->BelongsToCurrentThread());
295
296 if (keep_alive_timer_)
297 keep_alive_timer_->Reset();
298 }
299
300 void VideoScheduler::SendCursorShape(
301 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) {
302 DCHECK(network_task_runner_->BelongsToCurrentThread());
303
304 if (!cursor_stub_)
305 return;
306
307 cursor_stub_->SetCursorShape(*cursor_shape);
308 }
309
310 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/video_scheduler.h ('k') | remoting/host/video_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698