blob: 1b13694c0168ba00128e7f18c9f4c257148aa9c2 [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
Jonas Olsson2e8d78c2019-05-27 13:31:45 +020017#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/units/data_rate.h"
19#include "api/units/time_delta.h"
20#include "api/units/timestamp.h"
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080021#include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/checks.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010023#include "rtc_base/logging.h"
24#include "rtc_base/numerics/safe_conversions.h"
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010025#include "system_wrappers/include/metrics.h"
26
27namespace webrtc {
28
29namespace {
30// The minimum number probing packets used.
31constexpr int kMinProbePacketsSent = 5;
32
33// The minimum probing duration in ms.
34constexpr int kMinProbeDurationMs = 15;
35
36// Maximum waiting time from the time of initiating probing to getting
37// the measured results back.
38constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
39
40// Value of |min_bitrate_to_probe_further_bps_| that indicates
41// further probing is disabled.
42constexpr int kExponentialProbingDisabled = 0;
43
44// Default probing bitrate limit. Applied only when the application didn't
45// specify max bitrate.
46constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
47
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010048// If the bitrate drops to a factor |kBitrateDropThreshold| or lower
49// and we recover within |kBitrateDropTimeoutMs|, then we'll send
50// a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate.
51constexpr double kBitrateDropThreshold = 0.66;
52constexpr int kBitrateDropTimeoutMs = 5000;
53constexpr double kProbeFractionAfterDrop = 0.85;
54
55// Timeout for probing after leaving ALR. If the bitrate drops significantly,
56// (as determined by the delay based estimator) and we leave ALR, then we will
57// send a probe if we recover within |kLeftAlrTimeoutMs| ms.
58constexpr int kAlrEndedTimeoutMs = 3000;
59
60// The expected uncertainty of probe result (as a fraction of the target probe
61// This is a limit on how often probing can be done when there is a BW
62// drop detected in ALR.
63constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
64
65// bitrate). Used to avoid probing if the probe bitrate is close to our current
66// estimate.
67constexpr double kProbeUncertainty = 0.05;
68
69// Use probing to recover faster after large bitrate estimate drops.
70constexpr char kBweRapidRecoveryExperiment[] =
71 "WebRTC-BweRapidRecoveryExperiment";
72
Erik Språngcfe36ca2018-11-29 17:32:48 +010073// Never probe higher than configured by OnMaxTotalAllocatedBitrate().
74constexpr char kCappedProbingFieldTrialName[] = "WebRTC-BweCappedProbing";
75
Konrad Hofbauer25f35a82019-04-10 12:38:06 +020076// Only do allocation probing when in ALR (but not when network-limited).
77constexpr char kAllocProbingOnlyInAlrFieldTrialName[] =
78 "WebRTC-BweAllocProbingOnlyInAlr";
79
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080080void MaybeLogProbeClusterCreated(RtcEventLog* event_log,
81 const ProbeClusterConfig& probe) {
82 RTC_DCHECK(event_log);
83 if (!event_log) {
84 return;
85 }
86
87 size_t min_bytes = static_cast<int32_t>(probe.target_data_rate.bps() *
88 probe.target_duration.ms() / 8000);
89 event_log->Log(absl::make_unique<RtcEventProbeClusterCreated>(
90 probe.id, probe.target_data_rate.bps(), probe.target_probe_count,
91 min_bytes));
92}
93
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +010094} // namespace
95
Jonas Olssone0960042019-03-12 13:49:26 +010096ProbeControllerConfig::ProbeControllerConfig(
97 const WebRtcKeyValueConfig* key_value_config)
Jonas Olsson01d36182019-03-27 15:57:02 +010098 : first_exponential_probe_scale("p1", 3.0),
99 second_exponential_probe_scale("p2", 6.0),
100 further_exponential_probe_scale("step_size", 2),
Jonas Olssone0960042019-03-12 13:49:26 +0100101 further_probe_threshold("further_probe_threshold", 0.7),
Jonas Olsson01d36182019-03-27 15:57:02 +0100102 alr_probing_interval("alr_interval", TimeDelta::seconds(5)),
103 alr_probe_scale("alr_scale", 2),
104 first_allocation_probe_scale("alloc_p1", 1),
105 second_allocation_probe_scale("alloc_p2", 2),
106 allocation_allow_further_probing("alloc_probe_further", false) {
Jonas Olssone0960042019-03-12 13:49:26 +0100107 ParseFieldTrial(
Jonas Olsson01d36182019-03-27 15:57:02 +0100108 {&first_exponential_probe_scale, &second_exponential_probe_scale,
109 &further_exponential_probe_scale, &further_probe_threshold,
110 &alr_probing_interval, &alr_probe_scale, &first_allocation_probe_scale,
111 &second_allocation_probe_scale, &allocation_allow_further_probing},
Jonas Olsson24923e82019-03-27 14:19:04 +0100112 key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
Jonas Olsson2e8d78c2019-05-27 13:31:45 +0200113
114 // Specialized keys overriding subsets of WebRTC-Bwe-ProbingConfiguration
115 ParseFieldTrial(
116 {&first_exponential_probe_scale, &second_exponential_probe_scale},
117 key_value_config->Lookup("WebRTC-Bwe-InitialProbing"));
118 ParseFieldTrial({&further_exponential_probe_scale, &further_probe_threshold},
119 key_value_config->Lookup("WebRTC-Bwe-ExponentialProbing"));
120 ParseFieldTrial({&alr_probing_interval, &alr_probe_scale},
121 key_value_config->Lookup("WebRTC-Bwe-AlrProbing"));
122 ParseFieldTrial(
123 {&first_allocation_probe_scale, &second_allocation_probe_scale,
124 &allocation_allow_further_probing},
125 key_value_config->Lookup("WebRTC-Bwe-AllocationProbing"));
Jonas Olssone0960042019-03-12 13:49:26 +0100126}
127
128ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) =
129 default;
130ProbeControllerConfig::~ProbeControllerConfig() = default;
131
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800132ProbeController::ProbeController(const WebRtcKeyValueConfig* key_value_config,
133 RtcEventLog* event_log)
Erik Språngcfe36ca2018-11-29 17:32:48 +0100134 : enable_periodic_alr_probing_(false),
135 in_rapid_recovery_experiment_(
Sebastian Jansson95edb032019-01-17 16:24:12 +0100136 key_value_config->Lookup(kBweRapidRecoveryExperiment)
137 .find("Enabled") == 0),
Erik Språngcfe36ca2018-11-29 17:32:48 +0100138 limit_probes_with_allocateable_rate_(
Sebastian Jansson95edb032019-01-17 16:24:12 +0100139 key_value_config->Lookup(kCappedProbingFieldTrialName)
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800140 .find("Disabled") != 0),
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200141 allocation_probing_only_in_alr_(
142 key_value_config->Lookup(kAllocProbingOnlyInAlrFieldTrialName)
143 .find("Enabled") == 0),
Jonas Olssone0960042019-03-12 13:49:26 +0100144 event_log_(event_log),
145 config_(ProbeControllerConfig(key_value_config)) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100146 Reset(0);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100147}
148
149ProbeController::~ProbeController() {}
150
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200151std::vector<ProbeClusterConfig> ProbeController::SetBitrates(
152 int64_t min_bitrate_bps,
153 int64_t start_bitrate_bps,
154 int64_t max_bitrate_bps,
155 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100156 if (start_bitrate_bps > 0) {
157 start_bitrate_bps_ = start_bitrate_bps;
158 estimated_bitrate_bps_ = start_bitrate_bps;
159 } else if (start_bitrate_bps_ == 0) {
160 start_bitrate_bps_ = min_bitrate_bps;
161 }
162
163 // The reason we use the variable |old_max_bitrate_pbs| is because we
164 // need to set |max_bitrate_bps_| before we call InitiateProbing.
165 int64_t old_max_bitrate_bps = max_bitrate_bps_;
166 max_bitrate_bps_ = max_bitrate_bps;
167
168 switch (state_) {
169 case State::kInit:
170 if (network_available_)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200171 return InitiateExponentialProbing(at_time_ms);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100172 break;
173
174 case State::kWaitingForProbingResult:
175 break;
176
177 case State::kProbingComplete:
Jonas Olssonf441ea92019-03-05 16:24:48 +0100178 // If the new max bitrate is higher than both the old max bitrate and the
179 // estimate then initiate probing.
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100180 if (estimated_bitrate_bps_ != 0 &&
181 old_max_bitrate_bps < max_bitrate_bps_ &&
182 estimated_bitrate_bps_ < max_bitrate_bps_) {
183 // The assumption is that if we jump more than 20% in the bandwidth
184 // estimate or if the bandwidth estimate is within 90% of the new
185 // max bitrate then the probing attempt was successful.
186 mid_call_probing_succcess_threshold_ =
187 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
188 mid_call_probing_waiting_for_result_ = true;
189 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
190
191 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
192 max_bitrate_bps_ / 1000);
193
Jonas Olssonf441ea92019-03-05 16:24:48 +0100194 return InitiateProbing(at_time_ms, {max_bitrate_bps_}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100195 }
196 break;
197 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200198 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100199}
200
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200201std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
philipeldb4fa4b2018-03-06 18:29:22 +0100202 int64_t max_total_allocated_bitrate,
203 int64_t at_time_ms) {
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200204 const bool in_alr = alr_start_time_ms_.has_value();
205 const bool allow_allocation_probe =
206 allocation_probing_only_in_alr_ ? in_alr : true;
207
philipel9fd6b982018-04-19 16:58:07 +0200208 if (state_ == State::kProbingComplete &&
209 max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
philipel0676f222018-04-17 16:12:21 +0200210 estimated_bitrate_bps_ != 0 &&
211 (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
Konrad Hofbauer25f35a82019-04-10 12:38:06 +0200212 estimated_bitrate_bps_ < max_total_allocated_bitrate &&
213 allow_allocation_probe) {
Sebastian Janssonb2ecc3d2018-07-13 17:22:01 +0200214 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Jonas Olsson01d36182019-03-27 15:57:02 +0100215
216 if (!config_.first_allocation_probe_scale)
217 return std::vector<ProbeClusterConfig>();
218
219 std::vector<int64_t> probes = {
220 static_cast<int64_t>(config_.first_allocation_probe_scale.Value() *
221 max_total_allocated_bitrate)};
222 if (config_.second_allocation_probe_scale) {
223 probes.push_back(config_.second_allocation_probe_scale.Value() *
224 max_total_allocated_bitrate);
225 }
226 return InitiateProbing(at_time_ms, probes,
227 config_.allocation_allow_further_probing);
philipeldb4fa4b2018-03-06 18:29:22 +0100228 }
Sebastian Janssonf5e767d2018-10-15 13:24:31 +0200229 max_total_allocated_bitrate_ = max_total_allocated_bitrate;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200230 return std::vector<ProbeClusterConfig>();
philipeldb4fa4b2018-03-06 18:29:22 +0100231}
232
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200233std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
234 NetworkAvailability msg) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100235 network_available_ = msg.network_available;
philipel9fd6b982018-04-19 16:58:07 +0200236
237 if (!network_available_ && state_ == State::kWaitingForProbingResult) {
238 state_ = State::kProbingComplete;
239 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
240 }
241
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100242 if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200243 return InitiateExponentialProbing(msg.at_time.ms());
244 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100245}
246
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200247std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
248 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100249 RTC_DCHECK(network_available_);
250 RTC_DCHECK(state_ == State::kInit);
251 RTC_DCHECK_GT(start_bitrate_bps_, 0);
252
253 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
254 // 1.2 Mbps to continue probing.
Jonas Olssone0960042019-03-12 13:49:26 +0100255 std::vector<int64_t> probes = {static_cast<int64_t>(
Jonas Olsson01d36182019-03-27 15:57:02 +0100256 config_.first_exponential_probe_scale * start_bitrate_bps_)};
257 if (config_.second_exponential_probe_scale) {
258 probes.push_back(config_.second_exponential_probe_scale.Value() *
Jonas Olssone0960042019-03-12 13:49:26 +0100259 start_bitrate_bps_);
260 }
261 return InitiateProbing(at_time_ms, probes, true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100262}
263
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200264std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
265 int64_t bitrate_bps,
266 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100267 if (mid_call_probing_waiting_for_result_ &&
268 bitrate_bps >= mid_call_probing_succcess_threshold_) {
269 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
270 mid_call_probing_bitrate_bps_ / 1000);
271 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
272 bitrate_bps / 1000);
273 mid_call_probing_waiting_for_result_ = false;
274 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200275 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100276 if (state_ == State::kWaitingForProbingResult) {
277 // Continue probing if probing results indicate channel has greater
278 // capacity.
279 RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
280 << " Minimum to probe further: "
281 << min_bitrate_to_probe_further_bps_;
282
283 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
284 bitrate_bps > min_bitrate_to_probe_further_bps_) {
Jonas Olssone0960042019-03-12 13:49:26 +0100285 pending_probes = InitiateProbing(
286 at_time_ms,
Jonas Olsson01d36182019-03-27 15:57:02 +0100287 {static_cast<int64_t>(config_.further_exponential_probe_scale *
Jonas Olssone0960042019-03-12 13:49:26 +0100288 bitrate_bps)},
289 true);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100290 }
291 }
292
293 if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100294 time_of_last_large_drop_ms_ = at_time_ms;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100295 bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
296 }
297
298 estimated_bitrate_bps_ = bitrate_bps;
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200299 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100300}
301
302void ProbeController::EnablePeriodicAlrProbing(bool enable) {
303 enable_periodic_alr_probing_ = enable;
304}
305
306void ProbeController::SetAlrStartTimeMs(
Danil Chapovalov0040b662018-06-18 10:48:16 +0200307 absl::optional<int64_t> alr_start_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100308 alr_start_time_ms_ = alr_start_time_ms;
309}
310void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
311 alr_end_time_ms_.emplace(alr_end_time_ms);
312}
313
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200314std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
315 int64_t at_time_ms) {
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100316 // Called once we have returned to normal state after a large drop in
317 // estimated bandwidth. The current response is to initiate a single probe
318 // session (if not already probing) at the previous bitrate.
319 //
320 // If the probe session fails, the assumption is that this drop was a
321 // real one from a competing flow or a network change.
322 bool in_alr = alr_start_time_ms_.has_value();
323 bool alr_ended_recently =
324 (alr_end_time_ms_.has_value() &&
325 at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs);
326 if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
327 if (state_ == State::kProbingComplete) {
328 uint32_t suggested_probe_bps =
329 kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_;
330 uint32_t min_expected_probe_result_bps =
331 (1 - kProbeUncertainty) * suggested_probe_bps;
332 int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_;
333 int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_;
334 if (min_expected_probe_result_bps > estimated_bitrate_bps_ &&
335 time_since_drop_ms < kBitrateDropTimeoutMs &&
336 time_since_probe_ms > kMinTimeBetweenAlrProbesMs) {
337 RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
338 // Track how often we probe in response to bandwidth drop in ALR.
339 RTC_HISTOGRAM_COUNTS_10000(
340 "WebRTC.BWE.BweDropProbingIntervalInS",
341 (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100342 last_bwe_drop_probing_time_ms_ = at_time_ms;
Sebastian Janssonc9d0b082019-02-26 15:29:53 +0100343 return InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100344 }
345 }
346 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200347 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100348}
349
Erik Språng791d43c2019-01-08 15:46:06 +0100350void ProbeController::SetMaxBitrate(int64_t max_bitrate_bps) {
351 max_bitrate_bps_ = max_bitrate_bps;
352}
353
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100354void ProbeController::Reset(int64_t at_time_ms) {
355 network_available_ = true;
356 state_ = State::kInit;
357 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
358 time_last_probing_initiated_ms_ = 0;
359 estimated_bitrate_bps_ = 0;
360 start_bitrate_bps_ = 0;
361 max_bitrate_bps_ = 0;
362 int64_t now_ms = at_time_ms;
363 last_bwe_drop_probing_time_ms_ = now_ms;
364 alr_end_time_ms_.reset();
365 mid_call_probing_waiting_for_result_ = false;
366 time_of_last_large_drop_ms_ = now_ms;
367 bitrate_before_last_large_drop_bps_ = 0;
philipel0676f222018-04-17 16:12:21 +0200368 max_total_allocated_bitrate_ = 0;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100369}
370
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200371std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100372 if (at_time_ms - time_last_probing_initiated_ms_ >
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100373 kMaxWaitingTimeForProbingResultMs) {
374 mid_call_probing_waiting_for_result_ = false;
375
376 if (state_ == State::kWaitingForProbingResult) {
377 RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
378 state_ = State::kProbingComplete;
379 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
380 }
381 }
382
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200383 if (enable_periodic_alr_probing_ && state_ == State::kProbingComplete) {
384 // Probe bandwidth periodically when in ALR state.
385 if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) {
386 int64_t next_probe_time_ms =
387 std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) +
Jonas Olsson01d36182019-03-27 15:57:02 +0100388 config_.alr_probing_interval->ms();
Jonas Olssonf441ea92019-03-05 16:24:48 +0100389 if (at_time_ms >= next_probe_time_ms) {
Jonas Olssone0960042019-03-12 13:49:26 +0100390 return InitiateProbing(at_time_ms,
391 {static_cast<int64_t>(estimated_bitrate_bps_ *
Jonas Olsson01d36182019-03-27 15:57:02 +0100392 config_.alr_probe_scale)},
Jonas Olssone0960042019-03-12 13:49:26 +0100393 true);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200394 }
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100395 }
396 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200397 return std::vector<ProbeClusterConfig>();
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100398}
399
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200400std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100401 int64_t now_ms,
Jonas Olssone0960042019-03-12 13:49:26 +0100402 std::vector<int64_t> bitrates_to_probe,
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100403 bool probe_further) {
Jonas Olssonf441ea92019-03-05 16:24:48 +0100404 int64_t max_probe_bitrate_bps =
405 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
406 if (limit_probes_with_allocateable_rate_ &&
407 max_total_allocated_bitrate_ > 0) {
408 // If a max allocated bitrate has been configured, allow probing up to 2x
409 // that rate. This allows some overhead to account for bursty streams,
410 // which otherwise would have to ramp up when the overshoot is already in
411 // progress.
412 // It also avoids minor quality reduction caused by probes often being
413 // received at slightly less than the target probe bitrate.
414 max_probe_bitrate_bps =
415 std::min(max_probe_bitrate_bps, max_total_allocated_bitrate_ * 2);
416 }
417
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200418 std::vector<ProbeClusterConfig> pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100419 for (int64_t bitrate : bitrates_to_probe) {
420 RTC_DCHECK_GT(bitrate, 0);
Jonas Olssonf441ea92019-03-05 16:24:48 +0100421
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100422 if (bitrate > max_probe_bitrate_bps) {
423 bitrate = max_probe_bitrate_bps;
424 probe_further = false;
425 }
426
427 ProbeClusterConfig config;
428 config.at_time = Timestamp::ms(now_ms);
429 config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate));
430 config.target_duration = TimeDelta::ms(kMinProbeDurationMs);
431 config.target_probe_count = kMinProbePacketsSent;
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800432 config.id = next_probe_cluster_id_;
433 next_probe_cluster_id_++;
434 MaybeLogProbeClusterCreated(event_log_, config);
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200435 pending_probes.push_back(config);
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100436 }
437 time_last_probing_initiated_ms_ = now_ms;
438 if (probe_further) {
439 state_ = State::kWaitingForProbingResult;
440 min_bitrate_to_probe_further_bps_ =
Jonas Olssone0960042019-03-12 13:49:26 +0100441 (*(bitrates_to_probe.end() - 1)) * config_.further_probe_threshold;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100442 } else {
443 state_ = State::kProbingComplete;
444 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
445 }
Sebastian Janssonda2ec402018-08-02 16:27:28 +0200446 return pending_probes;
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100447}
448
Sebastian Jansson6bcd7f62018-02-27 17:07:02 +0100449} // namespace webrtc