blob: 654f8b98f42d46d9830ee3f85044d8efed49bc89 [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)
philipelcb9ba302017-03-07 06:30:59 -080051 : pacer_(pacer), clock_(clock), enable_periodic_alr_probing_(false) {
52 Reset();
53}
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070054
sergeyu5bc39452016-12-15 10:42:09 -080055void ProbeController::SetBitrates(int64_t min_bitrate_bps,
56 int64_t start_bitrate_bps,
57 int64_t max_bitrate_bps) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070058 rtc::CritScope cs(&critsect_);
Sergey Ulanove2b15012016-11-22 16:08:30 -080059
60 if (start_bitrate_bps > 0) {
61 start_bitrate_bps_ = start_bitrate_bps;
62 } else if (start_bitrate_bps_ == 0) {
63 start_bitrate_bps_ = min_bitrate_bps;
honghaiz906c5dc2016-11-15 14:39:06 -080064 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070065
philipelda5e9d02017-01-17 02:08:28 -080066 // The reason we use the variable |old_max_bitrate_pbs| is because we
67 // need to set |max_bitrate_bps_| before we call InitiateProbing.
sergeyu5bc39452016-12-15 10:42:09 -080068 int64_t old_max_bitrate_bps = max_bitrate_bps_;
sergeyu07c147d2016-11-03 11:59:50 -070069 max_bitrate_bps_ = max_bitrate_bps;
70
Sergey Ulanove2b15012016-11-22 16:08:30 -080071 switch (state_) {
72 case State::kInit:
73 if (network_state_ == kNetworkUp)
74 InitiateExponentialProbing();
75 break;
76
77 case State::kWaitingForProbingResult:
78 break;
79
80 case State::kProbingComplete:
philipelda5e9d02017-01-17 02:08:28 -080081 // If the new max bitrate is higher than the old max bitrate and the
82 // estimate is lower than the new max bitrate then initiate probing.
83 if (estimated_bitrate_bps_ != 0 &&
84 old_max_bitrate_bps < max_bitrate_bps_ &&
85 estimated_bitrate_bps_ < max_bitrate_bps_) {
philipel2df07342017-01-16 09:31:49 -080086 // The assumption is that if we jump more than 20% in the bandwidth
87 // estimate or if the bandwidth estimate is within 90% of the new
88 // max bitrate then the probing attempt was successful.
89 mid_call_probing_succcess_threshold_ =
90 std::min(estimated_bitrate_bps_ * 1.2, max_bitrate_bps_ * 0.9);
91 mid_call_probing_waiting_for_result_ = true;
92 mid_call_probing_bitrate_bps_ = max_bitrate_bps_;
93
94 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Initiated",
95 max_bitrate_bps_ / 1000);
96
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
philipel2df07342017-01-16 09:31:49 -0800125 if (mid_call_probing_waiting_for_result_ &&
126 bitrate_bps >= mid_call_probing_succcess_threshold_) {
127 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.Success",
128 mid_call_probing_bitrate_bps_ / 1000);
129 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.MidCallProbing.ProbedKbps",
130 bitrate_bps / 1000);
131 mid_call_probing_waiting_for_result_ = false;
132 }
133
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700134 if (state_ == State::kWaitingForProbingResult) {
sergeyu9cef11b2016-12-02 11:03:01 -0800135 // Continue probing if probing results indicate channel has greater
136 // capacity.
137 LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
138 << " Minimum to probe further: "
139 << min_bitrate_to_probe_further_bps_;
philipel2df07342017-01-16 09:31:49 -0800140
sergeyu9cef11b2016-12-02 11:03:01 -0800141 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
142 bitrate_bps > min_bitrate_to_probe_further_bps_) {
sergeyu5bc39452016-12-15 10:42:09 -0800143 // Double the probing bitrate.
144 InitiateProbing(now_ms, {2 * bitrate_bps}, true);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700145 }
sergeyu80ed35e2016-11-28 13:11:13 -0800146 }
147
148 // Detect a drop in estimated BW when operating in ALR and not already
149 // probing. The current response is to initiate a single probe session at the
150 // previous bitrate and immediately use the reported bitrate as the new
151 // bitrate.
152 //
153 // If the probe session fails, the assumption is that this drop was a
154 // real one from a competing flow or something else on the network and
155 // it ramps up from bitrate_bps.
156 if (state_ == State::kProbingComplete &&
157 pacer_->GetApplicationLimitedRegionStartTime() &&
stefanf00497c2017-01-27 02:27:33 -0800158 bitrate_bps < 2 * estimated_bitrate_bps_ / 3 &&
sergeyu80ed35e2016-11-28 13:11:13 -0800159 (now_ms - last_alr_probing_time_) > kAlrProbingIntervalMinMs) {
160 LOG(LS_INFO) << "Detected big BW drop in ALR, start probe.";
161 // Track how often we probe in response to BW drop in ALR.
162 RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.AlrProbingIntervalInS",
163 (now_ms - last_alr_probing_time_) / 1000);
sergeyu5bc39452016-12-15 10:42:09 -0800164 InitiateProbing(now_ms, {estimated_bitrate_bps_}, false);
sergeyu80ed35e2016-11-28 13:11:13 -0800165 last_alr_probing_time_ = now_ms;
166
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700167 // TODO(isheriff): May want to track when we did ALR probing in order
168 // to reset |last_alr_probing_time_| if we validate that it was a
169 // drop due to exogenous event.
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700170 }
sergeyu80ed35e2016-11-28 13:11:13 -0800171
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700172 estimated_bitrate_bps_ = bitrate_bps;
173}
174
sergeyu80ed35e2016-11-28 13:11:13 -0800175void ProbeController::EnablePeriodicAlrProbing(bool enable) {
176 rtc::CritScope cs(&critsect_);
177 enable_periodic_alr_probing_ = enable;
178}
179
philipelcb9ba302017-03-07 06:30:59 -0800180void ProbeController::Reset() {
181 rtc::CritScope cs(&critsect_);
182 network_state_ = kNetworkUp;
183 state_ = State::kInit;
184 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
185 time_last_probing_initiated_ms_ = 0;
186 estimated_bitrate_bps_ = 0;
187 start_bitrate_bps_ = 0;
188 max_bitrate_bps_ = 0;
189 last_alr_probing_time_ = clock_->TimeInMilliseconds();
190 mid_call_probing_waiting_for_result_ = false;
191}
192
sergeyu80ed35e2016-11-28 13:11:13 -0800193void ProbeController::Process() {
194 rtc::CritScope cs(&critsect_);
195
sergeyu9cef11b2016-12-02 11:03:01 -0800196 int64_t now_ms = clock_->TimeInMilliseconds();
197
philipel2df07342017-01-16 09:31:49 -0800198 if (now_ms - time_last_probing_initiated_ms_ >
199 kMaxWaitingTimeForProbingResultMs) {
200 mid_call_probing_waiting_for_result_ = false;
201
202 if (state_ == State::kWaitingForProbingResult) {
203 LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
204 state_ = State::kProbingComplete;
205 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
206 }
sergeyu9cef11b2016-12-02 11:03:01 -0800207 }
208
sergeyu80ed35e2016-11-28 13:11:13 -0800209 if (state_ != State::kProbingComplete || !enable_periodic_alr_probing_)
210 return;
211
212 // Probe bandwidth periodically when in ALR state.
213 rtc::Optional<int64_t> alr_start_time =
214 pacer_->GetApplicationLimitedRegionStartTime();
215 if (alr_start_time) {
sergeyu80ed35e2016-11-28 13:11:13 -0800216 int64_t next_probe_time_ms =
217 std::max(*alr_start_time, time_last_probing_initiated_ms_) +
218 kAlrPeriodicProbingIntervalMs;
219 if (now_ms >= next_probe_time_ms) {
sergeyu5bc39452016-12-15 10:42:09 -0800220 InitiateProbing(now_ms, {estimated_bitrate_bps_ * 2}, true);
sergeyu80ed35e2016-11-28 13:11:13 -0800221 }
222 }
223}
224
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700225void ProbeController::InitiateProbing(
sergeyu80ed35e2016-11-28 13:11:13 -0800226 int64_t now_ms,
sergeyu5bc39452016-12-15 10:42:09 -0800227 std::initializer_list<int64_t> bitrates_to_probe,
228 bool probe_further) {
sergeyu5bc39452016-12-15 10:42:09 -0800229 for (int64_t bitrate : bitrates_to_probe) {
230 int64_t max_probe_bitrate_bps =
sergeyu07c147d2016-11-03 11:59:50 -0700231 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
sergeyu5bc39452016-12-15 10:42:09 -0800232 if (bitrate > max_probe_bitrate_bps) {
233 bitrate = max_probe_bitrate_bps;
234 probe_further = false;
235 }
kwibergd3edd772017-03-01 18:52:48 -0800236 pacer_->CreateProbeCluster(rtc::dchecked_cast<int>(bitrate));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700237 }
sergeyu80ed35e2016-11-28 13:11:13 -0800238 time_last_probing_initiated_ms_ = now_ms;
sergeyu5bc39452016-12-15 10:42:09 -0800239 if (probe_further) {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700240 state_ = State::kWaitingForProbingResult;
sergeyu5bc39452016-12-15 10:42:09 -0800241 min_bitrate_to_probe_further_bps_ =
242 (*(bitrates_to_probe.end() - 1)) * kRepeatedProbeMinPercentage / 100;
243 } else {
244 state_ = State::kProbingComplete;
245 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
246 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700247}
248
249} // namespace webrtc