blob: 43bf5924230fcc8dff9cf5c499f99836c3d597c5 [file] [log] [blame]
philipel863a8262016-06-17 09:21:34 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/congestion_controller/delay_based_bwe.h"
12
philipel863a8262016-06-17 09:21:34 -070013#include <algorithm>
Stefan Holmer492ee282016-10-27 17:19:20 +020014#include <cmath>
tereliusafaef8b2016-11-17 03:48:18 -080015#include <string>
philipel863a8262016-06-17 09:21:34 -070016
17#include "webrtc/base/checks.h"
18#include "webrtc/base/constructormagic.h"
19#include "webrtc/base/logging.h"
20#include "webrtc/base/thread_annotations.h"
terelius0baf55d2017-02-17 03:38:28 -080021#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070022#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
philipel863a8262016-06-17 09:21:34 -070023#include "webrtc/modules/pacing/paced_sender.h"
24#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
terelius5a388362016-12-09 05:50:01 -080025#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Stefan Holmer492ee282016-10-27 17:19:20 +020026#include "webrtc/system_wrappers/include/field_trial.h"
stefan64636dd2016-08-03 00:29:03 -070027#include "webrtc/system_wrappers/include/metrics.h"
philipel863a8262016-06-17 09:21:34 -070028#include "webrtc/typedefs.h"
29
30namespace {
philipel7522a282016-08-16 10:59:36 +020031constexpr int kTimestampGroupLengthMs = 5;
32constexpr int kAbsSendTimeFraction = 18;
33constexpr int kAbsSendTimeInterArrivalUpshift = 8;
34constexpr int kInterArrivalShift =
35 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift;
36constexpr double kTimestampToMs =
philipel863a8262016-06-17 09:21:34 -070037 1000.0 / static_cast<double>(1 << kInterArrivalShift);
philipel7522a282016-08-16 10:59:36 +020038// This ssrc is used to fulfill the current API but will be removed
39// after the API has been changed.
40constexpr uint32_t kFixedSsrc = 0;
Stefan Holmer492ee282016-10-27 17:19:20 +020041constexpr int kInitialRateWindowMs = 500;
42constexpr int kRateWindowMs = 150;
43
terelius5a388362016-12-09 05:50:01 -080044// Parameters for linear least squares fit of regression line to noisy data.
tereliusafaef8b2016-11-17 03:48:18 -080045constexpr size_t kDefaultTrendlineWindowSize = 15;
46constexpr double kDefaultTrendlineSmoothingCoeff = 0.9;
47constexpr double kDefaultTrendlineThresholdGain = 4.0;
48
terelius5a388362016-12-09 05:50:01 -080049// Parameters for Theil-Sen robust fitting of line to noisy data.
50constexpr size_t kDefaultMedianSlopeWindowSize = 20;
51constexpr double kDefaultMedianSlopeThresholdGain = 4.0;
52
stefane3a55672017-02-13 09:08:22 -080053constexpr int kMaxConsecutiveFailedLookups = 5;
54
Stefan Holmer492ee282016-10-27 17:19:20 +020055const char kBitrateEstimateExperiment[] = "WebRTC-ImprovedBitrateEstimate";
tereliusafaef8b2016-11-17 03:48:18 -080056const char kBweTrendlineFilterExperiment[] = "WebRTC-BweTrendlineFilter";
terelius5a388362016-12-09 05:50:01 -080057const char kBweMedianSlopeFilterExperiment[] = "WebRTC-BweMedianSlopeFilter";
Stefan Holmer492ee282016-10-27 17:19:20 +020058
59bool BitrateEstimateExperimentIsEnabled() {
sprangc1b57a12017-02-28 08:50:47 -080060 return webrtc::field_trial::IsEnabled(kBitrateEstimateExperiment);
Stefan Holmer492ee282016-10-27 17:19:20 +020061}
tereliusafaef8b2016-11-17 03:48:18 -080062
63bool TrendlineFilterExperimentIsEnabled() {
64 std::string experiment_string =
65 webrtc::field_trial::FindFullName(kBweTrendlineFilterExperiment);
66 // The experiment is enabled iff the field trial string begins with "Enabled".
67 return experiment_string.find("Enabled") == 0;
68}
69
terelius5a388362016-12-09 05:50:01 -080070bool MedianSlopeFilterExperimentIsEnabled() {
71 std::string experiment_string =
72 webrtc::field_trial::FindFullName(kBweMedianSlopeFilterExperiment);
73 // The experiment is enabled iff the field trial string begins with "Enabled".
74 return experiment_string.find("Enabled") == 0;
75}
76
77bool ReadTrendlineFilterExperimentParameters(size_t* window_size,
tereliusafaef8b2016-11-17 03:48:18 -080078 double* smoothing_coef,
79 double* threshold_gain) {
80 RTC_DCHECK(TrendlineFilterExperimentIsEnabled());
terelius5a388362016-12-09 05:50:01 -080081 RTC_DCHECK(!MedianSlopeFilterExperimentIsEnabled());
82 RTC_DCHECK(window_size != nullptr);
83 RTC_DCHECK(smoothing_coef != nullptr);
84 RTC_DCHECK(threshold_gain != nullptr);
tereliusafaef8b2016-11-17 03:48:18 -080085 std::string experiment_string =
86 webrtc::field_trial::FindFullName(kBweTrendlineFilterExperiment);
87 int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%zu,%lf,%lf",
terelius5a388362016-12-09 05:50:01 -080088 window_size, smoothing_coef, threshold_gain);
tereliusafaef8b2016-11-17 03:48:18 -080089 if (parsed_values == 3) {
terelius5a388362016-12-09 05:50:01 -080090 RTC_CHECK_GT(*window_size, 1) << "Need at least 2 points to fit a line.";
tereliusafaef8b2016-11-17 03:48:18 -080091 RTC_CHECK(0 <= *smoothing_coef && *smoothing_coef <= 1)
92 << "Coefficient needs to be between 0 and 1 for weighted average.";
93 RTC_CHECK_GT(*threshold_gain, 0) << "Threshold gain needs to be positive.";
94 return true;
95 }
96 LOG(LS_WARNING) << "Failed to parse parameters for BweTrendlineFilter "
97 "experiment from field trial string. Using default.";
terelius5a388362016-12-09 05:50:01 -080098 *window_size = kDefaultTrendlineWindowSize;
tereliusafaef8b2016-11-17 03:48:18 -080099 *smoothing_coef = kDefaultTrendlineSmoothingCoeff;
100 *threshold_gain = kDefaultTrendlineThresholdGain;
101 return false;
102}
103
terelius5a388362016-12-09 05:50:01 -0800104bool ReadMedianSlopeFilterExperimentParameters(size_t* window_size,
105 double* threshold_gain) {
106 RTC_DCHECK(!TrendlineFilterExperimentIsEnabled());
107 RTC_DCHECK(MedianSlopeFilterExperimentIsEnabled());
108 RTC_DCHECK(window_size != nullptr);
109 RTC_DCHECK(threshold_gain != nullptr);
110 std::string experiment_string =
111 webrtc::field_trial::FindFullName(kBweMedianSlopeFilterExperiment);
112 int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%zu,%lf",
113 window_size, threshold_gain);
114 if (parsed_values == 2) {
115 RTC_CHECK_GT(*window_size, 1) << "Need at least 2 points to fit a line.";
116 RTC_CHECK_GT(*threshold_gain, 0) << "Threshold gain needs to be positive.";
117 return true;
118 }
119 LOG(LS_WARNING) << "Failed to parse parameters for BweMedianSlopeFilter "
120 "experiment from field trial string. Using default.";
121 *window_size = kDefaultMedianSlopeWindowSize;
122 *threshold_gain = kDefaultMedianSlopeThresholdGain;
123 return false;
124}
philipel863a8262016-06-17 09:21:34 -0700125} // namespace
126
127namespace webrtc {
tereliusafaef8b2016-11-17 03:48:18 -0800128
Stefan Holmer492ee282016-10-27 17:19:20 +0200129DelayBasedBwe::BitrateEstimator::BitrateEstimator()
130 : sum_(0),
131 current_win_ms_(0),
132 prev_time_ms_(-1),
133 bitrate_estimate_(-1.0f),
134 bitrate_estimate_var_(50.0f),
135 old_estimator_(kBitrateWindowMs, 8000),
136 in_experiment_(BitrateEstimateExperimentIsEnabled()) {}
137
138void DelayBasedBwe::BitrateEstimator::Update(int64_t now_ms, int bytes) {
139 if (!in_experiment_) {
140 old_estimator_.Update(bytes, now_ms);
141 rtc::Optional<uint32_t> rate = old_estimator_.Rate(now_ms);
142 bitrate_estimate_ = -1.0f;
143 if (rate)
144 bitrate_estimate_ = *rate / 1000.0f;
145 return;
146 }
147 int rate_window_ms = kRateWindowMs;
148 // We use a larger window at the beginning to get a more stable sample that
149 // we can use to initialize the estimate.
150 if (bitrate_estimate_ < 0.f)
151 rate_window_ms = kInitialRateWindowMs;
152 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms);
153 if (bitrate_sample < 0.0f)
154 return;
155 if (bitrate_estimate_ < 0.0f) {
156 // This is the very first sample we get. Use it to initialize the estimate.
157 bitrate_estimate_ = bitrate_sample;
158 return;
159 }
160 // Define the sample uncertainty as a function of how far away it is from the
161 // current estimate.
162 float sample_uncertainty =
163 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_;
164 float sample_var = sample_uncertainty * sample_uncertainty;
165 // Update a bayesian estimate of the rate, weighting it lower if the sample
166 // uncertainty is large.
167 // The bitrate estimate uncertainty is increased with each update to model
168 // that the bitrate changes over time.
169 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f;
170 bitrate_estimate_ = (sample_var * bitrate_estimate_ +
171 pred_bitrate_estimate_var * bitrate_sample) /
172 (sample_var + pred_bitrate_estimate_var);
173 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var /
174 (sample_var + pred_bitrate_estimate_var);
175}
176
177float DelayBasedBwe::BitrateEstimator::UpdateWindow(int64_t now_ms,
178 int bytes,
179 int rate_window_ms) {
180 // Reset if time moves backwards.
181 if (now_ms < prev_time_ms_) {
182 prev_time_ms_ = -1;
183 sum_ = 0;
184 current_win_ms_ = 0;
185 }
186 if (prev_time_ms_ >= 0) {
187 current_win_ms_ += now_ms - prev_time_ms_;
188 // Reset if nothing has been received for more than a full window.
189 if (now_ms - prev_time_ms_ > rate_window_ms) {
190 sum_ = 0;
191 current_win_ms_ %= rate_window_ms;
192 }
193 }
194 prev_time_ms_ = now_ms;
195 float bitrate_sample = -1.0f;
196 if (current_win_ms_ >= rate_window_ms) {
197 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms);
198 current_win_ms_ -= rate_window_ms;
199 sum_ = 0;
200 }
201 sum_ += bytes;
202 return bitrate_sample;
203}
204
205rtc::Optional<uint32_t> DelayBasedBwe::BitrateEstimator::bitrate_bps() const {
206 if (bitrate_estimate_ < 0.f)
207 return rtc::Optional<uint32_t>();
208 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000);
209}
philipel863a8262016-06-17 09:21:34 -0700210
terelius0baf55d2017-02-17 03:38:28 -0800211DelayBasedBwe::DelayBasedBwe(RtcEventLog* event_log, Clock* clock)
terelius5a388362016-12-09 05:50:01 -0800212 : in_trendline_experiment_(TrendlineFilterExperimentIsEnabled()),
213 in_median_slope_experiment_(MedianSlopeFilterExperimentIsEnabled()),
terelius0baf55d2017-02-17 03:38:28 -0800214 event_log_(event_log),
terelius5a388362016-12-09 05:50:01 -0800215 clock_(clock),
philipel863a8262016-06-17 09:21:34 -0700216 inter_arrival_(),
tereliusafaef8b2016-11-17 03:48:18 -0800217 kalman_estimator_(),
218 trendline_estimator_(),
terelius84f83f82016-12-27 10:43:01 -0800219 detector_(),
Stefan Holmer492ee282016-10-27 17:19:20 +0200220 receiver_incoming_bitrate_(),
philipel863a8262016-06-17 09:21:34 -0700221 last_update_ms_(-1),
philipel7522a282016-08-16 10:59:36 +0200222 last_seen_packet_ms_(-1),
tereliusafaef8b2016-11-17 03:48:18 -0800223 uma_recorded_(false),
224 trendline_window_size_(kDefaultTrendlineWindowSize),
225 trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff),
226 trendline_threshold_gain_(kDefaultTrendlineThresholdGain),
terelius5a388362016-12-09 05:50:01 -0800227 probing_interval_estimator_(&rate_control_),
228 median_slope_window_size_(kDefaultMedianSlopeWindowSize),
stefane3a55672017-02-13 09:08:22 -0800229 median_slope_threshold_gain_(kDefaultMedianSlopeThresholdGain),
terelius0baf55d2017-02-17 03:38:28 -0800230 consecutive_delayed_feedbacks_(0),
231 last_logged_bitrate_(0),
232 last_logged_state_(kBwNormal) {
tereliusafaef8b2016-11-17 03:48:18 -0800233 if (in_trendline_experiment_) {
234 ReadTrendlineFilterExperimentParameters(&trendline_window_size_,
235 &trendline_smoothing_coeff_,
236 &trendline_threshold_gain_);
tereliused1850a2017-02-08 08:45:20 -0800237 LOG(LS_INFO) << "Trendline filter experiment enabled with parameters "
238 << trendline_window_size_ << ',' << trendline_smoothing_coeff_
239 << ',' << trendline_threshold_gain_;
tereliusafaef8b2016-11-17 03:48:18 -0800240 }
terelius5a388362016-12-09 05:50:01 -0800241 if (in_median_slope_experiment_) {
terelius804ab6f2017-01-17 03:30:05 -0800242 ReadMedianSlopeFilterExperimentParameters(&median_slope_window_size_,
243 &median_slope_threshold_gain_);
tereliused1850a2017-02-08 08:45:20 -0800244 LOG(LS_INFO) << "Median-slope filter experiment enabled with parameters "
245 << median_slope_window_size_ << ','
246 << median_slope_threshold_gain_;
247 }
248 if (!in_trendline_experiment_ && !in_median_slope_experiment_) {
249 LOG(LS_INFO) << "No overuse experiment enabled. Using Kalman filter.";
terelius5a388362016-12-09 05:50:01 -0800250 }
251
philipel863a8262016-06-17 09:21:34 -0700252 network_thread_.DetachFromThread();
253}
254
Stefan Holmer280de9e2016-09-30 10:06:51 +0200255DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector(
elad.alonf9490002017-03-06 05:32:21 -0800256 const std::vector<PacketFeedback>& packet_feedback_vector) {
philipel863a8262016-06-17 09:21:34 -0700257 RTC_DCHECK(network_thread_.CalledOnValidThread());
stefan64636dd2016-08-03 00:29:03 -0700258 if (!uma_recorded_) {
asapersson1d02d3e2016-09-09 22:40:25 -0700259 RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram,
260 BweNames::kSendSideTransportSeqNum,
261 BweNames::kBweNamesMax);
stefan64636dd2016-08-03 00:29:03 -0700262 uma_recorded_ = true;
263 }
Stefan Holmer280de9e2016-09-30 10:06:51 +0200264 Result aggregated_result;
stefane3a55672017-02-13 09:08:22 -0800265 bool delayed_feedback = true;
elad.alonf9490002017-03-06 05:32:21 -0800266 for (const auto& packet_feedback : packet_feedback_vector) {
267 if (packet_feedback.send_time_ms < 0)
stefane3a55672017-02-13 09:08:22 -0800268 continue;
269 delayed_feedback = false;
elad.alonf9490002017-03-06 05:32:21 -0800270 Result result = IncomingPacketFeedback(packet_feedback);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200271 if (result.updated)
272 aggregated_result = result;
philipel863a8262016-06-17 09:21:34 -0700273 }
stefane3a55672017-02-13 09:08:22 -0800274 if (delayed_feedback) {
275 ++consecutive_delayed_feedbacks_;
276 } else {
277 consecutive_delayed_feedbacks_ = 0;
278 }
279 if (consecutive_delayed_feedbacks_ >= kMaxConsecutiveFailedLookups) {
280 aggregated_result =
281 OnLongFeedbackDelay(packet_feedback_vector.back().arrival_time_ms);
282 consecutive_delayed_feedbacks_ = 0;
283 }
Stefan Holmer280de9e2016-09-30 10:06:51 +0200284 return aggregated_result;
philipel863a8262016-06-17 09:21:34 -0700285}
286
stefane3a55672017-02-13 09:08:22 -0800287DelayBasedBwe::Result DelayBasedBwe::OnLongFeedbackDelay(
288 int64_t arrival_time_ms) {
289 // Estimate should always be valid since a start bitrate always is set in the
290 // Call constructor. An alternative would be to return an empty Result here,
291 // or to estimate the throughput based on the feedback we received.
292 RTC_DCHECK(rate_control_.ValidEstimate());
293 rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2,
294 arrival_time_ms);
295 Result result;
296 result.updated = true;
297 result.probe = false;
298 result.target_bitrate_bps = rate_control_.LatestEstimate();
299 LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to "
300 << result.target_bitrate_bps;
301 return result;
302}
303
elad.alonf9490002017-03-06 05:32:21 -0800304DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedback(
305 const PacketFeedback& packet_feedback) {
stefan5e12d362016-07-11 01:44:02 -0700306 int64_t now_ms = clock_->TimeInMilliseconds();
philipel863a8262016-06-17 09:21:34 -0700307
elad.alonf9490002017-03-06 05:32:21 -0800308 receiver_incoming_bitrate_.Update(packet_feedback.arrival_time_ms,
309 packet_feedback.payload_size);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200310 Result result;
311 // Reset if the stream has timed out.
312 if (last_seen_packet_ms_ == -1 ||
313 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) {
314 inter_arrival_.reset(
315 new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000,
316 kTimestampToMs, true));
tereliusafaef8b2016-11-17 03:48:18 -0800317 kalman_estimator_.reset(new OveruseEstimator(OverUseDetectorOptions()));
318 trendline_estimator_.reset(new TrendlineEstimator(
319 trendline_window_size_, trendline_smoothing_coeff_,
320 trendline_threshold_gain_));
terelius5a388362016-12-09 05:50:01 -0800321 median_slope_estimator_.reset(new MedianSlopeEstimator(
322 median_slope_window_size_, median_slope_threshold_gain_));
Stefan Holmer280de9e2016-09-30 10:06:51 +0200323 }
324 last_seen_packet_ms_ = now_ms;
philipel863a8262016-06-17 09:21:34 -0700325
Stefan Holmer280de9e2016-09-30 10:06:51 +0200326 uint32_t send_time_24bits =
327 static_cast<uint32_t>(
elad.alonf9490002017-03-06 05:32:21 -0800328 ((static_cast<uint64_t>(packet_feedback.send_time_ms)
329 << kAbsSendTimeFraction) +
Stefan Holmer280de9e2016-09-30 10:06:51 +0200330 500) /
331 1000) &
332 0x00FFFFFF;
333 // Shift up send time to use the full 32 bits that inter_arrival works with,
334 // so wrapping works properly.
335 uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift;
stefanfd0d4262016-09-29 02:44:31 -0700336
Stefan Holmer280de9e2016-09-30 10:06:51 +0200337 uint32_t ts_delta = 0;
338 int64_t t_delta = 0;
339 int size_delta = 0;
elad.alonf9490002017-03-06 05:32:21 -0800340 if (inter_arrival_->ComputeDeltas(timestamp, packet_feedback.arrival_time_ms,
341 now_ms, packet_feedback.payload_size,
342 &ts_delta, &t_delta, &size_delta)) {
Stefan Holmer280de9e2016-09-30 10:06:51 +0200343 double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift);
tereliusafaef8b2016-11-17 03:48:18 -0800344 if (in_trendline_experiment_) {
elad.alonf9490002017-03-06 05:32:21 -0800345 trendline_estimator_->Update(t_delta, ts_delta_ms,
346 packet_feedback.arrival_time_ms);
tereliusafaef8b2016-11-17 03:48:18 -0800347 detector_.Detect(trendline_estimator_->trendline_slope(), ts_delta_ms,
348 trendline_estimator_->num_of_deltas(),
elad.alonf9490002017-03-06 05:32:21 -0800349 packet_feedback.arrival_time_ms);
terelius5a388362016-12-09 05:50:01 -0800350 } else if (in_median_slope_experiment_) {
351 median_slope_estimator_->Update(t_delta, ts_delta_ms,
elad.alonf9490002017-03-06 05:32:21 -0800352 packet_feedback.arrival_time_ms);
terelius5a388362016-12-09 05:50:01 -0800353 detector_.Detect(median_slope_estimator_->trendline_slope(), ts_delta_ms,
354 median_slope_estimator_->num_of_deltas(),
elad.alonf9490002017-03-06 05:32:21 -0800355 packet_feedback.arrival_time_ms);
tereliusafaef8b2016-11-17 03:48:18 -0800356 } else {
357 kalman_estimator_->Update(t_delta, ts_delta_ms, size_delta,
elad.alonf9490002017-03-06 05:32:21 -0800358 detector_.State(),
359 packet_feedback.arrival_time_ms);
tereliusafaef8b2016-11-17 03:48:18 -0800360 detector_.Detect(kalman_estimator_->offset(), ts_delta_ms,
361 kalman_estimator_->num_of_deltas(),
elad.alonf9490002017-03-06 05:32:21 -0800362 packet_feedback.arrival_time_ms);
tereliusafaef8b2016-11-17 03:48:18 -0800363 }
stefan5ec85fb2016-09-29 04:19:38 -0700364 }
365
Stefan Holmer280de9e2016-09-30 10:06:51 +0200366 int probing_bps = 0;
elad.alonf9490002017-03-06 05:32:21 -0800367 if (packet_feedback.pacing_info.probe_cluster_id !=
368 PacedPacketInfo::kNotAProbe) {
369 probing_bps =
370 probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200371 }
Stefan Holmer492ee282016-10-27 17:19:20 +0200372 rtc::Optional<uint32_t> acked_bitrate_bps =
373 receiver_incoming_bitrate_.bitrate_bps();
Stefan Holmer280de9e2016-09-30 10:06:51 +0200374 // Currently overusing the bandwidth.
375 if (detector_.State() == kBwOverusing) {
Stefan Holmer492ee282016-10-27 17:19:20 +0200376 if (acked_bitrate_bps &&
377 rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) {
378 result.updated =
elad.alonf9490002017-03-06 05:32:21 -0800379 UpdateEstimate(packet_feedback.arrival_time_ms, now_ms,
380 acked_bitrate_bps, &result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200381 }
382 } else if (probing_bps > 0) {
383 // No overuse, but probing measured a bitrate.
elad.alonf9490002017-03-06 05:32:21 -0800384 rate_control_.SetEstimate(probing_bps, packet_feedback.arrival_time_ms);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200385 result.probe = true;
Stefan Holmer492ee282016-10-27 17:19:20 +0200386 result.updated =
elad.alonf9490002017-03-06 05:32:21 -0800387 UpdateEstimate(packet_feedback.arrival_time_ms, now_ms,
388 acked_bitrate_bps, &result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200389 }
Stefan Holmer280de9e2016-09-30 10:06:51 +0200390 if (!result.updated &&
391 (last_update_ms_ == -1 ||
terelius6ed592d2016-10-18 05:55:30 -0700392 now_ms - last_update_ms_ > rate_control_.GetFeedbackInterval())) {
Stefan Holmer492ee282016-10-27 17:19:20 +0200393 result.updated =
elad.alonf9490002017-03-06 05:32:21 -0800394 UpdateEstimate(packet_feedback.arrival_time_ms, now_ms,
395 acked_bitrate_bps, &result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200396 }
terelius5a388362016-12-09 05:50:01 -0800397 if (result.updated) {
stefan5ec85fb2016-09-29 04:19:38 -0700398 last_update_ms_ = now_ms;
terelius5a388362016-12-09 05:50:01 -0800399 BWE_TEST_LOGGING_PLOT(1, "target_bitrate_bps", now_ms,
400 result.target_bitrate_bps);
terelius0baf55d2017-02-17 03:38:28 -0800401 if (event_log_ && (result.target_bitrate_bps != last_logged_bitrate_ ||
402 detector_.State() != last_logged_state_)) {
terelius424e6cf2017-02-20 05:14:41 -0800403 event_log_->LogDelayBasedBweUpdate(result.target_bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800404 detector_.State());
405 last_logged_bitrate_ = result.target_bitrate_bps;
406 last_logged_state_ = detector_.State();
407 }
terelius5a388362016-12-09 05:50:01 -0800408 }
Stefan Holmer280de9e2016-09-30 10:06:51 +0200409
410 return result;
philipel863a8262016-06-17 09:21:34 -0700411}
412
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700413bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms,
414 int64_t now_ms,
Stefan Holmer492ee282016-10-27 17:19:20 +0200415 rtc::Optional<uint32_t> acked_bitrate_bps,
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700416 uint32_t* target_bitrate_bps) {
tereliusafaef8b2016-11-17 03:48:18 -0800417 // TODO(terelius): RateControlInput::noise_var is deprecated and will be
418 // removed. In the meantime, we set it to zero.
419 const RateControlInput input(detector_.State(), acked_bitrate_bps, 0);
terelius6ed592d2016-10-18 05:55:30 -0700420 rate_control_.Update(&input, now_ms);
421 *target_bitrate_bps = rate_control_.UpdateBandwidthEstimate(now_ms);
422 return rate_control_.ValidEstimate();
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700423}
424
philipel863a8262016-06-17 09:21:34 -0700425void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
terelius6ed592d2016-10-18 05:55:30 -0700426 rate_control_.SetRtt(avg_rtt_ms);
philipel863a8262016-06-17 09:21:34 -0700427}
428
philipel863a8262016-06-17 09:21:34 -0700429bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs,
430 uint32_t* bitrate_bps) const {
431 // Currently accessed from both the process thread (see
432 // ModuleRtpRtcpImpl::Process()) and the configuration thread (see
433 // Call::GetStats()). Should in the future only be accessed from a single
434 // thread.
435 RTC_DCHECK(ssrcs);
436 RTC_DCHECK(bitrate_bps);
terelius6ed592d2016-10-18 05:55:30 -0700437 if (!rate_control_.ValidEstimate())
philipel863a8262016-06-17 09:21:34 -0700438 return false;
philipel7522a282016-08-16 10:59:36 +0200439
440 *ssrcs = {kFixedSsrc};
terelius6ed592d2016-10-18 05:55:30 -0700441 *bitrate_bps = rate_control_.LatestEstimate();
philipel863a8262016-06-17 09:21:34 -0700442 return true;
443}
444
stefan5a2c5062017-01-27 06:43:18 -0800445void DelayBasedBwe::SetStartBitrate(int start_bitrate_bps) {
446 LOG(LS_WARNING) << "BWE Setting start bitrate to: " << start_bitrate_bps;
447 rate_control_.SetStartBitrate(start_bitrate_bps);
448}
449
philipel863a8262016-06-17 09:21:34 -0700450void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) {
451 // Called from both the configuration thread and the network thread. Shouldn't
452 // be called from the network thread in the future.
terelius6ed592d2016-10-18 05:55:30 -0700453 rate_control_.SetMinBitrate(min_bitrate_bps);
philipel863a8262016-06-17 09:21:34 -0700454}
minyue78b4d562016-11-30 04:47:39 -0800455
456int64_t DelayBasedBwe::GetProbingIntervalMs() const {
457 return probing_interval_estimator_.GetIntervalMs();
458}
philipel863a8262016-06-17 09:21:34 -0700459} // namespace webrtc