Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/screen_recorder.h" | 5 #include "remoting/host/screen_recorder.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/sys_info.h" | |
| 14 #include "base/task.h" | 15 #include "base/task.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "remoting/base/capture_data.h" | 17 #include "remoting/base/capture_data.h" |
| 17 #include "remoting/proto/control.pb.h" | 18 #include "remoting/proto/control.pb.h" |
| 18 #include "remoting/proto/video.pb.h" | 19 #include "remoting/proto/video.pb.h" |
| 19 #include "remoting/protocol/client_stub.h" | 20 #include "remoting/protocol/client_stub.h" |
| 20 #include "remoting/protocol/connection_to_client.h" | 21 #include "remoting/protocol/connection_to_client.h" |
| 21 #include "remoting/protocol/message_decoder.h" | 22 #include "remoting/protocol/message_decoder.h" |
| 22 #include "remoting/protocol/util.h" | 23 #include "remoting/protocol/util.h" |
| 23 | 24 |
| 24 using remoting::protocol::ConnectionToClient; | 25 using remoting::protocol::ConnectionToClient; |
| 25 | 26 |
| 26 namespace remoting { | 27 namespace remoting { |
| 27 | 28 |
| 28 // By default we capture 20 times a second. This number is obtained by | |
| 29 // experiment to provide good latency. | |
| 30 static const double kDefaultCaptureRate = 20.0; | |
| 31 | |
| 32 // Maximum number of frames that can be processed similtaneously. | 29 // Maximum number of frames that can be processed similtaneously. |
| 33 // TODO(sergeyu): Should this be set to 1? Or should we change | |
| 34 // dynamically depending on how fast network and CPU are? Experement | |
| 35 // with it. | |
| 36 static const int kMaxRecordings = 2; | 30 static const int kMaxRecordings = 2; |
| 37 | 31 |
| 38 ScreenRecorder::ScreenRecorder( | 32 ScreenRecorder::ScreenRecorder( |
| 39 MessageLoop* capture_loop, | 33 MessageLoop* capture_loop, |
| 40 MessageLoop* encode_loop, | 34 MessageLoop* encode_loop, |
| 41 base::MessageLoopProxy* network_loop, | 35 base::MessageLoopProxy* network_loop, |
| 42 Capturer* capturer, | 36 Capturer* capturer, |
| 43 Encoder* encoder) | 37 Encoder* encoder) |
| 44 : capture_loop_(capture_loop), | 38 : capture_loop_(capture_loop), |
| 45 encode_loop_(encode_loop), | 39 encode_loop_(encode_loop), |
| 46 network_loop_(network_loop), | 40 network_loop_(network_loop), |
| 47 capturer_(capturer), | 41 capturer_(capturer), |
| 48 encoder_(encoder), | 42 encoder_(encoder), |
| 49 is_recording_(false), | 43 is_recording_(false), |
| 50 network_stopped_(false), | 44 network_stopped_(false), |
| 51 encoder_stopped_(false), | 45 encoder_stopped_(false), |
| 46 max_recordings_(kMaxRecordings), | |
| 52 recordings_(0), | 47 recordings_(0), |
| 53 frame_skipped_(false), | 48 frame_skipped_(false), |
| 54 max_rate_(kDefaultCaptureRate), | |
| 55 sequence_number_(0) { | 49 sequence_number_(0) { |
| 56 DCHECK(capture_loop_); | 50 DCHECK(capture_loop_); |
| 57 DCHECK(encode_loop_); | 51 DCHECK(encode_loop_); |
| 58 DCHECK(network_loop_); | 52 DCHECK(network_loop_); |
| 53 | |
| 54 // If the system has only one processor there's no use to have simutaneous | |
| 55 // recording sessions, it will just create excessive system load. | |
| 56 if (base::SysInfo::NumberOfProcessors() < 2) | |
| 57 max_recordings_ = 2; | |
|
Wez
2011/10/25 00:05:38
Is there evidence of excessive load, even with the
Alpha Left Google
2011/10/31 18:47:24
The original design was assuming multiple core we
| |
| 59 } | 58 } |
| 60 | 59 |
| 61 ScreenRecorder::~ScreenRecorder() { | 60 ScreenRecorder::~ScreenRecorder() { |
| 62 } | 61 } |
| 63 | 62 |
| 64 // Public methods -------------------------------------------------------------- | 63 // Public methods -------------------------------------------------------------- |
| 65 | 64 |
| 66 void ScreenRecorder::Start() { | 65 void ScreenRecorder::Start() { |
| 67 capture_loop_->PostTask( | 66 capture_loop_->PostTask( |
| 68 FROM_HERE, NewRunnableMethod(this, &ScreenRecorder::DoStart)); | 67 FROM_HERE, NewRunnableMethod(this, &ScreenRecorder::DoStart)); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void ScreenRecorder::Stop(const base::Closure& done_task) { | 70 void ScreenRecorder::Stop(const base::Closure& done_task) { |
| 72 if (MessageLoop::current() != capture_loop_) { | 71 if (MessageLoop::current() != capture_loop_) { |
| 73 capture_loop_->PostTask(FROM_HERE, base::Bind( | 72 capture_loop_->PostTask(FROM_HERE, base::Bind( |
| 74 &ScreenRecorder::Stop, this, done_task)); | 73 &ScreenRecorder::Stop, this, done_task)); |
| 75 return; | 74 return; |
| 76 } | 75 } |
| 77 | 76 |
| 78 DCHECK(!done_task.is_null()); | 77 DCHECK(!done_task.is_null()); |
| 79 | 78 |
| 80 capture_timer_.Stop(); | 79 capture_timer_.Stop(); |
| 81 is_recording_ = false; | 80 is_recording_ = false; |
| 82 | 81 |
| 83 network_loop_->PostTask(FROM_HERE, base::Bind( | 82 network_loop_->PostTask(FROM_HERE, base::Bind( |
| 84 &ScreenRecorder::DoStopOnNetworkThread, this, done_task)); | 83 &ScreenRecorder::DoStopOnNetworkThread, this, done_task)); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void ScreenRecorder::SetMaxRate(double rate) { | |
| 88 capture_loop_->PostTask( | |
| 89 FROM_HERE, NewRunnableMethod(this, &ScreenRecorder::DoSetMaxRate, rate)); | |
| 90 } | |
| 91 | |
| 92 void ScreenRecorder::AddConnection( | 86 void ScreenRecorder::AddConnection( |
| 93 scoped_refptr<ConnectionToClient> connection) { | 87 scoped_refptr<ConnectionToClient> connection) { |
| 94 capture_loop_->PostTask( | 88 capture_loop_->PostTask( |
| 95 FROM_HERE, | 89 FROM_HERE, |
| 96 NewRunnableMethod(this, &ScreenRecorder::DoInvalidateFullScreen)); | 90 NewRunnableMethod(this, &ScreenRecorder::DoInvalidateFullScreen)); |
| 97 | 91 |
| 98 // Add the client to the list so it can receive update stream. | 92 // Add the client to the list so it can receive update stream. |
| 99 network_loop_->PostTask( | 93 network_loop_->PostTask( |
| 100 FROM_HERE, | 94 FROM_HERE, |
| 101 NewRunnableMethod(this, &ScreenRecorder::DoAddConnection, connection)); | 95 NewRunnableMethod(this, &ScreenRecorder::DoAddConnection, connection)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 return; | 145 return; |
| 152 } | 146 } |
| 153 | 147 |
| 154 is_recording_ = true; | 148 is_recording_ = true; |
| 155 StartCaptureTimer(); | 149 StartCaptureTimer(); |
| 156 | 150 |
| 157 // Capture first frame immedately. | 151 // Capture first frame immedately. |
| 158 DoCapture(); | 152 DoCapture(); |
| 159 } | 153 } |
| 160 | 154 |
| 161 void ScreenRecorder::DoSetMaxRate(double max_rate) { | |
| 162 DCHECK_EQ(capture_loop_, MessageLoop::current()); | |
| 163 | |
| 164 // TODO(hclam): Should also check for small epsilon. | |
| 165 DCHECK_GT(max_rate, 0.0) << "Rate is too small."; | |
| 166 | |
| 167 max_rate_ = max_rate; | |
| 168 | |
| 169 // Restart the timer with the new rate. | |
| 170 if (is_recording_) { | |
| 171 capture_timer_.Stop(); | |
| 172 StartCaptureTimer(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void ScreenRecorder::StartCaptureTimer() { | 155 void ScreenRecorder::StartCaptureTimer() { |
| 177 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 156 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
| 178 | 157 |
| 179 base::TimeDelta interval = base::TimeDelta::FromMilliseconds( | 158 capture_timer_.Start(FROM_HERE, |
| 180 static_cast<int>(base::Time::kMillisecondsPerSecond / max_rate_)); | 159 scheduler_.NextCaptureDelay(), |
| 181 capture_timer_.Start(FROM_HERE, interval, this, &ScreenRecorder::DoCapture); | 160 this, |
| 161 &ScreenRecorder::DoCapture); | |
| 182 } | 162 } |
| 183 | 163 |
| 184 void ScreenRecorder::DoCapture() { | 164 void ScreenRecorder::DoCapture() { |
| 185 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 165 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
| 186 // Make sure we have at most two oustanding recordings. We can simply return | 166 // Make sure we have at most two oustanding recordings. We can simply return |
| 187 // if we can't make a capture now, the next capture will be started by the | 167 // if we can't make a capture now, the next capture will be started by the |
| 188 // end of an encode operation. | 168 // end of an encode operation. |
| 189 if (recordings_ >= kMaxRecordings || !is_recording_) { | 169 if (recordings_ >= max_recordings_ || !is_recording_) { |
| 190 frame_skipped_ = true; | 170 frame_skipped_ = true; |
| 191 return; | 171 return; |
| 192 } | 172 } |
| 193 | 173 |
| 194 if (frame_skipped_) { | 174 if (frame_skipped_) |
| 195 frame_skipped_ = false; | 175 frame_skipped_ = false; |
| 196 capture_timer_.Reset(); | |
| 197 } | |
| 198 | 176 |
| 199 // At this point we are going to perform one capture so save the current time. | 177 // At this point we are going to perform one capture so save the current time. |
| 200 ++recordings_; | 178 ++recordings_; |
| 201 DCHECK_LE(recordings_, kMaxRecordings); | 179 DCHECK_LE(recordings_, max_recordings_); |
| 180 | |
| 181 // Before doing a capture schedule for the next one. | |
| 182 capture_timer_.Stop(); | |
| 183 capture_timer_.Start(FROM_HERE, | |
| 184 scheduler_.NextCaptureDelay(), | |
| 185 this, | |
| 186 &ScreenRecorder::DoCapture); | |
| 202 | 187 |
| 203 // And finally perform one capture. | 188 // And finally perform one capture. |
| 204 capture_start_time_ = base::Time::Now(); | 189 capture_start_time_ = base::Time::Now(); |
| 205 capturer()->CaptureInvalidRegion( | 190 capturer()->CaptureInvalidRegion( |
| 206 NewCallback(this, &ScreenRecorder::CaptureDoneCallback)); | 191 NewCallback(this, &ScreenRecorder::CaptureDoneCallback)); |
| 207 } | 192 } |
| 208 | 193 |
| 209 void ScreenRecorder::CaptureDoneCallback( | 194 void ScreenRecorder::CaptureDoneCallback( |
| 210 scoped_refptr<CaptureData> capture_data) { | 195 scoped_refptr<CaptureData> capture_data) { |
| 211 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 196 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
| 212 | 197 |
| 213 if (!is_recording_) | 198 if (!is_recording_) |
| 214 return; | 199 return; |
| 215 | 200 |
| 216 if (capture_data) { | 201 if (capture_data) { |
| 217 int capture_time = static_cast<int>( | 202 base::TimeDelta capture_time = base::Time::Now() - capture_start_time_; |
| 218 (base::Time::Now() - capture_start_time_).InMilliseconds()); | 203 int capture_time_ms = |
| 219 capture_data->set_capture_time_ms(capture_time); | 204 static_cast<int>(capture_time.InMilliseconds()); |
| 205 capture_data->set_capture_time_ms(capture_time_ms); | |
| 206 scheduler_.RecordCaptureTime(capture_time); | |
| 220 | 207 |
| 221 // The best way to get this value is by binding the sequence number to | 208 // The best way to get this value is by binding the sequence number to |
| 222 // the callback when calling CaptureInvalidRects(). However the callback | 209 // the callback when calling CaptureInvalidRects(). However the callback |
| 223 // system doesn't allow this. Reading from the member variable is | 210 // system doesn't allow this. Reading from the member variable is |
| 224 // accurate as long as capture is synchronous as the following statement | 211 // accurate as long as capture is synchronous as the following statement |
| 225 // will obtain the most recent sequence number received. | 212 // will obtain the most recent sequence number received. |
| 226 capture_data->set_client_sequence_number(sequence_number_); | 213 capture_data->set_client_sequence_number(sequence_number_); |
| 227 } | 214 } |
| 228 | 215 |
| 229 encode_loop_->PostTask( | 216 encode_loop_->PostTask( |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 } | 361 } |
| 375 | 362 |
| 376 void ScreenRecorder::EncodedDataAvailableCallback(VideoPacket* packet) { | 363 void ScreenRecorder::EncodedDataAvailableCallback(VideoPacket* packet) { |
| 377 DCHECK_EQ(encode_loop_, MessageLoop::current()); | 364 DCHECK_EQ(encode_loop_, MessageLoop::current()); |
| 378 | 365 |
| 379 if (encoder_stopped_) | 366 if (encoder_stopped_) |
| 380 return; | 367 return; |
| 381 | 368 |
| 382 bool last = (packet->flags() & VideoPacket::LAST_PACKET) != 0; | 369 bool last = (packet->flags() & VideoPacket::LAST_PACKET) != 0; |
| 383 if (last) { | 370 if (last) { |
| 384 int encode_time = static_cast<int>( | 371 base::TimeDelta encode_time = base::Time::Now() - encode_start_time_; |
| 385 (base::Time::Now() - encode_start_time_).InMilliseconds()); | 372 int encode_time_ms = |
| 386 packet->set_encode_time_ms(encode_time); | 373 static_cast<int>(encode_time.InMilliseconds()); |
| 374 packet->set_encode_time_ms(encode_time_ms); | |
| 375 scheduler_.RecordEncodeTime(encode_time); | |
| 387 } | 376 } |
| 388 | 377 |
| 389 network_loop_->PostTask( | 378 network_loop_->PostTask( |
| 390 FROM_HERE, | 379 FROM_HERE, |
| 391 NewRunnableMethod(this, &ScreenRecorder::DoSendVideoPacket, packet)); | 380 NewRunnableMethod(this, &ScreenRecorder::DoSendVideoPacket, packet)); |
| 392 } | 381 } |
| 393 | 382 |
| 394 } // namespace remoting | 383 } // namespace remoting |
| OLD | NEW |