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