philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 14 | #include <cmath> |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 15 | #include <string> |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 16 | |
| 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" |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 21 | #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 22 | #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 23 | #include "webrtc/modules/pacing/paced_sender.h" |
| 24 | #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 25 | #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 26 | #include "webrtc/system_wrappers/include/field_trial.h" |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 27 | #include "webrtc/system_wrappers/include/metrics.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 28 | #include "webrtc/typedefs.h" |
| 29 | |
| 30 | namespace { |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 31 | constexpr int kTimestampGroupLengthMs = 5; |
| 32 | constexpr int kAbsSendTimeFraction = 18; |
| 33 | constexpr int kAbsSendTimeInterArrivalUpshift = 8; |
| 34 | constexpr int kInterArrivalShift = |
| 35 | kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; |
| 36 | constexpr double kTimestampToMs = |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 37 | 1000.0 / static_cast<double>(1 << kInterArrivalShift); |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 38 | // This ssrc is used to fulfill the current API but will be removed |
| 39 | // after the API has been changed. |
| 40 | constexpr uint32_t kFixedSsrc = 0; |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 41 | constexpr int kInitialRateWindowMs = 500; |
| 42 | constexpr int kRateWindowMs = 150; |
| 43 | |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 44 | // Parameters for linear least squares fit of regression line to noisy data. |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 45 | constexpr size_t kDefaultTrendlineWindowSize = 15; |
| 46 | constexpr double kDefaultTrendlineSmoothingCoeff = 0.9; |
| 47 | constexpr double kDefaultTrendlineThresholdGain = 4.0; |
| 48 | |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 49 | // Parameters for Theil-Sen robust fitting of line to noisy data. |
| 50 | constexpr size_t kDefaultMedianSlopeWindowSize = 20; |
| 51 | constexpr double kDefaultMedianSlopeThresholdGain = 4.0; |
| 52 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 53 | constexpr int kMaxConsecutiveFailedLookups = 5; |
| 54 | |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 55 | const char kBitrateEstimateExperiment[] = "WebRTC-ImprovedBitrateEstimate"; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 56 | const char kBweTrendlineFilterExperiment[] = "WebRTC-BweTrendlineFilter"; |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 57 | const char kBweMedianSlopeFilterExperiment[] = "WebRTC-BweMedianSlopeFilter"; |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 58 | |
| 59 | bool BitrateEstimateExperimentIsEnabled() { |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 60 | return webrtc::field_trial::IsEnabled(kBitrateEstimateExperiment); |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 61 | } |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 62 | |
| 63 | bool 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 | |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 70 | bool 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 | |
| 77 | bool ReadTrendlineFilterExperimentParameters(size_t* window_size, |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 78 | double* smoothing_coef, |
| 79 | double* threshold_gain) { |
| 80 | RTC_DCHECK(TrendlineFilterExperimentIsEnabled()); |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 81 | RTC_DCHECK(!MedianSlopeFilterExperimentIsEnabled()); |
| 82 | RTC_DCHECK(window_size != nullptr); |
| 83 | RTC_DCHECK(smoothing_coef != nullptr); |
| 84 | RTC_DCHECK(threshold_gain != nullptr); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 85 | std::string experiment_string = |
| 86 | webrtc::field_trial::FindFullName(kBweTrendlineFilterExperiment); |
| 87 | int parsed_values = sscanf(experiment_string.c_str(), "Enabled-%zu,%lf,%lf", |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 88 | window_size, smoothing_coef, threshold_gain); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 89 | if (parsed_values == 3) { |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 90 | RTC_CHECK_GT(*window_size, 1) << "Need at least 2 points to fit a line."; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 91 | 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."; |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 98 | *window_size = kDefaultTrendlineWindowSize; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 99 | *smoothing_coef = kDefaultTrendlineSmoothingCoeff; |
| 100 | *threshold_gain = kDefaultTrendlineThresholdGain; |
| 101 | return false; |
| 102 | } |
| 103 | |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 104 | bool 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 | } |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 125 | |
| 126 | class PacketFeedbackComparator { |
| 127 | public: |
| 128 | inline bool operator()(const webrtc::PacketFeedback& lhs, |
| 129 | const webrtc::PacketFeedback& rhs) { |
| 130 | if (lhs.arrival_time_ms != rhs.arrival_time_ms) |
| 131 | return lhs.arrival_time_ms < rhs.arrival_time_ms; |
| 132 | if (lhs.send_time_ms != rhs.send_time_ms) |
| 133 | return lhs.send_time_ms < rhs.send_time_ms; |
| 134 | return lhs.sequence_number < rhs.sequence_number; |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | void SortPacketFeedbackVector(const std::vector<webrtc::PacketFeedback>& input, |
| 139 | std::vector<webrtc::PacketFeedback>* output) { |
| 140 | auto pred = [](const webrtc::PacketFeedback& packet_feedback) { |
| 141 | return packet_feedback.arrival_time_ms != |
| 142 | webrtc::PacketFeedback::kNotReceived; |
| 143 | }; |
| 144 | std::copy_if(input.begin(), input.end(), std::back_inserter(*output), pred); |
| 145 | std::sort(output->begin(), output->end(), PacketFeedbackComparator()); |
| 146 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 147 | } // namespace |
| 148 | |
| 149 | namespace webrtc { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 150 | |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 151 | DelayBasedBwe::BitrateEstimator::BitrateEstimator() |
| 152 | : sum_(0), |
| 153 | current_win_ms_(0), |
| 154 | prev_time_ms_(-1), |
| 155 | bitrate_estimate_(-1.0f), |
| 156 | bitrate_estimate_var_(50.0f), |
| 157 | old_estimator_(kBitrateWindowMs, 8000), |
| 158 | in_experiment_(BitrateEstimateExperimentIsEnabled()) {} |
| 159 | |
| 160 | void DelayBasedBwe::BitrateEstimator::Update(int64_t now_ms, int bytes) { |
| 161 | if (!in_experiment_) { |
| 162 | old_estimator_.Update(bytes, now_ms); |
| 163 | rtc::Optional<uint32_t> rate = old_estimator_.Rate(now_ms); |
| 164 | bitrate_estimate_ = -1.0f; |
| 165 | if (rate) |
| 166 | bitrate_estimate_ = *rate / 1000.0f; |
| 167 | return; |
| 168 | } |
| 169 | int rate_window_ms = kRateWindowMs; |
| 170 | // We use a larger window at the beginning to get a more stable sample that |
| 171 | // we can use to initialize the estimate. |
| 172 | if (bitrate_estimate_ < 0.f) |
| 173 | rate_window_ms = kInitialRateWindowMs; |
| 174 | float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); |
| 175 | if (bitrate_sample < 0.0f) |
| 176 | return; |
| 177 | if (bitrate_estimate_ < 0.0f) { |
| 178 | // This is the very first sample we get. Use it to initialize the estimate. |
| 179 | bitrate_estimate_ = bitrate_sample; |
| 180 | return; |
| 181 | } |
| 182 | // Define the sample uncertainty as a function of how far away it is from the |
| 183 | // current estimate. |
| 184 | float sample_uncertainty = |
| 185 | 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_; |
| 186 | float sample_var = sample_uncertainty * sample_uncertainty; |
| 187 | // Update a bayesian estimate of the rate, weighting it lower if the sample |
| 188 | // uncertainty is large. |
| 189 | // The bitrate estimate uncertainty is increased with each update to model |
| 190 | // that the bitrate changes over time. |
| 191 | float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; |
| 192 | bitrate_estimate_ = (sample_var * bitrate_estimate_ + |
| 193 | pred_bitrate_estimate_var * bitrate_sample) / |
| 194 | (sample_var + pred_bitrate_estimate_var); |
| 195 | bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / |
| 196 | (sample_var + pred_bitrate_estimate_var); |
| 197 | } |
| 198 | |
| 199 | float DelayBasedBwe::BitrateEstimator::UpdateWindow(int64_t now_ms, |
| 200 | int bytes, |
| 201 | int rate_window_ms) { |
| 202 | // Reset if time moves backwards. |
| 203 | if (now_ms < prev_time_ms_) { |
| 204 | prev_time_ms_ = -1; |
| 205 | sum_ = 0; |
| 206 | current_win_ms_ = 0; |
| 207 | } |
| 208 | if (prev_time_ms_ >= 0) { |
| 209 | current_win_ms_ += now_ms - prev_time_ms_; |
| 210 | // Reset if nothing has been received for more than a full window. |
| 211 | if (now_ms - prev_time_ms_ > rate_window_ms) { |
| 212 | sum_ = 0; |
| 213 | current_win_ms_ %= rate_window_ms; |
| 214 | } |
| 215 | } |
| 216 | prev_time_ms_ = now_ms; |
| 217 | float bitrate_sample = -1.0f; |
| 218 | if (current_win_ms_ >= rate_window_ms) { |
| 219 | bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); |
| 220 | current_win_ms_ -= rate_window_ms; |
| 221 | sum_ = 0; |
| 222 | } |
| 223 | sum_ += bytes; |
| 224 | return bitrate_sample; |
| 225 | } |
| 226 | |
| 227 | rtc::Optional<uint32_t> DelayBasedBwe::BitrateEstimator::bitrate_bps() const { |
| 228 | if (bitrate_estimate_ < 0.f) |
| 229 | return rtc::Optional<uint32_t>(); |
| 230 | return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); |
| 231 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 232 | |
elad.alon | 61ce37e | 2017-03-09 07:09:31 -0800 | [diff] [blame] | 233 | DelayBasedBwe::DelayBasedBwe(RtcEventLog* event_log, const Clock* clock) |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 234 | : in_trendline_experiment_(TrendlineFilterExperimentIsEnabled()), |
| 235 | in_median_slope_experiment_(MedianSlopeFilterExperimentIsEnabled()), |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 236 | event_log_(event_log), |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 237 | clock_(clock), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 238 | inter_arrival_(), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 239 | kalman_estimator_(), |
| 240 | trendline_estimator_(), |
terelius | 84f83f8 | 2016-12-27 10:43:01 -0800 | [diff] [blame] | 241 | detector_(), |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 242 | receiver_incoming_bitrate_(), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 243 | last_update_ms_(-1), |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 244 | last_seen_packet_ms_(-1), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 245 | uma_recorded_(false), |
| 246 | trendline_window_size_(kDefaultTrendlineWindowSize), |
| 247 | trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff), |
| 248 | trendline_threshold_gain_(kDefaultTrendlineThresholdGain), |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 249 | probing_interval_estimator_(&rate_control_), |
| 250 | median_slope_window_size_(kDefaultMedianSlopeWindowSize), |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 251 | median_slope_threshold_gain_(kDefaultMedianSlopeThresholdGain), |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 252 | consecutive_delayed_feedbacks_(0), |
| 253 | last_logged_bitrate_(0), |
| 254 | last_logged_state_(kBwNormal) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 255 | if (in_trendline_experiment_) { |
| 256 | ReadTrendlineFilterExperimentParameters(&trendline_window_size_, |
| 257 | &trendline_smoothing_coeff_, |
| 258 | &trendline_threshold_gain_); |
terelius | ed1850a | 2017-02-08 08:45:20 -0800 | [diff] [blame] | 259 | LOG(LS_INFO) << "Trendline filter experiment enabled with parameters " |
| 260 | << trendline_window_size_ << ',' << trendline_smoothing_coeff_ |
| 261 | << ',' << trendline_threshold_gain_; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 262 | } |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 263 | if (in_median_slope_experiment_) { |
terelius | 804ab6f | 2017-01-17 03:30:05 -0800 | [diff] [blame] | 264 | ReadMedianSlopeFilterExperimentParameters(&median_slope_window_size_, |
| 265 | &median_slope_threshold_gain_); |
terelius | ed1850a | 2017-02-08 08:45:20 -0800 | [diff] [blame] | 266 | LOG(LS_INFO) << "Median-slope filter experiment enabled with parameters " |
| 267 | << median_slope_window_size_ << ',' |
| 268 | << median_slope_threshold_gain_; |
| 269 | } |
| 270 | if (!in_trendline_experiment_ && !in_median_slope_experiment_) { |
| 271 | LOG(LS_INFO) << "No overuse experiment enabled. Using Kalman filter."; |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 272 | } |
| 273 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 274 | network_thread_.DetachFromThread(); |
| 275 | } |
| 276 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 277 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 278 | const std::vector<PacketFeedback>& packet_feedback_vector) { |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 279 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
stefan | dd20054 | 2017-03-17 06:19:11 -0700 | [diff] [blame] | 280 | RTC_DCHECK(!packet_feedback_vector.empty()); |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 281 | |
| 282 | std::vector<PacketFeedback> sorted_packet_feedback_vector; |
| 283 | SortPacketFeedbackVector(packet_feedback_vector, |
| 284 | &sorted_packet_feedback_vector); |
| 285 | |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 286 | if (!uma_recorded_) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 287 | RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
| 288 | BweNames::kSendSideTransportSeqNum, |
| 289 | BweNames::kBweNamesMax); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 290 | uma_recorded_ = true; |
| 291 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 292 | Result aggregated_result; |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 293 | bool delayed_feedback = true; |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 294 | for (const auto& packet_feedback : sorted_packet_feedback_vector) { |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 295 | if (packet_feedback.send_time_ms < 0) |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 296 | continue; |
| 297 | delayed_feedback = false; |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 298 | Result result = IncomingPacketFeedback(packet_feedback); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 299 | if (result.updated) |
| 300 | aggregated_result = result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 301 | } |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 302 | if (delayed_feedback) { |
| 303 | ++consecutive_delayed_feedbacks_; |
| 304 | } else { |
| 305 | consecutive_delayed_feedbacks_ = 0; |
| 306 | } |
| 307 | if (consecutive_delayed_feedbacks_ >= kMaxConsecutiveFailedLookups) { |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 308 | aggregated_result = OnLongFeedbackDelay( |
| 309 | sorted_packet_feedback_vector.back().arrival_time_ms); |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 310 | consecutive_delayed_feedbacks_ = 0; |
| 311 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 312 | return aggregated_result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 313 | } |
| 314 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 315 | DelayBasedBwe::Result DelayBasedBwe::OnLongFeedbackDelay( |
| 316 | int64_t arrival_time_ms) { |
| 317 | // Estimate should always be valid since a start bitrate always is set in the |
| 318 | // Call constructor. An alternative would be to return an empty Result here, |
| 319 | // or to estimate the throughput based on the feedback we received. |
| 320 | RTC_DCHECK(rate_control_.ValidEstimate()); |
| 321 | rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2, |
| 322 | arrival_time_ms); |
| 323 | Result result; |
| 324 | result.updated = true; |
| 325 | result.probe = false; |
| 326 | result.target_bitrate_bps = rate_control_.LatestEstimate(); |
| 327 | LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to " |
| 328 | << result.target_bitrate_bps; |
| 329 | return result; |
| 330 | } |
| 331 | |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 332 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedback( |
| 333 | const PacketFeedback& packet_feedback) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 334 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 335 | |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 336 | receiver_incoming_bitrate_.Update(packet_feedback.arrival_time_ms, |
| 337 | packet_feedback.payload_size); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 338 | Result result; |
| 339 | // Reset if the stream has timed out. |
| 340 | if (last_seen_packet_ms_ == -1 || |
| 341 | now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { |
| 342 | inter_arrival_.reset( |
| 343 | new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
| 344 | kTimestampToMs, true)); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 345 | kalman_estimator_.reset(new OveruseEstimator(OverUseDetectorOptions())); |
| 346 | trendline_estimator_.reset(new TrendlineEstimator( |
| 347 | trendline_window_size_, trendline_smoothing_coeff_, |
| 348 | trendline_threshold_gain_)); |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 349 | median_slope_estimator_.reset(new MedianSlopeEstimator( |
| 350 | median_slope_window_size_, median_slope_threshold_gain_)); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 351 | } |
| 352 | last_seen_packet_ms_ = now_ms; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 353 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 354 | uint32_t send_time_24bits = |
| 355 | static_cast<uint32_t>( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 356 | ((static_cast<uint64_t>(packet_feedback.send_time_ms) |
| 357 | << kAbsSendTimeFraction) + |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 358 | 500) / |
| 359 | 1000) & |
| 360 | 0x00FFFFFF; |
| 361 | // Shift up send time to use the full 32 bits that inter_arrival works with, |
| 362 | // so wrapping works properly. |
| 363 | uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift; |
stefan | fd0d426 | 2016-09-29 02:44:31 -0700 | [diff] [blame] | 364 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 365 | uint32_t ts_delta = 0; |
| 366 | int64_t t_delta = 0; |
| 367 | int size_delta = 0; |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 368 | if (inter_arrival_->ComputeDeltas(timestamp, packet_feedback.arrival_time_ms, |
| 369 | now_ms, packet_feedback.payload_size, |
| 370 | &ts_delta, &t_delta, &size_delta)) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 371 | double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 372 | if (in_trendline_experiment_) { |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 373 | trendline_estimator_->Update(t_delta, ts_delta_ms, |
| 374 | packet_feedback.arrival_time_ms); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 375 | detector_.Detect(trendline_estimator_->trendline_slope(), ts_delta_ms, |
| 376 | trendline_estimator_->num_of_deltas(), |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 377 | packet_feedback.arrival_time_ms); |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 378 | } else if (in_median_slope_experiment_) { |
| 379 | median_slope_estimator_->Update(t_delta, ts_delta_ms, |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 380 | packet_feedback.arrival_time_ms); |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 381 | detector_.Detect(median_slope_estimator_->trendline_slope(), ts_delta_ms, |
| 382 | median_slope_estimator_->num_of_deltas(), |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 383 | packet_feedback.arrival_time_ms); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 384 | } else { |
| 385 | kalman_estimator_->Update(t_delta, ts_delta_ms, size_delta, |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 386 | detector_.State(), |
| 387 | packet_feedback.arrival_time_ms); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 388 | detector_.Detect(kalman_estimator_->offset(), ts_delta_ms, |
| 389 | kalman_estimator_->num_of_deltas(), |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 390 | packet_feedback.arrival_time_ms); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 391 | } |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 394 | int probing_bps = 0; |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 395 | if (packet_feedback.pacing_info.probe_cluster_id != |
| 396 | PacedPacketInfo::kNotAProbe) { |
| 397 | probing_bps = |
| 398 | probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 399 | } |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 400 | rtc::Optional<uint32_t> acked_bitrate_bps = |
| 401 | receiver_incoming_bitrate_.bitrate_bps(); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 402 | // Currently overusing the bandwidth. |
| 403 | if (detector_.State() == kBwOverusing) { |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 404 | if (acked_bitrate_bps && |
| 405 | rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) { |
| 406 | result.updated = |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 407 | UpdateEstimate(packet_feedback.arrival_time_ms, now_ms, |
| 408 | acked_bitrate_bps, &result.target_bitrate_bps); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 409 | } |
| 410 | } else if (probing_bps > 0) { |
| 411 | // No overuse, but probing measured a bitrate. |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 412 | rate_control_.SetEstimate(probing_bps, packet_feedback.arrival_time_ms); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 413 | result.probe = true; |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 414 | result.updated = |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 415 | UpdateEstimate(packet_feedback.arrival_time_ms, now_ms, |
| 416 | acked_bitrate_bps, &result.target_bitrate_bps); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 417 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 418 | if (!result.updated && |
| 419 | (last_update_ms_ == -1 || |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 420 | now_ms - last_update_ms_ > rate_control_.GetFeedbackInterval())) { |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 421 | result.updated = |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 422 | UpdateEstimate(packet_feedback.arrival_time_ms, now_ms, |
| 423 | acked_bitrate_bps, &result.target_bitrate_bps); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 424 | } |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 425 | if (result.updated) { |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 426 | last_update_ms_ = now_ms; |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 427 | BWE_TEST_LOGGING_PLOT(1, "target_bitrate_bps", now_ms, |
| 428 | result.target_bitrate_bps); |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 429 | if (event_log_ && (result.target_bitrate_bps != last_logged_bitrate_ || |
| 430 | detector_.State() != last_logged_state_)) { |
terelius | 424e6cf | 2017-02-20 05:14:41 -0800 | [diff] [blame] | 431 | event_log_->LogDelayBasedBweUpdate(result.target_bitrate_bps, |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 432 | detector_.State()); |
| 433 | last_logged_bitrate_ = result.target_bitrate_bps; |
| 434 | last_logged_state_ = detector_.State(); |
| 435 | } |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 436 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 437 | |
| 438 | return result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 441 | bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms, |
| 442 | int64_t now_ms, |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 443 | rtc::Optional<uint32_t> acked_bitrate_bps, |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 444 | uint32_t* target_bitrate_bps) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 445 | // TODO(terelius): RateControlInput::noise_var is deprecated and will be |
| 446 | // removed. In the meantime, we set it to zero. |
| 447 | const RateControlInput input(detector_.State(), acked_bitrate_bps, 0); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 448 | rate_control_.Update(&input, now_ms); |
| 449 | *target_bitrate_bps = rate_control_.UpdateBandwidthEstimate(now_ms); |
| 450 | return rate_control_.ValidEstimate(); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 451 | } |
| 452 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 453 | void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 454 | rate_control_.SetRtt(avg_rtt_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 455 | } |
| 456 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 457 | bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs, |
| 458 | uint32_t* bitrate_bps) const { |
| 459 | // Currently accessed from both the process thread (see |
| 460 | // ModuleRtpRtcpImpl::Process()) and the configuration thread (see |
| 461 | // Call::GetStats()). Should in the future only be accessed from a single |
| 462 | // thread. |
| 463 | RTC_DCHECK(ssrcs); |
| 464 | RTC_DCHECK(bitrate_bps); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 465 | if (!rate_control_.ValidEstimate()) |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 466 | return false; |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 467 | |
| 468 | *ssrcs = {kFixedSsrc}; |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 469 | *bitrate_bps = rate_control_.LatestEstimate(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 470 | return true; |
| 471 | } |
| 472 | |
stefan | 5a2c506 | 2017-01-27 06:43:18 -0800 | [diff] [blame] | 473 | void DelayBasedBwe::SetStartBitrate(int start_bitrate_bps) { |
| 474 | LOG(LS_WARNING) << "BWE Setting start bitrate to: " << start_bitrate_bps; |
| 475 | rate_control_.SetStartBitrate(start_bitrate_bps); |
| 476 | } |
| 477 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 478 | void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
| 479 | // Called from both the configuration thread and the network thread. Shouldn't |
| 480 | // be called from the network thread in the future. |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 481 | rate_control_.SetMinBitrate(min_bitrate_bps); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 482 | } |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 483 | |
| 484 | int64_t DelayBasedBwe::GetProbingIntervalMs() const { |
| 485 | return probing_interval_estimator_.GetIntervalMs(); |
| 486 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 487 | } // namespace webrtc |