blob: 1749e29ff75b9ecafdba2268abfd8df76eff42e4 [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"
Irfan Sheriff1eb12932016-10-18 17:04:25 -070017#include "webrtc/system_wrappers/include/metrics.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070018
19namespace webrtc {
20
21namespace {
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.
26constexpr int kProbeDeltasPerCluster = 5;
27
28// Maximum waiting time from the time of initiating probing to getting
29// the measured results back.
30constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000;
31
32// Value of |min_bitrate_to_probe_further_bps_| that indicates
33// further probing is disabled.
34constexpr int kExponentialProbingDisabled = 0;
35
sergeyu07c147d2016-11-03 11:59:50 -070036// Default probing bitrate limit. Applied only when the application didn't
37// specify max bitrate.
38constexpr int kDefaultMaxProbingBitrateBps = 100000000;
isheriff8e6a7612016-09-29 05:36:59 -070039
Irfan Sheriff1eb12932016-10-18 17:04:25 -070040// This is a limit on how often probing can be done when there is a BW
41// drop detected in ALR region.
42constexpr int kAlrProbingIntervalLimitMs = 5000;
43
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070044} // namespace
45
46ProbeController::ProbeController(PacedSender* pacer, Clock* clock)
47 : pacer_(pacer),
48 clock_(clock),
Sergey Ulanove2b15012016-11-22 16:08:30 -080049 network_state_(kNetworkUp),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070050 state_(State::kInit),
51 min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled),
52 time_last_probing_initiated_ms_(0),
53 estimated_bitrate_bps_(0),
Sergey Ulanove2b15012016-11-22 16:08:30 -080054 start_bitrate_bps_(0),
Irfan Sheriff1eb12932016-10-18 17:04:25 -070055 max_bitrate_bps_(0),
56 last_alr_probing_time_(clock_->TimeInMilliseconds()) {}
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070057
58void ProbeController::SetBitrates(int min_bitrate_bps,
59 int start_bitrate_bps,
60 int max_bitrate_bps) {
61 rtc::CritScope cs(&critsect_);
Sergey Ulanove2b15012016-11-22 16:08:30 -080062
63 if (start_bitrate_bps > 0) {
64 start_bitrate_bps_ = start_bitrate_bps;
65 } else if (start_bitrate_bps_ == 0) {
66 start_bitrate_bps_ = min_bitrate_bps;
honghaiz906c5dc2016-11-15 14:39:06 -080067 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070068
sergeyu07c147d2016-11-03 11:59:50 -070069 int old_max_bitrate_bps = max_bitrate_bps_;
70 max_bitrate_bps_ = max_bitrate_bps;
71
Sergey Ulanove2b15012016-11-22 16:08:30 -080072 switch (state_) {
73 case State::kInit:
74 if (network_state_ == kNetworkUp)
75 InitiateExponentialProbing();
76 break;
77
78 case State::kWaitingForProbingResult:
79 break;
80
81 case State::kProbingComplete:
82 // Initiate probing when |max_bitrate_| was increased mid-call.
83 if (estimated_bitrate_bps_ != 0 &&
84 estimated_bitrate_bps_ < old_max_bitrate_bps &&
85 max_bitrate_bps_ > old_max_bitrate_bps) {
86 InitiateProbing({max_bitrate_bps}, kExponentialProbingDisabled);
87 }
88 break;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070089 }
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070090}
91
Sergey Ulanove2b15012016-11-22 16:08:30 -080092void ProbeController::OnNetworkStateChanged(NetworkState network_state) {
93 rtc::CritScope cs(&critsect_);
94 network_state_ = network_state;
95 if (network_state_ == kNetworkUp && state_ == State::kInit)
96 InitiateExponentialProbing();
97}
98
99void ProbeController::InitiateExponentialProbing() {
100 RTC_DCHECK(network_state_ == kNetworkUp);
101 RTC_DCHECK(state_ == State::kInit);
102 RTC_DCHECK_GT(start_bitrate_bps_, 0);
103
104 // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of
105 // 1.2 Mbps to continue probing.
106 InitiateProbing({3 * start_bitrate_bps_, 6 * start_bitrate_bps_},
107 4 * start_bitrate_bps_);
108}
109
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700110void ProbeController::SetEstimatedBitrate(int bitrate_bps) {
111 rtc::CritScope cs(&critsect_);
112 if (state_ == State::kWaitingForProbingResult) {
113 if ((clock_->TimeInMilliseconds() - time_last_probing_initiated_ms_) >
114 kMaxWaitingTimeForProbingResultMs) {
115 LOG(LS_INFO) << "kWaitingForProbingResult: timeout";
116 state_ = State::kProbingComplete;
117 min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled;
118 } else {
119 // Continue probing if probing results indicate channel has greater
120 // capacity.
121 LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps
122 << " Minimum to probe further: "
123 << min_bitrate_to_probe_further_bps_;
124 if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled &&
125 bitrate_bps > min_bitrate_to_probe_further_bps_) {
126 // Double the probing bitrate and expect a minimum of 25% gain to
127 // continue probing.
128 InitiateProbing({2 * bitrate_bps}, 1.25 * bitrate_bps);
129 }
130 }
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700131 } else {
132 // A drop in estimated BW when operating in ALR and not already probing.
133 // 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 (pacer_->InApplicationLimitedRegion() &&
141 bitrate_bps < 0.5 * estimated_bitrate_bps_) {
142 int64_t now_ms = clock_->TimeInMilliseconds();
143 if ((now_ms - last_alr_probing_time_) > kAlrProbingIntervalLimitMs) {
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);
148 InitiateProbing({estimated_bitrate_bps_}, kExponentialProbingDisabled);
149 last_alr_probing_time_ = now_ms;
150 }
151 }
152 // TODO(isheriff): May want to track when we did ALR probing in order
153 // to reset |last_alr_probing_time_| if we validate that it was a
154 // drop due to exogenous event.
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700155 }
156 estimated_bitrate_bps_ = bitrate_bps;
157}
158
159void ProbeController::InitiateProbing(
160 std::initializer_list<int> bitrates_to_probe,
161 int min_bitrate_to_probe_further_bps) {
162 bool first_cluster = true;
163 for (int bitrate : bitrates_to_probe) {
sergeyu07c147d2016-11-03 11:59:50 -0700164 int max_probe_bitrate_bps =
165 max_bitrate_bps_ > 0 ? max_bitrate_bps_ : kDefaultMaxProbingBitrateBps;
166 bitrate = std::min(bitrate, max_probe_bitrate_bps);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700167 if (first_cluster) {
168 pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster + 1);
169 first_cluster = false;
170 } else {
171 pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster);
172 }
173 }
174 min_bitrate_to_probe_further_bps_ = min_bitrate_to_probe_further_bps;
175 time_last_probing_initiated_ms_ = clock_->TimeInMilliseconds();
176 if (min_bitrate_to_probe_further_bps == kExponentialProbingDisabled)
177 state_ = State::kProbingComplete;
178 else
179 state_ = State::kWaitingForProbingResult;
180}
181
182} // namespace webrtc