blob: 2f1b5b8061cf90004da5172dae483676499977a1 [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 {
Sebastian Jansson83b18422018-02-27 17:07:11 +010022namespace webrtc_cc {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010023
24namespace {
25// The minimum number probing packets used.
26constexpr int kMinProbePacketsSent = 5;
27
28// The minimum probing duration in ms.
29constexpr int kMinProbeDurationMs = 15;
30
31// Maximum waiting time from the time of initiating probing to getting
32// the measured results back.
33constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
34
35// Value of |min_bitrate_to_probe_further_bps_| that indicates
36// further probing is disabled.
37constexpr int kExponentialProbingDisabled = 0;
38
39// Default probing bitrate limit. Applied only when the application didn't
40// specify max bitrate.
41constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
42
43// Interval between probes when ALR periodic probing is enabled.
44constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
45
46// Minimum probe bitrate percentage to probe further for repeated probes,
47// relative to the previous probe. For example, if 1Mbps probe results in
48// 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be
49// sent if we get 600kbps from the first one.
50constexpr int kRepeatedProbeMinPercentage = 70;
51
52// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
53// and we recover within |kBitrateDropTimeoutMs|, then we'll send
54// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
55constexpr double kBitrateDropThreshold = 0.66;
56constexpr int kBitrateDropTimeoutMs = 5000;
57constexpr double kProbeFractionAfterDrop = 0.85;
58
59// Timeout for probing after leaving ALR. If the bitrate drops significantly,
60// (as determined by the delay based estimator) and we leave ALR, then we will
61// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
62constexpr int kAlrEndedTimeoutMs = 3000;
63
64// The expected uncertainty of probe result (as a fraction of the target probe
65// This is a limit on how often probing can be done when there is a BW
66// drop detected in ALR.
67constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
68
69// bitrate). Used to avoid probing if the probe bitrate is close to our current
70// estimate.
71constexpr double kProbeUncertainty = 0.05;
72
73// Use probing to recover faster after large bitrate estimate drops.
74constexpr char kBweRapidRecoveryExperiment[] =
75 "WebRTC-BweRapidRecoveryExperiment";
76
77} // namespace
78
79ProbeController::ProbeController(NetworkControllerObserver* observer)
80 : observer_(observer), enable_periodic_alr_probing_(false) {
81 Reset(0);
82 in_rapid_recovery_experiment_ = webrtc::field_trial::FindFullName(
83 kBweRapidRecoveryExperiment) == "Enabled";
84}
85
86ProbeController::~ProbeController() {}
87
88void ProbeController::SetBitrates(int64_t min_bitrate_bps,
89 int64_t start_bitrate_bps,
90 int64_t max_bitrate_bps,
91 int64_t at_time_ms) {
92 if (start_bitrate_bps > 0) {
93 start_bitrate_bps_ = start_bitrate_bps;
94 estimated_bitrate_bps_ = start_bitrate_bps;
95 } else if (start_bitrate_bps_ == 0) {
96 start_bitrate_bps_ = min_bitrate_bps;
97 }
98
99 // The reason we use the variable |old_max_bitrate_pbs| is because we
100 // need to set |max_bitrate_bps_| before we call InitiateProbing.
101 int64_t old_max_bitrate_bps = max_bitrate_bps_;
102 max_bitrate_bps_ = max_bitrate_bps;
103
104 switch (state_) {
105 case State::kInit:
106 if (network_available_)
107 InitiateExponentialProbing(at_time_ms);
108 break;
109
110 case State::kWaitingForProbingResult:
111 break;
112
113 case State::kProbingComplete:
114 // If the new max bitrate is higher than the old max bitrate and the
115 // estimate is lower than the new max bitrate then initiate probing.
116 if (estimated_bitrate_bps_ != 0 &&
117 old_max_bitrate_bps < max_bitrate_bps_ &&
118 estimated_bitrate_bps_ < max_bitrate_bps_) {
119 // The assumption is that if we jump more than 20% in the bandwidth
120 // estimate or if the bandwidth estimate is within 90% of the new
121 // max bitrate then the probing attempt was successful.
122 mid_call_probing_succcess_threshold_ =
123 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
124 mid_call_probing_waiting_for_result_ = true;
125 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
126
127 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
128 max_bitrate_bps_ / 1000);
129
130 InitiateProbing(at_time_ms, {max_bitrate_bps}, false);
131 }
132 break;
133 }
134}
135
philipeldb4fa4b2018-03-06 18:29:22 +0100136void ProbeController::OnMaxTotalAllocatedBitrate(
137 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?
141 if (estimated_bitrate_bps_ != 0 &&
142 estimated_bitrate_bps_ < max_bitrate_bps_ &&
143 estimated_bitrate_bps_ < max_total_allocated_bitrate) {
144 InitiateProbing(at_time_ms, {max_total_allocated_bitrate}, false);
145 }
146}
147
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100148void ProbeController::OnNetworkAvailability(NetworkAvailability msg) {
149 network_available_ = msg.network_available;
150 if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
151 InitiateExponentialProbing(msg.at_time.ms());
152}
153
154void ProbeController::InitiateExponentialProbing(int64_t at_time_ms) {
155 RTC_DCHECK(network_available_);
156 RTC_DCHECK(state_ == State::kInit);
157 RTC_DCHECK_GT(start_bitrate_bps_, 0);
158
159 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
160 // 1.2 Mbps to continue probing.
161 InitiateProbing(at_time_ms, {3 * start_bitrate_bps_, 6 * start_bitrate_bps_},
162 true);
163}
164
165void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps,
166 int64_t at_time_ms) {
167 int64_t now_ms = at_time_ms;
168
169 if (mid_call_probing_waiting_for_result_ &&
170 bitrate_bps >= mid_call_probing_succcess_threshold_) {
171 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
172 mid_call_probing_bitrate_bps_ / 1000);
173 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
174 bitrate_bps / 1000);
175 mid_call_probing_waiting_for_result_ = false;
176 }
177
178 if (state_ == State::kWaitingForProbingResult) {
179 // Continue probing if probing results indicate channel has greater
180 // capacity.
181 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
182 << " Minimum to probe further: "
183 << min_bitrate_to_probe_further_bps_;
184
185 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
186 bitrate_bps > min_bitrate_to_probe_further_bps_) {
187 // Double the probing bitrate.
188 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
189 }
190 }
191
192 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
193 time_of_last_large_drop_ms_ = now_ms;
194 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
195 }
196
197 estimated_bitrate_bps_ = bitrate_bps;
198}
199
200void ProbeController::EnablePeriodicAlrProbing(bool enable) {
201 enable_periodic_alr_probing_ = enable;
202}
203
204void ProbeController::SetAlrStartTimeMs(
205 rtc::Optional<int64_t> alr_start_time_ms) {
206 alr_start_time_ms_ = alr_start_time_ms;
207}
208void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
209 alr_end_time_ms_.emplace(alr_end_time_ms);
210}
211
212void ProbeController::RequestProbe(int64_t at_time_ms) {
213 // Called once we have returned to normal state after a large drop in
214 // estimated bandwidth. The current response is to initiate a single probe
215 // session (if not already probing) at the previous bitrate.
216 //
217 // If the probe session fails, the assumption is that this drop was a
218 // real one from a competing flow or a network change.
219 bool in_alr = alr_start_time_ms_.has_value();
220 bool alr_ended_recently =
221 (alr_end_time_ms_.has_value() &&
222 at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
223 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
224 if (state_ == State::kProbingComplete) {
225 uint32_t suggested_probe_bps =
226 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
227 uint32_t min_expected_probe_result_bps =
228 (1 - kProbeUncertainty) * suggested_probe_bps;
229 int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_;
230 int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_;
231 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
232 time_since_drop_ms < kBitrateDropTimeoutMs &&
233 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
234 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
235 // Track how often we probe in response to bandwidth drop in ALR.
236 RTC_HISTOGRAM_COUNTS_10000(
237 "WebRTC.BWE.BweDropProbingIntervalInS",
238 (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000);
239 InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
240 last_bwe_drop_probing_time_ms_ = at_time_ms;
241 }
242 }
243 }
244}
245
246void ProbeController::Reset(int64_t at_time_ms) {
247 network_available_ = true;
248 state_ = State::kInit;
249 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
250 time_last_probing_initiated_ms_ = 0;
251 estimated_bitrate_bps_ = 0;
252 start_bitrate_bps_ = 0;
253 max_bitrate_bps_ = 0;
254 int64_t now_ms = at_time_ms;
255 last_bwe_drop_probing_time_ms_ = now_ms;
256 alr_end_time_ms_.reset();
257 mid_call_probing_waiting_for_result_ = false;
258 time_of_last_large_drop_ms_ = now_ms;
259 bitrate_before_last_large_drop_bps_ = 0;
260}
261
262void ProbeController::Process(int64_t at_time_ms) {
263 int64_t now_ms = at_time_ms;
264
265 if (now_ms - time_last_probing_initiated_ms_ >
266 kMaxWaitingTimeForProbingResultMs) {
267 mid_call_probing_waiting_for_result_ = false;
268
269 if (state_ == State::kWaitingForProbingResult) {
270 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
271 state_ = State::kProbingComplete;
272 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
273 }
274 }
275
276 if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_)
277 return;
278
279 // Probe bandwidth periodically when in ALR state.
280 if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) {
281 int64_t next_probe_time_ms =
282 std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) +
283 kAlrPeriodicProbingIntervalMs;
284 if (now_ms >= next_probe_time_ms) {
285 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
286 }
287 }
288}
289
290void ProbeController::InitiateProbing(
291 int64_t now_ms,
292 std::initializer_list<int64_t> bitrates_to_probe,
293 bool probe_further) {
294 for (int64_t bitrate : bitrates_to_probe) {
295 RTC_DCHECK_GT(bitrate, 0);
296 int64_t max_probe_bitrate_bps =
297 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
298 if (bitrate > max_probe_bitrate_bps) {
299 bitrate = max_probe_bitrate_bps;
300 probe_further = false;
301 }
302
303 ProbeClusterConfig config;
304 config.at_time = Timestamp::ms(now_ms);
305 config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate));
306 config.target_duration = TimeDelta::ms(kMinProbeDurationMs);
307 config.target_probe_count = kMinProbePacketsSent;
308 observer_->OnProbeClusterConfig(config);
309 }
310 time_last_probing_initiated_ms_ = now_ms;
311 if (probe_further) {
312 state_ = State::kWaitingForProbingResult;
313 min_bitrate_to_probe_further_bps_ =
314 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
315 } else {
316 state_ = State::kProbingComplete;
317 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
318 }
319}
320
Sebastian Jansson83b18422018-02-27 17:07:11 +0100321} // namespace webrtc_cc
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100322} // namespace webrtc