1 /* 2 * Copyright (c) 2012 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 <algorithm> 12 #include <vector> 13 14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "webrtc/call/bitrate_allocator.h" 16 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" 17 18 namespace webrtc { 19 20 class TestBitrateObserver : public BitrateObserver { 21 public: 22 TestBitrateObserver() 23 : last_bitrate_(0), last_fraction_loss_(0), last_rtt_(0) {} 24 25 virtual void OnNetworkChanged(uint32_t bitrate, 26 uint8_t fraction_loss, 27 int64_t rtt) { 28 last_bitrate_ = bitrate; 29 last_fraction_loss_ = fraction_loss; 30 last_rtt_ = rtt; 31 } 32 uint32_t last_bitrate_; 33 uint8_t last_fraction_loss_; 34 int64_t last_rtt_; 35 }; 36 37 class BitrateAllocatorTest : public ::testing::Test { 38 protected: 39 BitrateAllocatorTest() : allocator_(new BitrateAllocator()) { 40 allocator_->OnNetworkChanged(300000u, 0, 0); 41 } 42 ~BitrateAllocatorTest() {} 43 44 rtc::scoped_ptr<BitrateAllocator> allocator_; 45 }; 46 47 TEST_F(BitrateAllocatorTest, UpdatingBitrateObserver) { 48 TestBitrateObserver bitrate_observer; 49 int start_bitrate = 50 allocator_->AddBitrateObserver(&bitrate_observer, 100000, 1500000); 51 EXPECT_EQ(300000, start_bitrate); 52 allocator_->OnNetworkChanged(200000, 0, 0); 53 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_); 54 55 // TODO(pbos): Expect capping to 1.5M instead of 3M when not boosting the max 56 // bitrate for FEC/retransmissions (see todo in BitrateAllocator). 57 allocator_->OnNetworkChanged(4000000, 0, 0); 58 EXPECT_EQ(3000000u, bitrate_observer.last_bitrate_); 59 start_bitrate = 60 allocator_->AddBitrateObserver(&bitrate_observer, 100000, 4000000); 61 EXPECT_EQ(4000000, start_bitrate); 62 63 start_bitrate = 64 allocator_->AddBitrateObserver(&bitrate_observer, 100000, 1500000); 65 EXPECT_EQ(3000000, start_bitrate); 66 EXPECT_EQ(3000000u, bitrate_observer.last_bitrate_); 67 allocator_->OnNetworkChanged(1500000, 0, 0); 68 EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_); 69 } 70 71 TEST_F(BitrateAllocatorTest, TwoBitrateObserversOneRtcpObserver) { 72 TestBitrateObserver bitrate_observer_1; 73 TestBitrateObserver bitrate_observer_2; 74 int start_bitrate = 75 allocator_->AddBitrateObserver(&bitrate_observer_1, 100000, 300000); 76 EXPECT_EQ(300000, start_bitrate); 77 start_bitrate = 78 allocator_->AddBitrateObserver(&bitrate_observer_2, 200000, 300000); 79 EXPECT_EQ(200000, start_bitrate); 80 81 // Test too low start bitrate, hence lower than sum of min. Min bitrates will 82 // be allocated to all observers. 83 allocator_->OnNetworkChanged(200000, 0, 50); 84 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); 85 EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_); 86 EXPECT_EQ(50, bitrate_observer_1.last_rtt_); 87 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); 88 EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_); 89 EXPECT_EQ(50, bitrate_observer_2.last_rtt_); 90 91 // Test a bitrate which should be distributed equally. 92 allocator_->OnNetworkChanged(500000, 0, 50); 93 const uint32_t kBitrateToShare = 500000 - 200000 - 100000; 94 EXPECT_EQ(100000u + kBitrateToShare / 2, bitrate_observer_1.last_bitrate_); 95 EXPECT_EQ(200000u + kBitrateToShare / 2, bitrate_observer_2.last_bitrate_); 96 97 // Limited by 2x max bitrates since we leave room for FEC and retransmissions. 98 allocator_->OnNetworkChanged(1500000, 0, 50); 99 EXPECT_EQ(600000u, bitrate_observer_1.last_bitrate_); 100 EXPECT_EQ(600000u, bitrate_observer_2.last_bitrate_); 101 } 102 103 class BitrateAllocatorTestNoEnforceMin : public ::testing::Test { 104 protected: 105 BitrateAllocatorTestNoEnforceMin() : allocator_(new BitrateAllocator()) { 106 allocator_->EnforceMinBitrate(false); 107 allocator_->OnNetworkChanged(300000u, 0, 0); 108 } 109 ~BitrateAllocatorTestNoEnforceMin() {} 110 111 rtc::scoped_ptr<BitrateAllocator> allocator_; 112 }; 113 114 // The following three tests verify that the EnforceMinBitrate() method works 115 // as intended. 116 TEST_F(BitrateAllocatorTestNoEnforceMin, OneBitrateObserver) { 117 TestBitrateObserver bitrate_observer_1; 118 int start_bitrate = 119 allocator_->AddBitrateObserver(&bitrate_observer_1, 100000, 400000); 120 EXPECT_EQ(300000, start_bitrate); 121 122 // High REMB. 123 allocator_->OnNetworkChanged(150000, 0, 0); 124 EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_); 125 126 // Low REMB. 127 allocator_->OnNetworkChanged(10000, 0, 0); 128 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_); 129 130 allocator_->RemoveBitrateObserver(&bitrate_observer_1); 131 } 132 133 TEST_F(BitrateAllocatorTestNoEnforceMin, ThreeBitrateObservers) { 134 TestBitrateObserver bitrate_observer_1; 135 TestBitrateObserver bitrate_observer_2; 136 TestBitrateObserver bitrate_observer_3; 137 // Set up the observers with min bitrates at 100000, 200000, and 300000. 138 int start_bitrate = 139 allocator_->AddBitrateObserver(&bitrate_observer_1, 100000, 400000); 140 EXPECT_EQ(300000, start_bitrate); 141 142 start_bitrate = 143 allocator_->AddBitrateObserver(&bitrate_observer_2, 200000, 400000); 144 EXPECT_EQ(200000, start_bitrate); 145 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); 146 147 start_bitrate = 148 allocator_->AddBitrateObserver(&bitrate_observer_3, 300000, 400000); 149 EXPECT_EQ(0, start_bitrate); 150 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); 151 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); 152 153 // High REMB. Make sure the controllers get a fair share of the surplus 154 // (i.e., what is left after each controller gets its min rate). 155 allocator_->OnNetworkChanged(690000, 0, 0); 156 // Verify that each observer gets its min rate (sum of min rates is 600000), 157 // and that the remaining 90000 is divided equally among the three. 158 uint32_t bitrate_to_share = 690000u - 100000u - 200000u - 300000u; 159 EXPECT_EQ(100000u + bitrate_to_share / 3, bitrate_observer_1.last_bitrate_); 160 EXPECT_EQ(200000u + bitrate_to_share / 3, bitrate_observer_2.last_bitrate_); 161 EXPECT_EQ(300000u + bitrate_to_share / 3, bitrate_observer_3.last_bitrate_); 162 163 // High REMB, but below the sum of min bitrates. 164 allocator_->OnNetworkChanged(500000, 0, 0); 165 // Verify that the first and second observers get their min bitrates, and the 166 // third gets the remainder. 167 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); // Min bitrate. 168 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); // Min bitrate. 169 EXPECT_EQ(200000u, bitrate_observer_3.last_bitrate_); // Remainder. 170 171 // Low REMB. 172 allocator_->OnNetworkChanged(10000, 0, 0); 173 // Verify that the first observer gets all the rate, and the rest get zero. 174 EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_); 175 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_); 176 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_); 177 178 allocator_->RemoveBitrateObserver(&bitrate_observer_1); 179 allocator_->RemoveBitrateObserver(&bitrate_observer_2); 180 allocator_->RemoveBitrateObserver(&bitrate_observer_3); 181 } 182 183 TEST_F(BitrateAllocatorTest, ThreeBitrateObserversLowRembEnforceMin) { 184 TestBitrateObserver bitrate_observer_1; 185 TestBitrateObserver bitrate_observer_2; 186 TestBitrateObserver bitrate_observer_3; 187 int start_bitrate = 188 allocator_->AddBitrateObserver(&bitrate_observer_1, 100000, 400000); 189 EXPECT_EQ(300000, start_bitrate); 190 191 start_bitrate = 192 allocator_->AddBitrateObserver(&bitrate_observer_2, 200000, 400000); 193 EXPECT_EQ(200000, start_bitrate); 194 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); 195 196 start_bitrate = 197 allocator_->AddBitrateObserver(&bitrate_observer_3, 300000, 400000); 198 EXPECT_EQ(300000, start_bitrate); 199 EXPECT_EQ(100000, static_cast<int>(bitrate_observer_1.last_bitrate_)); 200 EXPECT_EQ(200000, static_cast<int>(bitrate_observer_2.last_bitrate_)); 201 202 // Low REMB. Verify that all observers still get their respective min bitrate. 203 allocator_->OnNetworkChanged(1000, 0, 0); 204 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_); // Min cap. 205 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_); // Min cap. 206 EXPECT_EQ(300000u, bitrate_observer_3.last_bitrate_); // Min cap. 207 208 allocator_->RemoveBitrateObserver(&bitrate_observer_1); 209 allocator_->RemoveBitrateObserver(&bitrate_observer_2); 210 allocator_->RemoveBitrateObserver(&bitrate_observer_3); 211 } 212 } // namespace webrtc 213