blob: bf81517b7044d33be4ecd3fcf38896df8ff560b6 [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 {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070023// Maximum waiting time from the time of initiating probing to getting
24// the measured results back.
25constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
26
27// Value of |min_bitrate_to_probe_further_bps_| that indicates
28// further probing is disabled.
29constexpr int kExponentialProbingDisabled = 0;
30
sergeyu07c147d2016-11-03 11:59:50 -070031// Default probing bitrate limit. Applied only when the application didn't
32// specify max bitrate.
sergeyu5bc39452016-12-15 10:42:09 -080033constexpr int64_t kDefaultMaxProbingBitrateBps = 5000000;
isheriff8e6a7612016-09-29 05:36:59 -070034
Irfan Sheriff1eb12932016-10-18 17:04:25 -070035// This is a limit on how often probing can be done when there is a BW
sergeyu80ed35e2016-11-28 13:11:13 -080036// drop detected in ALR.
37constexpr int64_t kAlrProbingIntervalMinMs = 5000;
38
39// Interval between probes when ALR periodic probing is enabled.
40constexpr int64_t kAlrPeriodicProbingIntervalMs = 5000;
41
sergeyu5bc39452016-12-15 10:42:09 -080042// 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.
46constexpr int kRepeatedProbeMinPercentage = 70;
Irfan Sheriff1eb12932016-10-18 17:04:25 -070047
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070048} // namespace
49
50ProbeController::ProbeController(PacedSender* pacer, Clock* clock)
51 : pacer_(pacer),
52 clock_(clock),
Sergey Ulanove2b15012016-11-22 16:08:30 -080053 network_state_(kNetworkUp),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070054 state_(State::kInit),
55 min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled),
56 time_last_probing_initiated_ms_(0),
57 estimated_bitrate_bps_(0),
Sergey Ulanove2b15012016-11-22 16:08:30 -080058 start_bitrate_bps_(0),
Irfan Sheriff1eb12932016-10-18 17:04:25 -070059 max_bitrate_bps_(0),
sergeyu80ed35e2016-11-28 13:11:13 -080060 last_alr_probing_time_(clock_->TimeInMilliseconds()),
61 enable_periodic_alr_probing_(false) {}
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070062
sergeyu5bc39452016-12-15 10:42:09 -080063void ProbeController::SetBitrates(int64_t min_bitrate_bps,
64 int64_t start_bitrate_bps,
65 int64_t max_bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070066 rtc::CritScope cs(&critsect_);
Sergey Ulanove2b15012016-11-22 16:08:30 -080067
68 if (start_bitrate_bps > 0) {
69 start_bitrate_bps_ = start_bitrate_bps;
70 } else if (start_bitrate_bps_ == 0) {
71 start_bitrate_bps_ = min_bitrate_bps;
honghaiz906c5dc2016-11-15 14:39:06 -080072 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070073
sergeyu5bc39452016-12-15 10:42:09 -080074 int64_t old_max_bitrate_bps = max_bitrate_bps_;
sergeyu07c147d2016-11-03 11:59:50 -070075 max_bitrate_bps_ = max_bitrate_bps;
76
Sergey Ulanove2b15012016-11-22 16:08:30 -080077 switch (state_) {
78 case State::kInit:
79 if (network_state_ == kNetworkUp)
80 InitiateExponentialProbing();
81 break;
82
83 case State::kWaitingForProbingResult:
84 break;
85
86 case State::kProbingComplete:
87 // Initiate probing when |max_bitrate_| was increased mid-call.
sergeyu5bc39452016-12-15 10:42:09 -080088 if (estimated_bitrate_bps_ != kExponentialProbingDisabled &&
Sergey Ulanove2b15012016-11-22 16:08:30 -080089 estimated_bitrate_bps_ < old_max_bitrate_bps &&
90 max_bitrate_bps_ > old_max_bitrate_bps) {
sergeyu5bc39452016-12-15 10:42:09 -080091 InitiateProbing(clock_->TimeInMilliseconds(), {max_bitrate_bps}, false);
Sergey Ulanove2b15012016-11-22 16:08:30 -080092 }
93 break;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070094 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070095}
96
Sergey Ulanove2b15012016-11-22 16:08:30 -080097void ProbeController::OnNetworkStateChanged(NetworkState network_state) {
98 rtc::CritScope cs(&critsect_);
99 network_state_ = network_state;
100 if (network_state_ == kNetworkUp && state_ == State::kInit)
101 InitiateExponentialProbing();
102}
103
104void ProbeController::InitiateExponentialProbing() {
105 RTC_DCHECK(network_state_ == kNetworkUp);
106 RTC_DCHECK(state_ == State::kInit);
107 RTC_DCHECK_GT(start_bitrate_bps_, 0);
108
109 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
110 // 1.2 Mbps to continue probing.
sergeyu80ed35e2016-11-28 13:11:13 -0800111 InitiateProbing(clock_->TimeInMilliseconds(),
sergeyu5bc39452016-12-15 10:42:09 -0800112 {3 * start_bitrate_bps_, 6 * start_bitrate_bps_}, true);
Sergey Ulanove2b15012016-11-22 16:08:30 -0800113}
114
sergeyu5bc39452016-12-15 10:42:09 -0800115void ProbeController::SetEstimatedBitrate(int64_t bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700116 rtc::CritScope cs(&critsect_);
sergeyu80ed35e2016-11-28 13:11:13 -0800117 int64_t now_ms = clock_->TimeInMilliseconds();
118
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700119 if (state_ == State::kWaitingForProbingResult) {
sergeyu9cef11b2016-12-02 11:03:01 -0800120 // Continue probing if probing results indicate channel has greater
121 // capacity.
122 LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
123 << " Minimum to probe further: "
124 << min_bitrate_to_probe_further_bps_;
125 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
126 bitrate_bps > min_bitrate_to_probe_further_bps_) {
sergeyu5bc39452016-12-15 10:42:09 -0800127 // Double the probing bitrate.
128 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700129 }
sergeyu80ed35e2016-11-28 13:11:13 -0800130 }
131
132 // Detect a drop in estimated BW when operating in ALR and not already
133 // probing. The current response is to initiate a single probe session at the
134 // previous bitrate and immediately use the reported bitrate as the new
135 // bitrate.
136 //
137 // If the probe session fails, the assumption is that this drop was a
138 // real one from a competing flow or something else on the network and
139 // it ramps up from bitrate_bps.
140 if (state_ == State::kProbingComplete &&
141 pacer_->GetApplicationLimitedRegionStartTime() &&
142 bitrate_bps < estimated_bitrate_bps_ / 2 &&
143 (now_ms - last_alr_probing_time_) > kAlrProbingIntervalMinMs) {
144 LOG(LS_INFO) << "Detected big BW drop in ALR, start probe.";
145 // Track how often we probe in response to BW drop in ALR.
146 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.AlrProbingIntervalInS",
147 (now_ms - last_alr_probing_time_) / 1000);
sergeyu5bc39452016-12-15 10:42:09 -0800148 InitiateProbing(now_ms, {estimated_bitrate_bps_}, false);
sergeyu80ed35e2016-11-28 13:11:13 -0800149 last_alr_probing_time_ = now_ms;
150
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700151 // TODO(isheriff): May want to track when we did ALR probing in order
152 // to reset |last_alr_probing_time_| if we validate that it was a
153 // drop due to exogenous event.
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700154 }
sergeyu80ed35e2016-11-28 13:11:13 -0800155
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700156 estimated_bitrate_bps_ = bitrate_bps;
157}
158
sergeyu80ed35e2016-11-28 13:11:13 -0800159void ProbeController::EnablePeriodicAlrProbing(bool enable) {
160 rtc::CritScope cs(&critsect_);
161 enable_periodic_alr_probing_ = enable;
162}
163
164void ProbeController::Process() {
165 rtc::CritScope cs(&critsect_);
166
sergeyu9cef11b2016-12-02 11:03:01 -0800167 int64_t now_ms = clock_->TimeInMilliseconds();
168
169 if (state_ == State::kWaitingForProbingResult &&
170 (now_ms - time_last_probing_initiated_ms_) >
171 kMaxWaitingTimeForProbingResultMs) {
172 LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
173 state_ = State::kProbingComplete;
174 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
175 }
176
sergeyu80ed35e2016-11-28 13:11:13 -0800177 if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_)
178 return;
179
180 // Probe bandwidth periodically when in ALR state.
181 rtc::Optional<int64_t> alr_start_time =
182 pacer_->GetApplicationLimitedRegionStartTime();
183 if (alr_start_time) {
sergeyu80ed35e2016-11-28 13:11:13 -0800184 int64_t next_probe_time_ms =
185 std::max(*alr_start_time, time_last_probing_initiated_ms_) +
186 kAlrPeriodicProbingIntervalMs;
187 if (now_ms >= next_probe_time_ms) {
sergeyu5bc39452016-12-15 10:42:09 -0800188 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
sergeyu80ed35e2016-11-28 13:11:13 -0800189 }
190 }
191}
192
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700193void ProbeController::InitiateProbing(
sergeyu80ed35e2016-11-28 13:11:13 -0800194 int64_t now_ms,
sergeyu5bc39452016-12-15 10:42:09 -0800195 std::initializer_list<int64_t> bitrates_to_probe,
196 bool probe_further) {
sergeyu5bc39452016-12-15 10:42:09 -0800197 for (int64_t bitrate : bitrates_to_probe) {
198 int64_t max_probe_bitrate_bps =
sergeyu07c147d2016-11-03 11:59:50 -0700199 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
sergeyu5bc39452016-12-15 10:42:09 -0800200 if (bitrate > max_probe_bitrate_bps) {
201 bitrate = max_probe_bitrate_bps;
202 probe_further = false;
203 }
philipelfd58b612017-01-04 07:05:25 -0800204 pacer_->CreateProbeCluster(rtc::checked_cast<int>(bitrate));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700205 }
sergeyu80ed35e2016-11-28 13:11:13 -0800206 time_last_probing_initiated_ms_ = now_ms;
sergeyu5bc39452016-12-15 10:42:09 -0800207 if (probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700208 state_ = State::kWaitingForProbingResult;
sergeyu5bc39452016-12-15 10:42:09 -0800209 min_bitrate_to_probe_further_bps_ =
210 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
211 } else {
212 state_ = State::kProbingComplete;
213 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
214 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700215}
216
217} // namespace webrtc