blob: b4452add9a113972a77a1e5af87d8d48fb3a6992 [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"
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010019#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "test/gtest.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.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -070032 audio_state_config.audio_processing =
Fredrik Solenberg2a877972017-12-15 16:42:15 +010033 new rtc::RefCountedObject<testing::NiceMock<MockAudioProcessing>>();
34 audio_state_config.audio_device_module =
35 new rtc::RefCountedObject<MockAudioDeviceModule>();
solenberg566ef242015-11-06 15:34:49 -080036 }
aleloi04c07222016-11-22 06:42:53 -080037 AudioState::Config& config() { return audio_state_config; }
aleloi04c07222016-11-22 06:42:53 -080038 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
solenberg566ef242015-11-06 15:34:49 -080039
40 private:
aleloi04c07222016-11-22 06:42:53 -080041 AudioState::Config audio_state_config;
42 rtc::scoped_refptr<AudioMixer> audio_mixer;
solenberg566ef242015-11-06 15:34:49 -080043};
aleloi04c07222016-11-22 06:42:53 -080044
45class FakeAudioSource : public AudioMixer::Source {
46 public:
47 // TODO(aleloi): Valid overrides commented out, because the gmock
48 // methods don't use any override declarations, and we want to avoid
49 // warnings from -Winconsistent-missing-override. See
50 // http://crbug.com/428099.
51 int Ssrc() const /*override*/ { return 0; }
52
53 int PreferredSampleRate() const /*override*/ { return kSampleRate; }
54
55 MOCK_METHOD2(GetAudioFrameWithInfo,
56 AudioFrameInfo(int sample_rate_hz, AudioFrame* audio_frame));
57};
58
Fredrik Solenberg2a877972017-12-15 16:42:15 +010059std::vector<int16_t> Create10msSilentTestData(int sample_rate_hz,
60 size_t num_channels) {
61 const int samples_per_channel = sample_rate_hz / 100;
62 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
63 return audio_data;
64}
65
66std::vector<int16_t> Create10msTestData(int sample_rate_hz,
67 size_t num_channels) {
68 const int samples_per_channel = sample_rate_hz / 100;
69 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
70 // Fill the first channel with a 1kHz sine wave.
71 const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz;
72 float w = 0.f;
73 for (int i = 0; i < samples_per_channel; ++i) {
74 audio_data[i * num_channels] =
75 static_cast<int16_t>(32767.f * std::sin(w));
76 w += inc;
77 }
78 return audio_data;
79}
80
81std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
82 const size_t num_channels = audio_frame->num_channels_;
83 const size_t samples_per_channel = audio_frame->samples_per_channel_;
84 std::vector<uint32_t> levels(num_channels, 0);
85 for (size_t i = 0; i < samples_per_channel; ++i) {
86 for (size_t j = 0; j < num_channels; ++j) {
87 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
88 }
89 }
90 return levels;
91}
solenberg566ef242015-11-06 15:34:49 -080092} // namespace
93
94TEST(AudioStateTest, Create) {
95 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010096 auto audio_state = AudioState::Create(helper.config());
solenberg566ef242015-11-06 15:34:49 -080097 EXPECT_TRUE(audio_state.get());
98}
99
100TEST(AudioStateTest, ConstructDestruct) {
101 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -0800102 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -0800103 new internal::AudioState(helper.config()));
104}
105
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100106TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
aleloidd310712016-11-17 06:28:59 -0800107 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100108 std::unique_ptr<internal::AudioState> audio_state(
109 new internal::AudioState(helper.config()));
aleloidd310712016-11-17 06:28:59 -0800110
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100111 MockAudioSendStream stream;
112 audio_state->AddSendingStream(&stream, 8000, 2);
aleloi04c07222016-11-22 06:42:53 -0800113
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100114 EXPECT_CALL(stream, SendAudioDataForMock(testing::AllOf(
115 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(8000)),
116 testing::Field(&AudioFrame::num_channels_, testing::Eq(2u)))))
117 .WillOnce(
118 // Verify that channels are not swapped by default.
119 testing::Invoke([](AudioFrame* audio_frame) {
120 auto levels = ComputeChannelLevels(audio_frame);
121 EXPECT_LT(0u, levels[0]);
122 EXPECT_EQ(0u, levels[1]);
123 }));
124 MockAudioProcessing* ap =
125 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
126 EXPECT_CALL(*ap, set_stream_delay_ms(0));
127 EXPECT_CALL(*ap, set_stream_key_pressed(false));
128 EXPECT_CALL(*ap, ProcessStream(testing::_));
aleloi04c07222016-11-22 06:42:53 -0800129
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100130 constexpr int kSampleRate = 16000;
131 constexpr size_t kNumChannels = 2;
132 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
133 uint32_t new_mic_level = 667;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100134 audio_state->audio_transport()->RecordedDataIsAvailable(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100135 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
136 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
137 EXPECT_EQ(667u, new_mic_level);
138
139 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800140}
141
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100142TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
aleloi04c07222016-11-22 06:42:53 -0800143 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100144 std::unique_ptr<internal::AudioState> audio_state(
145 new internal::AudioState(helper.config()));
aleloi04c07222016-11-22 06:42:53 -0800146
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100147 MockAudioSendStream stream_1;
148 MockAudioSendStream stream_2;
149 audio_state->AddSendingStream(&stream_1, 8001, 2);
150 audio_state->AddSendingStream(&stream_2, 32000, 1);
151
152 EXPECT_CALL(stream_1, SendAudioDataForMock(testing::AllOf(
153 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
154 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
155 .WillOnce(
156 // Verify that there is output signal.
157 testing::Invoke([](AudioFrame* audio_frame) {
158 auto levels = ComputeChannelLevels(audio_frame);
159 EXPECT_LT(0u, levels[0]);
160 }));
161 EXPECT_CALL(stream_2, SendAudioDataForMock(testing::AllOf(
162 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
163 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
164 .WillOnce(
165 // Verify that there is output signal.
166 testing::Invoke([](AudioFrame* audio_frame) {
167 auto levels = ComputeChannelLevels(audio_frame);
168 EXPECT_LT(0u, levels[0]);
169 }));
170 MockAudioProcessing* ap =
171 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
172 EXPECT_CALL(*ap, set_stream_delay_ms(5));
173 EXPECT_CALL(*ap, set_stream_key_pressed(true));
174 EXPECT_CALL(*ap, ProcessStream(testing::_));
175
176 constexpr int kSampleRate = 16000;
177 constexpr size_t kNumChannels = 1;
178 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
179 uint32_t new_mic_level = 667;
180 audio_state->audio_transport()->RecordedDataIsAvailable(
181 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
182 kNumChannels, kSampleRate, 5, 0, 0, true, new_mic_level);
183 EXPECT_EQ(667u, new_mic_level);
184
185 audio_state->RemoveSendingStream(&stream_1);
186 audio_state->RemoveSendingStream(&stream_2);
187}
188
189TEST(AudioStateTest, EnableChannelSwap) {
190 constexpr int kSampleRate = 16000;
191 constexpr size_t kNumChannels = 2;
192
193 ConfigHelper helper;
194 std::unique_ptr<internal::AudioState> audio_state(
195 new internal::AudioState(helper.config()));
196 audio_state->SetStereoChannelSwapping(true);
197
198 MockAudioSendStream stream;
199 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
200
201 EXPECT_CALL(stream, SendAudioDataForMock(testing::_))
202 .WillOnce(
203 // Verify that channels are swapped.
204 testing::Invoke([](AudioFrame* audio_frame) {
205 auto levels = ComputeChannelLevels(audio_frame);
206 EXPECT_EQ(0u, levels[0]);
207 EXPECT_LT(0u, levels[1]);
208 }));
209
210 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
211 uint32_t new_mic_level = 667;
212 audio_state->audio_transport()->RecordedDataIsAvailable(
213 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
214 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
215 EXPECT_EQ(667u, new_mic_level);
216
217 audio_state->RemoveSendingStream(&stream);
218}
219
220TEST(AudioStateTest, InputLevelStats) {
221 constexpr int kSampleRate = 16000;
222 constexpr size_t kNumChannels = 1;
223
224 ConfigHelper helper;
225 std::unique_ptr<internal::AudioState> audio_state(
226 new internal::AudioState(helper.config()));
227
228 // Push a silent buffer -> Level stats should be zeros except for duration.
229 {
230 auto audio_data = Create10msSilentTestData(kSampleRate, kNumChannels);
231 uint32_t new_mic_level = 667;
232 audio_state->audio_transport()->RecordedDataIsAvailable(
233 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
234 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
235 auto stats = audio_state->GetAudioInputStats();
236 EXPECT_EQ(0, stats.audio_level);
237 EXPECT_EQ(0, stats.quantized_audio_level);
238 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.0));
239 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.01));
240 }
241
242 // Push 10 non-silent buffers -> Level stats should be non-zero.
243 {
244 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
245 uint32_t new_mic_level = 667;
246 for (int i = 0; i < 10; ++i) {
247 audio_state->audio_transport()->RecordedDataIsAvailable(
248 &audio_data[0], kSampleRate / 100, kNumChannels * 2,
249 kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level);
250 }
251 auto stats = audio_state->GetAudioInputStats();
252 EXPECT_EQ(32767, stats.audio_level);
253 EXPECT_EQ(9, stats.quantized_audio_level);
254 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.01));
255 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.11));
256 }
257}
258
259TEST(AudioStateTest,
260 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
261 ConfigHelper helper;
262 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800263
264 FakeAudioSource fake_source;
aleloi04c07222016-11-22 06:42:53 -0800265 helper.mixer()->AddSource(&fake_source);
266
267 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_))
268 .WillOnce(
269 testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
270 audio_frame->sample_rate_hz_ = sample_rate_hz;
271 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
272 audio_frame->num_channels_ = kNumberOfChannels;
273 return AudioMixer::Source::AudioFrameInfo::kNormal;
aleloidd310712016-11-17 06:28:59 -0800274 }));
275
aleloi04c07222016-11-22 06:42:53 -0800276 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
277 size_t n_samples_out;
278 int64_t elapsed_time_ms;
279 int64_t ntp_time_ms;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100280 audio_state->audio_transport()->NeedMorePlayData(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100281 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
aleloi04c07222016-11-22 06:42:53 -0800282 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800283}
solenberg566ef242015-11-06 15:34:49 -0800284} // namespace test
285} // namespace webrtc