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/base/logging.h" |
| 13 | #include "webrtc/modules/congestion_controller/probe_controller.h" |
| 14 | #include "webrtc/modules/pacing/mock/mock_paced_sender.h" |
| 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; |
| 36 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | class ProbeControllerTest : public ::testing::Test { |
| 40 | protected: |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 41 | ProbeControllerTest() : clock_(100000000L) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 42 | probe_controller_.reset(new ProbeController(&pacer_, &clock_)); |
| 43 | } |
| 44 | ~ProbeControllerTest() override {} |
| 45 | |
| 46 | SimulatedClock clock_; |
| 47 | NiceMock<MockPacedSender> pacer_; |
| 48 | std::unique_ptr<ProbeController> probe_controller_; |
| 49 | }; |
| 50 | |
| 51 | TEST_F(ProbeControllerTest, InitiatesProbingAtStart) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 52 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 53 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 54 | kMaxBitrateBps); |
| 55 | } |
| 56 | |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 57 | TEST_F(ProbeControllerTest, ProbeOnlyWhenNetworkIsUp) { |
| 58 | probe_controller_->OnNetworkStateChanged(kNetworkDown); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 59 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 60 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 61 | kMaxBitrateBps); |
| 62 | |
| 63 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 64 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 65 | probe_controller_->OnNetworkStateChanged(kNetworkUp); |
| 66 | } |
| 67 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 68 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncrease) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 69 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 70 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 71 | kMaxBitrateBps); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 72 | // Long enough to time out exponential probing. |
| 73 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 74 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 75 | probe_controller_->Process(); |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 76 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 77 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 78 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 79 | kMaxBitrateBps + 100); |
| 80 | } |
| 81 | |
philipel | da5e9d0 | 2017-01-17 02:08:28 -0800 | [diff] [blame^] | 82 | TEST_F(ProbeControllerTest, InitiatesProbingOnMaxBitrateIncreaseAtMaxBitrate) { |
| 83 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(AtLeast(2)); |
| 84 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 85 | kMaxBitrateBps); |
| 86 | // Long enough to time out exponential probing. |
| 87 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
| 88 | probe_controller_->SetEstimatedBitrate(kStartBitrateBps); |
| 89 | probe_controller_->Process(); |
| 90 | |
| 91 | probe_controller_->SetEstimatedBitrate(kMaxBitrateBps); |
| 92 | EXPECT_CALL(pacer_, CreateProbeCluster(kMaxBitrateBps + 100)); |
| 93 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 94 | kMaxBitrateBps + 100); |
| 95 | } |
| 96 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 97 | TEST_F(ProbeControllerTest, TestExponentialProbing) { |
| 98 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 99 | kMaxBitrateBps); |
sergeyu | 07c147d | 2016-11-03 11:59:50 -0700 | [diff] [blame] | 100 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 101 | // Repeated probe should only be sent when estimated bitrate climbs above |
| 102 | // 0.7 * 6 * kStartBitrateBps = 1260. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 103 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 104 | probe_controller_->SetEstimatedBitrate(1000); |
| 105 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 106 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 107 | EXPECT_CALL(pacer_, CreateProbeCluster(2 * 1800)); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 108 | probe_controller_->SetEstimatedBitrate(1800); |
| 109 | } |
| 110 | |
| 111 | TEST_F(ProbeControllerTest, TestExponentialProbingTimeout) { |
| 112 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 113 | kMaxBitrateBps); |
| 114 | |
| 115 | // 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] | 116 | clock_.AdvanceTimeMilliseconds(kExponentialProbingTimeoutMs); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 117 | probe_controller_->Process(); |
| 118 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 119 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 120 | probe_controller_->SetEstimatedBitrate(1800); |
| 121 | } |
| 122 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 123 | TEST_F(ProbeControllerTest, ProbeAfterEstimateDropInAlr) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 124 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 125 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 126 | kMaxBitrateBps); |
| 127 | probe_controller_->SetEstimatedBitrate(500); |
| 128 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 129 | |
| 130 | // When bandwidth estimate drops the controller should send a probe at the |
| 131 | // previous bitrate. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 132 | EXPECT_CALL(pacer_, CreateProbeCluster(500)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 133 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 134 | .WillRepeatedly( |
| 135 | Return(rtc::Optional<int64_t>(clock_.TimeInMilliseconds()))); |
| 136 | clock_.AdvanceTimeMilliseconds(kAlrProbeInterval + 1); |
sergeyu | 9cef11b | 2016-12-02 11:03:01 -0800 | [diff] [blame] | 137 | probe_controller_->Process(); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 138 | probe_controller_->SetEstimatedBitrate(50); |
| 139 | } |
| 140 | |
| 141 | TEST_F(ProbeControllerTest, PeriodicProbing) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 142 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(2); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 143 | probe_controller_->EnablePeriodicAlrProbing(true); |
| 144 | probe_controller_->SetBitrates(kMinBitrateBps, kStartBitrateBps, |
| 145 | kMaxBitrateBps); |
| 146 | probe_controller_->SetEstimatedBitrate(500); |
| 147 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 148 | |
| 149 | int64_t start_time = clock_.TimeInMilliseconds(); |
| 150 | |
| 151 | // Expect the controller to send a new probe after 5s has passed. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 152 | EXPECT_CALL(pacer_, CreateProbeCluster(1000)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 153 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 154 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 155 | clock_.AdvanceTimeMilliseconds(5000); |
| 156 | probe_controller_->Process(); |
| 157 | probe_controller_->SetEstimatedBitrate(500); |
| 158 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 159 | |
| 160 | // The following probe should be sent at 10s into ALR. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 161 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 162 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 163 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 164 | clock_.AdvanceTimeMilliseconds(4000); |
| 165 | probe_controller_->Process(); |
| 166 | probe_controller_->SetEstimatedBitrate(500); |
| 167 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 168 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 169 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(1); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 170 | EXPECT_CALL(pacer_, GetApplicationLimitedRegionStartTime()) |
| 171 | .WillRepeatedly(Return(rtc::Optional<int64_t>(start_time))); |
| 172 | clock_.AdvanceTimeMilliseconds(1000); |
| 173 | probe_controller_->Process(); |
| 174 | probe_controller_->SetEstimatedBitrate(500); |
| 175 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 176 | } |
| 177 | |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 178 | TEST_F(ProbeControllerTest, TestExponentialProbingOverflow) { |
| 179 | const int64_t kMbpsMultiplier = 1000000; |
| 180 | probe_controller_->SetBitrates(kMinBitrateBps, 10 * kMbpsMultiplier, |
| 181 | 100 * kMbpsMultiplier); |
| 182 | |
| 183 | // Verify that probe bitrate is capped at the specified max bitrate |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 184 | EXPECT_CALL(pacer_, CreateProbeCluster(100 * kMbpsMultiplier)); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 185 | probe_controller_->SetEstimatedBitrate(60 * kMbpsMultiplier); |
| 186 | testing::Mock::VerifyAndClearExpectations(&pacer_); |
| 187 | |
| 188 | // Verify that repeated probes aren't sent. |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 189 | EXPECT_CALL(pacer_, CreateProbeCluster(_)).Times(0); |
sergeyu | 5bc3945 | 2016-12-15 10:42:09 -0800 | [diff] [blame] | 190 | probe_controller_->SetEstimatedBitrate(100 * kMbpsMultiplier); |
| 191 | } |
| 192 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 193 | } // namespace test |
| 194 | } // namespace webrtc |