Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: webrtc/modules/congestion_controller/delay_based_bwe.cc

Issue 2917873002: Refactored incoming bitrate estimator. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 20 matching lines...) Expand all
31 constexpr int kTimestampGroupLengthMs = 5; 31 constexpr int kTimestampGroupLengthMs = 5;
32 constexpr int kAbsSendTimeFraction = 18; 32 constexpr int kAbsSendTimeFraction = 18;
33 constexpr int kAbsSendTimeInterArrivalUpshift = 8; 33 constexpr int kAbsSendTimeInterArrivalUpshift = 8;
34 constexpr int kInterArrivalShift = 34 constexpr int kInterArrivalShift =
35 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; 35 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift;
36 constexpr double kTimestampToMs = 36 constexpr double kTimestampToMs =
37 1000.0 / static_cast<double>(1 << kInterArrivalShift); 37 1000.0 / static_cast<double>(1 << kInterArrivalShift);
38 // This ssrc is used to fulfill the current API but will be removed 38 // This ssrc is used to fulfill the current API but will be removed
39 // after the API has been changed. 39 // after the API has been changed.
40 constexpr uint32_t kFixedSsrc = 0; 40 constexpr uint32_t kFixedSsrc = 0;
41 constexpr int kInitialRateWindowMs = 500;
42 constexpr int kRateWindowMs = 150;
43 41
44 // Parameters for linear least squares fit of regression line to noisy data. 42 // Parameters for linear least squares fit of regression line to noisy data.
45 constexpr size_t kDefaultTrendlineWindowSize = 20; 43 constexpr size_t kDefaultTrendlineWindowSize = 20;
46 constexpr double kDefaultTrendlineSmoothingCoeff = 0.9; 44 constexpr double kDefaultTrendlineSmoothingCoeff = 0.9;
47 constexpr double kDefaultTrendlineThresholdGain = 4.0; 45 constexpr double kDefaultTrendlineThresholdGain = 4.0;
48 46
49 constexpr int kMaxConsecutiveFailedLookups = 5; 47 constexpr int kMaxConsecutiveFailedLookups = 5;
50 48
51 const char kBweSparseUpdateExperiment[] = "WebRTC-BweSparseUpdateExperiment"; 49 const char kBweSparseUpdateExperiment[] = "WebRTC-BweSparseUpdateExperiment";
52 50
(...skipping 21 matching lines...) Expand all
74 return packet_feedback.arrival_time_ms != 72 return packet_feedback.arrival_time_ms !=
75 webrtc::PacketFeedback::kNotReceived; 73 webrtc::PacketFeedback::kNotReceived;
76 }; 74 };
77 std::copy_if(input.begin(), input.end(), std::back_inserter(*output), pred); 75 std::copy_if(input.begin(), input.end(), std::back_inserter(*output), pred);
78 std::sort(output->begin(), output->end(), PacketFeedbackComparator()); 76 std::sort(output->begin(), output->end(), PacketFeedbackComparator());
79 } 77 }
80 } // namespace 78 } // namespace
81 79
82 namespace webrtc { 80 namespace webrtc {
83 81
84 DelayBasedBwe::BitrateEstimator::BitrateEstimator() 82 DelayBasedBwe::DelayBasedBwe(
85 : sum_(0), 83 RtcEventLog* event_log,
86 current_win_ms_(0), 84 IncomingBitrateEstimator* const receiver_incoming_bitrate,
87 prev_time_ms_(-1), 85 const Clock* clock)
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
156 DelayBasedBwe::DelayBasedBwe(RtcEventLog* event_log, const Clock* clock)
157 : event_log_(event_log), 86 : event_log_(event_log),
158 clock_(clock), 87 clock_(clock),
159 inter_arrival_(), 88 inter_arrival_(),
160 trendline_estimator_(), 89 trendline_estimator_(),
161 detector_(), 90 detector_(),
162 receiver_incoming_bitrate_(), 91 receiver_incoming_bitrate_(receiver_incoming_bitrate),
163 last_seen_packet_ms_(-1), 92 last_seen_packet_ms_(-1),
164 uma_recorded_(false), 93 uma_recorded_(false),
165 probe_bitrate_estimator_(event_log), 94 probe_bitrate_estimator_(event_log),
166 trendline_window_size_(kDefaultTrendlineWindowSize), 95 trendline_window_size_(kDefaultTrendlineWindowSize),
167 trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff), 96 trendline_smoothing_coeff_(kDefaultTrendlineSmoothingCoeff),
168 trendline_threshold_gain_(kDefaultTrendlineThresholdGain), 97 trendline_threshold_gain_(kDefaultTrendlineThresholdGain),
169 consecutive_delayed_feedbacks_(0), 98 consecutive_delayed_feedbacks_(0),
170 last_logged_bitrate_(0), 99 last_logged_bitrate_(0),
171 last_logged_state_(BandwidthUsage::kBwNormal), 100 last_logged_state_(BandwidthUsage::kBwNormal),
172 in_sparse_update_experiment_(BweSparseUpdateExperimentIsEnabled()) { 101 in_sparse_update_experiment_(BweSparseUpdateExperimentIsEnabled()) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 result.target_bitrate_bps = rate_control_.LatestEstimate(); 166 result.target_bitrate_bps = rate_control_.LatestEstimate();
238 LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to " 167 LOG(LS_WARNING) << "Long feedback delay detected, reducing BWE to "
239 << result.target_bitrate_bps; 168 << result.target_bitrate_bps;
240 return result; 169 return result;
241 } 170 }
242 171
243 void DelayBasedBwe::IncomingPacketFeedback( 172 void DelayBasedBwe::IncomingPacketFeedback(
244 const PacketFeedback& packet_feedback) { 173 const PacketFeedback& packet_feedback) {
245 int64_t now_ms = clock_->TimeInMilliseconds(); 174 int64_t now_ms = clock_->TimeInMilliseconds();
246 175
247 receiver_incoming_bitrate_.Update(packet_feedback.arrival_time_ms, 176 receiver_incoming_bitrate_->Update(packet_feedback.arrival_time_ms,
248 packet_feedback.payload_size); 177 packet_feedback.payload_size);
249 Result result; 178 Result result;
250 // Reset if the stream has timed out. 179 // Reset if the stream has timed out.
251 if (last_seen_packet_ms_ == -1 || 180 if (last_seen_packet_ms_ == -1 ||
252 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { 181 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) {
253 inter_arrival_.reset( 182 inter_arrival_.reset(
254 new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, 183 new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000,
255 kTimestampToMs, true)); 184 kTimestampToMs, true));
256 trendline_estimator_.reset(new TrendlineEstimator( 185 trendline_estimator_.reset(new TrendlineEstimator(
257 trendline_window_size_, trendline_smoothing_coeff_, 186 trendline_window_size_, trendline_smoothing_coeff_,
258 trendline_threshold_gain_)); 187 trendline_threshold_gain_));
(...skipping 28 matching lines...) Expand all
287 PacedPacketInfo::kNotAProbe) { 216 PacedPacketInfo::kNotAProbe) {
288 probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback); 217 probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(packet_feedback);
289 } 218 }
290 } 219 }
291 220
292 DelayBasedBwe::Result DelayBasedBwe::MaybeUpdateEstimate(bool overusing) { 221 DelayBasedBwe::Result DelayBasedBwe::MaybeUpdateEstimate(bool overusing) {
293 Result result; 222 Result result;
294 int64_t now_ms = clock_->TimeInMilliseconds(); 223 int64_t now_ms = clock_->TimeInMilliseconds();
295 224
296 rtc::Optional<uint32_t> acked_bitrate_bps = 225 rtc::Optional<uint32_t> acked_bitrate_bps =
297 receiver_incoming_bitrate_.bitrate_bps(); 226 receiver_incoming_bitrate_->bitrate_bps();
298 rtc::Optional<int> probe_bitrate_bps = 227 rtc::Optional<int> probe_bitrate_bps =
299 probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrateBps(); 228 probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrateBps();
300 // Currently overusing the bandwidth. 229 // Currently overusing the bandwidth.
301 if (overusing) { 230 if (overusing) {
302 if (acked_bitrate_bps && 231 if (acked_bitrate_bps &&
303 rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) { 232 rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) {
304 result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing, 233 result.updated = UpdateEstimate(now_ms, acked_bitrate_bps, overusing,
305 &result.target_bitrate_bps); 234 &result.target_bitrate_bps);
306 } 235 }
307 } else { 236 } else {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { 296 void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) {
368 // Called from both the configuration thread and the network thread. Shouldn't 297 // Called from both the configuration thread and the network thread. Shouldn't
369 // be called from the network thread in the future. 298 // be called from the network thread in the future.
370 rate_control_.SetMinBitrate(min_bitrate_bps); 299 rate_control_.SetMinBitrate(min_bitrate_bps);
371 } 300 }
372 301
373 int64_t DelayBasedBwe::GetExpectedBwePeriodMs() const { 302 int64_t DelayBasedBwe::GetExpectedBwePeriodMs() const {
374 return rate_control_.GetExpectedBandwidthPeriodMs(); 303 return rate_control_.GetExpectedBandwidthPeriodMs();
375 } 304 }
376 } // namespace webrtc 305 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698