1 /* 2 * Copyright 2011 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 <limits.h> 12 13 #include "webrtc/base/bandwidthsmoother.h" 14 #include "webrtc/base/gunit.h" 15 16 namespace rtc { 17 18 static const int kTimeBetweenIncrease = 10; 19 static const double kPercentIncrease = 1.1; 20 static const size_t kSamplesCountToAverage = 2; 21 static const double kMinSampleCountPercent = 1.0; 22 23 TEST(BandwidthSmootherTest, TestSampleIncrease) { 24 BandwidthSmoother mon(1000, // initial_bandwidth_guess 25 kTimeBetweenIncrease, 26 kPercentIncrease, 27 kSamplesCountToAverage, 28 kMinSampleCountPercent); 29 30 int bandwidth_sample = 1000; 31 EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation()); 32 bandwidth_sample = 33 static_cast<int>(bandwidth_sample * kPercentIncrease); 34 EXPECT_FALSE(mon.Sample(9, bandwidth_sample)); 35 EXPECT_TRUE(mon.Sample(10, bandwidth_sample)); 36 EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation()); 37 int next_expected_est = 38 static_cast<int>(bandwidth_sample * kPercentIncrease); 39 bandwidth_sample *= 2; 40 EXPECT_TRUE(mon.Sample(20, bandwidth_sample)); 41 EXPECT_EQ(next_expected_est, mon.get_bandwidth_estimation()); 42 } 43 44 TEST(BandwidthSmootherTest, TestSampleIncreaseFromZero) { 45 BandwidthSmoother mon(0, // initial_bandwidth_guess 46 kTimeBetweenIncrease, 47 kPercentIncrease, 48 kSamplesCountToAverage, 49 kMinSampleCountPercent); 50 51 const int kBandwidthSample = 1000; 52 EXPECT_EQ(0, mon.get_bandwidth_estimation()); 53 EXPECT_FALSE(mon.Sample(9, kBandwidthSample)); 54 EXPECT_TRUE(mon.Sample(10, kBandwidthSample)); 55 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation()); 56 } 57 58 TEST(BandwidthSmootherTest, TestSampleDecrease) { 59 BandwidthSmoother mon(1000, // initial_bandwidth_guess 60 kTimeBetweenIncrease, 61 kPercentIncrease, 62 kSamplesCountToAverage, 63 kMinSampleCountPercent); 64 65 const int kBandwidthSample = 999; 66 EXPECT_EQ(1000, mon.get_bandwidth_estimation()); 67 EXPECT_FALSE(mon.Sample(1, kBandwidthSample)); 68 EXPECT_EQ(1000, mon.get_bandwidth_estimation()); 69 EXPECT_TRUE(mon.Sample(2, kBandwidthSample)); 70 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation()); 71 } 72 73 TEST(BandwidthSmootherTest, TestSampleTooFewSamples) { 74 BandwidthSmoother mon(1000, // initial_bandwidth_guess 75 kTimeBetweenIncrease, 76 kPercentIncrease, 77 10, // 10 samples. 78 0.5); // 5 min samples. 79 80 const int kBandwidthSample = 500; 81 EXPECT_EQ(1000, mon.get_bandwidth_estimation()); 82 EXPECT_FALSE(mon.Sample(1, kBandwidthSample)); 83 EXPECT_FALSE(mon.Sample(2, kBandwidthSample)); 84 EXPECT_FALSE(mon.Sample(3, kBandwidthSample)); 85 EXPECT_FALSE(mon.Sample(4, kBandwidthSample)); 86 EXPECT_EQ(1000, mon.get_bandwidth_estimation()); 87 EXPECT_TRUE(mon.Sample(5, kBandwidthSample)); 88 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation()); 89 } 90 91 TEST(BandwidthSmootherTest, TestSampleRollover) { 92 const int kHugeBandwidth = 2000000000; // > INT_MAX/1.1 93 BandwidthSmoother mon(kHugeBandwidth, 94 kTimeBetweenIncrease, 95 kPercentIncrease, 96 kSamplesCountToAverage, 97 kMinSampleCountPercent); 98 99 EXPECT_FALSE(mon.Sample(10, INT_MAX)); 100 EXPECT_FALSE(mon.Sample(11, INT_MAX)); 101 EXPECT_EQ(kHugeBandwidth, mon.get_bandwidth_estimation()); 102 } 103 104 TEST(BandwidthSmootherTest, TestSampleNegative) { 105 BandwidthSmoother mon(1000, // initial_bandwidth_guess 106 kTimeBetweenIncrease, 107 kPercentIncrease, 108 kSamplesCountToAverage, 109 kMinSampleCountPercent); 110 111 EXPECT_FALSE(mon.Sample(10, -1)); 112 EXPECT_FALSE(mon.Sample(11, -1)); 113 EXPECT_EQ(1000, mon.get_bandwidth_estimation()); 114 } 115 116 } // namespace rtc 117