blob: 824a2ee2b4c624e1c9ae472c817906525b51714e [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "audio/audio_state.h"
12
kwibergfffa42b2016-02-23 10:46:32 -080013#include <memory>
Olga Sharonova09ceed22020-09-30 18:27:39 +020014#include <utility>
Fredrik Solenberg2a877972017-12-15 16:42:15 +010015#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080016
Fredrik Solenberg2a877972017-12-15 16:42:15 +010017#include "call/test/mock_audio_send_stream.h"
18#include "modules/audio_device/include/mock_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_mixer/audio_mixer_impl.h"
20#include "modules/audio_processing/include/mock_audio_processing.h"
21#include "test/gtest.h"
solenberg566ef242015-11-06 15:34:49 -080022
23namespace webrtc {
24namespace test {
25namespace {
26
Per Åhgren71652f42020-03-17 13:23:58 +010027using ::testing::_;
Per Åhgrendc5522b2020-03-19 14:55:58 +010028using ::testing::Matcher;
Olga Sharonova09ceed22020-09-30 18:27:39 +020029using ::testing::NiceMock;
30using ::testing::StrictMock;
31using ::testing::Values;
Per Åhgren71652f42020-03-17 13:23:58 +010032
Fredrik Solenberg2a877972017-12-15 16:42:15 +010033constexpr int kSampleRate = 16000;
34constexpr int kNumberOfChannels = 1;
aleloidd310712016-11-17 06:28:59 -080035
Olga Sharonova09ceed22020-09-30 18:27:39 +020036struct FakeAsyncAudioProcessingHelper {
37 class FakeTaskQueue : public StrictMock<TaskQueueBase> {
38 public:
39 FakeTaskQueue() = default;
40
41 void Delete() override { delete this; }
42 void PostTask(std::unique_ptr<QueuedTask> task) override {
43 std::move(task)->Run();
44 }
45 MOCK_METHOD(void,
46 PostDelayedTask,
47 (std::unique_ptr<QueuedTask> task, uint32_t milliseconds),
48 (override));
49 };
50
51 class FakeTaskQueueFactory : public TaskQueueFactory {
52 public:
53 FakeTaskQueueFactory() = default;
54 ~FakeTaskQueueFactory() override = default;
55 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
56 absl::string_view name,
57 Priority priority) const override {
58 return std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>(
59 new FakeTaskQueue());
60 }
61 };
62
63 class MockAudioFrameProcessor : public AudioFrameProcessor {
64 public:
65 ~MockAudioFrameProcessor() override = default;
66
67 MOCK_METHOD(void, ProcessCalled, ());
68 MOCK_METHOD(void, SinkSet, ());
69 MOCK_METHOD(void, SinkCleared, ());
70
71 void Process(std::unique_ptr<AudioFrame> frame) override {
72 ProcessCalled();
73 sink_callback_(std::move(frame));
74 }
75
76 void SetSink(OnAudioFrameCallback sink_callback) override {
77 sink_callback_ = std::move(sink_callback);
78 if (sink_callback_ == nullptr)
79 SinkCleared();
80 else
81 SinkSet();
82 }
83
84 private:
85 OnAudioFrameCallback sink_callback_;
86 };
87
88 NiceMock<MockAudioFrameProcessor> audio_frame_processor_;
89 FakeTaskQueueFactory task_queue_factory_;
90
91 rtc::scoped_refptr<AsyncAudioProcessing::Factory> CreateFactory() {
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +020092 return rtc::make_ref_counted<AsyncAudioProcessing::Factory>(
Olga Sharonova09ceed22020-09-30 18:27:39 +020093 audio_frame_processor_, task_queue_factory_);
94 }
95};
96
aleloi04c07222016-11-22 06:42:53 -080097struct ConfigHelper {
Olga Sharonova09ceed22020-09-30 18:27:39 +020098 struct Params {
99 bool use_null_audio_processing;
100 bool use_async_audio_processing;
101 };
102
103 explicit ConfigHelper(const Params& params)
Per Åhgrencc73ed32020-04-26 23:56:17 +0200104 : audio_mixer(AudioMixerImpl::Create()) {
aleloi04c07222016-11-22 06:42:53 -0800105 audio_state_config.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -0700106 audio_state_config.audio_processing =
Olga Sharonova09ceed22020-09-30 18:27:39 +0200107 params.use_null_audio_processing
Per Åhgrencc73ed32020-04-26 23:56:17 +0200108 ? nullptr
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200109 : rtc::make_ref_counted<testing::NiceMock<MockAudioProcessing>>();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100110 audio_state_config.audio_device_module =
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200111 rtc::make_ref_counted<NiceMock<MockAudioDeviceModule>>();
Olga Sharonova09ceed22020-09-30 18:27:39 +0200112 if (params.use_async_audio_processing) {
113 audio_state_config.async_audio_processing_factory =
114 async_audio_processing_helper_.CreateFactory();
115 }
solenberg566ef242015-11-06 15:34:49 -0800116 }
aleloi04c07222016-11-22 06:42:53 -0800117 AudioState::Config& config() { return audio_state_config; }
aleloi04c07222016-11-22 06:42:53 -0800118 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200119 NiceMock<FakeAsyncAudioProcessingHelper::MockAudioFrameProcessor>&
120 mock_audio_frame_processor() {
121 return async_audio_processing_helper_.audio_frame_processor_;
122 }
solenberg566ef242015-11-06 15:34:49 -0800123
124 private:
aleloi04c07222016-11-22 06:42:53 -0800125 AudioState::Config audio_state_config;
126 rtc::scoped_refptr<AudioMixer> audio_mixer;
Olga Sharonova09ceed22020-09-30 18:27:39 +0200127 FakeAsyncAudioProcessingHelper async_audio_processing_helper_;
solenberg566ef242015-11-06 15:34:49 -0800128};
aleloi04c07222016-11-22 06:42:53 -0800129
130class FakeAudioSource : public AudioMixer::Source {
131 public:
132 // TODO(aleloi): Valid overrides commented out, because the gmock
133 // methods don't use any override declarations, and we want to avoid
134 // warnings from -Winconsistent-missing-override. See
135 // http://crbug.com/428099.
136 int Ssrc() const /*override*/ { return 0; }
137
138 int PreferredSampleRate() const /*override*/ { return kSampleRate; }
139
Danil Chapovalovf9c6b682020-05-15 11:40:44 +0200140 MOCK_METHOD(AudioFrameInfo,
141 GetAudioFrameWithInfo,
142 (int sample_rate_hz, AudioFrame*),
143 (override));
aleloi04c07222016-11-22 06:42:53 -0800144};
145
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100146std::vector<int16_t> Create10msTestData(int sample_rate_hz,
147 size_t num_channels) {
148 const int samples_per_channel = sample_rate_hz / 100;
149 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
150 // Fill the first channel with a 1kHz sine wave.
151 const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz;
152 float w = 0.f;
153 for (int i = 0; i < samples_per_channel; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200154 audio_data[i * num_channels] = static_cast<int16_t>(32767.f * std::sin(w));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100155 w += inc;
156 }
157 return audio_data;
158}
159
160std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
161 const size_t num_channels = audio_frame->num_channels_;
162 const size_t samples_per_channel = audio_frame->samples_per_channel_;
163 std::vector<uint32_t> levels(num_channels, 0);
164 for (size_t i = 0; i < samples_per_channel; ++i) {
165 for (size_t j = 0; j < num_channels; ++j) {
166 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
167 }
168 }
169 return levels;
170}
solenberg566ef242015-11-06 15:34:49 -0800171} // namespace
172
Olga Sharonova09ceed22020-09-30 18:27:39 +0200173class AudioStateTest : public ::testing::TestWithParam<ConfigHelper::Params> {};
174
175TEST_P(AudioStateTest, Create) {
176 ConfigHelper helper(GetParam());
177 auto audio_state = AudioState::Create(helper.config());
178 EXPECT_TRUE(audio_state.get());
solenberg566ef242015-11-06 15:34:49 -0800179}
180
Olga Sharonova09ceed22020-09-30 18:27:39 +0200181TEST_P(AudioStateTest, ConstructDestruct) {
182 ConfigHelper helper(GetParam());
183 rtc::scoped_refptr<internal::AudioState> audio_state(
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200184 rtc::make_ref_counted<internal::AudioState>(helper.config()));
solenberg566ef242015-11-06 15:34:49 -0800185}
186
Olga Sharonova09ceed22020-09-30 18:27:39 +0200187TEST_P(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
188 ConfigHelper helper(GetParam());
aleloidd310712016-11-17 06:28:59 -0800189
Olga Sharonova09ceed22020-09-30 18:27:39 +0200190 if (GetParam().use_async_audio_processing) {
191 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
192 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
193 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200194 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200195
196 rtc::scoped_refptr<internal::AudioState> audio_state(
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200197 rtc::make_ref_counted<internal::AudioState>(helper.config()));
Olga Sharonova09ceed22020-09-30 18:27:39 +0200198
199 MockAudioSendStream stream;
200 audio_state->AddSendingStream(&stream, 8000, 2);
201
202 EXPECT_CALL(
203 stream,
204 SendAudioDataForMock(::testing::AllOf(
205 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(8000)),
206 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(2u)))))
207 .WillOnce(
208 // Verify that channels are not swapped by default.
209 ::testing::Invoke([](AudioFrame* audio_frame) {
210 auto levels = ComputeChannelLevels(audio_frame);
211 EXPECT_LT(0u, levels[0]);
212 EXPECT_EQ(0u, levels[1]);
213 }));
214 MockAudioProcessing* ap =
215 GetParam().use_null_audio_processing
216 ? nullptr
217 : static_cast<MockAudioProcessing*>(audio_state->audio_processing());
218 if (ap) {
219 EXPECT_CALL(*ap, set_stream_delay_ms(0));
220 EXPECT_CALL(*ap, set_stream_key_pressed(false));
221 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
222 }
223
224 constexpr int kSampleRate = 16000;
225 constexpr size_t kNumChannels = 2;
226 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
227 uint32_t new_mic_level = 667;
228 audio_state->audio_transport()->RecordedDataIsAvailable(
229 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
230 kSampleRate, 0, 0, 0, false, new_mic_level);
231 EXPECT_EQ(667u, new_mic_level);
232
233 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800234}
235
Olga Sharonova09ceed22020-09-30 18:27:39 +0200236TEST_P(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
237 ConfigHelper helper(GetParam());
aleloi04c07222016-11-22 06:42:53 -0800238
Olga Sharonova09ceed22020-09-30 18:27:39 +0200239 if (GetParam().use_async_audio_processing) {
240 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
241 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
242 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200243 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200244
245 rtc::scoped_refptr<internal::AudioState> audio_state(
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200246 rtc::make_ref_counted<internal::AudioState>(helper.config()));
Olga Sharonova09ceed22020-09-30 18:27:39 +0200247
248 MockAudioSendStream stream_1;
249 MockAudioSendStream stream_2;
250 audio_state->AddSendingStream(&stream_1, 8001, 2);
251 audio_state->AddSendingStream(&stream_2, 32000, 1);
252
253 EXPECT_CALL(
254 stream_1,
255 SendAudioDataForMock(::testing::AllOf(
256 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
257 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
258 .WillOnce(
259 // Verify that there is output signal.
260 ::testing::Invoke([](AudioFrame* audio_frame) {
261 auto levels = ComputeChannelLevels(audio_frame);
262 EXPECT_LT(0u, levels[0]);
263 }));
264 EXPECT_CALL(
265 stream_2,
266 SendAudioDataForMock(::testing::AllOf(
267 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
268 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
269 .WillOnce(
270 // Verify that there is output signal.
271 ::testing::Invoke([](AudioFrame* audio_frame) {
272 auto levels = ComputeChannelLevels(audio_frame);
273 EXPECT_LT(0u, levels[0]);
274 }));
275 MockAudioProcessing* ap =
276 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
277 if (ap) {
278 EXPECT_CALL(*ap, set_stream_delay_ms(5));
279 EXPECT_CALL(*ap, set_stream_key_pressed(true));
280 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
281 }
282
283 constexpr int kSampleRate = 16000;
284 constexpr size_t kNumChannels = 1;
285 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
286 uint32_t new_mic_level = 667;
287 audio_state->audio_transport()->RecordedDataIsAvailable(
288 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
289 kSampleRate, 5, 0, 0, true, new_mic_level);
290 EXPECT_EQ(667u, new_mic_level);
291
292 audio_state->RemoveSendingStream(&stream_1);
293 audio_state->RemoveSendingStream(&stream_2);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100294}
295
Olga Sharonova09ceed22020-09-30 18:27:39 +0200296TEST_P(AudioStateTest, EnableChannelSwap) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100297 constexpr int kSampleRate = 16000;
298 constexpr size_t kNumChannels = 2;
299
Olga Sharonova09ceed22020-09-30 18:27:39 +0200300 ConfigHelper helper(GetParam());
Niels Möllerac63ac72019-01-08 13:47:12 +0100301
Olga Sharonova09ceed22020-09-30 18:27:39 +0200302 if (GetParam().use_async_audio_processing) {
303 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
304 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
305 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200306 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200307
308 rtc::scoped_refptr<internal::AudioState> audio_state(
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +0200309 rtc::make_ref_counted<internal::AudioState>(helper.config()));
Olga Sharonova09ceed22020-09-30 18:27:39 +0200310
311 audio_state->SetStereoChannelSwapping(true);
312
313 MockAudioSendStream stream;
314 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
315
316 EXPECT_CALL(stream, SendAudioDataForMock(_))
317 .WillOnce(
318 // Verify that channels are swapped.
319 ::testing::Invoke([](AudioFrame* audio_frame) {
320 auto levels = ComputeChannelLevels(audio_frame);
321 EXPECT_EQ(0u, levels[0]);
322 EXPECT_LT(0u, levels[1]);
323 }));
324
325 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
326 uint32_t new_mic_level = 667;
327 audio_state->audio_transport()->RecordedDataIsAvailable(
328 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
329 kSampleRate, 0, 0, 0, false, new_mic_level);
330 EXPECT_EQ(667u, new_mic_level);
331
332 audio_state->RemoveSendingStream(&stream);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100333}
334
Olga Sharonova09ceed22020-09-30 18:27:39 +0200335TEST_P(AudioStateTest,
336 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
337 ConfigHelper helper(GetParam());
338 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800339
Olga Sharonova09ceed22020-09-30 18:27:39 +0200340 FakeAudioSource fake_source;
341 helper.mixer()->AddSource(&fake_source);
aleloi04c07222016-11-22 06:42:53 -0800342
Olga Sharonova09ceed22020-09-30 18:27:39 +0200343 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(_, _))
344 .WillOnce(
345 ::testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
346 audio_frame->sample_rate_hz_ = sample_rate_hz;
347 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
348 audio_frame->num_channels_ = kNumberOfChannels;
349 return AudioMixer::Source::AudioFrameInfo::kNormal;
350 }));
aleloidd310712016-11-17 06:28:59 -0800351
Olga Sharonova09ceed22020-09-30 18:27:39 +0200352 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
353 size_t n_samples_out;
354 int64_t elapsed_time_ms;
355 int64_t ntp_time_ms;
356 audio_state->audio_transport()->NeedMorePlayData(
357 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
358 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800359}
Olga Sharonova09ceed22020-09-30 18:27:39 +0200360
361INSTANTIATE_TEST_SUITE_P(AudioStateTest,
362 AudioStateTest,
363 Values(ConfigHelper::Params({false, false}),
364 ConfigHelper::Params({true, false}),
365 ConfigHelper::Params({false, true}),
366 ConfigHelper::Params({true, true})));
367
solenberg566ef242015-11-06 15:34:49 -0800368} // namespace test
369} // namespace webrtc