1 /* 2 * Copyright (c) 2012 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 * FEC and NACK added bitrate is handled outside class 11 */ 12 13 #ifndef MODULES_CONGESTION_CONTROLLER_GOOG_CC_SEND_SIDE_BANDWIDTH_ESTIMATION_H_ 14 #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_SEND_SIDE_BANDWIDTH_ESTIMATION_H_ 15 16 #include <stdint.h> 17 18 #include <deque> 19 #include <utility> 20 #include <vector> 21 22 #include "absl/types/optional.h" 23 #include "api/field_trials_view.h" 24 #include "api/network_state_predictor.h" 25 #include "api/transport/network_types.h" 26 #include "api/units/data_rate.h" 27 #include "api/units/time_delta.h" 28 #include "api/units/timestamp.h" 29 #include "modules/congestion_controller/goog_cc/loss_based_bandwidth_estimation.h" 30 #include "modules/congestion_controller/goog_cc/loss_based_bwe_v2.h" 31 #include "rtc_base/experiments/field_trial_parser.h" 32 33 namespace webrtc { 34 35 class RtcEventLog; 36 37 class LinkCapacityTracker { 38 public: 39 LinkCapacityTracker(); 40 ~LinkCapacityTracker(); 41 // Call when a new delay-based estimate is available. 42 void UpdateDelayBasedEstimate(Timestamp at_time, 43 DataRate delay_based_bitrate); 44 void OnStartingRate(DataRate start_rate); 45 void OnRateUpdate(absl::optional<DataRate> acknowledged, 46 DataRate target, 47 Timestamp at_time); 48 void OnRttBackoff(DataRate backoff_rate, Timestamp at_time); 49 DataRate estimate() const; 50 51 private: 52 FieldTrialParameter<TimeDelta> tracking_rate; 53 double capacity_estimate_bps_ = 0; 54 Timestamp last_link_capacity_update_ = Timestamp::MinusInfinity(); 55 DataRate last_delay_based_estimate_ = DataRate::PlusInfinity(); 56 }; 57 58 class RttBasedBackoff { 59 public: 60 explicit RttBasedBackoff(const FieldTrialsView* key_value_config); 61 ~RttBasedBackoff(); 62 void UpdatePropagationRtt(Timestamp at_time, TimeDelta propagation_rtt); 63 TimeDelta CorrectedRtt(Timestamp at_time) const; 64 65 FieldTrialFlag disabled_; 66 FieldTrialParameter<TimeDelta> configured_limit_; 67 FieldTrialParameter<double> drop_fraction_; 68 FieldTrialParameter<TimeDelta> drop_interval_; 69 FieldTrialParameter<DataRate> bandwidth_floor_; 70 71 public: 72 TimeDelta rtt_limit_; 73 Timestamp last_propagation_rtt_update_; 74 TimeDelta last_propagation_rtt_; 75 Timestamp last_packet_sent_; 76 }; 77 78 class SendSideBandwidthEstimation { 79 public: 80 SendSideBandwidthEstimation() = delete; 81 SendSideBandwidthEstimation(const FieldTrialsView* key_value_config, 82 RtcEventLog* event_log); 83 ~SendSideBandwidthEstimation(); 84 85 void OnRouteChange(); 86 87 DataRate target_rate() const; 88 LossBasedState loss_based_state() const; fraction_loss()89 uint8_t fraction_loss() const { return last_fraction_loss_; } round_trip_time()90 TimeDelta round_trip_time() const { return last_round_trip_time_; } 91 92 DataRate GetEstimatedLinkCapacity() const; 93 // Call periodically to update estimate. 94 void UpdateEstimate(Timestamp at_time); 95 void OnSentPacket(const SentPacket& sent_packet); 96 void UpdatePropagationRtt(Timestamp at_time, TimeDelta propagation_rtt); 97 98 // Call when we receive a RTCP message with TMMBR or REMB. 99 void UpdateReceiverEstimate(Timestamp at_time, DataRate bandwidth); 100 101 // Call when a new delay-based estimate is available. 102 void UpdateDelayBasedEstimate(Timestamp at_time, DataRate bitrate); 103 104 // Call when we receive a RTCP message with a ReceiveBlock. 105 void UpdatePacketsLost(int64_t packets_lost, 106 int64_t number_of_packets, 107 Timestamp at_time); 108 109 // Call when we receive a RTCP message with a ReceiveBlock. 110 void UpdateRtt(TimeDelta rtt, Timestamp at_time); 111 112 void SetBitrates(absl::optional<DataRate> send_bitrate, 113 DataRate min_bitrate, 114 DataRate max_bitrate, 115 Timestamp at_time); 116 void SetSendBitrate(DataRate bitrate, Timestamp at_time); 117 void SetMinMaxBitrate(DataRate min_bitrate, DataRate max_bitrate); 118 int GetMinBitrate() const; 119 void SetAcknowledgedRate(absl::optional<DataRate> acknowledged_rate, 120 Timestamp at_time); 121 void UpdateLossBasedEstimator(const TransportPacketsFeedback& report, 122 BandwidthUsage delay_detector_state, 123 absl::optional<DataRate> probe_bitrate, 124 DataRate upper_link_capacity); 125 126 private: 127 friend class GoogCcStatePrinter; 128 129 enum UmaState { kNoUpdate, kFirstDone, kDone }; 130 131 bool IsInStartPhase(Timestamp at_time) const; 132 133 void UpdateUmaStatsPacketsLost(Timestamp at_time, int packets_lost); 134 135 // Updates history of min bitrates. 136 // After this method returns min_bitrate_history_.front().second contains the 137 // min bitrate used during last kBweIncreaseIntervalMs. 138 void UpdateMinHistory(Timestamp at_time); 139 140 // Gets the upper limit for the target bitrate. This is the minimum of the 141 // delay based limit, the receiver limit and the loss based controller limit. 142 DataRate GetUpperLimit() const; 143 // Prints a warning if `bitrate` if sufficiently long time has past since last 144 // warning. 145 void MaybeLogLowBitrateWarning(DataRate bitrate, Timestamp at_time); 146 // Stores an update to the event log if the loss rate has changed, the target 147 // has changed, or sufficient time has passed since last stored event. 148 void MaybeLogLossBasedEvent(Timestamp at_time); 149 150 // Cap `bitrate` to [min_bitrate_configured_, max_bitrate_configured_] and 151 // set `current_bitrate_` to the capped value and updates the event log. 152 void UpdateTargetBitrate(DataRate bitrate, Timestamp at_time); 153 // Applies lower and upper bounds to the current target rate. 154 // TODO(srte): This seems to be called even when limits haven't changed, that 155 // should be cleaned up. 156 void ApplyTargetLimits(Timestamp at_time); 157 158 bool LossBasedBandwidthEstimatorV1Enabled() const; 159 bool LossBasedBandwidthEstimatorV2Enabled() const; 160 161 bool LossBasedBandwidthEstimatorV1ReadyForUse() const; 162 bool LossBasedBandwidthEstimatorV2ReadyForUse() const; 163 164 RttBasedBackoff rtt_backoff_; 165 LinkCapacityTracker link_capacity_; 166 167 std::deque<std::pair<Timestamp, DataRate> > min_bitrate_history_; 168 169 // incoming filters 170 int lost_packets_since_last_loss_update_; 171 int expected_packets_since_last_loss_update_; 172 173 absl::optional<DataRate> acknowledged_rate_; 174 DataRate current_target_; 175 DataRate last_logged_target_; 176 DataRate min_bitrate_configured_; 177 DataRate max_bitrate_configured_; 178 Timestamp last_low_bitrate_log_; 179 180 bool has_decreased_since_last_fraction_loss_; 181 Timestamp last_loss_feedback_; 182 Timestamp last_loss_packet_report_; 183 uint8_t last_fraction_loss_; 184 uint8_t last_logged_fraction_loss_; 185 TimeDelta last_round_trip_time_; 186 187 // The max bitrate as set by the receiver in the call. This is typically 188 // signalled using the REMB RTCP message and is used when we don't have any 189 // send side delay based estimate. 190 DataRate receiver_limit_; 191 DataRate delay_based_limit_; 192 Timestamp time_last_decrease_; 193 Timestamp first_report_time_; 194 int initially_lost_packets_; 195 DataRate bitrate_at_2_seconds_; 196 UmaState uma_update_state_; 197 UmaState uma_rtt_state_; 198 std::vector<bool> rampup_uma_stats_updated_; 199 RtcEventLog* const event_log_; 200 Timestamp last_rtc_event_log_; 201 float low_loss_threshold_; 202 float high_loss_threshold_; 203 DataRate bitrate_threshold_; 204 LossBasedBandwidthEstimation loss_based_bandwidth_estimator_v1_; 205 LossBasedBweV2 loss_based_bandwidth_estimator_v2_; 206 LossBasedState loss_based_state_; 207 FieldTrialFlag disable_receiver_limit_caps_only_; 208 }; 209 } // namespace webrtc 210 #endif // MODULES_CONGESTION_CONTROLLER_GOOG_CC_SEND_SIDE_BANDWIDTH_ESTIMATION_H_ 211