blob: af967dc9f4459beaf6690385070c9f6ccc2aee74 [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>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <string>
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010016
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "api/units/data_rate.h"
18#include "api/units/time_delta.h"
19#include "api/units/timestamp.h"
20#include "rtc_base/checks.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010021#include "rtc_base/logging.h"
22#include "rtc_base/numerics/safe_conversions.h"
23#include "system_wrappers/include/field_trial.h"
24#include "system_wrappers/include/metrics.h"
25
26namespace webrtc {
27
28namespace {
29// The minimum number probing packets used.
30constexpr int kMinProbePacketsSent = 5;
31
32// The minimum probing duration in ms.
33constexpr int kMinProbeDurationMs = 15;
34
35// Maximum waiting time from the time of initiating probing to getting
36// the measured results back.
37constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
38
39// Value of |min_bitrate_to_probe_further_bps_| that indicates
40// further probing is disabled.
41constexpr int kExponentialProbingDisabled = 0;
42
43// Default probing bitrate limit. Applied only when the application didn't
44// specify max bitrate.
45constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
46
47// Interval between probes when ALR periodic probing is enabled.
48constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
49
50// Minimum probe bitrate percentage to probe further for repeated probes,
51// relative to the previous probe. For example, if 1Mbps probe results in
52// 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be
53// sent if we get 600kbps from the first one.
54constexpr int kRepeatedProbeMinPercentage = 70;
55
56// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
57// and we recover within |kBitrateDropTimeoutMs|, then we'll send
58// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
59constexpr double kBitrateDropThreshold = 0.66;
60constexpr int kBitrateDropTimeoutMs = 5000;
61constexpr double kProbeFractionAfterDrop = 0.85;
62
63// Timeout for probing after leaving ALR. If the bitrate drops significantly,
64// (as determined by the delay based estimator) and we leave ALR, then we will
65// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
66constexpr int kAlrEndedTimeoutMs = 3000;
67
68// The expected uncertainty of probe result (as a fraction of the target probe
69// This is a limit on how often probing can be done when there is a BW
70// drop detected in ALR.
71constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
72
73// bitrate). Used to avoid probing if the probe bitrate is close to our current
74// estimate.
75constexpr double kProbeUncertainty = 0.05;
76
77// Use probing to recover faster after large bitrate estimate drops.
78constexpr char kBweRapidRecoveryExperiment[] =
79 "WebRTC-BweRapidRecoveryExperiment";
80
81} // namespace
82
Sebastian Janssonf2e3e7a2018-04-06 17:16:06 +020083ProbeController::ProbeController() : enable_periodic_alr_probing_(false) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010084 Reset(0);
85 in_rapid_recovery_experiment_ = webrtc::field_trial::FindFullName(
86 kBweRapidRecoveryExperiment) == "Enabled";
87}
88
89ProbeController::~ProbeController() {}
90
Sebastian Janssonda2ec402018-08-02 16:27:28 +020091std::vector<ProbeClusterConfig> ProbeController::SetBitrates(
92 int64_t min_bitrate_bps,
93 int64_t start_bitrate_bps,
94 int64_t max_bitrate_bps,
95 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010096 if (start_bitrate_bps > 0) {
97 start_bitrate_bps_ = start_bitrate_bps;
98 estimated_bitrate_bps_ = start_bitrate_bps;
99 } else if (start_bitrate_bps_ == 0) {
100 start_bitrate_bps_ = min_bitrate_bps;
101 }
102
103 // The reason we use the variable |old_max_bitrate_pbs| is because we
104 // need to set |max_bitrate_bps_| before we call InitiateProbing.
105 int64_t old_max_bitrate_bps = max_bitrate_bps_;
106 max_bitrate_bps_ = max_bitrate_bps;
107
108 switch (state_) {
109 case State::kInit:
110 if (network_available_)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200111 return InitiateExponentialProbing(at_time_ms);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100112 break;
113
114 case State::kWaitingForProbingResult:
115 break;
116
117 case State::kProbingComplete:
118 // If the new max bitrate is higher than the old max bitrate and the
119 // estimate is lower than the new max bitrate then initiate probing.
120 if (estimated_bitrate_bps_ != 0 &&
121 old_max_bitrate_bps < max_bitrate_bps_ &&
122 estimated_bitrate_bps_ < max_bitrate_bps_) {
123 // The assumption is that if we jump more than 20% in the bandwidth
124 // estimate or if the bandwidth estimate is within 90% of the new
125 // max bitrate then the probing attempt was successful.
126 mid_call_probing_succcess_threshold_ =
127 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
128 mid_call_probing_waiting_for_result_ = true;
129 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
130
131 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
132 max_bitrate_bps_ / 1000);
133
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200134 return InitiateProbing(at_time_ms, {max_bitrate_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100135 }
136 break;
137 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200138 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100139}
140
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200141std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
philipeldb4fa4b2018-03-06 18:29:22 +0100142 int64_t max_total_allocated_bitrate,
143 int64_t at_time_ms) {
144 // TODO(philipel): Should |max_total_allocated_bitrate| be used as a limit for
145 // ALR probing?
philipel9fd6b982018-04-19 16:58:07 +0200146 if (state_ == State::kProbingComplete &&
147 max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
philipel0676f222018-04-17 16:12:21 +0200148 estimated_bitrate_bps_ != 0 &&
149 (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
philipeldb4fa4b2018-03-06 18:29:22 +0100150 estimated_bitrate_bps_ < max_total_allocated_bitrate) {
Sebastian Janssonb2ecc3d2018-07-13 17:22:01 +0200151 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200152 return InitiateProbing(at_time_ms, {max_total_allocated_bitrate}, false);
philipeldb4fa4b2018-03-06 18:29:22 +0100153 }
Sebastian Janssonf5e767d2018-10-15 13:24:31 +0200154 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200155 return std::vector<ProbeClusterConfig>();
philipeldb4fa4b2018-03-06 18:29:22 +0100156}
157
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200158std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
159 NetworkAvailability msg) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100160 network_available_ = msg.network_available;
philipel9fd6b982018-04-19 16:58:07 +0200161
162 if (!network_available_ && state_ == State::kWaitingForProbingResult) {
163 state_ = State::kProbingComplete;
164 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
165 }
166
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100167 if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200168 return InitiateExponentialProbing(msg.at_time.ms());
169 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100170}
171
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200172std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
173 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100174 RTC_DCHECK(network_available_);
175 RTC_DCHECK(state_ == State::kInit);
176 RTC_DCHECK_GT(start_bitrate_bps_, 0);
177
178 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
179 // 1.2 Mbps to continue probing.
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200180 return InitiateProbing(
181 at_time_ms, {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100182}
183
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200184std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
185 int64_t bitrate_bps,
186 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100187 int64_t now_ms = at_time_ms;
188
189 if (mid_call_probing_waiting_for_result_ &&
190 bitrate_bps >= mid_call_probing_succcess_threshold_) {
191 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
192 mid_call_probing_bitrate_bps_ / 1000);
193 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
194 bitrate_bps / 1000);
195 mid_call_probing_waiting_for_result_ = false;
196 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200197 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100198 if (state_ == State::kWaitingForProbingResult) {
199 // Continue probing if probing results indicate channel has greater
200 // capacity.
201 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
202 << " Minimum to probe further: "
203 << min_bitrate_to_probe_further_bps_;
204
205 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
206 bitrate_bps > min_bitrate_to_probe_further_bps_) {
207 // Double the probing bitrate.
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200208 pending_probes = InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100209 }
210 }
211
212 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
213 time_of_last_large_drop_ms_ = now_ms;
214 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
215 }
216
217 estimated_bitrate_bps_ = bitrate_bps;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200218 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100219}
220
221void ProbeController::EnablePeriodicAlrProbing(bool enable) {
222 enable_periodic_alr_probing_ = enable;
223}
224
225void ProbeController::SetAlrStartTimeMs(
Danil Chapovalov0040b662018-06-18 10:48:16 +0200226 absl::optional<int64_t> alr_start_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100227 alr_start_time_ms_ = alr_start_time_ms;
228}
229void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
230 alr_end_time_ms_.emplace(alr_end_time_ms);
231}
232
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200233std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
234 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100235 // Called once we have returned to normal state after a large drop in
236 // estimated bandwidth. The current response is to initiate a single probe
237 // session (if not already probing) at the previous bitrate.
238 //
239 // If the probe session fails, the assumption is that this drop was a
240 // real one from a competing flow or a network change.
241 bool in_alr = alr_start_time_ms_.has_value();
242 bool alr_ended_recently =
243 (alr_end_time_ms_.has_value() &&
244 at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
245 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
246 if (state_ == State::kProbingComplete) {
247 uint32_t suggested_probe_bps =
248 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
249 uint32_t min_expected_probe_result_bps =
250 (1 - kProbeUncertainty) * suggested_probe_bps;
251 int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_;
252 int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_;
253 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
254 time_since_drop_ms < kBitrateDropTimeoutMs &&
255 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
256 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
257 // Track how often we probe in response to bandwidth drop in ALR.
258 RTC_HISTOGRAM_COUNTS_10000(
259 "WebRTC.BWE.BweDropProbingIntervalInS",
260 (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200261 return InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100262 last_bwe_drop_probing_time_ms_ = at_time_ms;
263 }
264 }
265 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200266 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100267}
268
Sebastian Jansson8d33c0c2018-10-22 10:22:03 +0200269std::vector<ProbeClusterConfig> ProbeController::InitiateCapacityProbing(
270 int64_t bitrate_bps,
271 int64_t at_time_ms) {
272 if (state_ != State::kWaitingForProbingResult) {
273 RTC_DCHECK(network_available_);
274 return InitiateProbing(at_time_ms, {2 * bitrate_bps}, true);
275 }
276 return std::vector<ProbeClusterConfig>();
277}
278
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100279void ProbeController::Reset(int64_t at_time_ms) {
280 network_available_ = true;
281 state_ = State::kInit;
282 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
283 time_last_probing_initiated_ms_ = 0;
284 estimated_bitrate_bps_ = 0;
285 start_bitrate_bps_ = 0;
286 max_bitrate_bps_ = 0;
287 int64_t now_ms = at_time_ms;
288 last_bwe_drop_probing_time_ms_ = now_ms;
289 alr_end_time_ms_.reset();
290 mid_call_probing_waiting_for_result_ = false;
291 time_of_last_large_drop_ms_ = now_ms;
292 bitrate_before_last_large_drop_bps_ = 0;
philipel0676f222018-04-17 16:12:21 +0200293 max_total_allocated_bitrate_ = 0;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100294}
295
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200296std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100297 int64_t now_ms = at_time_ms;
298
299 if (now_ms - time_last_probing_initiated_ms_ >
300 kMaxWaitingTimeForProbingResultMs) {
301 mid_call_probing_waiting_for_result_ = false;
302
303 if (state_ == State::kWaitingForProbingResult) {
304 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
305 state_ = State::kProbingComplete;
306 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
307 }
308 }
309
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200310 if (enable_periodic_alr_probing_ && state_ == State::kProbingComplete) {
311 // Probe bandwidth periodically when in ALR state.
312 if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) {
313 int64_t next_probe_time_ms =
314 std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) +
315 kAlrPeriodicProbingIntervalMs;
316 if (now_ms >= next_probe_time_ms) {
317 return InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
318 }
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100319 }
320 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200321 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100322}
323
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200324std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100325 int64_t now_ms,
326 std::initializer_list<int64_t> bitrates_to_probe,
327 bool probe_further) {
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200328 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100329 for (int64_t bitrate : bitrates_to_probe) {
330 RTC_DCHECK_GT(bitrate, 0);
331 int64_t max_probe_bitrate_bps =
332 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
333 if (bitrate > max_probe_bitrate_bps) {
334 bitrate = max_probe_bitrate_bps;
335 probe_further = false;
336 }
337
338 ProbeClusterConfig config;
339 config.at_time = Timestamp::ms(now_ms);
340 config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate));
341 config.target_duration = TimeDelta::ms(kMinProbeDurationMs);
342 config.target_probe_count = kMinProbePacketsSent;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200343 pending_probes.push_back(config);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100344 }
345 time_last_probing_initiated_ms_ = now_ms;
346 if (probe_further) {
347 state_ = State::kWaitingForProbingResult;
348 min_bitrate_to_probe_further_bps_ =
349 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
350 } else {
351 state_ = State::kProbingComplete;
352 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
353 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200354 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100355}
356
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100357} // namespace webrtc