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/capture_scheduler.h" | 5 #include "remoting/host/capture_scheduler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| 11 #include "base/time/default_tick_clock.h" | 11 #include "base/time/default_tick_clock.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "remoting/proto/video.pb.h" | |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Number of samples to average the most recent capture and encode time | 17 // Number of samples to average the most recent capture and encode time |
| 17 // over. | 18 // over. |
| 18 const int kStatisticsWindow = 3; | 19 const int kStatisticsWindow = 3; |
| 19 | 20 |
| 20 // The hard limit is 30fps or 33ms per recording cycle. | 21 // The hard limit is 30fps or 33ms per recording cycle. |
| 21 const int64 kDefaultMinimumIntervalMs = 33; | 22 const int64 kDefaultMinimumIntervalMs = 33; |
| 22 | 23 |
| 23 // Controls how much CPU time we can use for encode and capture. | 24 // Controls how much CPU time we can use for encode and capture. |
| 24 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs | 25 // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs |
| 25 // available while 1 means using 100% of all CPUs available. | 26 // available while 1 means using 100% of all CPUs available. |
| 26 const double kRecordingCpuConsumption = 0.5; | 27 const double kRecordingCpuConsumption = 0.5; |
| 27 | 28 |
| 29 // Maximum number of captured frames in the encoding queue. Currently capturer | |
| 30 // implementations do not allow to keep more than 2 DesktopFrame objects. | |
| 31 static const int kMaxFramesInEncodingQueue = 2; | |
| 32 | |
| 28 // Maximum number of frames that can be processed simultaneously. | 33 // Maximum number of frames that can be processed simultaneously. |
| 29 static const int kMaxPendingFrames = 2; | 34 static const int kMaxPendingFrames = 4; |
| 35 static const int kMaxPendingFramesWithoutAcks = 2; | |
| 30 | 36 |
| 31 } // namespace | 37 } // namespace |
| 32 | 38 |
| 33 namespace remoting { | 39 namespace remoting { |
| 34 | 40 |
| 35 // We assume that the number of available cores is constant. | 41 // We assume that the number of available cores is constant. |
| 36 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure) | 42 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure) |
| 37 : capture_closure_(capture_closure), | 43 : capture_closure_(capture_closure), |
| 44 acks_supported_(false), | |
| 38 tick_clock_(new base::DefaultTickClock()), | 45 tick_clock_(new base::DefaultTickClock()), |
| 39 capture_timer_(new base::Timer(false, false)), | 46 capture_timer_(new base::Timer(false, false)), |
| 40 minimum_interval_( | 47 minimum_interval_( |
| 41 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), | 48 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), |
| 42 num_of_processors_(base::SysInfo::NumberOfProcessors()), | 49 num_of_processors_(base::SysInfo::NumberOfProcessors()), |
| 43 capture_time_(kStatisticsWindow), | 50 capture_time_(kStatisticsWindow), |
| 44 encode_time_(kStatisticsWindow), | 51 encode_time_(kStatisticsWindow), |
| 45 pending_frames_(0), | 52 num_encoding_frames_(0), |
| 53 num_sending_frames_(0), | |
| 54 num_unacknowledged_frames_(0), | |
| 46 capture_pending_(false), | 55 capture_pending_(false), |
| 47 is_paused_(false) { | 56 is_paused_(false), |
| 57 next_frame_id_(0) { | |
| 48 DCHECK(num_of_processors_); | 58 DCHECK(num_of_processors_); |
| 49 } | 59 } |
| 50 | 60 |
| 51 CaptureScheduler::~CaptureScheduler() { | 61 CaptureScheduler::~CaptureScheduler() { |
| 52 } | 62 } |
| 53 | 63 |
| 54 void CaptureScheduler::Start() { | 64 void CaptureScheduler::Start() { |
| 55 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
| 56 | 66 |
| 57 ScheduleNextCapture(); | 67 ScheduleNextCapture(); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 71 } | 81 } |
| 72 } | 82 } |
| 73 | 83 |
| 74 void CaptureScheduler::OnCaptureCompleted() { | 84 void CaptureScheduler::OnCaptureCompleted() { |
| 75 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
| 76 | 86 |
| 77 capture_pending_ = false; | 87 capture_pending_ = false; |
| 78 capture_time_.Record( | 88 capture_time_.Record( |
| 79 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); | 89 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); |
| 80 | 90 |
| 91 ++num_encoding_frames_; | |
| 92 | |
| 93 ScheduleNextCapture(); | |
| 94 } | |
| 95 | |
| 96 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) { | |
| 97 DCHECK(CalledOnValidThread()); | |
| 98 | |
| 99 // Set packet_id for the outgoing packet. | |
| 100 packet->set_frame_id(next_frame_id_); | |
|
Wez
2015/02/11 02:22:55
Do we want to do that if ACKs are disabled?
Sergey Ulanov
2015/02/17 19:37:06
Yes. In the current version of this CL protocol ve
| |
| 101 ++next_frame_id_; | |
| 102 | |
| 103 // Update internal stats. | |
| 104 encode_time_.Record(packet->encode_time_ms()); | |
| 105 | |
| 106 --num_encoding_frames_; | |
| 107 ++num_sending_frames_; | |
| 108 ++num_unacknowledged_frames_; | |
|
Wez
2015/02/11 02:22:55
This will grow unbounded if ACKs aren't enabled, r
Sergey Ulanov
2015/02/17 19:37:06
Right. And then it's ignored in ScheduleNextCaptur
| |
| 109 | |
| 81 ScheduleNextCapture(); | 110 ScheduleNextCapture(); |
| 82 } | 111 } |
| 83 | 112 |
| 84 void CaptureScheduler::OnFrameSent() { | 113 void CaptureScheduler::OnFrameSent() { |
| 85 DCHECK(CalledOnValidThread()); | 114 DCHECK(CalledOnValidThread()); |
| 86 | 115 |
| 87 // Decrement the pending capture count. | 116 --num_sending_frames_; |
| 88 pending_frames_--; | 117 DCHECK_GE(num_sending_frames_, 0); |
| 89 DCHECK_GE(pending_frames_, 0); | |
| 90 | 118 |
| 91 ScheduleNextCapture(); | 119 ScheduleNextCapture(); |
| 92 } | 120 } |
| 93 | 121 |
| 94 void CaptureScheduler::OnFrameEncoded(base::TimeDelta encode_time) { | 122 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) { |
| 95 DCHECK(CalledOnValidThread()); | 123 DCHECK(CalledOnValidThread()); |
| 96 | 124 |
| 97 encode_time_.Record(encode_time.InMilliseconds()); | 125 acks_supported_ = true; |
|
Wez
2015/02/11 02:22:55
It seems strange to have this depend upon whether
Sergey Ulanov
2015/02/17 19:37:06
See my comment in OnFrameEncoded(). It makes this
Wez
2015/02/21 03:12:02
It makes the CL simpler but arguably makes the res
Sergey Ulanov
2015/02/23 17:35:38
Opened crbug.com/460963
| |
| 126 | |
| 127 --num_unacknowledged_frames_; | |
| 128 DCHECK_GE(num_unacknowledged_frames_, 0); | |
| 129 | |
| 98 ScheduleNextCapture(); | 130 ScheduleNextCapture(); |
| 99 } | 131 } |
| 100 | 132 |
| 101 void CaptureScheduler::SetTickClockForTest( | 133 void CaptureScheduler::SetTickClockForTest( |
| 102 scoped_ptr<base::TickClock> tick_clock) { | 134 scoped_ptr<base::TickClock> tick_clock) { |
| 103 tick_clock_ = tick_clock.Pass(); | 135 tick_clock_ = tick_clock.Pass(); |
| 104 } | 136 } |
| 137 | |
| 105 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 138 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) { |
| 106 capture_timer_ = timer.Pass(); | 139 capture_timer_ = timer.Pass(); |
| 107 } | 140 } |
| 141 | |
| 108 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { | 142 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { |
| 109 num_of_processors_ = num_of_processors; | 143 num_of_processors_ = num_of_processors; |
| 110 } | 144 } |
| 111 | 145 |
| 112 void CaptureScheduler::ScheduleNextCapture() { | 146 void CaptureScheduler::ScheduleNextCapture() { |
| 113 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
| 114 | 148 |
| 115 if (is_paused_ || pending_frames_ >= kMaxPendingFrames || capture_pending_) | 149 if (is_paused_ || capture_pending_ || |
| 150 num_encoding_frames_ >= kMaxFramesInEncodingQueue) { | |
| 116 return; | 151 return; |
| 152 } | |
| 153 | |
| 154 if (acks_supported_) { | |
| 155 if (num_encoding_frames_ + num_unacknowledged_frames_ >= kMaxPendingFrames) | |
| 156 return; | |
| 157 } else { | |
| 158 if (num_encoding_frames_ + num_sending_frames_ >= | |
| 159 kMaxPendingFramesWithoutAcks) { | |
| 160 return; | |
| 161 } | |
| 162 } | |
| 117 | 163 |
| 118 // Delay by an amount chosen such that if capture and encode times | 164 // Delay by an amount chosen such that if capture and encode times |
| 119 // continue to follow the averages, then we'll consume the target | 165 // continue to follow the averages, then we'll consume the target |
| 120 // fraction of CPU across all cores. | 166 // fraction of CPU across all cores. |
| 121 base::TimeDelta delay = | 167 base::TimeDelta delay = |
| 122 std::max(minimum_interval_, | 168 std::max(minimum_interval_, |
| 123 base::TimeDelta::FromMilliseconds( | 169 base::TimeDelta::FromMilliseconds( |
| 124 (capture_time_.Average() + encode_time_.Average()) / | 170 (capture_time_.Average() + encode_time_.Average()) / |
| 125 (kRecordingCpuConsumption * num_of_processors_))); | 171 (kRecordingCpuConsumption * num_of_processors_))); |
| 126 | 172 |
| 127 // Account for the time that has passed since the last capture. | 173 // Account for the time that has passed since the last capture. |
| 128 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() - | 174 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() - |
| 129 last_capture_started_time_)); | 175 last_capture_started_time_)); |
| 130 | 176 |
| 131 capture_timer_->Start( | 177 capture_timer_->Start( |
| 132 FROM_HERE, delay, | 178 FROM_HERE, delay, |
| 133 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); | 179 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); |
| 134 } | 180 } |
| 135 | 181 |
| 136 void CaptureScheduler::CaptureNextFrame() { | 182 void CaptureScheduler::CaptureNextFrame() { |
| 137 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
| 138 DCHECK(!is_paused_); | 184 DCHECK(!is_paused_); |
| 139 DCHECK(!capture_pending_); | 185 DCHECK(!capture_pending_); |
| 140 | 186 |
| 141 pending_frames_++; | |
| 142 DCHECK_LE(pending_frames_, kMaxPendingFrames); | |
| 143 | |
| 144 capture_pending_ = true; | 187 capture_pending_ = true; |
| 145 last_capture_started_time_ = tick_clock_->NowTicks(); | 188 last_capture_started_time_ = tick_clock_->NowTicks(); |
| 146 capture_closure_.Run(); | 189 capture_closure_.Run(); |
| 147 } | 190 } |
| 148 | 191 |
| 149 } // namespace remoting | 192 } // namespace remoting |
| OLD | NEW |