OLD | NEW |
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/proto/control.pb.h" | 17 #include "remoting/proto/control.pb.h" |
18 #include "remoting/proto/internal.pb.h" | 18 #include "remoting/proto/internal.pb.h" |
19 #include "remoting/proto/video.pb.h" | 19 #include "remoting/proto/video.pb.h" |
20 #include "remoting/protocol/cursor_shape_stub.h" | 20 #include "remoting/protocol/cursor_shape_stub.h" |
21 #include "remoting/protocol/message_decoder.h" | 21 #include "remoting/protocol/message_decoder.h" |
22 #include "remoting/protocol/util.h" | 22 #include "remoting/protocol/util.h" |
23 #include "remoting/protocol/video_stub.h" | 23 #include "remoting/protocol/video_stub.h" |
24 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 24 #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_shape.h" | 25 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_shape.h" |
27 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" | 26 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
28 | 27 |
29 namespace remoting { | 28 namespace remoting { |
30 | 29 |
31 // Maximum number of frames that can be processed simultaneously. | 30 // Maximum number of frames that can be processed simultaneously. |
32 // TODO(hclam): Move this value to CaptureScheduler. | 31 // TODO(hclam): Move this value to CaptureScheduler. |
33 static const int kMaxPendingFrames = 2; | 32 static const int kMaxPendingFrames = 2; |
34 | 33 |
35 VideoScheduler::VideoScheduler( | 34 VideoScheduler::VideoScheduler( |
36 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | 35 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
37 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 36 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
38 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | 37 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
39 scoped_ptr<webrtc::ScreenCapturer> capturer, | 38 scoped_ptr<webrtc::ScreenCapturer> capturer, |
40 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | |
41 scoped_ptr<VideoEncoder> encoder, | 39 scoped_ptr<VideoEncoder> encoder, |
42 protocol::CursorShapeStub* cursor_stub, | 40 protocol::CursorShapeStub* cursor_stub, |
43 protocol::VideoStub* video_stub) | 41 protocol::VideoStub* video_stub) |
44 : capture_task_runner_(capture_task_runner), | 42 : capture_task_runner_(capture_task_runner), |
45 encode_task_runner_(encode_task_runner), | 43 encode_task_runner_(encode_task_runner), |
46 network_task_runner_(network_task_runner), | 44 network_task_runner_(network_task_runner), |
47 capturer_(capturer.Pass()), | 45 capturer_(capturer.Pass()), |
48 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), | |
49 encoder_(encoder.Pass()), | 46 encoder_(encoder.Pass()), |
50 cursor_stub_(cursor_stub), | 47 cursor_stub_(cursor_stub), |
51 video_stub_(video_stub), | 48 video_stub_(video_stub), |
52 pending_frames_(0), | 49 pending_frames_(0), |
53 capture_pending_(false), | 50 capture_pending_(false), |
54 did_skip_frame_(false), | 51 did_skip_frame_(false), |
55 is_paused_(false), | 52 is_paused_(false), |
56 sequence_number_(0) { | 53 sequence_number_(0) { |
57 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 54 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
58 DCHECK(capturer_); | 55 DCHECK(capturer_); |
(...skipping 24 matching lines...) Expand all Loading... |
83 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this, | 80 FROM_HERE, base::Bind(&VideoScheduler::EncodeFrame, this, |
84 base::Passed(&owned_frame), sequence_number_)); | 81 base::Passed(&owned_frame), sequence_number_)); |
85 | 82 |
86 // If a frame was skipped, try to capture it again. | 83 // If a frame was skipped, try to capture it again. |
87 if (did_skip_frame_) { | 84 if (did_skip_frame_) { |
88 capture_task_runner_->PostTask( | 85 capture_task_runner_->PostTask( |
89 FROM_HERE, base::Bind(&VideoScheduler::CaptureNextFrame, this)); | 86 FROM_HERE, base::Bind(&VideoScheduler::CaptureNextFrame, this)); |
90 } | 87 } |
91 } | 88 } |
92 | 89 |
93 void VideoScheduler::OnMouseCursor(webrtc::MouseCursor* cursor) { | 90 void VideoScheduler::OnCursorShapeChanged( |
| 91 webrtc::MouseCursorShape* cursor_shape) { |
94 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 92 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
95 | 93 |
96 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor); | 94 scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape); |
97 | 95 |
98 // Do nothing if the scheduler is being stopped. | 96 // Do nothing if the scheduler is being stopped. |
99 if (!capturer_) | 97 if (!capturer_) |
100 return; | 98 return; |
101 | 99 |
102 scoped_ptr<protocol::CursorShapeInfo> cursor_proto( | 100 scoped_ptr<protocol::CursorShapeInfo> cursor_proto( |
103 new protocol::CursorShapeInfo()); | 101 new protocol::CursorShapeInfo()); |
104 cursor_proto->set_width(cursor->image()->size().width()); | 102 cursor_proto->set_width(cursor_shape->size.width()); |
105 cursor_proto->set_height(cursor->image()->size().height()); | 103 cursor_proto->set_height(cursor_shape->size.height()); |
106 cursor_proto->set_hotspot_x(cursor->hotspot().x()); | 104 cursor_proto->set_hotspot_x(cursor_shape->hotspot.x()); |
107 cursor_proto->set_hotspot_y(cursor->hotspot().y()); | 105 cursor_proto->set_hotspot_y(cursor_shape->hotspot.y()); |
108 | 106 cursor_proto->set_data(cursor_shape->data); |
109 std::string data; | |
110 uint8_t* current_row = cursor->image()->data(); | |
111 for (int y = 0; y < cursor->image()->size().height(); ++y) { | |
112 cursor_proto->mutable_data()->append( | |
113 current_row, | |
114 current_row + cursor->image()->size().width() * | |
115 webrtc::DesktopFrame::kBytesPerPixel); | |
116 current_row += cursor->image()->stride(); | |
117 } | |
118 | 107 |
119 network_task_runner_->PostTask( | 108 network_task_runner_->PostTask( |
120 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this, | 109 FROM_HERE, base::Bind(&VideoScheduler::SendCursorShape, this, |
121 base::Passed(&cursor_proto))); | 110 base::Passed(&cursor_proto))); |
122 } | 111 } |
123 | 112 |
124 void VideoScheduler::OnMouseCursorPosition( | |
125 webrtc::MouseCursorMonitor::CursorState state, | |
126 const webrtc::DesktopVector& position) { | |
127 // We're not subscribing to mouse position changes. | |
128 NOTREACHED(); | |
129 } | |
130 | |
131 void VideoScheduler::Start() { | 113 void VideoScheduler::Start() { |
132 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 114 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
133 | 115 |
134 capture_task_runner_->PostTask( | 116 capture_task_runner_->PostTask( |
135 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); | 117 FROM_HERE, base::Bind(&VideoScheduler::StartOnCaptureThread, this)); |
136 } | 118 } |
137 | 119 |
138 void VideoScheduler::Stop() { | 120 void VideoScheduler::Stop() { |
139 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 121 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
140 | 122 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 161 |
180 VideoScheduler::~VideoScheduler() { | 162 VideoScheduler::~VideoScheduler() { |
181 } | 163 } |
182 | 164 |
183 // Capturer thread ------------------------------------------------------------- | 165 // Capturer thread ------------------------------------------------------------- |
184 | 166 |
185 void VideoScheduler::StartOnCaptureThread() { | 167 void VideoScheduler::StartOnCaptureThread() { |
186 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 168 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
187 DCHECK(!capture_timer_); | 169 DCHECK(!capture_timer_); |
188 | 170 |
189 // Start mouse cursor monitor. | 171 // Start the capturer and let it notify us if cursor shape changes. |
190 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); | 172 capturer_->SetMouseShapeObserver(this); |
191 | |
192 // Start the capturer. | |
193 capturer_->Start(this); | 173 capturer_->Start(this); |
194 | 174 |
195 capture_timer_.reset(new base::OneShotTimer<VideoScheduler>()); | 175 capture_timer_.reset(new base::OneShotTimer<VideoScheduler>()); |
196 | 176 |
197 // Capture first frame immedately. | 177 // Capture first frame immedately. |
198 CaptureNextFrame(); | 178 CaptureNextFrame(); |
199 } | 179 } |
200 | 180 |
201 void VideoScheduler::StopOnCaptureThread() { | 181 void VideoScheduler::StopOnCaptureThread() { |
202 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 182 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 217 |
238 // At this point we are going to perform one capture so save the current time. | 218 // At this point we are going to perform one capture so save the current time. |
239 pending_frames_++; | 219 pending_frames_++; |
240 DCHECK_LE(pending_frames_, kMaxPendingFrames); | 220 DCHECK_LE(pending_frames_, kMaxPendingFrames); |
241 | 221 |
242 // Before doing a capture schedule for the next one. | 222 // Before doing a capture schedule for the next one. |
243 ScheduleNextCapture(); | 223 ScheduleNextCapture(); |
244 | 224 |
245 capture_pending_ = true; | 225 capture_pending_ = true; |
246 | 226 |
247 // Capture the mouse shape. | |
248 mouse_cursor_monitor_->Capture(); | |
249 | |
250 // And finally perform one capture. | 227 // And finally perform one capture. |
251 capturer_->Capture(webrtc::DesktopRegion()); | 228 capturer_->Capture(webrtc::DesktopRegion()); |
252 } | 229 } |
253 | 230 |
254 void VideoScheduler::FrameCaptureCompleted() { | 231 void VideoScheduler::FrameCaptureCompleted() { |
255 DCHECK(capture_task_runner_->BelongsToCurrentThread()); | 232 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
256 | 233 |
257 // Decrement the pending capture count. | 234 // Decrement the pending capture count. |
258 pending_frames_--; | 235 pending_frames_--; |
259 DCHECK_GE(pending_frames_, 0); | 236 DCHECK_GE(pending_frames_, 0); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 frame.reset(); | 300 frame.reset(); |
324 | 301 |
325 scheduler_.RecordEncodeTime( | 302 scheduler_.RecordEncodeTime( |
326 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); | 303 base::TimeDelta::FromMilliseconds(packet->encode_time_ms())); |
327 network_task_runner_->PostTask( | 304 network_task_runner_->PostTask( |
328 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this, | 305 FROM_HERE, base::Bind(&VideoScheduler::SendVideoPacket, this, |
329 base::Passed(&packet))); | 306 base::Passed(&packet))); |
330 } | 307 } |
331 | 308 |
332 } // namespace remoting | 309 } // namespace remoting |
OLD | NEW |