blob: abae9857b71681891300c453e793f9bd65812fa5 [file] [log] [blame]
solenberg566ef242015-11-06 15:34:49 -08001/*
2 * Copyright (c) 2015 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
kwibergfffa42b2016-02-23 10:46:32 -080011#include <memory>
Fredrik Solenberg2a877972017-12-15 16:42:15 +010012#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "audio/audio_state.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010015#include "call/test/mock_audio_send_stream.h"
16#include "modules/audio_device/include/mock_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_mixer/audio_mixer_impl.h"
18#include "modules/audio_processing/include/mock_audio_processing.h"
19#include "test/gtest.h"
20#include "test/mock_voice_engine.h"
solenberg566ef242015-11-06 15:34:49 -080021
22namespace webrtc {
23namespace test {
24namespace {
25
Fredrik Solenberg2a877972017-12-15 16:42:15 +010026constexpr int kSampleRate = 16000;
27constexpr int kNumberOfChannels = 1;
aleloidd310712016-11-17 06:28:59 -080028
aleloi04c07222016-11-22 06:42:53 -080029struct ConfigHelper {
30 ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) {
aleloi04c07222016-11-22 06:42:53 -080031 audio_state_config.voice_engine = &mock_voice_engine;
32 audio_state_config.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -070033 audio_state_config.audio_processing =
Fredrik Solenberg2a877972017-12-15 16:42:15 +010034 new rtc::RefCountedObject<testing::NiceMock<MockAudioProcessing>>();
35 audio_state_config.audio_device_module =
36 new rtc::RefCountedObject<MockAudioDeviceModule>();
solenberg566ef242015-11-06 15:34:49 -080037 }
aleloi04c07222016-11-22 06:42:53 -080038 AudioState::Config& config() { return audio_state_config; }
39 MockVoiceEngine& voice_engine() { return mock_voice_engine; }
40 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
solenberg566ef242015-11-06 15:34:49 -080041
42 private:
aleloi04c07222016-11-22 06:42:53 -080043 testing::StrictMock<MockVoiceEngine> mock_voice_engine;
44 AudioState::Config audio_state_config;
45 rtc::scoped_refptr<AudioMixer> audio_mixer;
solenberg566ef242015-11-06 15:34:49 -080046};
aleloi04c07222016-11-22 06:42:53 -080047
48class FakeAudioSource : public AudioMixer::Source {
49 public:
50 // TODO(aleloi): Valid overrides commented out, because the gmock
51 // methods don't use any override declarations, and we want to avoid
52 // warnings from -Winconsistent-missing-override. See
53 // http://crbug.com/428099.
54 int Ssrc() const /*override*/ { return 0; }
55
56 int PreferredSampleRate() const /*override*/ { return kSampleRate; }
57
58 MOCK_METHOD2(GetAudioFrameWithInfo,
59 AudioFrameInfo(int sample_rate_hz, AudioFrame* audio_frame));
60};
61
Fredrik Solenberg2a877972017-12-15 16:42:15 +010062std::vector<int16_t> Create10msSilentTestData(int sample_rate_hz,
63 size_t num_channels) {
64 const int samples_per_channel = sample_rate_hz / 100;
65 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
66 return audio_data;
67}
68
69std::vector<int16_t> Create10msTestData(int sample_rate_hz,
70 size_t num_channels) {
71 const int samples_per_channel = sample_rate_hz / 100;
72 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
73 // Fill the first channel with a 1kHz sine wave.
74 const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz;
75 float w = 0.f;
76 for (int i = 0; i < samples_per_channel; ++i) {
77 audio_data[i * num_channels] =
78 static_cast<int16_t>(32767.f * std::sin(w));
79 w += inc;
80 }
81 return audio_data;
82}
83
84std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
85 const size_t num_channels = audio_frame->num_channels_;
86 const size_t samples_per_channel = audio_frame->samples_per_channel_;
87 std::vector<uint32_t> levels(num_channels, 0);
88 for (size_t i = 0; i < samples_per_channel; ++i) {
89 for (size_t j = 0; j < num_channels; ++j) {
90 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
91 }
92 }
93 return levels;
94}
solenberg566ef242015-11-06 15:34:49 -080095} // namespace
96
97TEST(AudioStateTest, Create) {
98 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010099 auto audio_state = AudioState::Create(helper.config());
solenberg566ef242015-11-06 15:34:49 -0800100 EXPECT_TRUE(audio_state.get());
101}
102
103TEST(AudioStateTest, ConstructDestruct) {
104 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -0800105 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -0800106 new internal::AudioState(helper.config()));
107}
108
109TEST(AudioStateTest, GetVoiceEngine) {
110 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -0800111 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -0800112 new internal::AudioState(helper.config()));
113 EXPECT_EQ(audio_state->voice_engine(), &helper.voice_engine());
114}
115
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100116TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
aleloidd310712016-11-17 06:28:59 -0800117 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100118 std::unique_ptr<internal::AudioState> audio_state(
119 new internal::AudioState(helper.config()));
aleloidd310712016-11-17 06:28:59 -0800120
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100121 MockAudioSendStream stream;
122 audio_state->AddSendingStream(&stream, 8000, 2);
aleloi04c07222016-11-22 06:42:53 -0800123
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100124 EXPECT_CALL(stream, SendAudioDataForMock(testing::AllOf(
125 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(8000)),
126 testing::Field(&AudioFrame::num_channels_, testing::Eq(2u)))))
127 .WillOnce(
128 // Verify that channels are not swapped by default.
129 testing::Invoke([](AudioFrame* audio_frame) {
130 auto levels = ComputeChannelLevels(audio_frame);
131 EXPECT_LT(0u, levels[0]);
132 EXPECT_EQ(0u, levels[1]);
133 }));
134 MockAudioProcessing* ap =
135 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
136 EXPECT_CALL(*ap, set_stream_delay_ms(0));
137 EXPECT_CALL(*ap, set_stream_key_pressed(false));
138 EXPECT_CALL(*ap, ProcessStream(testing::_));
aleloi04c07222016-11-22 06:42:53 -0800139
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100140 constexpr int kSampleRate = 16000;
141 constexpr size_t kNumChannels = 2;
142 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
143 uint32_t new_mic_level = 667;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100144 audio_state->audio_transport()->RecordedDataIsAvailable(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100145 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
146 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
147 EXPECT_EQ(667u, new_mic_level);
148
149 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800150}
151
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100152TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
aleloi04c07222016-11-22 06:42:53 -0800153 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100154 std::unique_ptr<internal::AudioState> audio_state(
155 new internal::AudioState(helper.config()));
aleloi04c07222016-11-22 06:42:53 -0800156
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100157 MockAudioSendStream stream_1;
158 MockAudioSendStream stream_2;
159 audio_state->AddSendingStream(&stream_1, 8001, 2);
160 audio_state->AddSendingStream(&stream_2, 32000, 1);
161
162 EXPECT_CALL(stream_1, SendAudioDataForMock(testing::AllOf(
163 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
164 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
165 .WillOnce(
166 // Verify that there is output signal.
167 testing::Invoke([](AudioFrame* audio_frame) {
168 auto levels = ComputeChannelLevels(audio_frame);
169 EXPECT_LT(0u, levels[0]);
170 }));
171 EXPECT_CALL(stream_2, SendAudioDataForMock(testing::AllOf(
172 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
173 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
174 .WillOnce(
175 // Verify that there is output signal.
176 testing::Invoke([](AudioFrame* audio_frame) {
177 auto levels = ComputeChannelLevels(audio_frame);
178 EXPECT_LT(0u, levels[0]);
179 }));
180 MockAudioProcessing* ap =
181 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
182 EXPECT_CALL(*ap, set_stream_delay_ms(5));
183 EXPECT_CALL(*ap, set_stream_key_pressed(true));
184 EXPECT_CALL(*ap, ProcessStream(testing::_));
185
186 constexpr int kSampleRate = 16000;
187 constexpr size_t kNumChannels = 1;
188 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
189 uint32_t new_mic_level = 667;
190 audio_state->audio_transport()->RecordedDataIsAvailable(
191 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
192 kNumChannels, kSampleRate, 5, 0, 0, true, new_mic_level);
193 EXPECT_EQ(667u, new_mic_level);
194
195 audio_state->RemoveSendingStream(&stream_1);
196 audio_state->RemoveSendingStream(&stream_2);
197}
198
199TEST(AudioStateTest, EnableChannelSwap) {
200 constexpr int kSampleRate = 16000;
201 constexpr size_t kNumChannels = 2;
202
203 ConfigHelper helper;
204 std::unique_ptr<internal::AudioState> audio_state(
205 new internal::AudioState(helper.config()));
206 audio_state->SetStereoChannelSwapping(true);
207
208 MockAudioSendStream stream;
209 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
210
211 EXPECT_CALL(stream, SendAudioDataForMock(testing::_))
212 .WillOnce(
213 // Verify that channels are swapped.
214 testing::Invoke([](AudioFrame* audio_frame) {
215 auto levels = ComputeChannelLevels(audio_frame);
216 EXPECT_EQ(0u, levels[0]);
217 EXPECT_LT(0u, levels[1]);
218 }));
219
220 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
221 uint32_t new_mic_level = 667;
222 audio_state->audio_transport()->RecordedDataIsAvailable(
223 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
224 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
225 EXPECT_EQ(667u, new_mic_level);
226
227 audio_state->RemoveSendingStream(&stream);
228}
229
230TEST(AudioStateTest, InputLevelStats) {
231 constexpr int kSampleRate = 16000;
232 constexpr size_t kNumChannels = 1;
233
234 ConfigHelper helper;
235 std::unique_ptr<internal::AudioState> audio_state(
236 new internal::AudioState(helper.config()));
237
238 // Push a silent buffer -> Level stats should be zeros except for duration.
239 {
240 auto audio_data = Create10msSilentTestData(kSampleRate, kNumChannels);
241 uint32_t new_mic_level = 667;
242 audio_state->audio_transport()->RecordedDataIsAvailable(
243 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
244 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
245 auto stats = audio_state->GetAudioInputStats();
246 EXPECT_EQ(0, stats.audio_level);
247 EXPECT_EQ(0, stats.quantized_audio_level);
248 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.0));
249 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.01));
250 }
251
252 // Push 10 non-silent buffers -> Level stats should be non-zero.
253 {
254 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
255 uint32_t new_mic_level = 667;
256 for (int i = 0; i < 10; ++i) {
257 audio_state->audio_transport()->RecordedDataIsAvailable(
258 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
259 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
260 }
261 auto stats = audio_state->GetAudioInputStats();
262 EXPECT_EQ(32767, stats.audio_level);
263 EXPECT_EQ(9, stats.quantized_audio_level);
264 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.01));
265 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.11));
266 }
267}
268
269TEST(AudioStateTest,
270 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
271 ConfigHelper helper;
272 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800273
274 FakeAudioSource fake_source;
aleloi04c07222016-11-22 06:42:53 -0800275 helper.mixer()->AddSource(&fake_source);
276
277 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_))
278 .WillOnce(
279 testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
280 audio_frame->sample_rate_hz_ = sample_rate_hz;
281 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
282 audio_frame->num_channels_ = kNumberOfChannels;
283 return AudioMixer::Source::AudioFrameInfo::kNormal;
aleloidd310712016-11-17 06:28:59 -0800284 }));
285
aleloi04c07222016-11-22 06:42:53 -0800286 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
287 size_t n_samples_out;
288 int64_t elapsed_time_ms;
289 int64_t ntp_time_ms;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100290 audio_state->audio_transport()->NeedMorePlayData(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100291 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
aleloi04c07222016-11-22 06:42:53 -0800292 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800293}
solenberg566ef242015-11-06 15:34:49 -0800294} // namespace test
295} // namespace webrtc