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 "call/bitrate_allocator.h"
12
13 #include <algorithm>
14 #include <memory>
15 #include <vector>
16
17 #include "system_wrappers/include/clock.h"
18 #include "test/gmock.h"
19 #include "test/gtest.h"
20
21 using ::testing::_;
22 using ::testing::AllOf;
23 using ::testing::Field;
24 using ::testing::NiceMock;
25
26 namespace webrtc {
27
28 namespace {
AllocationLimitsEq(uint32_t min_allocatable_rate_bps,uint32_t max_padding_rate_bps,uint32_t max_allocatable_rate_bps)29 auto AllocationLimitsEq(uint32_t min_allocatable_rate_bps,
30 uint32_t max_padding_rate_bps,
31 uint32_t max_allocatable_rate_bps) {
32 return AllOf(Field(&BitrateAllocationLimits::min_allocatable_rate,
33 DataRate::BitsPerSec(min_allocatable_rate_bps)),
34 Field(&BitrateAllocationLimits::max_allocatable_rate,
35 DataRate::BitsPerSec(max_allocatable_rate_bps)),
36 Field(&BitrateAllocationLimits::max_padding_rate,
37 DataRate::BitsPerSec(max_padding_rate_bps)));
38 }
39
AllocationLimitsEq(uint32_t min_allocatable_rate_bps,uint32_t max_padding_rate_bps)40 auto AllocationLimitsEq(uint32_t min_allocatable_rate_bps,
41 uint32_t max_padding_rate_bps) {
42 return AllOf(Field(&BitrateAllocationLimits::min_allocatable_rate,
43 DataRate::BitsPerSec(min_allocatable_rate_bps)),
44 Field(&BitrateAllocationLimits::max_padding_rate,
45 DataRate::BitsPerSec(max_padding_rate_bps)));
46 }
47
48 class MockLimitObserver : public BitrateAllocator::LimitObserver {
49 public:
50 MOCK_METHOD(void,
51 OnAllocationLimitsChanged,
52 (BitrateAllocationLimits),
53 (override));
54 };
55
56 class TestBitrateObserver : public BitrateAllocatorObserver {
57 public:
TestBitrateObserver()58 TestBitrateObserver()
59 : last_bitrate_bps_(0),
60 last_fraction_loss_(0),
61 last_rtt_ms_(0),
62 last_probing_interval_ms_(0),
63 protection_ratio_(0.0) {}
64
SetBitrateProtectionRatio(double protection_ratio)65 void SetBitrateProtectionRatio(double protection_ratio) {
66 protection_ratio_ = protection_ratio;
67 }
68
OnBitrateUpdated(BitrateAllocationUpdate update)69 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override {
70 last_bitrate_bps_ = update.target_bitrate.bps();
71 last_fraction_loss_ =
72 rtc::dchecked_cast<uint8_t>(update.packet_loss_ratio * 256);
73 last_rtt_ms_ = update.round_trip_time.ms();
74 last_probing_interval_ms_ = update.bwe_period.ms();
75 return update.target_bitrate.bps() * protection_ratio_;
76 }
77 uint32_t last_bitrate_bps_;
78 uint8_t last_fraction_loss_;
79 int64_t last_rtt_ms_;
80 int last_probing_interval_ms_;
81 double protection_ratio_;
82 };
83
84 constexpr int64_t kDefaultProbingIntervalMs = 3000;
85 const double kDefaultBitratePriority = 1.0;
86
CreateTargetRateMessage(uint32_t target_bitrate_bps,uint8_t fraction_loss,int64_t rtt_ms,int64_t bwe_period_ms)87 TargetTransferRate CreateTargetRateMessage(uint32_t target_bitrate_bps,
88 uint8_t fraction_loss,
89 int64_t rtt_ms,
90 int64_t bwe_period_ms) {
91 TargetTransferRate msg;
92 // The timestamp is just for log output, keeping it fixed just means fewer log
93 // messages in the test.
94 msg.at_time = Timestamp::Seconds(10000);
95 msg.target_rate = DataRate::BitsPerSec(target_bitrate_bps);
96 msg.stable_target_rate = msg.target_rate;
97 msg.network_estimate.bandwidth = msg.target_rate;
98 msg.network_estimate.loss_rate_ratio = fraction_loss / 255.0;
99 msg.network_estimate.round_trip_time = TimeDelta::Millis(rtt_ms);
100 msg.network_estimate.bwe_period = TimeDelta::Millis(bwe_period_ms);
101 return msg;
102 }
103 } // namespace
104
105 class BitrateAllocatorTest : public ::testing::Test {
106 protected:
BitrateAllocatorTest()107 BitrateAllocatorTest() : allocator_(new BitrateAllocator(&limit_observer_)) {
108 allocator_->OnNetworkEstimateChanged(
109 CreateTargetRateMessage(300000u, 0, 0, kDefaultProbingIntervalMs));
110 }
~BitrateAllocatorTest()111 ~BitrateAllocatorTest() {}
AddObserver(BitrateAllocatorObserver * observer,uint32_t min_bitrate_bps,uint32_t max_bitrate_bps,uint32_t pad_up_bitrate_bps,bool enforce_min_bitrate,double bitrate_priority)112 void AddObserver(BitrateAllocatorObserver* observer,
113 uint32_t min_bitrate_bps,
114 uint32_t max_bitrate_bps,
115 uint32_t pad_up_bitrate_bps,
116 bool enforce_min_bitrate,
117 double bitrate_priority) {
118 allocator_->AddObserver(
119 observer,
120 {min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
121 /* priority_bitrate */ 0, enforce_min_bitrate, bitrate_priority});
122 }
DefaultConfig() const123 MediaStreamAllocationConfig DefaultConfig() const {
124 MediaStreamAllocationConfig default_config;
125 default_config.min_bitrate_bps = 0;
126 default_config.max_bitrate_bps = 1500000;
127 default_config.pad_up_bitrate_bps = 0;
128 default_config.priority_bitrate_bps = 0;
129 default_config.enforce_min_bitrate = true;
130 default_config.bitrate_priority = kDefaultBitratePriority;
131 return default_config;
132 }
133
134 NiceMock<MockLimitObserver> limit_observer_;
135 std::unique_ptr<BitrateAllocator> allocator_;
136 };
137
TEST_F(BitrateAllocatorTest,RespectsPriorityBitrate)138 TEST_F(BitrateAllocatorTest, RespectsPriorityBitrate) {
139 TestBitrateObserver stream_a;
140 auto config_a = DefaultConfig();
141 config_a.min_bitrate_bps = 100000;
142 config_a.priority_bitrate_bps = 0;
143 allocator_->AddObserver(&stream_a, config_a);
144
145 TestBitrateObserver stream_b;
146 auto config_b = DefaultConfig();
147 config_b.min_bitrate_bps = 100000;
148 config_b.max_bitrate_bps = 300000;
149 config_b.priority_bitrate_bps = 300000;
150 allocator_->AddObserver(&stream_b, config_b);
151
152 allocator_->OnNetworkEstimateChanged(
153 CreateTargetRateMessage(100000, 0, 0, 0));
154 EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
155 EXPECT_EQ(stream_b.last_bitrate_bps_, 100000u);
156
157 allocator_->OnNetworkEstimateChanged(
158 CreateTargetRateMessage(200000, 0, 0, 0));
159 EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
160 EXPECT_EQ(stream_b.last_bitrate_bps_, 100000u);
161
162 allocator_->OnNetworkEstimateChanged(
163 CreateTargetRateMessage(300000, 0, 0, 0));
164 EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
165 EXPECT_EQ(stream_b.last_bitrate_bps_, 200000u);
166
167 allocator_->OnNetworkEstimateChanged(
168 CreateTargetRateMessage(400000, 0, 0, 0));
169 EXPECT_EQ(stream_a.last_bitrate_bps_, 100000u);
170 EXPECT_EQ(stream_b.last_bitrate_bps_, 300000u);
171
172 allocator_->OnNetworkEstimateChanged(
173 CreateTargetRateMessage(800000, 0, 0, 0));
174 EXPECT_EQ(stream_a.last_bitrate_bps_, 500000u);
175 EXPECT_EQ(stream_b.last_bitrate_bps_, 300000u);
176 }
177
TEST_F(BitrateAllocatorTest,UpdatingBitrateObserver)178 TEST_F(BitrateAllocatorTest, UpdatingBitrateObserver) {
179 TestBitrateObserver bitrate_observer;
180 const uint32_t kMinSendBitrateBps = 100000;
181 const uint32_t kPadUpToBitrateBps = 50000;
182 const uint32_t kMaxBitrateBps = 1500000;
183
184 EXPECT_CALL(limit_observer_,
185 OnAllocationLimitsChanged(AllocationLimitsEq(
186 kMinSendBitrateBps, kPadUpToBitrateBps, kMaxBitrateBps)));
187 AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps,
188 kPadUpToBitrateBps, true, kDefaultBitratePriority);
189 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer));
190 allocator_->OnNetworkEstimateChanged(
191 CreateTargetRateMessage(200000, 0, 0, kDefaultProbingIntervalMs));
192 EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer));
193
194 // TODO(pbos): Expect capping to 1.5M instead of 3M when not boosting the max
195 // bitrate for FEC/retransmissions (see todo in BitrateAllocator).
196 allocator_->OnNetworkEstimateChanged(
197 CreateTargetRateMessage(4000000, 0, 0, kDefaultProbingIntervalMs));
198 EXPECT_EQ(3000000, allocator_->GetStartBitrate(&bitrate_observer));
199
200 // Expect |max_padding_bitrate_bps| to change to 0 if the observer is updated.
201 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
202 AllocationLimitsEq(kMinSendBitrateBps, 0)));
203 AddObserver(&bitrate_observer, kMinSendBitrateBps, 4000000, 0, true,
204 kDefaultBitratePriority);
205 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
206 AllocationLimitsEq(kMinSendBitrateBps, 0)));
207 EXPECT_EQ(4000000, allocator_->GetStartBitrate(&bitrate_observer));
208
209 AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps, 0, true,
210 kDefaultBitratePriority);
211 EXPECT_EQ(3000000, allocator_->GetStartBitrate(&bitrate_observer));
212 EXPECT_EQ(3000000u, bitrate_observer.last_bitrate_bps_);
213 allocator_->OnNetworkEstimateChanged(
214 CreateTargetRateMessage(kMaxBitrateBps, 0, 0, kDefaultProbingIntervalMs));
215 EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_bps_);
216 }
217
TEST_F(BitrateAllocatorTest,TwoBitrateObserversOneRtcpObserver)218 TEST_F(BitrateAllocatorTest, TwoBitrateObserversOneRtcpObserver) {
219 TestBitrateObserver bitrate_observer_1;
220 TestBitrateObserver bitrate_observer_2;
221 const uint32_t kObs1StartBitrateBps = 100000;
222 const uint32_t kObs2StartBitrateBps = 200000;
223 const uint32_t kObs1MaxBitrateBps = 300000;
224 const uint32_t kObs2MaxBitrateBps = 300000;
225
226 EXPECT_CALL(limit_observer_,
227 OnAllocationLimitsChanged(AllocationLimitsEq(
228 kObs1StartBitrateBps, 0, kObs1MaxBitrateBps)));
229 AddObserver(&bitrate_observer_1, kObs1StartBitrateBps, kObs1MaxBitrateBps, 0,
230 true, kDefaultBitratePriority);
231 EXPECT_EQ(static_cast<int>(kObs1MaxBitrateBps),
232 allocator_->GetStartBitrate(&bitrate_observer_1));
233 EXPECT_CALL(limit_observer_,
234 OnAllocationLimitsChanged(AllocationLimitsEq(
235 kObs1StartBitrateBps + kObs2StartBitrateBps, 0,
236 kObs1MaxBitrateBps + kObs2MaxBitrateBps)));
237 AddObserver(&bitrate_observer_2, kObs2StartBitrateBps, kObs2MaxBitrateBps, 0,
238 true, kDefaultBitratePriority);
239 EXPECT_EQ(static_cast<int>(kObs2StartBitrateBps),
240 allocator_->GetStartBitrate(&bitrate_observer_2));
241
242 // Test too low start bitrate, hence lower than sum of min. Min bitrates
243 // will
244 // be allocated to all observers.
245 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
246 kObs2StartBitrateBps, 0, 50, kDefaultProbingIntervalMs));
247 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
248 EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
249 EXPECT_EQ(50, bitrate_observer_1.last_rtt_ms_);
250 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
251 EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
252 EXPECT_EQ(50, bitrate_observer_2.last_rtt_ms_);
253
254 // Test a bitrate which should be distributed equally.
255 allocator_->OnNetworkEstimateChanged(
256 CreateTargetRateMessage(500000, 0, 50, kDefaultProbingIntervalMs));
257 const uint32_t kBitrateToShare =
258 500000 - kObs2StartBitrateBps - kObs1StartBitrateBps;
259 EXPECT_EQ(100000u + kBitrateToShare / 2,
260 bitrate_observer_1.last_bitrate_bps_);
261 EXPECT_EQ(200000u + kBitrateToShare / 2,
262 bitrate_observer_2.last_bitrate_bps_);
263
264 // Limited by 2x max bitrates since we leave room for FEC and
265 // retransmissions.
266 allocator_->OnNetworkEstimateChanged(
267 CreateTargetRateMessage(1500000, 0, 50, kDefaultProbingIntervalMs));
268 EXPECT_EQ(600000u, bitrate_observer_1.last_bitrate_bps_);
269 EXPECT_EQ(600000u, bitrate_observer_2.last_bitrate_bps_);
270
271 // Verify that if the bandwidth estimate is set to zero, the allocated
272 // rate is
273 // zero.
274 allocator_->OnNetworkEstimateChanged(
275 CreateTargetRateMessage(0, 0, 50, kDefaultProbingIntervalMs));
276 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
277 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
278 }
279
TEST_F(BitrateAllocatorTest,RemoveObserverTriggersLimitObserver)280 TEST_F(BitrateAllocatorTest, RemoveObserverTriggersLimitObserver) {
281 TestBitrateObserver bitrate_observer;
282 const uint32_t kMinSendBitrateBps = 100000;
283 const uint32_t kPadUpToBitrateBps = 50000;
284 const uint32_t kMaxBitrateBps = 1500000;
285
286 EXPECT_CALL(limit_observer_,
287 OnAllocationLimitsChanged(AllocationLimitsEq(
288 kMinSendBitrateBps, kPadUpToBitrateBps, kMaxBitrateBps)));
289 AddObserver(&bitrate_observer, kMinSendBitrateBps, kMaxBitrateBps,
290 kPadUpToBitrateBps, true, kDefaultBitratePriority);
291 EXPECT_CALL(limit_observer_,
292 OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
293 allocator_->RemoveObserver(&bitrate_observer);
294 }
295
296 class BitrateAllocatorTestNoEnforceMin : public ::testing::Test {
297 protected:
BitrateAllocatorTestNoEnforceMin()298 BitrateAllocatorTestNoEnforceMin()
299 : allocator_(new BitrateAllocator(&limit_observer_)) {
300 allocator_->OnNetworkEstimateChanged(
301 CreateTargetRateMessage(300000u, 0, 0, kDefaultProbingIntervalMs));
302 }
~BitrateAllocatorTestNoEnforceMin()303 ~BitrateAllocatorTestNoEnforceMin() {}
AddObserver(BitrateAllocatorObserver * observer,uint32_t min_bitrate_bps,uint32_t max_bitrate_bps,uint32_t pad_up_bitrate_bps,bool enforce_min_bitrate,std::string track_id,double bitrate_priority)304 void AddObserver(BitrateAllocatorObserver* observer,
305 uint32_t min_bitrate_bps,
306 uint32_t max_bitrate_bps,
307 uint32_t pad_up_bitrate_bps,
308 bool enforce_min_bitrate,
309 std::string track_id,
310 double bitrate_priority) {
311 allocator_->AddObserver(
312 observer, {min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps, 0,
313 enforce_min_bitrate, bitrate_priority});
314 }
315 NiceMock<MockLimitObserver> limit_observer_;
316 std::unique_ptr<BitrateAllocator> allocator_;
317 };
318
319 // The following three tests verify enforcing a minimum bitrate works as
320 // intended.
TEST_F(BitrateAllocatorTestNoEnforceMin,OneBitrateObserver)321 TEST_F(BitrateAllocatorTestNoEnforceMin, OneBitrateObserver) {
322 TestBitrateObserver bitrate_observer_1;
323 // Expect OnAllocationLimitsChanged with |min_send_bitrate_bps| = 0 since
324 // AddObserver is called with |enforce_min_bitrate| = false.
325 EXPECT_CALL(limit_observer_,
326 OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
327 EXPECT_CALL(limit_observer_,
328 OnAllocationLimitsChanged(AllocationLimitsEq(0, 120000)));
329 AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
330 kDefaultBitratePriority);
331 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
332
333 // High BWE.
334 allocator_->OnNetworkEstimateChanged(
335 CreateTargetRateMessage(150000, 0, 0, kDefaultProbingIntervalMs));
336 EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_bps_);
337
338 // Low BWE.
339 allocator_->OnNetworkEstimateChanged(
340 CreateTargetRateMessage(10000, 0, 0, kDefaultProbingIntervalMs));
341 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
342
343 EXPECT_CALL(limit_observer_,
344 OnAllocationLimitsChanged(AllocationLimitsEq(0, 0)));
345 allocator_->RemoveObserver(&bitrate_observer_1);
346 }
347
TEST_F(BitrateAllocatorTestNoEnforceMin,ThreeBitrateObservers)348 TEST_F(BitrateAllocatorTestNoEnforceMin, ThreeBitrateObservers) {
349 TestBitrateObserver bitrate_observer_1;
350 TestBitrateObserver bitrate_observer_2;
351 TestBitrateObserver bitrate_observer_3;
352 // Set up the observers with min bitrates at 100000, 200000, and 300000.
353 AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
354 kDefaultBitratePriority);
355 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
356
357 AddObserver(&bitrate_observer_2, 200000, 400000, 0, false, "",
358 kDefaultBitratePriority);
359 EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
360 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
361
362 AddObserver(&bitrate_observer_3, 300000, 400000, 0, false, "",
363 kDefaultBitratePriority);
364 EXPECT_EQ(0, allocator_->GetStartBitrate(&bitrate_observer_3));
365 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
366 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
367
368 // High BWE. Make sure the controllers get a fair share of the surplus (i.e.,
369 // what is left after each controller gets its min rate).
370 allocator_->OnNetworkEstimateChanged(
371 CreateTargetRateMessage(690000, 0, 0, kDefaultProbingIntervalMs));
372 // Verify that each observer gets its min rate (sum of min rates is 600000),
373 // and that the remaining 90000 is divided equally among the three.
374 uint32_t bitrate_to_share = 690000u - 100000u - 200000u - 300000u;
375 EXPECT_EQ(100000u + bitrate_to_share / 3,
376 bitrate_observer_1.last_bitrate_bps_);
377 EXPECT_EQ(200000u + bitrate_to_share / 3,
378 bitrate_observer_2.last_bitrate_bps_);
379 EXPECT_EQ(300000u + bitrate_to_share / 3,
380 bitrate_observer_3.last_bitrate_bps_);
381
382 // BWE below the sum of observer's min bitrate.
383 allocator_->OnNetworkEstimateChanged(
384 CreateTargetRateMessage(300000, 0, 0, kDefaultProbingIntervalMs));
385 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_); // Min bitrate.
386 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_); // Min bitrate.
387 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_); // Nothing.
388
389 // Increased BWE, but still below the sum of configured min bitrates for all
390 // observers and too little for observer 3. 1 and 2 will share the rest.
391 allocator_->OnNetworkEstimateChanged(
392 CreateTargetRateMessage(500000, 0, 0, kDefaultProbingIntervalMs));
393 EXPECT_EQ(200000u, bitrate_observer_1.last_bitrate_bps_); // Min + split.
394 EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_bps_); // Min + split.
395 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_); // Nothing.
396
397 // Below min for all.
398 allocator_->OnNetworkEstimateChanged(
399 CreateTargetRateMessage(10000, 0, 0, kDefaultProbingIntervalMs));
400 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
401 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
402 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_);
403
404 // Verify that zero estimated bandwidth, means that that all gets zero,
405 // regardless of set min bitrate.
406 allocator_->OnNetworkEstimateChanged(
407 CreateTargetRateMessage(0, 0, 0, kDefaultProbingIntervalMs));
408 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
409 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
410 EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_bps_);
411
412 allocator_->RemoveObserver(&bitrate_observer_1);
413 allocator_->RemoveObserver(&bitrate_observer_2);
414 allocator_->RemoveObserver(&bitrate_observer_3);
415 }
416
TEST_F(BitrateAllocatorTestNoEnforceMin,OneBitrateObserverWithPacketLoss)417 TEST_F(BitrateAllocatorTestNoEnforceMin, OneBitrateObserverWithPacketLoss) {
418 const uint32_t kMinBitrateBps = 100000;
419 const uint32_t kMaxBitrateBps = 400000;
420 // Hysteresis adds another 10% or 20kbps to min bitrate.
421 const uint32_t kMinStartBitrateBps =
422 kMinBitrateBps + std::max(20000u, kMinBitrateBps / 10);
423
424 // Expect OnAllocationLimitsChanged with |min_send_bitrate_bps| = 0 since
425 // AddObserver is called with |enforce_min_bitrate| = false.
426 TestBitrateObserver bitrate_observer;
427 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
428 AllocationLimitsEq(0, 0, kMaxBitrateBps)));
429 AddObserver(&bitrate_observer, kMinBitrateBps, kMaxBitrateBps, 0, false, "",
430 kDefaultBitratePriority);
431 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer));
432
433 // High BWE.
434 allocator_->OnNetworkEstimateChanged(
435 CreateTargetRateMessage(150000, 0, 0, kDefaultProbingIntervalMs));
436 EXPECT_EQ(150000u, bitrate_observer.last_bitrate_bps_);
437
438 // Add loss and use a part of the bitrate for protection.
439 const double kProtectionRatio = 0.4;
440 const uint8_t fraction_loss = kProtectionRatio * 256;
441 bitrate_observer.SetBitrateProtectionRatio(kProtectionRatio);
442 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
443 200000, 0, fraction_loss, kDefaultProbingIntervalMs));
444 EXPECT_EQ(200000u, bitrate_observer.last_bitrate_bps_);
445
446 // Above the min threshold, but not enough given the protection used.
447 // Limits changed, as we will video is now off and we need to pad up to the
448 // start bitrate.
449 // Verify the hysteresis is added for the protection.
450 const uint32_t kMinStartBitrateWithProtectionBps =
451 static_cast<uint32_t>(kMinStartBitrateBps * (1 + kProtectionRatio));
452 EXPECT_CALL(limit_observer_,
453 OnAllocationLimitsChanged(AllocationLimitsEq(
454 0, kMinStartBitrateWithProtectionBps, kMaxBitrateBps)));
455 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
456 kMinStartBitrateBps + 1000, 0, fraction_loss, kDefaultProbingIntervalMs));
457 EXPECT_EQ(0u, bitrate_observer.last_bitrate_bps_);
458
459 allocator_->OnNetworkEstimateChanged(
460 CreateTargetRateMessage(kMinStartBitrateWithProtectionBps - 1000, 0,
461 fraction_loss, kDefaultProbingIntervalMs));
462 EXPECT_EQ(0u, bitrate_observer.last_bitrate_bps_);
463
464 // Just enough to enable video again.
465 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(
466 AllocationLimitsEq(0, 0, kMaxBitrateBps)));
467 allocator_->OnNetworkEstimateChanged(
468 CreateTargetRateMessage(kMinStartBitrateWithProtectionBps, 0,
469 fraction_loss, kDefaultProbingIntervalMs));
470 EXPECT_EQ(kMinStartBitrateWithProtectionBps,
471 bitrate_observer.last_bitrate_bps_);
472
473 // Remove all protection and make sure video is not paused as earlier.
474 bitrate_observer.SetBitrateProtectionRatio(0.0);
475 allocator_->OnNetworkEstimateChanged(
476 CreateTargetRateMessage(kMinStartBitrateWithProtectionBps - 1000, 0, 0,
477 kDefaultProbingIntervalMs));
478 EXPECT_EQ(kMinStartBitrateWithProtectionBps - 1000,
479 bitrate_observer.last_bitrate_bps_);
480
481 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
482 kMinStartBitrateBps, 0, 0, kDefaultProbingIntervalMs));
483 EXPECT_EQ(kMinStartBitrateBps, bitrate_observer.last_bitrate_bps_);
484
485 EXPECT_CALL(limit_observer_,
486 OnAllocationLimitsChanged(AllocationLimitsEq(0, 0, 0)));
487 allocator_->RemoveObserver(&bitrate_observer);
488 }
489
TEST_F(BitrateAllocatorTest,TotalAllocationLimitsAreUnaffectedByProtectionRatio)490 TEST_F(BitrateAllocatorTest,
491 TotalAllocationLimitsAreUnaffectedByProtectionRatio) {
492 TestBitrateObserver bitrate_observer;
493
494 const uint32_t kMinBitrateBps = 100000;
495 const uint32_t kMaxBitrateBps = 400000;
496
497 // Register |bitrate_observer| and expect total allocation limits to change.
498 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(AllocationLimitsEq(
499 kMinBitrateBps, 0, kMaxBitrateBps)))
500 .Times(1);
501 MediaStreamAllocationConfig allocation_config = DefaultConfig();
502 allocation_config.min_bitrate_bps = kMinBitrateBps;
503 allocation_config.max_bitrate_bps = kMaxBitrateBps;
504 allocator_->AddObserver(&bitrate_observer, allocation_config);
505
506 // Observer uses 20% of it's allocated bitrate for protection.
507 bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.2);
508 // Total allocation limits are unaffected by the protection rate change.
509 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
510 allocator_->OnNetworkEstimateChanged(
511 CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
512
513 // Observer uses 0% of it's allocated bitrate for protection.
514 bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.0);
515 // Total allocation limits are unaffected by the protection rate change.
516 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
517 allocator_->OnNetworkEstimateChanged(
518 CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
519
520 // Observer again uses 20% of it's allocated bitrate for protection.
521 bitrate_observer.SetBitrateProtectionRatio(/*protection_ratio=*/0.2);
522 // Total allocation limits are unaffected by the protection rate change.
523 EXPECT_CALL(limit_observer_, OnAllocationLimitsChanged(_)).Times(0);
524 allocator_->OnNetworkEstimateChanged(
525 CreateTargetRateMessage(200000u, 0, 100, kDefaultProbingIntervalMs));
526 }
527
TEST_F(BitrateAllocatorTestNoEnforceMin,TwoBitrateObserverWithPacketLoss)528 TEST_F(BitrateAllocatorTestNoEnforceMin, TwoBitrateObserverWithPacketLoss) {
529 TestBitrateObserver bitrate_observer_1;
530 TestBitrateObserver bitrate_observer_2;
531
532 AddObserver(&bitrate_observer_1, 100000, 400000, 0, false, "",
533 kDefaultBitratePriority);
534 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
535 AddObserver(&bitrate_observer_2, 200000, 400000, 0, false, "",
536 kDefaultBitratePriority);
537 EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
538 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
539
540 // Enough bitrate for both.
541 bitrate_observer_2.SetBitrateProtectionRatio(0.5);
542 allocator_->OnNetworkEstimateChanged(
543 CreateTargetRateMessage(300000, 0, 0, kDefaultProbingIntervalMs));
544 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
545 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_);
546
547 // Above min for observer 2, but too little given the protection used.
548 allocator_->OnNetworkEstimateChanged(
549 CreateTargetRateMessage(330000, 0, 0, kDefaultProbingIntervalMs));
550 EXPECT_EQ(330000u, bitrate_observer_1.last_bitrate_bps_);
551 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
552
553 allocator_->OnNetworkEstimateChanged(
554 CreateTargetRateMessage(100000, 0, 0, kDefaultProbingIntervalMs));
555 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
556 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
557
558 allocator_->OnNetworkEstimateChanged(
559 CreateTargetRateMessage(99999, 0, 0, kDefaultProbingIntervalMs));
560 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
561 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
562
563 allocator_->OnNetworkEstimateChanged(
564 CreateTargetRateMessage(119000, 0, 0, kDefaultProbingIntervalMs));
565 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
566 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
567
568 allocator_->OnNetworkEstimateChanged(
569 CreateTargetRateMessage(120000, 0, 0, kDefaultProbingIntervalMs));
570 EXPECT_EQ(120000u, bitrate_observer_1.last_bitrate_bps_);
571 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
572
573 // Verify the protection is accounted for before resuming observer 2.
574 allocator_->OnNetworkEstimateChanged(
575 CreateTargetRateMessage(429000, 0, 0, kDefaultProbingIntervalMs));
576 EXPECT_EQ(400000u, bitrate_observer_1.last_bitrate_bps_);
577 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
578
579 allocator_->OnNetworkEstimateChanged(
580 CreateTargetRateMessage(430000, 0, 0, kDefaultProbingIntervalMs));
581 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
582 EXPECT_EQ(330000u, bitrate_observer_2.last_bitrate_bps_);
583
584 allocator_->RemoveObserver(&bitrate_observer_1);
585 allocator_->RemoveObserver(&bitrate_observer_2);
586 }
587
TEST_F(BitrateAllocatorTest,ThreeBitrateObserversLowBweEnforceMin)588 TEST_F(BitrateAllocatorTest, ThreeBitrateObserversLowBweEnforceMin) {
589 TestBitrateObserver bitrate_observer_1;
590 TestBitrateObserver bitrate_observer_2;
591 TestBitrateObserver bitrate_observer_3;
592
593 AddObserver(&bitrate_observer_1, 100000, 400000, 0, true,
594 kDefaultBitratePriority);
595 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
596
597 AddObserver(&bitrate_observer_2, 200000, 400000, 0, true,
598 kDefaultBitratePriority);
599 EXPECT_EQ(200000, allocator_->GetStartBitrate(&bitrate_observer_2));
600 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_);
601
602 AddObserver(&bitrate_observer_3, 300000, 400000, 0, true,
603 kDefaultBitratePriority);
604 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_3));
605 EXPECT_EQ(100000, static_cast<int>(bitrate_observer_1.last_bitrate_bps_));
606 EXPECT_EQ(200000, static_cast<int>(bitrate_observer_2.last_bitrate_bps_));
607
608 // Low BWE. Verify that all observers still get their respective min
609 // bitrate.
610 allocator_->OnNetworkEstimateChanged(
611 CreateTargetRateMessage(1000, 0, 0, kDefaultProbingIntervalMs));
612 EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_bps_); // Min cap.
613 EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_bps_); // Min cap.
614 EXPECT_EQ(300000u, bitrate_observer_3.last_bitrate_bps_); // Min cap.
615
616 allocator_->RemoveObserver(&bitrate_observer_1);
617 allocator_->RemoveObserver(&bitrate_observer_2);
618 allocator_->RemoveObserver(&bitrate_observer_3);
619 }
620
TEST_F(BitrateAllocatorTest,AddObserverWhileNetworkDown)621 TEST_F(BitrateAllocatorTest, AddObserverWhileNetworkDown) {
622 TestBitrateObserver bitrate_observer_1;
623 EXPECT_CALL(limit_observer_,
624 OnAllocationLimitsChanged(AllocationLimitsEq(50000, 0)));
625
626 AddObserver(&bitrate_observer_1, 50000, 400000, 0, true,
627 kDefaultBitratePriority);
628 EXPECT_EQ(300000, allocator_->GetStartBitrate(&bitrate_observer_1));
629
630 // Set network down, ie, no available bitrate.
631 allocator_->OnNetworkEstimateChanged(
632 CreateTargetRateMessage(0, 0, 0, kDefaultProbingIntervalMs));
633
634 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
635
636 TestBitrateObserver bitrate_observer_2;
637 // Adding an observer while the network is down should not affect the limits.
638 EXPECT_CALL(limit_observer_,
639 OnAllocationLimitsChanged(AllocationLimitsEq(50000 + 50000, 0)));
640 AddObserver(&bitrate_observer_2, 50000, 400000, 0, true,
641 kDefaultBitratePriority);
642
643 // Expect the start_bitrate to be set as if the network was still up but that
644 // the new observer have been notified that the network is down.
645 EXPECT_EQ(300000 / 2, allocator_->GetStartBitrate(&bitrate_observer_2));
646 EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_bps_);
647 EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_bps_);
648
649 // Set network back up.
650 allocator_->OnNetworkEstimateChanged(
651 CreateTargetRateMessage(1500000, 0, 50, kDefaultProbingIntervalMs));
652 EXPECT_EQ(750000u, bitrate_observer_1.last_bitrate_bps_);
653 EXPECT_EQ(750000u, bitrate_observer_2.last_bitrate_bps_);
654 }
655
TEST_F(BitrateAllocatorTest,MixedEnforecedConfigs)656 TEST_F(BitrateAllocatorTest, MixedEnforecedConfigs) {
657 TestBitrateObserver enforced_observer;
658 AddObserver(&enforced_observer, 6000, 30000, 0, true,
659 kDefaultBitratePriority);
660 EXPECT_EQ(60000, allocator_->GetStartBitrate(&enforced_observer));
661
662 TestBitrateObserver not_enforced_observer;
663 AddObserver(¬_enforced_observer, 30000, 2500000, 0, false,
664 kDefaultBitratePriority);
665 EXPECT_EQ(270000, allocator_->GetStartBitrate(¬_enforced_observer));
666 EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
667
668 allocator_->OnNetworkEstimateChanged(
669 CreateTargetRateMessage(36000, 0, 50, kDefaultProbingIntervalMs));
670 EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
671 EXPECT_EQ(30000u, not_enforced_observer.last_bitrate_bps_);
672
673 allocator_->OnNetworkEstimateChanged(
674 CreateTargetRateMessage(35000, 0, 50, kDefaultProbingIntervalMs));
675 EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
676 EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
677
678 allocator_->OnNetworkEstimateChanged(
679 CreateTargetRateMessage(5000, 0, 50, kDefaultProbingIntervalMs));
680 EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
681 EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
682
683 allocator_->OnNetworkEstimateChanged(
684 CreateTargetRateMessage(36000, 0, 50, kDefaultProbingIntervalMs));
685 EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
686 EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
687
688 allocator_->OnNetworkEstimateChanged(
689 CreateTargetRateMessage(55000, 0, 50, kDefaultProbingIntervalMs));
690 EXPECT_EQ(30000u, enforced_observer.last_bitrate_bps_);
691 EXPECT_EQ(0u, not_enforced_observer.last_bitrate_bps_);
692
693 allocator_->OnNetworkEstimateChanged(
694 CreateTargetRateMessage(56000, 0, 50, kDefaultProbingIntervalMs));
695 EXPECT_EQ(6000u, enforced_observer.last_bitrate_bps_);
696 EXPECT_EQ(50000u, not_enforced_observer.last_bitrate_bps_);
697
698 allocator_->OnNetworkEstimateChanged(
699 CreateTargetRateMessage(56000, 0, 50, kDefaultProbingIntervalMs));
700 EXPECT_EQ(16000u, enforced_observer.last_bitrate_bps_);
701 EXPECT_EQ(40000u, not_enforced_observer.last_bitrate_bps_);
702
703 allocator_->RemoveObserver(&enforced_observer);
704 allocator_->RemoveObserver(¬_enforced_observer);
705 }
706
TEST_F(BitrateAllocatorTest,AvoidToggleAbsolute)707 TEST_F(BitrateAllocatorTest, AvoidToggleAbsolute) {
708 TestBitrateObserver observer;
709 AddObserver(&observer, 30000, 300000, 0, false, kDefaultBitratePriority);
710 EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
711
712 allocator_->OnNetworkEstimateChanged(
713 CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
714 EXPECT_EQ(30000u, observer.last_bitrate_bps_);
715
716 allocator_->OnNetworkEstimateChanged(
717 CreateTargetRateMessage(20000, 0, 50, kDefaultProbingIntervalMs));
718 EXPECT_EQ(0u, observer.last_bitrate_bps_);
719
720 allocator_->OnNetworkEstimateChanged(
721 CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
722 EXPECT_EQ(0u, observer.last_bitrate_bps_);
723
724 allocator_->OnNetworkEstimateChanged(
725 CreateTargetRateMessage(49000, 0, 50, kDefaultProbingIntervalMs));
726 EXPECT_EQ(0u, observer.last_bitrate_bps_);
727
728 allocator_->OnNetworkEstimateChanged(
729 CreateTargetRateMessage(50000, 0, 50, kDefaultProbingIntervalMs));
730 EXPECT_EQ(50000u, observer.last_bitrate_bps_);
731
732 allocator_->OnNetworkEstimateChanged(
733 CreateTargetRateMessage(30000, 0, 50, kDefaultProbingIntervalMs));
734 EXPECT_EQ(30000u, observer.last_bitrate_bps_);
735
736 allocator_->RemoveObserver(&observer);
737 }
738
TEST_F(BitrateAllocatorTest,AvoidTogglePercent)739 TEST_F(BitrateAllocatorTest, AvoidTogglePercent) {
740 TestBitrateObserver observer;
741 AddObserver(&observer, 300000, 600000, 0, false, kDefaultBitratePriority);
742 EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
743
744 allocator_->OnNetworkEstimateChanged(
745 CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
746 EXPECT_EQ(300000u, observer.last_bitrate_bps_);
747
748 allocator_->OnNetworkEstimateChanged(
749 CreateTargetRateMessage(200000, 0, 50, kDefaultProbingIntervalMs));
750 EXPECT_EQ(0u, observer.last_bitrate_bps_);
751
752 allocator_->OnNetworkEstimateChanged(
753 CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
754 EXPECT_EQ(0u, observer.last_bitrate_bps_);
755
756 allocator_->OnNetworkEstimateChanged(
757 CreateTargetRateMessage(329000, 0, 50, kDefaultProbingIntervalMs));
758 EXPECT_EQ(0u, observer.last_bitrate_bps_);
759
760 allocator_->OnNetworkEstimateChanged(
761 CreateTargetRateMessage(330000, 0, 50, kDefaultProbingIntervalMs));
762 EXPECT_EQ(330000u, observer.last_bitrate_bps_);
763
764 allocator_->OnNetworkEstimateChanged(
765 CreateTargetRateMessage(300000, 0, 50, kDefaultProbingIntervalMs));
766 EXPECT_EQ(300000u, observer.last_bitrate_bps_);
767
768 allocator_->RemoveObserver(&observer);
769 }
770
TEST_F(BitrateAllocatorTest,PassProbingInterval)771 TEST_F(BitrateAllocatorTest, PassProbingInterval) {
772 TestBitrateObserver observer;
773 AddObserver(&observer, 300000, 600000, 0, false, kDefaultBitratePriority);
774 EXPECT_EQ(300000, allocator_->GetStartBitrate(&observer));
775
776 allocator_->OnNetworkEstimateChanged(
777 CreateTargetRateMessage(300000, 0, 50, 5000));
778 EXPECT_EQ(5000, observer.last_probing_interval_ms_);
779
780 allocator_->RemoveObserver(&observer);
781 }
782
TEST_F(BitrateAllocatorTest,PriorityRateOneObserverBasic)783 TEST_F(BitrateAllocatorTest, PriorityRateOneObserverBasic) {
784 TestBitrateObserver observer;
785 const uint32_t kMinSendBitrateBps = 10;
786 const uint32_t kMaxSendBitrateBps = 60;
787 const uint32_t kNetworkBandwidthBps = 30;
788
789 AddObserver(&observer, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true, 2.0);
790 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
791 kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
792
793 EXPECT_EQ(kNetworkBandwidthBps, observer.last_bitrate_bps_);
794
795 allocator_->RemoveObserver(&observer);
796 }
797
798 // Tests that two observers with the same bitrate priority are allocated
799 // their bitrate evenly.
TEST_F(BitrateAllocatorTest,PriorityRateTwoObserversBasic)800 TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBasic) {
801 TestBitrateObserver observer_low_1;
802 TestBitrateObserver observer_low_2;
803 const uint32_t kMinSendBitrateBps = 10;
804 const uint32_t kMaxSendBitrateBps = 60;
805 const uint32_t kNetworkBandwidthBps = 60;
806 AddObserver(&observer_low_1, kMinSendBitrateBps, kMaxSendBitrateBps, 0, false,
807 2.0);
808 AddObserver(&observer_low_2, kMinSendBitrateBps, kMaxSendBitrateBps, 0, false,
809 2.0);
810 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
811 kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
812
813 EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_1.last_bitrate_bps_);
814 EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_2.last_bitrate_bps_);
815
816 allocator_->RemoveObserver(&observer_low_1);
817 allocator_->RemoveObserver(&observer_low_2);
818 }
819
820 // Tests that there is no difference in functionality when the min bitrate is
821 // enforced.
TEST_F(BitrateAllocatorTest,PriorityRateTwoObserversBasicMinEnforced)822 TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBasicMinEnforced) {
823 TestBitrateObserver observer_low_1;
824 TestBitrateObserver observer_low_2;
825 const uint32_t kMinSendBitrateBps = 0;
826 const uint32_t kMaxSendBitrateBps = 60;
827 const uint32_t kNetworkBandwidthBps = 60;
828 AddObserver(&observer_low_1, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
829 2.0);
830 AddObserver(&observer_low_2, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
831 2.0);
832 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
833 kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
834
835 EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_1.last_bitrate_bps_);
836 EXPECT_EQ(kNetworkBandwidthBps / 2, observer_low_2.last_bitrate_bps_);
837
838 allocator_->RemoveObserver(&observer_low_1);
839 allocator_->RemoveObserver(&observer_low_2);
840 }
841
842 // Tests that if the available bandwidth is the sum of the max bitrate
843 // of all observers, they will be allocated their max.
TEST_F(BitrateAllocatorTest,PriorityRateTwoObserversBothAllocatedMax)844 TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversBothAllocatedMax) {
845 TestBitrateObserver observer_low;
846 TestBitrateObserver observer_mid;
847 const uint32_t kMinSendBitrateBps = 0;
848 const uint32_t kMaxSendBitrateBps = 60;
849 const uint32_t kNetworkBandwidthBps = kMaxSendBitrateBps * 2;
850 AddObserver(&observer_low, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
851 2.0);
852 AddObserver(&observer_mid, kMinSendBitrateBps, kMaxSendBitrateBps, 0, true,
853 4.0);
854 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
855 kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
856
857 EXPECT_EQ(kMaxSendBitrateBps, observer_low.last_bitrate_bps_);
858 EXPECT_EQ(kMaxSendBitrateBps, observer_mid.last_bitrate_bps_);
859
860 allocator_->RemoveObserver(&observer_low);
861 allocator_->RemoveObserver(&observer_mid);
862 }
863
864 // Tests that after a higher bitrate priority observer has been allocated its
865 // max bitrate the lower priority observer will then be allocated the remaining
866 // bitrate.
TEST_F(BitrateAllocatorTest,PriorityRateTwoObserversOneAllocatedToMax)867 TEST_F(BitrateAllocatorTest, PriorityRateTwoObserversOneAllocatedToMax) {
868 TestBitrateObserver observer_low;
869 TestBitrateObserver observer_mid;
870 AddObserver(&observer_low, 10, 50, 0, false, 2.0);
871 AddObserver(&observer_mid, 10, 50, 0, false, 4.0);
872 allocator_->OnNetworkEstimateChanged(
873 CreateTargetRateMessage(90, 0, 0, kDefaultProbingIntervalMs));
874
875 EXPECT_EQ(40u, observer_low.last_bitrate_bps_);
876 EXPECT_EQ(50u, observer_mid.last_bitrate_bps_);
877
878 allocator_->RemoveObserver(&observer_low);
879 allocator_->RemoveObserver(&observer_mid);
880 }
881
882 // Tests that three observers with three different bitrate priorities will all
883 // be allocated bitrate according to their relative bitrate priority.
TEST_F(BitrateAllocatorTest,PriorityRateThreeObserversAllocatedRelativeAmounts)884 TEST_F(BitrateAllocatorTest,
885 PriorityRateThreeObserversAllocatedRelativeAmounts) {
886 TestBitrateObserver observer_low;
887 TestBitrateObserver observer_mid;
888 TestBitrateObserver observer_high;
889 const uint32_t kMaxBitrate = 100;
890 // Not enough bandwidth to fill any observer's max bitrate.
891 const uint32_t kNetworkBandwidthBps = 70;
892 const double kLowBitratePriority = 2.0;
893 const double kMidBitratePriority = 4.0;
894 const double kHighBitratePriority = 8.0;
895 const double kTotalBitratePriority =
896 kLowBitratePriority + kMidBitratePriority + kHighBitratePriority;
897 AddObserver(&observer_low, 0, kMaxBitrate, 0, false, kLowBitratePriority);
898 AddObserver(&observer_mid, 0, kMaxBitrate, 0, false, kMidBitratePriority);
899 AddObserver(&observer_high, 0, kMaxBitrate, 0, false, kHighBitratePriority);
900 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
901 kNetworkBandwidthBps, 0, 0, kDefaultProbingIntervalMs));
902
903 const double kLowFractionAllocated =
904 kLowBitratePriority / kTotalBitratePriority;
905 const double kMidFractionAllocated =
906 kMidBitratePriority / kTotalBitratePriority;
907 const double kHighFractionAllocated =
908 kHighBitratePriority / kTotalBitratePriority;
909 EXPECT_EQ(kLowFractionAllocated * kNetworkBandwidthBps,
910 observer_low.last_bitrate_bps_);
911 EXPECT_EQ(kMidFractionAllocated * kNetworkBandwidthBps,
912 observer_mid.last_bitrate_bps_);
913 EXPECT_EQ(kHighFractionAllocated * kNetworkBandwidthBps,
914 observer_high.last_bitrate_bps_);
915
916 allocator_->RemoveObserver(&observer_low);
917 allocator_->RemoveObserver(&observer_mid);
918 allocator_->RemoveObserver(&observer_high);
919 }
920
921 // Tests that after the high priority observer has been allocated its maximum
922 // bitrate, the other two observers are still allocated bitrate according to
923 // their relative bitrate priority.
TEST_F(BitrateAllocatorTest,PriorityRateThreeObserversHighAllocatedToMax)924 TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversHighAllocatedToMax) {
925 TestBitrateObserver observer_low;
926 const double kLowBitratePriority = 2.0;
927 TestBitrateObserver observer_mid;
928 const double kMidBitratePriority = 4.0;
929 TestBitrateObserver observer_high;
930 const double kHighBitratePriority = 8.0;
931
932 const uint32_t kAvailableBitrate = 90;
933 const uint32_t kMaxBitrate = 40;
934 const uint32_t kMinBitrate = 10;
935 // Remaining bitrate after allocating to all mins and knowing that the high
936 // priority observer will have its max bitrate allocated.
937 const uint32_t kRemainingBitrate =
938 kAvailableBitrate - kMaxBitrate - (2 * kMinBitrate);
939
940 AddObserver(&observer_low, kMinBitrate, kMaxBitrate, 0, false,
941 kLowBitratePriority);
942 AddObserver(&observer_mid, kMinBitrate, kMaxBitrate, 0, false,
943 kMidBitratePriority);
944 AddObserver(&observer_high, kMinBitrate, kMaxBitrate, 0, false,
945 kHighBitratePriority);
946 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
947 kAvailableBitrate, 0, 0, kDefaultProbingIntervalMs));
948
949 const double kLowFractionAllocated =
950 kLowBitratePriority / (kLowBitratePriority + kMidBitratePriority);
951 const double kMidFractionAllocated =
952 kMidBitratePriority / (kLowBitratePriority + kMidBitratePriority);
953 EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kLowFractionAllocated),
954 observer_low.last_bitrate_bps_);
955 EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kMidFractionAllocated),
956 observer_mid.last_bitrate_bps_);
957 EXPECT_EQ(40u, observer_high.last_bitrate_bps_);
958
959 allocator_->RemoveObserver(&observer_low);
960 allocator_->RemoveObserver(&observer_mid);
961 allocator_->RemoveObserver(&observer_high);
962 }
963
964 // Tests that after the low priority observer has been allocated its maximum
965 // bitrate, the other two observers are still allocated bitrate according to
966 // their relative bitrate priority.
TEST_F(BitrateAllocatorTest,PriorityRateThreeObserversLowAllocatedToMax)967 TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversLowAllocatedToMax) {
968 TestBitrateObserver observer_low;
969 const double kLowBitratePriority = 2.0;
970 const uint32_t kLowMaxBitrate = 10;
971 TestBitrateObserver observer_mid;
972 const double kMidBitratePriority = 4.0;
973 TestBitrateObserver observer_high;
974 const double kHighBitratePriority = 8.0;
975
976 const uint32_t kMinBitrate = 0;
977 const uint32_t kMaxBitrate = 60;
978 const uint32_t kAvailableBitrate = 100;
979 // Remaining bitrate knowing that the low priority observer is allocated its
980 // max bitrate. We know this because it is allocated 2.0/14.0 (1/7) of the
981 // available bitrate, so 70 bps would be sufficient network bandwidth.
982 const uint32_t kRemainingBitrate = kAvailableBitrate - kLowMaxBitrate;
983
984 AddObserver(&observer_low, kMinBitrate, kLowMaxBitrate, 0, false,
985 kLowBitratePriority);
986 AddObserver(&observer_mid, kMinBitrate, kMaxBitrate, 0, false,
987 kMidBitratePriority);
988 AddObserver(&observer_high, kMinBitrate, kMaxBitrate, 0, false,
989 kHighBitratePriority);
990 allocator_->OnNetworkEstimateChanged(CreateTargetRateMessage(
991 kAvailableBitrate, 0, 0, kDefaultProbingIntervalMs));
992
993 const double kMidFractionAllocated =
994 kMidBitratePriority / (kMidBitratePriority + kHighBitratePriority);
995 const double kHighFractionAllocated =
996 kHighBitratePriority / (kMidBitratePriority + kHighBitratePriority);
997 EXPECT_EQ(kLowMaxBitrate, observer_low.last_bitrate_bps_);
998 EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kMidFractionAllocated),
999 observer_mid.last_bitrate_bps_);
1000 EXPECT_EQ(kMinBitrate + (kRemainingBitrate * kHighFractionAllocated),
1001 observer_high.last_bitrate_bps_);
1002
1003 allocator_->RemoveObserver(&observer_low);
1004 allocator_->RemoveObserver(&observer_mid);
1005 allocator_->RemoveObserver(&observer_high);
1006 }
1007
1008 // Tests that after two observers are allocated bitrate to their max, the
1009 // the remaining observer is allocated what's left appropriately. This test
1010 // handles an edge case where the medium and high observer reach their
1011 // "relative" max allocation at the same time. The high has 40 to allocate
1012 // above its min, and the mid has 20 to allocate above its min, which scaled
1013 // by their bitrate priority is the same for each.
TEST_F(BitrateAllocatorTest,PriorityRateThreeObserversTwoAllocatedToMax)1014 TEST_F(BitrateAllocatorTest, PriorityRateThreeObserversTwoAllocatedToMax) {
1015 TestBitrateObserver observer_low;
1016 TestBitrateObserver observer_mid;
1017 TestBitrateObserver observer_high;
1018 AddObserver(&observer_low, 10, 40, 0, false, 2.0);
1019 // Scaled allocation above the min allocation is the same for these two,
1020 // meaning they will get allocated their max at the same time.
1021 // Scaled (target allocation) = (max - min) / bitrate priority
1022 AddObserver(&observer_mid, 10, 30, 0, false, 4.0);
1023 AddObserver(&observer_high, 10, 50, 0, false, 8.0);
1024 allocator_->OnNetworkEstimateChanged(
1025 CreateTargetRateMessage(110, 0, 0, kDefaultProbingIntervalMs));
1026
1027 EXPECT_EQ(30u, observer_low.last_bitrate_bps_);
1028 EXPECT_EQ(30u, observer_mid.last_bitrate_bps_);
1029 EXPECT_EQ(50u, observer_high.last_bitrate_bps_);
1030
1031 allocator_->RemoveObserver(&observer_low);
1032 allocator_->RemoveObserver(&observer_mid);
1033 allocator_->RemoveObserver(&observer_high);
1034 }
1035
1036 } // namespace webrtc
1037