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

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

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

Powered by Google App Engine
This is Rietveld 408576698