blob: f2a14ea9cfacbd3b404c95c4ccdd7d6a6c9f8740 [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
ivoc14d5dbe2016-07-04 07:06:55 -070016#include "webrtc/call/mock/mock_rtc_event_log.h"
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000017#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkjec81bcd2016-05-11 06:01:13 -070018#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010019#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000020
perkjec81bcd2016-05-11 06:01:13 -070021using ::testing::Exactly;
22using ::testing::Return;
23
perkje30c2722016-05-09 04:57:11 -070024using webrtc::BitrateController;
perkjec81bcd2016-05-11 06:01:13 -070025using webrtc::BitrateObserver;
26using webrtc::PacedSender;
27using webrtc::RtcpBandwidthObserver;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000028
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000029uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1,
30 int num_packets2, uint8_t fraction_loss2) {
31 int weighted_sum = num_packets1 * fraction_loss1 +
32 num_packets2 * fraction_loss2;
33 int total_num_packets = num_packets1 + num_packets2;
34 return (weighted_sum + total_num_packets / 2) / total_num_packets;
35}
36
37webrtc::RTCPReportBlock CreateReportBlock(
38 uint32_t remote_ssrc, uint32_t source_ssrc,
39 uint8_t fraction_lost, uint32_t extended_high_sequence_number) {
40 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
41 extended_high_sequence_number, 0, 0, 0);
42}
43
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000044class TestBitrateObserver: public BitrateObserver {
45 public:
46 TestBitrateObserver()
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000047 : last_bitrate_(0),
48 last_fraction_loss_(0),
49 last_rtt_(0) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000050 }
51
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000052 virtual void OnNetworkChanged(uint32_t bitrate,
53 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000054 int64_t rtt) {
Stefan Holmere5904162015-03-26 11:11:06 +010055 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000056 last_fraction_loss_ = fraction_loss;
57 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000058 }
Stefan Holmere5904162015-03-26 11:11:06 +010059 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000060 uint8_t last_fraction_loss_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000061 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000062};
63
64class BitrateControllerTest : public ::testing::Test {
65 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000066 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000067 ~BitrateControllerTest() {}
68
69 virtual void SetUp() {
ivoc14d5dbe2016-07-04 07:06:55 -070070 controller_ = BitrateController::CreateBitrateController(
71 &clock_, &bitrate_observer_, &event_log_);
Stefan Holmere5904162015-03-26 11:11:06 +010072 controller_->SetStartBitrate(kStartBitrateBps);
73 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
74 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
75 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000076 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver();
77 }
78
79 virtual void TearDown() {
80 delete bandwidth_observer_;
81 delete controller_;
82 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000083
Stefan Holmere5904162015-03-26 11:11:06 +010084 const int kMinBitrateBps = 100000;
85 const int kStartBitrateBps = 200000;
86 const int kMaxBitrateBps = 300000;
87
88 const int kDefaultMinBitrateBps = 10000;
89 const int kDefaultMaxBitrateBps = 1000000000;
90
andresp@webrtc.org44caf012014-03-26 21:00:21 +000091 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000092 TestBitrateObserver bitrate_observer_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000093 BitrateController* controller_;
94 RtcpBandwidthObserver* bandwidth_observer_;
ivoc14d5dbe2016-07-04 07:06:55 -070095 webrtc::MockRtcEventLog event_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000096};
97
Stefan Holmere5904162015-03-26 11:11:06 +010098TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
99 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
100 controller_->SetMinMaxBitrate(0, 0);
101 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
102 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
103 EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_);
104 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
105 clock_.AdvanceTimeMilliseconds(1000);
106 controller_->Process();
107 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
108}
109
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000110TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000111 // First REMB applies immediately.
ivoc14d5dbe2016-07-04 07:06:55 -0700112 EXPECT_CALL(event_log_, LogBwePacketLossEvent(testing::Gt(0), 0, 0)).Times(8);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000113 int64_t time_ms = 1001;
114 webrtc::ReportBlockList report_blocks;
115 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
116 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100117 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000118 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
119 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000120 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
121 report_blocks.clear();
122 time_ms += 2000;
123
124 // Receive a high remb, test bitrate inc.
125 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000126
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000127 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000128 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
129 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100130 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000131 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
132 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000133 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000134
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000135 report_blocks.clear();
136 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000137 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100138 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000139 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
140 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000141 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000142
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000143 report_blocks.clear();
144 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000145 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100146 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000147 time_ms += 1000;
148
149 report_blocks.clear();
150 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
151 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100152 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000153 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000154
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000155 report_blocks.clear();
156 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000157 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100158 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000159 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000160
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000161 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000162 report_blocks.clear();
163 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000164 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100165 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000166 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000167
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000168 report_blocks.clear();
169 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000170 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100171 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000172
stefan32f81542016-01-20 07:13:58 -0800173 // Test that a low delay-based estimate limits the combined estimate.
174 controller_->UpdateDelayBasedEstimate(280000);
175 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
176
177 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000178 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100179 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000180 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
181 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000182
183 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800184 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000185}
186
187TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000188 // REMBs during the first 2 seconds apply immediately.
ivoc14d5dbe2016-07-04 07:06:55 -0700189 EXPECT_CALL(event_log_, LogBwePacketLossEvent(testing::Gt(0), 0, 0)).Times(9);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000190 int64_t time_ms = 1;
191 webrtc::ReportBlockList report_blocks;
192 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
193 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
194 report_blocks.clear();
195 time_ms += 500;
196
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000197 RtcpBandwidthObserver* second_bandwidth_observer =
198 controller_->CreateRtcpBandwidthObserver();
199
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000200 // Test start bitrate.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000201 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000202 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
203 report_blocks, 100, 1);
Stefan Holmere5904162015-03-26 11:11:06 +0100204 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000205 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
206 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000207 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000208
209 // Test bitrate increase 8% per second.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000210 report_blocks.clear();
211 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000212 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
213 time_ms += 500;
214 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
215 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100216 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000217 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
218 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000219 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000220
221 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000222 report_blocks.clear();
223 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000224 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
225 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100226 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000228
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000229 report_blocks.clear();
230 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000231 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100232 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000233
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000234 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000235 report_blocks.clear();
236 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000237 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
238 report_blocks, 100, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100239 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000240 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000241
242 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000243 report_blocks.clear();
244 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000245 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
246 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100247 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000248 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000249
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000250 report_blocks.clear();
251 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000252 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
253 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100254 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000255 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000256
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000257 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000258 report_blocks.clear();
259 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000260 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000261 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100262 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000263 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000264
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000265 report_blocks.clear();
266 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000267 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000268 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100269 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000270
271 // Test that a low REMB trigger immediately.
272 // We don't care which bandwidth observer that delivers the REMB.
273 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100274 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000275 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
276 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000278 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000279 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100280 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000281 delete second_bandwidth_observer;
282}
283
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000284TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
ivoc14d5dbe2016-07-04 07:06:55 -0700285 testing::Expectation first_calls =
286 EXPECT_CALL(event_log_, LogBwePacketLossEvent(testing::Gt(0), 0, 0))
287 .Times(7);
288 EXPECT_CALL(event_log_,
289 LogBwePacketLossEvent(testing::Gt(0), testing::Gt(0), 0))
290 .Times(2)
291 .After(first_calls);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000292 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100293 const int kStartBitrate = 200000;
294 const int kMinBitrate = 100000;
295 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000296 controller_->SetStartBitrate(kStartBitrate);
297 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000298
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000299 // REMBs during the first 2 seconds apply immediately.
300 int64_t time_ms = 1001;
301 webrtc::ReportBlockList report_blocks;
302 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
303 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
304 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
305 report_blocks.clear();
306 time_ms += 2000;
307
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000308 // Receive a high REMB, test bitrate increase.
309 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
310
Stefan Holmere5904162015-03-26 11:11:06 +0100311 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000312 // Ramp up to max bitrate.
313 for (int i = 0; i < 6; ++i) {
314 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
315 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
316 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
317 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000318 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
319 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
320 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
321 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000322 time_ms += 1000;
323 sequence_number[0] += 20;
324 sequence_number[1] += 1;
325 report_blocks.clear();
326 }
327
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000328 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000329
330 // Packet loss on the first stream. Verify that bitrate decreases.
331 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
332 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
333 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000334 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
335 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
336 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
337 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000338 sequence_number[0] += 20;
339 sequence_number[1] += 20;
340 time_ms += 1000;
341 report_blocks.clear();
342
343 // Packet loss on the second stream. Verify that bitrate decreases.
344 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
345 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
346 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000347 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
348 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
349 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
350 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000351 sequence_number[0] += 20;
352 sequence_number[1] += 1;
353 time_ms += 1000;
354 report_blocks.clear();
355
356 // All packets lost on stream with few packets, no back-off.
357 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
358 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
359 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000360 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
361 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
362 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
363 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000364 sequence_number[0] += 20;
365 sequence_number[1] += 1;
366 report_blocks.clear();
367}
368
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000369TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000370 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000371 controller_->SetReservedBitrate(0);
372 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100373 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000374 controller_->SetReservedBitrate(50000);
375 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100376 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000377
378 controller_->SetReservedBitrate(0);
379 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100380 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000381 controller_->SetReservedBitrate(50000);
382 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100383 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000384
385 controller_->SetReservedBitrate(0);
386 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100387 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000388 controller_->SetReservedBitrate(30000);
389 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100390 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000391
392 controller_->SetReservedBitrate(0);
393 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100394 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000395 controller_->SetReservedBitrate(30000);
396 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100397 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000398
399 controller_->SetReservedBitrate(0);
400 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100401 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000402 controller_->SetReservedBitrate(10000);
403 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100404 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000405
406 controller_->SetReservedBitrate(0);
407 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100408 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000409 controller_->SetReservedBitrate(50000);
410 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000411 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100412 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000413
414 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000415 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100416 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000417}