OLD | NEW |
1 // Copyright 2015 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_frame_pump.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/single_thread_task_runner.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" |
16 #include "remoting/host/capture_scheduler.h" | |
17 #include "remoting/proto/control.pb.h" | 16 #include "remoting/proto/control.pb.h" |
18 #include "remoting/proto/internal.pb.h" | |
19 #include "remoting/proto/video.pb.h" | 17 #include "remoting/proto/video.pb.h" |
20 #include "remoting/protocol/cursor_shape_stub.h" | |
21 #include "remoting/protocol/video_stub.h" | 18 #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" | 19 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
24 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" | |
25 | 20 |
26 namespace remoting { | 21 namespace remoting { |
27 | 22 |
28 namespace { | 23 namespace { |
29 | 24 |
30 // Helper used to encode frames on the encode thread. | 25 // Helper used to encode frames on the encode thread. |
31 // | 26 // |
32 // TODO(sergeyu): This functions doesn't do much beside calling | 27 // TODO(sergeyu): This functions doesn't do much beside calling |
33 // VideoEncoder::Encode(). It's only needed to handle empty frames properly and | 28 // VideoEncoder::Encode(). It's only needed to handle empty frames properly and |
34 // that logic can be moved to VideoEncoder implementations. | 29 // that logic can be moved to VideoEncoder implementations. |
(...skipping 15 matching lines...) Expand all Loading... |
50 static const int kKeepAlivePacketIntervalMs = 200; | 45 static const int kKeepAlivePacketIntervalMs = 200; |
51 | 46 |
52 static bool g_enable_timestamps = false; | 47 static bool g_enable_timestamps = false; |
53 | 48 |
54 // static | 49 // static |
55 void VideoFramePump::EnableTimestampsForTests() { | 50 void VideoFramePump::EnableTimestampsForTests() { |
56 g_enable_timestamps = true; | 51 g_enable_timestamps = true; |
57 } | 52 } |
58 | 53 |
59 VideoFramePump::VideoFramePump( | 54 VideoFramePump::VideoFramePump( |
60 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | |
61 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 55 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
63 scoped_ptr<webrtc::DesktopCapturer> capturer, | 56 scoped_ptr<webrtc::DesktopCapturer> capturer, |
64 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | |
65 scoped_ptr<VideoEncoder> encoder, | 57 scoped_ptr<VideoEncoder> encoder, |
66 protocol::CursorShapeStub* cursor_stub, | |
67 protocol::VideoStub* video_stub) | 58 protocol::VideoStub* video_stub) |
68 : capture_task_runner_(capture_task_runner), | 59 : encode_task_runner_(encode_task_runner), |
69 encode_task_runner_(encode_task_runner), | |
70 network_task_runner_(network_task_runner), | |
71 capturer_(capturer.Pass()), | 60 capturer_(capturer.Pass()), |
72 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), | |
73 encoder_(encoder.Pass()), | 61 encoder_(encoder.Pass()), |
74 cursor_stub_(cursor_stub), | |
75 video_stub_(video_stub), | 62 video_stub_(video_stub), |
76 latest_event_timestamp_(0) { | 63 keep_alive_timer_(true, true), |
77 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 64 capture_scheduler_(base::Bind(&VideoFramePump::CaptureNextFrame, |
78 DCHECK(capturer_); | 65 base::Unretained(this))), |
79 DCHECK(mouse_cursor_monitor_); | 66 latest_event_timestamp_(0), |
| 67 weak_factory_(this) { |
80 DCHECK(encoder_); | 68 DCHECK(encoder_); |
81 DCHECK(cursor_stub_); | |
82 DCHECK(video_stub_); | 69 DCHECK(video_stub_); |
| 70 |
| 71 capturer_->Start(this); |
| 72 capture_scheduler_.Start(); |
| 73 |
| 74 keep_alive_timer_.Start( |
| 75 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), |
| 76 base::Bind(&VideoFramePump::SendKeepAlivePacket, base::Unretained(this))); |
83 } | 77 } |
84 | 78 |
85 // Public methods -------------------------------------------------------------- | 79 VideoFramePump::~VideoFramePump() { |
86 | 80 encode_task_runner_->DeleteSoon(FROM_HERE, encoder_.release()); |
87 void VideoFramePump::Start() { | |
88 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
89 | |
90 keep_alive_timer_.reset(new base::DelayTimer<VideoFramePump>( | |
91 FROM_HERE, base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), | |
92 this, &VideoFramePump::SendKeepAlivePacket)); | |
93 | |
94 capture_scheduler_.reset(new CaptureScheduler( | |
95 base::Bind(&VideoFramePump::CaptureNextFrame, this))); | |
96 capture_scheduler_->Start(); | |
97 | |
98 capture_task_runner_->PostTask( | |
99 FROM_HERE, base::Bind(&VideoFramePump::StartOnCaptureThread, this)); | |
100 } | |
101 | |
102 void VideoFramePump::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(&VideoFramePump::StopOnCaptureThread, this)); | |
114 } | 81 } |
115 | 82 |
116 void VideoFramePump::Pause(bool pause) { | 83 void VideoFramePump::Pause(bool pause) { |
117 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
118 | 85 |
119 capture_scheduler_->Pause(pause); | 86 capture_scheduler_.Pause(pause); |
120 } | 87 } |
121 | 88 |
122 void VideoFramePump::SetLatestEventTimestamp(int64 latest_event_timestamp) { | 89 void VideoFramePump::SetLatestEventTimestamp(int64 latest_event_timestamp) { |
123 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 90 DCHECK(thread_checker_.CalledOnValidThread()); |
124 | 91 |
125 latest_event_timestamp_ = latest_event_timestamp; | 92 latest_event_timestamp_ = latest_event_timestamp; |
126 } | 93 } |
127 | 94 |
128 void VideoFramePump::SetLosslessEncode(bool want_lossless) { | 95 void VideoFramePump::SetLosslessEncode(bool want_lossless) { |
129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 96 DCHECK(thread_checker_.CalledOnValidThread()); |
130 | 97 |
131 encode_task_runner_->PostTask( | 98 encode_task_runner_->PostTask( |
132 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessEncode, | 99 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessEncode, |
133 base::Unretained(encoder_.get()), want_lossless)); | 100 base::Unretained(encoder_.get()), want_lossless)); |
134 } | 101 } |
135 | 102 |
136 void VideoFramePump::SetLosslessColor(bool want_lossless) { | 103 void VideoFramePump::SetLosslessColor(bool want_lossless) { |
137 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 104 DCHECK(thread_checker_.CalledOnValidThread()); |
138 | 105 |
139 encode_task_runner_->PostTask( | 106 encode_task_runner_->PostTask( |
140 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessColor, | 107 FROM_HERE, base::Bind(&VideoEncoder::SetLosslessColor, |
141 base::Unretained(encoder_.get()), want_lossless)); | 108 base::Unretained(encoder_.get()), want_lossless)); |
142 } | 109 } |
143 | 110 |
144 // Private methods ----------------------------------------------------------- | |
145 | |
146 VideoFramePump::~VideoFramePump() { | |
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* VideoFramePump::CreateSharedMemory(size_t size) { | 111 webrtc::SharedMemory* VideoFramePump::CreateSharedMemory(size_t size) { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); |
156 return nullptr; | 113 return nullptr; |
157 } | 114 } |
158 | 115 |
159 void VideoFramePump::OnCaptureCompleted(webrtc::DesktopFrame* frame) { | 116 void VideoFramePump::OnCaptureCompleted(webrtc::DesktopFrame* frame) { |
160 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
161 | 118 |
162 network_task_runner_->PostTask( | 119 capture_scheduler_.OnCaptureCompleted(); |
163 FROM_HERE, base::Bind(&VideoFramePump::EncodeAndSendFrame, this, | |
164 base::Passed(make_scoped_ptr(frame)))); | |
165 } | |
166 | |
167 void VideoFramePump::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(&VideoFramePump::SendCursorShape, this, | |
191 base::Passed(&cursor_proto))); | |
192 } | |
193 | |
194 void VideoFramePump::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 VideoFramePump::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 VideoFramePump::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 VideoFramePump::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 VideoFramePump::CaptureNextFrame() { | |
229 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
230 | |
231 capture_task_runner_->PostTask( | |
232 FROM_HERE, | |
233 base::Bind(&VideoFramePump::CaptureNextFrameOnCaptureThread, this)); | |
234 } | |
235 | |
236 void VideoFramePump::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 | 120 |
245 // Even when |frame| is nullptr we still need to post it to the encode thread | 121 // 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 | 122 // 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. | 123 // that we don't start capturing frame n+2 before frame n is freed. |
248 base::PostTaskAndReplyWithResult( | 124 base::PostTaskAndReplyWithResult( |
249 encode_task_runner_.get(), FROM_HERE, | 125 encode_task_runner_.get(), FROM_HERE, |
250 base::Bind(&EncodeFrame, encoder_.get(), base::Passed(&frame)), | 126 base::Bind(&EncodeFrame, encoder_.get(), |
251 base::Bind(&VideoFramePump::SendEncodedFrame, this, | 127 base::Passed(make_scoped_ptr(frame))), |
| 128 base::Bind(&VideoFramePump::SendEncodedFrame, weak_factory_.GetWeakPtr(), |
252 latest_event_timestamp_, base::TimeTicks::Now())); | 129 latest_event_timestamp_, base::TimeTicks::Now())); |
253 } | 130 } |
254 | 131 |
| 132 void VideoFramePump::CaptureNextFrame() { |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 134 |
| 135 capturer_->Capture(webrtc::DesktopRegion()); |
| 136 } |
| 137 |
255 void VideoFramePump::SendEncodedFrame(int64 latest_event_timestamp, | 138 void VideoFramePump::SendEncodedFrame(int64 latest_event_timestamp, |
256 base::TimeTicks timestamp, | 139 base::TimeTicks timestamp, |
257 scoped_ptr<VideoPacket> packet) { | 140 scoped_ptr<VideoPacket> packet) { |
258 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 141 DCHECK(thread_checker_.CalledOnValidThread()); |
259 | |
260 if (!video_stub_) | |
261 return; | |
262 | 142 |
263 if (g_enable_timestamps) | 143 if (g_enable_timestamps) |
264 packet->set_timestamp(timestamp.ToInternalValue()); | 144 packet->set_timestamp(timestamp.ToInternalValue()); |
265 | 145 |
266 packet->set_latest_event_timestamp(latest_event_timestamp); | 146 packet->set_latest_event_timestamp(latest_event_timestamp); |
267 | 147 |
268 capture_scheduler_->OnFrameEncoded( | 148 capture_scheduler_.OnFrameEncoded( |
269 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); | 149 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); |
270 | 150 |
271 video_stub_->ProcessVideoPacket( | 151 video_stub_->ProcessVideoPacket(packet.Pass(), |
272 packet.Pass(), base::Bind(&VideoFramePump::OnVideoPacketSent, this)); | 152 base::Bind(&VideoFramePump::OnVideoPacketSent, |
| 153 weak_factory_.GetWeakPtr())); |
273 } | 154 } |
274 | 155 |
275 void VideoFramePump::OnVideoPacketSent() { | 156 void VideoFramePump::OnVideoPacketSent() { |
276 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
277 | 158 |
278 if (!video_stub_) | 159 capture_scheduler_.OnFrameSent(); |
279 return; | 160 keep_alive_timer_.Reset(); |
280 | |
281 capture_scheduler_->OnFrameSent(); | |
282 keep_alive_timer_->Reset(); | |
283 } | 161 } |
284 | 162 |
285 void VideoFramePump::SendKeepAlivePacket() { | 163 void VideoFramePump::SendKeepAlivePacket() { |
286 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 164 DCHECK(thread_checker_.CalledOnValidThread()); |
287 | 165 |
288 video_stub_->ProcessVideoPacket( | 166 video_stub_->ProcessVideoPacket( |
289 make_scoped_ptr(new VideoPacket()), | 167 make_scoped_ptr(new VideoPacket()), |
290 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, this)); | 168 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, |
| 169 weak_factory_.GetWeakPtr())); |
291 } | 170 } |
292 | 171 |
293 void VideoFramePump::OnKeepAlivePacketSent() { | 172 void VideoFramePump::OnKeepAlivePacketSent() { |
294 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
295 | 174 |
296 if (keep_alive_timer_) | 175 keep_alive_timer_.Reset(); |
297 keep_alive_timer_->Reset(); | |
298 } | |
299 | |
300 void VideoFramePump::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 } | 176 } |
309 | 177 |
310 } // namespace remoting | 178 } // namespace remoting |
OLD | NEW |