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. |
| 38 | constexpr int kDefaultMaxProbingBitrateBps = 100000000; |
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 |
| 41 | // drop detected in ALR region. |
| 42 | constexpr int kAlrProbingIntervalLimitMs = 5000; |
| 43 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | ProbeController::ProbeController(PacedSender* pacer, Clock* clock) |
| 47 | : pacer_(pacer), |
| 48 | clock_(clock), |
| 49 | state_(State::kInit), |
| 50 | min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled), |
| 51 | time_last_probing_initiated_ms_(0), |
| 52 | estimated_bitrate_bps_(0), |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 53 | max_bitrate_bps_(0), |
| 54 | last_alr_probing_time_(clock_->TimeInMilliseconds()) {} |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 55 | |
| 56 | void ProbeController::SetBitrates(int min_bitrate_bps, |
| 57 | int start_bitrate_bps, |
| 58 | int max_bitrate_bps) { |
| 59 | rtc::CritScope cs(&critsect_); |
honghaiz | 906c5dc | 2016-11-15 14:39:06 -0800 | [diff] [blame] | 60 | if (state_ == State::kInit) { |
| 61 | // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of |
| 62 | // 1.2 Mbps to continue probing. |
| 63 | InitiateProbing({3 * start_bitrate_bps, 6 * start_bitrate_bps}, |
| 64 | 4 * start_bitrate_bps); |
| 65 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 66 | |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 67 | int old_max_bitrate_bps = max_bitrate_bps_; |
| 68 | max_bitrate_bps_ = max_bitrate_bps; |
| 69 | |
honghaiz | 906c5dc | 2016-11-15 14:39:06 -0800 | [diff] [blame] | 70 | // Only do probing if: |
| 71 | // we are mid-call, which we consider to be if |
| 72 | // exponential probing is not active and |
| 73 | // |estimated_bitrate_bps_| is valid (> 0) and |
| 74 | // the current bitrate is lower than the new |max_bitrate_bps|, and |
| 75 | // |max_bitrate_bps_| was increased. |
| 76 | if (state_ != State::kWaitingForProbingResult && |
| 77 | estimated_bitrate_bps_ != 0 && |
| 78 | estimated_bitrate_bps_ < old_max_bitrate_bps && |
| 79 | max_bitrate_bps_ > old_max_bitrate_bps) { |
| 80 | InitiateProbing({max_bitrate_bps}, kExponentialProbingDisabled); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 81 | } |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void ProbeController::SetEstimatedBitrate(int bitrate_bps) { |
| 85 | rtc::CritScope cs(&critsect_); |
| 86 | if (state_ == State::kWaitingForProbingResult) { |
| 87 | if ((clock_->TimeInMilliseconds() - time_last_probing_initiated_ms_) > |
| 88 | kMaxWaitingTimeForProbingResultMs) { |
| 89 | LOG(LS_INFO) << "kWaitingForProbingResult: timeout"; |
| 90 | state_ = State::kProbingComplete; |
| 91 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 92 | } else { |
| 93 | // Continue probing if probing results indicate channel has greater |
| 94 | // capacity. |
| 95 | LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps |
| 96 | << " Minimum to probe further: " |
| 97 | << min_bitrate_to_probe_further_bps_; |
| 98 | if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled && |
| 99 | bitrate_bps > min_bitrate_to_probe_further_bps_) { |
| 100 | // Double the probing bitrate and expect a minimum of 25% gain to |
| 101 | // continue probing. |
| 102 | InitiateProbing({2 * bitrate_bps}, 1.25 * bitrate_bps); |
| 103 | } |
| 104 | } |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 105 | } else { |
| 106 | // A drop in estimated BW when operating in ALR and not already probing. |
| 107 | // The current response is to initiate a single probe session at the |
| 108 | // previous bitrate and immediately use the reported bitrate as the new |
| 109 | // bitrate. |
| 110 | // |
| 111 | // If the probe session fails, the assumption is that this drop was a |
| 112 | // real one from a competing flow or something else on the network and |
| 113 | // it ramps up from bitrate_bps. |
| 114 | if (pacer_->InApplicationLimitedRegion() && |
| 115 | bitrate_bps < 0.5 * estimated_bitrate_bps_) { |
| 116 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 117 | if ((now_ms - last_alr_probing_time_) > kAlrProbingIntervalLimitMs) { |
| 118 | LOG(LS_INFO) << "Detected big BW drop in ALR, start probe."; |
| 119 | // Track how often we probe in response to BW drop in ALR. |
| 120 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.AlrProbingIntervalInS", |
| 121 | (now_ms - last_alr_probing_time_) / 1000); |
| 122 | InitiateProbing({estimated_bitrate_bps_}, kExponentialProbingDisabled); |
| 123 | last_alr_probing_time_ = now_ms; |
| 124 | } |
| 125 | } |
| 126 | // TODO(isheriff): May want to track when we did ALR probing in order |
| 127 | // to reset |last_alr_probing_time_| if we validate that it was a |
| 128 | // drop due to exogenous event. |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 129 | } |
| 130 | estimated_bitrate_bps_ = bitrate_bps; |
| 131 | } |
| 132 | |
| 133 | void ProbeController::InitiateProbing( |
| 134 | std::initializer_list<int> bitrates_to_probe, |
| 135 | int min_bitrate_to_probe_further_bps) { |
| 136 | bool first_cluster = true; |
| 137 | for (int bitrate : bitrates_to_probe) { |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 138 | int max_probe_bitrate_bps = |
| 139 | max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps; |
| 140 | bitrate = std::min(bitrate, max_probe_bitrate_bps); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 141 | if (first_cluster) { |
| 142 | pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster + 1); |
| 143 | first_cluster = false; |
| 144 | } else { |
| 145 | pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster); |
| 146 | } |
| 147 | } |
| 148 | min_bitrate_to_probe_further_bps_ = min_bitrate_to_probe_further_bps; |
| 149 | time_last_probing_initiated_ms_ = clock_->TimeInMilliseconds(); |
| 150 | if (min_bitrate_to_probe_further_bps == kExponentialProbingDisabled) |
| 151 | state_ = State::kProbingComplete; |
| 152 | else |
| 153 | state_ = State::kWaitingForProbingResult; |
| 154 | } |
| 155 | |
| 156 | } // namespace webrtc |