pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <vector> |
| 13 | |
jbauch | f91e6d0 | 2016-01-24 23:05:21 -0800 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 18 | |
Per | 28a4456 | 2016-05-04 17:12:51 +0200 | [diff] [blame] | 19 | using webrtc::RtcpBandwidthObserver; |
perkj | e30c272 | 2016-05-09 04:57:11 -0700 | [diff] [blame] | 20 | using webrtc::BitrateObserver; |
| 21 | using webrtc::BitrateController; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 22 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 23 | uint8_t WeightedLoss(int num_packets1, uint8_t fraction_loss1, |
| 24 | int num_packets2, uint8_t fraction_loss2) { |
| 25 | int weighted_sum = num_packets1 * fraction_loss1 + |
| 26 | num_packets2 * fraction_loss2; |
| 27 | int total_num_packets = num_packets1 + num_packets2; |
| 28 | return (weighted_sum + total_num_packets / 2) / total_num_packets; |
| 29 | } |
| 30 | |
| 31 | webrtc::RTCPReportBlock CreateReportBlock( |
| 32 | uint32_t remote_ssrc, uint32_t source_ssrc, |
| 33 | uint8_t fraction_lost, uint32_t extended_high_sequence_number) { |
| 34 | return webrtc::RTCPReportBlock(remote_ssrc, source_ssrc, fraction_lost, 0, |
| 35 | extended_high_sequence_number, 0, 0, 0); |
| 36 | } |
| 37 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 38 | class TestBitrateObserver: public BitrateObserver { |
| 39 | public: |
| 40 | TestBitrateObserver() |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 41 | : last_bitrate_(0), |
| 42 | last_fraction_loss_(0), |
| 43 | last_rtt_(0) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 44 | } |
| 45 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 46 | virtual void OnNetworkChanged(uint32_t bitrate, |
| 47 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 48 | int64_t rtt) { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 49 | last_bitrate_ = static_cast<int>(bitrate); |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 50 | last_fraction_loss_ = fraction_loss; |
| 51 | last_rtt_ = rtt; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 52 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 53 | int last_bitrate_; |
kjellander@webrtc.org | 2e84c11 | 2012-05-31 13:55:01 +0000 | [diff] [blame] | 54 | uint8_t last_fraction_loss_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 55 | int64_t last_rtt_; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | class BitrateControllerTest : public ::testing::Test { |
| 59 | protected: |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 60 | BitrateControllerTest() : clock_(0) {} |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 61 | ~BitrateControllerTest() {} |
| 62 | |
| 63 | virtual void SetUp() { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 64 | controller_ = |
| 65 | BitrateController::CreateBitrateController(&clock_, &bitrate_observer_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 66 | controller_->SetStartBitrate(kStartBitrateBps); |
| 67 | EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_); |
| 68 | controller_->SetMinMaxBitrate(kMinBitrateBps, kMaxBitrateBps); |
| 69 | EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 70 | bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver(); |
| 71 | } |
| 72 | |
| 73 | virtual void TearDown() { |
| 74 | delete bandwidth_observer_; |
| 75 | delete controller_; |
| 76 | } |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 77 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 78 | const int kMinBitrateBps = 100000; |
| 79 | const int kStartBitrateBps = 200000; |
| 80 | const int kMaxBitrateBps = 300000; |
| 81 | |
| 82 | const int kDefaultMinBitrateBps = 10000; |
| 83 | const int kDefaultMaxBitrateBps = 1000000000; |
| 84 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 85 | webrtc::SimulatedClock clock_; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 86 | TestBitrateObserver bitrate_observer_; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 87 | BitrateController* controller_; |
| 88 | RtcpBandwidthObserver* bandwidth_observer_; |
| 89 | }; |
| 90 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 91 | TEST_F(BitrateControllerTest, DefaultMinMaxBitrate) { |
| 92 | // Receive successively lower REMBs, verify the reserved bitrate is deducted. |
| 93 | controller_->SetMinMaxBitrate(0, 0); |
| 94 | EXPECT_EQ(kStartBitrateBps, bitrate_observer_.last_bitrate_); |
| 95 | bandwidth_observer_->OnReceivedEstimatedBitrate(kDefaultMinBitrateBps / 2); |
| 96 | EXPECT_EQ(kDefaultMinBitrateBps, bitrate_observer_.last_bitrate_); |
| 97 | bandwidth_observer_->OnReceivedEstimatedBitrate(2 * kDefaultMaxBitrateBps); |
| 98 | clock_.AdvanceTimeMilliseconds(1000); |
| 99 | controller_->Process(); |
| 100 | EXPECT_EQ(kDefaultMaxBitrateBps, bitrate_observer_.last_bitrate_); |
| 101 | } |
| 102 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 103 | TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 104 | // First REMB applies immediately. |
| 105 | int64_t time_ms = 1001; |
| 106 | webrtc::ReportBlockList report_blocks; |
| 107 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 1)); |
| 108 | bandwidth_observer_->OnReceivedEstimatedBitrate(200000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 109 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 110 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 111 | EXPECT_EQ(0, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 112 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 113 | report_blocks.clear(); |
| 114 | time_ms += 2000; |
| 115 | |
| 116 | // Receive a high remb, test bitrate inc. |
| 117 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 118 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 119 | // Test bitrate increase 8% per second. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 120 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
| 121 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 122 | EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 123 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 124 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 125 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 126 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 127 | report_blocks.clear(); |
| 128 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 129 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 130 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 131 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 132 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 133 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 134 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 135 | report_blocks.clear(); |
| 136 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 137 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 138 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 139 | time_ms += 1000; |
| 140 | |
| 141 | report_blocks.clear(); |
| 142 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 81)); |
| 143 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 144 | EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 145 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 146 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 147 | report_blocks.clear(); |
| 148 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 801)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 149 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 150 | EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 151 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 152 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 153 | // Reach max cap. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 154 | report_blocks.clear(); |
| 155 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 101)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 156 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 157 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 158 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 159 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 160 | report_blocks.clear(); |
| 161 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 162 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 163 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 164 | |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 165 | // Test that a low delay-based estimate limits the combined estimate. |
| 166 | controller_->UpdateDelayBasedEstimate(280000); |
| 167 | EXPECT_EQ(280000, bitrate_observer_.last_bitrate_); |
| 168 | |
| 169 | // Test that a low REMB limits the combined estimate. |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 170 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 171 | EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 172 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 173 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 174 | |
| 175 | bandwidth_observer_->OnReceivedEstimatedBitrate(1000); |
stefan | 32f8154 | 2016-01-20 07:13:58 -0800 | [diff] [blame] | 176 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 180 | // REMBs during the first 2 seconds apply immediately. |
| 181 | int64_t time_ms = 1; |
| 182 | webrtc::ReportBlockList report_blocks; |
| 183 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 1)); |
| 184 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 185 | report_blocks.clear(); |
| 186 | time_ms += 500; |
| 187 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 188 | RtcpBandwidthObserver* second_bandwidth_observer = |
| 189 | controller_->CreateRtcpBandwidthObserver(); |
| 190 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 191 | // Test start bitrate. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 192 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 193 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 194 | report_blocks, 100, 1); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 195 | EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 196 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 197 | EXPECT_EQ(100, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 198 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 199 | |
| 200 | // Test bitrate increase 8% per second. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 201 | report_blocks.clear(); |
| 202 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 203 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 204 | time_ms += 500; |
| 205 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 206 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 207 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 208 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 209 | EXPECT_EQ(100, bitrate_observer_.last_rtt_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 210 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 211 | |
| 212 | // Extra report should not change estimate. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 213 | report_blocks.clear(); |
| 214 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 31)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 215 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 216 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 217 | EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 218 | time_ms += 500; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 219 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 220 | report_blocks.clear(); |
| 221 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 222 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 223 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 224 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 225 | // Second report should not change estimate. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 226 | report_blocks.clear(); |
| 227 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 228 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 229 | report_blocks, 100, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 230 | EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 231 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 232 | |
| 233 | // Reports from only one bandwidth observer is ok. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 234 | report_blocks.clear(); |
| 235 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 236 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 237 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 238 | EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 239 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 240 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 241 | report_blocks.clear(); |
| 242 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 81)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 243 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
| 244 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 245 | EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 246 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 247 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 248 | // Reach max cap. |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 249 | report_blocks.clear(); |
| 250 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 121)); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 251 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 252 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 253 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 254 | time_ms += 1000; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 255 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 256 | report_blocks.clear(); |
| 257 | report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 258 | second_bandwidth_observer->OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 259 | report_blocks, 50, time_ms); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 260 | EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 261 | |
| 262 | // Test that a low REMB trigger immediately. |
| 263 | // We don't care which bandwidth observer that delivers the REMB. |
| 264 | second_bandwidth_observer->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 265 | EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 266 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 267 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 268 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 269 | // Min cap. |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 270 | bandwidth_observer_->OnReceivedEstimatedBitrate(1000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 271 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 272 | delete second_bandwidth_observer; |
| 273 | } |
| 274 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 275 | TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) { |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 276 | uint32_t sequence_number[2] = {0, 0xFF00}; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 277 | const int kStartBitrate = 200000; |
| 278 | const int kMinBitrate = 100000; |
| 279 | const int kMaxBitrate = 300000; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 280 | controller_->SetStartBitrate(kStartBitrate); |
| 281 | controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 282 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 283 | // REMBs during the first 2 seconds apply immediately. |
| 284 | int64_t time_ms = 1001; |
| 285 | webrtc::ReportBlockList report_blocks; |
| 286 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 287 | bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate); |
| 288 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
| 289 | report_blocks.clear(); |
| 290 | time_ms += 2000; |
| 291 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 292 | // Receive a high REMB, test bitrate increase. |
| 293 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
| 294 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 295 | int last_bitrate = 0; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 296 | // Ramp up to max bitrate. |
| 297 | for (int i = 0; i < 6; ++i) { |
| 298 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 299 | report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); |
| 300 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, |
| 301 | time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 302 | EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 303 | EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); |
| 304 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 305 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 306 | time_ms += 1000; |
| 307 | sequence_number[0] += 20; |
| 308 | sequence_number[1] += 1; |
| 309 | report_blocks.clear(); |
| 310 | } |
| 311 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 312 | EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_); |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 313 | |
| 314 | // Packet loss on the first stream. Verify that bitrate decreases. |
| 315 | report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0])); |
| 316 | report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); |
| 317 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 318 | EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 319 | EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_); |
| 320 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 321 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 322 | sequence_number[0] += 20; |
| 323 | sequence_number[1] += 20; |
| 324 | time_ms += 1000; |
| 325 | report_blocks.clear(); |
| 326 | |
| 327 | // Packet loss on the second stream. Verify that bitrate decreases. |
| 328 | report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); |
| 329 | report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1])); |
| 330 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 331 | EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); |
| 332 | EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_); |
| 333 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 334 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 335 | sequence_number[0] += 20; |
| 336 | sequence_number[1] += 1; |
| 337 | time_ms += 1000; |
| 338 | report_blocks.clear(); |
| 339 | |
| 340 | // All packets lost on stream with few packets, no back-off. |
| 341 | report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0])); |
| 342 | report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1])); |
| 343 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 344 | EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate); |
| 345 | EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_); |
| 346 | EXPECT_EQ(50, bitrate_observer_.last_rtt_); |
| 347 | last_bitrate = bitrate_observer_.last_bitrate_; |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 348 | sequence_number[0] += 20; |
| 349 | sequence_number[1] += 1; |
| 350 | report_blocks.clear(); |
| 351 | } |
| 352 | |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 353 | TEST_F(BitrateControllerTest, SetReservedBitrate) { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 354 | // Receive successively lower REMBs, verify the reserved bitrate is deducted. |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 355 | controller_->SetReservedBitrate(0); |
| 356 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 357 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 358 | controller_->SetReservedBitrate(50000); |
| 359 | bandwidth_observer_->OnReceivedEstimatedBitrate(400000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 360 | EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 361 | |
| 362 | controller_->SetReservedBitrate(0); |
| 363 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 364 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 365 | controller_->SetReservedBitrate(50000); |
| 366 | bandwidth_observer_->OnReceivedEstimatedBitrate(250000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 367 | EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 368 | |
| 369 | controller_->SetReservedBitrate(0); |
| 370 | bandwidth_observer_->OnReceivedEstimatedBitrate(200000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 371 | EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 372 | controller_->SetReservedBitrate(30000); |
| 373 | bandwidth_observer_->OnReceivedEstimatedBitrate(200000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 374 | EXPECT_EQ(170000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 375 | |
| 376 | controller_->SetReservedBitrate(0); |
| 377 | bandwidth_observer_->OnReceivedEstimatedBitrate(160000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 378 | EXPECT_EQ(160000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 379 | controller_->SetReservedBitrate(30000); |
| 380 | bandwidth_observer_->OnReceivedEstimatedBitrate(160000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 381 | EXPECT_EQ(130000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 382 | |
| 383 | controller_->SetReservedBitrate(0); |
| 384 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 385 | EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 386 | controller_->SetReservedBitrate(10000); |
| 387 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 388 | EXPECT_EQ(110000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 389 | |
| 390 | controller_->SetReservedBitrate(0); |
| 391 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 392 | EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 393 | controller_->SetReservedBitrate(50000); |
| 394 | bandwidth_observer_->OnReceivedEstimatedBitrate(120000); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 395 | // Limited by min bitrate. |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 396 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 397 | |
| 398 | controller_->SetReservedBitrate(10000); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 399 | bandwidth_observer_->OnReceivedEstimatedBitrate(1); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 400 | EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 401 | } |