blob: ea1f45ca5be06b8ba72e95cb03addcaee711f47d [file] [log] [blame]
Irfan Sheriffb2540bb2016-09-12 12:28:54 -07001/*
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
Danil Chapovalovbda50682018-02-14 09:08:28 +000011#include "modules/congestion_controller/probe_controller.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070012
isheriff8e6a7612016-09-29 05:36:59 -070013#include <algorithm>
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070014#include <initializer_list>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010017#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "system_wrappers/include/field_trial.h"
19#include "system_wrappers/include/metrics.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070020
21namespace webrtc {
22
23namespace {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070024// Maximum waiting time from the time of initiating probing to getting
25// the measured results back.
26constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
27
28// Value of |min_bitrate_to_probe_further_bps_| that indicates
29// further probing is disabled.
30constexpr int kExponentialProbingDisabled = 0;
31
sergeyu07c147d2016-11-03 11:59:50 -070032// Default probing bitrate limit. Applied only when the application didn't
33// specify max bitrate.
sergeyu5bc39452016-12-15 10:42:09 -080034constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
isheriff8e6a7612016-09-29 05:36:59 -070035
sergeyu80ed35e2016-11-28 13:11:13 -080036// Interval between probes when ALR periodic probing is enabled.
37constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
38
sergeyu5bc39452016-12-15 10:42:09 -080039// Minimum probe bitrate percentage to probe further for repeated probes,
40// relative to the previous probe. For example, if 1Mbps probe results in
41// 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be
42// sent if we get 600kbps from the first one.
43constexpr int kRepeatedProbeMinPercentage = 70;
Irfan Sheriff1eb12932016-10-18 17:04:25 -070044
terelius3376c842017-07-31 04:23:25 -070045// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
46// and we recover within |kBitrateDropTimeoutMs|, then we'll send
47// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
48constexpr double kBitrateDropThreshold = 0.66;
49constexpr int kBitrateDropTimeoutMs = 5000;
50constexpr double kProbeFractionAfterDrop = 0.85;
51
52// Timeout for probing after leaving ALR. If the bitrate drops significantly,
53// (as determined by the delay based estimator) and we leave ALR, then we will
54// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
55constexpr int kAlrEndedTimeoutMs = 3000;
56
57// The expected uncertainty of probe result (as a fraction of the target probe
58// This is a limit on how often probing can be done when there is a BW
59// drop detected in ALR.
60constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
61
62// bitrate). Used to avoid probing if the probe bitrate is close to our current
63// estimate.
64constexpr double kProbeUncertainty = 0.05;
65
66// Use probing to recover faster after large bitrate estimate drops.
67constexpr char kBweRapidRecoveryExperiment[] =
68 "WebRTC-BweRapidRecoveryExperiment";
69
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070070} // namespace
71
Sebastian Janssonea86bb72018-02-14 16:53:38 +000072ProbeController::ProbeController(PacedSender* pacer, const Clock* clock)
73 : pacer_(pacer), clock_(clock), enable_periodic_alr_probing_(false) {
74 Reset();
terelius3376c842017-07-31 04:23:25 -070075 in_rapid_recovery_experiment_ = webrtc::field_trial::FindFullName(
76 kBweRapidRecoveryExperiment) == "Enabled";
philipelcb9ba302017-03-07 06:30:59 -080077}
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070078
sergeyu5bc39452016-12-15 10:42:09 -080079void ProbeController::SetBitrates(int64_t min_bitrate_bps,
80 int64_t start_bitrate_bps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000081 int64_t max_bitrate_bps) {
82 rtc::CritScope cs(&critsect_);
83
84 if (start_bitrate_bps > 0) {
Sergey Ulanove2b15012016-11-22 16:08:30 -080085 start_bitrate_bps_ = start_bitrate_bps;
philipeld1d247f2017-05-04 08:35:52 -070086 estimated_bitrate_bps_ = start_bitrate_bps;
Sergey Ulanove2b15012016-11-22 16:08:30 -080087 } else if (start_bitrate_bps_ == 0) {
88 start_bitrate_bps_ = min_bitrate_bps;
honghaiz906c5dc2016-11-15 14:39:06 -080089 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070090
philipelda5e9d02017-01-17 02:08:28 -080091 // The reason we use the variable |old_max_bitrate_pbs| is because we
92 // need to set |max_bitrate_bps_| before we call InitiateProbing.
sergeyu5bc39452016-12-15 10:42:09 -080093 int64_t old_max_bitrate_bps = max_bitrate_bps_;
sergeyu07c147d2016-11-03 11:59:50 -070094 max_bitrate_bps_ = max_bitrate_bps;
95
Sergey Ulanove2b15012016-11-22 16:08:30 -080096 switch (state_) {
97 case State::kInit:
Sebastian Janssonea86bb72018-02-14 16:53:38 +000098 if (network_state_ == kNetworkUp)
99 InitiateExponentialProbing();
Sergey Ulanove2b15012016-11-22 16:08:30 -0800100 break;
101
102 case State::kWaitingForProbingResult:
103 break;
104
105 case State::kProbingComplete:
philipelda5e9d02017-01-17 02:08:28 -0800106 // If the new max bitrate is higher than the old max bitrate and the
107 // estimate is lower than the new max bitrate then initiate probing.
108 if (estimated_bitrate_bps_ != 0 &&
109 old_max_bitrate_bps < max_bitrate_bps_ &&
110 estimated_bitrate_bps_ < max_bitrate_bps_) {
philipel2df07342017-01-16 09:31:49 -0800111 // The assumption is that if we jump more than 20% in the bandwidth
112 // estimate or if the bandwidth estimate is within 90% of the new
113 // max bitrate then the probing attempt was successful.
114 mid_call_probing_succcess_threshold_ =
115 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
116 mid_call_probing_waiting_for_result_ = true;
117 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
118
119 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
120 max_bitrate_bps_ / 1000);
121
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000122 InitiateProbing(clock_->TimeInMilliseconds(), {max_bitrate_bps}, false);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800123 }
124 break;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700125 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700126}
127
philipeldb4fa4b2018-03-06 18:29:22 +0100128void ProbeController::OnMaxTotalAllocatedBitrate(
129 int64_t max_total_allocated_bitrate) {
130 rtc::CritScope cs(&critsect_);
131 // TODO(philipel): Should |max_total_allocated_bitrate| be used as a limit for
132 // ALR probing?
philipel9fd6b982018-04-19 16:58:07 +0200133 if (state_ == State::kProbingComplete &&
134 max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
philipel0676f222018-04-17 16:12:21 +0200135 estimated_bitrate_bps_ != 0 &&
136 (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
philipeldb4fa4b2018-03-06 18:29:22 +0100137 estimated_bitrate_bps_ < max_total_allocated_bitrate) {
philipel0676f222018-04-17 16:12:21 +0200138 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
philipeldb4fa4b2018-03-06 18:29:22 +0100139 InitiateProbing(clock_->TimeInMilliseconds(), {max_total_allocated_bitrate},
140 false);
141 }
142}
143
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000144void ProbeController::OnNetworkStateChanged(NetworkState network_state) {
145 rtc::CritScope cs(&critsect_);
146 network_state_ = network_state;
philipel9fd6b982018-04-19 16:58:07 +0200147
148 if (network_state_ == kNetworkDown &&
149 state_ == State::kWaitingForProbingResult) {
150 state_ = State::kProbingComplete;
151 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
152 }
153
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000154 if (network_state_ == kNetworkUp && state_ == State::kInit)
155 InitiateExponentialProbing();
Sergey Ulanove2b15012016-11-22 16:08:30 -0800156}
157
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000158void ProbeController::InitiateExponentialProbing() {
159 RTC_DCHECK(network_state_ == kNetworkUp);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800160 RTC_DCHECK(state_ == State::kInit);
161 RTC_DCHECK_GT(start_bitrate_bps_, 0);
162
163 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
164 // 1.2 Mbps to continue probing.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000165 InitiateProbing(clock_->TimeInMilliseconds(),
166 {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800167}
168
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000169void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps) {
170 rtc::CritScope cs(&critsect_);
171 int64_t now_ms = clock_->TimeInMilliseconds();
sergeyu80ed35e2016-11-28 13:11:13 -0800172
philipel2df07342017-01-16 09:31:49 -0800173 if (mid_call_probing_waiting_for_result_ &&
174 bitrate_bps >= mid_call_probing_succcess_threshold_) {
175 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
176 mid_call_probing_bitrate_bps_ / 1000);
177 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
178 bitrate_bps / 1000);
179 mid_call_probing_waiting_for_result_ = false;
180 }
181
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700182 if (state_ == State::kWaitingForProbingResult) {
sergeyu9cef11b2016-12-02 11:03:01 -0800183 // Continue probing if probing results indicate channel has greater
184 // capacity.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100185 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
186 << " Minimum to probe further: "
187 << min_bitrate_to_probe_further_bps_;
philipel2df07342017-01-16 09:31:49 -0800188
sergeyu9cef11b2016-12-02 11:03:01 -0800189 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
190 bitrate_bps > min_bitrate_to_probe_further_bps_) {
sergeyu5bc39452016-12-15 10:42:09 -0800191 // Double the probing bitrate.
192 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700193 }
sergeyu80ed35e2016-11-28 13:11:13 -0800194 }
195
terelius3376c842017-07-31 04:23:25 -0700196 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
197 time_of_last_large_drop_ms_ = now_ms;
198 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700199 }
sergeyu80ed35e2016-11-28 13:11:13 -0800200
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700201 estimated_bitrate_bps_ = bitrate_bps;
202}
203
sergeyu80ed35e2016-11-28 13:11:13 -0800204void ProbeController::EnablePeriodicAlrProbing(bool enable) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000205 rtc::CritScope cs(&critsect_);
sergeyu80ed35e2016-11-28 13:11:13 -0800206 enable_periodic_alr_probing_ = enable;
207}
208
terelius3376c842017-07-31 04:23:25 -0700209void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000210 rtc::CritScope cs(&critsect_);
terelius3376c842017-07-31 04:23:25 -0700211 alr_end_time_ms_.emplace(alr_end_time_ms);
212}
213
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000214void ProbeController::RequestProbe() {
215 int64_t now_ms = clock_->TimeInMilliseconds();
216 rtc::CritScope cs(&critsect_);
terelius3376c842017-07-31 04:23:25 -0700217 // Called once we have returned to normal state after a large drop in
218 // estimated bandwidth. The current response is to initiate a single probe
219 // session (if not already probing) at the previous bitrate.
220 //
221 // If the probe session fails, the assumption is that this drop was a
222 // real one from a competing flow or a network change.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000223 bool in_alr = pacer_->GetApplicationLimitedRegionStartTime().has_value();
terelius3376c842017-07-31 04:23:25 -0700224 bool alr_ended_recently =
225 (alr_end_time_ms_.has_value() &&
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000226 now_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
terelius3376c842017-07-31 04:23:25 -0700227 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
228 if (state_ == State::kProbingComplete) {
229 uint32_t suggested_probe_bps =
230 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
231 uint32_t min_expected_probe_result_bps =
232 (1 - kProbeUncertainty) * suggested_probe_bps;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000233 int64_t time_since_drop_ms = now_ms - time_of_last_large_drop_ms_;
234 int64_t time_since_probe_ms = now_ms - last_bwe_drop_probing_time_ms_;
terelius3376c842017-07-31 04:23:25 -0700235 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
236 time_since_drop_ms < kBitrateDropTimeoutMs &&
237 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
terelius3376c842017-07-31 04:23:25 -0700239 // Track how often we probe in response to bandwidth drop in ALR.
240 RTC_HISTOGRAM_COUNTS_10000(
241 "WebRTC.BWE.BweDropProbingIntervalInS",
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000242 (now_ms - last_bwe_drop_probing_time_ms_) / 1000);
243 InitiateProbing(now_ms, {suggested_probe_bps}, false);
244 last_bwe_drop_probing_time_ms_ = now_ms;
terelius3376c842017-07-31 04:23:25 -0700245 }
246 }
247 }
248}
249
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000250void ProbeController::Reset() {
251 rtc::CritScope cs(&critsect_);
252 network_state_ = kNetworkUp;
philipelcb9ba302017-03-07 06:30:59 -0800253 state_ = State::kInit;
254 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
255 time_last_probing_initiated_ms_ = 0;
256 estimated_bitrate_bps_ = 0;
257 start_bitrate_bps_ = 0;
258 max_bitrate_bps_ = 0;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000259 int64_t now_ms = clock_->TimeInMilliseconds();
terelius3376c842017-07-31 04:23:25 -0700260 last_bwe_drop_probing_time_ms_ = now_ms;
261 alr_end_time_ms_.reset();
philipelcb9ba302017-03-07 06:30:59 -0800262 mid_call_probing_waiting_for_result_ = false;
terelius3376c842017-07-31 04:23:25 -0700263 time_of_last_large_drop_ms_ = now_ms;
264 bitrate_before_last_large_drop_bps_ = 0;
philipel0676f222018-04-17 16:12:21 +0200265 max_total_allocated_bitrate_ = 0;
philipelcb9ba302017-03-07 06:30:59 -0800266}
267
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000268void ProbeController::Process() {
269 rtc::CritScope cs(&critsect_);
270
271 int64_t now_ms = clock_->TimeInMilliseconds();
sergeyu9cef11b2016-12-02 11:03:01 -0800272
philipel2df07342017-01-16 09:31:49 -0800273 if (now_ms - time_last_probing_initiated_ms_ >
274 kMaxWaitingTimeForProbingResultMs) {
275 mid_call_probing_waiting_for_result_ = false;
276
277 if (state_ == State::kWaitingForProbingResult) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100278 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
philipel2df07342017-01-16 09:31:49 -0800279 state_ = State::kProbingComplete;
280 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
281 }
sergeyu9cef11b2016-12-02 11:03:01 -0800282 }
283
sergeyu80ed35e2016-11-28 13:11:13 -0800284 if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_)
285 return;
286
287 // Probe bandwidth periodically when in ALR state.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000288 rtc::Optional<int64_t> alr_start_time =
289 pacer_->GetApplicationLimitedRegionStartTime();
290 if (alr_start_time && estimated_bitrate_bps_ > 0) {
sergeyu80ed35e2016-11-28 13:11:13 -0800291 int64_t next_probe_time_ms =
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000292 std::max(*alr_start_time, time_last_probing_initiated_ms_) +
sergeyu80ed35e2016-11-28 13:11:13 -0800293 kAlrPeriodicProbingIntervalMs;
294 if (now_ms >= next_probe_time_ms) {
sergeyu5bc39452016-12-15 10:42:09 -0800295 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
sergeyu80ed35e2016-11-28 13:11:13 -0800296 }
297 }
298}
299
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700300void ProbeController::InitiateProbing(
sergeyu80ed35e2016-11-28 13:11:13 -0800301 int64_t now_ms,
sergeyu5bc39452016-12-15 10:42:09 -0800302 std::initializer_list<int64_t> bitrates_to_probe,
303 bool probe_further) {
sergeyu5bc39452016-12-15 10:42:09 -0800304 for (int64_t bitrate : bitrates_to_probe) {
philipeld1d247f2017-05-04 08:35:52 -0700305 RTC_DCHECK_GT(bitrate, 0);
sergeyu5bc39452016-12-15 10:42:09 -0800306 int64_t max_probe_bitrate_bps =
sergeyu07c147d2016-11-03 11:59:50 -0700307 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
sergeyu5bc39452016-12-15 10:42:09 -0800308 if (bitrate > max_probe_bitrate_bps) {
309 bitrate = max_probe_bitrate_bps;
310 probe_further = false;
311 }
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000312 pacer_->CreateProbeCluster(rtc::dchecked_cast<int>(bitrate));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700313 }
sergeyu80ed35e2016-11-28 13:11:13 -0800314 time_last_probing_initiated_ms_ = now_ms;
sergeyu5bc39452016-12-15 10:42:09 -0800315 if (probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700316 state_ = State::kWaitingForProbingResult;
sergeyu5bc39452016-12-15 10:42:09 -0800317 min_bitrate_to_probe_further_bps_ =
318 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
319 } else {
320 state_ = State::kProbingComplete;
321 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
322 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700323}
324
325} // namespace webrtc