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#include "webrtc/modules/remote_bitrate_estimator/rate_statistics.h"
13
14namespace {
15
16using webrtc::RateStatistics;
17
18class RateStatisticsTest : public ::testing::Test {
19 protected:
20  RateStatisticsTest() : stats_(500, 8000) {}
21  RateStatistics stats_;
22};
23
24TEST_F(RateStatisticsTest, TestStrictMode) {
25  int64_t now_ms = 0;
26  // Should be initialized to 0.
27  EXPECT_EQ(0u, stats_.Rate(now_ms));
28  stats_.Update(1500, now_ms);
29  // Expecting 24 kbps given a 500 ms window with one 1500 bytes packet.
30  EXPECT_EQ(24000u, stats_.Rate(now_ms));
31  stats_.Reset();
32  // Expecting 0 after init.
33  EXPECT_EQ(0u, stats_.Rate(now_ms));
34  for (int i = 0; i < 100000; ++i) {
35    if (now_ms % 10 == 0) {
36      stats_.Update(1500, now_ms);
37    }
38    // Approximately 1200 kbps expected. Not exact since when packets
39    // are removed we will jump 10 ms to the next packet.
40    if (now_ms > 0 && now_ms % 500 == 0) {
41      EXPECT_NEAR(1200000u, stats_.Rate(now_ms), 24000u);
42    }
43    now_ms += 1;
44  }
45  now_ms += 500;
46  // The window is 2 seconds. If nothing has been received for that time
47  // the estimate should be 0.
48  EXPECT_EQ(0u, stats_.Rate(now_ms));
49}
50
51TEST_F(RateStatisticsTest, IncreasingThenDecreasingBitrate) {
52  int64_t now_ms = 0;
53  stats_.Reset();
54  // Expecting 0 after init.
55  uint32_t bitrate = stats_.Rate(now_ms);
56  EXPECT_EQ(0u, bitrate);
57  // 1000 bytes per millisecond until plateau is reached.
58  while (++now_ms < 10000) {
59    stats_.Update(1000, now_ms);
60    uint32_t new_bitrate = stats_.Rate(now_ms);
61    if (new_bitrate != bitrate) {
62      // New bitrate must be higher than previous one.
63      EXPECT_GT(new_bitrate, bitrate);
64    } else {
65      // Plateau reached, 8000 kbps expected.
66      EXPECT_NEAR(8000000u, bitrate, 80000u);
67      break;
68    }
69    bitrate = new_bitrate;
70  }
71  // 1000 bytes per millisecond until 10-second mark, 8000 kbps expected.
72  while (++now_ms < 10000) {
73    stats_.Update(1000, now_ms);
74    bitrate = stats_.Rate(now_ms);
75    EXPECT_NEAR(8000000u, bitrate, 80000u);
76  }
77  // Zero bytes per millisecond until 0 is reached.
78  while (++now_ms < 20000) {
79    stats_.Update(0, now_ms);
80    uint32_t new_bitrate = stats_.Rate(now_ms);
81    if (new_bitrate != bitrate) {
82      // New bitrate must be lower than previous one.
83      EXPECT_LT(new_bitrate, bitrate);
84    } else {
85      // 0 kbps expected.
86      EXPECT_EQ(0u, bitrate);
87      break;
88    }
89    bitrate = new_bitrate;
90  }
91  // Zero bytes per millisecond until 20-second mark, 0 kbps expected.
92  while (++now_ms < 20000) {
93    stats_.Update(0, now_ms);
94    EXPECT_EQ(0u, stats_.Rate(now_ms));
95  }
96}
97}  // namespace
98