blob: 2a1018c120badcb4876fd6221af41e0c9ad07fef [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>
Fredrik Solenberg2a877972017-12-15 16:42:15 +010014#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080015
Fredrik Solenberg2a877972017-12-15 16:42:15 +010016#include "call/test/mock_audio_send_stream.h"
17#include "modules/audio_device/include/mock_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_mixer/audio_mixer_impl.h"
19#include "modules/audio_processing/include/mock_audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#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;
Per Åhgren71652f42020-03-17 13:23:58 +010029
Fredrik Solenberg2a877972017-12-15 16:42:15 +010030constexpr int kSampleRate = 16000;
31constexpr int kNumberOfChannels = 1;
aleloidd310712016-11-17 06:28:59 -080032
aleloi04c07222016-11-22 06:42:53 -080033struct ConfigHelper {
34 ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) {
aleloi04c07222016-11-22 06:42:53 -080035 audio_state_config.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -070036 audio_state_config.audio_processing =
Fredrik Solenberg2a877972017-12-15 16:42:15 +010037 new rtc::RefCountedObject<testing::NiceMock<MockAudioProcessing>>();
38 audio_state_config.audio_device_module =
39 new rtc::RefCountedObject<MockAudioDeviceModule>();
solenberg566ef242015-11-06 15:34:49 -080040 }
aleloi04c07222016-11-22 06:42:53 -080041 AudioState::Config& config() { return audio_state_config; }
aleloi04c07222016-11-22 06:42:53 -080042 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
solenberg566ef242015-11-06 15:34:49 -080043
44 private:
aleloi04c07222016-11-22 06:42:53 -080045 AudioState::Config audio_state_config;
46 rtc::scoped_refptr<AudioMixer> audio_mixer;
solenberg566ef242015-11-06 15:34:49 -080047};
aleloi04c07222016-11-22 06:42:53 -080048
49class FakeAudioSource : public AudioMixer::Source {
50 public:
51 // TODO(aleloi): Valid overrides commented out, because the gmock
52 // methods don't use any override declarations, and we want to avoid
53 // warnings from -Winconsistent-missing-override. See
54 // http://crbug.com/428099.
55 int Ssrc() const /*override*/ { return 0; }
56
57 int PreferredSampleRate() const /*override*/ { return kSampleRate; }
58
59 MOCK_METHOD2(GetAudioFrameWithInfo,
60 AudioFrameInfo(int sample_rate_hz, AudioFrame* audio_frame));
61};
62
Fredrik Solenberg2a877972017-12-15 16:42:15 +010063std::vector<int16_t> Create10msTestData(int sample_rate_hz,
64 size_t num_channels) {
65 const int samples_per_channel = sample_rate_hz / 100;
66 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
67 // Fill the first channel with a 1kHz sine wave.
68 const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz;
69 float w = 0.f;
70 for (int i = 0; i < samples_per_channel; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +020071 audio_data[i * num_channels] = static_cast<int16_t>(32767.f * std::sin(w));
Fredrik Solenberg2a877972017-12-15 16:42:15 +010072 w += inc;
73 }
74 return audio_data;
75}
76
77std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) {
78 const size_t num_channels = audio_frame->num_channels_;
79 const size_t samples_per_channel = audio_frame->samples_per_channel_;
80 std::vector<uint32_t> levels(num_channels, 0);
81 for (size_t i = 0; i < samples_per_channel; ++i) {
82 for (size_t j = 0; j < num_channels; ++j) {
83 levels[j] += std::abs(audio_frame->data()[i * num_channels + j]);
84 }
85 }
86 return levels;
87}
solenberg566ef242015-11-06 15:34:49 -080088} // namespace
89
90TEST(AudioStateTest, Create) {
91 ConfigHelper helper;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010092 auto audio_state = AudioState::Create(helper.config());
solenberg566ef242015-11-06 15:34:49 -080093 EXPECT_TRUE(audio_state.get());
94}
95
96TEST(AudioStateTest, ConstructDestruct) {
97 ConfigHelper helper;
Niels Möllerac63ac72019-01-08 13:47:12 +010098 rtc::scoped_refptr<internal::AudioState> audio_state(
99 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
solenberg566ef242015-11-06 15:34:49 -0800100}
101
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100102TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
aleloidd310712016-11-17 06:28:59 -0800103 ConfigHelper helper;
Niels Möllerac63ac72019-01-08 13:47:12 +0100104 rtc::scoped_refptr<internal::AudioState> audio_state(
105 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
aleloidd310712016-11-17 06:28:59 -0800106
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100107 MockAudioSendStream stream;
108 audio_state->AddSendingStream(&stream, 8000, 2);
aleloi04c07222016-11-22 06:42:53 -0800109
Yves Gerey665174f2018-06-19 15:03:05 +0200110 EXPECT_CALL(
111 stream,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200112 SendAudioDataForMock(::testing::AllOf(
113 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(8000)),
114 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(2u)))))
Yves Gerey665174f2018-06-19 15:03:05 +0200115 .WillOnce(
116 // Verify that channels are not swapped by default.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200117 ::testing::Invoke([](AudioFrame* audio_frame) {
Yves Gerey665174f2018-06-19 15:03:05 +0200118 auto levels = ComputeChannelLevels(audio_frame);
119 EXPECT_LT(0u, levels[0]);
120 EXPECT_EQ(0u, levels[1]);
121 }));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100122 MockAudioProcessing* ap =
123 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
124 EXPECT_CALL(*ap, set_stream_delay_ms(0));
125 EXPECT_CALL(*ap, set_stream_key_pressed(false));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100126 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
aleloi04c07222016-11-22 06:42:53 -0800127
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100128 constexpr int kSampleRate = 16000;
129 constexpr size_t kNumChannels = 2;
130 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
131 uint32_t new_mic_level = 667;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100132 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200133 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
134 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100135 EXPECT_EQ(667u, new_mic_level);
136
137 audio_state->RemoveSendingStream(&stream);
aleloi04c07222016-11-22 06:42:53 -0800138}
139
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100140TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
aleloi04c07222016-11-22 06:42:53 -0800141 ConfigHelper helper;
Niels Möllerac63ac72019-01-08 13:47:12 +0100142 rtc::scoped_refptr<internal::AudioState> audio_state(
143 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
aleloi04c07222016-11-22 06:42:53 -0800144
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100145 MockAudioSendStream stream_1;
146 MockAudioSendStream stream_2;
147 audio_state->AddSendingStream(&stream_1, 8001, 2);
148 audio_state->AddSendingStream(&stream_2, 32000, 1);
149
Yves Gerey665174f2018-06-19 15:03:05 +0200150 EXPECT_CALL(
151 stream_1,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200152 SendAudioDataForMock(::testing::AllOf(
153 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
154 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
Yves Gerey665174f2018-06-19 15:03:05 +0200155 .WillOnce(
156 // Verify that there is output signal.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200157 ::testing::Invoke([](AudioFrame* audio_frame) {
Yves Gerey665174f2018-06-19 15:03:05 +0200158 auto levels = ComputeChannelLevels(audio_frame);
159 EXPECT_LT(0u, levels[0]);
160 }));
161 EXPECT_CALL(
162 stream_2,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200163 SendAudioDataForMock(::testing::AllOf(
164 ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)),
165 ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u)))))
Yves Gerey665174f2018-06-19 15:03:05 +0200166 .WillOnce(
167 // Verify that there is output signal.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200168 ::testing::Invoke([](AudioFrame* audio_frame) {
Yves Gerey665174f2018-06-19 15:03:05 +0200169 auto levels = ComputeChannelLevels(audio_frame);
170 EXPECT_LT(0u, levels[0]);
171 }));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100172 MockAudioProcessing* ap =
173 static_cast<MockAudioProcessing*>(audio_state->audio_processing());
174 EXPECT_CALL(*ap, set_stream_delay_ms(5));
175 EXPECT_CALL(*ap, set_stream_key_pressed(true));
Per Åhgrendc5522b2020-03-19 14:55:58 +0100176 EXPECT_CALL(*ap, ProcessStream(_, _, _, Matcher<int16_t*>(_)));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100177
178 constexpr int kSampleRate = 16000;
179 constexpr size_t kNumChannels = 1;
180 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
181 uint32_t new_mic_level = 667;
182 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200183 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
184 kSampleRate, 5, 0, 0, true, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100185 EXPECT_EQ(667u, new_mic_level);
186
187 audio_state->RemoveSendingStream(&stream_1);
188 audio_state->RemoveSendingStream(&stream_2);
189}
190
191TEST(AudioStateTest, EnableChannelSwap) {
192 constexpr int kSampleRate = 16000;
193 constexpr size_t kNumChannels = 2;
194
195 ConfigHelper helper;
Niels Möllerac63ac72019-01-08 13:47:12 +0100196 rtc::scoped_refptr<internal::AudioState> audio_state(
197 new rtc::RefCountedObject<internal::AudioState>(helper.config()));
198
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100199 audio_state->SetStereoChannelSwapping(true);
200
201 MockAudioSendStream stream;
202 audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels);
203
Per Åhgren71652f42020-03-17 13:23:58 +0100204 EXPECT_CALL(stream, SendAudioDataForMock(_))
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100205 .WillOnce(
206 // Verify that channels are swapped.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200207 ::testing::Invoke([](AudioFrame* audio_frame) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100208 auto levels = ComputeChannelLevels(audio_frame);
209 EXPECT_EQ(0u, levels[0]);
210 EXPECT_LT(0u, levels[1]);
211 }));
212
213 auto audio_data = Create10msTestData(kSampleRate, kNumChannels);
214 uint32_t new_mic_level = 667;
215 audio_state->audio_transport()->RecordedDataIsAvailable(
Yves Gerey665174f2018-06-19 15:03:05 +0200216 &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels,
217 kSampleRate, 0, 0, 0, false, new_mic_level);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100218 EXPECT_EQ(667u, new_mic_level);
219
220 audio_state->RemoveSendingStream(&stream);
221}
222
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100223TEST(AudioStateTest,
224 QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) {
225 ConfigHelper helper;
226 auto audio_state = AudioState::Create(helper.config());
aleloi04c07222016-11-22 06:42:53 -0800227
228 FakeAudioSource fake_source;
aleloi04c07222016-11-22 06:42:53 -0800229 helper.mixer()->AddSource(&fake_source);
230
Per Åhgren71652f42020-03-17 13:23:58 +0100231 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(_, _))
aleloi04c07222016-11-22 06:42:53 -0800232 .WillOnce(
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200233 ::testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
aleloi04c07222016-11-22 06:42:53 -0800234 audio_frame->sample_rate_hz_ = sample_rate_hz;
235 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
236 audio_frame->num_channels_ = kNumberOfChannels;
237 return AudioMixer::Source::AudioFrameInfo::kNormal;
aleloidd310712016-11-17 06:28:59 -0800238 }));
239
aleloi04c07222016-11-22 06:42:53 -0800240 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
241 size_t n_samples_out;
242 int64_t elapsed_time_ms;
243 int64_t ntp_time_ms;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100244 audio_state->audio_transport()->NeedMorePlayData(
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100245 kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate,
aleloi04c07222016-11-22 06:42:53 -0800246 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800247}
solenberg566ef242015-11-06 15:34:49 -0800248} // namespace test
249} // namespace webrtc