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