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. |
| 40 | std::unique_ptr<PacketRouter> packet_router(new PacketRouter()); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 41 | controller_.reset(new CongestionController( |
| 42 | &clock_, &observer_, &remote_bitrate_observer_, &event_log_, |
| 43 | std::move(packet_router), std::move(pacer))); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 44 | bandwidth_observer_.reset( |
| 45 | controller_->GetBitrateController()->CreateRtcpBandwidthObserver()); |
| 46 | |
| 47 | // Set the initial bitrate estimate and expect the |observer| and |pacer_| |
| 48 | // to be updated. |
| 49 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _)); |
| 50 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps)); |
| 51 | controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps); |
| 52 | } |
| 53 | |
| 54 | SimulatedClock clock_; |
| 55 | StrictMock<MockCongestionObserver> observer_; |
| 56 | NiceMock<MockPacedSender>* pacer_; |
| 57 | NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_; |
Ivo Creusen | fa1d568 | 2016-07-06 09:12:06 +0200 | [diff] [blame] | 58 | NiceMock<MockRtcEventLog> event_log_; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 59 | std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; |
| 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 | |
| 69 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _)); |
| 70 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
| 71 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 72 | clock_.AdvanceTimeMilliseconds(25); |
| 73 | controller_->Process(); |
| 74 | |
| 75 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _)); |
| 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 | |
| 86 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _)); |
| 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 | |
| 93 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _)); |
| 94 | controller_->Process(); |
| 95 | } |
| 96 | |
| 97 | TEST_F(CongestionControllerTest, OnSendQueueFullAndEstimateChange) { |
| 98 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 99 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs + 1)); |
| 100 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _)); |
| 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)); |
| 116 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _)); |
| 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) { |
| 122 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _)); |
| 123 | controller_->SignalNetworkState(kNetworkDown); |
| 124 | |
| 125 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _)); |
| 126 | controller_->SignalNetworkState(kNetworkUp); |
| 127 | |
| 128 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _)); |
| 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; |
| 134 | EXPECT_CALL(observer_, OnNetworkChanged(new_bitrate, _, _)); |
| 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. |
| 140 | EXPECT_CALL(observer_, OnNetworkChanged( |
| 141 | congestion_controller::GetMinBitrateBps(), _, _)); |
| 142 | EXPECT_CALL(*pacer_, |
| 143 | SetEstimatedBitrate(congestion_controller::GetMinBitrateBps())); |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 144 | controller_->ResetBweAndBitrates(-1, -1, -1); |
| 145 | } |
| 146 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 147 | TEST_F(CongestionControllerTest, |
| 148 | SignalNetworkStateAndQueueIsFullAndEstimateChange) { |
| 149 | // Send queue is full |
| 150 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 151 | .WillRepeatedly(Return(PacedSender::kMaxQueueLengthMs + 1)); |
| 152 | EXPECT_CALL(observer_, OnNetworkChanged(0, _, _)); |
| 153 | controller_->Process(); |
| 154 | |
| 155 | // Queue is full and network is down. Expect no bitrate change. |
| 156 | controller_->SignalNetworkState(kNetworkDown); |
| 157 | controller_->Process(); |
| 158 | |
| 159 | // Queue is full but network is up. Expect no bitrate change. |
| 160 | controller_->SignalNetworkState(kNetworkUp); |
| 161 | controller_->Process(); |
| 162 | |
| 163 | // Receive new estimate but let the queue still be full. |
| 164 | EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
| 165 | bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
| 166 | clock_.AdvanceTimeMilliseconds(25); |
| 167 | controller_->Process(); |
| 168 | |
| 169 | // Let the pacer not be full next time the controller checks. |
| 170 | EXPECT_CALL(*pacer_, ExpectedQueueTimeMs()) |
| 171 | .WillOnce(Return(PacedSender::kMaxQueueLengthMs - 1)); |
| 172 | EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _)); |
| 173 | controller_->Process(); |
| 174 | } |
| 175 | |
asapersson | 14f1250 | 2016-09-07 23:14:50 -0700 | [diff] [blame] | 176 | TEST_F(CongestionControllerTest, GetPacerQueuingDelayMs) { |
| 177 | EXPECT_CALL(observer_, OnNetworkChanged(_, _, _)).Times(AtLeast(1)); |
| 178 | |
| 179 | const int64_t kQueueTimeMs = 123; |
| 180 | EXPECT_CALL(*pacer_, QueueInMs()).WillRepeatedly(Return(kQueueTimeMs)); |
| 181 | EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs()); |
| 182 | |
| 183 | // Expect zero pacer delay when network is down. |
| 184 | controller_->SignalNetworkState(kNetworkDown); |
| 185 | EXPECT_EQ(0, controller_->GetPacerQueuingDelayMs()); |
| 186 | |
| 187 | // Network is up, pacer delay should be reported. |
| 188 | controller_->SignalNetworkState(kNetworkUp); |
| 189 | EXPECT_EQ(kQueueTimeMs, controller_->GetPacerQueuingDelayMs()); |
| 190 | } |
| 191 | |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 192 | } // namespace test |
| 193 | } // namespace webrtc |