blob: 6f6c57d4f2a3888f1a96e7778e0baede5b5beebd [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
terelius2d81eb32016-10-25 07:04:37 -070014#include "webrtc/logging/rtc_event_log/mock/mock_rtc_event_log.h"
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000015#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkjec81bcd2016-05-11 06:01:13 -070016#include "webrtc/modules/pacing/mock/mock_paced_sender.h"
michaeltf082c2a2016-11-07 04:17:14 -080017#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
Stefan Holmer52200d02016-09-20 14:14:23 +020018#include "webrtc/test/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/test/gtest.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() {
Stefan Holmer52200d02016-09-20 14:14:23 +020070 controller_.reset(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_);
Stefan Holmer52200d02016-09-20 14:14:23 +020076 bandwidth_observer_.reset(controller_->CreateRtcpBandwidthObserver());
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000077 }
78
79 virtual void TearDown() {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000080 }
andresp@webrtc.org44caf012014-03-26 21:00:21 +000081
Stefan Holmere5904162015-03-26 11:11:06 +010082 const int kMinBitrateBps = 100000;
83 const int kStartBitrateBps = 200000;
84 const int kMaxBitrateBps = 300000;
85
86 const int kDefaultMinBitrateBps = 10000;
87 const int kDefaultMaxBitrateBps = 1000000000;
88
andresp@webrtc.org44caf012014-03-26 21:00:21 +000089 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000090 TestBitrateObserver bitrate_observer_;
Stefan Holmer52200d02016-09-20 14:14:23 +020091 std::unique_ptr<BitrateController> controller_;
92 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020093 testing::NiceMock<webrtc::MockRtcEventLog> event_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000094};
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);
michaeltf082c2a2016-11-07 04:17:14 -0800101 EXPECT_EQ(webrtc::congestion_controller::GetMinBitrateBps(),
102 bitrate_observer_.last_bitrate_);
Stefan Holmere5904162015-03-26 11:11:06 +0100103 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
104 clock_.AdvanceTimeMilliseconds(1000);
105 controller_->Process();
106 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
107}
108
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000109TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000110 // First REMB applies immediately.
111 int64_t time_ms = 1001;
112 webrtc::ReportBlockList report_blocks;
113 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
114 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100115 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000116 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
117 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000118 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
119 report_blocks.clear();
120 time_ms += 2000;
121
122 // Receive a high remb, test bitrate inc.
123 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000124
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000125 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000126 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
127 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100128 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
130 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000131 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000132
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000133 report_blocks.clear();
134 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100136 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000137 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
138 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000139 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000140
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000141 report_blocks.clear();
142 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000143 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100144 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000145 time_ms += 1000;
146
147 report_blocks.clear();
148 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
149 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100150 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000151 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000152
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000153 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700154 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100156 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000157 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000158
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000159 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000160 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700161 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000162 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100163 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000164 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000165
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000166 report_blocks.clear();
167 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000168 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100169 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000170
stefan32f81542016-01-20 07:13:58 -0800171 // Test that a low delay-based estimate limits the combined estimate.
Stefan Holmer280de9e2016-09-30 10:06:51 +0200172 webrtc::DelayBasedBwe::Result result(false, 280000);
173 controller_->OnDelayBasedBweResult(result);
stefan32f81542016-01-20 07:13:58 -0800174 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
175
176 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100178 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000179 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
180 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000181
182 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800183 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000184}
185
186TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
Stefan Holmer52200d02016-09-20 14:14:23 +0200187 const uint32_t kSsrc1 = 1;
188 const uint32_t kSsrc2 = 2;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000189 // REMBs during the first 2 seconds apply immediately.
190 int64_t time_ms = 1;
191 webrtc::ReportBlockList report_blocks;
Stefan Holmer52200d02016-09-20 14:14:23 +0200192 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 1));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000193 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 Holmer52200d02016-09-20 14:14:23 +0200201 report_blocks.push_back(CreateReportBlock(2, 2, 0, 21));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000202 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
203 report_blocks, 100, 1);
Stefan Holmer52200d02016-09-20 14:14:23 +0200204 EXPECT_EQ(200000, 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();
Stefan Holmer52200d02016-09-20 14:14:23 +0200211 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 21));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000212 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
213 time_ms += 500;
Stefan Holmer52200d02016-09-20 14:14:23 +0200214 report_blocks.front().remoteSSRC = kSsrc2;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000215 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
216 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200217 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000218 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
219 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000220 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221
222 // Extra report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000223 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200224 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 31));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000225 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
226 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200227 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000228 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000229
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000230 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200231 report_blocks.push_back(CreateReportBlock(kSsrc1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000232 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200233 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000234
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000235 // Second report should not change estimate.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000236 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200237 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000238 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
239 report_blocks, 100, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200240 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000241 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000242
243 // Reports from only one bandwidth observer is ok.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000244 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200245 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 61));
246 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
247 report_blocks, 50, time_ms);
248 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
249 time_ms += 1000;
250
251 report_blocks.clear();
252 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 81));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000253 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
254 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100255 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000256 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000257
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000258 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200259 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000260 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
261 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100262 EXPECT_EQ(299732, 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
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000265 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000266 report_blocks.clear();
Stefan Holmer52200d02016-09-20 14:14:23 +0200267 report_blocks.push_back(CreateReportBlock(kSsrc2, 2, 0, 141));
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000268 second_bandwidth_observer->OnReceivedRtcpReceiverReport(
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000269 report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100270 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000271
272 // Test that a low REMB trigger immediately.
273 // We don't care which bandwidth observer that delivers the REMB.
274 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100275 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000276 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
277 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000278
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000279 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000280 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100281 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000282 delete second_bandwidth_observer;
283}
284
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000285TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000286 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100287 const int kStartBitrate = 200000;
288 const int kMinBitrate = 100000;
289 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000290 controller_->SetStartBitrate(kStartBitrate);
291 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000292
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000293 // REMBs during the first 2 seconds apply immediately.
294 int64_t time_ms = 1001;
295 webrtc::ReportBlockList report_blocks;
296 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
297 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
298 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
299 report_blocks.clear();
300 time_ms += 2000;
301
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000302 // Receive a high REMB, test bitrate increase.
303 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
304
Stefan Holmere5904162015-03-26 11:11:06 +0100305 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000306 // Ramp up to max bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200307 for (int i = 0; i < 7; ++i) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000308 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
309 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
310 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
311 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000312 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
313 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
314 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
315 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000316 time_ms += 1000;
317 sequence_number[0] += 20;
318 sequence_number[1] += 1;
319 report_blocks.clear();
320 }
321
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000322 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000323
324 // Packet loss on the first stream. Verify that bitrate decreases.
325 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
326 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
327 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000328 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
329 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
330 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
331 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000332 sequence_number[0] += 20;
333 sequence_number[1] += 20;
334 time_ms += 1000;
335 report_blocks.clear();
336
337 // Packet loss on the second stream. Verify that bitrate decreases.
338 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
339 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
340 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000341 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
342 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
343 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
344 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000345 sequence_number[0] += 20;
346 sequence_number[1] += 1;
347 time_ms += 1000;
348 report_blocks.clear();
349
350 // All packets lost on stream with few packets, no back-off.
351 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0]));
352 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
353 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000354 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
355 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_);
356 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
357 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000358 sequence_number[0] += 20;
359 sequence_number[1] += 1;
360 report_blocks.clear();
361}
362
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000363TEST_F(BitrateControllerTest, SetReservedBitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000364 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000365 controller_->SetReservedBitrate(0);
366 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100367 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000368 controller_->SetReservedBitrate(50000);
369 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
Stefan Holmere5904162015-03-26 11:11:06 +0100370 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000371
372 controller_->SetReservedBitrate(0);
373 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100374 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000375 controller_->SetReservedBitrate(50000);
376 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100377 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000378
379 controller_->SetReservedBitrate(0);
380 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100381 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000382 controller_->SetReservedBitrate(30000);
383 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100384 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000385
386 controller_->SetReservedBitrate(0);
387 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100388 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000389 controller_->SetReservedBitrate(30000);
390 bandwidth_observer_->OnReceivedEstimatedBitrate(160000);
Stefan Holmere5904162015-03-26 11:11:06 +0100391 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000392
393 controller_->SetReservedBitrate(0);
394 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100395 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000396 controller_->SetReservedBitrate(10000);
397 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100398 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000399
400 controller_->SetReservedBitrate(0);
401 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
Stefan Holmere5904162015-03-26 11:11:06 +0100402 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000403 controller_->SetReservedBitrate(50000);
404 bandwidth_observer_->OnReceivedEstimatedBitrate(120000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000405 // Limited by min bitrate.
Stefan Holmere5904162015-03-26 11:11:06 +0100406 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000407
408 controller_->SetReservedBitrate(10000);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000409 bandwidth_observer_->OnReceivedEstimatedBitrate(1);
Stefan Holmere5904162015-03-26 11:11:06 +0100410 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000411}
Stefan Holmer52200d02016-09-20 14:14:23 +0200412
413TEST_F(BitrateControllerTest, TimeoutsWithoutFeedback) {
414 {
415 webrtc::test::ScopedFieldTrials override_field_trials(
Stefan Holmerdb158f92016-10-13 02:11:51 +0200416 "WebRTC-FeedbackTimeout/Enabled/");
Stefan Holmer52200d02016-09-20 14:14:23 +0200417 SetUp();
418 int expected_bitrate_bps = 300000;
419 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
420 kDefaultMaxBitrateBps);
421
422 webrtc::ReportBlockList report_blocks;
423 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
424 bandwidth_observer_->OnReceivedRtcpReceiverReport(
425 report_blocks, 50, clock_.TimeInMilliseconds());
426 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
427 clock_.AdvanceTimeMilliseconds(500);
428
429 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
430 bandwidth_observer_->OnReceivedRtcpReceiverReport(
431 report_blocks, 50, clock_.TimeInMilliseconds());
432 report_blocks.clear();
433 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
434 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
435 clock_.AdvanceTimeMilliseconds(1500);
436
437 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
438 bandwidth_observer_->OnReceivedRtcpReceiverReport(
439 report_blocks, 50, clock_.TimeInMilliseconds());
440 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
441 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
442 clock_.AdvanceTimeMilliseconds(1000);
443
444 // 1 seconds since feedback, expect increase.
445 controller_->Process();
446 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
447 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
448 clock_.AdvanceTimeMilliseconds(800);
449
450 // 1.8 seconds since feedback, expect no increase.
451 controller_->Process();
452 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
453 clock_.AdvanceTimeMilliseconds(3701);
454
455 // More than 4.5 seconds since feedback, expect decrease.
456 controller_->Process();
457 expected_bitrate_bps *= 0.8;
458 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
459 clock_.AdvanceTimeMilliseconds(500);
460
461 // Only one timeout every second.
462 controller_->Process();
463 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
464 clock_.AdvanceTimeMilliseconds(501);
465
466 // New timeout allowed.
467 controller_->Process();
468 expected_bitrate_bps *= 0.8;
469 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
470 }
471}
472
473TEST_F(BitrateControllerTest, StopIncreaseWithoutPacketReports) {
474 int expected_bitrate_bps = 300000;
475 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
476 kDefaultMaxBitrateBps);
477
478 webrtc::ReportBlockList report_blocks;
479 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
480 bandwidth_observer_->OnReceivedRtcpReceiverReport(
481 report_blocks, 50, clock_.TimeInMilliseconds());
482 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
483 clock_.AdvanceTimeMilliseconds(500);
484
485 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
486 bandwidth_observer_->OnReceivedRtcpReceiverReport(
487 report_blocks, 50, clock_.TimeInMilliseconds());
488 report_blocks.clear();
489 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
490 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
491 clock_.AdvanceTimeMilliseconds(1500);
492
493 // 1.2 seconds without packets reported as received, no increase.
494 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
495 bandwidth_observer_->OnReceivedRtcpReceiverReport(
496 report_blocks, 50, clock_.TimeInMilliseconds());
497 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
498 clock_.AdvanceTimeMilliseconds(1000);
499
500 // 5 packets reported as received since last, too few, no increase.
501 report_blocks.push_back(CreateReportBlock(1, 2, 0, 26));
502 bandwidth_observer_->OnReceivedRtcpReceiverReport(
503 report_blocks, 50, clock_.TimeInMilliseconds());
504 report_blocks.clear();
505 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
506 clock_.AdvanceTimeMilliseconds(100);
507
508 // 15 packets reported as received since last, enough to increase.
509 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
510 bandwidth_observer_->OnReceivedRtcpReceiverReport(
511 report_blocks, 50, clock_.TimeInMilliseconds());
512 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
513 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
514 clock_.AdvanceTimeMilliseconds(1000);
515}