1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
| DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( const webrtc::TransportPacketsFeedback& msg, absl::optional<webrtc::DataRate> acked_bitrate) { auto packet_feedback_vector = msg.SortedByReceiveTime(); if (packet_feedback_vector.empty()) { return DelayBasedBwe::Result(); }
bool recover_from_overusing = false; webrtc::BandwidthUsage prev_detector_state = video_delay_detector_->State(); for (const auto& packet_feedback : packet_feedback_vector) { IncomingPacketFeedback(packet_feedback, msg.feedback_time); if (prev_detector_state == webrtc::BandwidthUsage::kBwUnderusing && video_delay_detector_->State() == webrtc::BandwidthUsage::kBwNormal) { recover_from_overusing = true; } prev_detector_state = video_delay_detector_->State(); }
return MaybeUpdateEstimate(acked_bitrate, recover_from_overusing, msg.feedback_time); }
DelayBasedBwe::Result DelayBasedBwe::MaybeUpdateEstimate( absl::optional<webrtc::DataRate> acked_bitrate, bool recover_from_overusing, webrtc::Timestamp at_time) { Result result; if (video_delay_detector_->State() == webrtc::BandwidthUsage::kBwOverusing) { if (acked_bitrate && rate_control_.TimeToReduceFurther(at_time, *acked_bitrate)) { result.updated = UpdateEstimate(acked_bitrate, at_time, &result.target_bitrate); } else if (!acked_bitrate && rate_control_.ValidEstimate() && rate_control_.InitialTimeToReduceFurther(at_time)) { rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2, at_time); result.updated = true; result.target_bitrate = rate_control_.LatestEstimate(); } } else { result.updated = UpdateEstimate(acked_bitrate, at_time, &result.target_bitrate); }
return result; }
bool DelayBasedBwe::UpdateEstimate(absl::optional<webrtc::DataRate> acked_bitrate, webrtc::Timestamp at_time, webrtc::DataRate* target_bitrate) { *target_bitrate = rate_control_.Update(acked_bitrate, video_delay_detector_->State(), at_time); return rate_control_.ValidEstimate(); }
#include <api/units/data_rate.h> #include <api/units/timestamp.h> #include <api/network_state_predictor.h> #include <absl/types/optional.h>
#include "xrtc/rtc/modules/congestion_controller/goog_cc/link_capacity_estimator.h"
namespace xrtc {
class AimdRateControl { public: AimdRateControl(); ~AimdRateControl();
void SetStartBitrate(webrtc::DataRate start_bitrate); bool ValidEstimate() const;
bool TimeToReduceFurther(webrtc::Timestamp at_time, webrtc::DataRate estimated_throughput) const; bool InitialTimeToReduceFurther(webrtc::Timestamp at_time) const; webrtc::DataRate LatestEstimate() const; void SetRtt(webrtc::TimeDelta rtt); void SetEstimate(webrtc::DataRate new_bitrate, webrtc::Timestamp at_time); webrtc::DataRate Update(absl::optional<webrtc::DataRate> throughput_estimate, webrtc::BandwidthUsage state, webrtc::Timestamp at_time);
private: enum class RateControlState { kRcHold, kRcIncrease, kRcDecrease, };
webrtc::DataRate ClampBitrate(webrtc::DataRate new_bitrate); webrtc::DataRate AdditiveRateIncrease(webrtc::Timestamp at_time, webrtc::Timestamp last_time); webrtc::DataRate MultiplicativeRateIncrease(webrtc::Timestamp at_time, webrtc::Timestamp last_time); double GetNearMaxIncreaseRateBpsPerSecond() const; void ChangeBitrate(absl::optional<webrtc::DataRate> throughput_estimate, webrtc::BandwidthUsage state, webrtc::Timestamp at_time); void ChangeState(webrtc::BandwidthUsage state, webrtc::Timestamp at_time);
private: webrtc::DataRate min_config_bitrate_; webrtc::DataRate max_config_bitrate_; webrtc::DataRate current_bitrate_; webrtc::DataRate latest_estimated_throughput_; bool bitrate_is_init_ = false; webrtc::TimeDelta rtt_; double beta_; webrtc::Timestamp time_last_bitrate_change_ = webrtc::Timestamp::MinusInfinity(); webrtc::Timestamp time_last_bitrate_decrease_ = webrtc::Timestamp::MinusInfinity(); webrtc::Timestamp time_first_throughput_estimate_ = webrtc::Timestamp::MinusInfinity(); RateControlState rate_control_state_ = RateControlState::kRcHold; LinkCapacityEstimator link_capacity_; };
}
#include "xrtc/rtc/modules/congestion_controller/goog_cc/aimd_rate_control.h" #include <rtc_base/logging.h>
namespace xrtc { namespace {
constexpr webrtc::TimeDelta kDefaultRtt = webrtc::TimeDelta::Millis(200); const double kDefaultBackOffFractor = 0.85;
}
AimdRateControl::AimdRateControl() : min_config_bitrate_(webrtc::DataRate::KilobitsPerSec(5)), max_config_bitrate_(webrtc::DataRate::KilobitsPerSec(30000)), current_bitrate_(max_config_bitrate_), latest_estimated_throughput_(current_bitrate_), rtt_(kDefaultRtt), beta_(kDefaultBackOffFractor) { }
AimdRateControl::~AimdRateControl() { }
void AimdRateControl::SetStartBitrate(webrtc::DataRate start_bitrate) { current_bitrate_ = start_bitrate; bitrate_is_init_ = true; }
bool AimdRateControl::ValidEstimate() const { return bitrate_is_init_; }
bool AimdRateControl::TimeToReduceFurther(webrtc::Timestamp at_time, webrtc::DataRate estimated_throughput) const { webrtc::TimeDelta bitrate_reduction_interval = rtt_.Clamped(webrtc::TimeDelta::Millis(10), webrtc::TimeDelta::Millis(200)); if (at_time - time_last_bitrate_change_ >= rtt_) { return true; }
if (ValidEstimate()) { webrtc::DataRate threshold = LatestEstimate() * 0.5; return estimated_throughput < threshold; }
return false; }
bool AimdRateControl::InitialTimeToReduceFurther( webrtc::Timestamp at_time) const { return ValidEstimate() && TimeToReduceFurther(at_time, LatestEstimate() / 2 - webrtc::DataRate::BitsPerSec(1)); }
webrtc::DataRate AimdRateControl::LatestEstimate() const { return current_bitrate_; }
void AimdRateControl::SetRtt(webrtc::TimeDelta rtt) { rtt_ = rtt; RTC_LOG(LS_WARNING) << "==========rtt: " << rtt.ms(); }
void AimdRateControl::SetEstimate(webrtc::DataRate new_bitrate, webrtc::Timestamp at_time) { bitrate_is_init_ = true; webrtc::DataRate prev_bitrate = current_bitrate_; current_bitrate_ = ClampBitrate(new_bitrate); time_last_bitrate_change_ = at_time; if (current_bitrate_ < prev_bitrate) { time_last_bitrate_decrease_ = at_time; } }
webrtc::DataRate AimdRateControl::Update( absl::optional<webrtc::DataRate> throughput_estimate, webrtc::BandwidthUsage state, webrtc::Timestamp at_time) { if (!bitrate_is_init_) { const webrtc::TimeDelta kInitTime = webrtc::TimeDelta::Seconds(5); if (time_first_throughput_estimate_.IsInfinite()) { time_first_throughput_estimate_ = at_time; } else if (at_time - time_first_throughput_estimate_ > kInitTime && throughput_estimate) { current_bitrate_ = *throughput_estimate; bitrate_is_init_ = true; } }
ChangeBitrate(throughput_estimate, state, at_time);
return current_bitrate_; }
webrtc::DataRate AimdRateControl::ClampBitrate(webrtc::DataRate new_bitrate) { new_bitrate = std::max(new_bitrate, min_config_bitrate_); return new_bitrate; }
webrtc::DataRate AimdRateControl::AdditiveRateIncrease( webrtc::Timestamp at_time, webrtc::Timestamp last_time) { double time_delta_seconds = (at_time - last_time).seconds<double>(); double increase_rate_bps = GetNearMaxIncreaseRateBpsPerSecond() * time_delta_seconds; return webrtc::DataRate::BitsPerSec(increase_rate_bps); }
webrtc::DataRate AimdRateControl::MultiplicativeRateIncrease( webrtc::Timestamp at_time, webrtc::Timestamp last_time) { double alpha = 1.08; if (last_time.IsFinite()) { double time_since_last_update = (at_time - last_time).seconds<double>(); alpha = pow(alpha, std::min(time_since_last_update, 1.0)); }
webrtc::DataRate multiplicative_increase = std::max(current_bitrate_ * (alpha - 1.0), webrtc::DataRate::BitsPerSec(1000));
return multiplicative_increase; }
double AimdRateControl::GetNearMaxIncreaseRateBpsPerSecond() const { const webrtc::TimeDelta kFrameInterval = webrtc::TimeDelta::Seconds(1) / 30; webrtc::DataSize frame_size = current_bitrate_ * kFrameInterval; const webrtc::DataSize packet_size = webrtc::DataSize::Bytes(1200); double packets_per_frame = std::ceil(frame_size / packet_size); webrtc::DataSize avg_packet_size = frame_size / packets_per_frame;
webrtc::TimeDelta response_time = rtt_ + webrtc::TimeDelta::Millis(100); double increase_rate_bps_per_second = (avg_packet_size / response_time).bps<double>(); const double kMinIncreaseRateBps = 4000;
return std::max(kMinIncreaseRateBps, increase_rate_bps_per_second); }
void AimdRateControl::ChangeBitrate( absl::optional<webrtc::DataRate> acked_bitrate, webrtc::BandwidthUsage state, webrtc::Timestamp at_time) { absl::optional<webrtc::DataRate> new_bitrate; webrtc::DataRate estimated_throughput = acked_bitrate.value_or(latest_estimated_throughput_); if (acked_bitrate) { latest_estimated_throughput_ = *acked_bitrate; }
if (!bitrate_is_init_ && state != webrtc::BandwidthUsage::kBwOverusing) { return; }
ChangeState(state, at_time);
webrtc::DataRate throughput_base_limit = estimated_throughput * 1.5 + webrtc::DataRate::KilobitsPerSec(10);
switch (rate_control_state_) { case RateControlState::kRcHold: break; case RateControlState::kRcIncrease: if (estimated_throughput > link_capacity_.UpperBound()) { link_capacity_.Reset(); }
if (current_bitrate_ < throughput_base_limit) { webrtc::DataRate increased_bitrate = webrtc::DataRate::MinusInfinity(); if (link_capacity_.HasEstimate()) { webrtc::DataRate additive_increase = AdditiveRateIncrease(at_time, time_last_bitrate_change_); increased_bitrate = current_bitrate_ + additive_increase; RTC_LOG(LS_WARNING) << "**************add_increase: " << additive_increase.kbps() << ", increased_bitrate: " << increased_bitrate.kbps(); } else { webrtc::DataRate multiplicative_increase = MultiplicativeRateIncrease(at_time, time_last_bitrate_change_); increased_bitrate = current_bitrate_ + multiplicative_increase; RTC_LOG(LS_WARNING) << "**************muliti_increase: " << multiplicative_increase.kbps() << ", increased_bitrate: " << increased_bitrate.kbps(); }
new_bitrate = std::min(increased_bitrate, throughput_base_limit); }
time_last_bitrate_change_ = at_time; break; case RateControlState::kRcDecrease: { webrtc::DataRate decreased_bitrate = webrtc::DataRate::PlusInfinity(); decreased_bitrate = estimated_throughput * beta_; if (decreased_bitrate > current_bitrate_) { if (link_capacity_.HasEstimate()) { decreased_bitrate = link_capacity_.estimate() * beta_; } }
if (decreased_bitrate < current_bitrate_) { new_bitrate = decreased_bitrate; RTC_LOG(LS_WARNING) << "**************decreased_bitrate: " << decreased_bitrate.kbps(); }
if (estimated_throughput < link_capacity_.LowerBound()) { link_capacity_.Reset(); }
link_capacity_.OnOveruseDetected(estimated_throughput); bitrate_is_init_ = true; rate_control_state_ = RateControlState::kRcHold; time_last_bitrate_change_ = at_time; time_last_bitrate_decrease_ = at_time; } break; default: break; }
current_bitrate_ = ClampBitrate(new_bitrate.value_or(current_bitrate_)); }
void AimdRateControl::ChangeState(webrtc::BandwidthUsage state, webrtc::Timestamp at_time) { switch (state) { case webrtc::BandwidthUsage::kBwNormal: if (rate_control_state_ == RateControlState::kRcHold) { rate_control_state_ = RateControlState::kRcIncrease; time_last_bitrate_change_ = at_time; } break; case webrtc::BandwidthUsage::kBwOverusing: if (rate_control_state_ != RateControlState::kRcDecrease) { rate_control_state_ = RateControlState::kRcDecrease; } break; case webrtc::BandwidthUsage::kBwUnderusing: rate_control_state_ = RateControlState::kRcHold; break; default: break; } }
}
|