blob: 4694aa622d3ce747d035ae277fede498b93b922e [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/bitrate_controller/send_side_bandwidth_estimation.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000012
jbauchf91e6d02016-01-24 23:05:21 -080013#include <algorithm>
Piotr Tworek5e4833c2017-12-12 12:09:31 +010014#include <cstdio>
Stefan Holmer9c79ed92017-03-31 15:53:27 +020015#include <limits>
16#include <string>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000017
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "logging/rtc_event_log/events/rtc_event.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020020#include "logging/rtc_event_log/events/rtc_event_bwe_update_loss_based.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#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.org1cd11622012-04-19 12:13:52 +000027
28namespace webrtc {
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000029namespace {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020030constexpr TimeDelta kBweIncreaseInterval = TimeDelta::Millis<1000>();
31constexpr TimeDelta kBweDecreaseInterval = TimeDelta::Millis<300>();
32constexpr TimeDelta kStartPhase = TimeDelta::Millis<2000>();
33constexpr TimeDelta kBweConverganceTime = TimeDelta::Millis<20000>();
34constexpr int kLimitNumPackets = 20;
35constexpr DataRate kDefaultMaxBitrate = DataRate::BitsPerSec<1000000000>();
36constexpr TimeDelta kLowBitrateLogPeriod = TimeDelta::Millis<10000>();
37constexpr TimeDelta kRtcEventLogPeriod = TimeDelta::Millis<5000>();
Stefan Holmer52200d02016-09-20 14:14:23 +020038// Expecting that RTCP feedback is sent uniformly within [0.5, 1.5]s intervals.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020039constexpr TimeDelta kMaxRtcpFeedbackInterval = TimeDelta::Millis<5000>();
40constexpr int kFeedbackTimeoutIntervals = 3;
41constexpr TimeDelta kTimeoutInterval = TimeDelta::Millis<1000>();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000042
Sebastian Jansson7c1744d2018-10-08 11:00:50 +020043constexpr float kDefaultLowLossThreshold = 0.02f;
44constexpr float kDefaultHighLossThreshold = 0.1f;
45constexpr DataRate kDefaultBitrateThreshold = DataRate::Zero();
Stefan Holmer9c79ed92017-03-31 15:53:27 +020046
stefan@webrtc.org474e36e2015-01-19 15:44:47 +000047struct UmaRampUpMetric {
48 const char* metric_name;
49 int bitrate_kbps;
50};
51
52const UmaRampUpMetric kUmaRampupMetrics[] = {
53 {"WebRTC.BWE.RampUpTimeTo500kbpsInMs", 500},
54 {"WebRTC.BWE.RampUpTimeTo1000kbpsInMs", 1000},
55 {"WebRTC.BWE.RampUpTimeTo2000kbpsInMs", 2000}};
56const size_t kNumUmaRampupMetrics =
57 sizeof(kUmaRampupMetrics) / sizeof(kUmaRampupMetrics[0]);
58
Stefan Holmer9c79ed92017-03-31 15:53:27 +020059const char kBweLosExperiment[] = "WebRTC-BweLossExperiment";
60
61bool 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
68bool 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 Bonadei675513b2017-11-09 11:09:25 +010098 RTC_LOG(LS_WARNING) << "Failed to parse parameters for BweLossExperiment "
99 "experiment from field trial string. Using default.";
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200100 *low_loss_threshold = kDefaultLowLossThreshold;
101 *high_loss_threshold = kDefaultHighLossThreshold;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200102 *bitrate_threshold_kbps = kDefaultBitrateThreshold.kbps();
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200103 return false;
104}
jbauchf91e6d02016-01-24 23:05:21 -0800105} // namespace
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000106
Sebastian Jansson24643482018-11-14 14:19:45 +0100107RttBasedBackoff::RttBasedBackoff()
108 : rtt_limit_("limit", TimeDelta::PlusInfinity()),
109 drop_fraction_("fraction", 0.5),
110 drop_interval_("interval", TimeDelta::ms(300)),
111 persist_on_route_change_("persist"),
112 // By initializing this to plus infinity, we make sure that we never
113 // trigger rtt backoff unless packet feedback is enabled.
114 last_propagation_rtt_update_(Timestamp::PlusInfinity()),
115 last_propagation_rtt_(TimeDelta::Zero()) {
116 ParseFieldTrial({&rtt_limit_, &drop_fraction_, &drop_interval_,
117 &persist_on_route_change_},
118 field_trial::FindFullName("WebRTC-Bwe-MaxRttLimit"));
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200119}
Sebastian Jansson24643482018-11-14 14:19:45 +0100120
121void RttBasedBackoff::OnRouteChange() {
122 if (!persist_on_route_change_) {
123 last_propagation_rtt_update_ = Timestamp::PlusInfinity();
124 last_propagation_rtt_ = TimeDelta::Zero();
125 }
126}
127
128void RttBasedBackoff::UpdatePropagationRtt(Timestamp at_time,
129 TimeDelta propagation_rtt) {
130 last_propagation_rtt_update_ = at_time;
131 last_propagation_rtt_ = propagation_rtt;
132}
133
134TimeDelta RttBasedBackoff::RttLowerBound(Timestamp at_time) const {
135 // TODO(srte): Use time since last unacknowledged packet for this.
136 TimeDelta time_since_rtt = at_time - last_propagation_rtt_update_;
137 return time_since_rtt + last_propagation_rtt_;
138}
139
140RttBasedBackoff::~RttBasedBackoff() = default;
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200141
ivoc14d5dbe2016-07-04 07:06:55 -0700142SendSideBandwidthEstimation::SendSideBandwidthEstimation(RtcEventLog* event_log)
Sebastian Jansson24643482018-11-14 14:19:45 +0100143 : lost_packets_since_last_loss_update_(0),
pbosb7edb882015-10-22 08:52:20 -0700144 expected_packets_since_last_loss_update_(0),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200145 current_bitrate_(DataRate::Zero()),
146 min_bitrate_configured_(
147 DataRate::bps(congestion_controller::GetMinBitrateBps())),
148 max_bitrate_configured_(kDefaultMaxBitrate),
149 last_low_bitrate_log_(Timestamp::MinusInfinity()),
pbosb7edb882015-10-22 08:52:20 -0700150 has_decreased_since_last_fraction_loss_(false),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200151 last_loss_feedback_(Timestamp::MinusInfinity()),
152 last_loss_packet_report_(Timestamp::MinusInfinity()),
153 last_timeout_(Timestamp::MinusInfinity()),
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000154 last_fraction_loss_(0),
stefan3821ff82016-09-04 05:07:26 -0700155 last_logged_fraction_loss_(0),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200156 last_round_trip_time_(TimeDelta::Zero()),
157 bwe_incoming_(DataRate::Zero()),
158 delay_based_bitrate_(DataRate::Zero()),
159 time_last_decrease_(Timestamp::MinusInfinity()),
160 first_report_time_(Timestamp::MinusInfinity()),
stefan@webrtc.org548b2282014-11-03 14:42:43 +0000161 initially_lost_packets_(0),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200162 bitrate_at_2_seconds_(DataRate::Zero()),
stefan@webrtc.org474e36e2015-01-19 15:44:47 +0000163 uma_update_state_(kNoUpdate),
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100164 uma_rtt_state_(kNoUpdate),
terelius006d93d2015-11-05 12:02:15 -0800165 rampup_uma_stats_updated_(kNumUmaRampupMetrics, false),
stefan3821ff82016-09-04 05:07:26 -0700166 event_log_(event_log),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200167 last_rtc_event_log_(Timestamp::MinusInfinity()),
sprangc1b57a12017-02-28 08:50:47 -0800168 in_timeout_experiment_(
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200169 webrtc::field_trial::IsEnabled("WebRTC-FeedbackTimeout")),
170 low_loss_threshold_(kDefaultLowLossThreshold),
171 high_loss_threshold_(kDefaultHighLossThreshold),
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200172 bitrate_threshold_(kDefaultBitrateThreshold) {
ivoc14d5dbe2016-07-04 07:06:55 -0700173 RTC_DCHECK(event_log);
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200174 if (BweLossExperimentIsEnabled()) {
175 uint32_t bitrate_threshold_kbps;
176 if (ReadBweLossExperimentParameters(&low_loss_threshold_,
177 &high_loss_threshold_,
178 &bitrate_threshold_kbps)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(LS_INFO) << "Enabled BweLossExperiment with parameters "
180 << low_loss_threshold_ << ", " << high_loss_threshold_
181 << ", " << bitrate_threshold_kbps;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200182 bitrate_threshold_ = DataRate::kbps(bitrate_threshold_kbps);
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200183 }
184 }
ivoc14d5dbe2016-07-04 07:06:55 -0700185}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000186
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000187SendSideBandwidthEstimation::~SendSideBandwidthEstimation() {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000188
Sebastian Jansson24643482018-11-14 14:19:45 +0100189void SendSideBandwidthEstimation::OnRouteChange() {
190 lost_packets_since_last_loss_update_ = 0;
191 expected_packets_since_last_loss_update_ = 0;
192 current_bitrate_ = DataRate::Zero();
193 min_bitrate_configured_ =
194 DataRate::bps(congestion_controller::GetMinBitrateBps());
195 max_bitrate_configured_ = kDefaultMaxBitrate;
196 last_low_bitrate_log_ = Timestamp::MinusInfinity();
197 has_decreased_since_last_fraction_loss_ = false;
198 last_loss_feedback_ = Timestamp::MinusInfinity();
199 last_loss_packet_report_ = Timestamp::MinusInfinity();
200 last_timeout_ = Timestamp::MinusInfinity();
201 last_fraction_loss_ = 0;
202 last_logged_fraction_loss_ = 0;
203 last_round_trip_time_ = TimeDelta::Zero();
204 bwe_incoming_ = DataRate::Zero();
205 delay_based_bitrate_ = DataRate::Zero();
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 last_rtc_event_log_ = Timestamp::MinusInfinity();
213
214 rtt_backoff_.OnRouteChange();
215}
216
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200217void SendSideBandwidthEstimation::SetBitrates(
218 absl::optional<DataRate> send_bitrate,
219 DataRate min_bitrate,
220 DataRate max_bitrate,
221 Timestamp at_time) {
philipel1b965312017-04-18 06:55:32 -0700222 SetMinMaxBitrate(min_bitrate, max_bitrate);
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200223 if (send_bitrate)
224 SetSendBitrate(*send_bitrate, at_time);
philipelc6957c72016-04-28 15:52:49 +0200225}
226
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200227void SendSideBandwidthEstimation::SetSendBitrate(DataRate bitrate,
228 Timestamp at_time) {
229 RTC_DCHECK(bitrate > DataRate::Zero());
230 // Reset to avoid being capped by the estimate.
231 delay_based_bitrate_ = DataRate::Zero();
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100232 if (loss_based_bandwidth_estimation_.Enabled()) {
233 loss_based_bandwidth_estimation_.MaybeReset(bitrate);
234 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200235 CapBitrateToThresholds(at_time, bitrate);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000236 // Clear last sent bitrate history so the new value can be used directly
237 // and not capped.
238 min_bitrate_history_.clear();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000239}
240
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200241void SendSideBandwidthEstimation::SetMinMaxBitrate(DataRate min_bitrate,
242 DataRate max_bitrate) {
michaeltf082c2a2016-11-07 04:17:14 -0800243 min_bitrate_configured_ =
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200244 std::max(min_bitrate, congestion_controller::GetMinBitrate());
245 if (max_bitrate > DataRate::Zero() && max_bitrate.IsFinite()) {
246 max_bitrate_configured_ = std::max(min_bitrate_configured_, max_bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100247 } else {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200248 max_bitrate_configured_ = kDefaultMaxBitrate;
Stefan Holmere5904162015-03-26 11:11:06 +0100249 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000250}
251
Stefan Holmere5904162015-03-26 11:11:06 +0100252int SendSideBandwidthEstimation::GetMinBitrate() const {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200253 return min_bitrate_configured_.bps<int>();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000254}
255
Stefan Holmere5904162015-03-26 11:11:06 +0100256void SendSideBandwidthEstimation::CurrentEstimate(int* bitrate,
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000257 uint8_t* loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000258 int64_t* rtt) const {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200259 *bitrate = current_bitrate_.bps<int>();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000260 *loss = last_fraction_loss_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200261 *rtt = last_round_trip_time_.ms<int64_t>();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000262}
263
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200264void SendSideBandwidthEstimation::UpdateReceiverEstimate(Timestamp at_time,
265 DataRate bandwidth) {
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000266 bwe_incoming_ = bandwidth;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200267 CapBitrateToThresholds(at_time, current_bitrate_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000268}
269
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200270void SendSideBandwidthEstimation::UpdateDelayBasedEstimate(Timestamp at_time,
271 DataRate bitrate) {
272 delay_based_bitrate_ = bitrate;
273 CapBitrateToThresholds(at_time, current_bitrate_);
stefan32f81542016-01-20 07:13:58 -0800274}
275
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100276void SendSideBandwidthEstimation::IncomingPacketFeedbackVector(
277 const TransportPacketsFeedback& report,
Sebastian Janssonb6787bc2018-11-19 18:01:17 +0100278 absl::optional<DataRate> acked_bitrate) {
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100279 if (!loss_based_bandwidth_estimation_.Enabled())
280 return;
Sebastian Janssonb6787bc2018-11-19 18:01:17 +0100281 if (acked_bitrate) {
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100282 loss_based_bandwidth_estimation_.UpdateAcknowledgedBitrate(
Sebastian Janssonb6787bc2018-11-19 18:01:17 +0100283 *acked_bitrate, report.feedback_time);
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100284 }
285 loss_based_bandwidth_estimation_.UpdateLossStatistics(report.packet_feedbacks,
286 report.feedback_time);
287}
288
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000289void SendSideBandwidthEstimation::UpdateReceiverBlock(uint8_t fraction_loss,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200290 TimeDelta rtt,
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000291 int number_of_packets,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200292 Timestamp at_time) {
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100293 const int kRoundingConstant = 128;
294 int packets_lost = (static_cast<int>(fraction_loss) * number_of_packets +
295 kRoundingConstant) >>
296 8;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200297 UpdatePacketsLost(packets_lost, number_of_packets, at_time);
298 UpdateRtt(rtt, at_time);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100299}
300
301void SendSideBandwidthEstimation::UpdatePacketsLost(int packets_lost,
302 int number_of_packets,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200303 Timestamp at_time) {
304 last_loss_feedback_ = at_time;
305 if (first_report_time_.IsInfinite())
306 first_report_time_ = at_time;
stefan@webrtc.org83d48042014-11-10 13:55:16 +0000307
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000308 // Check sequence number diff and weight loss report
309 if (number_of_packets > 0) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000310 // Accumulate reports.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100311 lost_packets_since_last_loss_update_ += packets_lost;
pbosb7edb882015-10-22 08:52:20 -0700312 expected_packets_since_last_loss_update_ += number_of_packets;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000313
pbosb7edb882015-10-22 08:52:20 -0700314 // Don't generate a loss rate until it can be based on enough packets.
315 if (expected_packets_since_last_loss_update_ < kLimitNumPackets)
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000316 return;
pbosb7edb882015-10-22 08:52:20 -0700317
318 has_decreased_since_last_fraction_loss_ = false;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100319 int64_t lost_q8 = lost_packets_since_last_loss_update_ << 8;
320 int64_t expected = expected_packets_since_last_loss_update_;
321 last_fraction_loss_ = std::min<int>(lost_q8 / expected, 255);
pbosb7edb882015-10-22 08:52:20 -0700322
323 // Reset accumulators.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100324
325 lost_packets_since_last_loss_update_ = 0;
pbosb7edb882015-10-22 08:52:20 -0700326 expected_packets_since_last_loss_update_ = 0;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200327 last_loss_packet_report_ = at_time;
328 UpdateEstimate(at_time);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000329 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200330 UpdateUmaStatsPacketsLost(at_time, packets_lost);
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000331}
332
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200333void SendSideBandwidthEstimation::UpdateUmaStatsPacketsLost(Timestamp at_time,
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100334 int packets_lost) {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200335 DataRate bitrate_kbps = DataRate::kbps((current_bitrate_.bps() + 500) / 1000);
stefan@webrtc.org474e36e2015-01-19 15:44:47 +0000336 for (size_t i = 0; i < kNumUmaRampupMetrics; ++i) {
337 if (!rampup_uma_stats_updated_[i] &&
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200338 bitrate_kbps.kbps() >= kUmaRampupMetrics[i].bitrate_kbps) {
asapersson1d02d3e2016-09-09 22:40:25 -0700339 RTC_HISTOGRAMS_COUNTS_100000(i, kUmaRampupMetrics[i].metric_name,
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200340 (at_time - first_report_time_).ms());
stefan@webrtc.org474e36e2015-01-19 15:44:47 +0000341 rampup_uma_stats_updated_[i] = true;
342 }
343 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200344 if (IsInStartPhase(at_time)) {
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100345 initially_lost_packets_ += packets_lost;
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000346 } else if (uma_update_state_ == kNoUpdate) {
347 uma_update_state_ = kFirstDone;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200348 bitrate_at_2_seconds_ = bitrate_kbps;
asapersson1d02d3e2016-09-09 22:40:25 -0700349 RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitiallyLostPackets",
350 initially_lost_packets_, 0, 100, 50);
asapersson1d02d3e2016-09-09 22:40:25 -0700351 RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialBandwidthEstimate",
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200352 bitrate_at_2_seconds_.kbps(), 0, 2000, 50);
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000353 } else if (uma_update_state_ == kFirstDone &&
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200354 at_time - first_report_time_ >= kBweConverganceTime) {
stefan@webrtc.orgdb262472014-11-04 19:32:10 +0000355 uma_update_state_ = kDone;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200356 int bitrate_diff_kbps = std::max(
357 bitrate_at_2_seconds_.kbps<int>() - bitrate_kbps.kbps<int>(), 0);
asapersson1d02d3e2016-09-09 22:40:25 -0700358 RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialVsConvergedDiff", bitrate_diff_kbps,
359 0, 2000, 50);
stefan@webrtc.org548b2282014-11-03 14:42:43 +0000360 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000361}
362
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200363void SendSideBandwidthEstimation::UpdateRtt(TimeDelta rtt, Timestamp at_time) {
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100364 // Update RTT if we were able to compute an RTT based on this RTCP.
365 // FlexFEC doesn't send RTCP SR, which means we won't be able to compute RTT.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200366 if (rtt > TimeDelta::Zero())
367 last_round_trip_time_ = rtt;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100368
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200369 if (!IsInStartPhase(at_time) && uma_rtt_state_ == kNoUpdate) {
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100370 uma_rtt_state_ = kDone;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200371 RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialRtt", rtt.ms<int>(), 0, 2000, 50);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100372 }
373}
374
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200375void SendSideBandwidthEstimation::UpdateEstimate(Timestamp at_time) {
376 DataRate new_bitrate = current_bitrate_;
Sebastian Jansson24643482018-11-14 14:19:45 +0100377 if (rtt_backoff_.RttLowerBound(at_time) > rtt_backoff_.rtt_limit_) {
378 if (at_time - time_last_decrease_ >= rtt_backoff_.drop_interval_) {
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200379 time_last_decrease_ = at_time;
Sebastian Jansson24643482018-11-14 14:19:45 +0100380 new_bitrate = current_bitrate_ * rtt_backoff_.drop_fraction_;
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200381 }
382 CapBitrateToThresholds(at_time, new_bitrate);
383 return;
384 }
385
stefanfa156692016-01-21 08:55:03 -0800386 // We trust the REMB and/or delay-based estimate during the first 2 seconds if
387 // we haven't had any packet loss reported, to allow startup bitrate probing.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200388 if (last_fraction_loss_ == 0 && IsInStartPhase(at_time)) {
philipel1b965312017-04-18 06:55:32 -0700389 new_bitrate = std::max(bwe_incoming_, new_bitrate);
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200390 new_bitrate = std::max(delay_based_bitrate_, new_bitrate);
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100391 if (loss_based_bandwidth_estimation_.Enabled()) {
392 loss_based_bandwidth_estimation_.SetInitialBitrate(new_bitrate);
393 }
philipel1b965312017-04-18 06:55:32 -0700394
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200395 if (new_bitrate != current_bitrate_) {
stefanfa156692016-01-21 08:55:03 -0800396 min_bitrate_history_.clear();
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200397 min_bitrate_history_.push_back(std::make_pair(at_time, current_bitrate_));
398 CapBitrateToThresholds(at_time, new_bitrate);
stefanfa156692016-01-21 08:55:03 -0800399 return;
400 }
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000401 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200402 UpdateMinHistory(at_time);
403 if (last_loss_packet_report_.IsInfinite()) {
Stefan Holmer52200d02016-09-20 14:14:23 +0200404 // No feedback received.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200405 CapBitrateToThresholds(at_time, current_bitrate_);
Stefan Holmer52200d02016-09-20 14:14:23 +0200406 return;
407 }
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100408
409 if (loss_based_bandwidth_estimation_.Enabled()) {
410 loss_based_bandwidth_estimation_.Update(
411 at_time, min_bitrate_history_.front().second, last_round_trip_time_);
412 new_bitrate = MaybeRampupOrBackoff(new_bitrate, at_time);
413 CapBitrateToThresholds(at_time, new_bitrate);
414 return;
415 }
416
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200417 TimeDelta time_since_loss_packet_report = at_time - last_loss_packet_report_;
418 TimeDelta time_since_loss_feedback = at_time - last_loss_feedback_;
419 if (time_since_loss_packet_report < 1.2 * kMaxRtcpFeedbackInterval) {
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200420 // We only care about loss above a given bitrate threshold.
421 float loss = last_fraction_loss_ / 256.0f;
422 // We only make decisions based on loss when the bitrate is above a
423 // threshold. This is a crude way of handling loss which is uncorrelated
424 // to congestion.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200425 if (current_bitrate_ < bitrate_threshold_ || loss <= low_loss_threshold_) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000426 // Loss < 2%: Increase rate by 8% of the min bitrate in the last
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200427 // kBweIncreaseInterval.
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000428 // Note that by remembering the bitrate over the last second one can
429 // rampup up one second faster than if only allowed to start ramping
430 // at 8% per second rate now. E.g.:
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100431 // If sending a constant 100kbps it can rampup immediately to 108kbps
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000432 // whenever a receiver report is received with lower packet loss.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200433 // If instead one would do: current_bitrate_ *= 1.08^(delta time),
philipel1b965312017-04-18 06:55:32 -0700434 // it would take over one second since the lower packet loss to achieve
Stefan Holmer52200d02016-09-20 14:14:23 +0200435 // 108kbps.
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200436 new_bitrate =
437 DataRate::bps(min_bitrate_history_.front().second.bps() * 1.08 + 0.5);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000438
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000439 // Add 1 kbps extra, just to make sure that we do not get stuck
440 // (gives a little extra increase at low rates, negligible at higher
441 // rates).
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200442 new_bitrate += DataRate::bps(1000);
443 } else if (current_bitrate_ > bitrate_threshold_) {
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200444 if (loss <= high_loss_threshold_) {
445 // Loss between 2% - 10%: Do nothing.
446 } else {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200447 // Loss > 10%: Limit the rate decreases to once a kBweDecreaseInterval
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200448 // + rtt.
449 if (!has_decreased_since_last_fraction_loss_ &&
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200450 (at_time - time_last_decrease_) >=
451 (kBweDecreaseInterval + last_round_trip_time_)) {
452 time_last_decrease_ = at_time;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000453
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200454 // Reduce rate:
455 // newRate = rate * (1 - 0.5*lossRate);
456 // where packetLoss = 256*lossRate;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200457 new_bitrate =
458 DataRate::bps((current_bitrate_.bps() *
459 static_cast<double>(512 - last_fraction_loss_)) /
460 512.0);
Stefan Holmer9c79ed92017-03-31 15:53:27 +0200461 has_decreased_since_last_fraction_loss_ = true;
462 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000463 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000464 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200465 } else if (time_since_loss_feedback >
466 kFeedbackTimeoutIntervals * kMaxRtcpFeedbackInterval &&
467 (last_timeout_.IsInfinite() ||
468 at_time - last_timeout_ > kTimeoutInterval)) {
Stefan Holmer52200d02016-09-20 14:14:23 +0200469 if (in_timeout_experiment_) {
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200470 RTC_LOG(LS_WARNING) << "Feedback timed out ("
471 << ToString(time_since_loss_feedback)
472 << "), reducing bitrate.";
473 new_bitrate = new_bitrate * 0.8;
Stefan Holmer52200d02016-09-20 14:14:23 +0200474 // Reset accumulators since we've already acted on missing feedback and
475 // shouldn't to act again on these old lost packets.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100476 lost_packets_since_last_loss_update_ = 0;
Stefan Holmer52200d02016-09-20 14:14:23 +0200477 expected_packets_since_last_loss_update_ = 0;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200478 last_timeout_ = at_time;
Stefan Holmer52200d02016-09-20 14:14:23 +0200479 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000480 }
philipel1b965312017-04-18 06:55:32 -0700481
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200482 CapBitrateToThresholds(at_time, new_bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000483}
andresp@webrtc.org4e69f782014-03-17 17:07:48 +0000484
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200485void SendSideBandwidthEstimation::UpdatePropagationRtt(
486 Timestamp at_time,
487 TimeDelta propagation_rtt) {
Sebastian Jansson24643482018-11-14 14:19:45 +0100488 rtt_backoff_.UpdatePropagationRtt(at_time, propagation_rtt);
Sebastian Jansson2e068e82018-10-08 12:49:53 +0200489}
490
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200491bool SendSideBandwidthEstimation::IsInStartPhase(Timestamp at_time) const {
492 return first_report_time_.IsInfinite() ||
493 at_time - first_report_time_ < kStartPhase;
stefan@webrtc.org548b2282014-11-03 14:42:43 +0000494}
495
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200496void SendSideBandwidthEstimation::UpdateMinHistory(Timestamp at_time) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000497 // Remove old data points from history.
498 // Since history precision is in ms, add one so it is able to increase
499 // bitrate if it is off by as little as 0.5ms.
500 while (!min_bitrate_history_.empty() &&
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200501 at_time - min_bitrate_history_.front().first + TimeDelta::ms(1) >
502 kBweIncreaseInterval) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000503 min_bitrate_history_.pop_front();
504 }
505
506 // Typical minimum sliding-window algorithm: Pop values higher than current
507 // bitrate before pushing it.
508 while (!min_bitrate_history_.empty() &&
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200509 current_bitrate_ <= min_bitrate_history_.back().second) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000510 min_bitrate_history_.pop_back();
511 }
512
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200513 min_bitrate_history_.push_back(std::make_pair(at_time, current_bitrate_));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000514}
515
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100516DataRate SendSideBandwidthEstimation::MaybeRampupOrBackoff(DataRate new_bitrate,
517 Timestamp at_time) {
518 // TODO(crodbro): reuse this code in UpdateEstimate instead of current
519 // inlining of very similar functionality.
520 const TimeDelta time_since_loss_packet_report =
521 at_time - last_loss_packet_report_;
522 const TimeDelta time_since_loss_feedback = at_time - last_loss_feedback_;
523 if (time_since_loss_packet_report < 1.2 * kMaxRtcpFeedbackInterval) {
524 new_bitrate = min_bitrate_history_.front().second * 1.08;
525 new_bitrate += DataRate::bps(1000);
526 } else if (time_since_loss_feedback >
527 kFeedbackTimeoutIntervals * kMaxRtcpFeedbackInterval &&
528 (last_timeout_.IsInfinite() ||
529 at_time - last_timeout_ > kTimeoutInterval)) {
530 if (in_timeout_experiment_) {
531 RTC_LOG(LS_WARNING) << "Feedback timed out ("
532 << ToString(time_since_loss_feedback)
533 << "), reducing bitrate.";
534 new_bitrate = new_bitrate * 0.8;
535 // Reset accumulators since we've already acted on missing feedback and
536 // shouldn't to act again on these old lost packets.
537 lost_packets_since_last_loss_update_ = 0;
538 expected_packets_since_last_loss_update_ = 0;
539 last_timeout_ = at_time;
540 }
541 }
542 return new_bitrate;
543}
544
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200545void SendSideBandwidthEstimation::CapBitrateToThresholds(Timestamp at_time,
546 DataRate bitrate) {
547 if (bwe_incoming_ > DataRate::Zero() && bitrate > bwe_incoming_) {
548 bitrate = bwe_incoming_;
andresp@webrtc.org4e69f782014-03-17 17:07:48 +0000549 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200550 if (delay_based_bitrate_ > DataRate::Zero() &&
551 bitrate > delay_based_bitrate_) {
552 bitrate = delay_based_bitrate_;
stefan32f81542016-01-20 07:13:58 -0800553 }
Christoffer Rodbro3a837482018-11-19 15:30:23 +0100554 if (loss_based_bandwidth_estimation_.Enabled() &&
555 loss_based_bandwidth_estimation_.GetEstimate() > DataRate::Zero()) {
556 bitrate = std::min(bitrate, loss_based_bandwidth_estimation_.GetEstimate());
557 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200558 if (bitrate > max_bitrate_configured_) {
559 bitrate = max_bitrate_configured_;
andresp@webrtc.org4e69f782014-03-17 17:07:48 +0000560 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200561 if (bitrate < min_bitrate_configured_) {
562 if (last_low_bitrate_log_.IsInfinite() ||
563 at_time - last_low_bitrate_log_ > kLowBitrateLogPeriod) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100564 RTC_LOG(LS_WARNING) << "Estimated available bandwidth "
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200565 << ToString(bitrate)
566 << " is below configured min bitrate "
567 << ToString(min_bitrate_configured_) << ".";
568 last_low_bitrate_log_ = at_time;
stefanb6b0b922015-09-04 03:04:56 -0700569 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200570 bitrate = min_bitrate_configured_;
andresp@webrtc.org4e69f782014-03-17 17:07:48 +0000571 }
philipel1b965312017-04-18 06:55:32 -0700572
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200573 if (bitrate != current_bitrate_ ||
philipel1b965312017-04-18 06:55:32 -0700574 last_fraction_loss_ != last_logged_fraction_loss_ ||
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200575 at_time - last_rtc_event_log_ > kRtcEventLogPeriod) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200576 event_log_->Log(absl::make_unique<RtcEventBweUpdateLossBased>(
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200577 bitrate.bps(), last_fraction_loss_,
Elad Alon4a87e1c2017-10-03 16:11:34 +0200578 expected_packets_since_last_loss_update_));
philipel1b965312017-04-18 06:55:32 -0700579 last_logged_fraction_loss_ = last_fraction_loss_;
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200580 last_rtc_event_log_ = at_time;
philipel1b965312017-04-18 06:55:32 -0700581 }
Sebastian Jansson7c1744d2018-10-08 11:00:50 +0200582 current_bitrate_ = bitrate;
andresp@webrtc.org4e69f782014-03-17 17:07:48 +0000583}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000584} // namespace webrtc