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