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