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; |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 41 | constexpr int kInitialRateWindowMs = 500; |
| 42 | constexpr int kRateWindowMs = 150; |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 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. |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 45 | constexpr size_t kDefaultTrendlineWindowSize = 20; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 46 | constexpr double kDefaultTrendlineSmoothingCoeff = 0.9; |
| 47 | constexpr double kDefaultTrendlineThresholdGain = 4.0; |
| 48 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 49 | constexpr int kMaxConsecutiveFailedLookups = 5; |
| 50 | |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 51 | const char kBweSparseUpdateExperiment[] = "WebRTC-BweSparseUpdateExperiment"; |
| 52 | |
| 53 | bool BweSparseUpdateExperimentIsEnabled() { |
| 54 | std::string experiment_string = |
| 55 | webrtc::field_trial::FindFullName(kBweSparseUpdateExperiment); |
| 56 | return experiment_string == "Enabled"; |
| 57 | } |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 58 | |
| 59 | class PacketFeedbackComparator { |
| 60 | public: |
| 61 | inline bool operator()(const webrtc::PacketFeedback& lhs, |
| 62 | const webrtc::PacketFeedback& rhs) { |
| 63 | if (lhs.arrival_time_ms != rhs.arrival_time_ms) |
| 64 | return lhs.arrival_time_ms < rhs.arrival_time_ms; |
| 65 | if (lhs.send_time_ms != rhs.send_time_ms) |
| 66 | return lhs.send_time_ms < rhs.send_time_ms; |
| 67 | return lhs.sequence_number < rhs.sequence_number; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | void SortPacketFeedbackVector(const std::vector<webrtc::PacketFeedback>& input, |
| 72 | std::vector<webrtc::PacketFeedback>* output) { |
| 73 | auto pred = [](const webrtc::PacketFeedback& packet_feedback) { |
| 74 | return packet_feedback.arrival_time_ms != |
| 75 | webrtc::PacketFeedback::kNotReceived; |
| 76 | }; |
| 77 | std::copy_if(input.begin(), input.end(), std::back_inserter(*output), pred); |
| 78 | std::sort(output->begin(), output->end(), PacketFeedbackComparator()); |
| 79 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 80 | } // namespace |
| 81 | |
| 82 | namespace webrtc { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 83 | |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 84 | DelayBasedBwe::BitrateEstimator::BitrateEstimator() |
| 85 | : sum_(0), |
| 86 | current_win_ms_(0), |
| 87 | prev_time_ms_(-1), |
| 88 | bitrate_estimate_(-1.0f), |
| 89 | bitrate_estimate_var_(50.0f) {} |
| 90 | |
| 91 | void DelayBasedBwe::BitrateEstimator::Update(int64_t now_ms, int bytes) { |
| 92 | int rate_window_ms = kRateWindowMs; |
| 93 | // We use a larger window at the beginning to get a more stable sample that |
| 94 | // we can use to initialize the estimate. |
| 95 | if (bitrate_estimate_ < 0.f) |
| 96 | rate_window_ms = kInitialRateWindowMs; |
| 97 | float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); |
| 98 | if (bitrate_sample < 0.0f) |
| 99 | return; |
| 100 | if (bitrate_estimate_ < 0.0f) { |
| 101 | // This is the very first sample we get. Use it to initialize the estimate. |
| 102 | bitrate_estimate_ = bitrate_sample; |
| 103 | return; |
| 104 | } |
| 105 | // Define the sample uncertainty as a function of how far away it is from the |
| 106 | // current estimate. |
| 107 | float sample_uncertainty = |
| 108 | 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_; |
| 109 | float sample_var = sample_uncertainty * sample_uncertainty; |
| 110 | // Update a bayesian estimate of the rate, weighting it lower if the sample |
| 111 | // uncertainty is large. |
| 112 | // The bitrate estimate uncertainty is increased with each update to model |
| 113 | // that the bitrate changes over time. |
| 114 | float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; |
| 115 | bitrate_estimate_ = (sample_var * bitrate_estimate_ + |
| 116 | pred_bitrate_estimate_var * bitrate_sample) / |
| 117 | (sample_var + pred_bitrate_estimate_var); |
| 118 | bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / |
| 119 | (sample_var + pred_bitrate_estimate_var); |
| 120 | } |
| 121 | |
| 122 | float DelayBasedBwe::BitrateEstimator::UpdateWindow(int64_t now_ms, |
| 123 | int bytes, |
| 124 | int rate_window_ms) { |
| 125 | // Reset if time moves backwards. |
| 126 | if (now_ms < prev_time_ms_) { |
| 127 | prev_time_ms_ = -1; |
| 128 | sum_ = 0; |
| 129 | current_win_ms_ = 0; |
| 130 | } |
| 131 | if (prev_time_ms_ >= 0) { |
| 132 | current_win_ms_ += now_ms - prev_time_ms_; |
| 133 | // Reset if nothing has been received for more than a full window. |
| 134 | if (now_ms - prev_time_ms_ > rate_window_ms) { |
| 135 | sum_ = 0; |
| 136 | current_win_ms_ %= rate_window_ms; |
| 137 | } |
| 138 | } |
| 139 | prev_time_ms_ = now_ms; |
| 140 | float bitrate_sample = -1.0f; |
| 141 | if (current_win_ms_ >= rate_window_ms) { |
| 142 | bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); |
| 143 | current_win_ms_ -= rate_window_ms; |
| 144 | sum_ = 0; |
| 145 | } |
| 146 | sum_ += bytes; |
| 147 | return bitrate_sample; |
| 148 | } |
| 149 | |
| 150 | rtc::Optional<uint32_t> DelayBasedBwe::BitrateEstimator::bitrate_bps() const { |
| 151 | if (bitrate_estimate_ < 0.f) |
| 152 | return rtc::Optional<uint32_t>(); |
| 153 | return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); |
| 154 | } |
| 155 | |
elad.alon | 61ce37e | 2017-03-09 07:09:31 -0800 | [diff] [blame] | 156 | DelayBasedBwe::DelayBasedBwe(RtcEventLog* event_log, const Clock* clock) |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 157 | : event_log_(event_log), |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 158 | clock_(clock), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 159 | inter_arrival_(), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 160 | trendline_estimator_(), |
terelius | 84f83f8 | 2016-12-27 10:43:01 -0800 | [diff] [blame] | 161 | detector_(), |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 162 | receiver_incoming_bitrate_(), |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 163 | last_seen_packet_ms_(-1), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 164 | uma_recorded_(false), |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 165 | probe_bitrate_estimator_(event_log), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 166 | trendline_window_size_(kDefaultTrendlineWindowSize), |
| 167 | trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff), |
| 168 | trendline_threshold_gain_(kDefaultTrendlineThresholdGain), |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 169 | consecutive_delayed_feedbacks_(0), |
| 170 | last_logged_bitrate_(0), |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 171 | last_logged_state_(BandwidthUsage::kBwNormal), |
| 172 | in_sparse_update_experiment_(BweSparseUpdateExperimentIsEnabled()) { |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 173 | LOG(LS_INFO) << "Using Trendline filter for delay change estimation."; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 174 | network_thread_.DetachFromThread(); |
| 175 | } |
| 176 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 177 | DelayBasedBwe::~DelayBasedBwe() {} |
| 178 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 179 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 180 | const std::vector<PacketFeedback>& packet_feedback_vector) { |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 181 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 182 | |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 183 | std::vector<PacketFeedback> sorted_packet_feedback_vector; |
| 184 | SortPacketFeedbackVector(packet_feedback_vector, |
| 185 | &sorted_packet_feedback_vector); |
stefan | 80fba25 | 2017-04-18 06:45:12 -0700 | [diff] [blame] | 186 | // TOOD(holmer): An empty feedback vector here likely means that |
| 187 | // all acks were too late and that the send time history had |
| 188 | // timed out. We should reduce the rate when this occurs. |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 189 | if (sorted_packet_feedback_vector.empty()) { |
stefan | 80fba25 | 2017-04-18 06:45:12 -0700 | [diff] [blame] | 190 | LOG(LS_WARNING) << "Very late feedback received."; |
| 191 | return DelayBasedBwe::Result(); |
| 192 | } |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 193 | |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 194 | if (!uma_recorded_) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 195 | RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
| 196 | BweNames::kSendSideTransportSeqNum, |
| 197 | BweNames::kBweNamesMax); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 198 | uma_recorded_ = true; |
| 199 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 200 | bool overusing = false; |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 201 | bool delayed_feedback = true; |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 202 | for (const auto& packet_feedback : sorted_packet_feedback_vector) { |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 203 | if (packet_feedback.send_time_ms < 0) |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 204 | continue; |
| 205 | delayed_feedback = false; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 206 | IncomingPacketFeedback(packet_feedback); |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 207 | if (!in_sparse_update_experiment_) |
| 208 | overusing |= (detector_.State() == BandwidthUsage::kBwOverusing); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 209 | } |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 210 | if (in_sparse_update_experiment_) |
| 211 | overusing = (detector_.State() == BandwidthUsage::kBwOverusing); |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 212 | if (delayed_feedback) { |
| 213 | ++consecutive_delayed_feedbacks_; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 214 | if (consecutive_delayed_feedbacks_ >= kMaxConsecutiveFailedLookups) { |
| 215 | consecutive_delayed_feedbacks_ = 0; |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 216 | return OnLongFeedbackDelay( |
| 217 | sorted_packet_feedback_vector.back().arrival_time_ms); |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 218 | } |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 219 | } else { |
| 220 | consecutive_delayed_feedbacks_ = 0; |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 221 | return MaybeUpdateEstimate(overusing); |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 222 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 223 | return Result(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 224 | } |
| 225 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 226 | DelayBasedBwe::Result DelayBasedBwe::OnLongFeedbackDelay( |
| 227 | int64_t arrival_time_ms) { |
| 228 | // Estimate should always be valid since a start bitrate always is set in the |
| 229 | // Call constructor. An alternative would be to return an empty Result here, |
| 230 | // or to estimate the throughput based on the feedback we received. |
| 231 | RTC_DCHECK(rate_control_.ValidEstimate()); |
| 232 | rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2, |
| 233 | arrival_time_ms); |
| 234 | Result result; |
| 235 | result.updated = true; |
| 236 | result.probe = false; |
| 237 | result.target_bitrate_bps = rate_control_.LatestEstimate(); |
| 238 | LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to " |
| 239 | << result.target_bitrate_bps; |
| 240 | return result; |
| 241 | } |
| 242 | |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 243 | void DelayBasedBwe::IncomingPacketFeedback( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 244 | const PacketFeedback& packet_feedback) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 245 | int64_t now_ms = clock_->TimeInMilliseconds(); |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 246 | |
| 247 | receiver_incoming_bitrate_.Update(packet_feedback.arrival_time_ms, |
| 248 | packet_feedback.payload_size); |
| 249 | Result result; |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 250 | // Reset if the stream has timed out. |
| 251 | if (last_seen_packet_ms_ == -1 || |
| 252 | now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { |
| 253 | inter_arrival_.reset( |
| 254 | new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
| 255 | kTimestampToMs, true)); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 256 | trendline_estimator_.reset(new TrendlineEstimator( |
| 257 | trendline_window_size_, trendline_smoothing_coeff_, |
| 258 | trendline_threshold_gain_)); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 259 | } |
| 260 | last_seen_packet_ms_ = now_ms; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 261 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 262 | uint32_t send_time_24bits = |
| 263 | static_cast<uint32_t>( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 264 | ((static_cast<uint64_t>(packet_feedback.send_time_ms) |
| 265 | << kAbsSendTimeFraction) + |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 266 | 500) / |
| 267 | 1000) & |
| 268 | 0x00FFFFFF; |
| 269 | // Shift up send time to use the full 32 bits that inter_arrival works with, |
| 270 | // so wrapping works properly. |
| 271 | uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift; |
stefan | fd0d426 | 2016-09-29 02:44:31 -0700 | [diff] [blame] | 272 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 273 | uint32_t ts_delta = 0; |
| 274 | int64_t t_delta = 0; |
| 275 | int size_delta = 0; |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 276 | if (inter_arrival_->ComputeDeltas(timestamp, packet_feedback.arrival_time_ms, |
| 277 | now_ms, packet_feedback.payload_size, |
| 278 | &ts_delta, &t_delta, &size_delta)) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 279 | double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift); |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 280 | trendline_estimator_->Update(t_delta, ts_delta_ms, |
| 281 | packet_feedback.arrival_time_ms); |
| 282 | detector_.Detect(trendline_estimator_->trendline_slope(), ts_delta_ms, |
| 283 | trendline_estimator_->num_of_deltas(), |
| 284 | packet_feedback.arrival_time_ms); |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 285 | } |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 286 | if (packet_feedback.pacing_info.probe_cluster_id != |
| 287 | PacedPacketInfo::kNotAProbe) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 288 | probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 289 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 290 | } |
| 291 | |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 292 | DelayBasedBwe::Result DelayBasedBwe::MaybeUpdateEstimate(bool overusing) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 293 | Result result; |
| 294 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 295 | |
tschumim | 807736e | 2017-06-08 00:10:31 -0700 | [diff] [blame^] | 296 | rtc::Optional<uint32_t> acked_bitrate_bps = |
| 297 | receiver_incoming_bitrate_.bitrate_bps(); |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 298 | rtc::Optional<int> probe_bitrate_bps = |
| 299 | probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrateBps(); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 300 | // Currently overusing the bandwidth. |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 301 | if (overusing) { |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 302 | if (acked_bitrate_bps && |
| 303 | rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 304 | result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing, |
| 305 | &result.target_bitrate_bps); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 306 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 307 | } else { |
| 308 | if (probe_bitrate_bps) { |
| 309 | rate_control_.SetEstimate(*probe_bitrate_bps, now_ms); |
| 310 | result.probe = true; |
| 311 | } |
| 312 | result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing, |
| 313 | &result.target_bitrate_bps); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 314 | } |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 315 | if (result.updated) { |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 316 | BWE_TEST_LOGGING_PLOT(1, "target_bitrate_bps", now_ms, |
| 317 | result.target_bitrate_bps); |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 318 | if (event_log_ && (result.target_bitrate_bps != last_logged_bitrate_ || |
| 319 | detector_.State() != last_logged_state_)) { |
terelius | 424e6cf | 2017-02-20 05:14:41 -0800 | [diff] [blame] | 320 | event_log_->LogDelayBasedBweUpdate(result.target_bitrate_bps, |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 321 | detector_.State()); |
| 322 | last_logged_bitrate_ = result.target_bitrate_bps; |
| 323 | last_logged_state_ = detector_.State(); |
| 324 | } |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 325 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 326 | return result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 327 | } |
| 328 | |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 329 | bool DelayBasedBwe::UpdateEstimate(int64_t now_ms, |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 330 | rtc::Optional<uint32_t> acked_bitrate_bps, |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 331 | bool overusing, |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 332 | uint32_t* target_bitrate_bps) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 333 | // TODO(terelius): RateControlInput::noise_var is deprecated and will be |
| 334 | // removed. In the meantime, we set it to zero. |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 335 | const RateControlInput input( |
| 336 | overusing ? BandwidthUsage::kBwOverusing : detector_.State(), |
| 337 | acked_bitrate_bps, 0); |
terelius | d1b0e0e | 2017-04-03 02:27:08 -0700 | [diff] [blame] | 338 | *target_bitrate_bps = rate_control_.Update(&input, now_ms); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 339 | return rate_control_.ValidEstimate(); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 340 | } |
| 341 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 342 | void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 343 | rate_control_.SetRtt(avg_rtt_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 344 | } |
| 345 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 346 | bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs, |
| 347 | uint32_t* bitrate_bps) const { |
| 348 | // Currently accessed from both the process thread (see |
| 349 | // ModuleRtpRtcpImpl::Process()) and the configuration thread (see |
| 350 | // Call::GetStats()). Should in the future only be accessed from a single |
| 351 | // thread. |
| 352 | RTC_DCHECK(ssrcs); |
| 353 | RTC_DCHECK(bitrate_bps); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 354 | if (!rate_control_.ValidEstimate()) |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 355 | return false; |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 356 | |
| 357 | *ssrcs = {kFixedSsrc}; |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 358 | *bitrate_bps = rate_control_.LatestEstimate(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
stefan | 5a2c506 | 2017-01-27 06:43:18 -0800 | [diff] [blame] | 362 | void DelayBasedBwe::SetStartBitrate(int start_bitrate_bps) { |
| 363 | LOG(LS_WARNING) << "BWE Setting start bitrate to: " << start_bitrate_bps; |
| 364 | rate_control_.SetStartBitrate(start_bitrate_bps); |
| 365 | } |
| 366 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 367 | void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
| 368 | // Called from both the configuration thread and the network thread. Shouldn't |
| 369 | // be called from the network thread in the future. |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 370 | rate_control_.SetMinBitrate(min_bitrate_bps); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 371 | } |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 372 | |
terelius | 6737045 | 2017-04-19 09:15:04 -0700 | [diff] [blame] | 373 | int64_t DelayBasedBwe::GetExpectedBwePeriodMs() const { |
| 374 | return rate_control_.GetExpectedBandwidthPeriodMs(); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 375 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 376 | } // namespace webrtc |