• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/transport/network_types.h"
18 #include "api/transport/webrtc_key_value_config.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 WebRtcKeyValueConfig* key_value_config);
34   AimdRateControl(const WebRtcKeyValueConfig* 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   friend class GoogCcStatePrinter;
69   // Update the target bitrate based on, among other things, the current rate
70   // control state, the current target bitrate and the estimated throughput.
71   // When in the "increase" state the bitrate will be increased either
72   // additively or multiplicatively depending on the rate control region. When
73   // in the "decrease" state the bitrate will be decreased to slightly below the
74   // current throughput. When in the "hold" state the bitrate will be kept
75   // constant to allow built up queues to drain.
76   void ChangeBitrate(const RateControlInput& input, Timestamp at_time);
77 
78   DataRate ClampBitrate(DataRate new_bitrate) const;
79   DataRate MultiplicativeRateIncrease(Timestamp at_time,
80                                       Timestamp last_ms,
81                                       DataRate current_bitrate) const;
82   DataRate AdditiveRateIncrease(Timestamp at_time, Timestamp last_time) const;
83   void UpdateChangePeriod(Timestamp at_time);
84   void ChangeState(const RateControlInput& input, Timestamp at_time);
85 
86   DataRate min_configured_bitrate_;
87   DataRate max_configured_bitrate_;
88   DataRate current_bitrate_;
89   DataRate latest_estimated_throughput_;
90   LinkCapacityEstimator link_capacity_;
91   absl::optional<NetworkStateEstimate> network_estimate_;
92   RateControlState rate_control_state_;
93   Timestamp time_last_bitrate_change_;
94   Timestamp time_last_bitrate_decrease_;
95   Timestamp time_first_throughput_estimate_;
96   bool bitrate_is_initialized_;
97   double beta_;
98   bool in_alr_;
99   TimeDelta rtt_;
100   const bool send_side_;
101   const bool in_experiment_;
102   // Allow the delay based estimate to only increase as long as application
103   // limited region (alr) is not detected.
104   const bool no_bitrate_increase_in_alr_;
105   // Use estimated link capacity lower bound if it is higher than the
106   // acknowledged rate when backing off due to overuse.
107   const bool estimate_bounded_backoff_;
108   // Use estimated link capacity upper bound as upper limit for increasing
109   // bitrate over the acknowledged rate.
110   const bool estimate_bounded_increase_;
111   absl::optional<DataRate> last_decrease_;
112   FieldTrialOptional<TimeDelta> initial_backoff_interval_;
113   FieldTrialFlag link_capacity_fix_;
114 };
115 }  // namespace webrtc
116 
117 #endif  // MODULES_REMOTE_BITRATE_ESTIMATOR_AIMD_RATE_CONTROL_H_
118