OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 current_input_ = *input; | 123 current_input_ = *input; |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { | 127 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { |
128 updated_ = true; | 128 updated_ = true; |
129 bitrate_is_initialized_ = true; | 129 bitrate_is_initialized_ = true; |
130 current_bitrate_bps_ = ChangeBitrate(bitrate_bps, bitrate_bps, now_ms); | 130 current_bitrate_bps_ = ChangeBitrate(bitrate_bps, bitrate_bps, now_ms); |
131 } | 131 } |
132 | 132 |
| 133 int AimdRateControl::GetNearMaxIncreaseRateBps() const { |
| 134 RTC_DCHECK_GT(current_bitrate_bps_, 0); |
| 135 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; |
| 136 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); |
| 137 double avg_packet_size_bits = bits_per_frame / packets_per_frame; |
| 138 // Approximate the over-use estimator delay to 100 ms. |
| 139 const int64_t response_time = in_experiment_ ? (rtt_ + 100) * 2 : rtt_ + 100; |
| 140 |
| 141 constexpr double kMinIncreaseRateBps = 4000; |
| 142 return static_cast<int>(std::max( |
| 143 kMinIncreaseRateBps, (avg_packet_size_bits * 1000) / response_time)); |
| 144 } |
| 145 |
| 146 rtc::Optional<int> AimdRateControl::GetLastBitrateDecreaseBps() const { |
| 147 return last_decrease_; |
| 148 } |
| 149 |
133 uint32_t AimdRateControl::ChangeBitrate(uint32_t current_bitrate_bps, | 150 uint32_t AimdRateControl::ChangeBitrate(uint32_t current_bitrate_bps, |
134 uint32_t incoming_bitrate_bps, | 151 uint32_t incoming_bitrate_bps, |
135 int64_t now_ms) { | 152 int64_t now_ms) { |
136 if (!updated_) { | 153 if (!updated_) { |
137 return current_bitrate_bps_; | 154 return current_bitrate_bps_; |
138 } | 155 } |
139 // An over-use should always trigger us to reduce the bitrate, even though | 156 // An over-use should always trigger us to reduce the bitrate, even though |
140 // we have not yet established our first estimate. By acting on the over-use, | 157 // we have not yet established our first estimate. By acting on the over-use, |
141 // we will end up with a valid estimate. | 158 // we will end up with a valid estimate. |
142 if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing) | 159 if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing) |
(...skipping 11 matching lines...) Expand all Loading... |
154 break; | 171 break; |
155 | 172 |
156 case kRcIncrease: | 173 case kRcIncrease: |
157 if (avg_max_bitrate_kbps_ >= 0 && | 174 if (avg_max_bitrate_kbps_ >= 0 && |
158 incoming_bitrate_kbps > | 175 incoming_bitrate_kbps > |
159 avg_max_bitrate_kbps_ + 3 * std_max_bit_rate) { | 176 avg_max_bitrate_kbps_ + 3 * std_max_bit_rate) { |
160 ChangeRegion(kRcMaxUnknown); | 177 ChangeRegion(kRcMaxUnknown); |
161 avg_max_bitrate_kbps_ = -1.0; | 178 avg_max_bitrate_kbps_ = -1.0; |
162 } | 179 } |
163 if (rate_control_region_ == kRcNearMax) { | 180 if (rate_control_region_ == kRcNearMax) { |
164 // Approximate the over-use estimator delay to 100 ms. | 181 uint32_t additive_increase_bps = |
165 const int64_t response_time = rtt_ + 100; | 182 AdditiveRateIncrease(now_ms, time_last_bitrate_change_); |
166 uint32_t additive_increase_bps = AdditiveRateIncrease( | |
167 now_ms, time_last_bitrate_change_, response_time); | |
168 current_bitrate_bps += additive_increase_bps; | 183 current_bitrate_bps += additive_increase_bps; |
169 | |
170 } else { | 184 } else { |
171 uint32_t multiplicative_increase_bps = MultiplicativeRateIncrease( | 185 uint32_t multiplicative_increase_bps = MultiplicativeRateIncrease( |
172 now_ms, time_last_bitrate_change_, current_bitrate_bps); | 186 now_ms, time_last_bitrate_change_, current_bitrate_bps); |
173 current_bitrate_bps += multiplicative_increase_bps; | 187 current_bitrate_bps += multiplicative_increase_bps; |
174 } | 188 } |
175 | 189 |
176 time_last_bitrate_change_ = now_ms; | 190 time_last_bitrate_change_ = now_ms; |
177 break; | 191 break; |
178 | 192 |
179 case kRcDecrease: | 193 case kRcDecrease: |
180 bitrate_is_initialized_ = true; | 194 bitrate_is_initialized_ = true; |
181 if (incoming_bitrate_bps < min_configured_bitrate_bps_) { | 195 if (incoming_bitrate_bps < min_configured_bitrate_bps_) { |
182 current_bitrate_bps = min_configured_bitrate_bps_; | 196 current_bitrate_bps = min_configured_bitrate_bps_; |
183 } else { | 197 } else { |
184 // Set bit rate to something slightly lower than max | 198 // Set bit rate to something slightly lower than max |
185 // to get rid of any self-induced delay. | 199 // to get rid of any self-induced delay. |
186 current_bitrate_bps = static_cast<uint32_t>(beta_ * | 200 current_bitrate_bps = static_cast<uint32_t>(beta_ * |
187 incoming_bitrate_bps + 0.5); | 201 incoming_bitrate_bps + 0.5); |
188 if (current_bitrate_bps > current_bitrate_bps_) { | 202 if (current_bitrate_bps > current_bitrate_bps_) { |
189 // Avoid increasing the rate when over-using. | 203 // Avoid increasing the rate when over-using. |
190 if (rate_control_region_ != kRcMaxUnknown) { | 204 if (rate_control_region_ != kRcMaxUnknown) { |
191 current_bitrate_bps = static_cast<uint32_t>( | 205 current_bitrate_bps = static_cast<uint32_t>( |
192 beta_ * avg_max_bitrate_kbps_ * 1000 + 0.5f); | 206 beta_ * avg_max_bitrate_kbps_ * 1000 + 0.5f); |
193 } | 207 } |
194 current_bitrate_bps = std::min(current_bitrate_bps, | 208 current_bitrate_bps = std::min(current_bitrate_bps, |
195 current_bitrate_bps_); | 209 current_bitrate_bps_); |
196 } | 210 } |
197 ChangeRegion(kRcNearMax); | 211 ChangeRegion(kRcNearMax); |
198 | 212 |
| 213 if (incoming_bitrate_bps < current_bitrate_bps_) { |
| 214 last_decrease_ = |
| 215 rtc::Optional<int>(current_bitrate_bps_ - current_bitrate_bps); |
| 216 } |
199 if (incoming_bitrate_kbps < avg_max_bitrate_kbps_ - | 217 if (incoming_bitrate_kbps < avg_max_bitrate_kbps_ - |
200 3 * std_max_bit_rate) { | 218 3 * std_max_bit_rate) { |
201 avg_max_bitrate_kbps_ = -1.0f; | 219 avg_max_bitrate_kbps_ = -1.0f; |
202 } | 220 } |
203 | 221 |
204 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); | 222 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); |
205 } | 223 } |
206 // Stay on hold until the pipes are cleared. | 224 // Stay on hold until the pipes are cleared. |
207 ChangeState(kRcHold); | 225 ChangeState(kRcHold); |
208 time_last_bitrate_change_ = now_ms; | 226 time_last_bitrate_change_ = now_ms; |
(...skipping 18 matching lines...) Expand all Loading... |
227 if (last_ms > -1) { | 245 if (last_ms > -1) { |
228 int time_since_last_update_ms = std::min(static_cast<int>(now_ms - last_ms), | 246 int time_since_last_update_ms = std::min(static_cast<int>(now_ms - last_ms), |
229 1000); | 247 1000); |
230 alpha = pow(alpha, time_since_last_update_ms / 1000.0); | 248 alpha = pow(alpha, time_since_last_update_ms / 1000.0); |
231 } | 249 } |
232 uint32_t multiplicative_increase_bps = std::max( | 250 uint32_t multiplicative_increase_bps = std::max( |
233 current_bitrate_bps * (alpha - 1.0), 1000.0); | 251 current_bitrate_bps * (alpha - 1.0), 1000.0); |
234 return multiplicative_increase_bps; | 252 return multiplicative_increase_bps; |
235 } | 253 } |
236 | 254 |
237 uint32_t AimdRateControl::AdditiveRateIncrease( | 255 uint32_t AimdRateControl::AdditiveRateIncrease(int64_t now_ms, |
238 int64_t now_ms, int64_t last_ms, int64_t response_time_ms) const { | 256 int64_t last_ms) const { |
239 assert(response_time_ms > 0); | 257 return static_cast<uint32_t>((now_ms - last_ms) * |
240 double beta = 0.0; | 258 GetNearMaxIncreaseRateBps() / 1000); |
241 if (last_ms > 0) { | |
242 beta = std::min((now_ms - last_ms) / static_cast<double>(response_time_ms), | |
243 1.0); | |
244 if (in_experiment_) | |
245 beta /= 2.0; | |
246 } | |
247 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; | |
248 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); | |
249 double avg_packet_size_bits = bits_per_frame / packets_per_frame; | |
250 uint32_t additive_increase_bps = std::max( | |
251 1000.0, beta * avg_packet_size_bits); | |
252 return additive_increase_bps; | |
253 } | 259 } |
254 | 260 |
255 void AimdRateControl::UpdateMaxBitRateEstimate(float incoming_bitrate_kbps) { | 261 void AimdRateControl::UpdateMaxBitRateEstimate(float incoming_bitrate_kbps) { |
256 const float alpha = 0.05f; | 262 const float alpha = 0.05f; |
257 if (avg_max_bitrate_kbps_ == -1.0f) { | 263 if (avg_max_bitrate_kbps_ == -1.0f) { |
258 avg_max_bitrate_kbps_ = incoming_bitrate_kbps; | 264 avg_max_bitrate_kbps_ = incoming_bitrate_kbps; |
259 } else { | 265 } else { |
260 avg_max_bitrate_kbps_ = (1 - alpha) * avg_max_bitrate_kbps_ + | 266 avg_max_bitrate_kbps_ = (1 - alpha) * avg_max_bitrate_kbps_ + |
261 alpha * incoming_bitrate_kbps; | 267 alpha * incoming_bitrate_kbps; |
262 } | 268 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 } | 305 } |
300 | 306 |
301 void AimdRateControl::ChangeRegion(RateControlRegion region) { | 307 void AimdRateControl::ChangeRegion(RateControlRegion region) { |
302 rate_control_region_ = region; | 308 rate_control_region_ = region; |
303 } | 309 } |
304 | 310 |
305 void AimdRateControl::ChangeState(RateControlState new_state) { | 311 void AimdRateControl::ChangeState(RateControlState new_state) { |
306 rate_control_state_ = new_state; | 312 rate_control_state_ = new_state; |
307 } | 313 } |
308 } // namespace webrtc | 314 } // namespace webrtc |
OLD | NEW |