blob: 55a8866564e29b2a2f76c780615b089c3c7579bb [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#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"
Karl Wiberge40468b2017-11-22 10:42:26 +010021#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25namespace webrtc {
26
27TEST(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;
Henrik Lundinbef77e22015-08-18 14:58:09 +020033 StatisticsCalculator statistics;
34 Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035}
36
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000037TEST(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;
Henrik Lundinbef77e22015-08-18 14:58:09 +020043 StatisticsCalculator statistics;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000044 ExpandFactory expand_factory;
Henrik Lundinbef77e22015-08-18 14:58:09 +020045 Expand* expand = expand_factory.Create(&bgn, &sync_buffer, &random_vector,
46 &statistics, fs, channels);
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000047 EXPECT_TRUE(expand != NULL);
48 delete expand;
49}
50
Henrik Lundinbef77e22015-08-18 14:58:09 +020051namespace {
52class FakeStatisticsCalculator : public StatisticsCalculator {
53 public:
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +010054 void LogDelayedPacketOutageEvent(int num_samples, int fs_hz) override {
55 last_outage_duration_samples_ = num_samples;
Henrik Lundinbef77e22015-08-18 14:58:09 +020056 }
57
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +010058 int last_outage_duration_samples() const {
59 return last_outage_duration_samples_;
60 }
Henrik Lundinbef77e22015-08-18 14:58:09 +020061
62 private:
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +010063 int last_outage_duration_samples_ = 0;
Henrik Lundinbef77e22015-08-18 14:58:09 +020064};
65
66// This is the same size that is given to the SyncBuffer object in NetEq.
67const size_t kNetEqSyncBufferLengthMs = 720;
68} // namespace
69
70class ExpandTest : public ::testing::Test {
71 protected:
72 ExpandTest()
73 : input_file_(test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
74 32000),
75 test_sample_rate_hz_(32000),
76 num_channels_(1),
77 background_noise_(num_channels_),
78 sync_buffer_(num_channels_,
79 kNetEqSyncBufferLengthMs * test_sample_rate_hz_ / 1000),
80 expand_(&background_noise_,
81 &sync_buffer_,
82 &random_vector_,
83 &statistics_,
84 test_sample_rate_hz_,
85 num_channels_) {
Henrik Lundinbef77e22015-08-18 14:58:09 +020086 input_file_.set_output_rate_hz(test_sample_rate_hz_);
87 }
88
89 void SetUp() override {
90 // Fast-forward the input file until there is speech (about 1.1 second into
91 // the file).
Mirko Bonadei737e0732017-10-19 09:00:17 +020092 const int speech_start_samples =
93 static_cast<int>(test_sample_rate_hz_ * 1.1f);
Henrik Lundinbef77e22015-08-18 14:58:09 +020094 ASSERT_TRUE(input_file_.Seek(speech_start_samples));
95
96 // Pre-load the sync buffer with speech data.
minyue-webrtc79553cb2016-05-10 19:55:56 +020097 std::unique_ptr<int16_t[]> temp(new int16_t[sync_buffer_.Size()]);
98 ASSERT_TRUE(input_file_.Read(sync_buffer_.Size(), temp.get()));
99 sync_buffer_.Channel(0).OverwriteAt(temp.get(), sync_buffer_.Size(), 0);
Henrik Lundinbef77e22015-08-18 14:58:09 +0200100 ASSERT_EQ(1u, num_channels_) << "Fix: Must populate all channels.";
101 }
102
103 test::ResampleInputAudioFile input_file_;
104 int test_sample_rate_hz_;
105 size_t num_channels_;
106 BackgroundNoise background_noise_;
107 SyncBuffer sync_buffer_;
108 RandomVector random_vector_;
109 FakeStatisticsCalculator statistics_;
110 Expand expand_;
111};
112
113// This test calls the expand object to produce concealment data a few times,
114// and then ends by calling SetParametersForNormalAfterExpand. This simulates
115// the situation where the packet next up for decoding was just delayed, not
116// lost.
117TEST_F(ExpandTest, DelayedPacketOutage) {
118 AudioMultiVector output(num_channels_);
119 size_t sum_output_len_samples = 0;
120 for (int i = 0; i < 10; ++i) {
121 EXPECT_EQ(0, expand_.Process(&output));
122 EXPECT_GT(output.Size(), 0u);
123 sum_output_len_samples += output.Size();
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100124 EXPECT_EQ(0, statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200125 }
126 expand_.SetParametersForNormalAfterExpand();
127 // Convert |sum_output_len_samples| to milliseconds.
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100128 EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples),
129 statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200130}
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).
136TEST_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();
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100143 EXPECT_EQ(0, statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200144 }
145 expand_.SetParametersForMergeAfterExpand();
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100146 EXPECT_EQ(0, statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200147}
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.
153TEST_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 }
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100164 EXPECT_EQ(0, statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200165 }
166 expand_.SetParametersForNormalAfterExpand();
167 // Convert |sum_output_len_samples| to milliseconds.
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100168 EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples),
169 statistics_.last_outage_duration_samples());
Henrik Lundinbef77e22015-08-18 14:58:09 +0200170}
171
henrik.lundinf3995f72016-05-10 05:54:35 -0700172namespace {
173// Runs expand until Muted() returns true. Times out after 1000 calls.
174void ExpandUntilMuted(size_t num_channels, Expand* expand) {
175 EXPECT_FALSE(expand->Muted()) << "Instance is muted from the start";
176 AudioMultiVector output(num_channels);
177 int num_calls = 0;
178 while (!expand->Muted()) {
179 ASSERT_LT(num_calls++, 1000) << "Test timed out";
180 EXPECT_EQ(0, expand->Process(&output));
181 }
182}
183} // namespace
184
185// Verifies that Muted() returns true after a long expand period. Also verifies
186// that Muted() is reset to false after calling Reset(),
187// SetParametersForMergeAfterExpand() and SetParametersForNormalAfterExpand().
188TEST_F(ExpandTest, Muted) {
189 ExpandUntilMuted(num_channels_, &expand_);
190 expand_.Reset();
191 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
192
193 ExpandUntilMuted(num_channels_, &expand_);
194 expand_.SetParametersForMergeAfterExpand();
195 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
196
197 expand_.Reset(); // Must reset in order to start a new expand period.
198 ExpandUntilMuted(num_channels_, &expand_);
199 expand_.SetParametersForNormalAfterExpand();
200 EXPECT_FALSE(expand_.Muted()); // Should be back to unmuted.
201}
202
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203// TODO(hlundin): Write more tests.
204
205} // namespace webrtc