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 Expand class.
12
13 #include "modules/audio_coding/neteq/expand.h"
14
15 #include "common_audio/signal_processing/include/signal_processing_library.h"
16 #include "modules/audio_coding/neteq/background_noise.h"
17 #include "modules/audio_coding/neteq/random_vector.h"
18 #include "modules/audio_coding/neteq/statistics_calculator.h"
19 #include "modules/audio_coding/neteq/sync_buffer.h"
20 #include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
21 #include "rtc_base/numerics/safe_conversions.h"
22 #include "test/gtest.h"
23 #include "test/testsupport/fileutils.h"
24
25 namespace webrtc {
26
TEST(Expand,CreateAndDestroy)27 TEST(Expand, CreateAndDestroy) {
28 int fs = 8000;
29 size_t channels = 1;
30 BackgroundNoise bgn(channels);
31 SyncBuffer sync_buffer(1, 1000);
32 RandomVector random_vector;
33 StatisticsCalculator statistics;
34 Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
35 }
36
TEST(Expand,CreateUsingFactory)37 TEST(Expand, CreateUsingFactory) {
38 int fs = 8000;
39 size_t channels = 1;
40 BackgroundNoise bgn(channels);
41 SyncBuffer sync_buffer(1, 1000);
42 RandomVector random_vector;
43 StatisticsCalculator statistics;
44 ExpandFactory expand_factory;
45 Expand* expand = expand_factory.Create(&bgn, &sync_buffer, &random_vector,
46 &statistics, fs, channels);
47 EXPECT_TRUE(expand != NULL);
48 delete expand;
49 }
50
51 namespace {
52 class FakeStatisticsCalculator : public StatisticsCalculator {
53 public:
LogDelayedPacketOutageEvent(int outage_duration_ms)54 void LogDelayedPacketOutageEvent(int outage_duration_ms) override {
55 last_outage_duration_ms_ = outage_duration_ms;
56 }
57
last_outage_duration_ms() const58 int last_outage_duration_ms() const { return last_outage_duration_ms_; }
59
60 private:
61 int last_outage_duration_ms_ = 0;
62 };
63
64 // This is the same size that is given to the SyncBuffer object in NetEq.
65 const size_t kNetEqSyncBufferLengthMs = 720;
66 } // namespace
67
68 class ExpandTest : public ::testing::Test {
69 protected:
ExpandTest()70 ExpandTest()
71 : input_file_(test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
72 32000),
73 test_sample_rate_hz_(32000),
74 num_channels_(1),
75 background_noise_(num_channels_),
76 sync_buffer_(num_channels_,
77 kNetEqSyncBufferLengthMs * test_sample_rate_hz_ / 1000),
78 expand_(&background_noise_,
79 &sync_buffer_,
80 &random_vector_,
81 &statistics_,
82 test_sample_rate_hz_,
83 num_channels_) {
84 WebRtcSpl_Init();
85 input_file_.set_output_rate_hz(test_sample_rate_hz_);
86 }
87
SetUp()88 void SetUp() override {
89 // Fast-forward the input file until there is speech (about 1.1 second into
90 // the file).
91 const int speech_start_samples =
92 static_cast<int>(test_sample_rate_hz_ * 1.1f);
93 ASSERT_TRUE(input_file_.Seek(speech_start_samples));
94
95 // Pre-load the sync buffer with speech data.
96 std::unique_ptr<int16_t[]> temp(new int16_t[sync_buffer_.Size()]);
97 ASSERT_TRUE(input_file_.Read(sync_buffer_.Size(), temp.get()));
98 sync_buffer_.Channel(0).OverwriteAt(temp.get(), sync_buffer_.Size(), 0);
99 ASSERT_EQ(1u, num_channels_) << "Fix: Must populate all channels.";
100 }
101
102 test::ResampleInputAudioFile input_file_;
103 int test_sample_rate_hz_;
104 size_t num_channels_;
105 BackgroundNoise background_noise_;
106 SyncBuffer sync_buffer_;
107 RandomVector random_vector_;
108 FakeStatisticsCalculator statistics_;
109 Expand expand_;
110 };
111
112 // This test calls the expand object to produce concealment data a few times,
113 // and then ends by calling SetParametersForNormalAfterExpand. This simulates
114 // the situation where the packet next up for decoding was just delayed, not
115 // lost.
TEST_F(ExpandTest,DelayedPacketOutage)116 TEST_F(ExpandTest, DelayedPacketOutage) {
117 AudioMultiVector output(num_channels_);
118 size_t sum_output_len_samples = 0;
119 for (int i = 0; i < 10; ++i) {
120 EXPECT_EQ(0, expand_.Process(&output));
121 EXPECT_GT(output.Size(), 0u);
122 sum_output_len_samples += output.Size();
123 EXPECT_EQ(0, statistics_.last_outage_duration_ms());
124 }
125 expand_.SetParametersForNormalAfterExpand();
126 // Convert |sum_output_len_samples| to milliseconds.
127 EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples /
128 (test_sample_rate_hz_ / 1000)),
129 statistics_.last_outage_duration_ms());
130 }
131
132 // This test is similar to DelayedPacketOutage, but ends by calling
133 // SetParametersForMergeAfterExpand. This simulates the situation where the
134 // packet next up for decoding was actually lost (or at least a later packet
135 // arrived before it).
TEST_F(ExpandTest,LostPacketOutage)136 TEST_F(ExpandTest, LostPacketOutage) {
137 AudioMultiVector output(num_channels_);
138 size_t sum_output_len_samples = 0;
139 for (int i = 0; i < 10; ++i) {
140 EXPECT_EQ(0, expand_.Process(&output));
141 EXPECT_GT(output.Size(), 0u);
142 sum_output_len_samples += output.Size();
143 EXPECT_EQ(0, statistics_.last_outage_duration_ms());
144 }
145 expand_.SetParametersForMergeAfterExpand();
146 EXPECT_EQ(0, statistics_.last_outage_duration_ms());
147 }
148
149 // This test is similar to the DelayedPacketOutage test above, but with the
150 // difference that Expand::Reset() is called after 5 calls to Expand::Process().
151 // This should reset the statistics, and will in the end lead to an outage of
152 // 5 periods instead of 10.
TEST_F(ExpandTest,CheckOutageStatsAfterReset)153 TEST_F(ExpandTest, CheckOutageStatsAfterReset) {
154 AudioMultiVector output(num_channels_);
155 size_t sum_output_len_samples = 0;
156 for (int i = 0; i < 10; ++i) {
157 EXPECT_EQ(0, expand_.Process(&output));
158 EXPECT_GT(output.Size(), 0u);
159 sum_output_len_samples += output.Size();
160 if (i == 5) {
161 expand_.Reset();
162 sum_output_len_samples = 0;
163 }
164 EXPECT_EQ(0, statistics_.last_outage_duration_ms());
165 }
166 expand_.SetParametersForNormalAfterExpand();
167 // Convert |sum_output_len_samples| to milliseconds.
168 EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples /
169 (test_sample_rate_hz_ / 1000)),
170 statistics_.last_outage_duration_ms());
171 }
172
173 namespace {
174 // Runs expand until Muted() returns true. Times out after 1000 calls.
ExpandUntilMuted(size_t num_channels,Expand * expand)175 void ExpandUntilMuted(size_t num_channels, Expand* expand) {
176 EXPECT_FALSE(expand->Muted()) << "Instance is muted from the start";
177 AudioMultiVector output(num_channels);
178 int num_calls = 0;
179 while (!expand->Muted()) {
180 ASSERT_LT(num_calls++, 1000) << "Test timed out";
181 EXPECT_EQ(0, expand->Process(&output));
182 }
183 }
184 } // namespace
185
186 // Verifies that Muted() returns true after a long expand period. Also verifies
187 // that Muted() is reset to false after calling Reset(),
188 // SetParametersForMergeAfterExpand() and SetParametersForNormalAfterExpand().
TEST_F(ExpandTest,Muted)189 TEST_F(ExpandTest, Muted) {
190 ExpandUntilMuted(num_channels_, &expand_);
191 expand_.Reset();
192 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
193
194 ExpandUntilMuted(num_channels_, &expand_);
195 expand_.SetParametersForMergeAfterExpand();
196 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
197
198 expand_.Reset(); // Must reset in order to start a new expand period.
199 ExpandUntilMuted(num_channels_, &expand_);
200 expand_.SetParametersForNormalAfterExpand();
201 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
202 }
203
204 // TODO(hlundin): Write more tests.
205
206 } // namespace webrtc
207