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// Unit tests for BufferLevelFilter class.
12
13#include "webrtc/modules/audio_coding/neteq/buffer_level_filter.h"
14
15#include <math.h>  // Access to pow function.
16
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace webrtc {
20
21TEST(BufferLevelFilter, CreateAndDestroy) {
22  BufferLevelFilter* filter = new BufferLevelFilter();
23  EXPECT_EQ(0, filter->filtered_current_level());
24  delete filter;
25}
26
27TEST(BufferLevelFilter, ConvergenceTest) {
28  BufferLevelFilter filter;
29  for (int times = 10; times <= 50; times += 10) {
30    for (int value = 100; value <= 200; value += 10) {
31      filter.Reset();
32      filter.SetTargetBufferLevel(1);  // Makes filter coefficient 251/256.
33      std::ostringstream ss;
34      ss << "times = " << times << ", value = " << value;
35      SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
36      for (int i = 0; i < times; ++i) {
37        filter.Update(value, 0 /* time_stretched_samples */,
38                      160 /* packet_len_samples */);
39      }
40      // Expect the filtered value to be (theoretically)
41      // (1 - (251/256) ^ |times|) * |value|.
42      double expected_value_double =
43          (1 - pow(251.0 / 256.0, times)) * value;
44      int expected_value = static_cast<int>(expected_value_double);
45      // filtered_current_level() returns the value in Q8.
46      // The actual value may differ slightly from the expected value due to
47      // intermediate-stage rounding errors in the filter implementation.
48      // This is why we have to use EXPECT_NEAR with a tolerance of +/-1.
49      EXPECT_NEAR(expected_value, filter.filtered_current_level() >> 8, 1);
50    }
51  }
52}
53
54// Verify that target buffer level impacts on the filter convergence.
55TEST(BufferLevelFilter, FilterFactor) {
56  BufferLevelFilter filter;
57  // Update 10 times with value 100.
58  const int kTimes = 10;
59  const int kValue = 100;
60
61  filter.SetTargetBufferLevel(3);  // Makes filter coefficient 252/256.
62  for (int i = 0; i < kTimes; ++i) {
63    filter.Update(kValue, 0 /* time_stretched_samples */,
64                  160 /* packet_len_samples */);
65  }
66  // Expect the filtered value to be
67  // (1 - (252/256) ^ |kTimes|) * |kValue|.
68  int expected_value = 14;
69  // filtered_current_level() returns the value in Q8.
70  EXPECT_EQ(expected_value, filter.filtered_current_level() >> 8);
71
72  filter.Reset();
73  filter.SetTargetBufferLevel(7);  // Makes filter coefficient 253/256.
74  for (int i = 0; i < kTimes; ++i) {
75    filter.Update(kValue, 0 /* time_stretched_samples */,
76                  160 /* packet_len_samples */);
77  }
78  // Expect the filtered value to be
79  // (1 - (253/256) ^ |kTimes|) * |kValue|.
80  expected_value = 11;
81  // filtered_current_level() returns the value in Q8.
82  EXPECT_EQ(expected_value, filter.filtered_current_level() >> 8);
83
84  filter.Reset();
85  filter.SetTargetBufferLevel(8);  // Makes filter coefficient 254/256.
86  for (int i = 0; i < kTimes; ++i) {
87    filter.Update(kValue, 0 /* time_stretched_samples */,
88                  160 /* packet_len_samples */);
89  }
90  // Expect the filtered value to be
91  // (1 - (254/256) ^ |kTimes|) * |kValue|.
92  expected_value = 7;
93  // filtered_current_level() returns the value in Q8.
94  EXPECT_EQ(expected_value, filter.filtered_current_level() >> 8);
95}
96
97
98TEST(BufferLevelFilter, TimeStretchedSamples) {
99  BufferLevelFilter filter;
100  filter.SetTargetBufferLevel(1);  // Makes filter coefficient 251/256.
101  // Update 10 times with value 100.
102  const int kTimes = 10;
103  const int kValue = 100;
104  const int kPacketSizeSamples = 160;
105  const int kNumPacketsStretched = 2;
106  const int kTimeStretchedSamples = kNumPacketsStretched * kPacketSizeSamples;
107  for (int i = 0; i < kTimes; ++i) {
108    // Packet size set to 0. Do not expect the parameter
109    // |kTimeStretchedSamples| to have any effect.
110    filter.Update(kValue, kTimeStretchedSamples, 0 /* packet_len_samples */);
111  }
112  // Expect the filtered value to be
113  // (1 - (251/256) ^ |kTimes|) * |kValue|.
114  const int kExpectedValue = 17;
115  // filtered_current_level() returns the value in Q8.
116  EXPECT_EQ(kExpectedValue, filter.filtered_current_level() >> 8);
117
118  // Update filter again, now with non-zero value for packet length.
119  // Set the current filtered value to be the input, in order to isolate the
120  // impact of |kTimeStretchedSamples|.
121  filter.Update(filter.filtered_current_level() >> 8, kTimeStretchedSamples,
122                kPacketSizeSamples);
123  EXPECT_EQ(kExpectedValue - kNumPacketsStretched,
124            filter.filtered_current_level() >> 8);
125  // Try negative value and verify that we come back to the previous result.
126  filter.Update(filter.filtered_current_level() >> 8, -kTimeStretchedSamples,
127                kPacketSizeSamples);
128  EXPECT_EQ(kExpectedValue, filter.filtered_current_level() >> 8);
129}
130
131TEST(BufferLevelFilter, TimeStretchedSamplesNegativeUnevenFrames) {
132  BufferLevelFilter filter;
133  filter.SetTargetBufferLevel(1);  // Makes filter coefficient 251/256.
134  // Update 10 times with value 100.
135  const int kTimes = 10;
136  const int kValue = 100;
137  const int kPacketSizeSamples = 160;
138  const int kTimeStretchedSamples = -3.1415 * kPacketSizeSamples;
139  for (int i = 0; i < kTimes; ++i) {
140    // Packet size set to 0. Do not expect the parameter
141    // |kTimeStretchedSamples| to have any effect.
142    filter.Update(kValue, kTimeStretchedSamples, 0 /* packet_len_samples */);
143  }
144  // Expect the filtered value to be
145  // (1 - (251/256) ^ |kTimes|) * |kValue|.
146  const int kExpectedValue = 17;
147  // filtered_current_level() returns the value in Q8.
148  EXPECT_EQ(kExpectedValue, filter.filtered_current_level() >> 8);
149
150  // Update filter again, now with non-zero value for packet length.
151  // Set the current filtered value to be the input, in order to isolate the
152  // impact of |kTimeStretchedSamples|.
153  filter.Update(filter.filtered_current_level() >> 8, kTimeStretchedSamples,
154                kPacketSizeSamples);
155  EXPECT_EQ(21, filter.filtered_current_level() >> 8);
156  // Try negative value and verify that we come back to the previous result.
157  filter.Update(filter.filtered_current_level() >> 8, -kTimeStretchedSamples,
158                kPacketSizeSamples);
159  EXPECT_EQ(kExpectedValue, filter.filtered_current_level() >> 8);
160}
161
162}  // namespace webrtc
163