OLD | NEW |
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 |
11 #include "webrtc/modules/congestion_controller/delay_based_bwe.h" | 11 #include "webrtc/modules/congestion_controller/delay_based_bwe.h" |
12 | 12 |
13 #include <math.h> | |
14 | |
15 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cmath> |
16 | 15 |
17 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/base/constructormagic.h" |
19 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/thread_annotations.h" | 19 #include "webrtc/base/thread_annotations.h" |
21 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 20 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
22 #include "webrtc/modules/pacing/paced_sender.h" | 21 #include "webrtc/modules/pacing/paced_sender.h" |
23 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimat
or.h" | 22 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimat
or.h" |
| 23 #include "webrtc/system_wrappers/include/field_trial.h" |
24 #include "webrtc/system_wrappers/include/metrics.h" | 24 #include "webrtc/system_wrappers/include/metrics.h" |
25 #include "webrtc/typedefs.h" | 25 #include "webrtc/typedefs.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 constexpr int kTimestampGroupLengthMs = 5; | 28 constexpr int kTimestampGroupLengthMs = 5; |
29 constexpr int kAbsSendTimeFraction = 18; | 29 constexpr int kAbsSendTimeFraction = 18; |
30 constexpr int kAbsSendTimeInterArrivalUpshift = 8; | 30 constexpr int kAbsSendTimeInterArrivalUpshift = 8; |
31 constexpr int kInterArrivalShift = | 31 constexpr int kInterArrivalShift = |
32 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; | 32 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; |
33 constexpr double kTimestampToMs = | 33 constexpr double kTimestampToMs = |
34 1000.0 / static_cast<double>(1 << kInterArrivalShift); | 34 1000.0 / static_cast<double>(1 << kInterArrivalShift); |
35 // This ssrc is used to fulfill the current API but will be removed | 35 // This ssrc is used to fulfill the current API but will be removed |
36 // after the API has been changed. | 36 // after the API has been changed. |
37 constexpr uint32_t kFixedSsrc = 0; | 37 constexpr uint32_t kFixedSsrc = 0; |
| 38 constexpr int kInitialRateWindowMs = 500; |
| 39 constexpr int kRateWindowMs = 150; |
| 40 |
| 41 const char kBitrateEstimateExperiment[] = "WebRTC-ImprovedBitrateEstimate"; |
| 42 |
| 43 bool BitrateEstimateExperimentIsEnabled() { |
| 44 return webrtc::field_trial::FindFullName(kBitrateEstimateExperiment) == |
| 45 "Enabled"; |
| 46 } |
38 } // namespace | 47 } // namespace |
39 | 48 |
40 namespace webrtc { | 49 namespace webrtc { |
| 50 DelayBasedBwe::BitrateEstimator::BitrateEstimator() |
| 51 : sum_(0), |
| 52 current_win_ms_(0), |
| 53 prev_time_ms_(-1), |
| 54 bitrate_estimate_(-1.0f), |
| 55 bitrate_estimate_var_(50.0f), |
| 56 old_estimator_(kBitrateWindowMs, 8000), |
| 57 in_experiment_(BitrateEstimateExperimentIsEnabled()) {} |
| 58 |
| 59 void DelayBasedBwe::BitrateEstimator::Update(int64_t now_ms, int bytes) { |
| 60 if (!in_experiment_) { |
| 61 old_estimator_.Update(bytes, now_ms); |
| 62 rtc::Optional<uint32_t> rate = old_estimator_.Rate(now_ms); |
| 63 bitrate_estimate_ = -1.0f; |
| 64 if (rate) |
| 65 bitrate_estimate_ = *rate / 1000.0f; |
| 66 return; |
| 67 } |
| 68 int rate_window_ms = kRateWindowMs; |
| 69 // We use a larger window at the beginning to get a more stable sample that |
| 70 // we can use to initialize the estimate. |
| 71 if (bitrate_estimate_ < 0.f) |
| 72 rate_window_ms = kInitialRateWindowMs; |
| 73 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); |
| 74 if (bitrate_sample < 0.0f) |
| 75 return; |
| 76 if (bitrate_estimate_ < 0.0f) { |
| 77 // This is the very first sample we get. Use it to initialize the estimate. |
| 78 bitrate_estimate_ = bitrate_sample; |
| 79 return; |
| 80 } |
| 81 // Define the sample uncertainty as a function of how far away it is from the |
| 82 // current estimate. |
| 83 float sample_uncertainty = |
| 84 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_; |
| 85 float sample_var = sample_uncertainty * sample_uncertainty; |
| 86 // Update a bayesian estimate of the rate, weighting it lower if the sample |
| 87 // uncertainty is large. |
| 88 // The bitrate estimate uncertainty is increased with each update to model |
| 89 // that the bitrate changes over time. |
| 90 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; |
| 91 bitrate_estimate_ = (sample_var * bitrate_estimate_ + |
| 92 pred_bitrate_estimate_var * bitrate_sample) / |
| 93 (sample_var + pred_bitrate_estimate_var); |
| 94 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / |
| 95 (sample_var + pred_bitrate_estimate_var); |
| 96 } |
| 97 |
| 98 float DelayBasedBwe::BitrateEstimator::UpdateWindow(int64_t now_ms, |
| 99 int bytes, |
| 100 int rate_window_ms) { |
| 101 // Reset if time moves backwards. |
| 102 if (now_ms < prev_time_ms_) { |
| 103 prev_time_ms_ = -1; |
| 104 sum_ = 0; |
| 105 current_win_ms_ = 0; |
| 106 } |
| 107 if (prev_time_ms_ >= 0) { |
| 108 current_win_ms_ += now_ms - prev_time_ms_; |
| 109 // Reset if nothing has been received for more than a full window. |
| 110 if (now_ms - prev_time_ms_ > rate_window_ms) { |
| 111 sum_ = 0; |
| 112 current_win_ms_ %= rate_window_ms; |
| 113 } |
| 114 } |
| 115 prev_time_ms_ = now_ms; |
| 116 float bitrate_sample = -1.0f; |
| 117 if (current_win_ms_ >= rate_window_ms) { |
| 118 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); |
| 119 current_win_ms_ -= rate_window_ms; |
| 120 sum_ = 0; |
| 121 } |
| 122 sum_ += bytes; |
| 123 return bitrate_sample; |
| 124 } |
| 125 |
| 126 rtc::Optional<uint32_t> DelayBasedBwe::BitrateEstimator::bitrate_bps() const { |
| 127 if (bitrate_estimate_ < 0.f) |
| 128 return rtc::Optional<uint32_t>(); |
| 129 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); |
| 130 } |
41 | 131 |
42 DelayBasedBwe::DelayBasedBwe(Clock* clock) | 132 DelayBasedBwe::DelayBasedBwe(Clock* clock) |
43 : clock_(clock), | 133 : clock_(clock), |
44 inter_arrival_(), | 134 inter_arrival_(), |
45 estimator_(), | 135 estimator_(), |
46 detector_(OverUseDetectorOptions()), | 136 detector_(OverUseDetectorOptions()), |
47 receiver_incoming_bitrate_(kBitrateWindowMs, 8000), | 137 receiver_incoming_bitrate_(), |
48 last_update_ms_(-1), | 138 last_update_ms_(-1), |
49 last_seen_packet_ms_(-1), | 139 last_seen_packet_ms_(-1), |
50 uma_recorded_(false) { | 140 uma_recorded_(false) { |
51 network_thread_.DetachFromThread(); | 141 network_thread_.DetachFromThread(); |
52 } | 142 } |
53 | 143 |
54 DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( | 144 DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( |
55 const std::vector<PacketInfo>& packet_feedback_vector) { | 145 const std::vector<PacketInfo>& packet_feedback_vector) { |
56 RTC_DCHECK(network_thread_.CalledOnValidThread()); | 146 RTC_DCHECK(network_thread_.CalledOnValidThread()); |
57 if (!uma_recorded_) { | 147 if (!uma_recorded_) { |
58 RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, | 148 RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
59 BweNames::kSendSideTransportSeqNum, | 149 BweNames::kSendSideTransportSeqNum, |
60 BweNames::kBweNamesMax); | 150 BweNames::kBweNamesMax); |
61 uma_recorded_ = true; | 151 uma_recorded_ = true; |
62 } | 152 } |
63 Result aggregated_result; | 153 Result aggregated_result; |
64 for (const auto& packet_info : packet_feedback_vector) { | 154 for (const auto& packet_info : packet_feedback_vector) { |
65 Result result = IncomingPacketInfo(packet_info); | 155 Result result = IncomingPacketInfo(packet_info); |
66 if (result.updated) | 156 if (result.updated) |
67 aggregated_result = result; | 157 aggregated_result = result; |
68 } | 158 } |
69 return aggregated_result; | 159 return aggregated_result; |
70 } | 160 } |
71 | 161 |
72 DelayBasedBwe::Result DelayBasedBwe::IncomingPacketInfo( | 162 DelayBasedBwe::Result DelayBasedBwe::IncomingPacketInfo( |
73 const PacketInfo& info) { | 163 const PacketInfo& info) { |
74 int64_t now_ms = clock_->TimeInMilliseconds(); | 164 int64_t now_ms = clock_->TimeInMilliseconds(); |
75 | 165 |
76 receiver_incoming_bitrate_.Update(info.payload_size, info.arrival_time_ms); | 166 receiver_incoming_bitrate_.Update(info.arrival_time_ms, info.payload_size); |
77 Result result; | 167 Result result; |
78 // Reset if the stream has timed out. | 168 // Reset if the stream has timed out. |
79 if (last_seen_packet_ms_ == -1 || | 169 if (last_seen_packet_ms_ == -1 || |
80 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { | 170 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { |
81 inter_arrival_.reset( | 171 inter_arrival_.reset( |
82 new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, | 172 new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
83 kTimestampToMs, true)); | 173 kTimestampToMs, true)); |
84 estimator_.reset(new OveruseEstimator(OverUseDetectorOptions())); | 174 estimator_.reset(new OveruseEstimator(OverUseDetectorOptions())); |
85 } | 175 } |
86 last_seen_packet_ms_ = now_ms; | 176 last_seen_packet_ms_ = now_ms; |
(...skipping 18 matching lines...) Expand all Loading... |
105 estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State(), | 195 estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State(), |
106 info.arrival_time_ms); | 196 info.arrival_time_ms); |
107 detector_.Detect(estimator_->offset(), ts_delta_ms, | 197 detector_.Detect(estimator_->offset(), ts_delta_ms, |
108 estimator_->num_of_deltas(), info.arrival_time_ms); | 198 estimator_->num_of_deltas(), info.arrival_time_ms); |
109 } | 199 } |
110 | 200 |
111 int probing_bps = 0; | 201 int probing_bps = 0; |
112 if (info.probe_cluster_id != PacketInfo::kNotAProbe) { | 202 if (info.probe_cluster_id != PacketInfo::kNotAProbe) { |
113 probing_bps = probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(info); | 203 probing_bps = probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(info); |
114 } | 204 } |
115 | 205 rtc::Optional<uint32_t> acked_bitrate_bps = |
| 206 receiver_incoming_bitrate_.bitrate_bps(); |
116 // Currently overusing the bandwidth. | 207 // Currently overusing the bandwidth. |
117 if (detector_.State() == kBwOverusing) { | 208 if (detector_.State() == kBwOverusing) { |
118 rtc::Optional<uint32_t> incoming_rate = | 209 if (acked_bitrate_bps && |
119 receiver_incoming_bitrate_.Rate(info.arrival_time_ms); | 210 rate_control_.TimeToReduceFurther(now_ms, *acked_bitrate_bps)) { |
120 if (incoming_rate && | 211 result.updated = |
121 rate_control_.TimeToReduceFurther(now_ms, *incoming_rate)) { | 212 UpdateEstimate(info.arrival_time_ms, now_ms, acked_bitrate_bps, |
122 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, | 213 &result.target_bitrate_bps); |
123 &result.target_bitrate_bps); | |
124 } | 214 } |
125 } else if (probing_bps > 0) { | 215 } else if (probing_bps > 0) { |
126 // No overuse, but probing measured a bitrate. | 216 // No overuse, but probing measured a bitrate. |
127 rate_control_.SetEstimate(probing_bps, info.arrival_time_ms); | 217 rate_control_.SetEstimate(probing_bps, info.arrival_time_ms); |
128 result.probe = true; | 218 result.probe = true; |
129 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, | 219 result.updated = |
130 &result.target_bitrate_bps); | 220 UpdateEstimate(info.arrival_time_ms, now_ms, acked_bitrate_bps, |
| 221 &result.target_bitrate_bps); |
131 } | 222 } |
132 if (!result.updated && | 223 if (!result.updated && |
133 (last_update_ms_ == -1 || | 224 (last_update_ms_ == -1 || |
134 now_ms - last_update_ms_ > rate_control_.GetFeedbackInterval())) { | 225 now_ms - last_update_ms_ > rate_control_.GetFeedbackInterval())) { |
135 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, | 226 result.updated = |
136 &result.target_bitrate_bps); | 227 UpdateEstimate(info.arrival_time_ms, now_ms, acked_bitrate_bps, |
| 228 &result.target_bitrate_bps); |
137 } | 229 } |
138 if (result.updated) | 230 if (result.updated) |
139 last_update_ms_ = now_ms; | 231 last_update_ms_ = now_ms; |
140 | 232 |
141 return result; | 233 return result; |
142 } | 234 } |
143 | 235 |
144 bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms, | 236 bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms, |
145 int64_t now_ms, | 237 int64_t now_ms, |
| 238 rtc::Optional<uint32_t> acked_bitrate_bps, |
146 uint32_t* target_bitrate_bps) { | 239 uint32_t* target_bitrate_bps) { |
147 // The first overuse should immediately trigger a new estimate. | 240 const RateControlInput input(detector_.State(), acked_bitrate_bps, |
148 // We also have to update the estimate immediately if we are overusing | |
149 // and the target bitrate is too high compared to what we are receiving. | |
150 const RateControlInput input(detector_.State(), | |
151 receiver_incoming_bitrate_.Rate(arrival_time_ms), | |
152 estimator_->var_noise()); | 241 estimator_->var_noise()); |
153 rate_control_.Update(&input, now_ms); | 242 rate_control_.Update(&input, now_ms); |
154 *target_bitrate_bps = rate_control_.UpdateBandwidthEstimate(now_ms); | 243 *target_bitrate_bps = rate_control_.UpdateBandwidthEstimate(now_ms); |
155 return rate_control_.ValidEstimate(); | 244 return rate_control_.ValidEstimate(); |
156 } | 245 } |
157 | 246 |
158 void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { | 247 void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
159 rate_control_.SetRtt(avg_rtt_ms); | 248 rate_control_.SetRtt(avg_rtt_ms); |
160 } | 249 } |
161 | 250 |
(...skipping 12 matching lines...) Expand all Loading... |
174 *bitrate_bps = rate_control_.LatestEstimate(); | 263 *bitrate_bps = rate_control_.LatestEstimate(); |
175 return true; | 264 return true; |
176 } | 265 } |
177 | 266 |
178 void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { | 267 void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
179 // Called from both the configuration thread and the network thread. Shouldn't | 268 // Called from both the configuration thread and the network thread. Shouldn't |
180 // be called from the network thread in the future. | 269 // be called from the network thread in the future. |
181 rate_control_.SetMinBitrate(min_bitrate_bps); | 270 rate_control_.SetMinBitrate(min_bitrate_bps); |
182 } | 271 } |
183 } // namespace webrtc | 272 } // namespace webrtc |
OLD | NEW |