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