normal_unittest.cc (5233B)
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 Normal class. 12 13 #include "modules/audio_coding/neteq/normal.h" 14 15 #include <cstddef> 16 #include <cstdint> 17 18 #include "api/neteq/neteq.h" 19 #include "api/neteq/tick_timer.h" 20 #include "modules/audio_coding/neteq/audio_multi_vector.h" 21 #include "modules/audio_coding/neteq/background_noise.h" 22 #include "modules/audio_coding/neteq/expand.h" 23 #include "modules/audio_coding/neteq/mock/mock_decoder_database.h" 24 #include "modules/audio_coding/neteq/mock/mock_expand.h" 25 #include "modules/audio_coding/neteq/random_vector.h" 26 #include "modules/audio_coding/neteq/statistics_calculator.h" 27 #include "modules/audio_coding/neteq/sync_buffer.h" 28 #include "test/gmock.h" 29 #include "test/gtest.h" 30 31 using ::testing::_; 32 using ::testing::Invoke; 33 34 namespace webrtc { 35 36 namespace { 37 38 int ExpandProcess120ms(AudioMultiVector* output) { 39 AudioMultiVector dummy_audio(1, 11520u); 40 dummy_audio.CopyTo(output); 41 return 0; 42 } 43 44 } // namespace 45 46 TEST(Normal, CreateAndDestroy) { 47 MockDecoderDatabase db; 48 int fs = 8000; 49 size_t channels = 1; 50 BackgroundNoise bgn(channels); 51 SyncBuffer sync_buffer(1, 1000); 52 RandomVector random_vector; 53 TickTimer timer; 54 StatisticsCalculator statistics(&timer); 55 Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels); 56 Normal normal(fs, &db, bgn, &expand, &statistics); 57 EXPECT_CALL(db, Die()); // Called when `db` goes out of scope. 58 } 59 60 TEST(Normal, AvoidDivideByZero) { 61 MockDecoderDatabase db; 62 int fs = 8000; 63 size_t channels = 1; 64 BackgroundNoise bgn(channels); 65 SyncBuffer sync_buffer(1, 1000); 66 RandomVector random_vector; 67 TickTimer timer; 68 StatisticsCalculator statistics(&timer); 69 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, 70 channels); 71 Normal normal(fs, &db, bgn, &expand, &statistics); 72 73 int16_t input[1000] = {0}; 74 AudioMultiVector output(channels); 75 76 // Zero input length. 77 EXPECT_EQ(0, normal.Process(input, 0, NetEq::Mode::kExpand, &output)); 78 EXPECT_EQ(0u, output.Size()); 79 80 // Try to make energy_length >> scaling = 0; 81 EXPECT_CALL(expand, SetParametersForNormalAfterExpand()); 82 EXPECT_CALL(expand, Process(_)); 83 EXPECT_CALL(expand, Reset()); 84 // If input_size_samples < 64, then energy_length in Normal::Process() will 85 // be equal to input_size_samples. Since the input is all zeros, decoded_max 86 // will be zero, and scaling will be >= 6. Thus, energy_length >> scaling = 0, 87 // and using this as a denominator would lead to problems. 88 int input_size_samples = 63; 89 EXPECT_EQ(input_size_samples, normal.Process(input, input_size_samples, 90 NetEq::Mode::kExpand, &output)); 91 92 EXPECT_CALL(db, Die()); // Called when `db` goes out of scope. 93 EXPECT_CALL(expand, Die()); // Called when `expand` goes out of scope. 94 } 95 96 TEST(Normal, InputLengthAndChannelsDoNotMatch) { 97 MockDecoderDatabase db; 98 int fs = 8000; 99 size_t channels = 2; 100 BackgroundNoise bgn(channels); 101 SyncBuffer sync_buffer(channels, 1000); 102 RandomVector random_vector; 103 TickTimer timer; 104 StatisticsCalculator statistics(&timer); 105 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, 106 channels); 107 Normal normal(fs, &db, bgn, &expand, &statistics); 108 109 int16_t input[1000] = {0}; 110 AudioMultiVector output(channels); 111 112 // Let the number of samples be one sample less than 80 samples per channel. 113 size_t input_len = 80 * channels - 1; 114 EXPECT_EQ(0, normal.Process(input, input_len, NetEq::Mode::kExpand, &output)); 115 EXPECT_EQ(0u, output.Size()); 116 117 EXPECT_CALL(db, Die()); // Called when `db` goes out of scope. 118 EXPECT_CALL(expand, Die()); // Called when `expand` goes out of scope. 119 } 120 121 TEST(Normal, LastModeExpand120msPacket) { 122 MockDecoderDatabase db; 123 const int kFs = 48000; 124 const size_t kPacketsizeBytes = 11520u; 125 const size_t kChannels = 1; 126 BackgroundNoise bgn(kChannels); 127 SyncBuffer sync_buffer(kChannels, 1000); 128 RandomVector random_vector; 129 TickTimer timer; 130 StatisticsCalculator statistics(&timer); 131 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, kFs, 132 kChannels); 133 Normal normal(kFs, &db, bgn, &expand, &statistics); 134 135 int16_t input[kPacketsizeBytes] = {0}; 136 AudioMultiVector output(kChannels); 137 138 EXPECT_CALL(expand, SetParametersForNormalAfterExpand()); 139 EXPECT_CALL(expand, Process(_)).WillOnce(Invoke(ExpandProcess120ms)); 140 EXPECT_CALL(expand, Reset()); 141 EXPECT_EQ( 142 static_cast<int>(kPacketsizeBytes), 143 normal.Process(input, kPacketsizeBytes, NetEq::Mode::kExpand, &output)); 144 145 EXPECT_EQ(kPacketsizeBytes, output.Size()); 146 147 EXPECT_CALL(db, Die()); // Called when `db` goes out of scope. 148 EXPECT_CALL(expand, Die()); // Called when `expand` goes out of scope. 149 } 150 151 // TODO(hlundin): Write more tests. 152 153 } // namespace webrtc