Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <algorithm> | |
| 12 | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "webrtc/base/rate_statistics.h" | 14 #include "webrtc/base/rate_statistics.h" |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 using webrtc::RateStatistics; | 18 using webrtc::RateStatistics; |
| 17 | 19 |
| 18 class RateStatisticsTest : public ::testing::Test { | 20 class RateStatisticsTest : public ::testing::Test { |
| 19 protected: | 21 protected: |
| 20 RateStatisticsTest() : stats_(500, 8000) {} | 22 RateStatisticsTest() : stats_(500, 8000) {} |
| 21 RateStatistics stats_; | 23 RateStatistics stats_; |
| 22 }; | 24 }; |
| 23 | 25 |
| 24 TEST_F(RateStatisticsTest, TestStrictMode) { | 26 TEST_F(RateStatisticsTest, TestStrictMode) { |
| 25 int64_t now_ms = 0; | 27 int64_t now_ms = 0; |
| 26 // Should be initialized to 0. | 28 // Should be initialized to 0. |
| 27 EXPECT_EQ(0u, stats_.Rate(now_ms)); | 29 EXPECT_EQ(0u, stats_.Rate(now_ms)); |
| 28 stats_.Update(1500, now_ms); | 30 stats_.Update(1500, now_ms); |
| 29 // Expecting 24 kbps given a 500 ms window with one 1500 bytes packet. | 31 // Expecting 1200 kbps since the window is initially kept small and grows as |
| 30 EXPECT_EQ(24000u, stats_.Rate(now_ms)); | 32 // we have more data. |
| 33 EXPECT_EQ(12000000u, stats_.Rate(now_ms)); | |
| 31 stats_.Reset(); | 34 stats_.Reset(); |
| 32 // Expecting 0 after init. | 35 // Expecting 0 after init. |
| 33 EXPECT_EQ(0u, stats_.Rate(now_ms)); | 36 EXPECT_EQ(0u, stats_.Rate(now_ms)); |
| 34 for (int i = 0; i < 100000; ++i) { | 37 for (int i = 0; i < 100000; ++i) { |
| 35 if (now_ms % 10 == 0) { | 38 if (now_ms % 10 == 0) { |
| 36 stats_.Update(1500, now_ms); | 39 stats_.Update(1500, now_ms); |
| 37 } | 40 } |
| 38 // Approximately 1200 kbps expected. Not exact since when packets | 41 // Approximately 1200 kbps expected. Not exact since when packets |
| 39 // are removed we will jump 10 ms to the next packet. | 42 // are removed we will jump 10 ms to the next packet. |
| 40 if (now_ms > 0 && now_ms % 500 == 0) { | 43 if (now_ms > 0 && now_ms % 500 == 0) { |
| 41 EXPECT_NEAR(1200000u, stats_.Rate(now_ms), 24000u); | 44 EXPECT_NEAR(1200000u, stats_.Rate(now_ms), 22000u); |
| 42 } | 45 } |
| 43 now_ms += 1; | 46 now_ms += 1; |
| 44 } | 47 } |
| 45 now_ms += 500; | 48 now_ms += 500; |
| 46 // The window is 2 seconds. If nothing has been received for that time | 49 // The window is 2 seconds. If nothing has been received for that time |
| 47 // the estimate should be 0. | 50 // the estimate should be 0. |
| 48 EXPECT_EQ(0u, stats_.Rate(now_ms)); | 51 EXPECT_EQ(0u, stats_.Rate(now_ms)); |
| 49 } | 52 } |
| 50 | 53 |
| 51 TEST_F(RateStatisticsTest, IncreasingThenDecreasingBitrate) { | 54 TEST_F(RateStatisticsTest, IncreasingThenDecreasingBitrate) { |
| 52 int64_t now_ms = 0; | 55 int64_t now_ms = 0; |
| 53 stats_.Reset(); | 56 stats_.Reset(); |
| 54 // Expecting 0 after init. | 57 // Expecting 0 after init. |
| 55 uint32_t bitrate = stats_.Rate(now_ms); | 58 uint32_t bitrate = stats_.Rate(now_ms); |
| 56 EXPECT_EQ(0u, bitrate); | 59 EXPECT_EQ(0u, bitrate); |
| 60 uint32_t kExpectedBitrate = 8000000; | |
|
perkj_webrtc
2016/04/22 05:24:58
nit: const uint32_t
| |
| 57 // 1000 bytes per millisecond until plateau is reached. | 61 // 1000 bytes per millisecond until plateau is reached. |
| 62 int prev_error = kExpectedBitrate; | |
| 58 while (++now_ms < 10000) { | 63 while (++now_ms < 10000) { |
| 59 stats_.Update(1000, now_ms); | 64 stats_.Update(1000, now_ms); |
| 60 uint32_t new_bitrate = stats_.Rate(now_ms); | 65 bitrate = stats_.Rate(now_ms); |
| 61 if (new_bitrate != bitrate) { | 66 int error = kExpectedBitrate - bitrate; |
| 62 // New bitrate must be higher than previous one. | 67 error = std::abs(error); |
| 63 EXPECT_GT(new_bitrate, bitrate); | 68 // Expect the estimation error to decrease as the window is extended. |
| 64 } else { | 69 EXPECT_LE(error, prev_error + 1); |
| 65 // Plateau reached, 8000 kbps expected. | 70 prev_error = error; |
| 66 EXPECT_NEAR(8000000u, bitrate, 80000u); | |
| 67 break; | |
| 68 } | |
| 69 bitrate = new_bitrate; | |
| 70 } | 71 } |
| 72 // Window filled, expect to be close to 8000000. | |
| 73 EXPECT_EQ(8000000u, bitrate); | |
| 74 | |
| 71 // 1000 bytes per millisecond until 10-second mark, 8000 kbps expected. | 75 // 1000 bytes per millisecond until 10-second mark, 8000 kbps expected. |
| 72 while (++now_ms < 10000) { | 76 while (++now_ms < 10000) { |
| 73 stats_.Update(1000, now_ms); | 77 stats_.Update(1000, now_ms); |
| 74 bitrate = stats_.Rate(now_ms); | 78 bitrate = stats_.Rate(now_ms); |
| 75 EXPECT_NEAR(8000000u, bitrate, 80000u); | 79 EXPECT_EQ(8000000u, bitrate); |
| 76 } | 80 } |
| 77 // Zero bytes per millisecond until 0 is reached. | 81 // Zero bytes per millisecond until 0 is reached. |
| 78 while (++now_ms < 20000) { | 82 while (++now_ms < 20000) { |
| 79 stats_.Update(0, now_ms); | 83 stats_.Update(0, now_ms); |
| 80 uint32_t new_bitrate = stats_.Rate(now_ms); | 84 uint32_t new_bitrate = stats_.Rate(now_ms); |
| 81 if (new_bitrate != bitrate) { | 85 if (new_bitrate != bitrate) { |
| 82 // New bitrate must be lower than previous one. | 86 // New bitrate must be lower than previous one. |
| 83 EXPECT_LT(new_bitrate, bitrate); | 87 EXPECT_LT(new_bitrate, bitrate); |
| 84 } else { | 88 } else { |
| 85 // 0 kbps expected. | 89 // 0 kbps expected. |
| 86 EXPECT_EQ(0u, bitrate); | 90 EXPECT_EQ(0u, bitrate); |
| 87 break; | 91 break; |
| 88 } | 92 } |
| 89 bitrate = new_bitrate; | 93 bitrate = new_bitrate; |
| 90 } | 94 } |
| 91 // Zero bytes per millisecond until 20-second mark, 0 kbps expected. | 95 // Zero bytes per millisecond until 20-second mark, 0 kbps expected. |
| 92 while (++now_ms < 20000) { | 96 while (++now_ms < 20000) { |
| 93 stats_.Update(0, now_ms); | 97 stats_.Update(0, now_ms); |
| 94 EXPECT_EQ(0u, stats_.Rate(now_ms)); | 98 EXPECT_EQ(0u, stats_.Rate(now_ms)); |
| 95 } | 99 } |
| 96 } | 100 } |
| 101 | |
| 102 TEST_F(RateStatisticsTest, ResetAfterSilence) { | |
| 103 int64_t now_ms = 0; | |
| 104 stats_.Reset(); | |
| 105 // Expecting 0 after init. | |
| 106 uint32_t bitrate = stats_.Rate(now_ms); | |
| 107 EXPECT_EQ(0u, bitrate); | |
| 108 uint32_t kExpectedBitrate = 8000000; | |
|
perkj_webrtc
2016/04/22 05:24:58
dito
| |
| 109 // 1000 bytes per millisecond until the window has been filled. | |
| 110 int prev_error = kExpectedBitrate; | |
| 111 while (++now_ms < 10000) { | |
| 112 stats_.Update(1000, now_ms); | |
| 113 bitrate = stats_.Rate(now_ms); | |
| 114 int error = kExpectedBitrate - bitrate; | |
| 115 error = std::abs(error); | |
| 116 // Expect the estimation error to decrease as the window is extended. | |
| 117 EXPECT_LE(error, prev_error + 1); | |
| 118 prev_error = error; | |
| 119 } | |
| 120 // Window filled, expect to be close to 8000000. | |
| 121 EXPECT_EQ(8000000u, bitrate); | |
| 122 | |
| 123 now_ms += 501; | |
|
sprang_webrtc
2016/04/22 10:36:58
Can we add a named constant for the 500ms window?
| |
| 124 EXPECT_EQ(0u, stats_.Rate(now_ms)); | |
| 125 stats_.Update(1000, now_ms); | |
| 126 // We expect one sample of 1000 bytes, and that the bitrate is measured over | |
| 127 // 2 milliseconds, i.e., 8 * 1000 / 0.002 = | |
| 128 EXPECT_EQ(8000000u, stats_.Rate(now_ms)); | |
| 129 } | |
| 97 } // namespace | 130 } // namespace |
| OLD | NEW |