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 | |
| 11 | #include "webrtc/modules/congestion_controller/probe_controller.h" |
| 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 | |
| 16 | #include "webrtc/base/logging.h" |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 17 | #include "webrtc/base/safe_conversions.h" |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/include/metrics.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | namespace { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 23 | // Maximum waiting time from the time of initiating probing to getting |
| 24 | // the measured results back. |
| 25 | constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000; |
| 26 | |
| 27 | // Value of |min_bitrate_to_probe_further_bps_| that indicates |
| 28 | // further probing is disabled. |
| 29 | constexpr int kExponentialProbingDisabled = 0; |
| 30 | |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 31 | // Default probing bitrate limit. Applied only when the application didn't |
| 32 | // specify max bitrate. |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 33 | constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000; |
isheriff | 8e6a761 | 2016-09-29 05:36:59 -0700 | [diff] [blame] | 34 | |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 35 | // This is a limit on how often probing can be done when there is a BW |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 36 | // drop detected in ALR. |
| 37 | constexpr int64_t kAlrProbingIntervalMinMs = 5000; |
| 38 | |
| 39 | // Interval between probes when ALR periodic probing is enabled. |
| 40 | constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000; |
| 41 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 42 | // Minimum probe bitrate percentage to probe further for repeated probes, |
| 43 | // relative to the previous probe. For example, if 1Mbps probe results in |
| 44 | // 80kbps, then we'll probe again at 1.6Mbps. In that case second probe won't be |
| 45 | // sent if we get 600kbps from the first one. |
| 46 | constexpr int kRepeatedProbeMinPercentage = 70; |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 47 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 48 | } // namespace |
| 49 | |
elad.alon | 61ce37e | 2017-03-09 07:09:31 -0800 | [diff] [blame] | 50 | ProbeController::ProbeController(PacedSender* pacer, const Clock* clock) |
philipel | cb9ba30 | 2017-03-07 06:30:59 -0800 | [diff] [blame] | 51 | : pacer_(pacer), clock_(clock), enable_periodic_alr_probing_(false) { |
| 52 | Reset(); |
| 53 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 54 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 55 | void ProbeController::SetBitrates(int64_t min_bitrate_bps, |
| 56 | int64_t start_bitrate_bps, |
| 57 | int64_t max_bitrate_bps) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 58 | rtc::CritScope cs(&critsect_); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 59 | |
| 60 | if (start_bitrate_bps > 0) { |
| 61 | start_bitrate_bps_ = start_bitrate_bps; |
philipel | d1d247f | 2017-05-04 08:35:52 -0700 | [diff] [blame^] | 62 | estimated_bitrate_bps_ = start_bitrate_bps; |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 63 | } else if (start_bitrate_bps_ == 0) { |
| 64 | start_bitrate_bps_ = min_bitrate_bps; |
honghaiz | 906c5dc | 2016-11-15 14:39:06 -0800 | [diff] [blame] | 65 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 66 | |
philipel | da5e9d0 | 2017-01-17 02:08:28 -0800 | [diff] [blame] | 67 | // The reason we use the variable |old_max_bitrate_pbs| is because we |
| 68 | // need to set |max_bitrate_bps_| before we call InitiateProbing. |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 69 | int64_t old_max_bitrate_bps = max_bitrate_bps_; |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 70 | max_bitrate_bps_ = max_bitrate_bps; |
| 71 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 72 | switch (state_) { |
| 73 | case State::kInit: |
| 74 | if (network_state_ == kNetworkUp) |
| 75 | InitiateExponentialProbing(); |
| 76 | break; |
| 77 | |
| 78 | case State::kWaitingForProbingResult: |
| 79 | break; |
| 80 | |
| 81 | case State::kProbingComplete: |
philipel | da5e9d0 | 2017-01-17 02:08:28 -0800 | [diff] [blame] | 82 | // If the new max bitrate is higher than the old max bitrate and the |
| 83 | // estimate is lower than the new max bitrate then initiate probing. |
| 84 | if (estimated_bitrate_bps_ != 0 && |
| 85 | old_max_bitrate_bps < max_bitrate_bps_ && |
| 86 | estimated_bitrate_bps_ < max_bitrate_bps_) { |
philipel | 2df0734 | 2017-01-16 09:31:49 -0800 | [diff] [blame] | 87 | // The assumption is that if we jump more than 20% in the bandwidth |
| 88 | // estimate or if the bandwidth estimate is within 90% of the new |
| 89 | // max bitrate then the probing attempt was successful. |
| 90 | mid_call_probing_succcess_threshold_ = |
| 91 | std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9); |
| 92 | mid_call_probing_waiting_for_result_ = true; |
| 93 | mid_call_probing_bitrate_bps_ = max_bitrate_bps_; |
| 94 | |
| 95 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated", |
| 96 | max_bitrate_bps_ / 1000); |
| 97 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 98 | InitiateProbing(clock_->TimeInMilliseconds(), {max_bitrate_bps}, false); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 99 | } |
| 100 | break; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 101 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 104 | void ProbeController::OnNetworkStateChanged(NetworkState network_state) { |
| 105 | rtc::CritScope cs(&critsect_); |
| 106 | network_state_ = network_state; |
| 107 | if (network_state_ == kNetworkUp && state_ == State::kInit) |
| 108 | InitiateExponentialProbing(); |
| 109 | } |
| 110 | |
| 111 | void ProbeController::InitiateExponentialProbing() { |
| 112 | RTC_DCHECK(network_state_ == kNetworkUp); |
| 113 | RTC_DCHECK(state_ == State::kInit); |
| 114 | RTC_DCHECK_GT(start_bitrate_bps_, 0); |
| 115 | |
| 116 | // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of |
| 117 | // 1.2 Mbps to continue probing. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 118 | InitiateProbing(clock_->TimeInMilliseconds(), |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 119 | {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 120 | } |
| 121 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 122 | void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 123 | rtc::CritScope cs(&critsect_); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 124 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 125 | |
philipel | 2df0734 | 2017-01-16 09:31:49 -0800 | [diff] [blame] | 126 | if (mid_call_probing_waiting_for_result_ && |
| 127 | bitrate_bps >= mid_call_probing_succcess_threshold_) { |
| 128 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success", |
| 129 | mid_call_probing_bitrate_bps_ / 1000); |
| 130 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps", |
| 131 | bitrate_bps / 1000); |
| 132 | mid_call_probing_waiting_for_result_ = false; |
| 133 | } |
| 134 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 135 | if (state_ == State::kWaitingForProbingResult) { |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 136 | // Continue probing if probing results indicate channel has greater |
| 137 | // capacity. |
| 138 | LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps |
| 139 | << " Minimum to probe further: " |
| 140 | << min_bitrate_to_probe_further_bps_; |
philipel | 2df0734 | 2017-01-16 09:31:49 -0800 | [diff] [blame] | 141 | |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 142 | if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled && |
| 143 | bitrate_bps > min_bitrate_to_probe_further_bps_) { |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 144 | // Double the probing bitrate. |
| 145 | InitiateProbing(now_ms, {2 * bitrate_bps}, true); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 146 | } |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Detect a drop in estimated BW when operating in ALR and not already |
| 150 | // probing. The current response is to initiate a single probe session at the |
| 151 | // previous bitrate and immediately use the reported bitrate as the new |
| 152 | // bitrate. |
| 153 | // |
| 154 | // If the probe session fails, the assumption is that this drop was a |
| 155 | // real one from a competing flow or something else on the network and |
| 156 | // it ramps up from bitrate_bps. |
| 157 | if (state_ == State::kProbingComplete && |
| 158 | pacer_->GetApplicationLimitedRegionStartTime() && |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame] | 159 | bitrate_bps < 2 * estimated_bitrate_bps_ / 3 && |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 160 | (now_ms - last_alr_probing_time_) > kAlrProbingIntervalMinMs) { |
| 161 | LOG(LS_INFO) << "Detected big BW drop in ALR, start probe."; |
| 162 | // Track how often we probe in response to BW drop in ALR. |
| 163 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.AlrProbingIntervalInS", |
| 164 | (now_ms - last_alr_probing_time_) / 1000); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 165 | InitiateProbing(now_ms, {estimated_bitrate_bps_}, false); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 166 | last_alr_probing_time_ = now_ms; |
| 167 | |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 168 | // TODO(isheriff): May want to track when we did ALR probing in order |
| 169 | // to reset |last_alr_probing_time_| if we validate that it was a |
| 170 | // drop due to exogenous event. |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 171 | } |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 172 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 173 | estimated_bitrate_bps_ = bitrate_bps; |
| 174 | } |
| 175 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 176 | void ProbeController::EnablePeriodicAlrProbing(bool enable) { |
| 177 | rtc::CritScope cs(&critsect_); |
| 178 | enable_periodic_alr_probing_ = enable; |
| 179 | } |
| 180 | |
philipel | cb9ba30 | 2017-03-07 06:30:59 -0800 | [diff] [blame] | 181 | void ProbeController::Reset() { |
| 182 | rtc::CritScope cs(&critsect_); |
| 183 | network_state_ = kNetworkUp; |
| 184 | state_ = State::kInit; |
| 185 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 186 | time_last_probing_initiated_ms_ = 0; |
| 187 | estimated_bitrate_bps_ = 0; |
| 188 | start_bitrate_bps_ = 0; |
| 189 | max_bitrate_bps_ = 0; |
| 190 | last_alr_probing_time_ = clock_->TimeInMilliseconds(); |
| 191 | mid_call_probing_waiting_for_result_ = false; |
| 192 | } |
| 193 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 194 | void ProbeController::Process() { |
| 195 | rtc::CritScope cs(&critsect_); |
| 196 | |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 197 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 198 | |
philipel | 2df0734 | 2017-01-16 09:31:49 -0800 | [diff] [blame] | 199 | if (now_ms - time_last_probing_initiated_ms_ > |
| 200 | kMaxWaitingTimeForProbingResultMs) { |
| 201 | mid_call_probing_waiting_for_result_ = false; |
| 202 | |
| 203 | if (state_ == State::kWaitingForProbingResult) { |
| 204 | LOG(LS_INFO) << "kWaitingForProbingResult: timeout"; |
| 205 | state_ = State::kProbingComplete; |
| 206 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 207 | } |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 208 | } |
| 209 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 210 | if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_) |
| 211 | return; |
| 212 | |
| 213 | // Probe bandwidth periodically when in ALR state. |
| 214 | rtc::Optional<int64_t> alr_start_time = |
| 215 | pacer_->GetApplicationLimitedRegionStartTime(); |
philipel | d1d247f | 2017-05-04 08:35:52 -0700 | [diff] [blame^] | 216 | if (alr_start_time && estimated_bitrate_bps_ > 0) { |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 217 | int64_t next_probe_time_ms = |
| 218 | std::max(*alr_start_time, time_last_probing_initiated_ms_) + |
| 219 | kAlrPeriodicProbingIntervalMs; |
| 220 | if (now_ms >= next_probe_time_ms) { |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 221 | InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 226 | void ProbeController::InitiateProbing( |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 227 | int64_t now_ms, |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 228 | std::initializer_list<int64_t> bitrates_to_probe, |
| 229 | bool probe_further) { |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 230 | for (int64_t bitrate : bitrates_to_probe) { |
philipel | d1d247f | 2017-05-04 08:35:52 -0700 | [diff] [blame^] | 231 | RTC_DCHECK_GT(bitrate, 0); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 232 | int64_t max_probe_bitrate_bps = |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 233 | max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps; |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 234 | if (bitrate > max_probe_bitrate_bps) { |
| 235 | bitrate = max_probe_bitrate_bps; |
| 236 | probe_further = false; |
| 237 | } |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 238 | pacer_->CreateProbeCluster(rtc::dchecked_cast<int>(bitrate)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 239 | } |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 240 | time_last_probing_initiated_ms_ = now_ms; |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 241 | if (probe_further) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 242 | state_ = State::kWaitingForProbingResult; |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 243 | min_bitrate_to_probe_further_bps_ = |
| 244 | (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100; |
| 245 | } else { |
| 246 | state_ = State::kProbingComplete; |
| 247 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 248 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | } // namespace webrtc |