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

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

Issue 850983002: Implement video frame acknowledgements in the chromoting protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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
« no previous file with comments | « remoting/host/capture_scheduler.h ('k') | remoting/host/capture_scheduler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
28 // Maximum number of frames that can be processed simultaneously. 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
33 // Maximum number of unacknowledged frames. Ignored if the client doesn't
34 // support ACKs. This value was chosen experimentally, using synthetic
35 // performance tests (see ProtocolPerfTest), to maximize frame rate, while
36 // keeping round-trip latency low.
37 static const int kMaxUnacknowledgedFrames = 4;
38
39 // Maximum number of frames that can be processed (captured, encoded or sent)
40 // simultaneously. It's used only in the case when the client doesn't support
41 // ACKs.
42 //
43 // TODO(sergeyu): Remove this once all current versions support ACKs.
44 // crbug.com/460963 .
29 static const int kMaxPendingFrames = 2; 45 static const int kMaxPendingFrames = 2;
30 46
31 } // namespace 47 } // namespace
32 48
33 namespace remoting { 49 namespace remoting {
34 50
35 // We assume that the number of available cores is constant. 51 // We assume that the number of available cores is constant.
36 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure) 52 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure)
37 : capture_closure_(capture_closure), 53 : capture_closure_(capture_closure),
54 acks_supported_(false),
38 tick_clock_(new base::DefaultTickClock()), 55 tick_clock_(new base::DefaultTickClock()),
39 capture_timer_(new base::Timer(false, false)), 56 capture_timer_(new base::Timer(false, false)),
40 minimum_interval_( 57 minimum_interval_(
41 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), 58 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
42 num_of_processors_(base::SysInfo::NumberOfProcessors()), 59 num_of_processors_(base::SysInfo::NumberOfProcessors()),
43 capture_time_(kStatisticsWindow), 60 capture_time_(kStatisticsWindow),
44 encode_time_(kStatisticsWindow), 61 encode_time_(kStatisticsWindow),
45 pending_frames_(0), 62 num_encoding_frames_(0),
63 num_sending_frames_(0),
64 num_unacknowledged_frames_(0),
46 capture_pending_(false), 65 capture_pending_(false),
47 is_paused_(false) { 66 is_paused_(false),
67 next_frame_id_(0) {
48 DCHECK(num_of_processors_); 68 DCHECK(num_of_processors_);
49 } 69 }
50 70
51 CaptureScheduler::~CaptureScheduler() { 71 CaptureScheduler::~CaptureScheduler() {
52 } 72 }
53 73
54 void CaptureScheduler::Start() { 74 void CaptureScheduler::Start() {
55 DCHECK(CalledOnValidThread()); 75 DCHECK(thread_checker_.CalledOnValidThread());
56 76
57 ScheduleNextCapture(); 77 ScheduleNextCapture();
58 } 78 }
59 79
60 void CaptureScheduler::Pause(bool pause) { 80 void CaptureScheduler::Pause(bool pause) {
61 DCHECK(CalledOnValidThread()); 81 DCHECK(thread_checker_.CalledOnValidThread());
62 82
63 if (is_paused_ != pause) { 83 if (is_paused_ != pause) {
64 is_paused_ = pause; 84 is_paused_ = pause;
65 85
66 if (is_paused_) { 86 if (is_paused_) {
67 capture_timer_->Stop(); 87 capture_timer_->Stop();
68 } else { 88 } else {
69 ScheduleNextCapture(); 89 ScheduleNextCapture();
70 } 90 }
71 } 91 }
72 } 92 }
73 93
74 void CaptureScheduler::OnCaptureCompleted() { 94 void CaptureScheduler::OnCaptureCompleted() {
75 DCHECK(CalledOnValidThread()); 95 DCHECK(thread_checker_.CalledOnValidThread());
76 96
77 capture_pending_ = false; 97 capture_pending_ = false;
78 capture_time_.Record( 98 capture_time_.Record(
79 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); 99 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
80 100
101 ++num_encoding_frames_;
102
103 ScheduleNextCapture();
104 }
105
106 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
107 DCHECK(thread_checker_.CalledOnValidThread());
108
109 // Set packet_id for the outgoing packet.
110 packet->set_frame_id(next_frame_id_);
111 ++next_frame_id_;
112
113 // Update internal stats.
114 encode_time_.Record(packet->encode_time_ms());
115
116 --num_encoding_frames_;
117 ++num_sending_frames_;
118 ++num_unacknowledged_frames_;
119
81 ScheduleNextCapture(); 120 ScheduleNextCapture();
82 } 121 }
83 122
84 void CaptureScheduler::OnFrameSent() { 123 void CaptureScheduler::OnFrameSent() {
85 DCHECK(CalledOnValidThread()); 124 DCHECK(thread_checker_.CalledOnValidThread());
86 125
87 // Decrement the pending capture count. 126 --num_sending_frames_;
88 pending_frames_--; 127 DCHECK_GE(num_sending_frames_, 0);
89 DCHECK_GE(pending_frames_, 0);
90 128
91 ScheduleNextCapture(); 129 ScheduleNextCapture();
92 } 130 }
93 131
94 void CaptureScheduler::OnFrameEncoded(base::TimeDelta encode_time) { 132 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) {
95 DCHECK(CalledOnValidThread()); 133 DCHECK(thread_checker_.CalledOnValidThread());
96 134
97 encode_time_.Record(encode_time.InMilliseconds()); 135 // Host always sets |frame_id| field to indicated that it expects ACK from the
136 // client. It's assumed that the client doesn't support ACKs until the first
137 // ACK message is received.
138 acks_supported_ = true;
139
140 --num_unacknowledged_frames_;
141 DCHECK_GE(num_unacknowledged_frames_, 0);
142
98 ScheduleNextCapture(); 143 ScheduleNextCapture();
99 } 144 }
100 145
101 void CaptureScheduler::SetTickClockForTest( 146 void CaptureScheduler::SetTickClockForTest(
102 scoped_ptr<base::TickClock> tick_clock) { 147 scoped_ptr<base::TickClock> tick_clock) {
103 tick_clock_ = tick_clock.Pass(); 148 tick_clock_ = tick_clock.Pass();
104 } 149 }
150
105 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) { 151 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
106 capture_timer_ = timer.Pass(); 152 capture_timer_ = timer.Pass();
107 } 153 }
154
108 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { 155 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
109 num_of_processors_ = num_of_processors; 156 num_of_processors_ = num_of_processors;
110 } 157 }
111 158
112 void CaptureScheduler::ScheduleNextCapture() { 159 void CaptureScheduler::ScheduleNextCapture() {
113 DCHECK(CalledOnValidThread()); 160 DCHECK(thread_checker_.CalledOnValidThread());
114 161
115 if (is_paused_ || pending_frames_ >= kMaxPendingFrames || capture_pending_) 162 if (is_paused_ || capture_pending_ ||
163 num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
116 return; 164 return;
165 }
166
167 if (acks_supported_) {
168 if (num_encoding_frames_ + num_unacknowledged_frames_ >=
169 kMaxUnacknowledgedFrames) {
170 return;
171 }
172 } else {
173 // TODO(sergeyu): Remove this once all current versions support ACKs.
174 // crbug.com/460963 .
175 if (num_encoding_frames_ + num_sending_frames_ >= kMaxPendingFrames) {
176 return;
177 }
178 }
117 179
118 // Delay by an amount chosen such that if capture and encode times 180 // Delay by an amount chosen such that if capture and encode times
119 // continue to follow the averages, then we'll consume the target 181 // continue to follow the averages, then we'll consume the target
120 // fraction of CPU across all cores. 182 // fraction of CPU across all cores.
121 base::TimeDelta delay = 183 base::TimeDelta delay =
122 std::max(minimum_interval_, 184 std::max(minimum_interval_,
123 base::TimeDelta::FromMilliseconds( 185 base::TimeDelta::FromMilliseconds(
124 (capture_time_.Average() + encode_time_.Average()) / 186 (capture_time_.Average() + encode_time_.Average()) /
125 (kRecordingCpuConsumption * num_of_processors_))); 187 (kRecordingCpuConsumption * num_of_processors_)));
126 188
127 // Account for the time that has passed since the last capture. 189 // Account for the time that has passed since the last capture.
128 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() - 190 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
129 last_capture_started_time_)); 191 last_capture_started_time_));
130 192
131 capture_timer_->Start( 193 capture_timer_->Start(
132 FROM_HERE, delay, 194 FROM_HERE, delay,
133 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); 195 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
134 } 196 }
135 197
136 void CaptureScheduler::CaptureNextFrame() { 198 void CaptureScheduler::CaptureNextFrame() {
137 DCHECK(CalledOnValidThread()); 199 DCHECK(thread_checker_.CalledOnValidThread());
138 DCHECK(!is_paused_); 200 DCHECK(!is_paused_);
139 DCHECK(!capture_pending_); 201 DCHECK(!capture_pending_);
140 202
141 pending_frames_++;
142 DCHECK_LE(pending_frames_, kMaxPendingFrames);
143
144 capture_pending_ = true; 203 capture_pending_ = true;
145 last_capture_started_time_ = tick_clock_->NowTicks(); 204 last_capture_started_time_ = tick_clock_->NowTicks();
146 capture_closure_.Run(); 205 capture_closure_.Run();
147 } 206 }
148 207
149 } // namespace remoting 208 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/capture_scheduler.h ('k') | remoting/host/capture_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698