Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | fc7ec8e | 2018-02-28 16:48:00 +0100 | [diff] [blame] | 11 | #include "modules/congestion_controller/goog_cc/probe_controller.h" |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <initializer_list> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <string> |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 17 | |
Mirko Bonadei | 57cabed | 2020-04-01 12:03:11 +0200 | [diff] [blame] | 18 | #include "absl/strings/match.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/units/data_rate.h" |
| 20 | #include "api/units/time_delta.h" |
| 21 | #include "api/units/timestamp.h" |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 22 | #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | |
Erik Språng | cfe36ca | 2018-11-29 17:32:48 +0100 | [diff] [blame] | 74 | // Never probe higher than configured by OnMaxTotalAllocatedBitrate(). |
| 75 | constexpr char kCappedProbingFieldTrialName[] = "WebRTC-BweCappedProbing"; |
| 76 | |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 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); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 86 | event_log->Log(std::make_unique<RtcEventProbeClusterCreated>( |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 87 | probe.id, probe.target_data_rate.bps(), probe.target_probe_count, |
| 88 | min_bytes)); |
| 89 | } |
| 90 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 91 | } // namespace |
| 92 | |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 93 | ProbeControllerConfig::ProbeControllerConfig( |
| 94 | const WebRtcKeyValueConfig* key_value_config) |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 95 | : first_exponential_probe_scale("p1", 3.0), |
| 96 | second_exponential_probe_scale("p2", 6.0), |
| 97 | further_exponential_probe_scale("step_size", 2), |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 98 | further_probe_threshold("further_probe_threshold", 0.7), |
Danil Chapovalov | 5528402 | 2020-02-07 14:53:52 +0100 | [diff] [blame] | 99 | alr_probing_interval("alr_interval", TimeDelta::Seconds(5)), |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 100 | alr_probe_scale("alr_scale", 2), |
| 101 | first_allocation_probe_scale("alloc_p1", 1), |
| 102 | second_allocation_probe_scale("alloc_p2", 2), |
Christoffer Rodbro | 377f5a2 | 2020-02-12 10:11:13 +0100 | [diff] [blame] | 103 | allocation_allow_further_probing("alloc_probe_further", false), |
| 104 | allocation_probe_max("alloc_probe_max", DataRate::PlusInfinity()) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 105 | ParseFieldTrial( |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 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}, |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 110 | key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration")); |
Jonas Olsson | 2e8d78c | 2019-05-27 13:31:45 +0200 | [diff] [blame] | 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, |
Christoffer Rodbro | 377f5a2 | 2020-02-12 10:11:13 +0100 | [diff] [blame] | 122 | &allocation_allow_further_probing, &allocation_probe_max}, |
Jonas Olsson | 2e8d78c | 2019-05-27 13:31:45 +0200 | [diff] [blame] | 123 | key_value_config->Lookup("WebRTC-Bwe-AllocationProbing")); |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) = |
| 127 | default; |
| 128 | ProbeControllerConfig::~ProbeControllerConfig() = default; |
| 129 | |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 130 | ProbeController::ProbeController(const WebRtcKeyValueConfig* key_value_config, |
| 131 | RtcEventLog* event_log) |
Erik Språng | cfe36ca | 2018-11-29 17:32:48 +0100 | [diff] [blame] | 132 | : enable_periodic_alr_probing_(false), |
Mirko Bonadei | 57cabed | 2020-04-01 12:03:11 +0200 | [diff] [blame] | 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")), |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 139 | event_log_(event_log), |
| 140 | config_(ProbeControllerConfig(key_value_config)) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 141 | Reset(0); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ProbeController::~ProbeController() {} |
| 145 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 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) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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_) |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 166 | return InitiateExponentialProbing(at_time_ms); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case State::kWaitingForProbingResult: |
| 170 | break; |
| 171 | |
| 172 | case State::kProbingComplete: |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 173 | // If the new max bitrate is higher than both the old max bitrate and the |
| 174 | // estimate then initiate probing. |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 189 | return InitiateProbing(at_time_ms, {max_bitrate_bps_}, false); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 190 | } |
| 191 | break; |
| 192 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 193 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 196 | std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate( |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 197 | int64_t max_total_allocated_bitrate, |
| 198 | int64_t at_time_ms) { |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 199 | const bool in_alr = alr_start_time_ms_.has_value(); |
Konrad Hofbauer | fdf3880 | 2019-08-19 13:50:57 +0200 | [diff] [blame] | 200 | const bool allow_allocation_probe = in_alr; |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 201 | |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 202 | if (state_ == State::kProbingComplete && |
| 203 | max_total_allocated_bitrate != max_total_allocated_bitrate_ && |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 204 | estimated_bitrate_bps_ != 0 && |
| 205 | (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) && |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 206 | estimated_bitrate_bps_ < max_total_allocated_bitrate && |
| 207 | allow_allocation_probe) { |
Sebastian Jansson | b2ecc3d | 2018-07-13 17:22:01 +0200 | [diff] [blame] | 208 | max_total_allocated_bitrate_ = max_total_allocated_bitrate; |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 209 | |
| 210 | if (!config_.first_allocation_probe_scale) |
| 211 | return std::vector<ProbeClusterConfig>(); |
| 212 | |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 213 | DataRate first_probe_rate = |
| 214 | DataRate::BitsPerSec(max_total_allocated_bitrate) * |
| 215 | config_.first_allocation_probe_scale.Value(); |
Christoffer Rodbro | 377f5a2 | 2020-02-12 10:11:13 +0100 | [diff] [blame] | 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()}; |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 219 | if (config_.second_allocation_probe_scale) { |
Christoffer Rodbro | 377f5a2 | 2020-02-12 10:11:13 +0100 | [diff] [blame] | 220 | DataRate second_probe_rate = |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 221 | DataRate::BitsPerSec(max_total_allocated_bitrate) * |
Christoffer Rodbro | 377f5a2 | 2020-02-12 10:11:13 +0100 | [diff] [blame] | 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()); |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 226 | } |
| 227 | return InitiateProbing(at_time_ms, probes, |
| 228 | config_.allocation_allow_further_probing); |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 229 | } |
Sebastian Jansson | f5e767d | 2018-10-15 13:24:31 +0200 | [diff] [blame] | 230 | max_total_allocated_bitrate_ = max_total_allocated_bitrate; |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 231 | return std::vector<ProbeClusterConfig>(); |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 234 | std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability( |
| 235 | NetworkAvailability msg) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 236 | network_available_ = msg.network_available; |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 237 | |
| 238 | if (!network_available_ && state_ == State::kWaitingForProbingResult) { |
| 239 | state_ = State::kProbingComplete; |
| 240 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 241 | } |
| 242 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 243 | if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0) |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 244 | return InitiateExponentialProbing(msg.at_time.ms()); |
| 245 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 248 | std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing( |
| 249 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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. |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 256 | std::vector<int64_t> probes = {static_cast<int64_t>( |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 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() * |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 260 | start_bitrate_bps_); |
| 261 | } |
| 262 | return InitiateProbing(at_time_ms, probes, true); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 265 | std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate( |
| 266 | int64_t bitrate_bps, |
| 267 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 276 | std::vector<ProbeClusterConfig> pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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_) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 286 | pending_probes = InitiateProbing( |
| 287 | at_time_ms, |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 288 | {static_cast<int64_t>(config_.further_exponential_probe_scale * |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 289 | bitrate_bps)}, |
| 290 | true); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 295 | time_of_last_large_drop_ms_ = at_time_ms; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 296 | bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_; |
| 297 | } |
| 298 | |
| 299 | estimated_bitrate_bps_ = bitrate_bps; |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 300 | return pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void ProbeController::EnablePeriodicAlrProbing(bool enable) { |
| 304 | enable_periodic_alr_probing_ = enable; |
| 305 | } |
| 306 | |
| 307 | void ProbeController::SetAlrStartTimeMs( |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 308 | absl::optional<int64_t> alr_start_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 309 | alr_start_time_ms_ = alr_start_time_ms; |
| 310 | } |
| 311 | void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) { |
| 312 | alr_end_time_ms_.emplace(alr_end_time_ms); |
| 313 | } |
| 314 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 315 | std::vector<ProbeClusterConfig> ProbeController::RequestProbe( |
| 316 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 343 | last_bwe_drop_probing_time_ms_ = at_time_ms; |
Sebastian Jansson | c9d0b08 | 2019-02-26 15:29:53 +0100 | [diff] [blame] | 344 | return InitiateProbing(at_time_ms, {suggested_probe_bps}, false); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 348 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Erik Språng | 791d43c | 2019-01-08 15:46:06 +0100 | [diff] [blame] | 351 | void ProbeController::SetMaxBitrate(int64_t max_bitrate_bps) { |
| 352 | max_bitrate_bps_ = max_bitrate_bps; |
| 353 | } |
| 354 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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; |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 369 | max_total_allocated_bitrate_ = 0; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 372 | std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 373 | if (at_time_ms - time_last_probing_initiated_ms_ > |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 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_) + |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 389 | config_.alr_probing_interval->ms(); |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 390 | if (at_time_ms >= next_probe_time_ms) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 391 | return InitiateProbing(at_time_ms, |
| 392 | {static_cast<int64_t>(estimated_bitrate_bps_ * |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 393 | config_.alr_probe_scale)}, |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 394 | true); |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 395 | } |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 398 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 401 | std::vector<ProbeClusterConfig> ProbeController::InitiateProbing( |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 402 | int64_t now_ms, |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 403 | std::vector<int64_t> bitrates_to_probe, |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 404 | bool probe_further) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 419 | std::vector<ProbeClusterConfig> pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 420 | for (int64_t bitrate : bitrates_to_probe) { |
| 421 | RTC_DCHECK_GT(bitrate, 0); |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 422 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 423 | if (bitrate > max_probe_bitrate_bps) { |
| 424 | bitrate = max_probe_bitrate_bps; |
| 425 | probe_further = false; |
| 426 | } |
| 427 | |
| 428 | ProbeClusterConfig config; |
Danil Chapovalov | 5528402 | 2020-02-07 14:53:52 +0100 | [diff] [blame] | 429 | config.at_time = Timestamp::Millis(now_ms); |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 430 | config.target_data_rate = |
| 431 | DataRate::BitsPerSec(rtc::dchecked_cast<int>(bitrate)); |
Danil Chapovalov | 5528402 | 2020-02-07 14:53:52 +0100 | [diff] [blame] | 432 | config.target_duration = TimeDelta::Millis(kMinProbeDurationMs); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 433 | config.target_probe_count = kMinProbePacketsSent; |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 434 | config.id = next_probe_cluster_id_; |
| 435 | next_probe_cluster_id_++; |
| 436 | MaybeLogProbeClusterCreated(event_log_, config); |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 437 | pending_probes.push_back(config); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 438 | } |
| 439 | time_last_probing_initiated_ms_ = now_ms; |
| 440 | if (probe_further) { |
| 441 | state_ = State::kWaitingForProbingResult; |
| 442 | min_bitrate_to_probe_further_bps_ = |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 443 | (*(bitrates_to_probe.end() - 1)) * config_.further_probe_threshold; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 444 | } else { |
| 445 | state_ = State::kProbingComplete; |
| 446 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 447 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 448 | return pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 451 | } // namespace webrtc |