blob: 29b472a873572cad95377b0d7337b9d903c2d222 [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>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <string>
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010017
Mirko Bonadei57cabed2020-04-01 12:03:11 +020018#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/units/data_rate.h"
20#include "api/units/time_delta.h"
21#include "api/units/timestamp.h"
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080022#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/checks.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010024#include "rtc_base/logging.h"
25#include "rtc_base/numerics/safe_conversions.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010026#include "system_wrappers/include/metrics.h"
27
28namespace webrtc {
29
30namespace {
31// The minimum number probing packets used.
32constexpr int kMinProbePacketsSent = 5;
33
34// The minimum probing duration in ms.
35constexpr int kMinProbeDurationMs = 15;
36
37// Maximum waiting time from the time of initiating probing to getting
38// the measured results back.
39constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
40
41// Value of |min_bitrate_to_probe_further_bps_| that indicates
42// further probing is disabled.
43constexpr int kExponentialProbingDisabled = 0;
44
45// Default probing bitrate limit. Applied only when the application didn't
46// specify max bitrate.
47constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
48
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010049// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
50// and we recover within |kBitrateDropTimeoutMs|, then we'll send
51// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
52constexpr double kBitrateDropThreshold = 0.66;
53constexpr int kBitrateDropTimeoutMs = 5000;
54constexpr double kProbeFractionAfterDrop = 0.85;
55
56// Timeout for probing after leaving ALR. If the bitrate drops significantly,
57// (as determined by the delay based estimator) and we leave ALR, then we will
58// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
59constexpr int kAlrEndedTimeoutMs = 3000;
60
61// The expected uncertainty of probe result (as a fraction of the target probe
62// This is a limit on how often probing can be done when there is a BW
63// drop detected in ALR.
64constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
65
66// bitrate). Used to avoid probing if the probe bitrate is close to our current
67// estimate.
68constexpr double kProbeUncertainty = 0.05;
69
70// Use probing to recover faster after large bitrate estimate drops.
71constexpr char kBweRapidRecoveryExperiment[] =
72 "WebRTC-BweRapidRecoveryExperiment";
73
Erik Språngcfe36ca2018-11-29 17:32:48 +010074// Never probe higher than configured by OnMaxTotalAllocatedBitrate().
75constexpr char kCappedProbingFieldTrialName[] = "WebRTC-BweCappedProbing";
76
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080077void MaybeLogProbeClusterCreated(RtcEventLog* event_log,
78 const ProbeClusterConfig& probe) {
79 RTC_DCHECK(event_log);
80 if (!event_log) {
81 return;
82 }
83
84 size_t min_bytes = static_cast<int32_t>(probe.target_data_rate.bps() *
85 probe.target_duration.ms() / 8000);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020086 event_log->Log(std::make_unique<RtcEventProbeClusterCreated>(
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080087 probe.id, probe.target_data_rate.bps(), probe.target_probe_count,
88 min_bytes));
89}
90
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010091} // namespace
92
Jonas Olssone0960042019-03-12 13:49:26 +010093ProbeControllerConfig::ProbeControllerConfig(
94 const WebRtcKeyValueConfig* key_value_config)
Jonas Olsson01d36182019-03-27 15:57:02 +010095 : first_exponential_probe_scale("p1", 3.0),
96 second_exponential_probe_scale("p2", 6.0),
97 further_exponential_probe_scale("step_size", 2),
Jonas Olssone0960042019-03-12 13:49:26 +010098 further_probe_threshold("further_probe_threshold", 0.7),
Danil Chapovalov55284022020-02-07 14:53:52 +010099 alr_probing_interval("alr_interval", TimeDelta::Seconds(5)),
Jonas Olsson01d36182019-03-27 15:57:02 +0100100 alr_probe_scale("alr_scale", 2),
101 first_allocation_probe_scale("alloc_p1", 1),
102 second_allocation_probe_scale("alloc_p2", 2),
Christoffer Rodbro377f5a22020-02-12 10:11:13 +0100103 allocation_allow_further_probing("alloc_probe_further", false),
104 allocation_probe_max("alloc_probe_max", DataRate::PlusInfinity()) {
Jonas Olssone0960042019-03-12 13:49:26 +0100105 ParseFieldTrial(
Jonas Olsson01d36182019-03-27 15:57:02 +0100106 {&first_exponential_probe_scale, &second_exponential_probe_scale,
107 &further_exponential_probe_scale, &further_probe_threshold,
108 &alr_probing_interval, &alr_probe_scale, &first_allocation_probe_scale,
109 &second_allocation_probe_scale, &allocation_allow_further_probing},
Jonas Olsson24923e82019-03-27 14:19:04 +0100110 key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
Jonas Olsson2e8d78c2019-05-27 13:31:45 +0200111
112 // Specialized keys overriding subsets of WebRTC-Bwe-ProbingConfiguration
113 ParseFieldTrial(
114 {&first_exponential_probe_scale, &second_exponential_probe_scale},
115 key_value_config->Lookup("WebRTC-Bwe-InitialProbing"));
116 ParseFieldTrial({&further_exponential_probe_scale, &further_probe_threshold},
117 key_value_config->Lookup("WebRTC-Bwe-ExponentialProbing"));
118 ParseFieldTrial({&alr_probing_interval, &alr_probe_scale},
119 key_value_config->Lookup("WebRTC-Bwe-AlrProbing"));
120 ParseFieldTrial(
121 {&first_allocation_probe_scale, &second_allocation_probe_scale,
Christoffer Rodbro377f5a22020-02-12 10:11:13 +0100122 &allocation_allow_further_probing, &allocation_probe_max},
Jonas Olsson2e8d78c2019-05-27 13:31:45 +0200123 key_value_config->Lookup("WebRTC-Bwe-AllocationProbing"));
Jonas Olssone0960042019-03-12 13:49:26 +0100124}
125
126ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) =
127 default;
128ProbeControllerConfig::~ProbeControllerConfig() = default;
129
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800130ProbeController::ProbeController(const WebRtcKeyValueConfig* key_value_config,
131 RtcEventLog* event_log)
Erik Språngcfe36ca2018-11-29 17:32:48 +0100132 : enable_periodic_alr_probing_(false),
Mirko Bonadei57cabed2020-04-01 12:03:11 +0200133 in_rapid_recovery_experiment_(absl::StartsWith(
134 key_value_config->Lookup(kBweRapidRecoveryExperiment),
135 "Enabled")),
136 limit_probes_with_allocateable_rate_(!absl::StartsWith(
137 key_value_config->Lookup(kCappedProbingFieldTrialName),
138 "Disabled")),
Jonas Olssone0960042019-03-12 13:49:26 +0100139 event_log_(event_log),
140 config_(ProbeControllerConfig(key_value_config)) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100141 Reset(0);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100142}
143
144ProbeController::~ProbeController() {}
145
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200146std::vector<ProbeClusterConfig> ProbeController::SetBitrates(
147 int64_t min_bitrate_bps,
148 int64_t start_bitrate_bps,
149 int64_t max_bitrate_bps,
150 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100151 if (start_bitrate_bps > 0) {
152 start_bitrate_bps_ = start_bitrate_bps;
153 estimated_bitrate_bps_ = start_bitrate_bps;
154 } else if (start_bitrate_bps_ == 0) {
155 start_bitrate_bps_ = min_bitrate_bps;
156 }
157
158 // The reason we use the variable |old_max_bitrate_pbs| is because we
159 // need to set |max_bitrate_bps_| before we call InitiateProbing.
160 int64_t old_max_bitrate_bps = max_bitrate_bps_;
161 max_bitrate_bps_ = max_bitrate_bps;
162
163 switch (state_) {
164 case State::kInit:
165 if (network_available_)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200166 return InitiateExponentialProbing(at_time_ms);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100167 break;
168
169 case State::kWaitingForProbingResult:
170 break;
171
172 case State::kProbingComplete:
Jonas Olssonf441ea92019-03-05 16:24:48 +0100173 // If the new max bitrate is higher than both the old max bitrate and the
174 // estimate then initiate probing.
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100175 if (estimated_bitrate_bps_ != 0 &&
176 old_max_bitrate_bps < max_bitrate_bps_ &&
177 estimated_bitrate_bps_ < max_bitrate_bps_) {
178 // The assumption is that if we jump more than 20% in the bandwidth
179 // estimate or if the bandwidth estimate is within 90% of the new
180 // max bitrate then the probing attempt was successful.
181 mid_call_probing_succcess_threshold_ =
182 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
183 mid_call_probing_waiting_for_result_ = true;
184 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
185
186 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
187 max_bitrate_bps_ / 1000);
188
Jonas Olssonf441ea92019-03-05 16:24:48 +0100189 return InitiateProbing(at_time_ms, {max_bitrate_bps_}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100190 }
191 break;
192 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200193 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100194}
195
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200196std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
philipeldb4fa4b2018-03-06 18:29:22 +0100197 int64_t max_total_allocated_bitrate,
198 int64_t at_time_ms) {
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200199 const bool in_alr = alr_start_time_ms_.has_value();
Konrad Hofbauerfdf38802019-08-19 13:50:57 +0200200 const bool allow_allocation_probe = in_alr;
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200201
philipel9fd6b982018-04-19 16:58:07 +0200202 if (state_ == State::kProbingComplete &&
203 max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
philipel0676f222018-04-17 16:12:21 +0200204 estimated_bitrate_bps_ != 0 &&
205 (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200206 estimated_bitrate_bps_ < max_total_allocated_bitrate &&
207 allow_allocation_probe) {
Sebastian Janssonb2ecc3d2018-07-13 17:22:01 +0200208 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Jonas Olsson01d36182019-03-27 15:57:02 +0100209
210 if (!config_.first_allocation_probe_scale)
211 return std::vector<ProbeClusterConfig>();
212
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100213 DataRate first_probe_rate =
214 DataRate::BitsPerSec(max_total_allocated_bitrate) *
215 config_.first_allocation_probe_scale.Value();
Christoffer Rodbro377f5a22020-02-12 10:11:13 +0100216 DataRate probe_cap = config_.allocation_probe_max.Get();
217 first_probe_rate = std::min(first_probe_rate, probe_cap);
218 std::vector<int64_t> probes = {first_probe_rate.bps()};
Jonas Olsson01d36182019-03-27 15:57:02 +0100219 if (config_.second_allocation_probe_scale) {
Christoffer Rodbro377f5a22020-02-12 10:11:13 +0100220 DataRate second_probe_rate =
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100221 DataRate::BitsPerSec(max_total_allocated_bitrate) *
Christoffer Rodbro377f5a22020-02-12 10:11:13 +0100222 config_.second_allocation_probe_scale.Value();
223 second_probe_rate = std::min(second_probe_rate, probe_cap);
224 if (second_probe_rate > first_probe_rate)
225 probes.push_back(second_probe_rate.bps());
Jonas Olsson01d36182019-03-27 15:57:02 +0100226 }
227 return InitiateProbing(at_time_ms, probes,
228 config_.allocation_allow_further_probing);
philipeldb4fa4b2018-03-06 18:29:22 +0100229 }
Sebastian Janssonf5e767d2018-10-15 13:24:31 +0200230 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200231 return std::vector<ProbeClusterConfig>();
philipeldb4fa4b2018-03-06 18:29:22 +0100232}
233
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200234std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
235 NetworkAvailability msg) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100236 network_available_ = msg.network_available;
philipel9fd6b982018-04-19 16:58:07 +0200237
238 if (!network_available_ && state_ == State::kWaitingForProbingResult) {
239 state_ = State::kProbingComplete;
240 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
241 }
242
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100243 if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200244 return InitiateExponentialProbing(msg.at_time.ms());
245 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100246}
247
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200248std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
249 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100250 RTC_DCHECK(network_available_);
251 RTC_DCHECK(state_ == State::kInit);
252 RTC_DCHECK_GT(start_bitrate_bps_, 0);
253
254 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
255 // 1.2 Mbps to continue probing.
Jonas Olssone0960042019-03-12 13:49:26 +0100256 std::vector<int64_t> probes = {static_cast<int64_t>(
Jonas Olsson01d36182019-03-27 15:57:02 +0100257 config_.first_exponential_probe_scale * start_bitrate_bps_)};
258 if (config_.second_exponential_probe_scale) {
259 probes.push_back(config_.second_exponential_probe_scale.Value() *
Jonas Olssone0960042019-03-12 13:49:26 +0100260 start_bitrate_bps_);
261 }
262 return InitiateProbing(at_time_ms, probes, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100263}
264
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200265std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
266 int64_t bitrate_bps,
267 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100268 if (mid_call_probing_waiting_for_result_ &&
269 bitrate_bps >= mid_call_probing_succcess_threshold_) {
270 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
271 mid_call_probing_bitrate_bps_ / 1000);
272 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
273 bitrate_bps / 1000);
274 mid_call_probing_waiting_for_result_ = false;
275 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200276 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100277 if (state_ == State::kWaitingForProbingResult) {
278 // Continue probing if probing results indicate channel has greater
279 // capacity.
280 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
281 << " Minimum to probe further: "
282 << min_bitrate_to_probe_further_bps_;
283
284 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
285 bitrate_bps > min_bitrate_to_probe_further_bps_) {
Jonas Olssone0960042019-03-12 13:49:26 +0100286 pending_probes = InitiateProbing(
287 at_time_ms,
Jonas Olsson01d36182019-03-27 15:57:02 +0100288 {static_cast<int64_t>(config_.further_exponential_probe_scale *
Jonas Olssone0960042019-03-12 13:49:26 +0100289 bitrate_bps)},
290 true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100291 }
292 }
293
294 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100295 time_of_last_large_drop_ms_ = at_time_ms;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100296 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
297 }
298
299 estimated_bitrate_bps_ = bitrate_bps;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200300 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100301}
302
303void ProbeController::EnablePeriodicAlrProbing(bool enable) {
304 enable_periodic_alr_probing_ = enable;
305}
306
307void ProbeController::SetAlrStartTimeMs(
Danil Chapovalov0040b662018-06-18 10:48:16 +0200308 absl::optional<int64_t> alr_start_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100309 alr_start_time_ms_ = alr_start_time_ms;
310}
311void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
312 alr_end_time_ms_.emplace(alr_end_time_ms);
313}
314
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200315std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
316 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100317 // Called once we have returned to normal state after a large drop in
318 // estimated bandwidth. The current response is to initiate a single probe
319 // session (if not already probing) at the previous bitrate.
320 //
321 // If the probe session fails, the assumption is that this drop was a
322 // real one from a competing flow or a network change.
323 bool in_alr = alr_start_time_ms_.has_value();
324 bool alr_ended_recently =
325 (alr_end_time_ms_.has_value() &&
326 at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
327 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
328 if (state_ == State::kProbingComplete) {
329 uint32_t suggested_probe_bps =
330 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
331 uint32_t min_expected_probe_result_bps =
332 (1 - kProbeUncertainty) * suggested_probe_bps;
333 int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_;
334 int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_;
335 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
336 time_since_drop_ms < kBitrateDropTimeoutMs &&
337 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
338 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
339 // Track how often we probe in response to bandwidth drop in ALR.
340 RTC_HISTOGRAM_COUNTS_10000(
341 "WebRTC.BWE.BweDropProbingIntervalInS",
342 (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100343 last_bwe_drop_probing_time_ms_ = at_time_ms;
Sebastian Janssonc9d0b082019-02-26 15:29:53 +0100344 return InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100345 }
346 }
347 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200348 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100349}
350
Erik Språng791d43c2019-01-08 15:46:06 +0100351void ProbeController::SetMaxBitrate(int64_t max_bitrate_bps) {
352 max_bitrate_bps_ = max_bitrate_bps;
353}
354
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100355void ProbeController::Reset(int64_t at_time_ms) {
356 network_available_ = true;
357 state_ = State::kInit;
358 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
359 time_last_probing_initiated_ms_ = 0;
360 estimated_bitrate_bps_ = 0;
361 start_bitrate_bps_ = 0;
362 max_bitrate_bps_ = 0;
363 int64_t now_ms = at_time_ms;
364 last_bwe_drop_probing_time_ms_ = now_ms;
365 alr_end_time_ms_.reset();
366 mid_call_probing_waiting_for_result_ = false;
367 time_of_last_large_drop_ms_ = now_ms;
368 bitrate_before_last_large_drop_bps_ = 0;
philipel0676f222018-04-17 16:12:21 +0200369 max_total_allocated_bitrate_ = 0;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100370}
371
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200372std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100373 if (at_time_ms - time_last_probing_initiated_ms_ >
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100374 kMaxWaitingTimeForProbingResultMs) {
375 mid_call_probing_waiting_for_result_ = false;
376
377 if (state_ == State::kWaitingForProbingResult) {
378 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
379 state_ = State::kProbingComplete;
380 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
381 }
382 }
383
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200384 if (enable_periodic_alr_probing_ && state_ == State::kProbingComplete) {
385 // Probe bandwidth periodically when in ALR state.
386 if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) {
387 int64_t next_probe_time_ms =
388 std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) +
Jonas Olsson01d36182019-03-27 15:57:02 +0100389 config_.alr_probing_interval->ms();
Jonas Olssonf441ea92019-03-05 16:24:48 +0100390 if (at_time_ms >= next_probe_time_ms) {
Jonas Olssone0960042019-03-12 13:49:26 +0100391 return InitiateProbing(at_time_ms,
392 {static_cast<int64_t>(estimated_bitrate_bps_ *
Jonas Olsson01d36182019-03-27 15:57:02 +0100393 config_.alr_probe_scale)},
Jonas Olssone0960042019-03-12 13:49:26 +0100394 true);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200395 }
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100396 }
397 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200398 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100399}
400
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200401std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100402 int64_t now_ms,
Jonas Olssone0960042019-03-12 13:49:26 +0100403 std::vector<int64_t> bitrates_to_probe,
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100404 bool probe_further) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100405 int64_t max_probe_bitrate_bps =
406 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
407 if (limit_probes_with_allocateable_rate_ &&
408 max_total_allocated_bitrate_ > 0) {
409 // If a max allocated bitrate has been configured, allow probing up to 2x
410 // that rate. This allows some overhead to account for bursty streams,
411 // which otherwise would have to ramp up when the overshoot is already in
412 // progress.
413 // It also avoids minor quality reduction caused by probes often being
414 // received at slightly less than the target probe bitrate.
415 max_probe_bitrate_bps =
416 std::min(max_probe_bitrate_bps, max_total_allocated_bitrate_ * 2);
417 }
418
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200419 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100420 for (int64_t bitrate : bitrates_to_probe) {
421 RTC_DCHECK_GT(bitrate, 0);
Jonas Olssonf441ea92019-03-05 16:24:48 +0100422
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100423 if (bitrate > max_probe_bitrate_bps) {
424 bitrate = max_probe_bitrate_bps;
425 probe_further = false;
426 }
427
428 ProbeClusterConfig config;
Danil Chapovalov55284022020-02-07 14:53:52 +0100429 config.at_time = Timestamp::Millis(now_ms);
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +0100430 config.target_data_rate =
431 DataRate::BitsPerSec(rtc::dchecked_cast<int>(bitrate));
Danil Chapovalov55284022020-02-07 14:53:52 +0100432 config.target_duration = TimeDelta::Millis(kMinProbeDurationMs);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100433 config.target_probe_count = kMinProbePacketsSent;
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800434 config.id = next_probe_cluster_id_;
435 next_probe_cluster_id_++;
436 MaybeLogProbeClusterCreated(event_log_, config);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200437 pending_probes.push_back(config);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100438 }
439 time_last_probing_initiated_ms_ = now_ms;
440 if (probe_further) {
441 state_ = State::kWaitingForProbingResult;
442 min_bitrate_to_probe_further_bps_ =
Jonas Olssone0960042019-03-12 13:49:26 +0100443 (*(bitrates_to_probe.end() - 1)) * config_.further_probe_threshold;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100444 } else {
445 state_ = State::kProbingComplete;
446 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
447 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200448 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100449}
450
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100451} // namespace webrtc