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