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