tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

probe_controller.cc (25503B)


      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 <optional>
     17 #include <vector>
     18 
     19 #include "api/field_trials_view.h"
     20 #include "api/rtc_event_log/rtc_event_log.h"
     21 #include "api/transport/network_types.h"
     22 #include "api/units/data_rate.h"
     23 #include "api/units/data_size.h"
     24 #include "api/units/time_delta.h"
     25 #include "api/units/timestamp.h"
     26 #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
     27 #include "rtc_base/checks.h"
     28 #include "rtc_base/experiments/field_trial_parser.h"
     29 #include "rtc_base/logging.h"
     30 #include "system_wrappers/include/metrics.h"
     31 
     32 namespace webrtc {
     33 
     34 namespace {
     35 // Maximum waiting time from the time of initiating probing to getting
     36 // the measured results back.
     37 constexpr TimeDelta kMaxWaitingTimeForProbingResult = TimeDelta::Seconds(1);
     38 
     39 // Default probing bitrate limit. Applied only when the application didn't
     40 // specify max bitrate.
     41 constexpr DataRate kDefaultMaxProbingBitrate = DataRate::KilobitsPerSec(5000);
     42 
     43 // If the bitrate drops to a factor `kBitrateDropThreshold` or lower
     44 // and we recover within `kBitrateDropTimeoutMs`, then we'll send
     45 // a probe at a fraction `kProbeFractionAfterDrop` of the original bitrate.
     46 constexpr double kBitrateDropThreshold = 0.66;
     47 constexpr TimeDelta kBitrateDropTimeout = TimeDelta::Seconds(5);
     48 constexpr double kProbeFractionAfterDrop = 0.85;
     49 
     50 // Timeout for probing after leaving ALR. If the bitrate drops significantly,
     51 // (as determined by the delay based estimator) and we leave ALR, then we will
     52 // send a probe if we recover within `kLeftAlrTimeoutMs` ms.
     53 constexpr TimeDelta kAlrEndedTimeout = TimeDelta::Seconds(3);
     54 
     55 // The expected uncertainty of probe result (as a fraction of the target probe
     56 // This is a limit on how often probing can be done when there is a BW
     57 // drop detected in ALR.
     58 constexpr TimeDelta kMinTimeBetweenAlrProbes = TimeDelta::Seconds(5);
     59 
     60 // bitrate). Used to avoid probing if the probe bitrate is close to our current
     61 // estimate.
     62 constexpr double kProbeUncertainty = 0.05;
     63 
     64 // Use probing to recover faster after large bitrate estimate drops.
     65 constexpr char kBweRapidRecoveryExperiment[] =
     66    "WebRTC-BweRapidRecoveryExperiment";
     67 
     68 void MaybeLogProbeClusterCreated(RtcEventLog* event_log,
     69                                 const ProbeClusterConfig& probe) {
     70  RTC_DCHECK(event_log);
     71  if (!event_log) {
     72    return;
     73  }
     74 
     75  DataSize min_data_size = probe.target_data_rate * probe.target_duration;
     76  event_log->Log(std::make_unique<RtcEventProbeClusterCreated>(
     77      probe.id, probe.target_data_rate.bps(), probe.target_probe_count,
     78      min_data_size.bytes()));
     79 }
     80 
     81 }  // namespace
     82 
     83 ProbeControllerConfig::ProbeControllerConfig(
     84    const FieldTrialsView* key_value_config)
     85    : first_exponential_probe_scale("p1", 3.0),
     86      second_exponential_probe_scale("p2", 6.0),
     87      further_exponential_probe_scale("step_size", 2),
     88      further_probe_threshold("further_probe_threshold", 0.7),
     89      abort_further_probe_if_max_lower_than_current("abort_further", false),
     90      repeated_initial_probing_time_period("initial_probing",
     91                                           TimeDelta::Seconds(5)),
     92      initial_probe_duration("initial_probe_duration", TimeDelta::Millis(100)),
     93      initial_min_probe_delta("initial_min_probe_delta", TimeDelta::Millis(20)),
     94      alr_probing_interval("alr_interval", TimeDelta::Seconds(5)),
     95      alr_probe_scale("alr_scale", 2),
     96      network_state_estimate_probing_interval("network_state_interval",
     97                                              TimeDelta::PlusInfinity()),
     98      probe_if_estimate_lower_than_network_state_estimate_ratio(
     99          "est_lower_than_network_ratio",
    100          0),
    101      estimate_lower_than_network_state_estimate_probing_interval(
    102          "est_lower_than_network_interval",
    103          TimeDelta::Seconds(3)),
    104      network_state_probe_scale("network_state_scale", 1.0),
    105      network_state_probe_duration("network_state_probe_duration",
    106                                   TimeDelta::Millis(15)),
    107      network_state_min_probe_delta("network_state_min_probe_delta",
    108                                    TimeDelta::Millis(20)),
    109      probe_on_max_allocated_bitrate_change("probe_max_allocation", true),
    110      first_allocation_probe_scale("alloc_p1", 1),
    111      second_allocation_probe_scale("alloc_p2", 2),
    112      allocation_probe_limit_by_current_scale("alloc_current_bwe_limit", 2),
    113      min_probe_packets_sent("min_probe_packets_sent", 5),
    114      min_probe_duration("min_probe_duration", TimeDelta::Millis(15)),
    115      min_probe_delta("min_probe_delta", TimeDelta::Millis(2)),
    116      loss_limited_probe_scale("loss_limited_scale", 1.5),
    117      skip_if_estimate_larger_than_fraction_of_max(
    118          "skip_if_est_larger_than_fraction_of_max",
    119          0.0),
    120      skip_probe_max_allocated_scale("skip_max_allocated_scale", 1.0) {
    121  ParseFieldTrial({&first_exponential_probe_scale,
    122                   &second_exponential_probe_scale,
    123                   &further_exponential_probe_scale,
    124                   &further_probe_threshold,
    125                   &abort_further_probe_if_max_lower_than_current,
    126                   &repeated_initial_probing_time_period,
    127                   &initial_probe_duration,
    128                   &alr_probing_interval,
    129                   &alr_probe_scale,
    130                   &probe_on_max_allocated_bitrate_change,
    131                   &first_allocation_probe_scale,
    132                   &second_allocation_probe_scale,
    133                   &allocation_probe_limit_by_current_scale,
    134                   &min_probe_duration,
    135                   &min_probe_delta,
    136                   &initial_min_probe_delta,
    137                   &network_state_estimate_probing_interval,
    138                   &network_state_min_probe_delta,
    139                   &probe_if_estimate_lower_than_network_state_estimate_ratio,
    140                   &estimate_lower_than_network_state_estimate_probing_interval,
    141                   &network_state_probe_scale,
    142                   &network_state_probe_duration,
    143                   &min_probe_packets_sent,
    144                   &loss_limited_probe_scale,
    145                   &skip_if_estimate_larger_than_fraction_of_max,
    146                   &skip_probe_max_allocated_scale},
    147                  key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
    148 
    149  // Specialized keys overriding subsets of WebRTC-Bwe-ProbingConfiguration
    150  ParseFieldTrial(
    151      {&first_exponential_probe_scale, &second_exponential_probe_scale},
    152      key_value_config->Lookup("WebRTC-Bwe-InitialProbing"));
    153  ParseFieldTrial({&further_exponential_probe_scale, &further_probe_threshold},
    154                  key_value_config->Lookup("WebRTC-Bwe-ExponentialProbing"));
    155  ParseFieldTrial(
    156      {&alr_probing_interval, &alr_probe_scale, &loss_limited_probe_scale},
    157      key_value_config->Lookup("WebRTC-Bwe-AlrProbing"));
    158  ParseFieldTrial(
    159      {&first_allocation_probe_scale, &second_allocation_probe_scale,
    160       &allocation_probe_limit_by_current_scale},
    161      key_value_config->Lookup("WebRTC-Bwe-AllocationProbing"));
    162  ParseFieldTrial({&min_probe_packets_sent, &min_probe_duration},
    163                  key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior"));
    164 }
    165 
    166 ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) =
    167    default;
    168 ProbeControllerConfig::~ProbeControllerConfig() = default;
    169 
    170 ProbeController::ProbeController(const FieldTrialsView* key_value_config,
    171                                 RtcEventLog* event_log)
    172    : network_available_(false),
    173      enable_periodic_alr_probing_(false),
    174      in_rapid_recovery_experiment_(
    175          key_value_config->IsEnabled(kBweRapidRecoveryExperiment)),
    176      event_log_(event_log),
    177      config_(ProbeControllerConfig(key_value_config)) {
    178  Reset(Timestamp::Zero());
    179 }
    180 
    181 ProbeController::~ProbeController() {}
    182 
    183 std::vector<ProbeClusterConfig> ProbeController::SetBitrates(
    184    DataRate min_bitrate,
    185    DataRate start_bitrate,
    186    DataRate max_bitrate,
    187    Timestamp at_time) {
    188  if (start_bitrate > DataRate::Zero()) {
    189    start_bitrate_ = start_bitrate;
    190    estimated_bitrate_ = start_bitrate;
    191  } else if (start_bitrate_.IsZero()) {
    192    start_bitrate_ = min_bitrate;
    193  }
    194 
    195  // The reason we use the variable `old_max_bitrate_pbs` is because we
    196  // need to set `max_bitrate_` before we call InitiateProbing.
    197  DataRate old_max_bitrate = max_bitrate_;
    198  max_bitrate_ =
    199      max_bitrate.IsFinite() ? max_bitrate : kDefaultMaxProbingBitrate;
    200 
    201  switch (state_) {
    202    case State::kInit:
    203      if (network_available_)
    204        return InitiateExponentialProbing(at_time);
    205      break;
    206 
    207    case State::kWaitingForProbingResult:
    208      break;
    209 
    210    case State::kProbingComplete:
    211      // If the new max bitrate is higher than both the old max bitrate and the
    212      // estimate then initiate probing.
    213      if (!estimated_bitrate_.IsZero() && old_max_bitrate < max_bitrate_ &&
    214          estimated_bitrate_ < max_bitrate_) {
    215        return InitiateProbing(at_time, {max_bitrate_}, false);
    216      }
    217      break;
    218  }
    219  return std::vector<ProbeClusterConfig>();
    220 }
    221 
    222 std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate(
    223    DataRate max_total_allocated_bitrate,
    224    Timestamp at_time) {
    225  const bool in_alr = alr_start_time_.has_value();
    226  const bool allow_allocation_probe = in_alr;
    227  if (config_.probe_on_max_allocated_bitrate_change &&
    228      state_ == State::kProbingComplete &&
    229      max_total_allocated_bitrate != max_total_allocated_bitrate_ &&
    230      estimated_bitrate_ < max_bitrate_ &&
    231      estimated_bitrate_ < max_total_allocated_bitrate &&
    232      allow_allocation_probe) {
    233    max_total_allocated_bitrate_ = max_total_allocated_bitrate;
    234 
    235    if (!config_.first_allocation_probe_scale)
    236      return std::vector<ProbeClusterConfig>();
    237 
    238    DataRate first_probe_rate = max_total_allocated_bitrate *
    239                                config_.first_allocation_probe_scale.Value();
    240    const DataRate current_bwe_limit =
    241        config_.allocation_probe_limit_by_current_scale.Get() *
    242        estimated_bitrate_;
    243    bool limited_by_current_bwe = current_bwe_limit < first_probe_rate;
    244    if (limited_by_current_bwe) {
    245      first_probe_rate = current_bwe_limit;
    246    }
    247 
    248    std::vector<DataRate> probes = {first_probe_rate};
    249    if (!limited_by_current_bwe && config_.second_allocation_probe_scale) {
    250      DataRate second_probe_rate =
    251          max_total_allocated_bitrate *
    252          config_.second_allocation_probe_scale.Value();
    253      limited_by_current_bwe = current_bwe_limit < second_probe_rate;
    254      if (limited_by_current_bwe) {
    255        second_probe_rate = current_bwe_limit;
    256      }
    257      if (second_probe_rate > first_probe_rate)
    258        probes.push_back(second_probe_rate);
    259    }
    260    bool allow_further_probing = limited_by_current_bwe;
    261 
    262    return InitiateProbing(at_time, probes, allow_further_probing);
    263  }
    264  if (!max_total_allocated_bitrate.IsZero()) {
    265    last_allowed_repeated_initial_probe_ = at_time;
    266  }
    267 
    268  max_total_allocated_bitrate_ = max_total_allocated_bitrate;
    269  return std::vector<ProbeClusterConfig>();
    270 }
    271 
    272 std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability(
    273    NetworkAvailability msg) {
    274  network_available_ = msg.network_available;
    275 
    276  if (!network_available_ && state_ == State::kWaitingForProbingResult) {
    277    state_ = State::kProbingComplete;
    278    min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
    279  }
    280 
    281  if (network_available_ && state_ == State::kInit && !start_bitrate_.IsZero())
    282    return InitiateExponentialProbing(msg.at_time);
    283  return std::vector<ProbeClusterConfig>();
    284 }
    285 
    286 void ProbeController::UpdateState(State new_state) {
    287  switch (new_state) {
    288    case State::kInit:
    289      state_ = State::kInit;
    290      break;
    291    case State::kWaitingForProbingResult:
    292      state_ = State::kWaitingForProbingResult;
    293      break;
    294    case State::kProbingComplete:
    295      state_ = State::kProbingComplete;
    296      min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
    297      break;
    298  }
    299 }
    300 
    301 std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing(
    302    Timestamp at_time) {
    303  RTC_DCHECK(network_available_);
    304  RTC_DCHECK(state_ == State::kInit);
    305  RTC_DCHECK_GT(start_bitrate_, DataRate::Zero());
    306 
    307  // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
    308  // 1.2 Mbps to continue probing.
    309  std::vector<DataRate> probes = {config_.first_exponential_probe_scale *
    310                                  start_bitrate_};
    311  if (config_.second_exponential_probe_scale &&
    312      config_.second_exponential_probe_scale.GetOptional().value() > 0) {
    313    probes.push_back(config_.second_exponential_probe_scale.Value() *
    314                     start_bitrate_);
    315  }
    316  if (repeated_initial_probing_enabled_ &&
    317      max_total_allocated_bitrate_.IsZero()) {
    318    last_allowed_repeated_initial_probe_ =
    319        at_time + config_.repeated_initial_probing_time_period;
    320    RTC_LOG(LS_INFO) << "Repeated initial probing enabled, last allowed probe: "
    321                     << last_allowed_repeated_initial_probe_
    322                     << " now: " << at_time;
    323  }
    324 
    325  return InitiateProbing(at_time, probes, true);
    326 }
    327 
    328 std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate(
    329    DataRate bitrate,
    330    BandwidthLimitedCause bandwidth_limited_cause,
    331    Timestamp at_time) {
    332  bandwidth_limited_cause_ = bandwidth_limited_cause;
    333  if (bitrate < kBitrateDropThreshold * estimated_bitrate_) {
    334    time_of_last_large_drop_ = at_time;
    335    bitrate_before_last_large_drop_ = estimated_bitrate_;
    336  }
    337  estimated_bitrate_ = bitrate;
    338 
    339  if (state_ == State::kWaitingForProbingResult) {
    340    // Continue probing if probing results indicate channel has greater
    341    // capacity unless we already reached the needed bitrate.
    342    if (config_.abort_further_probe_if_max_lower_than_current &&
    343        (bitrate > max_bitrate_ ||
    344         (!max_total_allocated_bitrate_.IsZero() &&
    345          bitrate > 2 * max_total_allocated_bitrate_))) {
    346      // No need to continue probing.
    347      min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
    348    }
    349    DataRate network_state_estimate_probe_further_limit =
    350        config_.network_state_estimate_probing_interval->IsFinite() &&
    351                network_estimate_ &&
    352                network_estimate_->link_capacity_upper.IsFinite()
    353            ? network_estimate_->link_capacity_upper *
    354                  config_.further_probe_threshold
    355            : DataRate::PlusInfinity();
    356    RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate
    357                     << " Minimum to probe further: "
    358                     << min_bitrate_to_probe_further_ << " upper limit: "
    359                     << network_state_estimate_probe_further_limit;
    360 
    361    if (bitrate > min_bitrate_to_probe_further_ &&
    362        bitrate <= network_state_estimate_probe_further_limit) {
    363      return InitiateProbing(
    364          at_time, {config_.further_exponential_probe_scale * bitrate}, true);
    365    }
    366  }
    367  return {};
    368 }
    369 
    370 void ProbeController::EnablePeriodicAlrProbing(bool enable) {
    371  enable_periodic_alr_probing_ = enable;
    372 }
    373 
    374 void ProbeController::EnableRepeatedInitialProbing(bool enable) {
    375  repeated_initial_probing_enabled_ = enable;
    376 }
    377 
    378 void ProbeController::SetAlrStartTime(std::optional<Timestamp> alr_start_time) {
    379  alr_start_time_ = alr_start_time;
    380 }
    381 void ProbeController::SetAlrEndedTime(Timestamp alr_end_time) {
    382  alr_end_time_ = alr_end_time;
    383 }
    384 
    385 std::vector<ProbeClusterConfig> ProbeController::RequestProbe(
    386    Timestamp at_time) {
    387  // Called once we have returned to normal state after a large drop in
    388  // estimated bandwidth. The current response is to initiate a single probe
    389  // session (if not already probing) at the previous bitrate.
    390  //
    391  // If the probe session fails, the assumption is that this drop was a
    392  // real one from a competing flow or a network change.
    393  bool in_alr = alr_start_time_.has_value();
    394  bool alr_ended_recently =
    395      (alr_end_time_.has_value() &&
    396       at_time - alr_end_time_.value() < kAlrEndedTimeout);
    397  if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) {
    398    if (state_ == State::kProbingComplete) {
    399      DataRate suggested_probe =
    400          kProbeFractionAfterDrop * bitrate_before_last_large_drop_;
    401      DataRate min_expected_probe_result =
    402          (1 - kProbeUncertainty) * suggested_probe;
    403      TimeDelta time_since_drop = at_time - time_of_last_large_drop_;
    404      TimeDelta time_since_probe = at_time - last_bwe_drop_probing_time_;
    405      if (min_expected_probe_result > estimated_bitrate_ &&
    406          time_since_drop < kBitrateDropTimeout &&
    407          time_since_probe > kMinTimeBetweenAlrProbes) {
    408        RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing.";
    409        // Track how often we probe in response to bandwidth drop in ALR.
    410        RTC_HISTOGRAM_COUNTS_10000(
    411            "WebRTC.BWE.BweDropProbingIntervalInS",
    412            (at_time - last_bwe_drop_probing_time_).seconds());
    413        last_bwe_drop_probing_time_ = at_time;
    414        return InitiateProbing(at_time, {suggested_probe}, false);
    415      }
    416    }
    417  }
    418  return std::vector<ProbeClusterConfig>();
    419 }
    420 
    421 void ProbeController::SetNetworkStateEstimate(NetworkStateEstimate estimate) {
    422  network_estimate_ = estimate;
    423 }
    424 
    425 void ProbeController::Reset(Timestamp at_time) {
    426  bandwidth_limited_cause_ = BandwidthLimitedCause::kDelayBasedLimited;
    427  state_ = State::kInit;
    428  min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
    429  time_last_probing_initiated_ = Timestamp::Zero();
    430  estimated_bitrate_ = DataRate::Zero();
    431  network_estimate_ = std::nullopt;
    432  start_bitrate_ = DataRate::Zero();
    433  max_bitrate_ = kDefaultMaxProbingBitrate;
    434  Timestamp now = at_time;
    435  last_bwe_drop_probing_time_ = now;
    436  alr_end_time_.reset();
    437  time_of_last_large_drop_ = now;
    438  bitrate_before_last_large_drop_ = DataRate::Zero();
    439 }
    440 
    441 bool ProbeController::TimeForAlrProbe(Timestamp at_time) const {
    442  if (enable_periodic_alr_probing_ && alr_start_time_) {
    443    Timestamp next_probe_time =
    444        std::max(*alr_start_time_, time_last_probing_initiated_) +
    445        config_.alr_probing_interval;
    446    return at_time >= next_probe_time;
    447  }
    448  return false;
    449 }
    450 
    451 bool ProbeController::TimeForNetworkStateProbe(Timestamp at_time) const {
    452  if (!network_estimate_ ||
    453      network_estimate_->link_capacity_upper.IsInfinite()) {
    454    return false;
    455  }
    456 
    457  bool probe_due_to_low_estimate =
    458      bandwidth_limited_cause_ == BandwidthLimitedCause::kDelayBasedLimited &&
    459      estimated_bitrate_ <
    460          config_.probe_if_estimate_lower_than_network_state_estimate_ratio *
    461              network_estimate_->link_capacity_upper;
    462  if (probe_due_to_low_estimate &&
    463      config_.estimate_lower_than_network_state_estimate_probing_interval
    464          ->IsFinite()) {
    465    Timestamp next_probe_time =
    466        time_last_probing_initiated_ +
    467        config_.estimate_lower_than_network_state_estimate_probing_interval;
    468    return at_time >= next_probe_time;
    469  }
    470 
    471  bool periodic_probe =
    472      estimated_bitrate_ < network_estimate_->link_capacity_upper;
    473  if (periodic_probe &&
    474      config_.network_state_estimate_probing_interval->IsFinite()) {
    475    Timestamp next_probe_time = time_last_probing_initiated_ +
    476                                config_.network_state_estimate_probing_interval;
    477    return at_time >= next_probe_time;
    478  }
    479 
    480  return false;
    481 }
    482 
    483 bool ProbeController::TimeForNextRepeatedInitialProbe(Timestamp at_time) const {
    484  if (state_ != State::kWaitingForProbingResult &&
    485      last_allowed_repeated_initial_probe_ > at_time) {
    486    Timestamp next_probe_time =
    487        time_last_probing_initiated_ + kMaxWaitingTimeForProbingResult;
    488    if (at_time >= next_probe_time) {
    489      return true;
    490    }
    491  }
    492  return false;
    493 }
    494 
    495 std::vector<ProbeClusterConfig> ProbeController::Process(Timestamp at_time) {
    496  if (at_time - time_last_probing_initiated_ >
    497      kMaxWaitingTimeForProbingResult) {
    498    if (state_ == State::kWaitingForProbingResult) {
    499      RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
    500      UpdateState(State::kProbingComplete);
    501    }
    502  }
    503  if (estimated_bitrate_.IsZero() || state_ != State::kProbingComplete) {
    504    return {};
    505  }
    506  if (TimeForNextRepeatedInitialProbe(at_time)) {
    507    return InitiateProbing(
    508        at_time, {estimated_bitrate_ * config_.first_exponential_probe_scale},
    509        true);
    510  }
    511  if (TimeForAlrProbe(at_time) || TimeForNetworkStateProbe(at_time)) {
    512    return InitiateProbing(
    513        at_time, {estimated_bitrate_ * config_.alr_probe_scale}, true);
    514  }
    515  return std::vector<ProbeClusterConfig>();
    516 }
    517 
    518 ProbeClusterConfig ProbeController::CreateProbeClusterConfig(Timestamp at_time,
    519                                                             DataRate bitrate) {
    520  ProbeClusterConfig config;
    521  config.at_time = at_time;
    522  config.target_data_rate = bitrate;
    523  if (network_estimate_ &&
    524      config_.network_state_estimate_probing_interval->IsFinite() &&
    525      network_estimate_->link_capacity_upper.IsFinite() &&
    526      network_estimate_->link_capacity_upper >= bitrate) {
    527    config.target_duration = config_.network_state_probe_duration;
    528    config.min_probe_delta = config_.network_state_min_probe_delta;
    529  } else if (at_time < last_allowed_repeated_initial_probe_) {
    530    config.target_duration = config_.initial_probe_duration;
    531    config.min_probe_delta = config_.initial_min_probe_delta;
    532  } else {
    533    config.target_duration = config_.min_probe_duration;
    534    config.min_probe_delta = config_.min_probe_delta;
    535  }
    536  config.target_probe_count = config_.min_probe_packets_sent;
    537  config.id = next_probe_cluster_id_;
    538  next_probe_cluster_id_++;
    539  MaybeLogProbeClusterCreated(event_log_, config);
    540  return config;
    541 }
    542 
    543 std::vector<ProbeClusterConfig> ProbeController::InitiateProbing(
    544    Timestamp now,
    545    std::vector<DataRate> bitrates_to_probe,
    546    bool probe_further) {
    547  if (config_.skip_if_estimate_larger_than_fraction_of_max > 0) {
    548    DataRate network_estimate = network_estimate_
    549                                    ? network_estimate_->link_capacity_upper
    550                                    : DataRate::PlusInfinity();
    551    DataRate max_probe_rate =
    552        max_total_allocated_bitrate_.IsZero()
    553            ? max_bitrate_
    554            : std::min(config_.skip_probe_max_allocated_scale *
    555                           max_total_allocated_bitrate_,
    556                       max_bitrate_);
    557    if (std::min(network_estimate, estimated_bitrate_) >
    558        config_.skip_if_estimate_larger_than_fraction_of_max * max_probe_rate) {
    559      UpdateState(State::kProbingComplete);
    560      return {};
    561    }
    562  }
    563 
    564  DataRate max_probe_bitrate = max_bitrate_;
    565  if (max_total_allocated_bitrate_ > DataRate::Zero()) {
    566    // If a max allocated bitrate has been configured, allow probing up to 2x
    567    // that rate. This allows some overhead to account for bursty streams,
    568    // which otherwise would have to ramp up when the overshoot is already in
    569    // progress.
    570    // It also avoids minor quality reduction caused by probes often being
    571    // received at slightly less than the target probe bitrate.
    572    max_probe_bitrate =
    573        std::min(max_probe_bitrate, max_total_allocated_bitrate_ * 2);
    574  }
    575 
    576  switch (bandwidth_limited_cause_) {
    577    case BandwidthLimitedCause::kRttBasedBackOffHighRtt:
    578    case BandwidthLimitedCause::kDelayBasedLimitedDelayIncreased:
    579    case BandwidthLimitedCause::kLossLimitedBwe:
    580      RTC_LOG(LS_INFO) << "Not sending probe in bandwidth limited state. "
    581                       << static_cast<int>(bandwidth_limited_cause_);
    582      return {};
    583    case BandwidthLimitedCause::kLossLimitedBweIncreasing:
    584      max_probe_bitrate =
    585          std::min(max_probe_bitrate,
    586                   estimated_bitrate_ * config_.loss_limited_probe_scale);
    587      break;
    588    case BandwidthLimitedCause::kDelayBasedLimited:
    589      break;
    590    default:
    591      break;
    592  }
    593 
    594  if (config_.network_state_estimate_probing_interval->IsFinite() &&
    595      network_estimate_ && network_estimate_->link_capacity_upper.IsFinite()) {
    596    if (network_estimate_->link_capacity_upper.IsZero()) {
    597      RTC_LOG(LS_INFO) << "Not sending probe, Network state estimate is zero";
    598      return {};
    599    }
    600    max_probe_bitrate = std::min(
    601        {max_probe_bitrate,
    602         std::max(estimated_bitrate_, network_estimate_->link_capacity_upper *
    603                                          config_.network_state_probe_scale)});
    604  }
    605 
    606  std::vector<ProbeClusterConfig> pending_probes;
    607  for (DataRate bitrate : bitrates_to_probe) {
    608    RTC_DCHECK(!bitrate.IsZero());
    609    if (bitrate >= max_probe_bitrate) {
    610      bitrate = max_probe_bitrate;
    611      probe_further = false;
    612    }
    613    pending_probes.push_back(CreateProbeClusterConfig(now, bitrate));
    614  }
    615  time_last_probing_initiated_ = now;
    616  if (probe_further) {
    617    UpdateState(State::kWaitingForProbingResult);
    618    // Dont expect probe results to be larger than a fraction of the actual
    619    // probe rate.
    620    min_bitrate_to_probe_further_ = pending_probes.back().target_data_rate *
    621                                    config_.further_probe_threshold;
    622  } else {
    623    UpdateState(State::kProbingComplete);
    624  }
    625  return pending_probes;
    626 }
    627 
    628 }  // namespace webrtc