OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_frame_pump.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/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
(...skipping 29 matching lines...) Expand all Loading... |
45 | 45 |
46 // Interval between empty keep-alive frames. These frames are sent only when the | 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 | 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 | 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. | 49 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. |
50 static const int kKeepAlivePacketIntervalMs = 200; | 50 static const int kKeepAlivePacketIntervalMs = 200; |
51 | 51 |
52 static bool g_enable_timestamps = false; | 52 static bool g_enable_timestamps = false; |
53 | 53 |
54 // static | 54 // static |
55 void VideoScheduler::EnableTimestampsForTests() { | 55 void VideoFramePump::EnableTimestampsForTests() { |
56 g_enable_timestamps = true; | 56 g_enable_timestamps = true; |
57 } | 57 } |
58 | 58 |
59 VideoScheduler::VideoScheduler( | 59 VideoFramePump::VideoFramePump( |
60 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | 60 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
61 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 61 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | 62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
63 scoped_ptr<webrtc::DesktopCapturer> capturer, | 63 scoped_ptr<webrtc::DesktopCapturer> capturer, |
64 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | 64 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, |
65 scoped_ptr<VideoEncoder> encoder, | 65 scoped_ptr<VideoEncoder> encoder, |
66 protocol::CursorShapeStub* cursor_stub, | 66 protocol::CursorShapeStub* cursor_stub, |
67 protocol::VideoStub* video_stub) | 67 protocol::VideoStub* video_stub) |
68 : capture_task_runner_(capture_task_runner), | 68 : capture_task_runner_(capture_task_runner), |
69 encode_task_runner_(encode_task_runner), | 69 encode_task_runner_(encode_task_runner), |
70 network_task_runner_(network_task_runner), | 70 network_task_runner_(network_task_runner), |
71 capturer_(capturer.Pass()), | 71 capturer_(capturer.Pass()), |
72 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), | 72 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), |
73 encoder_(encoder.Pass()), | 73 encoder_(encoder.Pass()), |
74 cursor_stub_(cursor_stub), | 74 cursor_stub_(cursor_stub), |
75 video_stub_(video_stub), | 75 video_stub_(video_stub), |
76 latest_event_timestamp_(0) { | 76 latest_event_timestamp_(0) { |
77 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 77 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
78 DCHECK(capturer_); | 78 DCHECK(capturer_); |
79 DCHECK(mouse_cursor_monitor_); | 79 DCHECK(mouse_cursor_monitor_); |
80 DCHECK(encoder_); | 80 DCHECK(encoder_); |
81 DCHECK(cursor_stub_); | 81 DCHECK(cursor_stub_); |
82 DCHECK(video_stub_); | 82 DCHECK(video_stub_); |
83 } | 83 } |
84 | 84 |
85 // Public methods -------------------------------------------------------------- | 85 // Public methods -------------------------------------------------------------- |
86 | 86 |
87 void VideoScheduler::Start() { | 87 void VideoFramePump::Start() { |
88 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 88 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
89 | 89 |
90 keep_alive_timer_.reset(new base::DelayTimer<VideoScheduler>( | 90 keep_alive_timer_.reset(new base::DelayTimer<VideoFramePump>( |
91 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), | 91 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), |
92 this, &VideoScheduler::SendKeepAlivePacket)); | 92 this, &VideoFramePump::SendKeepAlivePacket)); |
93 | 93 |
94 capture_scheduler_.reset(new CaptureScheduler( | 94 capture_scheduler_.reset(new CaptureScheduler( |
95 base::Bind(&VideoScheduler::CaptureNextFrame, this))); | 95 base::Bind(&VideoFramePump::CaptureNextFrame, this))); |
96 capture_scheduler_->Start(); | 96 capture_scheduler_->Start(); |
97 | 97 |
98 capture_task_runner_->PostTask( | 98 capture_task_runner_->PostTask( |
99 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); | 99 FROM_HERE, base::Bind(&VideoFramePump::StartOnCaptureThread, this)); |
100 } | 100 } |
101 | 101 |
102 void VideoScheduler::Stop() { | 102 void VideoFramePump::Stop() { |
103 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 103 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
104 | 104 |
105 // Clear stubs to prevent further updates reaching the client. | 105 // Clear stubs to prevent further updates reaching the client. |
106 cursor_stub_ = nullptr; | 106 cursor_stub_ = nullptr; |
107 video_stub_ = nullptr; | 107 video_stub_ = nullptr; |
108 | 108 |
109 capture_scheduler_.reset(); | 109 capture_scheduler_.reset(); |
110 keep_alive_timer_.reset(); | 110 keep_alive_timer_.reset(); |
111 | 111 |
112 capture_task_runner_->PostTask( | 112 capture_task_runner_->PostTask( |
113 FROM_HERE, base::Bind(&VideoScheduler::StopOnCaptureThread, this)); | 113 FROM_HERE, base::Bind(&VideoFramePump::StopOnCaptureThread, this)); |
114 } | 114 } |
115 | 115 |
116 void VideoScheduler::Pause(bool pause) { | 116 void VideoFramePump::Pause(bool pause) { |
117 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 117 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
118 | 118 |
119 capture_scheduler_->Pause(pause); | 119 capture_scheduler_->Pause(pause); |
120 } | 120 } |
121 | 121 |
122 void VideoScheduler::SetLatestEventTimestamp(int64 latest_event_timestamp) { | 122 void VideoFramePump::SetLatestEventTimestamp(int64 latest_event_timestamp) { |
123 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 123 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
124 | 124 |
125 latest_event_timestamp_ = latest_event_timestamp; | 125 latest_event_timestamp_ = latest_event_timestamp; |
126 } | 126 } |
127 | 127 |
128 void VideoScheduler::SetLosslessEncode(bool want_lossless) { | 128 void VideoFramePump::SetLosslessEncode(bool want_lossless) { |
129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 129 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
130 | 130 |
131 encode_task_runner_->PostTask( | 131 encode_task_runner_->PostTask( |
132 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessEncode, | 132 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessEncode, |
133 base::Unretained(encoder_.get()), want_lossless)); | 133 base::Unretained(encoder_.get()), want_lossless)); |
134 } | 134 } |
135 | 135 |
136 void VideoScheduler::SetLosslessColor(bool want_lossless) { | 136 void VideoFramePump::SetLosslessColor(bool want_lossless) { |
137 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 137 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
138 | 138 |
139 encode_task_runner_->PostTask( | 139 encode_task_runner_->PostTask( |
140 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessColor, | 140 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessColor, |
141 base::Unretained(encoder_.get()), want_lossless)); | 141 base::Unretained(encoder_.get()), want_lossless)); |
142 } | 142 } |
143 | 143 |
144 // Private methods ----------------------------------------------------------- | 144 // Private methods ----------------------------------------------------------- |
145 | 145 |
146 VideoScheduler::~VideoScheduler() { | 146 VideoFramePump::~VideoFramePump() { |
147 // Destroy the capturer and encoder on their respective threads. | 147 // Destroy the capturer and encoder on their respective threads. |
148 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release()); | 148 capture_task_runner_->DeleteSoon(FROM_HERE, capturer_.release()); |
149 capture_task_runner_->DeleteSoon(FROM_HERE, mouse_cursor_monitor_.release()); | 149 capture_task_runner_->DeleteSoon(FROM_HERE, mouse_cursor_monitor_.release()); |
150 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release()); | 150 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release()); |
151 } | 151 } |
152 | 152 |
153 // Capturer thread ------------------------------------------------------------- | 153 // Capturer thread ------------------------------------------------------------- |
154 | 154 |
155 webrtc::SharedMemory* VideoScheduler::CreateSharedMemory(size_t size) { | 155 webrtc::SharedMemory* VideoFramePump::CreateSharedMemory(size_t size) { |
156 return nullptr; | 156 return nullptr; |
157 } | 157 } |
158 | 158 |
159 void VideoScheduler::OnCaptureCompleted(webrtc::DesktopFrame* frame) { | 159 void VideoFramePump::OnCaptureCompleted(webrtc::DesktopFrame* frame) { |
160 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 160 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
161 | 161 |
162 network_task_runner_->PostTask( | 162 network_task_runner_->PostTask( |
163 FROM_HERE, base::Bind(&VideoScheduler::EncodeAndSendFrame, this, | 163 FROM_HERE, base::Bind(&VideoFramePump::EncodeAndSendFrame, this, |
164 base::Passed(make_scoped_ptr(frame)))); | 164 base::Passed(make_scoped_ptr(frame)))); |
165 } | 165 } |
166 | 166 |
167 void VideoScheduler::OnMouseCursor(webrtc::MouseCursor* cursor) { | 167 void VideoFramePump::OnMouseCursor(webrtc::MouseCursor* cursor) { |
168 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 168 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
169 | 169 |
170 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor); | 170 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor); |
171 | 171 |
172 scoped_ptr<protocol::CursorShapeInfo> cursor_proto( | 172 scoped_ptr<protocol::CursorShapeInfo> cursor_proto( |
173 new protocol::CursorShapeInfo()); | 173 new protocol::CursorShapeInfo()); |
174 cursor_proto->set_width(cursor->image()->size().width()); | 174 cursor_proto->set_width(cursor->image()->size().width()); |
175 cursor_proto->set_height(cursor->image()->size().height()); | 175 cursor_proto->set_height(cursor->image()->size().height()); |
176 cursor_proto->set_hotspot_x(cursor->hotspot().x()); | 176 cursor_proto->set_hotspot_x(cursor->hotspot().x()); |
177 cursor_proto->set_hotspot_y(cursor->hotspot().y()); | 177 cursor_proto->set_hotspot_y(cursor->hotspot().y()); |
178 | 178 |
179 cursor_proto->set_data(std::string()); | 179 cursor_proto->set_data(std::string()); |
180 uint8_t* current_row = cursor->image()->data(); | 180 uint8_t* current_row = cursor->image()->data(); |
181 for (int y = 0; y < cursor->image()->size().height(); ++y) { | 181 for (int y = 0; y < cursor->image()->size().height(); ++y) { |
182 cursor_proto->mutable_data()->append( | 182 cursor_proto->mutable_data()->append( |
183 current_row, | 183 current_row, |
184 current_row + cursor->image()->size().width() * | 184 current_row + cursor->image()->size().width() * |
185 webrtc::DesktopFrame::kBytesPerPixel); | 185 webrtc::DesktopFrame::kBytesPerPixel); |
186 current_row += cursor->image()->stride(); | 186 current_row += cursor->image()->stride(); |
187 } | 187 } |
188 | 188 |
189 network_task_runner_->PostTask( | 189 network_task_runner_->PostTask( |
190 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this, | 190 FROM_HERE, base::Bind(&VideoFramePump::SendCursorShape, this, |
191 base::Passed(&cursor_proto))); | 191 base::Passed(&cursor_proto))); |
192 } | 192 } |
193 | 193 |
194 void VideoScheduler::OnMouseCursorPosition( | 194 void VideoFramePump::OnMouseCursorPosition( |
195 webrtc::MouseCursorMonitor::CursorState state, | 195 webrtc::MouseCursorMonitor::CursorState state, |
196 const webrtc::DesktopVector& position) { | 196 const webrtc::DesktopVector& position) { |
197 // We're not subscribing to mouse position changes. | 197 // We're not subscribing to mouse position changes. |
198 NOTREACHED(); | 198 NOTREACHED(); |
199 } | 199 } |
200 | 200 |
201 void VideoScheduler::StartOnCaptureThread() { | 201 void VideoFramePump::StartOnCaptureThread() { |
202 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 202 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
203 | 203 |
204 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); | 204 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); |
205 capturer_->Start(this); | 205 capturer_->Start(this); |
206 } | 206 } |
207 | 207 |
208 void VideoScheduler::StopOnCaptureThread() { | 208 void VideoFramePump::StopOnCaptureThread() { |
209 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 209 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
210 | 210 |
211 // This doesn't deleted already captured frames, so encoder can keep using the | 211 // This doesn't deleted already captured frames, so encoder can keep using the |
212 // frames that were captured previously. | 212 // frames that were captured previously. |
213 capturer_.reset(); | 213 capturer_.reset(); |
214 | 214 |
215 mouse_cursor_monitor_.reset(); | 215 mouse_cursor_monitor_.reset(); |
216 } | 216 } |
217 | 217 |
218 void VideoScheduler::CaptureNextFrameOnCaptureThread() { | 218 void VideoFramePump::CaptureNextFrameOnCaptureThread() { |
219 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 219 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
220 | 220 |
221 // Capture mouse shape first and then screen content. | 221 // Capture mouse shape first and then screen content. |
222 mouse_cursor_monitor_->Capture(); | 222 mouse_cursor_monitor_->Capture(); |
223 capturer_->Capture(webrtc::DesktopRegion()); | 223 capturer_->Capture(webrtc::DesktopRegion()); |
224 } | 224 } |
225 | 225 |
226 // Network thread -------------------------------------------------------------- | 226 // Network thread -------------------------------------------------------------- |
227 | 227 |
228 void VideoScheduler::CaptureNextFrame() { | 228 void VideoFramePump::CaptureNextFrame() { |
229 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 229 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
230 | 230 |
231 capture_task_runner_->PostTask( | 231 capture_task_runner_->PostTask( |
232 FROM_HERE, | 232 FROM_HERE, |
233 base::Bind(&VideoScheduler::CaptureNextFrameOnCaptureThread, this)); | 233 base::Bind(&VideoFramePump::CaptureNextFrameOnCaptureThread, this)); |
234 } | 234 } |
235 | 235 |
236 void VideoScheduler::EncodeAndSendFrame( | 236 void VideoFramePump::EncodeAndSendFrame( |
237 scoped_ptr<webrtc::DesktopFrame> frame) { | 237 scoped_ptr<webrtc::DesktopFrame> frame) { |
238 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 238 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
239 | 239 |
240 if (!video_stub_) | 240 if (!video_stub_) |
241 return; | 241 return; |
242 | 242 |
243 capture_scheduler_->OnCaptureCompleted(); | 243 capture_scheduler_->OnCaptureCompleted(); |
244 | 244 |
245 // Even when |frame| is nullptr we still need to post it to the encode thread | 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 | 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. | 247 // that we don't start capturing frame n+2 before frame n is freed. |
248 base::PostTaskAndReplyWithResult( | 248 base::PostTaskAndReplyWithResult( |
249 encode_task_runner_.get(), FROM_HERE, | 249 encode_task_runner_.get(), FROM_HERE, |
250 base::Bind(&EncodeFrame, encoder_.get(), base::Passed(&frame)), | 250 base::Bind(&EncodeFrame, encoder_.get(), base::Passed(&frame)), |
251 base::Bind(&VideoScheduler::SendEncodedFrame, this, | 251 base::Bind(&VideoFramePump::SendEncodedFrame, this, |
252 latest_event_timestamp_, base::TimeTicks::Now())); | 252 latest_event_timestamp_, base::TimeTicks::Now())); |
253 } | 253 } |
254 | 254 |
255 void VideoScheduler::SendEncodedFrame(int64 latest_event_timestamp, | 255 void VideoFramePump::SendEncodedFrame(int64 latest_event_timestamp, |
256 base::TimeTicks timestamp, | 256 base::TimeTicks timestamp, |
257 scoped_ptr<VideoPacket> packet) { | 257 scoped_ptr<VideoPacket> packet) { |
258 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 258 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
259 | 259 |
260 if (!video_stub_) | 260 if (!video_stub_) |
261 return; | 261 return; |
262 | 262 |
263 if (g_enable_timestamps) | 263 if (g_enable_timestamps) |
264 packet->set_timestamp(timestamp.ToInternalValue()); | 264 packet->set_timestamp(timestamp.ToInternalValue()); |
265 | 265 |
266 packet->set_latest_event_timestamp(latest_event_timestamp); | 266 packet->set_latest_event_timestamp(latest_event_timestamp); |
267 | 267 |
268 capture_scheduler_->OnFrameEncoded( | 268 capture_scheduler_->OnFrameEncoded( |
269 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); | 269 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); |
270 | 270 |
271 video_stub_->ProcessVideoPacket( | 271 video_stub_->ProcessVideoPacket( |
272 packet.Pass(), base::Bind(&VideoScheduler::OnVideoPacketSent, this)); | 272 packet.Pass(), base::Bind(&VideoFramePump::OnVideoPacketSent, this)); |
273 } | 273 } |
274 | 274 |
275 void VideoScheduler::OnVideoPacketSent() { | 275 void VideoFramePump::OnVideoPacketSent() { |
276 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 276 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
277 | 277 |
278 if (!video_stub_) | 278 if (!video_stub_) |
279 return; | 279 return; |
280 | 280 |
281 capture_scheduler_->OnFrameSent(); | 281 capture_scheduler_->OnFrameSent(); |
282 keep_alive_timer_->Reset(); | 282 keep_alive_timer_->Reset(); |
283 } | 283 } |
284 | 284 |
285 void VideoScheduler::SendKeepAlivePacket() { | 285 void VideoFramePump::SendKeepAlivePacket() { |
286 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 286 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
287 | 287 |
288 video_stub_->ProcessVideoPacket( | 288 video_stub_->ProcessVideoPacket( |
289 make_scoped_ptr(new VideoPacket()), | 289 make_scoped_ptr(new VideoPacket()), |
290 base::Bind(&VideoScheduler::OnKeepAlivePacketSent, this)); | 290 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, this)); |
291 } | 291 } |
292 | 292 |
293 void VideoScheduler::OnKeepAlivePacketSent() { | 293 void VideoFramePump::OnKeepAlivePacketSent() { |
294 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 294 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
295 | 295 |
296 if (keep_alive_timer_) | 296 if (keep_alive_timer_) |
297 keep_alive_timer_->Reset(); | 297 keep_alive_timer_->Reset(); |
298 } | 298 } |
299 | 299 |
300 void VideoScheduler::SendCursorShape( | 300 void VideoFramePump::SendCursorShape( |
301 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) { | 301 scoped_ptr<protocol::CursorShapeInfo> cursor_shape) { |
302 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 302 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
303 | 303 |
304 if (!cursor_stub_) | 304 if (!cursor_stub_) |
305 return; | 305 return; |
306 | 306 |
307 cursor_stub_->SetCursorShape(*cursor_shape); | 307 cursor_stub_->SetCursorShape(*cursor_shape); |
308 } | 308 } |
309 | 309 |
310 } // namespace remoting | 310 } // namespace remoting |
OLD | NEW |