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