andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_processing_impl.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/test/test_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "test/gmock.h" |
| 15 | #include "test/gtest.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 16 | |
| 17 | using ::testing::Invoke; |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 20 | namespace { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 21 | |
| 22 | class MockInitialize : public AudioProcessingImpl { |
| 23 | public: |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 24 | explicit MockInitialize(const webrtc::Config& config) |
| 25 | : AudioProcessingImpl(config) {} |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 26 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 27 | MOCK_METHOD0(InitializeLocked, int()); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 28 | int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 29 | return AudioProcessingImpl::InitializeLocked(); |
| 30 | } |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 31 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 32 | MOCK_CONST_METHOD0(AddRef, void()); |
| 33 | MOCK_CONST_METHOD0(Release, rtc::RefCountReleaseStatus()); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 36 | void GenerateFixedFrame(int16_t audio_level, |
| 37 | size_t input_rate, |
| 38 | size_t num_channels, |
| 39 | AudioFrame* fixed_frame) { |
| 40 | const size_t samples_per_input_channel = rtc::CheckedDivExact( |
| 41 | input_rate, static_cast<size_t>(rtc::CheckedDivExact( |
| 42 | 1000, AudioProcessing::kChunkSizeMs))); |
| 43 | fixed_frame->samples_per_channel_ = samples_per_input_channel; |
| 44 | fixed_frame->sample_rate_hz_ = input_rate; |
| 45 | fixed_frame->num_channels_ = num_channels; |
| 46 | |
| 47 | RTC_DCHECK_LE(samples_per_input_channel * num_channels, |
| 48 | AudioFrame::kMaxDataSizeSamples); |
| 49 | for (size_t i = 0; i < samples_per_input_channel * num_channels; ++i) { |
| 50 | fixed_frame->mutable_data()[i] = audio_level; |
| 51 | } |
| 52 | } |
| 53 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 54 | } // namespace |
| 55 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 56 | TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 57 | webrtc::Config config; |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 58 | MockInitialize mock(config); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 59 | ON_CALL(mock, InitializeLocked()) |
| 60 | .WillByDefault(Invoke(&mock, &MockInitialize::RealInitializeLocked)); |
| 61 | |
| 62 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
| 63 | mock.Initialize(); |
| 64 | |
| 65 | AudioFrame frame; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 66 | // Call with the default parameters; there should be an init. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 67 | frame.num_channels_ = 1; |
| 68 | SetFrameSampleRate(&frame, 16000); |
Per Åhgren | 4bdced5 | 2017-06-27 16:00:38 +0200 | [diff] [blame] | 69 | EXPECT_CALL(mock, InitializeLocked()).Times(0); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 70 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 71 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 72 | |
| 73 | // New sample rate. (Only impacts ProcessStream). |
| 74 | SetFrameSampleRate(&frame, 32000); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 75 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 76 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 77 | |
| 78 | // New number of channels. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 79 | // TODO(peah): Investigate why this causes 2 inits. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 80 | frame.num_channels_ = 2; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 81 | EXPECT_CALL(mock, InitializeLocked()).Times(2); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 82 | EXPECT_NOERR(mock.ProcessStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 83 | // ProcessStream sets num_channels_ == num_output_channels. |
| 84 | frame.num_channels_ = 2; |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 85 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 86 | |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 87 | // A new sample rate passed to ProcessReverseStream should cause an init. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 88 | SetFrameSampleRate(&frame, 16000); |
Alex Luebs | 5b830fe | 2016-03-08 17:52:52 +0100 | [diff] [blame] | 89 | EXPECT_CALL(mock, InitializeLocked()).Times(1); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 90 | EXPECT_NOERR(mock.ProcessReverseStream(&frame)); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 93 | TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) { |
Alex Loiko | b5c9a79 | 2018-04-16 16:31:22 +0200 | [diff] [blame] | 94 | std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create()); |
| 95 | webrtc::AudioProcessing::Config apm_config; |
| 96 | apm_config.pre_amplifier.enabled = true; |
| 97 | apm_config.pre_amplifier.fixed_gain_factor = 1.f; |
| 98 | apm->ApplyConfig(apm_config); |
| 99 | |
| 100 | AudioFrame frame; |
| 101 | constexpr int16_t audio_level = 10000; |
| 102 | constexpr size_t input_rate = 48000; |
| 103 | constexpr size_t num_channels = 2; |
| 104 | |
| 105 | GenerateFixedFrame(audio_level, input_rate, num_channels, &frame); |
| 106 | apm->ProcessStream(&frame); |
| 107 | EXPECT_EQ(frame.data()[100], audio_level) |
| 108 | << "With factor 1, frame shouldn't be modified."; |
| 109 | |
| 110 | constexpr float gain_factor = 2.f; |
| 111 | apm->SetRuntimeSetting( |
| 112 | AudioProcessing::RuntimeSetting::CreateCapturePreGain(gain_factor)); |
| 113 | |
| 114 | // Process for two frames to have time to ramp up gain. |
| 115 | for (int i = 0; i < 2; ++i) { |
| 116 | GenerateFixedFrame(audio_level, input_rate, num_channels, &frame); |
| 117 | apm->ProcessStream(&frame); |
| 118 | } |
| 119 | EXPECT_EQ(frame.data()[100], gain_factor * audio_level) |
| 120 | << "Frame should be amplified."; |
Alessio Bazzica | c054e78 | 2018-04-16 12:10:09 +0200 | [diff] [blame] | 121 | } |
| 122 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 123 | } // namespace webrtc |