blob: dc622df00de5b1081c21ed352b9cd9306c572347 [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) {
Yves Gerey665174f2018-06-19 15:03:05 +020074 audio_data[i * num_channels] = static_cast<int16_t>(32767.f * std::sin(w));
Fredrik Solenberg2a877972017-12-15 16:42:15 +010075 w += inc;
76 }
77 return audio_data;
78}
79
80std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
81 const size_t num_channels = audio_frame->num_channels_;
82 const size_t samples_per_channel = audio_frame->samples_per_channel_;
83 std::vector<uint32_t> levels(num_channels, 0);
84 for (size_t i = 0; i < samples_per_channel; ++i) {
85 for (size_t j = 0; j < num_channels; ++j) {
86 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
87 }
88 }
89 return levels;
90}
solenberg566ef242015-11-06 15:34:49 -080091} // namespace
92
93TEST(AudioStateTest, Create) {
94 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010095 auto audio_state = AudioState::Create(helper.config());
solenberg566ef242015-11-06 15:34:49 -080096 EXPECT_TRUE(audio_state.get());
97}
98
99TEST(AudioStateTest, ConstructDestruct) {
100 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -0800101 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -0800102 new internal::AudioState(helper.config()));
103}
104
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100105TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
aleloidd310712016-11-17 06:28:59 -0800106 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100107 std::unique_ptr<internal::AudioState> audio_state(
108 new internal::AudioState(helper.config()));
aleloidd310712016-11-17 06:28:59 -0800109
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100110 MockAudioSendStream stream;
111 audio_state->AddSendingStream(&stream, 8000, 2);
aleloi04c07222016-11-22 06:42:53 -0800112
Yves Gerey665174f2018-06-19 15:03:05 +0200113 EXPECT_CALL(
114 stream,
115 SendAudioDataForMock(testing::AllOf(
116 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(8000)),
117 testing::Field(&AudioFrame::num_channels_, testing::Eq(2u)))))
118 .WillOnce(
119 // Verify that channels are not swapped by default.
120 testing::Invoke([](AudioFrame* audio_frame) {
121 auto levels = ComputeChannelLevels(audio_frame);
122 EXPECT_LT(0u, levels[0]);
123 EXPECT_EQ(0u, levels[1]);
124 }));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100125 MockAudioProcessing* ap =
126 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
127 EXPECT_CALL(*ap, set_stream_delay_ms(0));
128 EXPECT_CALL(*ap, set_stream_key_pressed(false));
129 EXPECT_CALL(*ap, ProcessStream(testing::_));
aleloi04c07222016-11-22 06:42:53 -0800130
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100131 constexpr int kSampleRate = 16000;
132 constexpr size_t kNumChannels = 2;
133 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
134 uint32_t new_mic_level = 667;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100135 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200136 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
137 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100138 EXPECT_EQ(667u, new_mic_level);
139
140 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800141}
142
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100143TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
aleloi04c07222016-11-22 06:42:53 -0800144 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100145 std::unique_ptr<internal::AudioState> audio_state(
146 new internal::AudioState(helper.config()));
aleloi04c07222016-11-22 06:42:53 -0800147
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100148 MockAudioSendStream stream_1;
149 MockAudioSendStream stream_2;
150 audio_state->AddSendingStream(&stream_1, 8001, 2);
151 audio_state->AddSendingStream(&stream_2, 32000, 1);
152
Yves Gerey665174f2018-06-19 15:03:05 +0200153 EXPECT_CALL(
154 stream_1,
155 SendAudioDataForMock(testing::AllOf(
156 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
157 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
158 .WillOnce(
159 // Verify that there is output signal.
160 testing::Invoke([](AudioFrame* audio_frame) {
161 auto levels = ComputeChannelLevels(audio_frame);
162 EXPECT_LT(0u, levels[0]);
163 }));
164 EXPECT_CALL(
165 stream_2,
166 SendAudioDataForMock(testing::AllOf(
167 testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)),
168 testing::Field(&AudioFrame::num_channels_, testing::Eq(1u)))))
169 .WillOnce(
170 // Verify that there is output signal.
171 testing::Invoke([](AudioFrame* audio_frame) {
172 auto levels = ComputeChannelLevels(audio_frame);
173 EXPECT_LT(0u, levels[0]);
174 }));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100175 MockAudioProcessing* ap =
176 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
177 EXPECT_CALL(*ap, set_stream_delay_ms(5));
178 EXPECT_CALL(*ap, set_stream_key_pressed(true));
179 EXPECT_CALL(*ap, ProcessStream(testing::_));
180
181 constexpr int kSampleRate = 16000;
182 constexpr size_t kNumChannels = 1;
183 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
184 uint32_t new_mic_level = 667;
185 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200186 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
187 kSampleRate, 5, 0, 0, true, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100188 EXPECT_EQ(667u, new_mic_level);
189
190 audio_state->RemoveSendingStream(&stream_1);
191 audio_state->RemoveSendingStream(&stream_2);
192}
193
194TEST(AudioStateTest, EnableChannelSwap) {
195 constexpr int kSampleRate = 16000;
196 constexpr size_t kNumChannels = 2;
197
198 ConfigHelper helper;
199 std::unique_ptr<internal::AudioState> audio_state(
200 new internal::AudioState(helper.config()));
201 audio_state->SetStereoChannelSwapping(true);
202
203 MockAudioSendStream stream;
204 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
205
206 EXPECT_CALL(stream, SendAudioDataForMock(testing::_))
207 .WillOnce(
208 // Verify that channels are swapped.
209 testing::Invoke([](AudioFrame* audio_frame) {
210 auto levels = ComputeChannelLevels(audio_frame);
211 EXPECT_EQ(0u, levels[0]);
212 EXPECT_LT(0u, levels[1]);
213 }));
214
215 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
216 uint32_t new_mic_level = 667;
217 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200218 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
219 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100220 EXPECT_EQ(667u, new_mic_level);
221
222 audio_state->RemoveSendingStream(&stream);
223}
224
225TEST(AudioStateTest, InputLevelStats) {
226 constexpr int kSampleRate = 16000;
227 constexpr size_t kNumChannels = 1;
228
229 ConfigHelper helper;
230 std::unique_ptr<internal::AudioState> audio_state(
231 new internal::AudioState(helper.config()));
232
233 // Push a silent buffer -> Level stats should be zeros except for duration.
234 {
235 auto audio_data = Create10msSilentTestData(kSampleRate, kNumChannels);
236 uint32_t new_mic_level = 667;
237 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200238 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
239 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100240 auto stats = audio_state->GetAudioInputStats();
241 EXPECT_EQ(0, stats.audio_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100242 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.0));
243 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.01));
244 }
245
246 // Push 10 non-silent buffers -> Level stats should be non-zero.
247 {
248 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
249 uint32_t new_mic_level = 667;
250 for (int i = 0; i < 10; ++i) {
251 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200252 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
253 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100254 }
255 auto stats = audio_state->GetAudioInputStats();
256 EXPECT_EQ(32767, stats.audio_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100257 EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.01));
258 EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.11));
259 }
260}
261
262TEST(AudioStateTest,
263 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
264 ConfigHelper helper;
265 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800266
267 FakeAudioSource fake_source;
aleloi04c07222016-11-22 06:42:53 -0800268 helper.mixer()->AddSource(&fake_source);
269
270 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_))
271 .WillOnce(
272 testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
273 audio_frame->sample_rate_hz_ = sample_rate_hz;
274 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
275 audio_frame->num_channels_ = kNumberOfChannels;
276 return AudioMixer::Source::AudioFrameInfo::kNormal;
aleloidd310712016-11-17 06:28:59 -0800277 }));
278
aleloi04c07222016-11-22 06:42:53 -0800279 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
280 size_t n_samples_out;
281 int64_t elapsed_time_ms;
282 int64_t ntp_time_ms;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100283 audio_state->audio_transport()->NeedMorePlayData(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100284 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
aleloi04c07222016-11-22 06:42:53 -0800285 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800286}
solenberg566ef242015-11-06 15:34:49 -0800287} // namespace test
288} // namespace webrtc