henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_coding/neteq/expand.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 23 | #include "test/testsupport/file_utils.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 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; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 33 | StatisticsCalculator statistics; |
| 34 | Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 35 | } |
| 36 | |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 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; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 43 | StatisticsCalculator statistics; |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 44 | ExpandFactory expand_factory; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 45 | Expand* expand = expand_factory.Create(&bgn, &sync_buffer, &random_vector, |
| 46 | &statistics, fs, channels); |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 47 | EXPECT_TRUE(expand != NULL); |
| 48 | delete expand; |
| 49 | } |
| 50 | |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 51 | namespace { |
| 52 | class FakeStatisticsCalculator : public StatisticsCalculator { |
| 53 | public: |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 54 | void LogDelayedPacketOutageEvent(int num_samples, int fs_hz) override { |
| 55 | last_outage_duration_samples_ = num_samples; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 58 | int last_outage_duration_samples() const { |
| 59 | return last_outage_duration_samples_; |
| 60 | } |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 61 | |
| 62 | private: |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 63 | int last_outage_duration_samples_ = 0; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // This is the same size that is given to the SyncBuffer object in NetEq. |
| 67 | const size_t kNetEqSyncBufferLengthMs = 720; |
| 68 | } // namespace |
| 69 | |
| 70 | class 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_) { |
| 86 | WebRtcSpl_Init(); |
| 87 | input_file_.set_output_rate_hz(test_sample_rate_hz_); |
| 88 | } |
| 89 | |
| 90 | void SetUp() override { |
| 91 | // Fast-forward the input file until there is speech (about 1.1 second into |
| 92 | // the file). |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 93 | const int speech_start_samples = |
| 94 | static_cast<int>(test_sample_rate_hz_ * 1.1f); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 95 | ASSERT_TRUE(input_file_.Seek(speech_start_samples)); |
| 96 | |
| 97 | // Pre-load the sync buffer with speech data. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 98 | std::unique_ptr<int16_t[]> temp(new int16_t[sync_buffer_.Size()]); |
| 99 | ASSERT_TRUE(input_file_.Read(sync_buffer_.Size(), temp.get())); |
| 100 | sync_buffer_.Channel(0).OverwriteAt(temp.get(), sync_buffer_.Size(), 0); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 101 | ASSERT_EQ(1u, num_channels_) << "Fix: Must populate all channels."; |
| 102 | } |
| 103 | |
| 104 | test::ResampleInputAudioFile input_file_; |
| 105 | int test_sample_rate_hz_; |
| 106 | size_t num_channels_; |
| 107 | BackgroundNoise background_noise_; |
| 108 | SyncBuffer sync_buffer_; |
| 109 | RandomVector random_vector_; |
| 110 | FakeStatisticsCalculator statistics_; |
| 111 | Expand expand_; |
| 112 | }; |
| 113 | |
| 114 | // This test calls the expand object to produce concealment data a few times, |
| 115 | // and then ends by calling SetParametersForNormalAfterExpand. This simulates |
| 116 | // the situation where the packet next up for decoding was just delayed, not |
| 117 | // lost. |
| 118 | TEST_F(ExpandTest, DelayedPacketOutage) { |
| 119 | AudioMultiVector output(num_channels_); |
| 120 | size_t sum_output_len_samples = 0; |
| 121 | for (int i = 0; i < 10; ++i) { |
| 122 | EXPECT_EQ(0, expand_.Process(&output)); |
| 123 | EXPECT_GT(output.Size(), 0u); |
| 124 | sum_output_len_samples += output.Size(); |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 125 | EXPECT_EQ(0, statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 126 | } |
| 127 | expand_.SetParametersForNormalAfterExpand(); |
| 128 | // Convert |sum_output_len_samples| to milliseconds. |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 129 | EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples), |
| 130 | statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // This test is similar to DelayedPacketOutage, but ends by calling |
| 134 | // SetParametersForMergeAfterExpand. This simulates the situation where the |
| 135 | // packet next up for decoding was actually lost (or at least a later packet |
| 136 | // arrived before it). |
| 137 | TEST_F(ExpandTest, LostPacketOutage) { |
| 138 | AudioMultiVector output(num_channels_); |
| 139 | size_t sum_output_len_samples = 0; |
| 140 | for (int i = 0; i < 10; ++i) { |
| 141 | EXPECT_EQ(0, expand_.Process(&output)); |
| 142 | EXPECT_GT(output.Size(), 0u); |
| 143 | sum_output_len_samples += output.Size(); |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 144 | EXPECT_EQ(0, statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 145 | } |
| 146 | expand_.SetParametersForMergeAfterExpand(); |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 147 | EXPECT_EQ(0, statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // This test is similar to the DelayedPacketOutage test above, but with the |
| 151 | // difference that Expand::Reset() is called after 5 calls to Expand::Process(). |
| 152 | // This should reset the statistics, and will in the end lead to an outage of |
| 153 | // 5 periods instead of 10. |
| 154 | TEST_F(ExpandTest, CheckOutageStatsAfterReset) { |
| 155 | AudioMultiVector output(num_channels_); |
| 156 | size_t sum_output_len_samples = 0; |
| 157 | for (int i = 0; i < 10; ++i) { |
| 158 | EXPECT_EQ(0, expand_.Process(&output)); |
| 159 | EXPECT_GT(output.Size(), 0u); |
| 160 | sum_output_len_samples += output.Size(); |
| 161 | if (i == 5) { |
| 162 | expand_.Reset(); |
| 163 | sum_output_len_samples = 0; |
| 164 | } |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 165 | EXPECT_EQ(0, statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 166 | } |
| 167 | expand_.SetParametersForNormalAfterExpand(); |
| 168 | // Convert |sum_output_len_samples| to milliseconds. |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 169 | EXPECT_EQ(rtc::checked_cast<int>(sum_output_len_samples), |
| 170 | statistics_.last_outage_duration_samples()); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 171 | } |
| 172 | |
henrik.lundin | f3995f7 | 2016-05-10 05:54:35 -0700 | [diff] [blame] | 173 | namespace { |
| 174 | // Runs expand until Muted() returns true. Times out after 1000 calls. |
| 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(). |
| 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 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | // TODO(hlundin): Write more tests. |
| 205 | |
| 206 | } // namespace webrtc |