Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 12 | #include "webrtc/modules/congestion_controller/probe_controller.h" |
| 13 | #include "webrtc/modules/pacing/mock/mock_paced_sender.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 14 | #include "webrtc/rtc_base/logging.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/clock.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 16 | #include "webrtc/test/gmock.h" |
| 17 | #include "webrtc/test/gtest.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 18 | |
| 19 | using testing::_; |
| 20 | using testing::AtLeast; |
| 21 | using testing::NiceMock; |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 22 | using testing::Return; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | constexpr int kMinBitrateBps = 100; |
| 30 | constexpr int kStartBitrateBps = 300; |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 31 | constexpr int kMaxBitrateBps = 10000; |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 32 | |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 33 | constexpr int kExponentialProbingTimeoutMs = 5000; |
| 34 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 35 | constexpr int kAlrProbeInterval = 5000; |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame^] | 36 | constexpr int kAlrEndedTimeoutMs = 3000; |
| 37 | constexpr int kBitrateDropTimeoutMs = 5000; |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 38 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 39 | } // namespace |
| 40 | |
| 41 | class ProbeControllerTest : public ::testing::Test { |
| 42 | protected: |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 43 | ProbeControllerTest() : clock_(100000000L) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 44 | probe_controller_.reset(new ProbeController(&pacer_, &clock_)); |
| 45 | } |
| 46 | ~ProbeControllerTest() override {} |
| 47 | |
| 48 | SimulatedClock clock_; |
| 49 | NiceMock<MockPacedSender> pacer_; |
| 50 | std::unique_ptr<ProbeController> probe_controller_; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(ProbeControllerTest, InitiatesProbingAtStart) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 54 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 55 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 56 | kMaxBitrateBps); |
| 57 | } |
| 58 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 59 | TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) { |
| 60 | probe_controller_->OnNetworkStateChanged(kNetworkDown); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 61 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 62 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 63 | kMaxBitrateBps); |
| 64 | |
| 65 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 66 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 67 | probe_controller_->OnNetworkStateChanged(kNetworkUp); |
| 68 | } |
| 69 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 70 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 71 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 72 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 73 | kMaxBitrateBps); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 74 | // Long enough to time out exponential probing. |
| 75 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 76 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 77 | probe_controller_->Process(); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 78 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 79 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 80 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 81 | kMaxBitrateBps + 100); |
| 82 | } |
| 83 | |
philipel | da5e9d0 | 2017-01-17 02:08:28 -0800 | [diff] [blame] | 84 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) { |
| 85 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
| 86 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 87 | kMaxBitrateBps); |
| 88 | // Long enough to time out exponential probing. |
| 89 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
| 90 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
| 91 | probe_controller_->Process(); |
| 92 | |
| 93 | probe_controller_->SetEstimatedBitrate(kMaxBitrateBps); |
| 94 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100)); |
| 95 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 96 | kMaxBitrateBps + 100); |
| 97 | } |
| 98 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 99 | TEST_F(ProbeControllerTest, TestExponentialProbing) { |
| 100 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 101 | kMaxBitrateBps); |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 102 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 103 | // Repeated probe should only be sent when estimated bitrate climbs above |
| 104 | // 0.7 * 6 * kStartBitrateBps = 1260. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 105 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 106 | probe_controller_->SetEstimatedBitrate(1000); |
| 107 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 108 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 109 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 110 | probe_controller_->SetEstimatedBitrate(1800); |
| 111 | } |
| 112 | |
| 113 | TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) { |
| 114 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 115 | kMaxBitrateBps); |
| 116 | |
| 117 | // Advance far enough to cause a time out in waiting for probing result. |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 118 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 119 | probe_controller_->Process(); |
| 120 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 121 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 122 | probe_controller_->SetEstimatedBitrate(1800); |
| 123 | } |
| 124 | |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame^] | 125 | TEST_F(ProbeControllerTest, RequestProbeInAlr) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 126 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 127 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 128 | kMaxBitrateBps); |
| 129 | probe_controller_->SetEstimatedBitrate(500); |
| 130 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame^] | 131 | EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 132 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 133 | .WillRepeatedly( |
| 134 | Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds()))); |
| 135 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 136 | probe_controller_->Process(); |
terelius | 3376c84 | 2017-07-31 04:23:25 -0700 | [diff] [blame^] | 137 | probe_controller_->SetEstimatedBitrate(250); |
| 138 | probe_controller_->RequestProbe(); |
| 139 | } |
| 140 | |
| 141 | TEST_F(ProbeControllerTest, RequestProbeWhenAlrEndedRecently) { |
| 142 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
| 143 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 144 | kMaxBitrateBps); |
| 145 | probe_controller_->SetEstimatedBitrate(500); |
| 146 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 147 | EXPECT_CALL(pacer_, CreateProbeCluster(0.85 * 500)).Times(1); |
| 148 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 149 | .WillRepeatedly(Return(rtc::Optional<int64_t>())); |
| 150 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
| 151 | probe_controller_->Process(); |
| 152 | probe_controller_->SetEstimatedBitrate(250); |
| 153 | probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds()); |
| 154 | clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs - 1); |
| 155 | probe_controller_->RequestProbe(); |
| 156 | } |
| 157 | |
| 158 | TEST_F(ProbeControllerTest, RequestProbeWhenAlrNotEndedRecently) { |
| 159 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
| 160 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 161 | kMaxBitrateBps); |
| 162 | probe_controller_->SetEstimatedBitrate(500); |
| 163 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 164 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
| 165 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 166 | .WillRepeatedly(Return(rtc::Optional<int64_t>())); |
| 167 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
| 168 | probe_controller_->Process(); |
| 169 | probe_controller_->SetEstimatedBitrate(250); |
| 170 | probe_controller_->SetAlrEndedTimeMs(clock_.TimeInMilliseconds()); |
| 171 | clock_.AdvanceTimeMilliseconds(kAlrEndedTimeoutMs + 1); |
| 172 | probe_controller_->RequestProbe(); |
| 173 | } |
| 174 | |
| 175 | TEST_F(ProbeControllerTest, RequestProbeWhenBweDropNotRecent) { |
| 176 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
| 177 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 178 | kMaxBitrateBps); |
| 179 | probe_controller_->SetEstimatedBitrate(500); |
| 180 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 181 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
| 182 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 183 | .WillRepeatedly( |
| 184 | Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds()))); |
| 185 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
| 186 | probe_controller_->Process(); |
| 187 | probe_controller_->SetEstimatedBitrate(250); |
| 188 | clock_.AdvanceTimeMilliseconds(kBitrateDropTimeoutMs + 1); |
| 189 | probe_controller_->RequestProbe(); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST_F(ProbeControllerTest, PeriodicProbing) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 193 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 194 | probe_controller_->EnablePeriodicAlrProbing(true); |
| 195 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 196 | kMaxBitrateBps); |
| 197 | probe_controller_->SetEstimatedBitrate(500); |
| 198 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 199 | |
| 200 | int64_t start_time = clock_.TimeInMilliseconds(); |
| 201 | |
| 202 | // Expect the controller to send a new probe after 5s has passed. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 203 | EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 204 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 205 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 206 | clock_.AdvanceTimeMilliseconds(5000); |
| 207 | probe_controller_->Process(); |
| 208 | probe_controller_->SetEstimatedBitrate(500); |
| 209 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 210 | |
| 211 | // The following probe should be sent at 10s into ALR. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 212 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 213 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 214 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 215 | clock_.AdvanceTimeMilliseconds(4000); |
| 216 | probe_controller_->Process(); |
| 217 | probe_controller_->SetEstimatedBitrate(500); |
| 218 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 219 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 220 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 221 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 222 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 223 | clock_.AdvanceTimeMilliseconds(1000); |
| 224 | probe_controller_->Process(); |
| 225 | probe_controller_->SetEstimatedBitrate(500); |
| 226 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 227 | } |
| 228 | |
philipel | d1d247f | 2017-05-04 08:35:52 -0700 | [diff] [blame] | 229 | TEST_F(ProbeControllerTest, PeriodicProbingAfterReset) { |
| 230 | testing::StrictMock<MockPacedSender> local_pacer; |
| 231 | probe_controller_.reset(new ProbeController(&local_pacer, &clock_)); |
| 232 | int64_t alr_start_time = clock_.TimeInMilliseconds(); |
| 233 | EXPECT_CALL(local_pacer, GetApplicationLimitedRegionStartTime()) |
| 234 | .WillRepeatedly( |
| 235 | Return(rtc::Optional<int64_t>(alr_start_time))); |
| 236 | |
| 237 | EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2); |
| 238 | probe_controller_->EnablePeriodicAlrProbing(true); |
| 239 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 240 | kMaxBitrateBps); |
| 241 | probe_controller_->Reset(); |
| 242 | |
| 243 | clock_.AdvanceTimeMilliseconds(10000); |
| 244 | probe_controller_->Process(); |
| 245 | |
| 246 | EXPECT_CALL(local_pacer, CreateProbeCluster(_)).Times(2); |
| 247 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 248 | kMaxBitrateBps); |
| 249 | |
| 250 | // Make sure we use |kStartBitrateBps| as the estimated bitrate |
| 251 | // until SetEstimatedBitrate is called with an updated estimate. |
| 252 | clock_.AdvanceTimeMilliseconds(10000); |
| 253 | EXPECT_CALL(local_pacer, CreateProbeCluster(kStartBitrateBps*2)); |
| 254 | probe_controller_->Process(); |
| 255 | } |
| 256 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 257 | TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) { |
| 258 | const int64_t kMbpsMultiplier = 1000000; |
| 259 | probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier, |
| 260 | 100 * kMbpsMultiplier); |
| 261 | |
| 262 | // Verify that probe bitrate is capped at the specified max bitrate |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 263 | EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier)); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 264 | probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier); |
| 265 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 266 | |
| 267 | // Verify that repeated probes aren't sent. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 268 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 269 | probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier); |
| 270 | } |
| 271 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 272 | } // namespace test |
| 273 | } // namespace webrtc |