aimd_rate_control.h (5166B)
1 /* 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_REMOTE_BITRATE_ESTIMATOR_AIMD_RATE_CONTROL_H_ 12 #define MODULES_REMOTE_BITRATE_ESTIMATOR_AIMD_RATE_CONTROL_H_ 13 14 #include <stdint.h> 15 16 #include <optional> 17 18 #include "api/field_trials_view.h" 19 #include "api/transport/network_types.h" 20 #include "api/units/data_rate.h" 21 #include "api/units/time_delta.h" 22 #include "api/units/timestamp.h" 23 #include "modules/congestion_controller/goog_cc/link_capacity_estimator.h" 24 #include "modules/remote_bitrate_estimator/include/bwe_defines.h" 25 #include "rtc_base/experiments/field_trial_parser.h" 26 27 namespace webrtc { 28 // A rate control implementation based on additive increases of 29 // bitrate when no over-use is detected and multiplicative decreases when 30 // over-uses are detected. When we think the available bandwidth has changes or 31 // is unknown, we will switch to a "slow-start mode" where we increase 32 // multiplicatively. 33 class AimdRateControl { 34 public: 35 explicit AimdRateControl(const FieldTrialsView& key_value_config); 36 AimdRateControl(const FieldTrialsView& key_value_config, bool send_side); 37 ~AimdRateControl(); 38 39 // Returns true if the target bitrate has been initialized. This happens 40 // either if it has been explicitly set via SetStartBitrate/SetEstimate, or if 41 // we have measured a throughput. 42 bool ValidEstimate() const; 43 void SetStartBitrate(DataRate start_bitrate); 44 void SetMinBitrate(DataRate min_bitrate); 45 TimeDelta GetFeedbackInterval() const; 46 47 // Returns true if the bitrate estimate hasn't been changed for more than 48 // an RTT, or if the estimated_throughput is less than half of the current 49 // estimate. Should be used to decide if we should reduce the rate further 50 // when over-using. 51 bool TimeToReduceFurther(Timestamp at_time, 52 DataRate estimated_throughput) const; 53 // As above. To be used if overusing before we have measured a throughput. 54 bool InitialTimeToReduceFurther(Timestamp at_time) const; 55 56 DataRate LatestEstimate() const; 57 void SetRtt(TimeDelta rtt); 58 DataRate Update(const RateControlInput& input, Timestamp at_time); 59 void SetInApplicationLimitedRegion(bool in_alr); 60 void SetEstimate(DataRate bitrate, Timestamp at_time); 61 void SetNetworkStateEstimate( 62 const std::optional<NetworkStateEstimate>& estimate); 63 64 // Returns the increase rate when used bandwidth is near the link capacity. 65 double GetNearMaxIncreaseRateBpsPerSecond() const; 66 // Returns the expected time between overuse signals (assuming steady state). 67 TimeDelta GetExpectedBandwidthPeriod() const; 68 69 private: 70 enum class RateControlState { kRcHold, kRcIncrease, kRcDecrease }; 71 72 friend class GoogCcStatePrinter; 73 // Update the target bitrate based on, among other things, the current rate 74 // control state, the current target bitrate and the estimated throughput. 75 // When in the "increase" state the bitrate will be increased either 76 // additively or multiplicatively depending on the rate control region. When 77 // in the "decrease" state the bitrate will be decreased to slightly below the 78 // current throughput. When in the "hold" state the bitrate will be kept 79 // constant to allow built up queues to drain. 80 void ChangeBitrate(const RateControlInput& input, Timestamp at_time); 81 82 DataRate ClampBitrate(DataRate new_bitrate) const; 83 DataRate MultiplicativeRateIncrease(Timestamp at_time, 84 Timestamp last_ms, 85 DataRate current_bitrate) const; 86 DataRate AdditiveRateIncrease(Timestamp at_time, Timestamp last_time) const; 87 void UpdateChangePeriod(Timestamp at_time); 88 void ChangeState(const RateControlInput& input, Timestamp at_time); 89 90 DataRate min_configured_bitrate_; 91 DataRate max_configured_bitrate_; 92 DataRate current_bitrate_; 93 DataRate latest_estimated_throughput_; 94 LinkCapacityEstimator link_capacity_; 95 std::optional<NetworkStateEstimate> network_estimate_; 96 RateControlState rate_control_state_; 97 Timestamp time_last_bitrate_change_; 98 Timestamp time_last_bitrate_decrease_; 99 Timestamp time_first_throughput_estimate_; 100 bool bitrate_is_initialized_; 101 double beta_; 102 bool in_alr_; 103 TimeDelta rtt_; 104 const bool send_side_; 105 // Allow the delay based estimate to only increase as long as application 106 // limited region (alr) is not detected. 107 const bool no_bitrate_increase_in_alr_; 108 // If "Disabled", estimated link capacity is not used as upper bound. 109 FieldTrialFlag disable_estimate_bounded_increase_{"Disabled"}; 110 FieldTrialParameter<bool> use_current_estimate_as_min_upper_bound_{"c_upper", 111 true}; 112 std::optional<DataRate> last_decrease_; 113 }; 114 } // namespace webrtc 115 116 #endif // MODULES_REMOTE_BITRATE_ESTIMATOR_AIMD_RATE_CONTROL_H_