• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "modules/congestion_controller/goog_cc/probe_controller.h"
12 
13 #include <algorithm>
14 #include <initializer_list>
15 #include <memory>
16 #include <string>
17 
18 #include "absl/strings/match.h"
19 #include "api/units/data_rate.h"
20 #include "api/units/time_delta.h"
21 #include "api/units/timestamp.h"
22 #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/logging.h"
25 #include "rtc_base/numerics/safe_conversions.h"
26 #include "system_wrappers/include/metrics.h"
27 
28 namespace webrtc {
29 
30 namespace {
31 // The minimum number probing packets used.
32 constexpr int kMinProbePacketsSent = 5;
33 
34 // The minimum probing duration in ms.
35 constexpr int kMinProbeDurationMs = 15;
36 
37 // Maximum waiting time from the time of initiating probing to getting
38 // the measured results back.
39 constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
40 
41 // Value of |min_bitrate_to_probe_further_bps_| that indicates
42 // further probing is disabled.
43 constexpr int kExponentialProbingDisabled = 0;
44 
45 // Default probing bitrate limit. Applied only when the application didn't
46 // specify max bitrate.
47 constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
48 
49 // 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.
52 constexpr double kBitrateDropThreshold = 0.66;
53 constexpr int kBitrateDropTimeoutMs = 5000;
54 constexpr 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.
59 constexpr 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.
64 constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000;
65 
66 // bitrate). Used to avoid probing if the probe bitrate is close to our current
67 // estimate.
68 constexpr double kProbeUncertainty = 0.05;
69 
70 // Use probing to recover faster after large bitrate estimate drops.
71 constexpr char kBweRapidRecoveryExperiment[] =
72     "WebRTC-BweRapidRecoveryExperiment";
73 
74 // Never probe higher than configured by OnMaxTotalAllocatedBitrate().
75 constexpr char kCappedProbingFieldTrialName[] = "WebRTC-BweCappedProbing";
76 
MaybeLogProbeClusterCreated(RtcEventLog * event_log,const ProbeClusterConfig & probe)77 void 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);
86   event_log->Log(std::make_unique<RtcEventProbeClusterCreated>(
87       probe.id, probe.target_data_rate.bps(), probe.target_probe_count,
88       min_bytes));
89 }
90 
91 }  // namespace
92 
ProbeControllerConfig(const WebRtcKeyValueConfig * key_value_config)93 ProbeControllerConfig::ProbeControllerConfig(
94     const WebRtcKeyValueConfig* key_value_config)
95     : first_exponential_probe_scale("p1", 3.0),
96       second_exponential_probe_scale("p2", 6.0),
97       further_exponential_probe_scale("step_size", 2),
98       further_probe_threshold("further_probe_threshold", 0.7),
99       alr_probing_interval("alr_interval", TimeDelta::Seconds(5)),
100       alr_probe_scale("alr_scale", 2),
101       first_allocation_probe_scale("alloc_p1", 1),
102       second_allocation_probe_scale("alloc_p2", 2),
103       allocation_allow_further_probing("alloc_probe_further", false),
104       allocation_probe_max("alloc_probe_max", DataRate::PlusInfinity()) {
105   ParseFieldTrial(
106       {&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},
110       key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
111 
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,
122        &allocation_allow_further_probing, &allocation_probe_max},
123       key_value_config->Lookup("WebRTC-Bwe-AllocationProbing"));
124 }
125 
126 ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) =
127     default;
128 ProbeControllerConfig::~ProbeControllerConfig() = default;
129 
ProbeController(const WebRtcKeyValueConfig * key_value_config,RtcEventLog * event_log)130 ProbeController::ProbeController(const WebRtcKeyValueConfig* key_value_config,
131                                  RtcEventLog* event_log)
132     : enable_periodic_alr_probing_(false),
133       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")),
139       event_log_(event_log),
140       config_(ProbeControllerConfig(key_value_config)) {
141   Reset(0);
142 }
143 
~ProbeController()144 ProbeController::~ProbeController() {}
145 
SetBitrates(int64_t min_bitrate_bps,int64_t start_bitrate_bps,int64_t max_bitrate_bps,int64_t at_time_ms)146 std::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) {
151   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_)
166         return InitiateExponentialProbing(at_time_ms);
167       break;
168 
169     case State::kWaitingForProbingResult:
170       break;
171 
172     case State::kProbingComplete:
173       // If the new max bitrate is higher than both the old max bitrate and the
174       // estimate then initiate probing.
175       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 
189         return InitiateProbing(at_time_ms, {max_bitrate_bps_}, false);
190       }
191       break;
192   }
193   return std::vector<ProbeClusterConfig>();
194 }
195 
OnMaxTotalAllocatedBitrate(int64_t max_total_allocated_bitrate,int64_t at_time_ms)196 std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
197     int64_t max_total_allocated_bitrate,
198     int64_t at_time_ms) {
199   const bool in_alr = alr_start_time_ms_.has_value();
200   const bool allow_allocation_probe = in_alr;
201 
202   if (state_ == State::kProbingComplete &&
203       max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
204       estimated_bitrate_bps_ != 0 &&
205       (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) &&
206       estimated_bitrate_bps_ < max_total_allocated_bitrate &&
207       allow_allocation_probe) {
208     max_total_allocated_bitrate_ = max_total_allocated_bitrate;
209 
210     if (!config_.first_allocation_probe_scale)
211       return std::vector<ProbeClusterConfig>();
212 
213     DataRate first_probe_rate =
214         DataRate::BitsPerSec(max_total_allocated_bitrate) *
215         config_.first_allocation_probe_scale.Value();
216     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()};
219     if (config_.second_allocation_probe_scale) {
220       DataRate second_probe_rate =
221           DataRate::BitsPerSec(max_total_allocated_bitrate) *
222           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());
226     }
227     return InitiateProbing(at_time_ms, probes,
228                            config_.allocation_allow_further_probing);
229   }
230   max_total_allocated_bitrate_ = max_total_allocated_bitrate;
231   return std::vector<ProbeClusterConfig>();
232 }
233 
OnNetworkAvailability(NetworkAvailability msg)234 std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
235     NetworkAvailability msg) {
236   network_available_ = msg.network_available;
237 
238   if (!network_available_ && state_ == State::kWaitingForProbingResult) {
239     state_ = State::kProbingComplete;
240     min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
241   }
242 
243   if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0)
244     return InitiateExponentialProbing(msg.at_time.ms());
245   return std::vector<ProbeClusterConfig>();
246 }
247 
InitiateExponentialProbing(int64_t at_time_ms)248 std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
249     int64_t at_time_ms) {
250   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.
256   std::vector<int64_t> probes = {static_cast<int64_t>(
257       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() *
260                      start_bitrate_bps_);
261   }
262   return InitiateProbing(at_time_ms, probes, true);
263 }
264 
SetEstimatedBitrate(int64_t bitrate_bps,int64_t at_time_ms)265 std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
266     int64_t bitrate_bps,
267     int64_t at_time_ms) {
268   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   }
276   std::vector<ProbeClusterConfig> pending_probes;
277   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_) {
286       pending_probes = InitiateProbing(
287           at_time_ms,
288           {static_cast<int64_t>(config_.further_exponential_probe_scale *
289                                 bitrate_bps)},
290           true);
291     }
292   }
293 
294   if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) {
295     time_of_last_large_drop_ms_ = at_time_ms;
296     bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_;
297   }
298 
299   estimated_bitrate_bps_ = bitrate_bps;
300   return pending_probes;
301 }
302 
EnablePeriodicAlrProbing(bool enable)303 void ProbeController::EnablePeriodicAlrProbing(bool enable) {
304   enable_periodic_alr_probing_ = enable;
305 }
306 
SetAlrStartTimeMs(absl::optional<int64_t> alr_start_time_ms)307 void ProbeController::SetAlrStartTimeMs(
308     absl::optional<int64_t> alr_start_time_ms) {
309   alr_start_time_ms_ = alr_start_time_ms;
310 }
SetAlrEndedTimeMs(int64_t alr_end_time_ms)311 void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) {
312   alr_end_time_ms_.emplace(alr_end_time_ms);
313 }
314 
RequestProbe(int64_t at_time_ms)315 std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
316     int64_t at_time_ms) {
317   // 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);
343         last_bwe_drop_probing_time_ms_ = at_time_ms;
344         return InitiateProbing(at_time_ms, {suggested_probe_bps}, false);
345       }
346     }
347   }
348   return std::vector<ProbeClusterConfig>();
349 }
350 
SetMaxBitrate(int64_t max_bitrate_bps)351 void ProbeController::SetMaxBitrate(int64_t max_bitrate_bps) {
352   max_bitrate_bps_ = max_bitrate_bps;
353 }
354 
Reset(int64_t at_time_ms)355 void 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;
369   max_total_allocated_bitrate_ = 0;
370 }
371 
Process(int64_t at_time_ms)372 std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) {
373   if (at_time_ms - time_last_probing_initiated_ms_ >
374       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 
384   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_) +
389           config_.alr_probing_interval->ms();
390       if (at_time_ms >= next_probe_time_ms) {
391         return InitiateProbing(at_time_ms,
392                                {static_cast<int64_t>(estimated_bitrate_bps_ *
393                                                      config_.alr_probe_scale)},
394                                true);
395       }
396     }
397   }
398   return std::vector<ProbeClusterConfig>();
399 }
400 
InitiateProbing(int64_t now_ms,std::vector<int64_t> bitrates_to_probe,bool probe_further)401 std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
402     int64_t now_ms,
403     std::vector<int64_t> bitrates_to_probe,
404     bool probe_further) {
405   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 
419   std::vector<ProbeClusterConfig> pending_probes;
420   for (int64_t bitrate : bitrates_to_probe) {
421     RTC_DCHECK_GT(bitrate, 0);
422 
423     if (bitrate > max_probe_bitrate_bps) {
424       bitrate = max_probe_bitrate_bps;
425       probe_further = false;
426     }
427 
428     ProbeClusterConfig config;
429     config.at_time = Timestamp::Millis(now_ms);
430     config.target_data_rate =
431         DataRate::BitsPerSec(rtc::dchecked_cast<int>(bitrate));
432     config.target_duration = TimeDelta::Millis(kMinProbeDurationMs);
433     config.target_probe_count = kMinProbePacketsSent;
434     config.id = next_probe_cluster_id_;
435     next_probe_cluster_id_++;
436     MaybeLogProbeClusterCreated(event_log_, config);
437     pending_probes.push_back(config);
438   }
439   time_last_probing_initiated_ms_ = now_ms;
440   if (probe_further) {
441     state_ = State::kWaitingForProbingResult;
442     min_bitrate_to_probe_further_bps_ =
443         (*(bitrates_to_probe.end() - 1)) * config_.further_probe_threshold;
444   } else {
445     state_ = State::kProbingComplete;
446     min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
447   }
448   return pending_probes;
449 }
450 
451 }  // namespace webrtc
452