blob: 3477c87a26f2f29313f2f49b2673002b2d13182a [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#include <memory>
11
Danil Chapovalovbda50682018-02-14 09:08:28 +000012#include "modules/congestion_controller/probe_controller.h"
Sebastian Janssonea86bb72018-02-14 16:53:38 +000013#include "modules/pacing/mock/mock_paced_sender.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/logging.h"
15#include "system_wrappers/include/clock.h"
16#include "test/gmock.h"
17#include "test/gtest.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070018
19using testing::_;
20using testing::AtLeast;
21using testing::NiceMock;
sergeyu80ed35e2016-11-28 13:11:13 -080022using testing::Return;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070023
24namespace webrtc {
25namespace test {
26
27namespace {
28
29constexpr int kMinBitrateBps = 100;
30constexpr int kStartBitrateBps = 300;
sergeyu07c147d2016-11-03 11:59:50 -070031constexpr int kMaxBitrateBps = 10000;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070032
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070033constexpr int kExponentialProbingTimeoutMs = 5000;
34
sergeyu80ed35e2016-11-28 13:11:13 -080035constexpr int kAlrProbeInterval = 5000;
terelius3376c842017-07-31 04:23:25 -070036constexpr int kAlrEndedTimeoutMs = 3000;
37constexpr int kBitrateDropTimeoutMs = 5000;
sergeyu80ed35e2016-11-28 13:11:13 -080038
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070039} // namespace
40
41class ProbeControllerTest : public ::testing::Test {
42 protected:
sergeyu80ed35e2016-11-28 13:11:13 -080043 ProbeControllerTest() : clock_(100000000L) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000044 probe_controller_.reset(new ProbeController(&pacer_, &clock_));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070045 }
46 ~ProbeControllerTest() override {}
47
48 SimulatedClock clock_;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000049 NiceMock<MockPacedSender> pacer_;
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070050 std::unique_ptr<ProbeController> probe_controller_;
51};
52
53TEST_F(ProbeControllerTest, InitiatesProbingAtStart) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000054 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070055 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000056 kMaxBitrateBps);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070057}
58
Sergey Ulanove2b15012016-11-22 16:08:30 -080059TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000060 probe_controller_->OnNetworkStateChanged(kNetworkDown);
61 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
Sergey Ulanove2b15012016-11-22 16:08:30 -080062 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000063 kMaxBitrateBps);
Sergey Ulanove2b15012016-11-22 16:08:30 -080064
Sebastian Janssonea86bb72018-02-14 16:53:38 +000065 testing::Mock::VerifyAndClearExpectations(&pacer_);
66 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
67 probe_controller_->OnNetworkStateChanged(kNetworkUp);
Sergey Ulanove2b15012016-11-22 16:08:30 -080068}
69
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070070TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000071 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070072 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000073 kMaxBitrateBps);
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070074 // Long enough to time out exponential probing.
75 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Sebastian Janssonea86bb72018-02-14 16:53:38 +000076 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
77 probe_controller_->Process();
Irfan Sheriff9b7b7532016-09-16 11:30:44 -070078
Sebastian Janssonea86bb72018-02-14 16:53:38 +000079 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070080 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000081 kMaxBitrateBps + 100);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070082}
83
philipelda5e9d02017-01-17 02:08:28 -080084TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000085 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2));
philipelda5e9d02017-01-17 02:08:28 -080086 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000087 kMaxBitrateBps);
philipelda5e9d02017-01-17 02:08:28 -080088 // Long enough to time out exponential probing.
89 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Sebastian Janssonea86bb72018-02-14 16:53:38 +000090 probe_controller_->SetEstimatedBitrate(kStartBitrateBps);
91 probe_controller_->Process();
philipelda5e9d02017-01-17 02:08:28 -080092
Sebastian Janssonea86bb72018-02-14 16:53:38 +000093 probe_controller_->SetEstimatedBitrate(kMaxBitrateBps);
94 EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100));
philipelda5e9d02017-01-17 02:08:28 -080095 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +000096 kMaxBitrateBps + 100);
philipelda5e9d02017-01-17 02:08:28 -080097}
98
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070099TEST_F(ProbeControllerTest, TestExponentialProbing) {
100 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000101 kMaxBitrateBps);
sergeyu07c147d2016-11-03 11:59:50 -0700102
sergeyu5bc39452016-12-15 10:42:09 -0800103 // Repeated probe should only be sent when estimated bitrate climbs above
104 // 0.7 * 6 * kStartBitrateBps = 1260.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000105 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
106 probe_controller_->SetEstimatedBitrate(1000);
107 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu9cef11b2016-12-02 11:03:01 -0800108
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000109 EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800));
110 probe_controller_->SetEstimatedBitrate(1800);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700111}
112
113TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) {
114 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000115 kMaxBitrateBps);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700116
117 // Advance far enough to cause a time out in waiting for probing result.
Irfan Sheriff9b7b7532016-09-16 11:30:44 -0700118 clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000119 probe_controller_->Process();
sergeyu9cef11b2016-12-02 11:03:01 -0800120
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000121 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
122 probe_controller_->SetEstimatedBitrate(1800);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700123}
124
terelius3376c842017-07-31 04:23:25 -0700125TEST_F(ProbeControllerTest, RequestProbeInAlr) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000126 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800127 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000128 kMaxBitrateBps);
129 probe_controller_->SetEstimatedBitrate(500);
130 testing::Mock::VerifyAndClearExpectations(&pacer_);
131 EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1);
132 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
133 .WillRepeatedly(Return(clock_.TimeInMilliseconds()));
sergeyu80ed35e2016-11-28 13:11:13 -0800134 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000135 probe_controller_->Process();
136 probe_controller_->SetEstimatedBitrate(250);
137 probe_controller_->RequestProbe();
terelius3376c842017-07-31 04:23:25 -0700138}
139
140TEST_F(ProbeControllerTest, RequestProbeWhenAlrEndedRecently) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000141 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
terelius3376c842017-07-31 04:23:25 -0700142 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000143 kMaxBitrateBps);
144 probe_controller_->SetEstimatedBitrate(500);
145 testing::Mock::VerifyAndClearExpectations(&pacer_);
146 EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1);
147 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
148 .WillRepeatedly(Return(rtc::nullopt));
terelius3376c842017-07-31 04:23:25 -0700149 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000150 probe_controller_->Process();
151 probe_controller_->SetEstimatedBitrate(250);
terelius3376c842017-07-31 04:23:25 -0700152 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
153 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs - 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000154 probe_controller_->RequestProbe();
terelius3376c842017-07-31 04:23:25 -0700155}
156
157TEST_F(ProbeControllerTest, RequestProbeWhenAlrNotEndedRecently) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000158 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
terelius3376c842017-07-31 04:23:25 -0700159 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000160 kMaxBitrateBps);
161 probe_controller_->SetEstimatedBitrate(500);
162 testing::Mock::VerifyAndClearExpectations(&pacer_);
163 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
164 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
165 .WillRepeatedly(Return(rtc::nullopt));
terelius3376c842017-07-31 04:23:25 -0700166 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000167 probe_controller_->Process();
168 probe_controller_->SetEstimatedBitrate(250);
terelius3376c842017-07-31 04:23:25 -0700169 probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds());
170 clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000171 probe_controller_->RequestProbe();
terelius3376c842017-07-31 04:23:25 -0700172}
173
174TEST_F(ProbeControllerTest, RequestProbeWhenBweDropNotRecent) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000175 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
terelius3376c842017-07-31 04:23:25 -0700176 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000177 kMaxBitrateBps);
178 probe_controller_->SetEstimatedBitrate(500);
179 testing::Mock::VerifyAndClearExpectations(&pacer_);
180 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
181 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
182 .WillRepeatedly(Return(clock_.TimeInMilliseconds()));
terelius3376c842017-07-31 04:23:25 -0700183 clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000184 probe_controller_->Process();
185 probe_controller_->SetEstimatedBitrate(250);
terelius3376c842017-07-31 04:23:25 -0700186 clock_.AdvanceTimeMilliseconds(kBitrateDropTimeoutMs + 1);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000187 probe_controller_->RequestProbe();
sergeyu80ed35e2016-11-28 13:11:13 -0800188}
189
190TEST_F(ProbeControllerTest, PeriodicProbing) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000191 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2);
sergeyu80ed35e2016-11-28 13:11:13 -0800192 probe_controller_->EnablePeriodicAlrProbing(true);
193 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000194 kMaxBitrateBps);
195 probe_controller_->SetEstimatedBitrate(500);
196 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu80ed35e2016-11-28 13:11:13 -0800197
198 int64_t start_time = clock_.TimeInMilliseconds();
199
200 // Expect the controller to send a new probe after 5s has passed.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000201 EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1);
202 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
203 .WillRepeatedly(Return(start_time));
sergeyu80ed35e2016-11-28 13:11:13 -0800204 clock_.AdvanceTimeMilliseconds(5000);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000205 probe_controller_->Process();
206 probe_controller_->SetEstimatedBitrate(500);
207 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu80ed35e2016-11-28 13:11:13 -0800208
209 // The following probe should be sent at 10s into ALR.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000210 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
211 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
212 .WillRepeatedly(Return(start_time));
sergeyu80ed35e2016-11-28 13:11:13 -0800213 clock_.AdvanceTimeMilliseconds(4000);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000214 probe_controller_->Process();
215 probe_controller_->SetEstimatedBitrate(500);
216 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu80ed35e2016-11-28 13:11:13 -0800217
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000218 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1);
219 EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime())
220 .WillRepeatedly(Return(start_time));
sergeyu80ed35e2016-11-28 13:11:13 -0800221 clock_.AdvanceTimeMilliseconds(1000);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000222 probe_controller_->Process();
223 probe_controller_->SetEstimatedBitrate(500);
224 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu80ed35e2016-11-28 13:11:13 -0800225}
226
philipeld1d247f2017-05-04 08:35:52 -0700227TEST_F(ProbeControllerTest, PeriodicProbingAfterReset) {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000228 testing::StrictMock<MockPacedSender> local_pacer;
229 probe_controller_.reset(new ProbeController(&local_pacer, &clock_));
philipeld1d247f2017-05-04 08:35:52 -0700230 int64_t alr_start_time = clock_.TimeInMilliseconds();
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000231 EXPECT_CALL(local_pacer, GetApplicationLimitedRegionStartTime())
232 .WillRepeatedly(Return(alr_start_time));
philipeld1d247f2017-05-04 08:35:52 -0700233
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000234 EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2);
philipeld1d247f2017-05-04 08:35:52 -0700235 probe_controller_->EnablePeriodicAlrProbing(true);
236 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000237 kMaxBitrateBps);
238 probe_controller_->Reset();
philipeld1d247f2017-05-04 08:35:52 -0700239
240 clock_.AdvanceTimeMilliseconds(10000);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000241 probe_controller_->Process();
philipeld1d247f2017-05-04 08:35:52 -0700242
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000243 EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2);
philipeld1d247f2017-05-04 08:35:52 -0700244 probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000245 kMaxBitrateBps);
philipeld1d247f2017-05-04 08:35:52 -0700246
247 // Make sure we use |kStartBitrateBps| as the estimated bitrate
248 // until SetEstimatedBitrate is called with an updated estimate.
249 clock_.AdvanceTimeMilliseconds(10000);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000250 EXPECT_CALL(local_pacer, CreateProbeCluster(kStartBitrateBps*2));
251 probe_controller_->Process();
philipeld1d247f2017-05-04 08:35:52 -0700252}
253
sergeyu5bc39452016-12-15 10:42:09 -0800254TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) {
255 const int64_t kMbpsMultiplier = 1000000;
256 probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier,
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000257 100 * kMbpsMultiplier);
sergeyu5bc39452016-12-15 10:42:09 -0800258
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000259 // Verify that probe bitrate is capped at the specified max bitrate
260 EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier));
261 probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier);
262 testing::Mock::VerifyAndClearExpectations(&pacer_);
sergeyu5bc39452016-12-15 10:42:09 -0800263
264 // Verify that repeated probes aren't sent.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000265 EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0);
266 probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier);
sergeyu5bc39452016-12-15 10:42:09 -0800267}
268
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700269} // namespace test
270} // namespace webrtc