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> |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 14 | #include <cmath> |
Piotr Tworek | 5e4833c | 2017-12-12 12:09:31 +0100 | [diff] [blame] | 15 | #include <cstdio> |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 16 | #include <limits> |
| 17 | #include <string> |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 18 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 19 | #include "absl/memory/memory.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 { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 30 | const int64_t kBweIncreaseIntervalMs = 1000; |
| 31 | const int64_t kBweDecreaseIntervalMs = 300; |
| 32 | const int64_t kStartPhaseMs = 2000; |
| 33 | const int64_t kBweConverganceTimeMs = 20000; |
| 34 | const int kLimitNumPackets = 20; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 35 | const int kDefaultMaxBitrateBps = 1000000000; |
stefan | b6b0b92 | 2015-09-04 03:04:56 -0700 | [diff] [blame] | 36 | const int64_t kLowBitrateLogPeriodMs = 10000; |
stefan | 3821ff8 | 2016-09-04 05:07:26 -0700 | [diff] [blame] | 37 | const int64_t kRtcEventLogPeriodMs = 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. |
stefan | 5cb1982 | 2017-06-16 07:47:00 -0700 | [diff] [blame] | 39 | const int64_t kFeedbackIntervalMs = 5000; |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 40 | const int64_t kFeedbackTimeoutIntervals = 3; |
| 41 | const int64_t kTimeoutIntervalMs = 1000; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 42 | |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 43 | const float kDefaultLowLossThreshold = 0.02f; |
| 44 | const float kDefaultHighLossThreshold = 0.1f; |
| 45 | const int kDefaultBitrateThresholdKbps = 0; |
| 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; |
| 102 | *bitrate_threshold_kbps = kDefaultBitrateThresholdKbps; |
| 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 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 107 | SendSideBandwidthEstimation::SendSideBandwidthEstimation(RtcEventLog* event_log) |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 108 | : lost_packets_since_last_loss_update_(0), |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 109 | expected_packets_since_last_loss_update_(0), |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 110 | current_bitrate_bps_(0), |
michaelt | f082c2a | 2016-11-07 04:17:14 -0800 | [diff] [blame] | 111 | min_bitrate_configured_(congestion_controller::GetMinBitrateBps()), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 112 | max_bitrate_configured_(kDefaultMaxBitrateBps), |
stefan | b6b0b92 | 2015-09-04 03:04:56 -0700 | [diff] [blame] | 113 | last_low_bitrate_log_ms_(-1), |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 114 | has_decreased_since_last_fraction_loss_(false), |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 115 | last_feedback_ms_(-1), |
| 116 | last_packet_report_ms_(-1), |
| 117 | last_timeout_ms_(-1), |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 118 | last_fraction_loss_(0), |
stefan | 3821ff8 | 2016-09-04 05:07:26 -0700 | [diff] [blame] | 119 | last_logged_fraction_loss_(0), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 120 | last_round_trip_time_ms_(0), |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 121 | bwe_incoming_(0), |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 122 | delay_based_bitrate_bps_(0), |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 123 | time_last_decrease_ms_(0), |
stefan@webrtc.org | 548b228 | 2014-11-03 14:42:43 +0000 | [diff] [blame] | 124 | first_report_time_ms_(-1), |
| 125 | initially_lost_packets_(0), |
stefan@webrtc.org | db26247 | 2014-11-04 19:32:10 +0000 | [diff] [blame] | 126 | bitrate_at_2_seconds_kbps_(0), |
stefan@webrtc.org | 474e36e | 2015-01-19 15:44:47 +0000 | [diff] [blame] | 127 | uma_update_state_(kNoUpdate), |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 128 | uma_rtt_state_(kNoUpdate), |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 129 | rampup_uma_stats_updated_(kNumUmaRampupMetrics, false), |
stefan | 3821ff8 | 2016-09-04 05:07:26 -0700 | [diff] [blame] | 130 | event_log_(event_log), |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 131 | last_rtc_event_log_ms_(-1), |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 132 | in_timeout_experiment_( |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 133 | webrtc::field_trial::IsEnabled("WebRTC-FeedbackTimeout")), |
| 134 | low_loss_threshold_(kDefaultLowLossThreshold), |
| 135 | high_loss_threshold_(kDefaultHighLossThreshold), |
| 136 | bitrate_threshold_bps_(1000 * kDefaultBitrateThresholdKbps) { |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 137 | RTC_DCHECK(event_log); |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 138 | if (BweLossExperimentIsEnabled()) { |
| 139 | uint32_t bitrate_threshold_kbps; |
| 140 | if (ReadBweLossExperimentParameters(&low_loss_threshold_, |
| 141 | &high_loss_threshold_, |
| 142 | &bitrate_threshold_kbps)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 143 | RTC_LOG(LS_INFO) << "Enabled BweLossExperiment with parameters " |
| 144 | << low_loss_threshold_ << ", " << high_loss_threshold_ |
| 145 | << ", " << bitrate_threshold_kbps; |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 146 | bitrate_threshold_bps_ = bitrate_threshold_kbps * 1000; |
| 147 | } |
| 148 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 149 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 150 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 151 | SendSideBandwidthEstimation::~SendSideBandwidthEstimation() {} |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 152 | |
philipel | c6957c7 | 2016-04-28 15:52:49 +0200 | [diff] [blame] | 153 | void SendSideBandwidthEstimation::SetBitrates(int send_bitrate, |
| 154 | int min_bitrate, |
| 155 | int max_bitrate) { |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 156 | SetMinMaxBitrate(min_bitrate, max_bitrate); |
philipel | c6957c7 | 2016-04-28 15:52:49 +0200 | [diff] [blame] | 157 | if (send_bitrate > 0) |
| 158 | SetSendBitrate(send_bitrate); |
philipel | c6957c7 | 2016-04-28 15:52:49 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 161 | void SendSideBandwidthEstimation::SetSendBitrate(int bitrate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 162 | RTC_DCHECK_GT(bitrate, 0); |
Bjorn Terelius | 29cb0e7 | 2017-11-08 14:41:12 +0100 | [diff] [blame] | 163 | delay_based_bitrate_bps_ = 0; // Reset to avoid being capped by the estimate. |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 164 | CapBitrateToThresholds(Clock::GetRealTimeClock()->TimeInMilliseconds(), |
| 165 | bitrate); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 166 | // Clear last sent bitrate history so the new value can be used directly |
| 167 | // and not capped. |
| 168 | min_bitrate_history_.clear(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 171 | void SendSideBandwidthEstimation::SetMinMaxBitrate(int min_bitrate, |
| 172 | int max_bitrate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 173 | RTC_DCHECK_GE(min_bitrate, 0); |
michaelt | f082c2a | 2016-11-07 04:17:14 -0800 | [diff] [blame] | 174 | min_bitrate_configured_ = |
| 175 | std::max(min_bitrate, congestion_controller::GetMinBitrateBps()); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 176 | if (max_bitrate > 0) { |
| 177 | max_bitrate_configured_ = |
| 178 | std::max<uint32_t>(min_bitrate_configured_, max_bitrate); |
| 179 | } else { |
| 180 | max_bitrate_configured_ = kDefaultMaxBitrateBps; |
| 181 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 184 | int SendSideBandwidthEstimation::GetMinBitrate() const { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 185 | return min_bitrate_configured_; |
henrik.lundin@webrtc.org | 845862f | 2014-03-06 07:19:28 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 188 | void SendSideBandwidthEstimation::CurrentEstimate(int* bitrate, |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 189 | uint8_t* loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 190 | int64_t* rtt) const { |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 191 | *bitrate = current_bitrate_bps_; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 192 | *loss = last_fraction_loss_; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 193 | *rtt = last_round_trip_time_ms_; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 196 | void SendSideBandwidthEstimation::UpdateReceiverEstimate(int64_t now_ms, |
| 197 | uint32_t bandwidth) { |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 198 | bwe_incoming_ = bandwidth; |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 199 | CapBitrateToThresholds(now_ms, current_bitrate_bps_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 200 | } |
| 201 | |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 202 | void SendSideBandwidthEstimation::UpdateDelayBasedEstimate( |
| 203 | int64_t now_ms, |
| 204 | uint32_t bitrate_bps) { |
| 205 | delay_based_bitrate_bps_ = bitrate_bps; |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 206 | CapBitrateToThresholds(now_ms, current_bitrate_bps_); |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 207 | } |
| 208 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 209 | void SendSideBandwidthEstimation::UpdateReceiverBlock(uint8_t fraction_loss, |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 210 | int64_t rtt_ms, |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 211 | int number_of_packets, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 212 | int64_t now_ms) { |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 213 | const int kRoundingConstant = 128; |
| 214 | int packets_lost = (static_cast<int>(fraction_loss) * number_of_packets + |
| 215 | kRoundingConstant) >> |
| 216 | 8; |
| 217 | UpdatePacketsLost(packets_lost, number_of_packets, now_ms); |
| 218 | UpdateRtt(rtt_ms, now_ms); |
| 219 | } |
| 220 | |
| 221 | void SendSideBandwidthEstimation::UpdatePacketsLost(int packets_lost, |
| 222 | int number_of_packets, |
| 223 | int64_t now_ms) { |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 224 | last_feedback_ms_ = now_ms; |
stefan@webrtc.org | 83d4804 | 2014-11-10 13:55:16 +0000 | [diff] [blame] | 225 | if (first_report_time_ms_ == -1) |
| 226 | first_report_time_ms_ = now_ms; |
| 227 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 228 | // Check sequence number diff and weight loss report |
| 229 | if (number_of_packets > 0) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 230 | // Accumulate reports. |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 231 | lost_packets_since_last_loss_update_ += packets_lost; |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 232 | expected_packets_since_last_loss_update_ += number_of_packets; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 233 | |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 234 | // Don't generate a loss rate until it can be based on enough packets. |
| 235 | if (expected_packets_since_last_loss_update_ < kLimitNumPackets) |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 236 | return; |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 237 | |
| 238 | has_decreased_since_last_fraction_loss_ = false; |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 239 | int64_t lost_q8 = lost_packets_since_last_loss_update_ << 8; |
| 240 | int64_t expected = expected_packets_since_last_loss_update_; |
| 241 | last_fraction_loss_ = std::min<int>(lost_q8 / expected, 255); |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 242 | |
| 243 | // Reset accumulators. |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 244 | |
| 245 | lost_packets_since_last_loss_update_ = 0; |
pbos | b7edb88 | 2015-10-22 08:52:20 -0700 | [diff] [blame] | 246 | expected_packets_since_last_loss_update_ = 0; |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 247 | last_packet_report_ms_ = now_ms; |
| 248 | UpdateEstimate(now_ms); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 249 | } |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 250 | UpdateUmaStatsPacketsLost(now_ms, packets_lost); |
stefan@webrtc.org | db26247 | 2014-11-04 19:32:10 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 253 | void SendSideBandwidthEstimation::UpdateUmaStatsPacketsLost(int64_t now_ms, |
| 254 | int packets_lost) { |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 255 | int bitrate_kbps = static_cast<int>((current_bitrate_bps_ + 500) / 1000); |
stefan@webrtc.org | 474e36e | 2015-01-19 15:44:47 +0000 | [diff] [blame] | 256 | for (size_t i = 0; i < kNumUmaRampupMetrics; ++i) { |
| 257 | if (!rampup_uma_stats_updated_[i] && |
| 258 | bitrate_kbps >= kUmaRampupMetrics[i].bitrate_kbps) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 259 | RTC_HISTOGRAMS_COUNTS_100000(i, kUmaRampupMetrics[i].metric_name, |
| 260 | now_ms - first_report_time_ms_); |
stefan@webrtc.org | 474e36e | 2015-01-19 15:44:47 +0000 | [diff] [blame] | 261 | rampup_uma_stats_updated_[i] = true; |
| 262 | } |
| 263 | } |
stefan@webrtc.org | db26247 | 2014-11-04 19:32:10 +0000 | [diff] [blame] | 264 | if (IsInStartPhase(now_ms)) { |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 265 | initially_lost_packets_ += packets_lost; |
stefan@webrtc.org | db26247 | 2014-11-04 19:32:10 +0000 | [diff] [blame] | 266 | } else if (uma_update_state_ == kNoUpdate) { |
| 267 | uma_update_state_ = kFirstDone; |
stefan@webrtc.org | 474e36e | 2015-01-19 15:44:47 +0000 | [diff] [blame] | 268 | bitrate_at_2_seconds_kbps_ = bitrate_kbps; |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 269 | RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitiallyLostPackets", |
| 270 | initially_lost_packets_, 0, 100, 50); |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 271 | RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialBandwidthEstimate", |
| 272 | bitrate_at_2_seconds_kbps_, 0, 2000, 50); |
stefan@webrtc.org | db26247 | 2014-11-04 19:32:10 +0000 | [diff] [blame] | 273 | } else if (uma_update_state_ == kFirstDone && |
| 274 | now_ms - first_report_time_ms_ >= kBweConverganceTimeMs) { |
| 275 | uma_update_state_ = kDone; |
stefan@webrtc.org | 474e36e | 2015-01-19 15:44:47 +0000 | [diff] [blame] | 276 | int bitrate_diff_kbps = |
| 277 | std::max(bitrate_at_2_seconds_kbps_ - bitrate_kbps, 0); |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 278 | RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialVsConvergedDiff", bitrate_diff_kbps, |
| 279 | 0, 2000, 50); |
stefan@webrtc.org | 548b228 | 2014-11-03 14:42:43 +0000 | [diff] [blame] | 280 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 283 | void SendSideBandwidthEstimation::UpdateRtt(int64_t rtt_ms, int64_t now_ms) { |
| 284 | // Update RTT if we were able to compute an RTT based on this RTCP. |
| 285 | // FlexFEC doesn't send RTCP SR, which means we won't be able to compute RTT. |
| 286 | if (rtt_ms > 0) |
| 287 | last_round_trip_time_ms_ = rtt_ms; |
| 288 | |
| 289 | if (!IsInStartPhase(now_ms) && uma_rtt_state_ == kNoUpdate) { |
| 290 | uma_rtt_state_ = kDone; |
| 291 | RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialRtt", static_cast<int>(rtt_ms), 0, |
| 292 | 2000, 50); |
| 293 | } |
| 294 | } |
| 295 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 296 | void SendSideBandwidthEstimation::UpdateEstimate(int64_t now_ms) { |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 297 | uint32_t new_bitrate = current_bitrate_bps_; |
stefan | fa15669 | 2016-01-21 08:55:03 -0800 | [diff] [blame] | 298 | // We trust the REMB and/or delay-based estimate during the first 2 seconds if |
| 299 | // we haven't had any packet loss reported, to allow startup bitrate probing. |
| 300 | if (last_fraction_loss_ == 0 && IsInStartPhase(now_ms)) { |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 301 | new_bitrate = std::max(bwe_incoming_, new_bitrate); |
| 302 | new_bitrate = std::max(delay_based_bitrate_bps_, new_bitrate); |
| 303 | |
| 304 | if (new_bitrate != current_bitrate_bps_) { |
stefan | fa15669 | 2016-01-21 08:55:03 -0800 | [diff] [blame] | 305 | min_bitrate_history_.clear(); |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 306 | min_bitrate_history_.push_back( |
| 307 | std::make_pair(now_ms, current_bitrate_bps_)); |
| 308 | CapBitrateToThresholds(now_ms, new_bitrate); |
stefan | fa15669 | 2016-01-21 08:55:03 -0800 | [diff] [blame] | 309 | return; |
| 310 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 311 | } |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 312 | UpdateMinHistory(now_ms); |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 313 | if (last_packet_report_ms_ == -1) { |
| 314 | // No feedback received. |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 315 | CapBitrateToThresholds(now_ms, current_bitrate_bps_); |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | int64_t time_since_packet_report_ms = now_ms - last_packet_report_ms_; |
| 319 | int64_t time_since_feedback_ms = now_ms - last_feedback_ms_; |
| 320 | if (time_since_packet_report_ms < 1.2 * kFeedbackIntervalMs) { |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 321 | // We only care about loss above a given bitrate threshold. |
| 322 | float loss = last_fraction_loss_ / 256.0f; |
| 323 | // We only make decisions based on loss when the bitrate is above a |
| 324 | // threshold. This is a crude way of handling loss which is uncorrelated |
| 325 | // to congestion. |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 326 | if (current_bitrate_bps_ < bitrate_threshold_bps_ || |
| 327 | loss <= low_loss_threshold_) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 328 | // Loss < 2%: Increase rate by 8% of the min bitrate in the last |
| 329 | // kBweIncreaseIntervalMs. |
| 330 | // Note that by remembering the bitrate over the last second one can |
| 331 | // rampup up one second faster than if only allowed to start ramping |
| 332 | // at 8% per second rate now. E.g.: |
| 333 | // If sending a constant 100kbps it can rampup immediatly to 108kbps |
| 334 | // whenever a receiver report is received with lower packet loss. |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 335 | // If instead one would do: current_bitrate_bps_ *= 1.08^(delta time), |
| 336 | // 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] | 337 | // 108kbps. |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 338 | new_bitrate = static_cast<uint32_t>( |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 339 | min_bitrate_history_.front().second * 1.08 + 0.5); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 340 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 341 | // Add 1 kbps extra, just to make sure that we do not get stuck |
| 342 | // (gives a little extra increase at low rates, negligible at higher |
| 343 | // rates). |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 344 | new_bitrate += 1000; |
| 345 | } else if (current_bitrate_bps_ > bitrate_threshold_bps_) { |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 346 | if (loss <= high_loss_threshold_) { |
| 347 | // Loss between 2% - 10%: Do nothing. |
| 348 | } else { |
| 349 | // Loss > 10%: Limit the rate decreases to once a kBweDecreaseIntervalMs |
| 350 | // + rtt. |
| 351 | if (!has_decreased_since_last_fraction_loss_ && |
| 352 | (now_ms - time_last_decrease_ms_) >= |
| 353 | (kBweDecreaseIntervalMs + last_round_trip_time_ms_)) { |
| 354 | time_last_decrease_ms_ = now_ms; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 355 | |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 356 | // Reduce rate: |
| 357 | // newRate = rate * (1 - 0.5*lossRate); |
| 358 | // where packetLoss = 256*lossRate; |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 359 | new_bitrate = static_cast<uint32_t>( |
| 360 | (current_bitrate_bps_ * |
| 361 | static_cast<double>(512 - last_fraction_loss_)) / |
Stefan Holmer | 9c79ed9 | 2017-03-31 15:53:27 +0200 | [diff] [blame] | 362 | 512.0); |
| 363 | has_decreased_since_last_fraction_loss_ = true; |
| 364 | } |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 365 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 366 | } |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 367 | } else if (time_since_feedback_ms > |
| 368 | kFeedbackTimeoutIntervals * kFeedbackIntervalMs && |
| 369 | (last_timeout_ms_ == -1 || |
| 370 | now_ms - last_timeout_ms_ > kTimeoutIntervalMs)) { |
| 371 | if (in_timeout_experiment_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 372 | RTC_LOG(LS_WARNING) << "Feedback timed out (" << time_since_feedback_ms |
| 373 | << " ms), reducing bitrate."; |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 374 | new_bitrate *= 0.8; |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 375 | // Reset accumulators since we've already acted on missing feedback and |
| 376 | // shouldn't to act again on these old lost packets. |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 377 | lost_packets_since_last_loss_update_ = 0; |
Stefan Holmer | 52200d0 | 2016-09-20 14:14:23 +0200 | [diff] [blame] | 378 | expected_packets_since_last_loss_update_ = 0; |
| 379 | last_timeout_ms_ = now_ms; |
| 380 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 381 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 382 | |
| 383 | CapBitrateToThresholds(now_ms, new_bitrate); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 384 | } |
andresp@webrtc.org | 4e69f78 | 2014-03-17 17:07:48 +0000 | [diff] [blame] | 385 | |
stefan@webrtc.org | 548b228 | 2014-11-03 14:42:43 +0000 | [diff] [blame] | 386 | bool SendSideBandwidthEstimation::IsInStartPhase(int64_t now_ms) const { |
| 387 | return first_report_time_ms_ == -1 || |
| 388 | now_ms - first_report_time_ms_ < kStartPhaseMs; |
| 389 | } |
| 390 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 391 | void SendSideBandwidthEstimation::UpdateMinHistory(int64_t now_ms) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 392 | // Remove old data points from history. |
| 393 | // Since history precision is in ms, add one so it is able to increase |
| 394 | // bitrate if it is off by as little as 0.5ms. |
| 395 | while (!min_bitrate_history_.empty() && |
| 396 | now_ms - min_bitrate_history_.front().first + 1 > |
| 397 | kBweIncreaseIntervalMs) { |
| 398 | min_bitrate_history_.pop_front(); |
| 399 | } |
| 400 | |
| 401 | // Typical minimum sliding-window algorithm: Pop values higher than current |
| 402 | // bitrate before pushing it. |
| 403 | while (!min_bitrate_history_.empty() && |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 404 | current_bitrate_bps_ <= min_bitrate_history_.back().second) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 405 | min_bitrate_history_.pop_back(); |
| 406 | } |
| 407 | |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 408 | min_bitrate_history_.push_back(std::make_pair(now_ms, current_bitrate_bps_)); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 409 | } |
| 410 | |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 411 | void SendSideBandwidthEstimation::CapBitrateToThresholds(int64_t now_ms, |
| 412 | uint32_t bitrate_bps) { |
| 413 | if (bwe_incoming_ > 0 && bitrate_bps > bwe_incoming_) { |
| 414 | bitrate_bps = bwe_incoming_; |
andresp@webrtc.org | 4e69f78 | 2014-03-17 17:07:48 +0000 | [diff] [blame] | 415 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 416 | if (delay_based_bitrate_bps_ > 0 && bitrate_bps > delay_based_bitrate_bps_) { |
| 417 | bitrate_bps = delay_based_bitrate_bps_; |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 418 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 419 | if (bitrate_bps > max_bitrate_configured_) { |
| 420 | bitrate_bps = max_bitrate_configured_; |
andresp@webrtc.org | 4e69f78 | 2014-03-17 17:07:48 +0000 | [diff] [blame] | 421 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 422 | if (bitrate_bps < min_bitrate_configured_) { |
stefan | b6b0b92 | 2015-09-04 03:04:56 -0700 | [diff] [blame] | 423 | if (last_low_bitrate_log_ms_ == -1 || |
| 424 | now_ms - last_low_bitrate_log_ms_ > kLowBitrateLogPeriodMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 425 | RTC_LOG(LS_WARNING) << "Estimated available bandwidth " |
| 426 | << bitrate_bps / 1000 |
| 427 | << " kbps is below configured min bitrate " |
| 428 | << min_bitrate_configured_ / 1000 << " kbps."; |
stefan | b6b0b92 | 2015-09-04 03:04:56 -0700 | [diff] [blame] | 429 | last_low_bitrate_log_ms_ = now_ms; |
| 430 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 431 | bitrate_bps = min_bitrate_configured_; |
andresp@webrtc.org | 4e69f78 | 2014-03-17 17:07:48 +0000 | [diff] [blame] | 432 | } |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 433 | |
| 434 | if (bitrate_bps != current_bitrate_bps_ || |
| 435 | last_fraction_loss_ != last_logged_fraction_loss_ || |
| 436 | now_ms - last_rtc_event_log_ms_ > kRtcEventLogPeriodMs) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 437 | event_log_->Log(absl::make_unique<RtcEventBweUpdateLossBased>( |
Elad Alon | 4a87e1c | 2017-10-03 16:11:34 +0200 | [diff] [blame] | 438 | bitrate_bps, last_fraction_loss_, |
| 439 | expected_packets_since_last_loss_update_)); |
philipel | 1b96531 | 2017-04-18 06:55:32 -0700 | [diff] [blame] | 440 | last_logged_fraction_loss_ = last_fraction_loss_; |
| 441 | last_rtc_event_log_ms_ = now_ms; |
| 442 | } |
| 443 | current_bitrate_bps_ = bitrate_bps; |
andresp@webrtc.org | 4e69f78 | 2014-03-17 17:07:48 +0000 | [diff] [blame] | 444 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 445 | } // namespace webrtc |