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> |
| 15 | |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "rtc_base/numerics/safe_conversions.h" |
| 18 | #include "system_wrappers/include/field_trial.h" |
| 19 | #include "system_wrappers/include/metrics.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace { |
| 24 | // The minimum number probing packets used. |
| 25 | constexpr int kMinProbePacketsSent = 5; |
| 26 | |
| 27 | // The minimum probing duration in ms. |
| 28 | constexpr int kMinProbeDurationMs = 15; |
| 29 | |
| 30 | // Maximum waiting time from the time of initiating probing to getting |
| 31 | // the measured results back. |
| 32 | constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000; |
| 33 | |
| 34 | // Value of |min_bitrate_to_probe_further_bps_| that indicates |
| 35 | // further probing is disabled. |
| 36 | constexpr int kExponentialProbingDisabled = 0; |
| 37 | |
| 38 | // Default probing bitrate limit. Applied only when the application didn't |
| 39 | // specify max bitrate. |
| 40 | constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000; |
| 41 | |
| 42 | // Interval between probes when ALR periodic probing is enabled. |
| 43 | constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000; |
| 44 | |
| 45 | // Minimum probe bitrate percentage to probe further for repeated probes, |
| 46 | // relative to the previous probe. For example, if 1Mbps probe results in |
| 47 | // 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be |
| 48 | // sent if we get 600kbps from the first one. |
| 49 | constexpr int kRepeatedProbeMinPercentage = 70; |
| 50 | |
| 51 | // If the bitrate drops to a factor |kBitrateDropThreshold| or lower |
| 52 | // and we recover within |kBitrateDropTimeoutMs|, then we'll send |
| 53 | // a probe at a fraction |kProbeFractionAfterDrop| of the original bitrate. |
| 54 | constexpr double kBitrateDropThreshold = 0.66; |
| 55 | constexpr int kBitrateDropTimeoutMs = 5000; |
| 56 | constexpr double kProbeFractionAfterDrop = 0.85; |
| 57 | |
| 58 | // Timeout for probing after leaving ALR. If the bitrate drops significantly, |
| 59 | // (as determined by the delay based estimator) and we leave ALR, then we will |
| 60 | // send a probe if we recover within |kLeftAlrTimeoutMs| ms. |
| 61 | constexpr int kAlrEndedTimeoutMs = 3000; |
| 62 | |
| 63 | // The expected uncertainty of probe result (as a fraction of the target probe |
| 64 | // This is a limit on how often probing can be done when there is a BW |
| 65 | // drop detected in ALR. |
| 66 | constexpr int64_t kMinTimeBetweenAlrProbesMs = 5000; |
| 67 | |
| 68 | // bitrate). Used to avoid probing if the probe bitrate is close to our current |
| 69 | // estimate. |
| 70 | constexpr double kProbeUncertainty = 0.05; |
| 71 | |
| 72 | // Use probing to recover faster after large bitrate estimate drops. |
| 73 | constexpr char kBweRapidRecoveryExperiment[] = |
| 74 | "WebRTC-BweRapidRecoveryExperiment"; |
| 75 | |
| 76 | } // namespace |
| 77 | |
Sebastian Jansson | f2e3e7a | 2018-04-06 17:16:06 +0200 | [diff] [blame] | 78 | ProbeController::ProbeController() : enable_periodic_alr_probing_(false) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 79 | Reset(0); |
| 80 | in_rapid_recovery_experiment_ = webrtc::field_trial::FindFullName( |
| 81 | kBweRapidRecoveryExperiment) == "Enabled"; |
| 82 | } |
| 83 | |
| 84 | ProbeController::~ProbeController() {} |
| 85 | |
| 86 | void ProbeController::SetBitrates(int64_t min_bitrate_bps, |
| 87 | int64_t start_bitrate_bps, |
| 88 | int64_t max_bitrate_bps, |
| 89 | int64_t at_time_ms) { |
| 90 | if (start_bitrate_bps > 0) { |
| 91 | start_bitrate_bps_ = start_bitrate_bps; |
| 92 | estimated_bitrate_bps_ = start_bitrate_bps; |
| 93 | } else if (start_bitrate_bps_ == 0) { |
| 94 | start_bitrate_bps_ = min_bitrate_bps; |
| 95 | } |
| 96 | |
| 97 | // The reason we use the variable |old_max_bitrate_pbs| is because we |
| 98 | // need to set |max_bitrate_bps_| before we call InitiateProbing. |
| 99 | int64_t old_max_bitrate_bps = max_bitrate_bps_; |
| 100 | max_bitrate_bps_ = max_bitrate_bps; |
| 101 | |
| 102 | switch (state_) { |
| 103 | case State::kInit: |
| 104 | if (network_available_) |
| 105 | InitiateExponentialProbing(at_time_ms); |
| 106 | break; |
| 107 | |
| 108 | case State::kWaitingForProbingResult: |
| 109 | break; |
| 110 | |
| 111 | case State::kProbingComplete: |
| 112 | // If the new max bitrate is higher than the old max bitrate and the |
| 113 | // estimate is lower than the new max bitrate then initiate probing. |
| 114 | if (estimated_bitrate_bps_ != 0 && |
| 115 | old_max_bitrate_bps < max_bitrate_bps_ && |
| 116 | estimated_bitrate_bps_ < max_bitrate_bps_) { |
| 117 | // The assumption is that if we jump more than 20% in the bandwidth |
| 118 | // estimate or if the bandwidth estimate is within 90% of the new |
| 119 | // max bitrate then the probing attempt was successful. |
| 120 | mid_call_probing_succcess_threshold_ = |
| 121 | std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9); |
| 122 | mid_call_probing_waiting_for_result_ = true; |
| 123 | mid_call_probing_bitrate_bps_ = max_bitrate_bps_; |
| 124 | |
| 125 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated", |
| 126 | max_bitrate_bps_ / 1000); |
| 127 | |
| 128 | InitiateProbing(at_time_ms, {max_bitrate_bps}, false); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 134 | void ProbeController::OnMaxTotalAllocatedBitrate( |
| 135 | int64_t max_total_allocated_bitrate, |
| 136 | int64_t at_time_ms) { |
| 137 | // TODO(philipel): Should |max_total_allocated_bitrate| be used as a limit for |
| 138 | // ALR probing? |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 139 | if (state_ == State::kProbingComplete && |
| 140 | max_total_allocated_bitrate != max_total_allocated_bitrate_ && |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 141 | estimated_bitrate_bps_ != 0 && |
| 142 | (max_bitrate_bps_ <= 0 || estimated_bitrate_bps_ < max_bitrate_bps_) && |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 143 | estimated_bitrate_bps_ < max_total_allocated_bitrate) { |
Sebastian Jansson | b2ecc3d | 2018-07-13 17:22:01 +0200 | [diff] [blame] | 144 | max_total_allocated_bitrate_ = max_total_allocated_bitrate; |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 145 | InitiateProbing(at_time_ms, {max_total_allocated_bitrate}, false); |
| 146 | } |
| 147 | } |
| 148 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 149 | void ProbeController::OnNetworkAvailability(NetworkAvailability msg) { |
| 150 | network_available_ = msg.network_available; |
philipel | 9fd6b98 | 2018-04-19 16:58:07 +0200 | [diff] [blame] | 151 | |
| 152 | if (!network_available_ && state_ == State::kWaitingForProbingResult) { |
| 153 | state_ = State::kProbingComplete; |
| 154 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 155 | } |
| 156 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 157 | if (network_available_ && state_ == State::kInit && start_bitrate_bps_ > 0) |
| 158 | InitiateExponentialProbing(msg.at_time.ms()); |
| 159 | } |
| 160 | |
| 161 | void ProbeController::InitiateExponentialProbing(int64_t at_time_ms) { |
| 162 | RTC_DCHECK(network_available_); |
| 163 | RTC_DCHECK(state_ == State::kInit); |
| 164 | RTC_DCHECK_GT(start_bitrate_bps_, 0); |
| 165 | |
| 166 | // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of |
| 167 | // 1.2 Mbps to continue probing. |
| 168 | InitiateProbing(at_time_ms, {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, |
| 169 | true); |
| 170 | } |
| 171 | |
| 172 | void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps, |
| 173 | int64_t at_time_ms) { |
| 174 | int64_t now_ms = at_time_ms; |
| 175 | |
| 176 | if (mid_call_probing_waiting_for_result_ && |
| 177 | bitrate_bps >= mid_call_probing_succcess_threshold_) { |
| 178 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success", |
| 179 | mid_call_probing_bitrate_bps_ / 1000); |
| 180 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps", |
| 181 | bitrate_bps / 1000); |
| 182 | mid_call_probing_waiting_for_result_ = false; |
| 183 | } |
| 184 | |
| 185 | if (state_ == State::kWaitingForProbingResult) { |
| 186 | // Continue probing if probing results indicate channel has greater |
| 187 | // capacity. |
| 188 | RTC_LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps |
| 189 | << " Minimum to probe further: " |
| 190 | << min_bitrate_to_probe_further_bps_; |
| 191 | |
| 192 | if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled && |
| 193 | bitrate_bps > min_bitrate_to_probe_further_bps_) { |
| 194 | // Double the probing bitrate. |
| 195 | InitiateProbing(now_ms, {2 * bitrate_bps}, true); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (bitrate_bps < kBitrateDropThreshold * estimated_bitrate_bps_) { |
| 200 | time_of_last_large_drop_ms_ = now_ms; |
| 201 | bitrate_before_last_large_drop_bps_ = estimated_bitrate_bps_; |
| 202 | } |
| 203 | |
| 204 | estimated_bitrate_bps_ = bitrate_bps; |
| 205 | } |
| 206 | |
| 207 | void ProbeController::EnablePeriodicAlrProbing(bool enable) { |
| 208 | enable_periodic_alr_probing_ = enable; |
| 209 | } |
| 210 | |
| 211 | void ProbeController::SetAlrStartTimeMs( |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 212 | absl::optional<int64_t> alr_start_time_ms) { |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 213 | alr_start_time_ms_ = alr_start_time_ms; |
| 214 | } |
| 215 | void ProbeController::SetAlrEndedTimeMs(int64_t alr_end_time_ms) { |
| 216 | alr_end_time_ms_.emplace(alr_end_time_ms); |
| 217 | } |
| 218 | |
| 219 | void ProbeController::RequestProbe(int64_t at_time_ms) { |
| 220 | // Called once we have returned to normal state after a large drop in |
| 221 | // estimated bandwidth. The current response is to initiate a single probe |
| 222 | // session (if not already probing) at the previous bitrate. |
| 223 | // |
| 224 | // If the probe session fails, the assumption is that this drop was a |
| 225 | // real one from a competing flow or a network change. |
| 226 | bool in_alr = alr_start_time_ms_.has_value(); |
| 227 | bool alr_ended_recently = |
| 228 | (alr_end_time_ms_.has_value() && |
| 229 | at_time_ms - alr_end_time_ms_.value() < kAlrEndedTimeoutMs); |
| 230 | if (in_alr || alr_ended_recently || in_rapid_recovery_experiment_) { |
| 231 | if (state_ == State::kProbingComplete) { |
| 232 | uint32_t suggested_probe_bps = |
| 233 | kProbeFractionAfterDrop * bitrate_before_last_large_drop_bps_; |
| 234 | uint32_t min_expected_probe_result_bps = |
| 235 | (1 - kProbeUncertainty) * suggested_probe_bps; |
| 236 | int64_t time_since_drop_ms = at_time_ms - time_of_last_large_drop_ms_; |
| 237 | int64_t time_since_probe_ms = at_time_ms - last_bwe_drop_probing_time_ms_; |
| 238 | if (min_expected_probe_result_bps > estimated_bitrate_bps_ && |
| 239 | time_since_drop_ms < kBitrateDropTimeoutMs && |
| 240 | time_since_probe_ms > kMinTimeBetweenAlrProbesMs) { |
| 241 | RTC_LOG(LS_INFO) << "Detected big bandwidth drop, start probing."; |
| 242 | // Track how often we probe in response to bandwidth drop in ALR. |
| 243 | RTC_HISTOGRAM_COUNTS_10000( |
| 244 | "WebRTC.BWE.BweDropProbingIntervalInS", |
| 245 | (at_time_ms - last_bwe_drop_probing_time_ms_) / 1000); |
| 246 | InitiateProbing(at_time_ms, {suggested_probe_bps}, false); |
| 247 | last_bwe_drop_probing_time_ms_ = at_time_ms; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void ProbeController::Reset(int64_t at_time_ms) { |
| 254 | network_available_ = true; |
| 255 | state_ = State::kInit; |
| 256 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 257 | time_last_probing_initiated_ms_ = 0; |
| 258 | estimated_bitrate_bps_ = 0; |
| 259 | start_bitrate_bps_ = 0; |
| 260 | max_bitrate_bps_ = 0; |
| 261 | int64_t now_ms = at_time_ms; |
| 262 | last_bwe_drop_probing_time_ms_ = now_ms; |
| 263 | alr_end_time_ms_.reset(); |
| 264 | mid_call_probing_waiting_for_result_ = false; |
| 265 | time_of_last_large_drop_ms_ = now_ms; |
| 266 | bitrate_before_last_large_drop_bps_ = 0; |
philipel | 0676f22 | 2018-04-17 16:12:21 +0200 | [diff] [blame] | 267 | max_total_allocated_bitrate_ = 0; |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void ProbeController::Process(int64_t at_time_ms) { |
| 271 | int64_t now_ms = at_time_ms; |
| 272 | |
| 273 | if (now_ms - time_last_probing_initiated_ms_ > |
| 274 | kMaxWaitingTimeForProbingResultMs) { |
| 275 | mid_call_probing_waiting_for_result_ = false; |
| 276 | |
| 277 | if (state_ == State::kWaitingForProbingResult) { |
| 278 | RTC_LOG(LS_INFO) << "kWaitingForProbingResult: timeout"; |
| 279 | state_ = State::kProbingComplete; |
| 280 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_) |
| 285 | return; |
| 286 | |
| 287 | // Probe bandwidth periodically when in ALR state. |
| 288 | if (alr_start_time_ms_ && estimated_bitrate_bps_ > 0) { |
| 289 | int64_t next_probe_time_ms = |
| 290 | std::max(*alr_start_time_ms_, time_last_probing_initiated_ms_) + |
| 291 | kAlrPeriodicProbingIntervalMs; |
| 292 | if (now_ms >= next_probe_time_ms) { |
| 293 | InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Sebastian Jansson | f2e3e7a | 2018-04-06 17:16:06 +0200 | [diff] [blame] | 298 | std::vector<ProbeClusterConfig> ProbeController::GetAndResetPendingProbes() { |
| 299 | if (pending_probes_.empty()) |
| 300 | return std::vector<ProbeClusterConfig>(); |
| 301 | std::vector<ProbeClusterConfig> pending_probes; |
| 302 | pending_probes_.swap(pending_probes); |
| 303 | return pending_probes; |
| 304 | } |
| 305 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 306 | void ProbeController::InitiateProbing( |
| 307 | int64_t now_ms, |
| 308 | std::initializer_list<int64_t> bitrates_to_probe, |
| 309 | bool probe_further) { |
| 310 | for (int64_t bitrate : bitrates_to_probe) { |
| 311 | RTC_DCHECK_GT(bitrate, 0); |
| 312 | int64_t max_probe_bitrate_bps = |
| 313 | max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps; |
| 314 | if (bitrate > max_probe_bitrate_bps) { |
| 315 | bitrate = max_probe_bitrate_bps; |
| 316 | probe_further = false; |
| 317 | } |
| 318 | |
| 319 | ProbeClusterConfig config; |
| 320 | config.at_time = Timestamp::ms(now_ms); |
| 321 | config.target_data_rate = DataRate::bps(rtc::dchecked_cast<int>(bitrate)); |
| 322 | config.target_duration = TimeDelta::ms(kMinProbeDurationMs); |
| 323 | config.target_probe_count = kMinProbePacketsSent; |
Sebastian Jansson | f2e3e7a | 2018-04-06 17:16:06 +0200 | [diff] [blame] | 324 | pending_probes_.push_back(config); |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 325 | } |
| 326 | time_last_probing_initiated_ms_ = now_ms; |
| 327 | if (probe_further) { |
| 328 | state_ = State::kWaitingForProbingResult; |
| 329 | min_bitrate_to_probe_further_bps_ = |
| 330 | (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100; |
| 331 | } else { |
| 332 | state_ = State::kProbingComplete; |
| 333 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 334 | } |
| 335 | } |
| 336 | |
Sebastian Jansson | 6bcd7f6 | 2018-02-27 17:07:02 +0100 | [diff] [blame] | 337 | } // namespace webrtc |