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> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <string> |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 16 | |
Jonas Olsson | 2e8d78c | 2019-05-27 13:31:45 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/units/data_rate.h" |
| 19 | #include "api/units/time_delta.h" |
| 20 | #include "api/units/timestamp.h" |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 21 | #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 25 | #include "system_wrappers/include/metrics.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | namespace { |
| 30 | // The minimum number probing packets used. |
| 31 | constexpr int kMinProbePacketsSent = 5; |
| 32 | |
| 33 | // The minimum probing duration in ms. |
| 34 | constexpr int kMinProbeDurationMs = 15; |
| 35 | |
| 36 | // Maximum waiting time from the time of initiating probing to getting |
| 37 | // the measured results back. |
| 38 | constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000; |
| 39 | |
| 40 | // Value of |min_bitrate_to_probe_further_bps_| that indicates |
| 41 | // further probing is disabled. |
| 42 | constexpr int kExponentialProbingDisabled = 0; |
| 43 | |
| 44 | // Default probing bitrate limit. Applied only when the application didn't |
| 45 | // specify max bitrate. |
| 46 | constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000; |
| 47 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 48 | // 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. |
| 51 | constexpr double kBitrateDropThreshold = 0.66; |
| 52 | constexpr int kBitrateDropTimeoutMs = 5000; |
| 53 | constexpr 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. |
| 58 | constexpr 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. |
| 63 | constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000; |
| 64 | |
| 65 | // bitrate). Used to avoid probing if the probe bitrate is close to our current |
| 66 | // estimate. |
| 67 | constexpr double kProbeUncertainty = 0.05; |
| 68 | |
| 69 | // Use probing to recover faster after large bitrate estimate drops. |
| 70 | constexpr char kBweRapidRecoveryExperiment[] = |
| 71 | "WebRTC-BweRapidRecoveryExperiment"; |
| 72 | |
Erik Språng | cfe36ca | 2018-11-29 17:32:48 +0100 | [diff] [blame] | 73 | // Never probe higher than configured by OnMaxTotalAllocatedBitrate(). |
| 74 | constexpr char kCappedProbingFieldTrialName[] = "WebRTC-BweCappedProbing"; |
| 75 | |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 76 | void MaybeLogProbeClusterCreated(RtcEventLog* event_log, |
| 77 | const ProbeClusterConfig& probe) { |
| 78 | RTC_DCHECK(event_log); |
| 79 | if (!event_log) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | size_t min_bytes = static_cast<int32_t>(probe.target_data_rate.bps() * |
| 84 | probe.target_duration.ms() / 8000); |
| 85 | event_log->Log(absl::make_unique<RtcEventProbeClusterCreated>( |
| 86 | probe.id, probe.target_data_rate.bps(), probe.target_probe_count, |
| 87 | min_bytes)); |
| 88 | } |
| 89 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 90 | } // namespace |
| 91 | |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 92 | ProbeControllerConfig::ProbeControllerConfig( |
| 93 | const WebRtcKeyValueConfig* key_value_config) |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 94 | : first_exponential_probe_scale("p1", 3.0), |
| 95 | second_exponential_probe_scale("p2", 6.0), |
| 96 | further_exponential_probe_scale("step_size", 2), |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 97 | further_probe_threshold("further_probe_threshold", 0.7), |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 98 | alr_probing_interval("alr_interval", TimeDelta::seconds(5)), |
| 99 | alr_probe_scale("alr_scale", 2), |
| 100 | first_allocation_probe_scale("alloc_p1", 1), |
| 101 | second_allocation_probe_scale("alloc_p2", 2), |
| 102 | allocation_allow_further_probing("alloc_probe_further", false) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 103 | ParseFieldTrial( |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 104 | {&first_exponential_probe_scale, &second_exponential_probe_scale, |
| 105 | &further_exponential_probe_scale, &further_probe_threshold, |
| 106 | &alr_probing_interval, &alr_probe_scale, &first_allocation_probe_scale, |
| 107 | &second_allocation_probe_scale, &allocation_allow_further_probing}, |
Jonas Olsson | 24923e8 | 2019-03-27 14:19:04 +0100 | [diff] [blame] | 108 | key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration")); |
Jonas Olsson | 2e8d78c | 2019-05-27 13:31:45 +0200 | [diff] [blame] | 109 | |
| 110 | // Specialized keys overriding subsets of WebRTC-Bwe-ProbingConfiguration |
| 111 | ParseFieldTrial( |
| 112 | {&first_exponential_probe_scale, &second_exponential_probe_scale}, |
| 113 | key_value_config->Lookup("WebRTC-Bwe-InitialProbing")); |
| 114 | ParseFieldTrial({&further_exponential_probe_scale, &further_probe_threshold}, |
| 115 | key_value_config->Lookup("WebRTC-Bwe-ExponentialProbing")); |
| 116 | ParseFieldTrial({&alr_probing_interval, &alr_probe_scale}, |
| 117 | key_value_config->Lookup("WebRTC-Bwe-AlrProbing")); |
| 118 | ParseFieldTrial( |
| 119 | {&first_allocation_probe_scale, &second_allocation_probe_scale, |
| 120 | &allocation_allow_further_probing}, |
| 121 | key_value_config->Lookup("WebRTC-Bwe-AllocationProbing")); |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ProbeControllerConfig::ProbeControllerConfig(const ProbeControllerConfig&) = |
| 125 | default; |
| 126 | ProbeControllerConfig::~ProbeControllerConfig() = default; |
| 127 | |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 128 | ProbeController::ProbeController(const WebRtcKeyValueConfig* key_value_config, |
| 129 | RtcEventLog* event_log) |
Erik Språng | cfe36ca | 2018-11-29 17:32:48 +0100 | [diff] [blame] | 130 | : enable_periodic_alr_probing_(false), |
| 131 | in_rapid_recovery_experiment_( |
Sebastian Jansson | 95edb03 | 2019-01-17 16:24:12 +0100 | [diff] [blame] | 132 | key_value_config->Lookup(kBweRapidRecoveryExperiment) |
| 133 | .find("Enabled") == 0), |
Erik Språng | cfe36ca | 2018-11-29 17:32:48 +0100 | [diff] [blame] | 134 | limit_probes_with_allocateable_rate_( |
Sebastian Jansson | 95edb03 | 2019-01-17 16:24:12 +0100 | [diff] [blame] | 135 | key_value_config->Lookup(kCappedProbingFieldTrialName) |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 136 | .find("Disabled") != 0), |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 137 | event_log_(event_log), |
| 138 | config_(ProbeControllerConfig(key_value_config)) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 139 | Reset(0); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | ProbeController::~ProbeController() {} |
| 143 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 144 | std::vector<ProbeClusterConfig> ProbeController::SetBitrates( |
| 145 | int64_t min_bitrate_bps, |
| 146 | int64_t start_bitrate_bps, |
| 147 | int64_t max_bitrate_bps, |
| 148 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 149 | if (start_bitrate_bps > 0) { |
| 150 | start_bitrate_bps_ = start_bitrate_bps; |
| 151 | estimated_bitrate_bps_ = start_bitrate_bps; |
| 152 | } else if (start_bitrate_bps_ == 0) { |
| 153 | start_bitrate_bps_ = min_bitrate_bps; |
| 154 | } |
| 155 | |
| 156 | // The reason we use the variable |old_max_bitrate_pbs| is because we |
| 157 | // need to set |max_bitrate_bps_| before we call InitiateProbing. |
| 158 | int64_t old_max_bitrate_bps = max_bitrate_bps_; |
| 159 | max_bitrate_bps_ = max_bitrate_bps; |
| 160 | |
| 161 | switch (state_) { |
| 162 | case State::kInit: |
| 163 | if (network_available_) |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 164 | return InitiateExponentialProbing(at_time_ms); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 165 | break; |
| 166 | |
| 167 | case State::kWaitingForProbingResult: |
| 168 | break; |
| 169 | |
| 170 | case State::kProbingComplete: |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 171 | // If the new max bitrate is higher than both the old max bitrate and the |
| 172 | // estimate then initiate probing. |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 173 | if (estimated_bitrate_bps_ != 0 && |
| 174 | old_max_bitrate_bps < max_bitrate_bps_ && |
| 175 | estimated_bitrate_bps_ < max_bitrate_bps_) { |
| 176 | // The assumption is that if we jump more than 20% in the bandwidth |
| 177 | // estimate or if the bandwidth estimate is within 90% of the new |
| 178 | // max bitrate then the probing attempt was successful. |
| 179 | mid_call_probing_succcess_threshold_ = |
| 180 | std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9); |
| 181 | mid_call_probing_waiting_for_result_ = true; |
| 182 | mid_call_probing_bitrate_bps_ = max_bitrate_bps_; |
| 183 | |
| 184 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated", |
| 185 | max_bitrate_bps_ / 1000); |
| 186 | |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 187 | return InitiateProbing(at_time_ms, {max_bitrate_bps_}, false); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 191 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 194 | std::vector<ProbeClusterConfig> ProbeController::OnMaxTotalAllocatedBitrate( |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 195 | int64_t max_total_allocated_bitrate, |
| 196 | int64_t at_time_ms) { |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 197 | const bool in_alr = alr_start_time_ms_.has_value(); |
Konrad Hofbauer | fdf3880 | 2019-08-19 13:50:57 +0200 | [diff] [blame] | 198 | const bool allow_allocation_probe = in_alr; |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 199 | |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 200 | if (state_ == State::kProbingComplete && |
| 201 | max_total_allocated_bitrate != max_total_allocated_bitrate_ && |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 202 | estimated_bitrate_bps_ != 0 && |
| 203 | (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) && |
Konrad Hofbauer | 25f35a8 | 2019-04-10 12:38:06 +0200 | [diff] [blame] | 204 | estimated_bitrate_bps_ < max_total_allocated_bitrate && |
| 205 | allow_allocation_probe) { |
Sebastian Jansson | b2ecc3d | 2018-07-13 17:22:01 +0200 | [diff] [blame] | 206 | max_total_allocated_bitrate_ = max_total_allocated_bitrate; |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 207 | |
| 208 | if (!config_.first_allocation_probe_scale) |
| 209 | return std::vector<ProbeClusterConfig>(); |
| 210 | |
| 211 | std::vector<int64_t> probes = { |
| 212 | static_cast<int64_t>(config_.first_allocation_probe_scale.Value() * |
| 213 | max_total_allocated_bitrate)}; |
| 214 | if (config_.second_allocation_probe_scale) { |
| 215 | probes.push_back(config_.second_allocation_probe_scale.Value() * |
| 216 | max_total_allocated_bitrate); |
| 217 | } |
| 218 | return InitiateProbing(at_time_ms, probes, |
| 219 | config_.allocation_allow_further_probing); |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 220 | } |
Sebastian Jansson | f5e767d | 2018-10-15 13:24:31 +0200 | [diff] [blame] | 221 | max_total_allocated_bitrate_ = max_total_allocated_bitrate; |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 222 | return std::vector<ProbeClusterConfig>(); |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 225 | std::vector<ProbeClusterConfig> ProbeController::OnNetworkAvailability( |
| 226 | NetworkAvailability msg) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 227 | network_available_ = msg.network_available; |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 228 | |
| 229 | if (!network_available_ && state_ == State::kWaitingForProbingResult) { |
| 230 | state_ = State::kProbingComplete; |
| 231 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 232 | } |
| 233 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 234 | if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0) |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 235 | return InitiateExponentialProbing(msg.at_time.ms()); |
| 236 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 239 | std::vector<ProbeClusterConfig> ProbeController::InitiateExponentialProbing( |
| 240 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 241 | RTC_DCHECK(network_available_); |
| 242 | RTC_DCHECK(state_ == State::kInit); |
| 243 | RTC_DCHECK_GT(start_bitrate_bps_, 0); |
| 244 | |
| 245 | // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of |
| 246 | // 1.2 Mbps to continue probing. |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 247 | std::vector<int64_t> probes = {static_cast<int64_t>( |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 248 | config_.first_exponential_probe_scale * start_bitrate_bps_)}; |
| 249 | if (config_.second_exponential_probe_scale) { |
| 250 | probes.push_back(config_.second_exponential_probe_scale.Value() * |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 251 | start_bitrate_bps_); |
| 252 | } |
| 253 | return InitiateProbing(at_time_ms, probes, true); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 256 | std::vector<ProbeClusterConfig> ProbeController::SetEstimatedBitrate( |
| 257 | int64_t bitrate_bps, |
| 258 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 259 | if (mid_call_probing_waiting_for_result_ && |
| 260 | bitrate_bps >= mid_call_probing_succcess_threshold_) { |
| 261 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success", |
| 262 | mid_call_probing_bitrate_bps_ / 1000); |
| 263 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps", |
| 264 | bitrate_bps / 1000); |
| 265 | mid_call_probing_waiting_for_result_ = false; |
| 266 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 267 | std::vector<ProbeClusterConfig> pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 268 | if (state_ == State::kWaitingForProbingResult) { |
| 269 | // Continue probing if probing results indicate channel has greater |
| 270 | // capacity. |
| 271 | RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps |
| 272 | << " Minimum to probe further: " |
| 273 | << min_bitrate_to_probe_further_bps_; |
| 274 | |
| 275 | if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled && |
| 276 | bitrate_bps > min_bitrate_to_probe_further_bps_) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 277 | pending_probes = InitiateProbing( |
| 278 | at_time_ms, |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 279 | {static_cast<int64_t>(config_.further_exponential_probe_scale * |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 280 | bitrate_bps)}, |
| 281 | true); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 286 | time_of_last_large_drop_ms_ = at_time_ms; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 287 | bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_; |
| 288 | } |
| 289 | |
| 290 | estimated_bitrate_bps_ = bitrate_bps; |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 291 | return pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void ProbeController::EnablePeriodicAlrProbing(bool enable) { |
| 295 | enable_periodic_alr_probing_ = enable; |
| 296 | } |
| 297 | |
| 298 | void ProbeController::SetAlrStartTimeMs( |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 299 | absl::optional<int64_t> alr_start_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 300 | alr_start_time_ms_ = alr_start_time_ms; |
| 301 | } |
| 302 | void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) { |
| 303 | alr_end_time_ms_.emplace(alr_end_time_ms); |
| 304 | } |
| 305 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 306 | std::vector<ProbeClusterConfig> ProbeController::RequestProbe( |
| 307 | int64_t at_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 308 | // Called once we have returned to normal state after a large drop in |
| 309 | // estimated bandwidth. The current response is to initiate a single probe |
| 310 | // session (if not already probing) at the previous bitrate. |
| 311 | // |
| 312 | // If the probe session fails, the assumption is that this drop was a |
| 313 | // real one from a competing flow or a network change. |
| 314 | bool in_alr = alr_start_time_ms_.has_value(); |
| 315 | bool alr_ended_recently = |
| 316 | (alr_end_time_ms_.has_value() && |
| 317 | at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs); |
| 318 | if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) { |
| 319 | if (state_ == State::kProbingComplete) { |
| 320 | uint32_t suggested_probe_bps = |
| 321 | kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_; |
| 322 | uint32_t min_expected_probe_result_bps = |
| 323 | (1 - kProbeUncertainty) * suggested_probe_bps; |
| 324 | int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_; |
| 325 | int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_; |
| 326 | if (min_expected_probe_result_bps > estimated_bitrate_bps_ && |
| 327 | time_since_drop_ms < kBitrateDropTimeoutMs && |
| 328 | time_since_probe_ms > kMinTimeBetweenAlrProbesMs) { |
| 329 | RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing."; |
| 330 | // Track how often we probe in response to bandwidth drop in ALR. |
| 331 | RTC_HISTOGRAM_COUNTS_10000( |
| 332 | "WebRTC.BWE.BweDropProbingIntervalInS", |
| 333 | (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 334 | last_bwe_drop_probing_time_ms_ = at_time_ms; |
Sebastian Jansson | c9d0b08 | 2019-02-26 15:29:53 +0100 | [diff] [blame] | 335 | return InitiateProbing(at_time_ms, {suggested_probe_bps}, false); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 339 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Erik Språng | 791d43c | 2019-01-08 15:46:06 +0100 | [diff] [blame] | 342 | void ProbeController::SetMaxBitrate(int64_t max_bitrate_bps) { |
| 343 | max_bitrate_bps_ = max_bitrate_bps; |
| 344 | } |
| 345 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 346 | void ProbeController::Reset(int64_t at_time_ms) { |
| 347 | network_available_ = true; |
| 348 | state_ = State::kInit; |
| 349 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 350 | time_last_probing_initiated_ms_ = 0; |
| 351 | estimated_bitrate_bps_ = 0; |
| 352 | start_bitrate_bps_ = 0; |
| 353 | max_bitrate_bps_ = 0; |
| 354 | int64_t now_ms = at_time_ms; |
| 355 | last_bwe_drop_probing_time_ms_ = now_ms; |
| 356 | alr_end_time_ms_.reset(); |
| 357 | mid_call_probing_waiting_for_result_ = false; |
| 358 | time_of_last_large_drop_ms_ = now_ms; |
| 359 | bitrate_before_last_large_drop_bps_ = 0; |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 360 | max_total_allocated_bitrate_ = 0; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 363 | std::vector<ProbeClusterConfig> ProbeController::Process(int64_t at_time_ms) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 364 | if (at_time_ms - time_last_probing_initiated_ms_ > |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 365 | kMaxWaitingTimeForProbingResultMs) { |
| 366 | mid_call_probing_waiting_for_result_ = false; |
| 367 | |
| 368 | if (state_ == State::kWaitingForProbingResult) { |
| 369 | RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout"; |
| 370 | state_ = State::kProbingComplete; |
| 371 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 372 | } |
| 373 | } |
| 374 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 375 | if (enable_periodic_alr_probing_ && state_ == State::kProbingComplete) { |
| 376 | // Probe bandwidth periodically when in ALR state. |
| 377 | if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) { |
| 378 | int64_t next_probe_time_ms = |
| 379 | std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) + |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 380 | config_.alr_probing_interval->ms(); |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 381 | if (at_time_ms >= next_probe_time_ms) { |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 382 | return InitiateProbing(at_time_ms, |
| 383 | {static_cast<int64_t>(estimated_bitrate_bps_ * |
Jonas Olsson | 01d3618 | 2019-03-27 15:57:02 +0100 | [diff] [blame] | 384 | config_.alr_probe_scale)}, |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 385 | true); |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 386 | } |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 389 | return std::vector<ProbeClusterConfig>(); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 392 | std::vector<ProbeClusterConfig> ProbeController::InitiateProbing( |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 393 | int64_t now_ms, |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 394 | std::vector<int64_t> bitrates_to_probe, |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 395 | bool probe_further) { |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 396 | int64_t max_probe_bitrate_bps = |
| 397 | max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps; |
| 398 | if (limit_probes_with_allocateable_rate_ && |
| 399 | max_total_allocated_bitrate_ > 0) { |
| 400 | // If a max allocated bitrate has been configured, allow probing up to 2x |
| 401 | // that rate. This allows some overhead to account for bursty streams, |
| 402 | // which otherwise would have to ramp up when the overshoot is already in |
| 403 | // progress. |
| 404 | // It also avoids minor quality reduction caused by probes often being |
| 405 | // received at slightly less than the target probe bitrate. |
| 406 | max_probe_bitrate_bps = |
| 407 | std::min(max_probe_bitrate_bps, max_total_allocated_bitrate_ * 2); |
| 408 | } |
| 409 | |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 410 | std::vector<ProbeClusterConfig> pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 411 | for (int64_t bitrate : bitrates_to_probe) { |
| 412 | RTC_DCHECK_GT(bitrate, 0); |
Jonas Olsson | f441ea9 | 2019-03-05 16:24:48 +0100 | [diff] [blame] | 413 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 414 | if (bitrate > max_probe_bitrate_bps) { |
| 415 | bitrate = max_probe_bitrate_bps; |
| 416 | probe_further = false; |
| 417 | } |
| 418 | |
| 419 | ProbeClusterConfig config; |
| 420 | config.at_time = Timestamp::ms(now_ms); |
| 421 | config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate)); |
| 422 | config.target_duration = TimeDelta::ms(kMinProbeDurationMs); |
| 423 | config.target_probe_count = kMinProbePacketsSent; |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 424 | config.id = next_probe_cluster_id_; |
| 425 | next_probe_cluster_id_++; |
| 426 | MaybeLogProbeClusterCreated(event_log_, config); |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 427 | pending_probes.push_back(config); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 428 | } |
| 429 | time_last_probing_initiated_ms_ = now_ms; |
| 430 | if (probe_further) { |
| 431 | state_ = State::kWaitingForProbingResult; |
| 432 | min_bitrate_to_probe_further_bps_ = |
Jonas Olsson | e096004 | 2019-03-12 13:49:26 +0100 | [diff] [blame] | 433 | (*(bitrates_to_probe.end() - 1)) * config_.further_probe_threshold; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 434 | } else { |
| 435 | state_ = State::kProbingComplete; |
| 436 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 437 | } |
Sebastian Jansson | da2ec40 | 2018-08-02 16:27:28 +0200 | [diff] [blame] | 438 | return pending_probes; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 441 | } // namespace webrtc |