blob: cdb9f3077129887533b5b85f177bf36799bc05ef [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stdint.h>
12#include <memory>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
15#include "modules/bitrate_controller/include/bitrate_controller.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "modules/congestion_controller/goog_cc/delay_based_bwe.h"
17#include "modules/pacing/paced_sender.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
20#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/field_trial.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "test/gmock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/gtest.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000024
perkjec81bcd2016-05-11 06:01:13 -070025using ::testing::Exactly;
26using ::testing::Return;
27
perkje30c2722016-05-09 04:57:11 -070028using webrtc::BitrateController;
perkjec81bcd2016-05-11 06:01:13 -070029using webrtc::BitrateObserver;
30using webrtc::PacedSender;
31using webrtc::RtcpBandwidthObserver;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000032
Yves Gerey665174f2018-06-19 15:03:05 +020033uint8_t WeightedLoss(int num_packets1,
34 uint8_t fraction_loss1,
35 int num_packets2,
36 uint8_t fraction_loss2) {
37 int weighted_sum =
38 num_packets1 * fraction_loss1 + num_packets2 * fraction_loss2;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000039 int total_num_packets = num_packets1 + num_packets2;
40 return (weighted_sum + total_num_packets / 2) / total_num_packets;
41}
42
43webrtc::RTCPReportBlock CreateReportBlock(
Yves Gerey665174f2018-06-19 15:03:05 +020044 uint32_t remote_ssrc,
45 uint32_t source_ssrc,
46 uint8_t fraction_lost,
47 uint32_t extended_high_sequence_number) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000048 return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0,
49 extended_high_sequence_number, 0, 0, 0);
50}
51
Yves Gerey665174f2018-06-19 15:03:05 +020052class TestBitrateObserver : public BitrateObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000053 public:
54 TestBitrateObserver()
Yves Gerey665174f2018-06-19 15:03:05 +020055 : last_bitrate_(0), last_fraction_loss_(0), last_rtt_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000056
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000057 virtual void OnNetworkChanged(uint32_t bitrate,
58 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000059 int64_t rtt) {
Stefan Holmere5904162015-03-26 11:11:06 +010060 last_bitrate_ = static_cast<int>(bitrate);
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000061 last_fraction_loss_ = fraction_loss;
62 last_rtt_ = rtt;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000063 }
Stefan Holmere5904162015-03-26 11:11:06 +010064 int last_bitrate_;
kjellander@webrtc.org2e84c112012-05-31 13:55:01 +000065 uint8_t last_fraction_loss_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +000066 int64_t last_rtt_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000067};
68
69class BitrateControllerTest : public ::testing::Test {
70 protected:
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000071 BitrateControllerTest() : clock_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000072 ~BitrateControllerTest() {}
73
74 virtual void SetUp() {
Stefan Holmer52200d02016-09-20 14:14:23 +020075 controller_.reset(BitrateController::CreateBitrateController(
76 &clock_, &bitrate_observer_, &event_log_));
Stefan Holmere5904162015-03-26 11:11:06 +010077 controller_->SetStartBitrate(kStartBitrateBps);
78 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
79 controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps);
80 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +010081 bandwidth_observer_ = controller_.get();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000082 }
83
Yves Gerey665174f2018-06-19 15:03:05 +020084 virtual void TearDown() {}
andresp@webrtc.org44caf012014-03-26 21:00:21 +000085
Stefan Holmere5904162015-03-26 11:11:06 +010086 const int kMinBitrateBps = 100000;
87 const int kStartBitrateBps = 200000;
88 const int kMaxBitrateBps = 300000;
89
90 const int kDefaultMinBitrateBps = 10000;
91 const int kDefaultMaxBitrateBps = 1000000000;
92
andresp@webrtc.org44caf012014-03-26 21:00:21 +000093 webrtc::SimulatedClock clock_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000094 TestBitrateObserver bitrate_observer_;
Stefan Holmer52200d02016-09-20 14:14:23 +020095 std::unique_ptr<BitrateController> controller_;
Sebastian Jansson8d9c5402017-11-15 17:22:16 +010096 RtcpBandwidthObserver* bandwidth_observer_;
Ivo Creusenfa1d5682016-07-06 09:12:06 +020097 testing::NiceMock<webrtc::MockRtcEventLog> event_log_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000098};
99
Stefan Holmere5904162015-03-26 11:11:06 +0100100TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) {
101 // Receive successively lower REMBs, verify the reserved bitrate is deducted.
102 controller_->SetMinMaxBitrate(0, 0);
103 EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_);
104 bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2);
michaeltf082c2a2016-11-07 04:17:14 -0800105 EXPECT_EQ(webrtc::congestion_controller::GetMinBitrateBps(),
106 bitrate_observer_.last_bitrate_);
Stefan Holmere5904162015-03-26 11:11:06 +0100107 bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps);
108 clock_.AdvanceTimeMilliseconds(1000);
109 controller_->Process();
110 EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_);
111}
112
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000113TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000114 // First REMB applies immediately.
115 int64_t time_ms = 1001;
116 webrtc::ReportBlockList report_blocks;
117 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
118 bandwidth_observer_->OnReceivedEstimatedBitrate(200000);
Stefan Holmere5904162015-03-26 11:11:06 +0100119 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000120 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
121 EXPECT_EQ(0, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000122 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
123 report_blocks.clear();
124 time_ms += 2000;
125
126 // Receive a high remb, test bitrate inc.
127 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000128
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000129 // Test bitrate increase 8% per second.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
131 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100132 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000133 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
134 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000135 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000136
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000137 report_blocks.clear();
138 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000139 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100140 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000141 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
142 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000143 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000144
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000145 report_blocks.clear();
146 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000147 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100148 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000149 time_ms += 1000;
150
151 report_blocks.clear();
152 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81));
153 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100154 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000155 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000156
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000157 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700158 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000159 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100160 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000161 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000162
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000163 // Reach max cap.
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000164 report_blocks.clear();
stefand48717b2016-08-22 08:50:31 -0700165 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000166 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100167 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000168 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000169
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000170 report_blocks.clear();
171 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000172 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100173 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000174
stefan32f81542016-01-20 07:13:58 -0800175 // Test that a low delay-based estimate limits the combined estimate.
Sebastian Janssonb6787bc2018-11-19 18:01:17 +0100176 webrtc::DelayBasedBwe::Result result(false, webrtc::DataRate::kbps(280));
Stefan Holmer280de9e2016-09-30 10:06:51 +0200177 controller_->OnDelayBasedBweResult(result);
stefan32f81542016-01-20 07:13:58 -0800178 EXPECT_EQ(280000, bitrate_observer_.last_bitrate_);
179
180 // Test that a low REMB limits the combined estimate.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000181 bandwidth_observer_->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100182 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000183 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
184 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000185
186 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
stefan32f81542016-01-20 07:13:58 -0800187 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000188}
189
190TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) {
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200191 const uint32_t kSenderSsrc1 = 1;
192 const uint32_t kSenderSsrc2 = 2;
193 const uint32_t kMediaSsrc1 = 3;
194 const uint32_t kMediaSsrc2 = 4;
195
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000196 int64_t time_ms = 1;
197 webrtc::ReportBlockList report_blocks;
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200198 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 1)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000199 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000200 time_ms += 500;
201
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100202 RtcpBandwidthObserver* second_bandwidth_observer = controller_.get();
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200203 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 21)};
Yves Gerey665174f2018-06-19 15:03:05 +0200204 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
205 time_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000206
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000207 // Test start bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200208 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000209 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
210 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000211 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000212
213 // Test bitrate increase 8% per second.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200214 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 21)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000215 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
216 time_ms += 500;
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200217
218 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 21)};
Yves Gerey665174f2018-06-19 15:03:05 +0200219 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
220 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200221 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000222 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
223 EXPECT_EQ(100, bitrate_observer_.last_rtt_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000224 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000225
226 // Extra report should not change estimate.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200227 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 31)};
Yves Gerey665174f2018-06-19 15:03:05 +0200228 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
229 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200230 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000231 time_ms += 500;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000232
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200233 report_blocks = {CreateReportBlock(kSenderSsrc1, kMediaSsrc1, 0, 41)};
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000234 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200235 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000236
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000237 // Second report should not change estimate.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200238 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 41)};
Yves Gerey665174f2018-06-19 15:03:05 +0200239 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 100,
240 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200241 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000242 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000243
244 // Reports from only one bandwidth observer is ok.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200245 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 61)};
Yves Gerey665174f2018-06-19 15:03:05 +0200246 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
247 time_ms);
Stefan Holmer52200d02016-09-20 14:14:23 +0200248 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_);
249 time_ms += 1000;
250
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200251 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 81)};
Yves Gerey665174f2018-06-19 15:03:05 +0200252 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
253 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100254 EXPECT_EQ(276604, 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
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200257 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 121)};
Yves Gerey665174f2018-06-19 15:03:05 +0200258 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
259 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100260 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000261 time_ms += 1000;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000262
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000263 // Reach max cap.
Danil Chapovalov7f8369a2017-06-08 17:22:51 +0200264 report_blocks = {CreateReportBlock(kSenderSsrc2, kMediaSsrc2, 0, 141)};
Yves Gerey665174f2018-06-19 15:03:05 +0200265 second_bandwidth_observer->OnReceivedRtcpReceiverReport(report_blocks, 50,
266 time_ms);
Stefan Holmere5904162015-03-26 11:11:06 +0100267 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000268
269 // Test that a low REMB trigger immediately.
270 // We don't care which bandwidth observer that delivers the REMB.
271 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000);
Stefan Holmere5904162015-03-26 11:11:06 +0100272 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000273 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
274 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000275
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000276 // Min cap.
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277 bandwidth_observer_->OnReceivedEstimatedBitrate(1000);
Stefan Holmere5904162015-03-26 11:11:06 +0100278 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000279}
280
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000281TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000282 uint32_t sequence_number[2] = {0, 0xFF00};
Stefan Holmere5904162015-03-26 11:11:06 +0100283 const int kStartBitrate = 200000;
284 const int kMinBitrate = 100000;
285 const int kMaxBitrate = 300000;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000286 controller_->SetStartBitrate(kStartBitrate);
287 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000288
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000289 // REMBs during the first 2 seconds apply immediately.
290 int64_t time_ms = 1001;
291 webrtc::ReportBlockList report_blocks;
292 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
293 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate);
294 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
295 report_blocks.clear();
296 time_ms += 2000;
297
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000298 // Receive a high REMB, test bitrate increase.
299 bandwidth_observer_->OnReceivedEstimatedBitrate(400000);
300
Stefan Holmere5904162015-03-26 11:11:06 +0100301 int last_bitrate = 0;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000302 // Ramp up to max bitrate.
Stefan Holmer52200d02016-09-20 14:14:23 +0200303 for (int i = 0; i < 7; ++i) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000304 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
305 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
306 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50,
307 time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000308 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate);
309 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_);
310 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
311 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000312 time_ms += 1000;
313 sequence_number[0] += 20;
314 sequence_number[1] += 1;
315 report_blocks.clear();
316 }
317
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000318 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000319
320 // Packet loss on the first stream. Verify that bitrate decreases.
321 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0]));
322 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1]));
323 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000324 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
325 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_);
326 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
327 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000328 sequence_number[0] += 20;
329 sequence_number[1] += 20;
330 time_ms += 1000;
331 report_blocks.clear();
332
333 // Packet loss on the second stream. Verify that bitrate decreases.
334 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
335 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1]));
336 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000337 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate);
338 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_);
339 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
340 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000341 sequence_number[0] += 20;
342 sequence_number[1] += 1;
343 time_ms += 1000;
344 report_blocks.clear();
345
346 // All packets lost on stream with few packets, no back-off.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100347 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0]));
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000348 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1]));
349 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000350 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100351 EXPECT_EQ(WeightedLoss(20, 0, 1, 255), bitrate_observer_.last_fraction_loss_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000352 EXPECT_EQ(50, bitrate_observer_.last_rtt_);
353 last_bitrate = bitrate_observer_.last_bitrate_;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000354 sequence_number[0] += 20;
355 sequence_number[1] += 1;
356 report_blocks.clear();
357}
358
Stefan Holmer52200d02016-09-20 14:14:23 +0200359TEST_F(BitrateControllerTest, TimeoutsWithoutFeedback) {
360 {
361 webrtc::test::ScopedFieldTrials override_field_trials(
Stefan Holmerdb158f92016-10-13 02:11:51 +0200362 "WebRTC-FeedbackTimeout/Enabled/");
Stefan Holmer52200d02016-09-20 14:14:23 +0200363 SetUp();
364 int expected_bitrate_bps = 300000;
365 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
366 kDefaultMaxBitrateBps);
367
368 webrtc::ReportBlockList report_blocks;
369 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
370 bandwidth_observer_->OnReceivedRtcpReceiverReport(
371 report_blocks, 50, clock_.TimeInMilliseconds());
372 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
373 clock_.AdvanceTimeMilliseconds(500);
374
375 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
376 bandwidth_observer_->OnReceivedRtcpReceiverReport(
377 report_blocks, 50, clock_.TimeInMilliseconds());
378 report_blocks.clear();
379 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
380 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
381 clock_.AdvanceTimeMilliseconds(1500);
382
383 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
384 bandwidth_observer_->OnReceivedRtcpReceiverReport(
385 report_blocks, 50, clock_.TimeInMilliseconds());
386 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
387 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700388 clock_.AdvanceTimeMilliseconds(4000);
Stefan Holmer52200d02016-09-20 14:14:23 +0200389
stefan5cb19822017-06-16 07:47:00 -0700390 // 4 seconds since feedback, expect increase.
Stefan Holmer52200d02016-09-20 14:14:23 +0200391 controller_->Process();
392 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
393 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700394 clock_.AdvanceTimeMilliseconds(2000);
Stefan Holmer52200d02016-09-20 14:14:23 +0200395
stefan5cb19822017-06-16 07:47:00 -0700396 // 6 seconds since feedback, expect no increase.
Stefan Holmer52200d02016-09-20 14:14:23 +0200397 controller_->Process();
398 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
stefan5cb19822017-06-16 07:47:00 -0700399 clock_.AdvanceTimeMilliseconds(9001);
Stefan Holmer52200d02016-09-20 14:14:23 +0200400
stefan5cb19822017-06-16 07:47:00 -0700401 // More than 15 seconds since feedback, expect decrease.
Stefan Holmer52200d02016-09-20 14:14:23 +0200402 controller_->Process();
403 expected_bitrate_bps *= 0.8;
404 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
405 clock_.AdvanceTimeMilliseconds(500);
406
407 // Only one timeout every second.
408 controller_->Process();
409 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
410 clock_.AdvanceTimeMilliseconds(501);
411
412 // New timeout allowed.
413 controller_->Process();
414 expected_bitrate_bps *= 0.8;
415 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
416 }
417}
418
419TEST_F(BitrateControllerTest, StopIncreaseWithoutPacketReports) {
420 int expected_bitrate_bps = 300000;
421 controller_->SetBitrates(300000, kDefaultMinBitrateBps,
422 kDefaultMaxBitrateBps);
423
424 webrtc::ReportBlockList report_blocks;
425 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1));
426 bandwidth_observer_->OnReceivedRtcpReceiverReport(
427 report_blocks, 50, clock_.TimeInMilliseconds());
428 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
429 clock_.AdvanceTimeMilliseconds(500);
430
431 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
432 bandwidth_observer_->OnReceivedRtcpReceiverReport(
433 report_blocks, 50, clock_.TimeInMilliseconds());
434 report_blocks.clear();
435 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
436 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
437 clock_.AdvanceTimeMilliseconds(1500);
438
439 // 1.2 seconds without packets reported as received, no increase.
440 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21));
441 bandwidth_observer_->OnReceivedRtcpReceiverReport(
442 report_blocks, 50, clock_.TimeInMilliseconds());
443 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
444 clock_.AdvanceTimeMilliseconds(1000);
445
446 // 5 packets reported as received since last, too few, no increase.
447 report_blocks.push_back(CreateReportBlock(1, 2, 0, 26));
448 bandwidth_observer_->OnReceivedRtcpReceiverReport(
449 report_blocks, 50, clock_.TimeInMilliseconds());
450 report_blocks.clear();
451 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
452 clock_.AdvanceTimeMilliseconds(100);
453
454 // 15 packets reported as received since last, enough to increase.
455 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41));
456 bandwidth_observer_->OnReceivedRtcpReceiverReport(
457 report_blocks, 50, clock_.TimeInMilliseconds());
458 expected_bitrate_bps = expected_bitrate_bps * 1.08 + 1000;
459 EXPECT_EQ(expected_bitrate_bps, bitrate_observer_.last_bitrate_);
460 clock_.AdvanceTimeMilliseconds(1000);
461}