perkj | ec81bcd | 2016-05-11 06:01:13 -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 | |
terelius | 2d81eb3 | 2016-10-25 07:04:37 -0700 | [diff] [blame] | 11 | #include "webrtc/logging/rtc_event_log/mock/mock_rtc_event_log.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 12 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 13 | #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
| 14 | #include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 15 | #include "webrtc/modules/pacing/mock/mock_paced_sender.h" |
michaelt | f082c2a | 2016-11-07 04:17:14 -0800 | [diff] [blame] | 16 | #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_observer.h" |
| 18 | #include "webrtc/system_wrappers/include/clock.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 19 | #include "webrtc/test/gmock.h" |
| 20 | #include "webrtc/test/gtest.h" |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 21 | |
| 22 | using testing::_; |
asapersson | 14f1250 | 2016-09-07 23:14:50 -0700 | [diff] [blame] | 23 | using testing::AtLeast; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 24 | using testing::NiceMock; |
| 25 | using testing::Return; |
| 26 | using testing::SaveArg; |
| 27 | using testing::StrictMock; |
| 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
| 32 | class CongestionControllerTest : public ::testing::Test { |
| 33 | protected: |
| 34 | CongestionControllerTest() : clock_(123456) {} |
| 35 | ~CongestionControllerTest() override {} |
| 36 | |
| 37 | void SetUp() override { |
| 38 | pacer_ = new NiceMock<MockPacedSender>(); |
| 39 | std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership. |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 40 | controller_.reset(new CongestionController( |
| 41 | &clock_, &observer_, &remote_bitrate_observer_, &event_log_, |
nisse | 0245da0 | 2016-11-30 03:35:20 -0800 | [diff] [blame] | 42 | &packet_router_, std::move(pacer))); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 43 | bandwidth_observer_.reset( |
| 44 | controller_->GetBitrateController()->CreateRtcpBandwidthObserver()); |
| 45 | |
| 46 | // Set the initial bitrate estimate and expect the |observer| and |pacer_| |
| 47 | // to be updated. |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 48 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 49 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps)); |
| 50 | controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps); |
| 51 | } |
| 52 | |
| 53 | SimulatedClock clock_; |
| 54 | StrictMock<MockCongestionObserver> observer_; |
| 55 | NiceMock<MockPacedSender>* pacer_; |
| 56 | NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_; |
Ivo Creusen | fa1d568 | 2016-07-06 09:12:06 +0200 | [diff] [blame] | 57 | NiceMock<MockRtcEventLog> event_log_; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 58 | std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; |
nisse | 0245da0 | 2016-11-30 03:35:20 -0800 | [diff] [blame] | 59 | PacketRouter packet_router_; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 60 | std::unique_ptr<CongestionController> controller_; |
| 61 | const uint32_t kInitialBitrateBps = 60000; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(CongestionControllerTest, OnNetworkChanged) { |
| 65 | // Test no change. |
| 66 | clock_.AdvanceTimeMilliseconds(25); |
| 67 | controller_->Process(); |
| 68 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 69 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 70 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
| 71 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 72 | clock_.AdvanceTimeMilliseconds(25); |
| 73 | controller_->Process(); |
| 74 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 75 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 76 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps)); |
| 77 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps); |
| 78 | clock_.AdvanceTimeMilliseconds(25); |
| 79 | controller_->Process(); |
| 80 | } |
| 81 | |
| 82 | TEST_F(CongestionControllerTest, OnSendQueueFull) { |
| 83 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 84 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1)); |
| 85 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 86 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 87 | controller_->Process(); |
| 88 | |
| 89 | // Let the pacer not be full next time the controller checks. |
| 90 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 91 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1)); |
| 92 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 93 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 94 | controller_->Process(); |
| 95 | } |
| 96 | |
| 97 | TEST_F(CongestionControllerTest, OnSendQueueFullAndEstimateChange) { |
| 98 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 99 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1)); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 100 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 101 | controller_->Process(); |
| 102 | |
| 103 | // Receive new estimate but let the queue still be full. |
| 104 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 105 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 106 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1)); |
| 107 | // The send pacer should get the new estimate though. |
| 108 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
| 109 | clock_.AdvanceTimeMilliseconds(25); |
| 110 | controller_->Process(); |
| 111 | |
| 112 | // Let the pacer not be full next time the controller checks. |
| 113 | // |OnNetworkChanged| should be called with the new estimate. |
| 114 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 115 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1)); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 116 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 117 | clock_.AdvanceTimeMilliseconds(25); |
| 118 | controller_->Process(); |
| 119 | } |
| 120 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 121 | TEST_F(CongestionControllerTest, SignalNetworkState) { |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 122 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _)); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 123 | controller_->SignalNetworkState(kNetworkDown); |
| 124 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 125 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 126 | controller_->SignalNetworkState(kNetworkUp); |
| 127 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 128 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _)); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 129 | controller_->SignalNetworkState(kNetworkDown); |
| 130 | } |
| 131 | |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 132 | TEST_F(CongestionControllerTest, ResetBweAndBitrates) { |
| 133 | int new_bitrate = 200000; |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 134 | EXPECT_CALL(observer_, OnNetworkChanged(new_bitrate, _, _, _)); |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 135 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(new_bitrate)); |
| 136 | controller_->ResetBweAndBitrates(new_bitrate, -1, -1); |
| 137 | |
| 138 | // If the bitrate is reset to -1, the new starting bitrate will be |
michaelt | f082c2a | 2016-11-07 04:17:14 -0800 | [diff] [blame] | 139 | // the minimum default bitrate kMinBitrateBps. |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 140 | EXPECT_CALL( |
| 141 | observer_, |
| 142 | OnNetworkChanged(congestion_controller::GetMinBitrateBps(), _, _, _)); |
michaelt | f082c2a | 2016-11-07 04:17:14 -0800 | [diff] [blame] | 143 | EXPECT_CALL(*pacer_, |
| 144 | SetEstimatedBitrate(congestion_controller::GetMinBitrateBps())); |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 145 | controller_->ResetBweAndBitrates(-1, -1, -1); |
| 146 | } |
| 147 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 148 | TEST_F(CongestionControllerTest, |
| 149 | SignalNetworkStateAndQueueIsFullAndEstimateChange) { |
| 150 | // Send queue is full |
| 151 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 152 | .WillRepeatedly(Return(PacedSender::kMaxQueueLengthMs + 1)); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 153 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _, _)); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 154 | controller_->Process(); |
| 155 | |
| 156 | // Queue is full and network is down. Expect no bitrate change. |
| 157 | controller_->SignalNetworkState(kNetworkDown); |
| 158 | controller_->Process(); |
| 159 | |
| 160 | // Queue is full but network is up. Expect no bitrate change. |
| 161 | controller_->SignalNetworkState(kNetworkUp); |
| 162 | controller_->Process(); |
| 163 | |
| 164 | // Receive new estimate but let the queue still be full. |
| 165 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
| 166 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 167 | clock_.AdvanceTimeMilliseconds(25); |
| 168 | controller_->Process(); |
| 169 | |
| 170 | // Let the pacer not be full next time the controller checks. |
| 171 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 172 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1)); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 173 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 174 | controller_->Process(); |
| 175 | } |
| 176 | |
asapersson | 14f1250 | 2016-09-07 23:14:50 -0700 | [diff] [blame] | 177 | TEST_F(CongestionControllerTest, GetPacerQueuingDelayMs) { |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 178 | EXPECT_CALL(observer_, OnNetworkChanged(_, _, _, _)).Times(AtLeast(1)); |
asapersson | 14f1250 | 2016-09-07 23:14:50 -0700 | [diff] [blame] | 179 | |
| 180 | const int64_t kQueueTimeMs = 123; |
| 181 | EXPECT_CALL(*pacer_, QueueInMs()).WillRepeatedly(Return(kQueueTimeMs)); |
| 182 | EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs()); |
| 183 | |
| 184 | // Expect zero pacer delay when network is down. |
| 185 | controller_->SignalNetworkState(kNetworkDown); |
| 186 | EXPECT_EQ(0, controller_->GetPacerQueuingDelayMs()); |
| 187 | |
| 188 | // Network is up, pacer delay should be reported. |
| 189 | controller_->SignalNetworkState(kNetworkUp); |
| 190 | EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs()); |
| 191 | } |
| 192 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 193 | TEST_F(CongestionControllerTest, GetProbingInterval) { |
| 194 | clock_.AdvanceTimeMilliseconds(25); |
| 195 | controller_->Process(); |
| 196 | |
| 197 | EXPECT_CALL(observer_, OnNetworkChanged(_, _, _, testing::Ne(0))); |
| 198 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(_)); |
| 199 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 200 | clock_.AdvanceTimeMilliseconds(25); |
| 201 | controller_->Process(); |
| 202 | } |
| 203 | |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 204 | } // namespace test |
| 205 | } // namespace webrtc |