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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/congestion_controller/delay_based_bwe.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 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 | |
Elad Alon | 4a87e1c | 2017-10-03 16:11:34 +0200 | [diff] [blame] | 17 | #include "logging/rtc_event_log/events/rtc_event_bwe_update_delay_based.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 19 | #include "modules/pacing/paced_sender.h" |
| 20 | #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 21 | #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/constructormagic.h" |
| 24 | #include "rtc_base/logging.h" |
Elad Alon | 4a87e1c | 2017-10-03 16:11:34 +0200 | [diff] [blame] | 25 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread_annotations.h" |
| 27 | #include "system_wrappers/include/field_trial.h" |
| 28 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 29 | #include "typedefs.h" // NOLINT(build/include) |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 30 | |
| 31 | namespace { |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 32 | constexpr int kTimestampGroupLengthMs = 5; |
| 33 | constexpr int kAbsSendTimeFraction = 18; |
| 34 | constexpr int kAbsSendTimeInterArrivalUpshift = 8; |
| 35 | constexpr int kInterArrivalShift = |
| 36 | kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; |
| 37 | constexpr double kTimestampToMs = |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 38 | 1000.0 / static_cast<double>(1 << kInterArrivalShift); |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 39 | // This ssrc is used to fulfill the current API but will be removed |
| 40 | // after the API has been changed. |
| 41 | constexpr uint32_t kFixedSsrc = 0; |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 42 | |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 43 | // Parameters for linear least squares fit of regression line to noisy data. |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 44 | constexpr size_t kDefaultTrendlineWindowSize = 20; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 45 | constexpr double kDefaultTrendlineSmoothingCoeff = 0.9; |
| 46 | constexpr double kDefaultTrendlineThresholdGain = 4.0; |
| 47 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 48 | constexpr int kMaxConsecutiveFailedLookups = 5; |
| 49 | |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 50 | const char kBweSparseUpdateExperiment[] = "WebRTC-BweSparseUpdateExperiment"; |
Stefan Holmer | ea00e48 | 2017-10-06 08:43:34 +0200 | [diff] [blame] | 51 | const char kBweWindowSizeInPacketsExperiment[] = |
| 52 | "WebRTC-BweWindowSizeInPackets"; |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 53 | |
Stefan Holmer | ea00e48 | 2017-10-06 08:43:34 +0200 | [diff] [blame] | 54 | size_t ReadTrendlineFilterWindowSize() { |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 55 | std::string experiment_string = |
Stefan Holmer | ea00e48 | 2017-10-06 08:43:34 +0200 | [diff] [blame] | 56 | webrtc::field_trial::FindFullName(kBweWindowSizeInPacketsExperiment); |
| 57 | size_t window_size; |
| 58 | int parsed_values = |
| 59 | sscanf(experiment_string.c_str(), "Enabled-%zu", &window_size); |
| 60 | if (parsed_values == 1) { |
| 61 | if (window_size > 1) |
| 62 | return window_size; |
| 63 | LOG(WARNING) << "Window size must be greater than 1."; |
| 64 | } |
| 65 | LOG(LS_WARNING) << "Failed to parse parameters for BweTrendlineFilter " |
| 66 | "experiment from field trial string. Using default."; |
| 67 | return kDefaultTrendlineWindowSize; |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 68 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 69 | } // namespace |
| 70 | |
| 71 | namespace webrtc { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 72 | |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 73 | DelayBasedBwe::Result::Result() |
| 74 | : updated(false), |
| 75 | probe(false), |
| 76 | target_bitrate_bps(0), |
| 77 | recovered_from_overuse(false) {} |
| 78 | |
| 79 | DelayBasedBwe::Result::Result(bool probe, uint32_t target_bitrate_bps) |
| 80 | : updated(true), |
| 81 | probe(probe), |
| 82 | target_bitrate_bps(target_bitrate_bps), |
| 83 | recovered_from_overuse(false) {} |
| 84 | |
| 85 | DelayBasedBwe::Result::~Result() {} |
| 86 | |
elad.alon | 61ce37e | 2017-03-09 07:09:31 -0800 | [diff] [blame] | 87 | DelayBasedBwe::DelayBasedBwe(RtcEventLog* event_log, const Clock* clock) |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 88 | : event_log_(event_log), |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 89 | clock_(clock), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 90 | inter_arrival_(), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 91 | trendline_estimator_(), |
terelius | 84f83f8 | 2016-12-27 10:43:01 -0800 | [diff] [blame] | 92 | detector_(), |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 93 | last_seen_packet_ms_(-1), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 94 | uma_recorded_(false), |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 95 | probe_bitrate_estimator_(event_log), |
Stefan Holmer | ea00e48 | 2017-10-06 08:43:34 +0200 | [diff] [blame] | 96 | trendline_window_size_( |
| 97 | webrtc::field_trial::IsEnabled(kBweWindowSizeInPacketsExperiment) |
| 98 | ? ReadTrendlineFilterWindowSize() |
| 99 | : kDefaultTrendlineWindowSize), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 100 | trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff), |
| 101 | trendline_threshold_gain_(kDefaultTrendlineThresholdGain), |
terelius | 0baf55d | 2017-02-17 03:38:28 -0800 | [diff] [blame] | 102 | consecutive_delayed_feedbacks_(0), |
philipel | 8e56076 | 2017-10-02 14:00:13 +0200 | [diff] [blame] | 103 | prev_bitrate_(0), |
| 104 | prev_state_(BandwidthUsage::kBwNormal), |
Stefan Holmer | ea00e48 | 2017-10-06 08:43:34 +0200 | [diff] [blame] | 105 | in_sparse_update_experiment_( |
| 106 | webrtc::field_trial::IsEnabled(kBweSparseUpdateExperiment)) { |
| 107 | LOG(LS_INFO) |
| 108 | << "Using Trendline filter for delay change estimation with window size " |
| 109 | << trendline_window_size_; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 110 | } |
| 111 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 112 | DelayBasedBwe::~DelayBasedBwe() {} |
| 113 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 114 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 115 | const std::vector<PacketFeedback>& packet_feedback_vector, |
| 116 | rtc::Optional<uint32_t> acked_bitrate_bps) { |
| 117 | RTC_DCHECK(std::is_sorted(packet_feedback_vector.begin(), |
| 118 | packet_feedback_vector.end(), |
| 119 | PacketFeedbackComparator())); |
erikvarga | bf5a2fc | 2017-06-16 05:02:05 -0700 | [diff] [blame] | 120 | RTC_DCHECK_RUNS_SERIALIZED(&network_race_); |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 121 | |
stefan | 80fba25 | 2017-04-18 06:45:12 -0700 | [diff] [blame] | 122 | // TOOD(holmer): An empty feedback vector here likely means that |
| 123 | // all acks were too late and that the send time history had |
| 124 | // timed out. We should reduce the rate when this occurs. |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 125 | if (packet_feedback_vector.empty()) { |
stefan | 80fba25 | 2017-04-18 06:45:12 -0700 | [diff] [blame] | 126 | LOG(LS_WARNING) << "Very late feedback received."; |
| 127 | return DelayBasedBwe::Result(); |
| 128 | } |
elad.alon | ec304f9 | 2017-03-08 05:03:53 -0800 | [diff] [blame] | 129 | |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 130 | if (!uma_recorded_) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 131 | RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
| 132 | BweNames::kSendSideTransportSeqNum, |
| 133 | BweNames::kBweNamesMax); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 134 | uma_recorded_ = true; |
| 135 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 136 | bool overusing = false; |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 137 | bool delayed_feedback = true; |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 138 | bool recovered_from_overuse = false; |
| 139 | BandwidthUsage prev_detector_state = detector_.State(); |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 140 | for (const auto& packet_feedback : packet_feedback_vector) { |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 141 | if (packet_feedback.send_time_ms < 0) |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 142 | continue; |
| 143 | delayed_feedback = false; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 144 | IncomingPacketFeedback(packet_feedback); |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 145 | if (!in_sparse_update_experiment_) |
| 146 | overusing |= (detector_.State() == BandwidthUsage::kBwOverusing); |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 147 | if (prev_detector_state == BandwidthUsage::kBwUnderusing && |
| 148 | detector_.State() == BandwidthUsage::kBwNormal) { |
| 149 | recovered_from_overuse = true; |
| 150 | } |
| 151 | prev_detector_state = detector_.State(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 152 | } |
terelius | bf2c049 | 2017-05-02 01:04:26 -0700 | [diff] [blame] | 153 | if (in_sparse_update_experiment_) |
| 154 | overusing = (detector_.State() == BandwidthUsage::kBwOverusing); |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 155 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 156 | if (delayed_feedback) { |
| 157 | ++consecutive_delayed_feedbacks_; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 158 | if (consecutive_delayed_feedbacks_ >= kMaxConsecutiveFailedLookups) { |
| 159 | consecutive_delayed_feedbacks_ = 0; |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 160 | return OnLongFeedbackDelay(packet_feedback_vector.back().arrival_time_ms); |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 161 | } |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 162 | } else { |
| 163 | consecutive_delayed_feedbacks_ = 0; |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 164 | return MaybeUpdateEstimate(overusing, acked_bitrate_bps, |
| 165 | recovered_from_overuse); |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 166 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 167 | return Result(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 168 | } |
| 169 | |
stefan | e3a5567 | 2017-02-13 09:08:22 -0800 | [diff] [blame] | 170 | DelayBasedBwe::Result DelayBasedBwe::OnLongFeedbackDelay( |
| 171 | int64_t arrival_time_ms) { |
| 172 | // Estimate should always be valid since a start bitrate always is set in the |
| 173 | // Call constructor. An alternative would be to return an empty Result here, |
| 174 | // or to estimate the throughput based on the feedback we received. |
| 175 | RTC_DCHECK(rate_control_.ValidEstimate()); |
| 176 | rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2, |
| 177 | arrival_time_ms); |
| 178 | Result result; |
| 179 | result.updated = true; |
| 180 | result.probe = false; |
| 181 | result.target_bitrate_bps = rate_control_.LatestEstimate(); |
| 182 | LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to " |
| 183 | << result.target_bitrate_bps; |
| 184 | return result; |
| 185 | } |
| 186 | |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 187 | void DelayBasedBwe::IncomingPacketFeedback( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 188 | const PacketFeedback& packet_feedback) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 189 | int64_t now_ms = clock_->TimeInMilliseconds(); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 190 | // Reset if the stream has timed out. |
| 191 | if (last_seen_packet_ms_ == -1 || |
| 192 | now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { |
| 193 | inter_arrival_.reset( |
| 194 | new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
| 195 | kTimestampToMs, true)); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 196 | trendline_estimator_.reset(new TrendlineEstimator( |
| 197 | trendline_window_size_, trendline_smoothing_coeff_, |
| 198 | trendline_threshold_gain_)); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 199 | } |
| 200 | last_seen_packet_ms_ = now_ms; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 201 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 202 | uint32_t send_time_24bits = |
| 203 | static_cast<uint32_t>( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 204 | ((static_cast<uint64_t>(packet_feedback.send_time_ms) |
| 205 | << kAbsSendTimeFraction) + |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 206 | 500) / |
| 207 | 1000) & |
| 208 | 0x00FFFFFF; |
| 209 | // Shift up send time to use the full 32 bits that inter_arrival works with, |
| 210 | // so wrapping works properly. |
| 211 | uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift; |
stefan | fd0d426 | 2016-09-29 02:44:31 -0700 | [diff] [blame] | 212 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 213 | uint32_t ts_delta = 0; |
| 214 | int64_t t_delta = 0; |
| 215 | int size_delta = 0; |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 216 | if (inter_arrival_->ComputeDeltas(timestamp, packet_feedback.arrival_time_ms, |
| 217 | now_ms, packet_feedback.payload_size, |
| 218 | &ts_delta, &t_delta, &size_delta)) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 219 | double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift); |
stefan | 76d9c9c | 2017-04-01 06:51:09 -0700 | [diff] [blame] | 220 | trendline_estimator_->Update(t_delta, ts_delta_ms, |
| 221 | packet_feedback.arrival_time_ms); |
| 222 | detector_.Detect(trendline_estimator_->trendline_slope(), ts_delta_ms, |
| 223 | trendline_estimator_->num_of_deltas(), |
| 224 | packet_feedback.arrival_time_ms); |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 225 | } |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 226 | if (packet_feedback.pacing_info.probe_cluster_id != |
| 227 | PacedPacketInfo::kNotAProbe) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 228 | probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 229 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 230 | } |
| 231 | |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 232 | DelayBasedBwe::Result DelayBasedBwe::MaybeUpdateEstimate( |
| 233 | bool overusing, |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 234 | rtc::Optional<uint32_t> acked_bitrate_bps, |
| 235 | bool recovered_from_overuse) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 236 | Result result; |
| 237 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 238 | |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 239 | rtc::Optional<int> probe_bitrate_bps = |
| 240 | probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrateBps(); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 241 | // Currently overusing the bandwidth. |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 242 | if (overusing) { |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 243 | if (acked_bitrate_bps && |
| 244 | rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 245 | result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing, |
| 246 | &result.target_bitrate_bps); |
terelius | a9521e2 | 2017-07-04 04:52:58 -0700 | [diff] [blame] | 247 | } else if (!acked_bitrate_bps && rate_control_.ValidEstimate() && |
| 248 | rate_control_.TimeToReduceFurther( |
| 249 | now_ms, rate_control_.LatestEstimate() / 2 - 1)) { |
| 250 | // Overusing before we have a measured acknowledged bitrate. We check |
| 251 | // TimeToReduceFurther (with a fake acknowledged bitrate) to avoid |
| 252 | // reducing too often. |
| 253 | // TODO(tschumim): Improve this and/or the acknowledged bitrate estimator |
| 254 | // so that we (almost) always have a bitrate estimate. |
| 255 | rate_control_.SetEstimate(rate_control_.LatestEstimate() / 2, now_ms); |
| 256 | result.updated = true; |
| 257 | result.probe = false; |
| 258 | result.target_bitrate_bps = rate_control_.LatestEstimate(); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 259 | } |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 260 | } else { |
| 261 | if (probe_bitrate_bps) { |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 262 | result.probe = true; |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 263 | result.updated = true; |
| 264 | result.target_bitrate_bps = *probe_bitrate_bps; |
| 265 | rate_control_.SetEstimate(*probe_bitrate_bps, now_ms); |
| 266 | } else { |
| 267 | result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing, |
| 268 | &result.target_bitrate_bps); |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame] | 269 | result.recovered_from_overuse = recovered_from_overuse; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 270 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 271 | } |
philipel | 8e56076 | 2017-10-02 14:00:13 +0200 | [diff] [blame] | 272 | if ((result.updated && prev_bitrate_ != result.target_bitrate_bps) || |
| 273 | detector_.State() != prev_state_) { |
| 274 | uint32_t bitrate_bps = |
| 275 | result.updated ? result.target_bitrate_bps : prev_bitrate_; |
| 276 | |
| 277 | BWE_TEST_LOGGING_PLOT(1, "target_bitrate_bps", now_ms, bitrate_bps); |
| 278 | |
Elad Alon | 4a87e1c | 2017-10-03 16:11:34 +0200 | [diff] [blame] | 279 | if (event_log_) { |
| 280 | event_log_->Log(rtc::MakeUnique<RtcEventBweUpdateDelayBased>( |
| 281 | bitrate_bps, detector_.State())); |
| 282 | } |
philipel | 8e56076 | 2017-10-02 14:00:13 +0200 | [diff] [blame] | 283 | |
| 284 | prev_bitrate_ = bitrate_bps; |
| 285 | prev_state_ = detector_.State(); |
terelius | 5a38836 | 2016-12-09 05:50:01 -0800 | [diff] [blame] | 286 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 287 | return result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 288 | } |
| 289 | |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 290 | bool DelayBasedBwe::UpdateEstimate(int64_t now_ms, |
Stefan Holmer | 492ee28 | 2016-10-27 17:19:20 +0200 | [diff] [blame] | 291 | rtc::Optional<uint32_t> acked_bitrate_bps, |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 292 | bool overusing, |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 293 | uint32_t* target_bitrate_bps) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 294 | // TODO(terelius): RateControlInput::noise_var is deprecated and will be |
| 295 | // removed. In the meantime, we set it to zero. |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 296 | const RateControlInput input( |
| 297 | overusing ? BandwidthUsage::kBwOverusing : detector_.State(), |
| 298 | acked_bitrate_bps, 0); |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 299 | uint32_t prev_target_bitrate_bps = rate_control_.LatestEstimate(); |
terelius | d1b0e0e | 2017-04-03 02:27:08 -0700 | [diff] [blame] | 300 | *target_bitrate_bps = rate_control_.Update(&input, now_ms); |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 301 | return rate_control_.ValidEstimate() && |
| 302 | prev_target_bitrate_bps != *target_bitrate_bps; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 303 | } |
| 304 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 305 | void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 306 | rate_control_.SetRtt(avg_rtt_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 307 | } |
| 308 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 309 | bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs, |
| 310 | uint32_t* bitrate_bps) const { |
| 311 | // Currently accessed from both the process thread (see |
| 312 | // ModuleRtpRtcpImpl::Process()) and the configuration thread (see |
| 313 | // Call::GetStats()). Should in the future only be accessed from a single |
| 314 | // thread. |
| 315 | RTC_DCHECK(ssrcs); |
| 316 | RTC_DCHECK(bitrate_bps); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 317 | if (!rate_control_.ValidEstimate()) |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 318 | return false; |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 319 | |
| 320 | *ssrcs = {kFixedSsrc}; |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 321 | *bitrate_bps = rate_control_.LatestEstimate(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
stefan | 5a2c506 | 2017-01-27 06:43:18 -0800 | [diff] [blame] | 325 | void DelayBasedBwe::SetStartBitrate(int start_bitrate_bps) { |
| 326 | LOG(LS_WARNING) << "BWE Setting start bitrate to: " << start_bitrate_bps; |
| 327 | rate_control_.SetStartBitrate(start_bitrate_bps); |
| 328 | } |
| 329 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 330 | void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
| 331 | // Called from both the configuration thread and the network thread. Shouldn't |
| 332 | // be called from the network thread in the future. |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame] | 333 | rate_control_.SetMinBitrate(min_bitrate_bps); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 334 | } |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 335 | |
terelius | 6737045 | 2017-04-19 09:15:04 -0700 | [diff] [blame] | 336 | int64_t DelayBasedBwe::GetExpectedBwePeriodMs() const { |
| 337 | return rate_control_.GetExpectedBandwidthPeriodMs(); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 338 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 339 | } // namespace webrtc |