blob: c2a8e023d7d40eb78ee52c9243b51f607ce4b904 [file] [log] [blame]
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +01001/*
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
Sebastian Janssonfc7ec8e2018-02-28 16:48:00 +010011#include "modules/congestion_controller/goog_cc/probe_controller.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010012
13#include <algorithm>
14#include <initializer_list>
15
16#include "rtc_base/logging.h"
17#include "rtc_base/numerics/safe_conversions.h"
18#include "system_wrappers/include/field_trial.h"
19#include "system_wrappers/include/metrics.h"
20
21namespace webrtc {
22
23namespace {
24// The minimum number probing packets used.
25constexpr int kMinProbePacketsSent = 5;
26
27// The minimum probing duration in ms.
28constexpr int kMinProbeDurationMs = 15;
29
30// Maximum waiting time from the time of initiating probing to getting
31// the measured results back.
32constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
33
34// Value of |min_bitrate_to_probe_further_bps_| that indicates
35// further probing is disabled.
36constexpr int kExponentialProbingDisabled = 0;
37
38// Default probing bitrate limit. Applied only when the application didn't
39// specify max bitrate.
40constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
41
42// Interval between probes when ALR periodic probing is enabled.
43constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
44
45// Minimum probe bitrate percentage to probe further for repeated probes,
46// relative to the previous probe. For example, if 1Mbps probe results in
47// 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be
48// sent if we get 600kbps from the first one.
49constexpr int kRepeatedProbeMinPercentage = 70;
50
51// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
52// and we recover within |kBitrateDropTimeoutMs|, then we'll send
53// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
54constexpr double kBitrateDropThreshold = 0.66;
55constexpr int kBitrateDropTimeoutMs = 5000;
56constexpr double kProbeFractionAfterDrop = 0.85;
57
58// Timeout for probing after leaving ALR. If the bitrate drops significantly,
59// (as determined by the delay based estimator) and we leave ALR, then we will
60// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
61constexpr int kAlrEndedTimeoutMs = 3000;
62
63// The expected uncertainty of probe result (as a fraction of the target probe
64// This is a limit on how often probing can be done when there is a BW
65// drop detected in ALR.
66constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
67
68// bitrate). Used to avoid probing if the probe bitrate is close to our current
69// estimate.
70constexpr double kProbeUncertainty = 0.05;
71
72// Use probing to recover faster after large bitrate estimate drops.
73constexpr char kBweRapidRecoveryExperiment[] =
74 "WebRTC-BweRapidRecoveryExperiment";
75
76} // namespace
77
Sebastian Janssonf2e3e7a2018-04-06 17:16:06 +020078ProbeController::ProbeController() : enable_periodic_alr_probing_(false) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010079 Reset(0);
80 in_rapid_recovery_experiment_ = webrtc::field_trial::FindFullName(
81 kBweRapidRecoveryExperiment) == "Enabled";
82}
83
84ProbeController::~ProbeController() {}
85
Sebastian Janssonda2ec402018-08-02 16:27:28 +020086std::vector<ProbeClusterConfig> ProbeController::SetBitrates(
87 int64_t min_bitrate_bps,
88 int64_t start_bitrate_bps,
89 int64_t max_bitrate_bps,
90 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010091 if (start_bitrate_bps > 0) {
92 start_bitrate_bps_ = start_bitrate_bps;
93 estimated_bitrate_bps_ = start_bitrate_bps;
94 } else if (start_bitrate_bps_ == 0) {
95 start_bitrate_bps_ = min_bitrate_bps;
96 }
97
98 // The reason we use the variable |old_max_bitrate_pbs| is because we
99 // need to set |max_bitrate_bps_| before we call InitiateProbing.
100 int64_t old_max_bitrate_bps = max_bitrate_bps_;
101 max_bitrate_bps_ = max_bitrate_bps;
102
103 switch (state_) {
104 case State::kInit:
105 if (network_available_)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200106 return InitiateExponentialProbing(at_time_ms);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100107 break;
108
109 case State::kWaitingForProbingResult:
110 break;
111
112 case State::kProbingComplete:
113 // If the new max bitrate is higher than the old max bitrate and the
114 // estimate is lower than the new max bitrate then initiate probing.
115 if (estimated_bitrate_bps_ != 0 &&
116 old_max_bitrate_bps < max_bitrate_bps_ &&
117 estimated_bitrate_bps_ < max_bitrate_bps_) {
118 // The assumption is that if we jump more than 20% in the bandwidth
119 // estimate or if the bandwidth estimate is within 90% of the new
120 // max bitrate then the probing attempt was successful.
121 mid_call_probing_succcess_threshold_ =
122 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
123 mid_call_probing_waiting_for_result_ = true;
124 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
125
126 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
127 max_bitrate_bps_ / 1000);
128
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200129 return InitiateProbing(at_time_ms, {max_bitrate_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100130 }
131 break;
132 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200133 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100134}
135
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200136std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
philipeldb4fa4b2018-03-06 18:29:22 +0100137 int64_t max_total_allocated_bitrate,
138 int64_t at_time_ms) {
139 // TODO(philipel): Should |max_total_allocated_bitrate| be used as a limit for
140 // ALR probing?
philipel9fd6b982018-04-19 16:58:07 +0200141 if (state_ == State::kProbingComplete &&
142 max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
philipel0676f222018-04-17 16:12:21 +0200143 estimated_bitrate_bps_ != 0 &&
144 (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
philipeldb4fa4b2018-03-06 18:29:22 +0100145 estimated_bitrate_bps_ < max_total_allocated_bitrate) {
Sebastian Janssonb2ecc3d2018-07-13 17:22:01 +0200146 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200147 return InitiateProbing(at_time_ms, {max_total_allocated_bitrate}, false);
philipeldb4fa4b2018-03-06 18:29:22 +0100148 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200149 return std::vector<ProbeClusterConfig>();
philipeldb4fa4b2018-03-06 18:29:22 +0100150}
151
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200152std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
153 NetworkAvailability msg) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100154 network_available_ = msg.network_available;
philipel9fd6b982018-04-19 16:58:07 +0200155
156 if (!network_available_ && state_ == State::kWaitingForProbingResult) {
157 state_ = State::kProbingComplete;
158 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
159 }
160
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100161 if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200162 return InitiateExponentialProbing(msg.at_time.ms());
163 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100164}
165
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200166std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
167 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100168 RTC_DCHECK(network_available_);
169 RTC_DCHECK(state_ == State::kInit);
170 RTC_DCHECK_GT(start_bitrate_bps_, 0);
171
172 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
173 // 1.2 Mbps to continue probing.
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200174 return InitiateProbing(
175 at_time_ms, {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100176}
177
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200178std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
179 int64_t bitrate_bps,
180 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100181 int64_t now_ms = at_time_ms;
182
183 if (mid_call_probing_waiting_for_result_ &&
184 bitrate_bps >= mid_call_probing_succcess_threshold_) {
185 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
186 mid_call_probing_bitrate_bps_ / 1000);
187 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
188 bitrate_bps / 1000);
189 mid_call_probing_waiting_for_result_ = false;
190 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200191 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100192 if (state_ == State::kWaitingForProbingResult) {
193 // Continue probing if probing results indicate channel has greater
194 // capacity.
195 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
196 << " Minimum to probe further: "
197 << min_bitrate_to_probe_further_bps_;
198
199 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
200 bitrate_bps > min_bitrate_to_probe_further_bps_) {
201 // Double the probing bitrate.
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200202 pending_probes = InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100203 }
204 }
205
206 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
207 time_of_last_large_drop_ms_ = now_ms;
208 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
209 }
210
211 estimated_bitrate_bps_ = bitrate_bps;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200212 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100213}
214
215void ProbeController::EnablePeriodicAlrProbing(bool enable) {
216 enable_periodic_alr_probing_ = enable;
217}
218
219void ProbeController::SetAlrStartTimeMs(
Danil Chapovalov0040b662018-06-18 10:48:16 +0200220 absl::optional<int64_t> alr_start_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100221 alr_start_time_ms_ = alr_start_time_ms;
222}
223void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
224 alr_end_time_ms_.emplace(alr_end_time_ms);
225}
226
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200227std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
228 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100229 // Called once we have returned to normal state after a large drop in
230 // estimated bandwidth. The current response is to initiate a single probe
231 // session (if not already probing) at the previous bitrate.
232 //
233 // If the probe session fails, the assumption is that this drop was a
234 // real one from a competing flow or a network change.
235 bool in_alr = alr_start_time_ms_.has_value();
236 bool alr_ended_recently =
237 (alr_end_time_ms_.has_value() &&
238 at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
239 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
240 if (state_ == State::kProbingComplete) {
241 uint32_t suggested_probe_bps =
242 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
243 uint32_t min_expected_probe_result_bps =
244 (1 - kProbeUncertainty) * suggested_probe_bps;
245 int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_;
246 int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_;
247 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
248 time_since_drop_ms < kBitrateDropTimeoutMs &&
249 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
250 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
251 // Track how often we probe in response to bandwidth drop in ALR.
252 RTC_HISTOGRAM_COUNTS_10000(
253 "WebRTC.BWE.BweDropProbingIntervalInS",
254 (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200255 return InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100256 last_bwe_drop_probing_time_ms_ = at_time_ms;
257 }
258 }
259 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200260 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100261}
262
263void ProbeController::Reset(int64_t at_time_ms) {
264 network_available_ = true;
265 state_ = State::kInit;
266 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
267 time_last_probing_initiated_ms_ = 0;
268 estimated_bitrate_bps_ = 0;
269 start_bitrate_bps_ = 0;
270 max_bitrate_bps_ = 0;
271 int64_t now_ms = at_time_ms;
272 last_bwe_drop_probing_time_ms_ = now_ms;
273 alr_end_time_ms_.reset();
274 mid_call_probing_waiting_for_result_ = false;
275 time_of_last_large_drop_ms_ = now_ms;
276 bitrate_before_last_large_drop_bps_ = 0;
philipel0676f222018-04-17 16:12:21 +0200277 max_total_allocated_bitrate_ = 0;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100278}
279
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200280std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100281 int64_t now_ms = at_time_ms;
282
283 if (now_ms - time_last_probing_initiated_ms_ >
284 kMaxWaitingTimeForProbingResultMs) {
285 mid_call_probing_waiting_for_result_ = false;
286
287 if (state_ == State::kWaitingForProbingResult) {
288 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
289 state_ = State::kProbingComplete;
290 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
291 }
292 }
293
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200294 if (enable_periodic_alr_probing_ && state_ == State::kProbingComplete) {
295 // Probe bandwidth periodically when in ALR state.
296 if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) {
297 int64_t next_probe_time_ms =
298 std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) +
299 kAlrPeriodicProbingIntervalMs;
300 if (now_ms >= next_probe_time_ms) {
301 return InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
302 }
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100303 }
304 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200305 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100306}
307
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200308std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100309 int64_t now_ms,
310 std::initializer_list<int64_t> bitrates_to_probe,
311 bool probe_further) {
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200312 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100313 for (int64_t bitrate : bitrates_to_probe) {
314 RTC_DCHECK_GT(bitrate, 0);
315 int64_t max_probe_bitrate_bps =
316 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
317 if (bitrate > max_probe_bitrate_bps) {
318 bitrate = max_probe_bitrate_bps;
319 probe_further = false;
320 }
321
322 ProbeClusterConfig config;
323 config.at_time = Timestamp::ms(now_ms);
324 config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate));
325 config.target_duration = TimeDelta::ms(kMinProbeDurationMs);
326 config.target_probe_count = kMinProbePacketsSent;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200327 pending_probes.push_back(config);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100328 }
329 time_last_probing_initiated_ms_ = now_ms;
330 if (probe_further) {
331 state_ = State::kWaitingForProbingResult;
332 min_bitrate_to_probe_further_bps_ =
333 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
334 } else {
335 state_ = State::kProbingComplete;
336 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
337 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200338 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100339}
340
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100341} // namespace webrtc