| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 current_input_.incoming_bitrate = input->incoming_bitrate; | 126 current_input_.incoming_bitrate = input->incoming_bitrate; |
| 127 } else { | 127 } else { |
| 128 updated_ = true; | 128 updated_ = true; |
| 129 current_input_ = *input; | 129 current_input_ = *input; |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { | 133 void AimdRateControl::SetEstimate(int bitrate_bps, int64_t now_ms) { |
| 134 updated_ = true; | 134 updated_ = true; |
| 135 bitrate_is_initialized_ = true; | 135 bitrate_is_initialized_ = true; |
| 136 current_bitrate_bps_ = ChangeBitrate(bitrate_bps, bitrate_bps, now_ms); | 136 current_bitrate_bps_ = ClampBitrate(bitrate_bps, bitrate_bps); |
| 137 time_last_bitrate_change_ = now_ms; |
| 137 } | 138 } |
| 138 | 139 |
| 139 int AimdRateControl::GetNearMaxIncreaseRateBps() const { | 140 int AimdRateControl::GetNearMaxIncreaseRateBps() const { |
| 140 RTC_DCHECK_GT(current_bitrate_bps_, 0); | 141 RTC_DCHECK_GT(current_bitrate_bps_, 0); |
| 141 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; | 142 double bits_per_frame = static_cast<double>(current_bitrate_bps_) / 30.0; |
| 142 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); | 143 double packets_per_frame = std::ceil(bits_per_frame / (8.0 * 1200.0)); |
| 143 double avg_packet_size_bits = bits_per_frame / packets_per_frame; | 144 double avg_packet_size_bits = bits_per_frame / packets_per_frame; |
| 144 // Approximate the over-use estimator delay to 100 ms. | 145 // Approximate the over-use estimator delay to 100 ms. |
| 145 const int64_t response_time = in_experiment_ ? (rtt_ + 100) * 2 : rtt_ + 100; | 146 const int64_t response_time = in_experiment_ ? (rtt_ + 100) * 2 : rtt_ + 100; |
| 146 | 147 |
| 147 constexpr double kMinIncreaseRateBps = 4000; | 148 constexpr double kMinIncreaseRateBps = 4000; |
| 148 return static_cast<int>(std::max( | 149 return static_cast<int>(std::max( |
| 149 kMinIncreaseRateBps, (avg_packet_size_bits * 1000) / response_time)); | 150 kMinIncreaseRateBps, (avg_packet_size_bits * 1000) / response_time)); |
| 150 } | 151 } |
| 151 | 152 |
| 152 rtc::Optional<int> AimdRateControl::GetLastBitrateDecreaseBps() const { | 153 rtc::Optional<int> AimdRateControl::GetLastBitrateDecreaseBps() const { |
| 153 return last_decrease_; | 154 return last_decrease_; |
| 154 } | 155 } |
| 155 | 156 |
| 156 uint32_t AimdRateControl::ChangeBitrate(uint32_t current_bitrate_bps, | 157 uint32_t AimdRateControl::ChangeBitrate(uint32_t new_bitrate_bps, |
| 157 uint32_t incoming_bitrate_bps, | 158 uint32_t incoming_bitrate_bps, |
| 158 int64_t now_ms) { | 159 int64_t now_ms) { |
| 159 if (!updated_) { | 160 if (!updated_) { |
| 160 return current_bitrate_bps_; | 161 return current_bitrate_bps_; |
| 161 } | 162 } |
| 162 // An over-use should always trigger us to reduce the bitrate, even though | 163 // An over-use should always trigger us to reduce the bitrate, even though |
| 163 // we have not yet established our first estimate. By acting on the over-use, | 164 // we have not yet established our first estimate. By acting on the over-use, |
| 164 // we will end up with a valid estimate. | 165 // we will end up with a valid estimate. |
| 165 if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing) | 166 if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing) |
| 166 return current_bitrate_bps_; | 167 return current_bitrate_bps_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 179 case kRcIncrease: | 180 case kRcIncrease: |
| 180 if (avg_max_bitrate_kbps_ >= 0 && | 181 if (avg_max_bitrate_kbps_ >= 0 && |
| 181 incoming_bitrate_kbps > | 182 incoming_bitrate_kbps > |
| 182 avg_max_bitrate_kbps_ + 3 * std_max_bit_rate) { | 183 avg_max_bitrate_kbps_ + 3 * std_max_bit_rate) { |
| 183 ChangeRegion(kRcMaxUnknown); | 184 ChangeRegion(kRcMaxUnknown); |
| 184 avg_max_bitrate_kbps_ = -1.0; | 185 avg_max_bitrate_kbps_ = -1.0; |
| 185 } | 186 } |
| 186 if (rate_control_region_ == kRcNearMax) { | 187 if (rate_control_region_ == kRcNearMax) { |
| 187 uint32_t additive_increase_bps = | 188 uint32_t additive_increase_bps = |
| 188 AdditiveRateIncrease(now_ms, time_last_bitrate_change_); | 189 AdditiveRateIncrease(now_ms, time_last_bitrate_change_); |
| 189 current_bitrate_bps += additive_increase_bps; | 190 new_bitrate_bps += additive_increase_bps; |
| 190 } else { | 191 } else { |
| 191 uint32_t multiplicative_increase_bps = MultiplicativeRateIncrease( | 192 uint32_t multiplicative_increase_bps = MultiplicativeRateIncrease( |
| 192 now_ms, time_last_bitrate_change_, current_bitrate_bps); | 193 now_ms, time_last_bitrate_change_, new_bitrate_bps); |
| 193 current_bitrate_bps += multiplicative_increase_bps; | 194 new_bitrate_bps += multiplicative_increase_bps; |
| 194 } | 195 } |
| 195 | 196 |
| 196 time_last_bitrate_change_ = now_ms; | 197 time_last_bitrate_change_ = now_ms; |
| 197 break; | 198 break; |
| 198 | 199 |
| 199 case kRcDecrease: | 200 case kRcDecrease: |
| 200 bitrate_is_initialized_ = true; | 201 bitrate_is_initialized_ = true; |
| 201 if (incoming_bitrate_bps < min_configured_bitrate_bps_) { | 202 // Set bit rate to something slightly lower than max |
| 202 current_bitrate_bps = min_configured_bitrate_bps_; | 203 // to get rid of any self-induced delay. |
| 203 } else { | 204 new_bitrate_bps = |
| 204 // Set bit rate to something slightly lower than max | 205 static_cast<uint32_t>(beta_ * incoming_bitrate_bps + 0.5); |
| 205 // to get rid of any self-induced delay. | 206 if (new_bitrate_bps > current_bitrate_bps_) { |
| 206 current_bitrate_bps = static_cast<uint32_t>(beta_ * | 207 // Avoid increasing the rate when over-using. |
| 207 incoming_bitrate_bps + 0.5); | 208 if (rate_control_region_ != kRcMaxUnknown) { |
| 208 if (current_bitrate_bps > current_bitrate_bps_) { | 209 new_bitrate_bps = static_cast<uint32_t>( |
| 209 // Avoid increasing the rate when over-using. | 210 beta_ * avg_max_bitrate_kbps_ * 1000 + 0.5f); |
| 210 if (rate_control_region_ != kRcMaxUnknown) { | |
| 211 current_bitrate_bps = static_cast<uint32_t>( | |
| 212 beta_ * avg_max_bitrate_kbps_ * 1000 + 0.5f); | |
| 213 } | |
| 214 current_bitrate_bps = std::min(current_bitrate_bps, | |
| 215 current_bitrate_bps_); | |
| 216 } | 211 } |
| 217 ChangeRegion(kRcNearMax); | 212 new_bitrate_bps = std::min(new_bitrate_bps, current_bitrate_bps_); |
| 213 } |
| 214 ChangeRegion(kRcNearMax); |
| 218 | 215 |
| 219 if (incoming_bitrate_bps < current_bitrate_bps_) { | 216 if (incoming_bitrate_bps < current_bitrate_bps_) { |
| 220 last_decrease_ = | 217 last_decrease_ = |
| 221 rtc::Optional<int>(current_bitrate_bps_ - current_bitrate_bps); | 218 rtc::Optional<int>(current_bitrate_bps_ - new_bitrate_bps); |
| 222 } | 219 } |
| 223 if (incoming_bitrate_kbps < avg_max_bitrate_kbps_ - | 220 if (incoming_bitrate_kbps < |
| 224 3 * std_max_bit_rate) { | 221 avg_max_bitrate_kbps_ - 3 * std_max_bit_rate) { |
| 225 avg_max_bitrate_kbps_ = -1.0f; | 222 avg_max_bitrate_kbps_ = -1.0f; |
| 226 } | 223 } |
| 227 | 224 |
| 228 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); | 225 UpdateMaxBitRateEstimate(incoming_bitrate_kbps); |
| 229 } | |
| 230 // Stay on hold until the pipes are cleared. | 226 // Stay on hold until the pipes are cleared. |
| 231 ChangeState(kRcHold); | 227 ChangeState(kRcHold); |
| 232 time_last_bitrate_change_ = now_ms; | 228 time_last_bitrate_change_ = now_ms; |
| 233 break; | 229 break; |
| 234 | 230 |
| 235 default: | 231 default: |
| 236 assert(false); | 232 assert(false); |
| 237 } | 233 } |
| 234 return ClampBitrate(new_bitrate_bps, incoming_bitrate_bps); |
| 235 } |
| 236 |
| 237 uint32_t AimdRateControl::ClampBitrate(uint32_t new_bitrate_bps, |
| 238 uint32_t incoming_bitrate_bps) const { |
| 238 // Don't change the bit rate if the send side is too far off. | 239 // Don't change the bit rate if the send side is too far off. |
| 239 // We allow a bit more lag at very low rates to not too easily get stuck if | 240 // We allow a bit more lag at very low rates to not too easily get stuck if |
| 240 // the encoder produces uneven outputs. | 241 // the encoder produces uneven outputs. |
| 241 const uint32_t max_bitrate_bps = | 242 const uint32_t max_bitrate_bps = |
| 242 static_cast<uint32_t>(1.5f * incoming_bitrate_bps) + 10000; | 243 static_cast<uint32_t>(1.5f * incoming_bitrate_bps) + 10000; |
| 243 if (current_bitrate_bps > current_bitrate_bps_ && | 244 if (new_bitrate_bps > current_bitrate_bps_ && |
| 244 current_bitrate_bps > max_bitrate_bps) { | 245 new_bitrate_bps > max_bitrate_bps) { |
| 245 current_bitrate_bps = std::max(current_bitrate_bps_, max_bitrate_bps); | 246 new_bitrate_bps = std::max(current_bitrate_bps_, max_bitrate_bps); |
| 246 time_last_bitrate_change_ = now_ms; | |
| 247 } | 247 } |
| 248 return current_bitrate_bps; | 248 new_bitrate_bps = std::max(new_bitrate_bps, min_configured_bitrate_bps_); |
| 249 return new_bitrate_bps; |
| 249 } | 250 } |
| 250 | 251 |
| 251 uint32_t AimdRateControl::MultiplicativeRateIncrease( | 252 uint32_t AimdRateControl::MultiplicativeRateIncrease( |
| 252 int64_t now_ms, int64_t last_ms, uint32_t current_bitrate_bps) const { | 253 int64_t now_ms, int64_t last_ms, uint32_t current_bitrate_bps) const { |
| 253 double alpha = 1.08; | 254 double alpha = 1.08; |
| 254 if (last_ms > -1) { | 255 if (last_ms > -1) { |
| 255 int time_since_last_update_ms = std::min(static_cast<int>(now_ms - last_ms), | 256 int time_since_last_update_ms = std::min(static_cast<int>(now_ms - last_ms), |
| 256 1000); | 257 1000); |
| 257 alpha = pow(alpha, time_since_last_update_ms / 1000.0); | 258 alpha = pow(alpha, time_since_last_update_ms / 1000.0); |
| 258 } | 259 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 } | 315 } |
| 315 | 316 |
| 316 void AimdRateControl::ChangeRegion(RateControlRegion region) { | 317 void AimdRateControl::ChangeRegion(RateControlRegion region) { |
| 317 rate_control_region_ = region; | 318 rate_control_region_ = region; |
| 318 } | 319 } |
| 319 | 320 |
| 320 void AimdRateControl::ChangeState(RateControlState new_state) { | 321 void AimdRateControl::ChangeState(RateControlState new_state) { |
| 321 rate_control_state_ = new_state; | 322 rate_control_state_ = new_state; |
| 322 } | 323 } |
| 323 } // namespace webrtc | 324 } // namespace webrtc |
| OLD | NEW |