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 11#include "testing/gtest/include/gtest/gtest.h" 12 13#include <algorithm> 14#include <vector> 15 16#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" 17#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 18 19using webrtc::RtcpBandwidthObserver; 20using webrtc::BitrateObserver; 21using webrtc::BitrateController; 22 23uint8_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 31webrtc::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 38class TestBitrateObserver: public BitrateObserver { 39 public: 40 TestBitrateObserver() 41 : last_bitrate_(0), 42 last_fraction_loss_(0), 43 last_rtt_(0) { 44 } 45 46 virtual void OnNetworkChanged(uint32_t bitrate, 47 uint8_t fraction_loss, 48 int64_t rtt) { 49 last_bitrate_ = static_cast<int>(bitrate); 50 last_fraction_loss_ = fraction_loss; 51 last_rtt_ = rtt; 52 } 53 int last_bitrate_; 54 uint8_t last_fraction_loss_; 55 int64_t last_rtt_; 56}; 57 58class BitrateControllerTest : public ::testing::Test { 59 protected: 60 BitrateControllerTest() : clock_(0) {} 61 ~BitrateControllerTest() {} 62 63 virtual void SetUp() { 64 controller_ = 65 BitrateController::CreateBitrateController(&clock_, &bitrate_observer_); 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_); 70 bandwidth_observer_ = controller_->CreateRtcpBandwidthObserver(); 71 } 72 73 virtual void TearDown() { 74 delete bandwidth_observer_; 75 delete controller_; 76 } 77 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 85 webrtc::SimulatedClock clock_; 86 TestBitrateObserver bitrate_observer_; 87 BitrateController* controller_; 88 RtcpBandwidthObserver* bandwidth_observer_; 89}; 90 91TEST_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 103TEST_F(BitrateControllerTest, OneBitrateObserverOneRtcpObserver) { 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); 109 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); 110 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 111 EXPECT_EQ(0, bitrate_observer_.last_rtt_); 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); 118 119 // Test bitrate increase 8% per second. 120 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); 121 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 122 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); 123 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 124 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 125 time_ms += 1000; 126 127 report_blocks.clear(); 128 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); 129 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 130 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); 131 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 132 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 133 time_ms += 1000; 134 135 report_blocks.clear(); 136 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); 137 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 138 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); 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); 144 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); 145 time_ms += 1000; 146 147 report_blocks.clear(); 148 report_blocks.push_back(CreateReportBlock(1, 2, 0, 801)); 149 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 150 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); 151 time_ms += 1000; 152 153 // Reach max cap. 154 report_blocks.clear(); 155 report_blocks.push_back(CreateReportBlock(1, 2, 0, 101)); 156 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 157 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); 158 time_ms += 1000; 159 160 report_blocks.clear(); 161 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); 162 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 163 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); 164 165 // Test that a low REMB trigger immediately. 166 bandwidth_observer_->OnReceivedEstimatedBitrate(250000); 167 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); 168 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 169 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 170 171 bandwidth_observer_->OnReceivedEstimatedBitrate(1000); 172 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); // Min cap. 173} 174 175TEST_F(BitrateControllerTest, OneBitrateObserverTwoRtcpObservers) { 176 // REMBs during the first 2 seconds apply immediately. 177 int64_t time_ms = 1; 178 webrtc::ReportBlockList report_blocks; 179 report_blocks.push_back(CreateReportBlock(1, 2, 0, 1)); 180 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 181 report_blocks.clear(); 182 time_ms += 500; 183 184 RtcpBandwidthObserver* second_bandwidth_observer = 185 controller_->CreateRtcpBandwidthObserver(); 186 187 // Test start bitrate. 188 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); 189 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 190 report_blocks, 100, 1); 191 EXPECT_EQ(217000, bitrate_observer_.last_bitrate_); 192 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 193 EXPECT_EQ(100, bitrate_observer_.last_rtt_); 194 time_ms += 500; 195 196 // Test bitrate increase 8% per second. 197 report_blocks.clear(); 198 report_blocks.push_back(CreateReportBlock(1, 2, 0, 21)); 199 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 200 time_ms += 500; 201 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 202 report_blocks, 100, time_ms); 203 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); 204 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 205 EXPECT_EQ(100, bitrate_observer_.last_rtt_); 206 time_ms += 500; 207 208 // Extra report should not change estimate. 209 report_blocks.clear(); 210 report_blocks.push_back(CreateReportBlock(1, 2, 0, 31)); 211 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 212 report_blocks, 100, time_ms); 213 EXPECT_EQ(235360, bitrate_observer_.last_bitrate_); 214 time_ms += 500; 215 216 report_blocks.clear(); 217 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); 218 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 219 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); 220 221 // Second report should not change estimate. 222 report_blocks.clear(); 223 report_blocks.push_back(CreateReportBlock(1, 2, 0, 41)); 224 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 225 report_blocks, 100, time_ms); 226 EXPECT_EQ(255189, bitrate_observer_.last_bitrate_); 227 time_ms += 1000; 228 229 // Reports from only one bandwidth observer is ok. 230 report_blocks.clear(); 231 report_blocks.push_back(CreateReportBlock(1, 2, 0, 61)); 232 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 233 report_blocks, 50, time_ms); 234 EXPECT_EQ(276604, bitrate_observer_.last_bitrate_); 235 time_ms += 1000; 236 237 report_blocks.clear(); 238 report_blocks.push_back(CreateReportBlock(1, 2, 0, 81)); 239 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 240 report_blocks, 50, time_ms); 241 EXPECT_EQ(299732, bitrate_observer_.last_bitrate_); 242 time_ms += 1000; 243 244 // Reach max cap. 245 report_blocks.clear(); 246 report_blocks.push_back(CreateReportBlock(1, 2, 0, 121)); 247 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 248 report_blocks, 50, time_ms); 249 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); 250 time_ms += 1000; 251 252 report_blocks.clear(); 253 report_blocks.push_back(CreateReportBlock(1, 2, 0, 141)); 254 second_bandwidth_observer->OnReceivedRtcpReceiverReport( 255 report_blocks, 50, time_ms); 256 EXPECT_EQ(300000, bitrate_observer_.last_bitrate_); 257 258 // Test that a low REMB trigger immediately. 259 // We don't care which bandwidth observer that delivers the REMB. 260 second_bandwidth_observer->OnReceivedEstimatedBitrate(250000); 261 EXPECT_EQ(250000, bitrate_observer_.last_bitrate_); 262 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 263 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 264 265 // Min cap. 266 bandwidth_observer_->OnReceivedEstimatedBitrate(1000); 267 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); 268 delete second_bandwidth_observer; 269} 270 271TEST_F(BitrateControllerTest, OneBitrateObserverMultipleReportBlocks) { 272 uint32_t sequence_number[2] = {0, 0xFF00}; 273 const int kStartBitrate = 200000; 274 const int kMinBitrate = 100000; 275 const int kMaxBitrate = 300000; 276 controller_->SetStartBitrate(kStartBitrate); 277 controller_->SetMinMaxBitrate(kMinBitrate, kMaxBitrate); 278 279 // REMBs during the first 2 seconds apply immediately. 280 int64_t time_ms = 1001; 281 webrtc::ReportBlockList report_blocks; 282 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); 283 bandwidth_observer_->OnReceivedEstimatedBitrate(kStartBitrate); 284 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 285 report_blocks.clear(); 286 time_ms += 2000; 287 288 // Receive a high REMB, test bitrate increase. 289 bandwidth_observer_->OnReceivedEstimatedBitrate(400000); 290 291 int last_bitrate = 0; 292 // Ramp up to max bitrate. 293 for (int i = 0; i < 6; ++i) { 294 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); 295 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); 296 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, 297 time_ms); 298 EXPECT_GT(bitrate_observer_.last_bitrate_, last_bitrate); 299 EXPECT_EQ(0, bitrate_observer_.last_fraction_loss_); 300 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 301 last_bitrate = bitrate_observer_.last_bitrate_; 302 time_ms += 1000; 303 sequence_number[0] += 20; 304 sequence_number[1] += 1; 305 report_blocks.clear(); 306 } 307 308 EXPECT_EQ(kMaxBitrate, bitrate_observer_.last_bitrate_); 309 310 // Packet loss on the first stream. Verify that bitrate decreases. 311 report_blocks.push_back(CreateReportBlock(1, 2, 50, sequence_number[0])); 312 report_blocks.push_back(CreateReportBlock(1, 3, 0, sequence_number[1])); 313 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 314 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); 315 EXPECT_EQ(WeightedLoss(20, 50, 1, 0), bitrate_observer_.last_fraction_loss_); 316 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 317 last_bitrate = bitrate_observer_.last_bitrate_; 318 sequence_number[0] += 20; 319 sequence_number[1] += 20; 320 time_ms += 1000; 321 report_blocks.clear(); 322 323 // Packet loss on the second stream. Verify that bitrate decreases. 324 report_blocks.push_back(CreateReportBlock(1, 2, 0, sequence_number[0])); 325 report_blocks.push_back(CreateReportBlock(1, 3, 75, sequence_number[1])); 326 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 327 EXPECT_LT(bitrate_observer_.last_bitrate_, last_bitrate); 328 EXPECT_EQ(WeightedLoss(20, 0, 20, 75), bitrate_observer_.last_fraction_loss_); 329 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 330 last_bitrate = bitrate_observer_.last_bitrate_; 331 sequence_number[0] += 20; 332 sequence_number[1] += 1; 333 time_ms += 1000; 334 report_blocks.clear(); 335 336 // All packets lost on stream with few packets, no back-off. 337 report_blocks.push_back(CreateReportBlock(1, 2, 1, sequence_number[0])); 338 report_blocks.push_back(CreateReportBlock(1, 3, 255, sequence_number[1])); 339 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, 50, time_ms); 340 EXPECT_EQ(bitrate_observer_.last_bitrate_, last_bitrate); 341 EXPECT_EQ(WeightedLoss(20, 1, 1, 255), bitrate_observer_.last_fraction_loss_); 342 EXPECT_EQ(50, bitrate_observer_.last_rtt_); 343 last_bitrate = bitrate_observer_.last_bitrate_; 344 sequence_number[0] += 20; 345 sequence_number[1] += 1; 346 report_blocks.clear(); 347} 348 349TEST_F(BitrateControllerTest, SetReservedBitrate) { 350 // Receive successively lower REMBs, verify the reserved bitrate is deducted. 351 controller_->SetReservedBitrate(0); 352 bandwidth_observer_->OnReceivedEstimatedBitrate(400000); 353 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); 354 controller_->SetReservedBitrate(50000); 355 bandwidth_observer_->OnReceivedEstimatedBitrate(400000); 356 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); 357 358 controller_->SetReservedBitrate(0); 359 bandwidth_observer_->OnReceivedEstimatedBitrate(250000); 360 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); 361 controller_->SetReservedBitrate(50000); 362 bandwidth_observer_->OnReceivedEstimatedBitrate(250000); 363 EXPECT_EQ(150000, bitrate_observer_.last_bitrate_); 364 365 controller_->SetReservedBitrate(0); 366 bandwidth_observer_->OnReceivedEstimatedBitrate(200000); 367 EXPECT_EQ(200000, bitrate_observer_.last_bitrate_); 368 controller_->SetReservedBitrate(30000); 369 bandwidth_observer_->OnReceivedEstimatedBitrate(200000); 370 EXPECT_EQ(170000, bitrate_observer_.last_bitrate_); 371 372 controller_->SetReservedBitrate(0); 373 bandwidth_observer_->OnReceivedEstimatedBitrate(160000); 374 EXPECT_EQ(160000, bitrate_observer_.last_bitrate_); 375 controller_->SetReservedBitrate(30000); 376 bandwidth_observer_->OnReceivedEstimatedBitrate(160000); 377 EXPECT_EQ(130000, bitrate_observer_.last_bitrate_); 378 379 controller_->SetReservedBitrate(0); 380 bandwidth_observer_->OnReceivedEstimatedBitrate(120000); 381 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); 382 controller_->SetReservedBitrate(10000); 383 bandwidth_observer_->OnReceivedEstimatedBitrate(120000); 384 EXPECT_EQ(110000, bitrate_observer_.last_bitrate_); 385 386 controller_->SetReservedBitrate(0); 387 bandwidth_observer_->OnReceivedEstimatedBitrate(120000); 388 EXPECT_EQ(120000, bitrate_observer_.last_bitrate_); 389 controller_->SetReservedBitrate(50000); 390 bandwidth_observer_->OnReceivedEstimatedBitrate(120000); 391 // Limited by min bitrate. 392 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); 393 394 controller_->SetReservedBitrate(10000); 395 bandwidth_observer_->OnReceivedEstimatedBitrate(1); 396 EXPECT_EQ(100000, bitrate_observer_.last_bitrate_); 397} 398