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