| 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 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 AimdRateControl::AimdRateControl() | 29 AimdRateControl::AimdRateControl() |
| 30 : min_configured_bitrate_bps_(congestion_controller::GetMinBitrateBps()), | 30 : min_configured_bitrate_bps_(congestion_controller::GetMinBitrateBps()), |
| 31 max_configured_bitrate_bps_(30000000), | 31 max_configured_bitrate_bps_(30000000), |
| 32 current_bitrate_bps_(max_configured_bitrate_bps_), | 32 current_bitrate_bps_(max_configured_bitrate_bps_), |
| 33 avg_max_bitrate_kbps_(-1.0f), | 33 avg_max_bitrate_kbps_(-1.0f), |
| 34 var_max_bitrate_kbps_(0.4f), | 34 var_max_bitrate_kbps_(0.4f), |
| 35 rate_control_state_(kRcHold), | 35 rate_control_state_(kRcHold), |
| 36 rate_control_region_(kRcMaxUnknown), | 36 rate_control_region_(kRcMaxUnknown), |
| 37 time_last_bitrate_change_(-1), | 37 time_last_bitrate_change_(-1), |
| 38 current_input_(kBwNormal, rtc::Optional<uint32_t>(), 1.0), | |
| 39 updated_(false), | |
| 40 time_first_incoming_estimate_(-1), | 38 time_first_incoming_estimate_(-1), |
| 41 bitrate_is_initialized_(false), | 39 bitrate_is_initialized_(false), |
| 42 beta_(0.85f), | 40 beta_(0.85f), |
| 43 rtt_(kDefaultRttMs), | 41 rtt_(kDefaultRttMs), |
| 44 in_experiment_(!AdaptiveThresholdExperimentIsDisabled()) {} | 42 in_experiment_(!AdaptiveThresholdExperimentIsDisabled()) {} |
| 45 | 43 |
| 46 AimdRateControl::~AimdRateControl() {} | 44 AimdRateControl::~AimdRateControl() {} |
| 47 | 45 |
| 48 void AimdRateControl::SetStartBitrate(int start_bitrate_bps) { | 46 void AimdRateControl::SetStartBitrate(int start_bitrate_bps) { |
| 49 current_bitrate_bps_ = start_bitrate_bps; | 47 current_bitrate_bps_ = start_bitrate_bps; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 const uint32_t threshold = static_cast<uint32_t> (0.5 * LatestEstimate()); | 81 const uint32_t threshold = static_cast<uint32_t> (0.5 * LatestEstimate()); |
| 84 return incoming_bitrate_bps < threshold; | 82 return incoming_bitrate_bps < threshold; |
| 85 } | 83 } |
| 86 return false; | 84 return false; |
| 87 } | 85 } |
| 88 | 86 |
| 89 uint32_t AimdRateControl::LatestEstimate() const { | 87 uint32_t AimdRateControl::LatestEstimate() const { |
| 90 return current_bitrate_bps_; | 88 return current_bitrate_bps_; |
| 91 } | 89 } |
| 92 | 90 |
| 93 uint32_t AimdRateControl::UpdateBandwidthEstimate(int64_t now_ms) { | |
| 94 current_bitrate_bps_ = ChangeBitrate( | |
| 95 current_bitrate_bps_, | |
| 96 current_input_.incoming_bitrate.value_or(current_bitrate_bps_), now_ms); | |
| 97 return current_bitrate_bps_; | |
| 98 } | |
| 99 | |
| 100 void AimdRateControl::SetRtt(int64_t rtt) { | 91 void AimdRateControl::SetRtt(int64_t rtt) { |
| 101 rtt_ = rtt; | 92 rtt_ = rtt; |
| 102 } | 93 } |
| 103 | 94 |
| 104 void AimdRateControl::Update(const RateControlInput* input, int64_t now_ms) { | 95 uint32_t AimdRateControl::Update(const RateControlInput* input, |
| 96 int64_t now_ms) { |
| 105 RTC_CHECK(input); | 97 RTC_CHECK(input); |
| 106 | 98 |
| 107 // Set the initial bit rate value to what we're receiving the first half | 99 // Set the initial bit rate value to what we're receiving the first half |
| 108 // second. | 100 // second. |
| 109 if (!bitrate_is_initialized_) { | 101 if (!bitrate_is_initialized_) { |
| 110 const int64_t kInitializationTimeMs = 5000; | 102 const int64_t kInitializationTimeMs = 5000; |
| 111 RTC_DCHECK_LE(kBitrateWindowMs, kInitializationTimeMs); | 103 RTC_DCHECK_LE(kBitrateWindowMs, kInitializationTimeMs); |
| 112 if (time_first_incoming_estimate_ < 0) { | 104 if (time_first_incoming_estimate_ < 0) { |
| 113 if (input->incoming_bitrate) | 105 if (input->incoming_bitrate) |
| 114 time_first_incoming_estimate_ = now_ms; | 106 time_first_incoming_estimate_ = now_ms; |
| 115 } else if (now_ms - time_first_incoming_estimate_ > kInitializationTimeMs && | 107 } else if (now_ms - time_first_incoming_estimate_ > kInitializationTimeMs && |
| 116 input->incoming_bitrate) { | 108 input->incoming_bitrate) { |
| 117 current_bitrate_bps_ = *input->incoming_bitrate; | 109 current_bitrate_bps_ = *input->incoming_bitrate; |
| 118 bitrate_is_initialized_ = true; | 110 bitrate_is_initialized_ = true; |
| 119 } | 111 } |
| 120 } | 112 } |
| 121 | 113 |
| 122 if (updated_ && current_input_.bw_state == kBwOverusing) { | 114 current_bitrate_bps_ = ChangeBitrate(current_bitrate_bps_, *input, now_ms); |
| 123 // Only update delay factor and incoming bit rate. We always want to react | 115 return current_bitrate_bps_; |
| 124 // on an over-use. | |
| 125 current_input_.noise_var = input->noise_var; | |
| 126 current_input_.incoming_bitrate = input->incoming_bitrate; | |
| 127 } else { | |
| 128 updated_ = true; | |
| 129 current_input_ = *input; | |
| 130 } | |
| 131 } | 116 } |
| 132 | 117 |
| 133 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { | 118 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { |
| 134 updated_ = true; | |
| 135 bitrate_is_initialized_ = true; | 119 bitrate_is_initialized_ = true; |
| 136 current_bitrate_bps_ = ClampBitrate(bitrate_bps, bitrate_bps); | 120 current_bitrate_bps_ = ClampBitrate(bitrate_bps, bitrate_bps); |
| 137 time_last_bitrate_change_ = now_ms; | 121 time_last_bitrate_change_ = now_ms; |
| 138 } | 122 } |
| 139 | 123 |
| 140 int AimdRateControl::GetNearMaxIncreaseRateBps() const { | 124 int AimdRateControl::GetNearMaxIncreaseRateBps() const { |
| 141 RTC_DCHECK_GT(current_bitrate_bps_, 0); | 125 RTC_DCHECK_GT(current_bitrate_bps_, 0); |
| 142 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; | 126 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; |
| 143 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); | 127 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); |
| 144 double avg_packet_size_bits = bits_per_frame / packets_per_frame; | 128 double avg_packet_size_bits = bits_per_frame / packets_per_frame; |
| 145 // Approximate the over-use estimator delay to 100 ms. | 129 // Approximate the over-use estimator delay to 100 ms. |
| 146 const int64_t response_time = in_experiment_ ? (rtt_ + 100) * 2 : rtt_ + 100; | 130 const int64_t response_time = in_experiment_ ? (rtt_ + 100) * 2 : rtt_ + 100; |
| 147 | 131 |
| 148 constexpr double kMinIncreaseRateBps = 4000; | 132 constexpr double kMinIncreaseRateBps = 4000; |
| 149 return static_cast<int>(std::max( | 133 return static_cast<int>(std::max( |
| 150 kMinIncreaseRateBps, (avg_packet_size_bits * 1000) / response_time)); | 134 kMinIncreaseRateBps, (avg_packet_size_bits * 1000) / response_time)); |
| 151 } | 135 } |
| 152 | 136 |
| 153 rtc::Optional<int> AimdRateControl::GetLastBitrateDecreaseBps() const { | 137 rtc::Optional<int> AimdRateControl::GetLastBitrateDecreaseBps() const { |
| 154 return last_decrease_; | 138 return last_decrease_; |
| 155 } | 139 } |
| 156 | 140 |
| 157 uint32_t AimdRateControl::ChangeBitrate(uint32_t new_bitrate_bps, | 141 uint32_t AimdRateControl::ChangeBitrate(uint32_t new_bitrate_bps, |
| 158 uint32_t incoming_bitrate_bps, | 142 const RateControlInput& input, |
| 159 int64_t now_ms) { | 143 int64_t now_ms) { |
| 160 if (!updated_) { | 144 uint32_t incoming_bitrate_bps = |
| 161 return current_bitrate_bps_; | 145 input.incoming_bitrate.value_or(current_bitrate_bps_); |
| 162 } | 146 |
| 163 // An over-use should always trigger us to reduce the bitrate, even though | 147 // An over-use should always trigger us to reduce the bitrate, even though |
| 164 // we have not yet established our first estimate. By acting on the over-use, | 148 // we have not yet established our first estimate. By acting on the over-use, |
| 165 // we will end up with a valid estimate. | 149 // we will end up with a valid estimate. |
| 166 if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing) | 150 if (!bitrate_is_initialized_ && input.bw_state != kBwOverusing) |
| 167 return current_bitrate_bps_; | 151 return current_bitrate_bps_; |
| 168 updated_ = false; | 152 |
| 169 ChangeState(current_input_, now_ms); | 153 ChangeState(input, now_ms); |
| 170 // Calculated here because it's used in multiple places. | 154 // Calculated here because it's used in multiple places. |
| 171 const float incoming_bitrate_kbps = incoming_bitrate_bps / 1000.0f; | 155 const float incoming_bitrate_kbps = incoming_bitrate_bps / 1000.0f; |
| 172 // Calculate the max bit rate std dev given the normalized | 156 // Calculate the max bit rate std dev given the normalized |
| 173 // variance and the current incoming bit rate. | 157 // variance and the current incoming bit rate. |
| 174 const float std_max_bit_rate = sqrt(var_max_bitrate_kbps_ * | 158 const float std_max_bit_rate = sqrt(var_max_bitrate_kbps_ * |
| 175 avg_max_bitrate_kbps_); | 159 avg_max_bitrate_kbps_); |
| 176 switch (rate_control_state_) { | 160 switch (rate_control_state_) { |
| 177 case kRcHold: | 161 case kRcHold: |
| 178 break; | 162 break; |
| 179 | 163 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 last_decrease_ = | 201 last_decrease_ = |
| 218 rtc::Optional<int>(current_bitrate_bps_ - new_bitrate_bps); | 202 rtc::Optional<int>(current_bitrate_bps_ - new_bitrate_bps); |
| 219 } | 203 } |
| 220 if (incoming_bitrate_kbps < | 204 if (incoming_bitrate_kbps < |
| 221 avg_max_bitrate_kbps_ - 3 * std_max_bit_rate) { | 205 avg_max_bitrate_kbps_ - 3 * std_max_bit_rate) { |
| 222 avg_max_bitrate_kbps_ = -1.0f; | 206 avg_max_bitrate_kbps_ = -1.0f; |
| 223 } | 207 } |
| 224 | 208 |
| 225 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); | 209 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); |
| 226 // Stay on hold until the pipes are cleared. | 210 // Stay on hold until the pipes are cleared. |
| 227 ChangeState(kRcHold); | 211 rate_control_state_ = kRcHold; |
| 228 time_last_bitrate_change_ = now_ms; | 212 time_last_bitrate_change_ = now_ms; |
| 229 break; | 213 break; |
| 230 | 214 |
| 231 default: | 215 default: |
| 232 assert(false); | 216 assert(false); |
| 233 } | 217 } |
| 234 return ClampBitrate(new_bitrate_bps, incoming_bitrate_bps); | 218 return ClampBitrate(new_bitrate_bps, incoming_bitrate_bps); |
| 235 } | 219 } |
| 236 | 220 |
| 237 uint32_t AimdRateControl::ClampBitrate(uint32_t new_bitrate_bps, | 221 uint32_t AimdRateControl::ClampBitrate(uint32_t new_bitrate_bps, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 var_max_bitrate_kbps_ = 0.4f; | 271 var_max_bitrate_kbps_ = 0.4f; |
| 288 } | 272 } |
| 289 // 2.5f ~= 35 kbit/s at 500 kbit/s | 273 // 2.5f ~= 35 kbit/s at 500 kbit/s |
| 290 if (var_max_bitrate_kbps_ > 2.5f) { | 274 if (var_max_bitrate_kbps_ > 2.5f) { |
| 291 var_max_bitrate_kbps_ = 2.5f; | 275 var_max_bitrate_kbps_ = 2.5f; |
| 292 } | 276 } |
| 293 } | 277 } |
| 294 | 278 |
| 295 void AimdRateControl::ChangeState(const RateControlInput& input, | 279 void AimdRateControl::ChangeState(const RateControlInput& input, |
| 296 int64_t now_ms) { | 280 int64_t now_ms) { |
| 297 switch (current_input_.bw_state) { | 281 switch (input.bw_state) { |
| 298 case kBwNormal: | 282 case kBwNormal: |
| 299 if (rate_control_state_ == kRcHold) { | 283 if (rate_control_state_ == kRcHold) { |
| 300 time_last_bitrate_change_ = now_ms; | 284 time_last_bitrate_change_ = now_ms; |
| 301 ChangeState(kRcIncrease); | 285 rate_control_state_ = kRcIncrease; |
| 302 } | 286 } |
| 303 break; | 287 break; |
| 304 case kBwOverusing: | 288 case kBwOverusing: |
| 305 if (rate_control_state_ != kRcDecrease) { | 289 if (rate_control_state_ != kRcDecrease) { |
| 306 ChangeState(kRcDecrease); | 290 rate_control_state_ = kRcDecrease; |
| 307 } | 291 } |
| 308 break; | 292 break; |
| 309 case kBwUnderusing: | 293 case kBwUnderusing: |
| 310 ChangeState(kRcHold); | 294 rate_control_state_ = kRcHold; |
| 311 break; | 295 break; |
| 312 default: | 296 default: |
| 313 assert(false); | 297 assert(false); |
| 314 } | 298 } |
| 315 } | 299 } |
| 316 | 300 |
| 317 void AimdRateControl::ChangeRegion(RateControlRegion region) { | 301 void AimdRateControl::ChangeRegion(RateControlRegion region) { |
| 318 rate_control_region_ = region; | 302 rate_control_region_ = region; |
| 319 } | 303 } |
| 320 | 304 |
| 321 void AimdRateControl::ChangeState(RateControlState new_state) { | |
| 322 rate_control_state_ = new_state; | |
| 323 } | |
| 324 } // namespace webrtc | 305 } // namespace webrtc |
| OLD | NEW |