blob: 4f92a3884b5f44deedec188fbc08c63b59fd940e [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
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
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000011#include <algorithm>
12#include <vector>
13
jbauchf91e6d02016-01-24 23:05:21 -080014#include "testing/gtest/include/gtest/gtest.h"
15
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000016#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Per28a44562016-05-04 17:12:51 +020017#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010018#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000019
Per28a44562016-05-04 17:12:51 +020020using ::testing::Exactly;
21using ::testing::Return;
22
perkj825eb582016-05-04 01:08:05 -070023using webrtc::BitrateController;
Per28a44562016-05-04 17:12:51 +020024using webrtc::BitrateObserver;
25using webrtc::PacedSender;
26using webrtc::RtcpBandwidthObserver;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000027
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000028uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1,
29 int num_packets2, uint8_t fraction_loss2) {
30 int weighted_sum = num_packets1 * fraction_loss1 +
31 num_packets2 * fraction_loss2;
32 int total_num_packets = num_packets1 + num_packets2;
33 return (weighted_sum + total_num_packets / 2) / total_num_packets;
34}
35
36webrtc::RTCPReportBlock CreateReportBlock(
37 uint32_t remote_ssrc, uint32_t source_ssrc,
38 uint8_t fraction_lost, uint32_t extended_high_sequence_number) {
39 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
40 extended_high_sequence_number, 0, 0, 0);
41}
42
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000043class TestBitrateObserver: public BitrateObserver {
44 public:
45 TestBitrateObserver()
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000046 : last_bitrate_(0),
47 last_fraction_loss_(0),
48 last_rtt_(0) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000049 }
50
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000051 virtual void OnNetworkChanged(uint32_t bitrate,
52 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000053 int64_t rtt) {
Stefan Holmere5904162015-03-26 11:11:06 +010054 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000055 last_fraction_loss_ = fraction_loss;
56 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000057 }
Stefan Holmere5904162015-03-26 11:11:06 +010058 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000059 uint8_t last_fraction_loss_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000060 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000061};
62
63class BitrateControllerTest : public ::testing::Test {
64 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000065 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000066 ~BitrateControllerTest() {}
67
68 virtual void SetUp() {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000069 controller_ =
70 BitrateController::CreateBitrateController(&clock_, &bitrate_observer_);
Stefan Holmere5904162015-03-26 11:11:06 +010071 controller_->SetStartBitrate(kStartBitrateBps);
72 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
73 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
74 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000075 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
76 }
77
78 virtual void TearDown() {
79 delete bandwidth_observer_;
80 delete controller_;
81 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000082
Stefan Holmere5904162015-03-26 11:11:06 +010083 const int kMinBitrateBps = 100000;
84 const int kStartBitrateBps = 200000;
85 const int kMaxBitrateBps = 300000;
86
87 const int kDefaultMinBitrateBps = 10000;
88 const int kDefaultMaxBitrateBps = 1000000000;
89
andresp@webrtc.org44caf012014-03-26 21:00:21 +000090 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000091 TestBitrateObserver bitrate_observer_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000092 BitrateController* controller_;
93 RtcpBandwidthObserver* bandwidth_observer_;
94};
95
Stefan Holmere5904162015-03-26 11:11:06 +010096TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
97 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
98 controller_->SetMinMaxBitrate(0, 0);
99 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
100 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
101 EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_);
102 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
103 clock_.AdvanceTimeMilliseconds(1000);
104 controller_->Process();
105 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
106}
107
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000108TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000109 // First REMB applies immediately.
110 int64_t time_ms = 1001;
111 webrtc::ReportBlockList report_blocks;
112 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
113 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100114 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000115 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
116 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000117 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
118 report_blocks.clear();
119 time_ms += 2000;
120
121 // Receive a high remb, test bitrate inc.
122 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000123
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000124 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000125 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
126 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100127 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000128 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
129 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000131
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000132 report_blocks.clear();
133 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000134 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100135 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000136 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
137 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000138 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000139
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000140 report_blocks.clear();
141 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000142 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100143 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000144 time_ms += 1000;
145
146 report_blocks.clear();
147 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
148 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100149 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000150 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000151
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000152 report_blocks.clear();
153 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000154 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100155 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000156 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000157
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000158 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000159 report_blocks.clear();
160 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000161 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100162 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000163 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000164
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000165 report_blocks.clear();
166 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000167 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100168 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000169
stefan32f81542016-01-20 07:13:58 -0800170 // Test that a low delay-based estimate limits the combined estimate.
171 controller_->UpdateDelayBasedEstimate(280000);
172 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
173
174 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000175 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100176 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000177 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
178 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000179
180 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800181 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000182}
183
184TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000185 // REMBs during the first 2 seconds apply immediately.
186 int64_t time_ms = 1;
187 webrtc::ReportBlockList report_blocks;
188 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
189 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
190 report_blocks.clear();
191 time_ms += 500;
192
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000193 RtcpBandwidthObserver* second_bandwidth_observer =
194 controller_->CreateRtcpBandwidthObserver();
195
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000196 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000197 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000198 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
199 report_blocks, 100, 1);
Stefan Holmere5904162015-03-26 11:11:06 +0100200 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000201 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
202 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000203 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000204
205 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000206 report_blocks.clear();
207 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000208 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
209 time_ms += 500;
210 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
211 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100212 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000213 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
214 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000215 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000216
217 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000218 report_blocks.clear();
219 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000220 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
221 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100222 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000223 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000224
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000225 report_blocks.clear();
226 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100228 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000229
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000230 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000231 report_blocks.clear();
232 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000233 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
234 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100235 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000236 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000237
238 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000239 report_blocks.clear();
240 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000241 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
242 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100243 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000244 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000245
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000246 report_blocks.clear();
247 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000248 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
249 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100250 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000251 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000252
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000253 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000254 report_blocks.clear();
255 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000256 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000257 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100258 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000259 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000260
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000261 report_blocks.clear();
262 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000263 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000264 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100265 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000266
267 // Test that a low REMB trigger immediately.
268 // We don't care which bandwidth observer that delivers the REMB.
269 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100270 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000271 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
272 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000273
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000274 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000275 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100276 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277 delete second_bandwidth_observer;
278}
279
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000280TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000281 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100282 const int kStartBitrate = 200000;
283 const int kMinBitrate = 100000;
284 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000285 controller_->SetStartBitrate(kStartBitrate);
286 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000287
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000288 // REMBs during the first 2 seconds apply immediately.
289 int64_t time_ms = 1001;
290 webrtc::ReportBlockList report_blocks;
291 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
292 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
293 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
294 report_blocks.clear();
295 time_ms += 2000;
296
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000297 // Receive a high REMB, test bitrate increase.
298 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
299
Stefan Holmere5904162015-03-26 11:11:06 +0100300 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000301 // Ramp up to max bitrate.
302 for (int i = 0; i < 6; ++i) {
303 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
304 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
305 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
306 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000307 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
308 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
309 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
310 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000311 time_ms += 1000;
312 sequence_number[0] += 20;
313 sequence_number[1] += 1;
314 report_blocks.clear();
315 }
316
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000317 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000318
319 // Packet loss on the first stream. Verify that bitrate decreases.
320 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
321 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
322 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000323 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
324 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
325 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
326 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000327 sequence_number[0] += 20;
328 sequence_number[1] += 20;
329 time_ms += 1000;
330 report_blocks.clear();
331
332 // Packet loss on the second stream. Verify that bitrate decreases.
333 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
334 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
335 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000336 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
337 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
338 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
339 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000340 sequence_number[0] += 20;
341 sequence_number[1] += 1;
342 time_ms += 1000;
343 report_blocks.clear();
344
345 // All packets lost on stream with few packets, no back-off.
346 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
347 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
348 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000349 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
350 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
351 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
352 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000353 sequence_number[0] += 20;
354 sequence_number[1] += 1;
355 report_blocks.clear();
356}
357
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000358TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000359 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000360 controller_->SetReservedBitrate(0);
361 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100362 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000363 controller_->SetReservedBitrate(50000);
364 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100365 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000366
367 controller_->SetReservedBitrate(0);
368 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100369 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000370 controller_->SetReservedBitrate(50000);
371 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100372 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000373
374 controller_->SetReservedBitrate(0);
375 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100376 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000377 controller_->SetReservedBitrate(30000);
378 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100379 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000380
381 controller_->SetReservedBitrate(0);
382 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100383 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000384 controller_->SetReservedBitrate(30000);
385 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100386 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000387
388 controller_->SetReservedBitrate(0);
389 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100390 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000391 controller_->SetReservedBitrate(10000);
392 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100393 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000394
395 controller_->SetReservedBitrate(0);
396 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100397 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000398 controller_->SetReservedBitrate(50000);
399 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000400 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100401 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000402
403 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000404 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100405 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000406}