blob: 24ce7d42c376c6ff0c15d25af4cdb122f0ed0ef1 [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
11#include "webrtc/modules/congestion_controller/probe_controller.h"
12
isheriff8e6a7612016-09-29 05:36:59 -070013#include <algorithm>
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070014#include <initializer_list>
15
Edward Lemurc20978e2017-07-06 19:44:34 +020016#include "webrtc/rtc_base/logging.h"
17#include "webrtc/rtc_base/safe_conversions.h"
terelius3376c842017-07-31 04:23:25 -070018#include "webrtc/system_wrappers/include/field_trial.h"
Irfan Sheriff1eb12932016-10-18 17:04:25 -070019#include "webrtc/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
elad.alon61ce37e2017-03-09 07:09:31 -080072ProbeController::ProbeController(PacedSender* pacer, const Clock* clock)
philipelcb9ba302017-03-07 06:30:59 -080073 : 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,
81 int64_t max_bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070082 rtc::CritScope cs(&critsect_);
Sergey Ulanove2b15012016-11-22 16:08:30 -080083
84 if (start_bitrate_bps > 0) {
85 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:
98 if (network_state_ == kNetworkUp)
99 InitiateExponentialProbing();
100 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
sergeyu5bc39452016-12-15 10:42:09 -0800122 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
Sergey Ulanove2b15012016-11-22 16:08:30 -0800128void ProbeController::OnNetworkStateChanged(NetworkState network_state) {
129 rtc::CritScope cs(&critsect_);
130 network_state_ = network_state;
131 if (network_state_ == kNetworkUp && state_ == State::kInit)
132 InitiateExponentialProbing();
133}
134
135void ProbeController::InitiateExponentialProbing() {
136 RTC_DCHECK(network_state_ == kNetworkUp);
137 RTC_DCHECK(state_ == State::kInit);
138 RTC_DCHECK_GT(start_bitrate_bps_, 0);
139
140 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
141 // 1.2 Mbps to continue probing.
sergeyu80ed35e2016-11-28 13:11:13 -0800142 InitiateProbing(clock_->TimeInMilliseconds(),
sergeyu5bc39452016-12-15 10:42:09 -0800143 {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800144}
145
sergeyu5bc39452016-12-15 10:42:09 -0800146void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700147 rtc::CritScope cs(&critsect_);
sergeyu80ed35e2016-11-28 13:11:13 -0800148 int64_t now_ms = clock_->TimeInMilliseconds();
149
philipel2df07342017-01-16 09:31:49 -0800150 if (mid_call_probing_waiting_for_result_ &&
151 bitrate_bps >= mid_call_probing_succcess_threshold_) {
152 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
153 mid_call_probing_bitrate_bps_ / 1000);
154 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
155 bitrate_bps / 1000);
156 mid_call_probing_waiting_for_result_ = false;
157 }
158
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700159 if (state_ == State::kWaitingForProbingResult) {
sergeyu9cef11b2016-12-02 11:03:01 -0800160 // Continue probing if probing results indicate channel has greater
161 // capacity.
162 LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
163 << " Minimum to probe further: "
164 << min_bitrate_to_probe_further_bps_;
philipel2df07342017-01-16 09:31:49 -0800165
sergeyu9cef11b2016-12-02 11:03:01 -0800166 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
167 bitrate_bps > min_bitrate_to_probe_further_bps_) {
sergeyu5bc39452016-12-15 10:42:09 -0800168 // Double the probing bitrate.
169 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700170 }
sergeyu80ed35e2016-11-28 13:11:13 -0800171 }
172
terelius3376c842017-07-31 04:23:25 -0700173 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
174 time_of_last_large_drop_ms_ = now_ms;
175 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700176 }
sergeyu80ed35e2016-11-28 13:11:13 -0800177
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700178 estimated_bitrate_bps_ = bitrate_bps;
179}
180
sergeyu80ed35e2016-11-28 13:11:13 -0800181void ProbeController::EnablePeriodicAlrProbing(bool enable) {
182 rtc::CritScope cs(&critsect_);
183 enable_periodic_alr_probing_ = enable;
184}
185
terelius3376c842017-07-31 04:23:25 -0700186void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
187 rtc::CritScope cs(&critsect_);
188 alr_end_time_ms_.emplace(alr_end_time_ms);
189}
190
191void ProbeController::RequestProbe() {
192 int64_t now_ms = clock_->TimeInMilliseconds();
193 rtc::CritScope cs(&critsect_);
194 // Called once we have returned to normal state after a large drop in
195 // estimated bandwidth. The current response is to initiate a single probe
196 // session (if not already probing) at the previous bitrate.
197 //
198 // If the probe session fails, the assumption is that this drop was a
199 // real one from a competing flow or a network change.
200 bool in_alr = pacer_->GetApplicationLimitedRegionStartTime().has_value();
201 bool alr_ended_recently =
202 (alr_end_time_ms_.has_value() &&
203 now_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
204 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
205 if (state_ == State::kProbingComplete) {
206 uint32_t suggested_probe_bps =
207 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
208 uint32_t min_expected_probe_result_bps =
209 (1 - kProbeUncertainty) * suggested_probe_bps;
210 int64_t time_since_drop_ms = now_ms - time_of_last_large_drop_ms_;
211 int64_t time_since_probe_ms = now_ms - last_bwe_drop_probing_time_ms_;
212 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
213 time_since_drop_ms < kBitrateDropTimeoutMs &&
214 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
215 LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
216 // Track how often we probe in response to bandwidth drop in ALR.
217 RTC_HISTOGRAM_COUNTS_10000(
218 "WebRTC.BWE.BweDropProbingIntervalInS",
219 (now_ms - last_bwe_drop_probing_time_ms_) / 1000);
220 InitiateProbing(now_ms, {suggested_probe_bps}, false);
221 last_bwe_drop_probing_time_ms_ = now_ms;
222 }
223 }
224 }
225}
226
philipelcb9ba302017-03-07 06:30:59 -0800227void ProbeController::Reset() {
228 rtc::CritScope cs(&critsect_);
229 network_state_ = kNetworkUp;
230 state_ = State::kInit;
231 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
232 time_last_probing_initiated_ms_ = 0;
233 estimated_bitrate_bps_ = 0;
234 start_bitrate_bps_ = 0;
235 max_bitrate_bps_ = 0;
terelius3376c842017-07-31 04:23:25 -0700236 int64_t now_ms = clock_->TimeInMilliseconds();
237 last_bwe_drop_probing_time_ms_ = now_ms;
238 alr_end_time_ms_.reset();
philipelcb9ba302017-03-07 06:30:59 -0800239 mid_call_probing_waiting_for_result_ = false;
terelius3376c842017-07-31 04:23:25 -0700240 time_of_last_large_drop_ms_ = now_ms;
241 bitrate_before_last_large_drop_bps_ = 0;
philipelcb9ba302017-03-07 06:30:59 -0800242}
243
sergeyu80ed35e2016-11-28 13:11:13 -0800244void ProbeController::Process() {
245 rtc::CritScope cs(&critsect_);
246
sergeyu9cef11b2016-12-02 11:03:01 -0800247 int64_t now_ms = clock_->TimeInMilliseconds();
248
philipel2df07342017-01-16 09:31:49 -0800249 if (now_ms - time_last_probing_initiated_ms_ >
250 kMaxWaitingTimeForProbingResultMs) {
251 mid_call_probing_waiting_for_result_ = false;
252
253 if (state_ == State::kWaitingForProbingResult) {
254 LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
255 state_ = State::kProbingComplete;
256 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
257 }
sergeyu9cef11b2016-12-02 11:03:01 -0800258 }
259
sergeyu80ed35e2016-11-28 13:11:13 -0800260 if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_)
261 return;
262
263 // Probe bandwidth periodically when in ALR state.
264 rtc::Optional<int64_t> alr_start_time =
265 pacer_->GetApplicationLimitedRegionStartTime();
philipeld1d247f2017-05-04 08:35:52 -0700266 if (alr_start_time && estimated_bitrate_bps_ > 0) {
sergeyu80ed35e2016-11-28 13:11:13 -0800267 int64_t next_probe_time_ms =
268 std::max(*alr_start_time, time_last_probing_initiated_ms_) +
269 kAlrPeriodicProbingIntervalMs;
270 if (now_ms >= next_probe_time_ms) {
sergeyu5bc39452016-12-15 10:42:09 -0800271 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
sergeyu80ed35e2016-11-28 13:11:13 -0800272 }
273 }
274}
275
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700276void ProbeController::InitiateProbing(
sergeyu80ed35e2016-11-28 13:11:13 -0800277 int64_t now_ms,
sergeyu5bc39452016-12-15 10:42:09 -0800278 std::initializer_list<int64_t> bitrates_to_probe,
279 bool probe_further) {
sergeyu5bc39452016-12-15 10:42:09 -0800280 for (int64_t bitrate : bitrates_to_probe) {
philipeld1d247f2017-05-04 08:35:52 -0700281 RTC_DCHECK_GT(bitrate, 0);
sergeyu5bc39452016-12-15 10:42:09 -0800282 int64_t max_probe_bitrate_bps =
sergeyu07c147d2016-11-03 11:59:50 -0700283 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
sergeyu5bc39452016-12-15 10:42:09 -0800284 if (bitrate > max_probe_bitrate_bps) {
285 bitrate = max_probe_bitrate_bps;
286 probe_further = false;
287 }
kwibergd3edd772017-03-01 18:52:48 -0800288 pacer_->CreateProbeCluster(rtc::dchecked_cast<int>(bitrate));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700289 }
sergeyu80ed35e2016-11-28 13:11:13 -0800290 time_last_probing_initiated_ms_ = now_ms;
sergeyu5bc39452016-12-15 10:42:09 -0800291 if (probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700292 state_ = State::kWaitingForProbingResult;
sergeyu5bc39452016-12-15 10:42:09 -0800293 min_bitrate_to_probe_further_bps_ =
294 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
295 } else {
296 state_ = State::kProbingComplete;
297 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
298 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700299}
300
301} // namespace webrtc