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 
11 #include "modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h"
12 
13 #include <algorithm>
14 #include <cstdio>
15 #include <limits>
16 #include <memory>
17 #include <string>
18 
19 #include "absl/strings/match.h"
20 #include "api/rtc_event_log/rtc_event.h"
21 #include "api/rtc_event_log/rtc_event_log.h"
22 #include "logging/rtc_event_log/events/rtc_event_bwe_update_loss_based.h"
23 #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
24 #include "rtc_base/checks.h"
25 #include "rtc_base/logging.h"
26 #include "system_wrappers/include/field_trial.h"
27 #include "system_wrappers/include/metrics.h"
28 
29 namespace webrtc {
30 namespace {
31 constexpr TimeDelta kBweIncreaseInterval = TimeDelta::Millis(1000);
32 constexpr TimeDelta kBweDecreaseInterval = TimeDelta::Millis(300);
33 constexpr TimeDelta kStartPhase = TimeDelta::Millis(2000);
34 constexpr TimeDelta kBweConverganceTime = TimeDelta::Millis(20000);
35 constexpr int kLimitNumPackets = 20;
36 constexpr DataRate kDefaultMaxBitrate = DataRate::BitsPerSec(1000000000);
37 constexpr TimeDelta kLowBitrateLogPeriod = TimeDelta::Millis(10000);
38 constexpr TimeDelta kRtcEventLogPeriod = TimeDelta::Millis(5000);
39 // Expecting that RTCP feedback is sent uniformly within [0.5, 1.5]s intervals.
40 constexpr TimeDelta kMaxRtcpFeedbackInterval = TimeDelta::Millis(5000);
41 
42 constexpr float kDefaultLowLossThreshold = 0.02f;
43 constexpr float kDefaultHighLossThreshold = 0.1f;
44 constexpr DataRate kDefaultBitrateThreshold = DataRate::Zero();
45 
46 struct UmaRampUpMetric {
47   const char* metric_name;
48   int bitrate_kbps;
49 };
50 
51 const UmaRampUpMetric kUmaRampupMetrics[] = {
52     {"WebRTC.BWE.RampUpTimeTo500kbpsInMs", 500},
53     {"WebRTC.BWE.RampUpTimeTo1000kbpsInMs", 1000},
54     {"WebRTC.BWE.RampUpTimeTo2000kbpsInMs", 2000}};
55 const size_t kNumUmaRampupMetrics =
56     sizeof(kUmaRampupMetrics) / sizeof(kUmaRampupMetrics[0]);
57 
58 const char kBweLosExperiment[] = "WebRTC-BweLossExperiment";
59 
BweLossExperimentIsEnabled()60 bool BweLossExperimentIsEnabled() {
61   std::string experiment_string =
62       webrtc::field_trial::FindFullName(kBweLosExperiment);
63   // The experiment is enabled iff the field trial string begins with "Enabled".
64   return absl::StartsWith(experiment_string, "Enabled");
65 }
66 
ReadBweLossExperimentParameters(float * low_loss_threshold,float * high_loss_threshold,uint32_t * bitrate_threshold_kbps)67 bool ReadBweLossExperimentParameters(float* low_loss_threshold,
68                                      float* high_loss_threshold,
69                                      uint32_t* bitrate_threshold_kbps) {
70   RTC_DCHECK(low_loss_threshold);
71   RTC_DCHECK(high_loss_threshold);
72   RTC_DCHECK(bitrate_threshold_kbps);
73   std::string experiment_string =
74       webrtc::field_trial::FindFullName(kBweLosExperiment);
75   int parsed_values =
76       sscanf(experiment_string.c_str(), "Enabled-%f,%f,%u", low_loss_threshold,
77              high_loss_threshold, bitrate_threshold_kbps);
78   if (parsed_values == 3) {
79     RTC_CHECK_GT(*low_loss_threshold, 0.0f)
80         << "Loss threshold must be greater than 0.";
81     RTC_CHECK_LE(*low_loss_threshold, 1.0f)
82         << "Loss threshold must be less than or equal to 1.";
83     RTC_CHECK_GT(*high_loss_threshold, 0.0f)
84         << "Loss threshold must be greater than 0.";
85     RTC_CHECK_LE(*high_loss_threshold, 1.0f)
86         << "Loss threshold must be less than or equal to 1.";
87     RTC_CHECK_LE(*low_loss_threshold, *high_loss_threshold)
88         << "The low loss threshold must be less than or equal to the high loss "
89            "threshold.";
90     RTC_CHECK_GE(*bitrate_threshold_kbps, 0)
91         << "Bitrate threshold can't be negative.";
92     RTC_CHECK_LT(*bitrate_threshold_kbps,
93                  std::numeric_limits<int>::max() / 1000)
94         << "Bitrate must be smaller enough to avoid overflows.";
95     return true;
96   }
97   RTC_LOG(LS_WARNING) << "Failed to parse parameters for BweLossExperiment "
98                          "experiment from field trial string. Using default.";
99   *low_loss_threshold = kDefaultLowLossThreshold;
100   *high_loss_threshold = kDefaultHighLossThreshold;
101   *bitrate_threshold_kbps = kDefaultBitrateThreshold.kbps();
102   return false;
103 }
104 }  // namespace
105 
LinkCapacityTracker()106 LinkCapacityTracker::LinkCapacityTracker()
107     : tracking_rate("rate", TimeDelta::Seconds(10)) {
108   ParseFieldTrial({&tracking_rate},
109                   field_trial::FindFullName("WebRTC-Bwe-LinkCapacity"));
110 }
111 
~LinkCapacityTracker()112 LinkCapacityTracker::~LinkCapacityTracker() {}
113 
UpdateDelayBasedEstimate(Timestamp at_time,DataRate delay_based_bitrate)114 void LinkCapacityTracker::UpdateDelayBasedEstimate(
115     Timestamp at_time,
116     DataRate delay_based_bitrate) {
117   if (delay_based_bitrate < last_delay_based_estimate_) {
118     capacity_estimate_bps_ =
119         std::min(capacity_estimate_bps_, delay_based_bitrate.bps<double>());
120     last_link_capacity_update_ = at_time;
121   }
122   last_delay_based_estimate_ = delay_based_bitrate;
123 }
124 
OnStartingRate(DataRate start_rate)125 void LinkCapacityTracker::OnStartingRate(DataRate start_rate) {
126   if (last_link_capacity_update_.IsInfinite())
127     capacity_estimate_bps_ = start_rate.bps<double>();
128 }
129 
OnRateUpdate(absl::optional<DataRate> acknowledged,DataRate target,Timestamp at_time)130 void LinkCapacityTracker::OnRateUpdate(absl::optional<DataRate> acknowledged,
131                                        DataRate target,
132                                        Timestamp at_time) {
133   if (!acknowledged)
134     return;
135   DataRate acknowledged_target = std::min(*acknowledged, target);
136   if (acknowledged_target.bps() > capacity_estimate_bps_) {
137     TimeDelta delta = at_time - last_link_capacity_update_;
138     double alpha = delta.IsFinite() ? exp(-(delta / tracking_rate.Get())) : 0;
139     capacity_estimate_bps_ = alpha * capacity_estimate_bps_ +
140                              (1 - alpha) * acknowledged_target.bps<double>();
141   }
142   last_link_capacity_update_ = at_time;
143 }
144 
OnRttBackoff(DataRate backoff_rate,Timestamp at_time)145 void LinkCapacityTracker::OnRttBackoff(DataRate backoff_rate,
146                                        Timestamp at_time) {
147   capacity_estimate_bps_ =
148       std::min(capacity_estimate_bps_, backoff_rate.bps<double>());
149   last_link_capacity_update_ = at_time;
150 }
151 
estimate() const152 DataRate LinkCapacityTracker::estimate() const {
153   return DataRate::BitsPerSec(capacity_estimate_bps_);
154 }
155 
RttBasedBackoff()156 RttBasedBackoff::RttBasedBackoff()
157     : rtt_limit_("limit", TimeDelta::Seconds(3)),
158       drop_fraction_("fraction", 0.8),
159       drop_interval_("interval", TimeDelta::Seconds(1)),
160       bandwidth_floor_("floor", DataRate::KilobitsPerSec(5)),
161       // By initializing this to plus infinity, we make sure that we never
162       // trigger rtt backoff unless packet feedback is enabled.
163       last_propagation_rtt_update_(Timestamp::PlusInfinity()),
164       last_propagation_rtt_(TimeDelta::Zero()),
165       last_packet_sent_(Timestamp::MinusInfinity()) {
166   ParseFieldTrial(
167       {&rtt_limit_, &drop_fraction_, &drop_interval_, &bandwidth_floor_},
168       field_trial::FindFullName("WebRTC-Bwe-MaxRttLimit"));
169 }
170 
UpdatePropagationRtt(Timestamp at_time,TimeDelta propagation_rtt)171 void RttBasedBackoff::UpdatePropagationRtt(Timestamp at_time,
172                                            TimeDelta propagation_rtt) {
173   last_propagation_rtt_update_ = at_time;
174   last_propagation_rtt_ = propagation_rtt;
175 }
176 
CorrectedRtt(Timestamp at_time) const177 TimeDelta RttBasedBackoff::CorrectedRtt(Timestamp at_time) const {
178   TimeDelta time_since_rtt = at_time - last_propagation_rtt_update_;
179   TimeDelta timeout_correction = time_since_rtt;
180   // Avoid timeout when no packets are being sent.
181   TimeDelta time_since_packet_sent = at_time - last_packet_sent_;
182   timeout_correction =
183       std::max(time_since_rtt - time_since_packet_sent, TimeDelta::Zero());
184   return timeout_correction + last_propagation_rtt_;
185 }
186 
187 RttBasedBackoff::~RttBasedBackoff() = default;
188 
SendSideBandwidthEstimation(RtcEventLog * event_log)189 SendSideBandwidthEstimation::SendSideBandwidthEstimation(RtcEventLog* event_log)
190     : lost_packets_since_last_loss_update_(0),
191       expected_packets_since_last_loss_update_(0),
192       current_target_(DataRate::Zero()),
193       last_logged_target_(DataRate::Zero()),
194       min_bitrate_configured_(
195           DataRate::BitsPerSec(congestion_controller::GetMinBitrateBps())),
196       max_bitrate_configured_(kDefaultMaxBitrate),
197       last_low_bitrate_log_(Timestamp::MinusInfinity()),
198       has_decreased_since_last_fraction_loss_(false),
199       last_loss_feedback_(Timestamp::MinusInfinity()),
200       last_loss_packet_report_(Timestamp::MinusInfinity()),
201       last_fraction_loss_(0),
202       last_logged_fraction_loss_(0),
203       last_round_trip_time_(TimeDelta::Zero()),
204       receiver_limit_(DataRate::PlusInfinity()),
205       delay_based_limit_(DataRate::PlusInfinity()),
206       time_last_decrease_(Timestamp::MinusInfinity()),
207       first_report_time_(Timestamp::MinusInfinity()),
208       initially_lost_packets_(0),
209       bitrate_at_2_seconds_(DataRate::Zero()),
210       uma_update_state_(kNoUpdate),
211       uma_rtt_state_(kNoUpdate),
212       rampup_uma_stats_updated_(kNumUmaRampupMetrics, false),
213       event_log_(event_log),
214       last_rtc_event_log_(Timestamp::MinusInfinity()),
215       low_loss_threshold_(kDefaultLowLossThreshold),
216       high_loss_threshold_(kDefaultHighLossThreshold),
217       bitrate_threshold_(kDefaultBitrateThreshold) {
218   RTC_DCHECK(event_log);
219   if (BweLossExperimentIsEnabled()) {
220     uint32_t bitrate_threshold_kbps;
221     if (ReadBweLossExperimentParameters(&low_loss_threshold_,
222                                         &high_loss_threshold_,
223                                         &bitrate_threshold_kbps)) {
224       RTC_LOG(LS_INFO) << "Enabled BweLossExperiment with parameters "
225                        << low_loss_threshold_ << ", " << high_loss_threshold_
226                        << ", " << bitrate_threshold_kbps;
227       bitrate_threshold_ = DataRate::KilobitsPerSec(bitrate_threshold_kbps);
228     }
229   }
230 }
231 
~SendSideBandwidthEstimation()232 SendSideBandwidthEstimation::~SendSideBandwidthEstimation() {}
233 
OnRouteChange()234 void SendSideBandwidthEstimation::OnRouteChange() {
235   lost_packets_since_last_loss_update_ = 0;
236   expected_packets_since_last_loss_update_ = 0;
237   current_target_ = DataRate::Zero();
238   min_bitrate_configured_ =
239       DataRate::BitsPerSec(congestion_controller::GetMinBitrateBps());
240   max_bitrate_configured_ = kDefaultMaxBitrate;
241   last_low_bitrate_log_ = Timestamp::MinusInfinity();
242   has_decreased_since_last_fraction_loss_ = false;
243   last_loss_feedback_ = Timestamp::MinusInfinity();
244   last_loss_packet_report_ = Timestamp::MinusInfinity();
245   last_fraction_loss_ = 0;
246   last_logged_fraction_loss_ = 0;
247   last_round_trip_time_ = TimeDelta::Zero();
248   receiver_limit_ = DataRate::PlusInfinity();
249   delay_based_limit_ = DataRate::PlusInfinity();
250   time_last_decrease_ = Timestamp::MinusInfinity();
251   first_report_time_ = Timestamp::MinusInfinity();
252   initially_lost_packets_ = 0;
253   bitrate_at_2_seconds_ = DataRate::Zero();
254   uma_update_state_ = kNoUpdate;
255   uma_rtt_state_ = kNoUpdate;
256   last_rtc_event_log_ = Timestamp::MinusInfinity();
257 }
258 
SetBitrates(absl::optional<DataRate> send_bitrate,DataRate min_bitrate,DataRate max_bitrate,Timestamp at_time)259 void SendSideBandwidthEstimation::SetBitrates(
260     absl::optional<DataRate> send_bitrate,
261     DataRate min_bitrate,
262     DataRate max_bitrate,
263     Timestamp at_time) {
264   SetMinMaxBitrate(min_bitrate, max_bitrate);
265   if (send_bitrate) {
266     link_capacity_.OnStartingRate(*send_bitrate);
267     SetSendBitrate(*send_bitrate, at_time);
268   }
269 }
270 
SetSendBitrate(DataRate bitrate,Timestamp at_time)271 void SendSideBandwidthEstimation::SetSendBitrate(DataRate bitrate,
272                                                  Timestamp at_time) {
273   RTC_DCHECK_GT(bitrate, DataRate::Zero());
274   // Reset to avoid being capped by the estimate.
275   delay_based_limit_ = DataRate::PlusInfinity();
276   if (loss_based_bandwidth_estimation_.Enabled()) {
277     loss_based_bandwidth_estimation_.MaybeReset(bitrate);
278   }
279   UpdateTargetBitrate(bitrate, at_time);
280   // Clear last sent bitrate history so the new value can be used directly
281   // and not capped.
282   min_bitrate_history_.clear();
283 }
284 
SetMinMaxBitrate(DataRate min_bitrate,DataRate max_bitrate)285 void SendSideBandwidthEstimation::SetMinMaxBitrate(DataRate min_bitrate,
286                                                    DataRate max_bitrate) {
287   min_bitrate_configured_ =
288       std::max(min_bitrate, congestion_controller::GetMinBitrate());
289   if (max_bitrate > DataRate::Zero() && max_bitrate.IsFinite()) {
290     max_bitrate_configured_ = std::max(min_bitrate_configured_, max_bitrate);
291   } else {
292     max_bitrate_configured_ = kDefaultMaxBitrate;
293   }
294 }
295 
GetMinBitrate() const296 int SendSideBandwidthEstimation::GetMinBitrate() const {
297   return min_bitrate_configured_.bps<int>();
298 }
299 
target_rate() const300 DataRate SendSideBandwidthEstimation::target_rate() const {
301   return std::max(min_bitrate_configured_, current_target_);
302 }
303 
GetEstimatedLinkCapacity() const304 DataRate SendSideBandwidthEstimation::GetEstimatedLinkCapacity() const {
305   return link_capacity_.estimate();
306 }
307 
UpdateReceiverEstimate(Timestamp at_time,DataRate bandwidth)308 void SendSideBandwidthEstimation::UpdateReceiverEstimate(Timestamp at_time,
309                                                          DataRate bandwidth) {
310   // TODO(srte): Ensure caller passes PlusInfinity, not zero, to represent no
311   // limitation.
312   receiver_limit_ = bandwidth.IsZero() ? DataRate::PlusInfinity() : bandwidth;
313   ApplyTargetLimits(at_time);
314 }
315 
UpdateDelayBasedEstimate(Timestamp at_time,DataRate bitrate)316 void SendSideBandwidthEstimation::UpdateDelayBasedEstimate(Timestamp at_time,
317                                                            DataRate bitrate) {
318   link_capacity_.UpdateDelayBasedEstimate(at_time, bitrate);
319   // TODO(srte): Ensure caller passes PlusInfinity, not zero, to represent no
320   // limitation.
321   delay_based_limit_ = bitrate.IsZero() ? DataRate::PlusInfinity() : bitrate;
322   ApplyTargetLimits(at_time);
323 }
324 
SetAcknowledgedRate(absl::optional<DataRate> acknowledged_rate,Timestamp at_time)325 void SendSideBandwidthEstimation::SetAcknowledgedRate(
326     absl::optional<DataRate> acknowledged_rate,
327     Timestamp at_time) {
328   acknowledged_rate_ = acknowledged_rate;
329   if (acknowledged_rate && loss_based_bandwidth_estimation_.Enabled()) {
330     loss_based_bandwidth_estimation_.UpdateAcknowledgedBitrate(
331         *acknowledged_rate, at_time);
332   }
333 }
334 
IncomingPacketFeedbackVector(const TransportPacketsFeedback & report)335 void SendSideBandwidthEstimation::IncomingPacketFeedbackVector(
336     const TransportPacketsFeedback& report) {
337   if (loss_based_bandwidth_estimation_.Enabled()) {
338     loss_based_bandwidth_estimation_.UpdateLossStatistics(
339         report.packet_feedbacks, report.feedback_time);
340   }
341 }
342 
UpdatePacketsLost(int packets_lost,int number_of_packets,Timestamp at_time)343 void SendSideBandwidthEstimation::UpdatePacketsLost(int packets_lost,
344                                                     int number_of_packets,
345                                                     Timestamp at_time) {
346   last_loss_feedback_ = at_time;
347   if (first_report_time_.IsInfinite())
348     first_report_time_ = at_time;
349 
350   // Check sequence number diff and weight loss report
351   if (number_of_packets > 0) {
352     // Accumulate reports.
353     lost_packets_since_last_loss_update_ += packets_lost;
354     expected_packets_since_last_loss_update_ += number_of_packets;
355 
356     // Don't generate a loss rate until it can be based on enough packets.
357     if (expected_packets_since_last_loss_update_ < kLimitNumPackets)
358       return;
359 
360     has_decreased_since_last_fraction_loss_ = false;
361     int64_t lost_q8 = lost_packets_since_last_loss_update_ << 8;
362     int64_t expected = expected_packets_since_last_loss_update_;
363     last_fraction_loss_ = std::min<int>(lost_q8 / expected, 255);
364 
365     // Reset accumulators.
366 
367     lost_packets_since_last_loss_update_ = 0;
368     expected_packets_since_last_loss_update_ = 0;
369     last_loss_packet_report_ = at_time;
370     UpdateEstimate(at_time);
371   }
372   UpdateUmaStatsPacketsLost(at_time, packets_lost);
373 }
374 
UpdateUmaStatsPacketsLost(Timestamp at_time,int packets_lost)375 void SendSideBandwidthEstimation::UpdateUmaStatsPacketsLost(Timestamp at_time,
376                                                             int packets_lost) {
377   DataRate bitrate_kbps =
378       DataRate::KilobitsPerSec((current_target_.bps() + 500) / 1000);
379   for (size_t i = 0; i < kNumUmaRampupMetrics; ++i) {
380     if (!rampup_uma_stats_updated_[i] &&
381         bitrate_kbps.kbps() >= kUmaRampupMetrics[i].bitrate_kbps) {
382       RTC_HISTOGRAMS_COUNTS_100000(i, kUmaRampupMetrics[i].metric_name,
383                                    (at_time - first_report_time_).ms());
384       rampup_uma_stats_updated_[i] = true;
385     }
386   }
387   if (IsInStartPhase(at_time)) {
388     initially_lost_packets_ += packets_lost;
389   } else if (uma_update_state_ == kNoUpdate) {
390     uma_update_state_ = kFirstDone;
391     bitrate_at_2_seconds_ = bitrate_kbps;
392     RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitiallyLostPackets",
393                          initially_lost_packets_, 0, 100, 50);
394     RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialBandwidthEstimate",
395                          bitrate_at_2_seconds_.kbps(), 0, 2000, 50);
396   } else if (uma_update_state_ == kFirstDone &&
397              at_time - first_report_time_ >= kBweConverganceTime) {
398     uma_update_state_ = kDone;
399     int bitrate_diff_kbps = std::max(
400         bitrate_at_2_seconds_.kbps<int>() - bitrate_kbps.kbps<int>(), 0);
401     RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialVsConvergedDiff", bitrate_diff_kbps,
402                          0, 2000, 50);
403   }
404 }
405 
UpdateRtt(TimeDelta rtt,Timestamp at_time)406 void SendSideBandwidthEstimation::UpdateRtt(TimeDelta rtt, Timestamp at_time) {
407   // Update RTT if we were able to compute an RTT based on this RTCP.
408   // FlexFEC doesn't send RTCP SR, which means we won't be able to compute RTT.
409   if (rtt > TimeDelta::Zero())
410     last_round_trip_time_ = rtt;
411 
412   if (!IsInStartPhase(at_time) && uma_rtt_state_ == kNoUpdate) {
413     uma_rtt_state_ = kDone;
414     RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialRtt", rtt.ms<int>(), 0, 2000, 50);
415   }
416 }
417 
UpdateEstimate(Timestamp at_time)418 void SendSideBandwidthEstimation::UpdateEstimate(Timestamp at_time) {
419   if (rtt_backoff_.CorrectedRtt(at_time) >
420       static_cast<TimeDelta>(rtt_backoff_.rtt_limit_)) {
421     if (at_time - time_last_decrease_ >=
422             static_cast<TimeDelta>(rtt_backoff_.drop_interval_) &&
423         current_target_ >
424             static_cast<DataRate>(rtt_backoff_.bandwidth_floor_)) {
425       time_last_decrease_ = at_time;
426       DataRate new_bitrate = std::max(
427           current_target_ * static_cast<double>(rtt_backoff_.drop_fraction_),
428           rtt_backoff_.bandwidth_floor_.Get());
429       link_capacity_.OnRttBackoff(new_bitrate, at_time);
430       UpdateTargetBitrate(new_bitrate, at_time);
431       return;
432     }
433     // TODO(srte): This is likely redundant in most cases.
434     ApplyTargetLimits(at_time);
435     return;
436   }
437 
438   // We trust the REMB and/or delay-based estimate during the first 2 seconds if
439   // we haven't had any packet loss reported, to allow startup bitrate probing.
440   if (last_fraction_loss_ == 0 && IsInStartPhase(at_time)) {
441     DataRate new_bitrate = current_target_;
442     // TODO(srte): We should not allow the new_bitrate to be larger than the
443     // receiver limit here.
444     if (receiver_limit_.IsFinite())
445       new_bitrate = std::max(receiver_limit_, new_bitrate);
446     if (delay_based_limit_.IsFinite())
447       new_bitrate = std::max(delay_based_limit_, new_bitrate);
448     if (loss_based_bandwidth_estimation_.Enabled()) {
449       loss_based_bandwidth_estimation_.SetInitialBitrate(new_bitrate);
450     }
451 
452     if (new_bitrate != current_target_) {
453       min_bitrate_history_.clear();
454       if (loss_based_bandwidth_estimation_.Enabled()) {
455         min_bitrate_history_.push_back(std::make_pair(at_time, new_bitrate));
456       } else {
457         min_bitrate_history_.push_back(
458             std::make_pair(at_time, current_target_));
459       }
460       UpdateTargetBitrate(new_bitrate, at_time);
461       return;
462     }
463   }
464   UpdateMinHistory(at_time);
465   if (last_loss_packet_report_.IsInfinite()) {
466     // No feedback received.
467     // TODO(srte): This is likely redundant in most cases.
468     ApplyTargetLimits(at_time);
469     return;
470   }
471 
472   if (loss_based_bandwidth_estimation_.Enabled()) {
473     loss_based_bandwidth_estimation_.Update(
474         at_time, min_bitrate_history_.front().second, last_round_trip_time_);
475     DataRate new_bitrate = MaybeRampupOrBackoff(current_target_, at_time);
476     UpdateTargetBitrate(new_bitrate, at_time);
477     return;
478   }
479 
480   TimeDelta time_since_loss_packet_report = at_time - last_loss_packet_report_;
481   if (time_since_loss_packet_report < 1.2 * kMaxRtcpFeedbackInterval) {
482     // We only care about loss above a given bitrate threshold.
483     float loss = last_fraction_loss_ / 256.0f;
484     // We only make decisions based on loss when the bitrate is above a
485     // threshold. This is a crude way of handling loss which is uncorrelated
486     // to congestion.
487     if (current_target_ < bitrate_threshold_ || loss <= low_loss_threshold_) {
488       // Loss < 2%: Increase rate by 8% of the min bitrate in the last
489       // kBweIncreaseInterval.
490       // Note that by remembering the bitrate over the last second one can
491       // rampup up one second faster than if only allowed to start ramping
492       // at 8% per second rate now. E.g.:
493       //   If sending a constant 100kbps it can rampup immediately to 108kbps
494       //   whenever a receiver report is received with lower packet loss.
495       //   If instead one would do: current_bitrate_ *= 1.08^(delta time),
496       //   it would take over one second since the lower packet loss to achieve
497       //   108kbps.
498       DataRate new_bitrate = DataRate::BitsPerSec(
499           min_bitrate_history_.front().second.bps() * 1.08 + 0.5);
500 
501       // Add 1 kbps extra, just to make sure that we do not get stuck
502       // (gives a little extra increase at low rates, negligible at higher
503       // rates).
504       new_bitrate += DataRate::BitsPerSec(1000);
505       UpdateTargetBitrate(new_bitrate, at_time);
506       return;
507     } else if (current_target_ > bitrate_threshold_) {
508       if (loss <= high_loss_threshold_) {
509         // Loss between 2% - 10%: Do nothing.
510       } else {
511         // Loss > 10%: Limit the rate decreases to once a kBweDecreaseInterval
512         // + rtt.
513         if (!has_decreased_since_last_fraction_loss_ &&
514             (at_time - time_last_decrease_) >=
515                 (kBweDecreaseInterval + last_round_trip_time_)) {
516           time_last_decrease_ = at_time;
517 
518           // Reduce rate:
519           //   newRate = rate * (1 - 0.5*lossRate);
520           //   where packetLoss = 256*lossRate;
521           DataRate new_bitrate = DataRate::BitsPerSec(
522               (current_target_.bps() *
523                static_cast<double>(512 - last_fraction_loss_)) /
524               512.0);
525           has_decreased_since_last_fraction_loss_ = true;
526           UpdateTargetBitrate(new_bitrate, at_time);
527           return;
528         }
529       }
530     }
531   }
532   // TODO(srte): This is likely redundant in most cases.
533   ApplyTargetLimits(at_time);
534 }
535 
UpdatePropagationRtt(Timestamp at_time,TimeDelta propagation_rtt)536 void SendSideBandwidthEstimation::UpdatePropagationRtt(
537     Timestamp at_time,
538     TimeDelta propagation_rtt) {
539   rtt_backoff_.UpdatePropagationRtt(at_time, propagation_rtt);
540 }
541 
OnSentPacket(const SentPacket & sent_packet)542 void SendSideBandwidthEstimation::OnSentPacket(const SentPacket& sent_packet) {
543   // Only feedback-triggering packets will be reported here.
544   rtt_backoff_.last_packet_sent_ = sent_packet.send_time;
545 }
546 
IsInStartPhase(Timestamp at_time) const547 bool SendSideBandwidthEstimation::IsInStartPhase(Timestamp at_time) const {
548   return first_report_time_.IsInfinite() ||
549          at_time - first_report_time_ < kStartPhase;
550 }
551 
UpdateMinHistory(Timestamp at_time)552 void SendSideBandwidthEstimation::UpdateMinHistory(Timestamp at_time) {
553   // Remove old data points from history.
554   // Since history precision is in ms, add one so it is able to increase
555   // bitrate if it is off by as little as 0.5ms.
556   while (!min_bitrate_history_.empty() &&
557          at_time - min_bitrate_history_.front().first + TimeDelta::Millis(1) >
558              kBweIncreaseInterval) {
559     min_bitrate_history_.pop_front();
560   }
561 
562   // Typical minimum sliding-window algorithm: Pop values higher than current
563   // bitrate before pushing it.
564   while (!min_bitrate_history_.empty() &&
565          current_target_ <= min_bitrate_history_.back().second) {
566     min_bitrate_history_.pop_back();
567   }
568 
569   min_bitrate_history_.push_back(std::make_pair(at_time, current_target_));
570 }
571 
MaybeRampupOrBackoff(DataRate new_bitrate,Timestamp at_time)572 DataRate SendSideBandwidthEstimation::MaybeRampupOrBackoff(DataRate new_bitrate,
573                                                            Timestamp at_time) {
574   // TODO(crodbro): reuse this code in UpdateEstimate instead of current
575   // inlining of very similar functionality.
576   const TimeDelta time_since_loss_packet_report =
577       at_time - last_loss_packet_report_;
578   if (time_since_loss_packet_report < 1.2 * kMaxRtcpFeedbackInterval) {
579     new_bitrate = min_bitrate_history_.front().second * 1.08;
580     new_bitrate += DataRate::BitsPerSec(1000);
581   }
582   return new_bitrate;
583 }
584 
GetUpperLimit() const585 DataRate SendSideBandwidthEstimation::GetUpperLimit() const {
586   DataRate upper_limit = std::min(delay_based_limit_, receiver_limit_);
587   upper_limit = std::min(upper_limit, max_bitrate_configured_);
588   if (loss_based_bandwidth_estimation_.Enabled() &&
589       loss_based_bandwidth_estimation_.GetEstimate() > DataRate::Zero()) {
590     upper_limit =
591         std::min(upper_limit, loss_based_bandwidth_estimation_.GetEstimate());
592   }
593   return upper_limit;
594 }
595 
MaybeLogLowBitrateWarning(DataRate bitrate,Timestamp at_time)596 void SendSideBandwidthEstimation::MaybeLogLowBitrateWarning(DataRate bitrate,
597                                                             Timestamp at_time) {
598   if (at_time - last_low_bitrate_log_ > kLowBitrateLogPeriod) {
599     RTC_LOG(LS_WARNING) << "Estimated available bandwidth " << ToString(bitrate)
600                         << " is below configured min bitrate "
601                         << ToString(min_bitrate_configured_) << ".";
602     last_low_bitrate_log_ = at_time;
603   }
604 }
605 
MaybeLogLossBasedEvent(Timestamp at_time)606 void SendSideBandwidthEstimation::MaybeLogLossBasedEvent(Timestamp at_time) {
607   if (current_target_ != last_logged_target_ ||
608       last_fraction_loss_ != last_logged_fraction_loss_ ||
609       at_time - last_rtc_event_log_ > kRtcEventLogPeriod) {
610     event_log_->Log(std::make_unique<RtcEventBweUpdateLossBased>(
611         current_target_.bps(), last_fraction_loss_,
612         expected_packets_since_last_loss_update_));
613     last_logged_fraction_loss_ = last_fraction_loss_;
614     last_logged_target_ = current_target_;
615     last_rtc_event_log_ = at_time;
616   }
617 }
618 
UpdateTargetBitrate(DataRate new_bitrate,Timestamp at_time)619 void SendSideBandwidthEstimation::UpdateTargetBitrate(DataRate new_bitrate,
620                                                       Timestamp at_time) {
621   new_bitrate = std::min(new_bitrate, GetUpperLimit());
622   if (new_bitrate < min_bitrate_configured_) {
623     MaybeLogLowBitrateWarning(new_bitrate, at_time);
624     new_bitrate = min_bitrate_configured_;
625   }
626   current_target_ = new_bitrate;
627   MaybeLogLossBasedEvent(at_time);
628   link_capacity_.OnRateUpdate(acknowledged_rate_, current_target_, at_time);
629 }
630 
ApplyTargetLimits(Timestamp at_time)631 void SendSideBandwidthEstimation::ApplyTargetLimits(Timestamp at_time) {
632   UpdateTargetBitrate(current_target_, at_time);
633 }
634 }  // namespace webrtc
635