• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "testing/gtest/include/gtest/gtest.h"
12 
13 #include <algorithm>
14 #include <vector>
15 
16 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
18 
19 using webrtc::RtcpBandwidthObserver;
20 using webrtc::BitrateObserver;
21 using webrtc::BitrateController;
22 
WeightedLoss(int num_packets1,uint8_t fraction_loss1,int num_packets2,uint8_t fraction_loss2)23 uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1,
24                      int num_packets2, uint8_t fraction_loss2) {
25   int weighted_sum = num_packets1 * fraction_loss1 +
26       num_packets2 * fraction_loss2;
27   int total_num_packets = num_packets1 + num_packets2;
28   return (weighted_sum + total_num_packets / 2) / total_num_packets;
29 }
30 
CreateReportBlock(uint32_t remote_ssrc,uint32_t source_ssrc,uint8_t fraction_lost,uint32_t extended_high_sequence_number)31 webrtc::RTCPReportBlock CreateReportBlock(
32     uint32_t remote_ssrc, uint32_t source_ssrc,
33     uint8_t fraction_lost, uint32_t extended_high_sequence_number) {
34   return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
35                                  extended_high_sequence_number, 0, 0, 0);
36 }
37 
38 class TestBitrateObserver: public BitrateObserver {
39  public:
TestBitrateObserver()40   TestBitrateObserver()
41       : last_bitrate_(0),
42         last_fraction_loss_(0),
43         last_rtt_(0) {
44   }
45 
OnNetworkChanged(const uint32_t bitrate,const uint8_t fraction_loss,const uint32_t rtt)46   virtual void OnNetworkChanged(const uint32_t bitrate,
47                                 const uint8_t fraction_loss,
48                                 const uint32_t rtt) {
49     last_bitrate_ = bitrate;
50     last_fraction_loss_ = fraction_loss;
51     last_rtt_ = rtt;
52   }
53   uint32_t last_bitrate_;
54   uint8_t last_fraction_loss_;
55   uint32_t last_rtt_;
56 };
57 
58 class BitrateControllerTest : public ::testing::Test {
59  protected:
BitrateControllerTest()60   BitrateControllerTest() : clock_(0), enforce_min_bitrate_(true) {}
~BitrateControllerTest()61   ~BitrateControllerTest() {}
62 
SetUp()63   virtual void SetUp() {
64     controller_ = BitrateController::CreateBitrateController(
65         &clock_, enforce_min_bitrate_);
66     bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
67   }
68 
TearDown()69   virtual void TearDown() {
70     delete bandwidth_observer_;
71     delete controller_;
72   }
73 
74   webrtc::SimulatedClock clock_;
75   bool enforce_min_bitrate_;
76   BitrateController* controller_;
77   RtcpBandwidthObserver* bandwidth_observer_;
78 };
79 
TEST_F(BitrateControllerTest,Basic)80 TEST_F(BitrateControllerTest, Basic) {
81   TestBitrateObserver bitrate_observer;
82   controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
83   controller_->RemoveBitrateObserver(&bitrate_observer);
84 }
85 
TEST_F(BitrateControllerTest,UpdatingBitrateObserver)86 TEST_F(BitrateControllerTest, UpdatingBitrateObserver) {
87   TestBitrateObserver bitrate_observer;
88   controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 1500000);
89   clock_.AdvanceTimeMilliseconds(25);
90   controller_->Process();
91   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
92 
93   controller_->SetBitrateObserver(&bitrate_observer, 1500000, 100000, 1500000);
94   clock_.AdvanceTimeMilliseconds(25);
95   controller_->Process();
96   EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_);
97 
98   controller_->SetBitrateObserver(&bitrate_observer, 500000, 100000, 1500000);
99   clock_.AdvanceTimeMilliseconds(25);
100   controller_->Process();
101   EXPECT_EQ(1500000u, bitrate_observer.last_bitrate_);
102 }
103 
TEST_F(BitrateControllerTest,OneBitrateObserverOneRtcpObserver)104 TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
105   TestBitrateObserver bitrate_observer;
106   controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
107 
108   // Receive a high remb, test bitrate inc.
109   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
110   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
111   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
112   EXPECT_EQ(0u, bitrate_observer.last_rtt_);
113 
114   // Test bitrate increase 8% per second.
115   webrtc::ReportBlockList report_blocks;
116   report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
117   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 1);
118   EXPECT_EQ(217000u, bitrate_observer.last_bitrate_);
119   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
120   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
121 
122   report_blocks.clear();
123   report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
124   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 1001);
125   EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
126   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
127   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
128 
129   report_blocks.clear();
130   report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
131   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 2001);
132   EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
133 
134   report_blocks.clear();
135   report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
136   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 3001);
137   EXPECT_EQ(276604u, bitrate_observer.last_bitrate_);
138 
139   report_blocks.clear();
140   report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
141   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 4001);
142   EXPECT_EQ(299732u, bitrate_observer.last_bitrate_);
143 
144   // Reach max cap.
145   report_blocks.clear();
146   report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
147   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 5001);
148   EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
149 
150   report_blocks.clear();
151   report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
152   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 7001);
153   EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
154 
155   // Test that a low REMB trigger immediately.
156   bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
157   EXPECT_EQ(250000u, bitrate_observer.last_bitrate_);
158   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
159   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
160 
161   bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
162   EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);  // Min cap.
163   controller_->RemoveBitrateObserver(&bitrate_observer);
164 }
165 
TEST_F(BitrateControllerTest,OneBitrateObserverTwoRtcpObservers)166 TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
167   TestBitrateObserver bitrate_observer;
168   controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
169 
170   RtcpBandwidthObserver* second_bandwidth_observer =
171       controller_->CreateRtcpBandwidthObserver();
172 
173   // Receive a high remb, test bitrate inc.
174   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
175   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
176   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
177   EXPECT_EQ(0u, bitrate_observer.last_rtt_);
178 
179   // Test start bitrate.
180   webrtc::ReportBlockList report_blocks;
181   report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
182   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 1);
183   second_bandwidth_observer->OnReceivedRtcpReceiverReport(
184       report_blocks, 100, 1);
185   EXPECT_EQ(217000u, bitrate_observer.last_bitrate_);
186   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
187   EXPECT_EQ(100u, bitrate_observer.last_rtt_);
188 
189   // Test bitrate increase 8% per second.
190   report_blocks.clear();
191   report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
192   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 501);
193   second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
194                                                           1001);
195   EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
196   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
197   EXPECT_EQ(100u, bitrate_observer.last_rtt_);
198 
199   // Extra report should not change estimate.
200   report_blocks.clear();
201   report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
202   second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
203                                                           1501);
204   EXPECT_EQ(235360u, bitrate_observer.last_bitrate_);
205 
206   report_blocks.clear();
207   report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
208   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 2001);
209   EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
210 
211   // Second report should not change estimate.
212   report_blocks.clear();
213   report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
214   second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
215                                                           2001);
216   EXPECT_EQ(255189u, bitrate_observer.last_bitrate_);
217 
218   // Reports from only one bandwidth observer is ok.
219   report_blocks.clear();
220   report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
221   second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
222                                                           3001);
223   EXPECT_EQ(276604u, bitrate_observer.last_bitrate_);
224 
225   report_blocks.clear();
226   report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
227   second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
228                                                           4001);
229   EXPECT_EQ(299732u, bitrate_observer.last_bitrate_);
230 
231   // Reach max cap.
232   report_blocks.clear();
233   report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
234   second_bandwidth_observer->OnReceivedRtcpReceiverReport(
235       report_blocks, 50, 5001);
236   EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
237 
238   report_blocks.clear();
239   report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
240   second_bandwidth_observer->OnReceivedRtcpReceiverReport(
241       report_blocks, 50, 6001);
242   EXPECT_EQ(300000u, bitrate_observer.last_bitrate_);
243 
244   // Test that a low REMB trigger immediately.
245   // We don't care which bandwidth observer that delivers the REMB.
246   second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
247   EXPECT_EQ(250000u, bitrate_observer.last_bitrate_);
248   EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
249   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
250 
251   // Min cap.
252   bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
253   EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
254   controller_->RemoveBitrateObserver(&bitrate_observer);
255   delete second_bandwidth_observer;
256 }
257 
TEST_F(BitrateControllerTest,OneBitrateObserverMultipleReportBlocks)258 TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
259   TestBitrateObserver bitrate_observer;
260   uint32_t sequence_number[2] = {0, 0xFF00};
261   const uint32_t kStartBitrate = 200000;
262   const uint32_t kMinBitrate = 100000;
263   const uint32_t kMaxBitrate = 300000;
264   controller_->SetBitrateObserver(&bitrate_observer, kStartBitrate, kMinBitrate,
265                                   kMaxBitrate);
266 
267   // Receive a high REMB, test bitrate increase.
268   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
269 
270   webrtc::ReportBlockList report_blocks;
271   int64_t time_ms = 1001;
272   uint32_t last_bitrate = 0;
273   // Ramp up to max bitrate.
274   for (int i = 0; i < 6; ++i) {
275     report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
276     report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
277     bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
278                                                       time_ms);
279     EXPECT_GT(bitrate_observer.last_bitrate_, last_bitrate);
280     EXPECT_EQ(0, bitrate_observer.last_fraction_loss_);
281     EXPECT_EQ(50u, bitrate_observer.last_rtt_);
282     last_bitrate = bitrate_observer.last_bitrate_;
283     time_ms += 1000;
284     sequence_number[0] += 20;
285     sequence_number[1] += 1;
286     report_blocks.clear();
287   }
288 
289   EXPECT_EQ(kMaxBitrate, bitrate_observer.last_bitrate_);
290 
291   // Packet loss on the first stream. Verify that bitrate decreases.
292   report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
293   report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
294   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
295   EXPECT_LT(bitrate_observer.last_bitrate_, last_bitrate);
296   EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer.last_fraction_loss_);
297   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
298   last_bitrate = bitrate_observer.last_bitrate_;
299   sequence_number[0] += 20;
300   sequence_number[1] += 20;
301   time_ms += 1000;
302   report_blocks.clear();
303 
304   // Packet loss on the second stream. Verify that bitrate decreases.
305   report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
306   report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
307   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
308   EXPECT_LT(bitrate_observer.last_bitrate_, last_bitrate);
309   EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer.last_fraction_loss_);
310   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
311   last_bitrate = bitrate_observer.last_bitrate_;
312   sequence_number[0] += 20;
313   sequence_number[1] += 1;
314   time_ms += 1000;
315   report_blocks.clear();
316 
317   // All packets lost on stream with few packets, no back-off.
318   report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
319   report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
320   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
321   EXPECT_EQ(bitrate_observer.last_bitrate_, last_bitrate);
322   EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer.last_fraction_loss_);
323   EXPECT_EQ(50u, bitrate_observer.last_rtt_);
324   last_bitrate = bitrate_observer.last_bitrate_;
325   sequence_number[0] += 20;
326   sequence_number[1] += 1;
327   report_blocks.clear();
328 }
329 
TEST_F(BitrateControllerTest,TwoBitrateObserversOneRtcpObserver)330 TEST_F(BitrateControllerTest, TwoBitrateObserversOneRtcpObserver) {
331   TestBitrateObserver bitrate_observer_1;
332   TestBitrateObserver bitrate_observer_2;
333   controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 300000);
334   controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 300000);
335 
336   // Receive a high remb, test bitrate inc.
337   // Test too low start bitrate, hence lower than sum of min.
338   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
339   EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_);
340   EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
341   EXPECT_EQ(0u, bitrate_observer_1.last_rtt_);
342 
343   // Test bitrate increase 8% per second, distributed equally.
344   webrtc::ReportBlockList report_blocks;
345   report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
346   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 1001);
347   EXPECT_EQ(112500u, bitrate_observer_1.last_bitrate_);
348   EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
349   EXPECT_EQ(50u, bitrate_observer_1.last_rtt_);
350 
351   EXPECT_EQ(212500u, bitrate_observer_2.last_bitrate_);
352   EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
353   EXPECT_EQ(50u, bitrate_observer_2.last_rtt_);
354 
355   report_blocks.clear();
356   report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
357   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 2001);
358   EXPECT_EQ(126000u, bitrate_observer_1.last_bitrate_);
359   EXPECT_EQ(226000u, bitrate_observer_2.last_bitrate_);
360 
361   report_blocks.clear();
362   report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
363   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 3001);
364   EXPECT_EQ(140580u, bitrate_observer_1.last_bitrate_);
365   EXPECT_EQ(240580u, bitrate_observer_2.last_bitrate_);
366 
367   // Check that the bitrate sum honor our REMB.
368   report_blocks.clear();
369   report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
370   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 5001);
371   EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_);
372   EXPECT_EQ(250000u, bitrate_observer_2.last_bitrate_);
373 
374   // Remove REMB cap, higher than sum of max.
375   bandwidth_observer_->OnReceivedEstimatedBitrate(700000);
376 
377   report_blocks.clear();
378   report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
379   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 6001);
380   EXPECT_EQ(166500u, bitrate_observer_1.last_bitrate_);
381   EXPECT_EQ(266500u, bitrate_observer_2.last_bitrate_);
382 
383   report_blocks.clear();
384   report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
385   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 7001);
386   EXPECT_EQ(184320u, bitrate_observer_1.last_bitrate_);
387   EXPECT_EQ(284320u, bitrate_observer_2.last_bitrate_);
388 
389   report_blocks.clear();
390   report_blocks.push_back(CreateReportBlock(1, 2, 0, 161));
391   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 8001);
392   EXPECT_EQ(207130u, bitrate_observer_1.last_bitrate_);
393   EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);  // Max cap.
394 
395   report_blocks.clear();
396   report_blocks.push_back(CreateReportBlock(1, 2, 0, 181));
397   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 9001);
398   EXPECT_EQ(248700u, bitrate_observer_1.last_bitrate_);
399   EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
400 
401   report_blocks.clear();
402   report_blocks.push_back(CreateReportBlock(1, 2, 0, 201));
403   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 10001);
404   EXPECT_EQ(293596u, bitrate_observer_1.last_bitrate_);
405   EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
406 
407   report_blocks.clear();
408   report_blocks.push_back(CreateReportBlock(1, 2, 0, 221));
409   bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 11001);
410   EXPECT_EQ(300000u, bitrate_observer_1.last_bitrate_);  // Max cap.
411   EXPECT_EQ(300000u, bitrate_observer_2.last_bitrate_);
412 
413   // Test that a low REMB trigger immediately.
414   bandwidth_observer_->OnReceivedEstimatedBitrate(350000);
415   EXPECT_EQ(125000u, bitrate_observer_1.last_bitrate_);
416   EXPECT_EQ(0, bitrate_observer_1.last_fraction_loss_);
417   EXPECT_EQ(50u, bitrate_observer_1.last_rtt_);
418   EXPECT_EQ(225000u, bitrate_observer_2.last_bitrate_);
419   EXPECT_EQ(0, bitrate_observer_2.last_fraction_loss_);
420   EXPECT_EQ(50u, bitrate_observer_2.last_rtt_);
421 
422   bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
423   EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_);  // Min cap.
424   EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_);  // Min cap.
425   controller_->RemoveBitrateObserver(&bitrate_observer_1);
426   controller_->RemoveBitrateObserver(&bitrate_observer_2);
427 }
428 
TEST_F(BitrateControllerTest,SetReservedBitrate)429 TEST_F(BitrateControllerTest, SetReservedBitrate) {
430   TestBitrateObserver bitrate_observer;
431   controller_->SetBitrateObserver(&bitrate_observer, 200000, 100000, 300000);
432 
433   // Receive successively lower REMBs, verify the reserved bitrate is deducted.
434 
435   controller_->SetReservedBitrate(0);
436   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
437   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
438   controller_->SetReservedBitrate(50000);
439   bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
440   EXPECT_EQ(150000u, bitrate_observer.last_bitrate_);
441 
442   controller_->SetReservedBitrate(0);
443   bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
444   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
445   controller_->SetReservedBitrate(50000);
446   bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
447   EXPECT_EQ(150000u, bitrate_observer.last_bitrate_);
448 
449   controller_->SetReservedBitrate(0);
450   bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
451   EXPECT_EQ(200000u, bitrate_observer.last_bitrate_);
452   controller_->SetReservedBitrate(30000);
453   bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
454   EXPECT_EQ(170000u, bitrate_observer.last_bitrate_);
455 
456   controller_->SetReservedBitrate(0);
457   bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
458   EXPECT_EQ(160000u, bitrate_observer.last_bitrate_);
459   controller_->SetReservedBitrate(30000);
460   bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
461   EXPECT_EQ(130000u, bitrate_observer.last_bitrate_);
462 
463   controller_->SetReservedBitrate(0);
464   bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
465   EXPECT_EQ(120000u, bitrate_observer.last_bitrate_);
466   controller_->SetReservedBitrate(10000);
467   bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
468   EXPECT_EQ(110000u, bitrate_observer.last_bitrate_);
469 
470   controller_->SetReservedBitrate(0);
471   bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
472   EXPECT_EQ(120000u, bitrate_observer.last_bitrate_);
473   controller_->SetReservedBitrate(50000);
474   bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
475   EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
476 
477   controller_->SetReservedBitrate(10000);
478   bandwidth_observer_->OnReceivedEstimatedBitrate(0);
479   EXPECT_EQ(100000u, bitrate_observer.last_bitrate_);
480 
481   controller_->RemoveBitrateObserver(&bitrate_observer);
482 }
483 
484 class BitrateControllerTestNoEnforceMin : public BitrateControllerTest {
485  protected:
BitrateControllerTestNoEnforceMin()486   BitrateControllerTestNoEnforceMin() : BitrateControllerTest() {
487     enforce_min_bitrate_ = false;
488   }
489 };
490 
491 // The following three tests verify that the EnforceMinBitrate() method works
492 // as intended.
TEST_F(BitrateControllerTestNoEnforceMin,OneBitrateObserver)493 TEST_F(BitrateControllerTestNoEnforceMin, OneBitrateObserver) {
494   TestBitrateObserver bitrate_observer_1;
495   controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 400000);
496 
497   // High REMB.
498   bandwidth_observer_->OnReceivedEstimatedBitrate(150000);
499   EXPECT_EQ(150000u, bitrate_observer_1.last_bitrate_);
500 
501   // Low REMB.
502   bandwidth_observer_->OnReceivedEstimatedBitrate(10000);
503   EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
504 
505   // Keeps at least 10 kbps.
506   bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
507   EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
508 
509   controller_->RemoveBitrateObserver(&bitrate_observer_1);
510 }
511 
TEST_F(BitrateControllerTestNoEnforceMin,SetReservedBitrate)512 TEST_F(BitrateControllerTestNoEnforceMin, SetReservedBitrate) {
513   TestBitrateObserver bitrate_observer_1;
514   controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 400000);
515   controller_->SetReservedBitrate(10000);
516 
517   // High REMB.
518   bandwidth_observer_->OnReceivedEstimatedBitrate(150000);
519   EXPECT_EQ(140000u, bitrate_observer_1.last_bitrate_);
520 
521   // Low REMB.
522   bandwidth_observer_->OnReceivedEstimatedBitrate(15000);
523   EXPECT_EQ(5000u, bitrate_observer_1.last_bitrate_);
524 
525   // Keeps at least 10 kbps.
526   bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
527   EXPECT_EQ(0u, bitrate_observer_1.last_bitrate_);
528 
529   controller_->RemoveBitrateObserver(&bitrate_observer_1);
530 }
531 
TEST_F(BitrateControllerTestNoEnforceMin,ThreeBitrateObservers)532 TEST_F(BitrateControllerTestNoEnforceMin, ThreeBitrateObservers) {
533   TestBitrateObserver bitrate_observer_1;
534   TestBitrateObserver bitrate_observer_2;
535   TestBitrateObserver bitrate_observer_3;
536   // Set up the observers with min bitrates at 100000, 200000, and 300000.
537   // Note: The start bitrate of bitrate_observer_1 (700000) is used as the
538   // overall start bitrate.
539   controller_->SetBitrateObserver(&bitrate_observer_1, 700000, 100000, 400000);
540   controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 400000);
541   controller_->SetBitrateObserver(&bitrate_observer_3, 200000, 300000, 400000);
542 
543   // High REMB. Make sure the controllers get a fair share of the surplus
544   // (i.e., what is left after each controller gets its min rate).
545   bandwidth_observer_->OnReceivedEstimatedBitrate(690000);
546   // Verify that each observer gets its min rate (sum of min rates is 600000),
547   // and that the remaining 90000 is divided equally among the three.
548   EXPECT_EQ(130000u, bitrate_observer_1.last_bitrate_);
549   EXPECT_EQ(230000u, bitrate_observer_2.last_bitrate_);
550   EXPECT_EQ(330000u, bitrate_observer_3.last_bitrate_);
551 
552   // High REMB, but below the sum of min bitrates.
553   bandwidth_observer_->OnReceivedEstimatedBitrate(500000);
554   // Verify that the first and second observers get their min bitrates, and the
555   // third gets the remainder.
556   EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_);  // Min bitrate.
557   EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_);  // Min bitrate.
558   EXPECT_EQ(200000u, bitrate_observer_3.last_bitrate_);  // Remainder.
559 
560   // Low REMB.
561   bandwidth_observer_->OnReceivedEstimatedBitrate(10000);
562   // Verify that the first observer gets all the rate, and the rest get zero.
563   EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
564   EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_);
565   EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_);
566 
567   // Verify it keeps an estimate of at least 10kbps.
568   bandwidth_observer_->OnReceivedEstimatedBitrate(9000);
569   EXPECT_EQ(10000u, bitrate_observer_1.last_bitrate_);
570   EXPECT_EQ(0u, bitrate_observer_2.last_bitrate_);
571   EXPECT_EQ(0u, bitrate_observer_3.last_bitrate_);
572 
573   controller_->RemoveBitrateObserver(&bitrate_observer_1);
574   controller_->RemoveBitrateObserver(&bitrate_observer_2);
575   controller_->RemoveBitrateObserver(&bitrate_observer_3);
576 }
577 
TEST_F(BitrateControllerTest,ThreeBitrateObserversLowRembEnforceMin)578 TEST_F(BitrateControllerTest, ThreeBitrateObserversLowRembEnforceMin) {
579   TestBitrateObserver bitrate_observer_1;
580   TestBitrateObserver bitrate_observer_2;
581   TestBitrateObserver bitrate_observer_3;
582   controller_->SetBitrateObserver(&bitrate_observer_1, 200000, 100000, 300000);
583   controller_->SetBitrateObserver(&bitrate_observer_2, 200000, 200000, 300000);
584   controller_->SetBitrateObserver(&bitrate_observer_3, 200000, 300000, 300000);
585 
586   // Low REMB. Verify that all observers still get their respective min bitrate.
587   bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
588   EXPECT_EQ(100000u, bitrate_observer_1.last_bitrate_);  // Min cap.
589   EXPECT_EQ(200000u, bitrate_observer_2.last_bitrate_);  // Min cap.
590   EXPECT_EQ(300000u, bitrate_observer_3.last_bitrate_);  // Min cap.
591 
592   controller_->RemoveBitrateObserver(&bitrate_observer_1);
593   controller_->RemoveBitrateObserver(&bitrate_observer_2);
594   controller_->RemoveBitrateObserver(&bitrate_observer_3);
595 }
596