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

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
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 frames that haven't been acknowledged. It's used only when
Wez 2015/02/21 03:12:02 nit: Suggest "Maximum number of unacknowledged fra
Sergey Ulanov 2015/02/23 17:35:38 Done.
34 // the client support ACKs. This values was chosen experimentally, using
Wez 2015/02/21 03:12:02 nit: "This" doesn't go with "values" - "This value
Sergey Ulanov 2015/02/23 17:35:38 Done.
35 // synthetic performance tests (see ProtocolPerfTest), to maximize frame rate,
36 // while keeping round-trip latency low.
37 static const int kMaxUnacknowledgedFrames = 4;
38
39 // Maximum number of frames that can be processed simultaneously. It's used only
Wez 2015/02/21 03:12:02 Need to be clear about where this limit comes from
Sergey Ulanov 2015/02/23 17:35:38 This is a legacy value and will need to be removed
40 // in the case when the client doesn't support ACKs.
29 static const int kMaxPendingFrames = 2; 41 static const int kMaxPendingFrames = 2;
30 42
31 } // namespace 43 } // namespace
32 44
33 namespace remoting { 45 namespace remoting {
34 46
35 // We assume that the number of available cores is constant. 47 // We assume that the number of available cores is constant.
36 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure) 48 CaptureScheduler::CaptureScheduler(const base::Closure& capture_closure)
37 : capture_closure_(capture_closure), 49 : capture_closure_(capture_closure),
50 acks_supported_(false),
38 tick_clock_(new base::DefaultTickClock()), 51 tick_clock_(new base::DefaultTickClock()),
39 capture_timer_(new base::Timer(false, false)), 52 capture_timer_(new base::Timer(false, false)),
40 minimum_interval_( 53 minimum_interval_(
41 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)), 54 base::TimeDelta::FromMilliseconds(kDefaultMinimumIntervalMs)),
42 num_of_processors_(base::SysInfo::NumberOfProcessors()), 55 num_of_processors_(base::SysInfo::NumberOfProcessors()),
43 capture_time_(kStatisticsWindow), 56 capture_time_(kStatisticsWindow),
44 encode_time_(kStatisticsWindow), 57 encode_time_(kStatisticsWindow),
45 pending_frames_(0), 58 num_encoding_frames_(0),
59 num_sending_frames_(0),
60 num_unacknowledged_frames_(0),
46 capture_pending_(false), 61 capture_pending_(false),
47 is_paused_(false) { 62 is_paused_(false),
63 next_frame_id_(0) {
48 DCHECK(num_of_processors_); 64 DCHECK(num_of_processors_);
49 } 65 }
50 66
51 CaptureScheduler::~CaptureScheduler() { 67 CaptureScheduler::~CaptureScheduler() {
52 } 68 }
53 69
54 void CaptureScheduler::Start() { 70 void CaptureScheduler::Start() {
55 DCHECK(CalledOnValidThread()); 71 DCHECK(CalledOnValidThread());
56 72
57 ScheduleNextCapture(); 73 ScheduleNextCapture();
(...skipping 13 matching lines...) Expand all
71 } 87 }
72 } 88 }
73 89
74 void CaptureScheduler::OnCaptureCompleted() { 90 void CaptureScheduler::OnCaptureCompleted() {
75 DCHECK(CalledOnValidThread()); 91 DCHECK(CalledOnValidThread());
76 92
77 capture_pending_ = false; 93 capture_pending_ = false;
78 capture_time_.Record( 94 capture_time_.Record(
79 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds()); 95 (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
80 96
97 ++num_encoding_frames_;
98
99 ScheduleNextCapture();
100 }
101
102 void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
103 DCHECK(CalledOnValidThread());
104
105 // Set packet_id for the outgoing packet.
106 packet->set_frame_id(next_frame_id_);
107 ++next_frame_id_;
108
109 // Update internal stats.
110 encode_time_.Record(packet->encode_time_ms());
111
112 --num_encoding_frames_;
113 ++num_sending_frames_;
114 ++num_unacknowledged_frames_;
115
81 ScheduleNextCapture(); 116 ScheduleNextCapture();
82 } 117 }
83 118
84 void CaptureScheduler::OnFrameSent() { 119 void CaptureScheduler::OnFrameSent() {
85 DCHECK(CalledOnValidThread()); 120 DCHECK(CalledOnValidThread());
86 121
87 // Decrement the pending capture count. 122 --num_sending_frames_;
88 pending_frames_--; 123 DCHECK_GE(num_sending_frames_, 0);
89 DCHECK_GE(pending_frames_, 0);
90 124
91 ScheduleNextCapture(); 125 ScheduleNextCapture();
92 } 126 }
93 127
94 void CaptureScheduler::OnFrameEncoded(base::TimeDelta encode_time) { 128 void CaptureScheduler::ProcessVideoAck(scoped_ptr<VideoAck> video_ack) {
95 DCHECK(CalledOnValidThread()); 129 DCHECK(CalledOnValidThread());
96 130
97 encode_time_.Record(encode_time.InMilliseconds()); 131 // Host always sets |frame_id| field to indicated that it expects ACK from the
132 // client. It's assumed that the client doesn't support ACKs until the first
133 // ACK message is received.
134 acks_supported_ = true;
135
136 --num_unacknowledged_frames_;
137 DCHECK_GE(num_unacknowledged_frames_, 0);
138
98 ScheduleNextCapture(); 139 ScheduleNextCapture();
99 } 140 }
100 141
101 void CaptureScheduler::SetTickClockForTest( 142 void CaptureScheduler::SetTickClockForTest(
102 scoped_ptr<base::TickClock> tick_clock) { 143 scoped_ptr<base::TickClock> tick_clock) {
103 tick_clock_ = tick_clock.Pass(); 144 tick_clock_ = tick_clock.Pass();
104 } 145 }
146
105 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) { 147 void CaptureScheduler::SetTimerForTest(scoped_ptr<base::Timer> timer) {
106 capture_timer_ = timer.Pass(); 148 capture_timer_ = timer.Pass();
107 } 149 }
150
108 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) { 151 void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
109 num_of_processors_ = num_of_processors; 152 num_of_processors_ = num_of_processors;
110 } 153 }
111 154
112 void CaptureScheduler::ScheduleNextCapture() { 155 void CaptureScheduler::ScheduleNextCapture() {
113 DCHECK(CalledOnValidThread()); 156 DCHECK(CalledOnValidThread());
114 157
115 if (is_paused_ || pending_frames_ >= kMaxPendingFrames || capture_pending_) 158 if (is_paused_ || capture_pending_ ||
159 num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
116 return; 160 return;
161 }
162
163 if (acks_supported_) {
164 if (num_encoding_frames_ + num_unacknowledged_frames_ >=
165 kMaxUnacknowledgedFrames) {
166 return;
167 }
168 } else {
169 if (num_encoding_frames_ + num_sending_frames_ >= kMaxPendingFrames) {
170 return;
171 }
172 }
117 173
118 // Delay by an amount chosen such that if capture and encode times 174 // Delay by an amount chosen such that if capture and encode times
119 // continue to follow the averages, then we'll consume the target 175 // continue to follow the averages, then we'll consume the target
120 // fraction of CPU across all cores. 176 // fraction of CPU across all cores.
121 base::TimeDelta delay = 177 base::TimeDelta delay =
122 std::max(minimum_interval_, 178 std::max(minimum_interval_,
123 base::TimeDelta::FromMilliseconds( 179 base::TimeDelta::FromMilliseconds(
124 (capture_time_.Average() + encode_time_.Average()) / 180 (capture_time_.Average() + encode_time_.Average()) /
125 (kRecordingCpuConsumption * num_of_processors_))); 181 (kRecordingCpuConsumption * num_of_processors_)));
126 182
127 // Account for the time that has passed since the last capture. 183 // Account for the time that has passed since the last capture.
128 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() - 184 delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
129 last_capture_started_time_)); 185 last_capture_started_time_));
130 186
131 capture_timer_->Start( 187 capture_timer_->Start(
132 FROM_HERE, delay, 188 FROM_HERE, delay,
133 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this))); 189 base::Bind(&CaptureScheduler::CaptureNextFrame, base::Unretained(this)));
134 } 190 }
135 191
136 void CaptureScheduler::CaptureNextFrame() { 192 void CaptureScheduler::CaptureNextFrame() {
137 DCHECK(CalledOnValidThread()); 193 DCHECK(CalledOnValidThread());
138 DCHECK(!is_paused_); 194 DCHECK(!is_paused_);
139 DCHECK(!capture_pending_); 195 DCHECK(!capture_pending_);
140 196
141 pending_frames_++;
142 DCHECK_LE(pending_frames_, kMaxPendingFrames);
143
144 capture_pending_ = true; 197 capture_pending_ = true;
145 last_capture_started_time_ = tick_clock_->NowTicks(); 198 last_capture_started_time_ = tick_clock_->NowTicks();
146 capture_closure_.Run(); 199 capture_closure_.Run();
147 } 200 }
148 201
149 } // namespace remoting 202 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698