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