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