blob: 6ce7d6b5df37b0a9df2cbf5e38ca710029dcf1d1 [file] [log] [blame]
Irfan Sheriffb2540bb2016-09-12 12:28:54 -07001/*
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
isheriff8e6a7612016-09-29 05:36:59 -070013#include <algorithm>
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070014#include <initializer_list>
15
16#include "webrtc/base/logging.h"
sergeyu5bc39452016-12-15 10:42:09 -080017#include "webrtc/base/safe_conversions.h"
Irfan Sheriff1eb12932016-10-18 17:04:25 -070018#include "webrtc/system_wrappers/include/metrics.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070019
20namespace webrtc {
21
22namespace {
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.
27constexpr int kProbeDeltasPerCluster = 5;
28
29// Maximum waiting time from the time of initiating probing to getting
30// the measured results back.
31constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
32
33// Value of |min_bitrate_to_probe_further_bps_| that indicates
34// further probing is disabled.
35constexpr int kExponentialProbingDisabled = 0;
36
sergeyu07c147d2016-11-03 11:59:50 -070037// Default probing bitrate limit. Applied only when the application didn't
38// specify max bitrate.
sergeyu5bc39452016-12-15 10:42:09 -080039constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
isheriff8e6a7612016-09-29 05:36:59 -070040
Irfan Sheriff1eb12932016-10-18 17:04:25 -070041// This is a limit on how often probing can be done when there is a BW
sergeyu80ed35e2016-11-28 13:11:13 -080042// drop detected in ALR.
43constexpr int64_t kAlrProbingIntervalMinMs = 5000;
44
45// Interval between probes when ALR periodic probing is enabled.
46constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
47
sergeyu5bc39452016-12-15 10:42:09 -080048// 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.
52constexpr int kRepeatedProbeMinPercentage = 70;
Irfan Sheriff1eb12932016-10-18 17:04:25 -070053
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070054} // namespace
55
56ProbeController::ProbeController(PacedSender* pacer, Clock* clock)
57 : pacer_(pacer),
58 clock_(clock),
Sergey Ulanove2b15012016-11-22 16:08:30 -080059 network_state_(kNetworkUp),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070060 state_(State::kInit),
61 min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled),
62 time_last_probing_initiated_ms_(0),
63 estimated_bitrate_bps_(0),
Sergey Ulanove2b15012016-11-22 16:08:30 -080064 start_bitrate_bps_(0),
Irfan Sheriff1eb12932016-10-18 17:04:25 -070065 max_bitrate_bps_(0),
sergeyu80ed35e2016-11-28 13:11:13 -080066 last_alr_probing_time_(clock_->TimeInMilliseconds()),
67 enable_periodic_alr_probing_(false) {}
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070068
sergeyu5bc39452016-12-15 10:42:09 -080069void ProbeController::SetBitrates(int64_t min_bitrate_bps,
70 int64_t start_bitrate_bps,
71 int64_t max_bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070072 rtc::CritScope cs(&critsect_);
Sergey Ulanove2b15012016-11-22 16:08:30 -080073
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;
honghaiz906c5dc2016-11-15 14:39:06 -080078 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070079
sergeyu5bc39452016-12-15 10:42:09 -080080 int64_t old_max_bitrate_bps = max_bitrate_bps_;
sergeyu07c147d2016-11-03 11:59:50 -070081 max_bitrate_bps_ = max_bitrate_bps;
82
Sergey Ulanove2b15012016-11-22 16:08:30 -080083 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.
sergeyu5bc39452016-12-15 10:42:09 -080094 if (estimated_bitrate_bps_ != kExponentialProbingDisabled &&
Sergey Ulanove2b15012016-11-22 16:08:30 -080095 estimated_bitrate_bps_ < old_max_bitrate_bps &&
96 max_bitrate_bps_ > old_max_bitrate_bps) {
sergeyu5bc39452016-12-15 10:42:09 -080097 InitiateProbing(clock_->TimeInMilliseconds(), {max_bitrate_bps}, false);
Sergey Ulanove2b15012016-11-22 16:08:30 -080098 }
99 break;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700100 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700101}
102
Sergey Ulanove2b15012016-11-22 16:08:30 -0800103void 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
110void 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.
sergeyu80ed35e2016-11-28 13:11:13 -0800117 InitiateProbing(clock_->TimeInMilliseconds(),
sergeyu5bc39452016-12-15 10:42:09 -0800118 {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800119}
120
sergeyu5bc39452016-12-15 10:42:09 -0800121void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700122 rtc::CritScope cs(&critsect_);
sergeyu80ed35e2016-11-28 13:11:13 -0800123 int64_t now_ms = clock_->TimeInMilliseconds();
124
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700125 if (state_ == State::kWaitingForProbingResult) {
sergeyu9cef11b2016-12-02 11:03:01 -0800126 // 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_) {
sergeyu5bc39452016-12-15 10:42:09 -0800133 // Double the probing bitrate.
134 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700135 }
sergeyu80ed35e2016-11-28 13:11:13 -0800136 }
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);
sergeyu5bc39452016-12-15 10:42:09 -0800154 InitiateProbing(now_ms, {estimated_bitrate_bps_}, false);
sergeyu80ed35e2016-11-28 13:11:13 -0800155 last_alr_probing_time_ = now_ms;
156
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700157 // 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 Sheriffb2540bb2016-09-12 12:28:54 -0700160 }
sergeyu80ed35e2016-11-28 13:11:13 -0800161
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700162 estimated_bitrate_bps_ = bitrate_bps;
163}
164
sergeyu80ed35e2016-11-28 13:11:13 -0800165void ProbeController::EnablePeriodicAlrProbing(bool enable) {
166 rtc::CritScope cs(&critsect_);
167 enable_periodic_alr_probing_ = enable;
168}
169
170void ProbeController::Process() {
171 rtc::CritScope cs(&critsect_);
172
sergeyu9cef11b2016-12-02 11:03:01 -0800173 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
sergeyu80ed35e2016-11-28 13:11:13 -0800183 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) {
sergeyu80ed35e2016-11-28 13:11:13 -0800190 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) {
sergeyu5bc39452016-12-15 10:42:09 -0800194 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
sergeyu80ed35e2016-11-28 13:11:13 -0800195 }
196 }
197}
198
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700199void ProbeController::InitiateProbing(
sergeyu80ed35e2016-11-28 13:11:13 -0800200 int64_t now_ms,
sergeyu5bc39452016-12-15 10:42:09 -0800201 std::initializer_list<int64_t> bitrates_to_probe,
202 bool probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700203 bool first_cluster = true;
sergeyu5bc39452016-12-15 10:42:09 -0800204 for (int64_t bitrate : bitrates_to_probe) {
205 int64_t max_probe_bitrate_bps =
sergeyu07c147d2016-11-03 11:59:50 -0700206 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
sergeyu5bc39452016-12-15 10:42:09 -0800207 if (bitrate > max_probe_bitrate_bps) {
208 bitrate = max_probe_bitrate_bps;
209 probe_further = false;
210 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700211 if (first_cluster) {
sergeyu5bc39452016-12-15 10:42:09 -0800212 pacer_->CreateProbeCluster(rtc::checked_cast<int>(bitrate),
213 kProbeDeltasPerCluster + 1);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700214 first_cluster = false;
215 } else {
sergeyu5bc39452016-12-15 10:42:09 -0800216 pacer_->CreateProbeCluster(rtc::checked_cast<int>(bitrate),
217 kProbeDeltasPerCluster);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700218 }
219 }
sergeyu80ed35e2016-11-28 13:11:13 -0800220 time_last_probing_initiated_ms_ = now_ms;
sergeyu5bc39452016-12-15 10:42:09 -0800221 if (probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700222 state_ = State::kWaitingForProbingResult;
sergeyu5bc39452016-12-15 10:42:09 -0800223 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 Sheriffb2540bb2016-09-12 12:28:54 -0700229}
230
231} // namespace webrtc