blob: 02fc04e6dcd68502c58f97caf0d43f58485937fb [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"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
solenberg566ef242015-11-06 15:34:49 -080023
24namespace webrtc {
25namespace test {
26namespace {
27
Per Åhgren71652f42020-03-17 13:23:58 +010028using ::testing::_;
Per Åhgrendc5522b2020-03-19 14:55:58 +010029using ::testing::Matcher;
Olga Sharonova09ceed22020-09-30 18:27:39 +020030using ::testing::NiceMock;
31using ::testing::StrictMock;
32using ::testing::Values;
Per Åhgren71652f42020-03-17 13:23:58 +010033
Fredrik Solenberg2a877972017-12-15 16:42:15 +010034constexpr int kSampleRate = 16000;
35constexpr int kNumberOfChannels = 1;
aleloidd310712016-11-17 06:28:59 -080036
Olga Sharonova09ceed22020-09-30 18:27:39 +020037struct FakeAsyncAudioProcessingHelper {
38 class FakeTaskQueue : public StrictMock<TaskQueueBase> {
39 public:
40 FakeTaskQueue() = default;
41
42 void Delete() override { delete this; }
43 void PostTask(std::unique_ptr<QueuedTask> task) override {
44 std::move(task)->Run();
45 }
46 MOCK_METHOD(void,
47 PostDelayedTask,
48 (std::unique_ptr<QueuedTask> task, uint32_t milliseconds),
49 (override));
50 };
51
52 class FakeTaskQueueFactory : public TaskQueueFactory {
53 public:
54 FakeTaskQueueFactory() = default;
55 ~FakeTaskQueueFactory() override = default;
56 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
57 absl::string_view name,
58 Priority priority) const override {
59 return std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>(
60 new FakeTaskQueue());
61 }
62 };
63
64 class MockAudioFrameProcessor : public AudioFrameProcessor {
65 public:
66 ~MockAudioFrameProcessor() override = default;
67
68 MOCK_METHOD(void, ProcessCalled, ());
69 MOCK_METHOD(void, SinkSet, ());
70 MOCK_METHOD(void, SinkCleared, ());
71
72 void Process(std::unique_ptr<AudioFrame> frame) override {
73 ProcessCalled();
74 sink_callback_(std::move(frame));
75 }
76
77 void SetSink(OnAudioFrameCallback sink_callback) override {
78 sink_callback_ = std::move(sink_callback);
79 if (sink_callback_ == nullptr)
80 SinkCleared();
81 else
82 SinkSet();
83 }
84
85 private:
86 OnAudioFrameCallback sink_callback_;
87 };
88
89 NiceMock<MockAudioFrameProcessor> audio_frame_processor_;
90 FakeTaskQueueFactory task_queue_factory_;
91
92 rtc::scoped_refptr<AsyncAudioProcessing::Factory> CreateFactory() {
93 return new rtc::RefCountedObject<AsyncAudioProcessing::Factory>(
94 audio_frame_processor_, task_queue_factory_);
95 }
96};
97
aleloi04c07222016-11-22 06:42:53 -080098struct ConfigHelper {
Olga Sharonova09ceed22020-09-30 18:27:39 +020099 struct Params {
100 bool use_null_audio_processing;
101 bool use_async_audio_processing;
102 };
103
104 explicit ConfigHelper(const Params& params)
Per Åhgrencc73ed32020-04-26 23:56:17 +0200105 : audio_mixer(AudioMixerImpl::Create()) {
aleloi04c07222016-11-22 06:42:53 -0800106 audio_state_config.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -0700107 audio_state_config.audio_processing =
Olga Sharonova09ceed22020-09-30 18:27:39 +0200108 params.use_null_audio_processing
Per Åhgrencc73ed32020-04-26 23:56:17 +0200109 ? nullptr
110 : new rtc::RefCountedObject<
111 testing::NiceMock<MockAudioProcessing>>();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100112 audio_state_config.audio_device_module =
Olga Sharonova09ceed22020-09-30 18:27:39 +0200113 new rtc::RefCountedObject<NiceMock<MockAudioDeviceModule>>();
114 if (params.use_async_audio_processing) {
115 audio_state_config.async_audio_processing_factory =
116 async_audio_processing_helper_.CreateFactory();
117 }
solenberg566ef242015-11-06 15:34:49 -0800118 }
aleloi04c07222016-11-22 06:42:53 -0800119 AudioState::Config& config() { return audio_state_config; }
aleloi04c07222016-11-22 06:42:53 -0800120 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200121 NiceMock<FakeAsyncAudioProcessingHelper::MockAudioFrameProcessor>&
122 mock_audio_frame_processor() {
123 return async_audio_processing_helper_.audio_frame_processor_;
124 }
solenberg566ef242015-11-06 15:34:49 -0800125
126 private:
aleloi04c07222016-11-22 06:42:53 -0800127 AudioState::Config audio_state_config;
128 rtc::scoped_refptr<AudioMixer> audio_mixer;
Olga Sharonova09ceed22020-09-30 18:27:39 +0200129 FakeAsyncAudioProcessingHelper async_audio_processing_helper_;
solenberg566ef242015-11-06 15:34:49 -0800130};
aleloi04c07222016-11-22 06:42:53 -0800131
132class FakeAudioSource : public AudioMixer::Source {
133 public:
134 // TODO(aleloi): Valid overrides commented out, because the gmock
135 // methods don't use any override declarations, and we want to avoid
136 // warnings from -Winconsistent-missing-override. See
137 // http://crbug.com/428099.
138 int Ssrc() const /*override*/ { return 0; }
139
140 int PreferredSampleRate() const /*override*/ { return kSampleRate; }
141
Danil Chapovalovf9c6b682020-05-15 11:40:44 +0200142 MOCK_METHOD(AudioFrameInfo,
143 GetAudioFrameWithInfo,
144 (int sample_rate_hz, AudioFrame*),
145 (override));
aleloi04c07222016-11-22 06:42:53 -0800146};
147
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100148std::vector<int16_t> Create10msTestData(int sample_rate_hz,
149 size_t num_channels) {
150 const int samples_per_channel = sample_rate_hz / 100;
151 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
152 // Fill the first channel with a 1kHz sine wave.
153 const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz;
154 float w = 0.f;
155 for (int i = 0; i < samples_per_channel; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200156 audio_data[i * num_channels] = static_cast<int16_t>(32767.f * std::sin(w));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100157 w += inc;
158 }
159 return audio_data;
160}
161
162std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
163 const size_t num_channels = audio_frame->num_channels_;
164 const size_t samples_per_channel = audio_frame->samples_per_channel_;
165 std::vector<uint32_t> levels(num_channels, 0);
166 for (size_t i = 0; i < samples_per_channel; ++i) {
167 for (size_t j = 0; j < num_channels; ++j) {
168 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
169 }
170 }
171 return levels;
172}
solenberg566ef242015-11-06 15:34:49 -0800173} // namespace
174
Olga Sharonova09ceed22020-09-30 18:27:39 +0200175class AudioStateTest : public ::testing::TestWithParam<ConfigHelper::Params> {};
176
177TEST_P(AudioStateTest, Create) {
178 ConfigHelper helper(GetParam());
179 auto audio_state = AudioState::Create(helper.config());
180 EXPECT_TRUE(audio_state.get());
solenberg566ef242015-11-06 15:34:49 -0800181}
182
Olga Sharonova09ceed22020-09-30 18:27:39 +0200183TEST_P(AudioStateTest, ConstructDestruct) {
184 ConfigHelper helper(GetParam());
185 rtc::scoped_refptr<internal::AudioState> audio_state(
186 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
solenberg566ef242015-11-06 15:34:49 -0800187}
188
Olga Sharonova09ceed22020-09-30 18:27:39 +0200189TEST_P(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
190 ConfigHelper helper(GetParam());
aleloidd310712016-11-17 06:28:59 -0800191
Olga Sharonova09ceed22020-09-30 18:27:39 +0200192 if (GetParam().use_async_audio_processing) {
193 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
194 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
195 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200196 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200197
198 rtc::scoped_refptr<internal::AudioState> audio_state(
199 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
200
201 MockAudioSendStream stream;
202 audio_state->AddSendingStream(&stream, 8000, 2);
203
204 EXPECT_CALL(
205 stream,
206 SendAudioDataForMock(::testing::AllOf(
207 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(8000)),
208 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(2u)))))
209 .WillOnce(
210 // Verify that channels are not swapped by default.
211 ::testing::Invoke([](AudioFrame* audio_frame) {
212 auto levels = ComputeChannelLevels(audio_frame);
213 EXPECT_LT(0u, levels[0]);
214 EXPECT_EQ(0u, levels[1]);
215 }));
216 MockAudioProcessing* ap =
217 GetParam().use_null_audio_processing
218 ? nullptr
219 : static_cast<MockAudioProcessing*>(audio_state->audio_processing());
220 if (ap) {
221 EXPECT_CALL(*ap, set_stream_delay_ms(0));
222 EXPECT_CALL(*ap, set_stream_key_pressed(false));
223 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
224 }
225
226 constexpr int kSampleRate = 16000;
227 constexpr size_t kNumChannels = 2;
228 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
229 uint32_t new_mic_level = 667;
230 audio_state->audio_transport()->RecordedDataIsAvailable(
231 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
232 kSampleRate, 0, 0, 0, false, new_mic_level);
233 EXPECT_EQ(667u, new_mic_level);
234
235 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800236}
237
Olga Sharonova09ceed22020-09-30 18:27:39 +0200238TEST_P(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
239 ConfigHelper helper(GetParam());
aleloi04c07222016-11-22 06:42:53 -0800240
Olga Sharonova09ceed22020-09-30 18:27:39 +0200241 if (GetParam().use_async_audio_processing) {
242 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
243 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
244 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200245 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200246
247 rtc::scoped_refptr<internal::AudioState> audio_state(
248 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
249
250 MockAudioSendStream stream_1;
251 MockAudioSendStream stream_2;
252 audio_state->AddSendingStream(&stream_1, 8001, 2);
253 audio_state->AddSendingStream(&stream_2, 32000, 1);
254
255 EXPECT_CALL(
256 stream_1,
257 SendAudioDataForMock(::testing::AllOf(
258 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
259 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
260 .WillOnce(
261 // Verify that there is output signal.
262 ::testing::Invoke([](AudioFrame* audio_frame) {
263 auto levels = ComputeChannelLevels(audio_frame);
264 EXPECT_LT(0u, levels[0]);
265 }));
266 EXPECT_CALL(
267 stream_2,
268 SendAudioDataForMock(::testing::AllOf(
269 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
270 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
271 .WillOnce(
272 // Verify that there is output signal.
273 ::testing::Invoke([](AudioFrame* audio_frame) {
274 auto levels = ComputeChannelLevels(audio_frame);
275 EXPECT_LT(0u, levels[0]);
276 }));
277 MockAudioProcessing* ap =
278 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
279 if (ap) {
280 EXPECT_CALL(*ap, set_stream_delay_ms(5));
281 EXPECT_CALL(*ap, set_stream_key_pressed(true));
282 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
283 }
284
285 constexpr int kSampleRate = 16000;
286 constexpr size_t kNumChannels = 1;
287 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
288 uint32_t new_mic_level = 667;
289 audio_state->audio_transport()->RecordedDataIsAvailable(
290 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
291 kSampleRate, 5, 0, 0, true, new_mic_level);
292 EXPECT_EQ(667u, new_mic_level);
293
294 audio_state->RemoveSendingStream(&stream_1);
295 audio_state->RemoveSendingStream(&stream_2);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100296}
297
Olga Sharonova09ceed22020-09-30 18:27:39 +0200298TEST_P(AudioStateTest, EnableChannelSwap) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100299 constexpr int kSampleRate = 16000;
300 constexpr size_t kNumChannels = 2;
301
Olga Sharonova09ceed22020-09-30 18:27:39 +0200302 ConfigHelper helper(GetParam());
Niels Möllerac63ac72019-01-08 13:47:12 +0100303
Olga Sharonova09ceed22020-09-30 18:27:39 +0200304 if (GetParam().use_async_audio_processing) {
305 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkSet);
306 EXPECT_CALL(helper.mock_audio_frame_processor(), ProcessCalled);
307 EXPECT_CALL(helper.mock_audio_frame_processor(), SinkCleared);
Per Åhgrencc73ed32020-04-26 23:56:17 +0200308 }
Olga Sharonova09ceed22020-09-30 18:27:39 +0200309
310 rtc::scoped_refptr<internal::AudioState> audio_state(
311 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
312
313 audio_state->SetStereoChannelSwapping(true);
314
315 MockAudioSendStream stream;
316 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
317
318 EXPECT_CALL(stream, SendAudioDataForMock(_))
319 .WillOnce(
320 // Verify that channels are swapped.
321 ::testing::Invoke([](AudioFrame* audio_frame) {
322 auto levels = ComputeChannelLevels(audio_frame);
323 EXPECT_EQ(0u, levels[0]);
324 EXPECT_LT(0u, levels[1]);
325 }));
326
327 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
328 uint32_t new_mic_level = 667;
329 audio_state->audio_transport()->RecordedDataIsAvailable(
330 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
331 kSampleRate, 0, 0, 0, false, new_mic_level);
332 EXPECT_EQ(667u, new_mic_level);
333
334 audio_state->RemoveSendingStream(&stream);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100335}
336
Olga Sharonova09ceed22020-09-30 18:27:39 +0200337TEST_P(AudioStateTest,
338 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
339 ConfigHelper helper(GetParam());
340 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800341
Olga Sharonova09ceed22020-09-30 18:27:39 +0200342 FakeAudioSource fake_source;
343 helper.mixer()->AddSource(&fake_source);
aleloi04c07222016-11-22 06:42:53 -0800344
Olga Sharonova09ceed22020-09-30 18:27:39 +0200345 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(_, _))
346 .WillOnce(
347 ::testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
348 audio_frame->sample_rate_hz_ = sample_rate_hz;
349 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
350 audio_frame->num_channels_ = kNumberOfChannels;
351 return AudioMixer::Source::AudioFrameInfo::kNormal;
352 }));
aleloidd310712016-11-17 06:28:59 -0800353
Olga Sharonova09ceed22020-09-30 18:27:39 +0200354 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
355 size_t n_samples_out;
356 int64_t elapsed_time_ms;
357 int64_t ntp_time_ms;
358 audio_state->audio_transport()->NeedMorePlayData(
359 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
360 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800361}
Olga Sharonova09ceed22020-09-30 18:27:39 +0200362
363INSTANTIATE_TEST_SUITE_P(AudioStateTest,
364 AudioStateTest,
365 Values(ConfigHelper::Params({false, false}),
366 ConfigHelper::Params({true, false}),
367 ConfigHelper::Params({false, true}),
368 ConfigHelper::Params({true, true})));
369
solenberg566ef242015-11-06 15:34:49 -0800370} // namespace test
371} // namespace webrtc