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