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