solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1 | /* |
| 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 Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "audio/audio_state.h" |
| 12 | |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 13 | #include <memory> |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 14 | #include <vector> |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 15 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 16 | #include "call/test/mock_audio_send_stream.h" |
| 17 | #include "modules/audio_device/include/mock_audio_device.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 19 | #include "modules/audio_processing/include/mock_audio_processing.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "test/gtest.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | namespace { |
| 26 | |
Per Åhgren | 71652f4 | 2020-03-17 13:23:58 +0100 | [diff] [blame] | 27 | using ::testing::_; |
| 28 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 29 | constexpr int kSampleRate = 16000; |
| 30 | constexpr int kNumberOfChannels = 1; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 31 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 32 | struct ConfigHelper { |
| 33 | ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) { |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 34 | audio_state_config.audio_mixer = audio_mixer; |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 35 | audio_state_config.audio_processing = |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 36 | new rtc::RefCountedObject<testing::NiceMock<MockAudioProcessing>>(); |
| 37 | audio_state_config.audio_device_module = |
| 38 | new rtc::RefCountedObject<MockAudioDeviceModule>(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 39 | } |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 40 | AudioState::Config& config() { return audio_state_config; } |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 41 | rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 42 | |
| 43 | private: |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 44 | AudioState::Config audio_state_config; |
| 45 | rtc::scoped_refptr<AudioMixer> audio_mixer; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 46 | }; |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 47 | |
| 48 | class FakeAudioSource : public AudioMixer::Source { |
| 49 | public: |
| 50 | // TODO(aleloi): Valid overrides commented out, because the gmock |
| 51 | // methods don't use any override declarations, and we want to avoid |
| 52 | // warnings from -Winconsistent-missing-override. See |
| 53 | // http://crbug.com/428099. |
| 54 | int Ssrc() const /*override*/ { return 0; } |
| 55 | |
| 56 | int PreferredSampleRate() const /*override*/ { return kSampleRate; } |
| 57 | |
| 58 | MOCK_METHOD2(GetAudioFrameWithInfo, |
| 59 | AudioFrameInfo(int sample_rate_hz, AudioFrame* audio_frame)); |
| 60 | }; |
| 61 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 62 | std::vector<int16_t> Create10msTestData(int sample_rate_hz, |
| 63 | size_t num_channels) { |
| 64 | const int samples_per_channel = sample_rate_hz / 100; |
| 65 | std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0); |
| 66 | // Fill the first channel with a 1kHz sine wave. |
| 67 | const float inc = (2 * 3.14159265f * 1000) / sample_rate_hz; |
| 68 | float w = 0.f; |
| 69 | for (int i = 0; i < samples_per_channel; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | audio_data[i * num_channels] = static_cast<int16_t>(32767.f * std::sin(w)); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 71 | w += inc; |
| 72 | } |
| 73 | return audio_data; |
| 74 | } |
| 75 | |
| 76 | std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) { |
| 77 | const size_t num_channels = audio_frame->num_channels_; |
| 78 | const size_t samples_per_channel = audio_frame->samples_per_channel_; |
| 79 | std::vector<uint32_t> levels(num_channels, 0); |
| 80 | for (size_t i = 0; i < samples_per_channel; ++i) { |
| 81 | for (size_t j = 0; j < num_channels; ++j) { |
| 82 | levels[j] += std::abs(audio_frame->data()[i * num_channels + j]); |
| 83 | } |
| 84 | } |
| 85 | return levels; |
| 86 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 87 | } // namespace |
| 88 | |
| 89 | TEST(AudioStateTest, Create) { |
| 90 | ConfigHelper helper; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 91 | auto audio_state = AudioState::Create(helper.config()); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 92 | EXPECT_TRUE(audio_state.get()); |
| 93 | } |
| 94 | |
| 95 | TEST(AudioStateTest, ConstructDestruct) { |
| 96 | ConfigHelper helper; |
Niels Möller | ac63ac7 | 2019-01-08 13:47:12 +0100 | [diff] [blame] | 97 | rtc::scoped_refptr<internal::AudioState> audio_state( |
| 98 | new rtc::RefCountedObject<internal::AudioState>(helper.config())); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 101 | TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) { |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 102 | ConfigHelper helper; |
Niels Möller | ac63ac7 | 2019-01-08 13:47:12 +0100 | [diff] [blame] | 103 | rtc::scoped_refptr<internal::AudioState> audio_state( |
| 104 | new rtc::RefCountedObject<internal::AudioState>(helper.config())); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 105 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 106 | MockAudioSendStream stream; |
| 107 | audio_state->AddSendingStream(&stream, 8000, 2); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 108 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | EXPECT_CALL( |
| 110 | stream, |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 111 | SendAudioDataForMock(::testing::AllOf( |
| 112 | ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(8000)), |
| 113 | ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(2u))))) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | .WillOnce( |
| 115 | // Verify that channels are not swapped by default. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 116 | ::testing::Invoke([](AudioFrame* audio_frame) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 117 | auto levels = ComputeChannelLevels(audio_frame); |
| 118 | EXPECT_LT(0u, levels[0]); |
| 119 | EXPECT_EQ(0u, levels[1]); |
| 120 | })); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 121 | MockAudioProcessing* ap = |
| 122 | static_cast<MockAudioProcessing*>(audio_state->audio_processing()); |
| 123 | EXPECT_CALL(*ap, set_stream_delay_ms(0)); |
| 124 | EXPECT_CALL(*ap, set_stream_key_pressed(false)); |
Per Åhgren | 71652f4 | 2020-03-17 13:23:58 +0100 | [diff] [blame] | 125 | EXPECT_CALL(*ap, ProcessStream(_, _, _, _, _)); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 126 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 127 | constexpr int kSampleRate = 16000; |
| 128 | constexpr size_t kNumChannels = 2; |
| 129 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 130 | uint32_t new_mic_level = 667; |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 131 | audio_state->audio_transport()->RecordedDataIsAvailable( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels, |
| 133 | kSampleRate, 0, 0, 0, false, new_mic_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 134 | EXPECT_EQ(667u, new_mic_level); |
| 135 | |
| 136 | audio_state->RemoveSendingStream(&stream); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 139 | TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) { |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 140 | ConfigHelper helper; |
Niels Möller | ac63ac7 | 2019-01-08 13:47:12 +0100 | [diff] [blame] | 141 | rtc::scoped_refptr<internal::AudioState> audio_state( |
| 142 | new rtc::RefCountedObject<internal::AudioState>(helper.config())); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 143 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 144 | MockAudioSendStream stream_1; |
| 145 | MockAudioSendStream stream_2; |
| 146 | audio_state->AddSendingStream(&stream_1, 8001, 2); |
| 147 | audio_state->AddSendingStream(&stream_2, 32000, 1); |
| 148 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 149 | EXPECT_CALL( |
| 150 | stream_1, |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 151 | SendAudioDataForMock(::testing::AllOf( |
| 152 | ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)), |
| 153 | ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u))))) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 154 | .WillOnce( |
| 155 | // Verify that there is output signal. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 156 | ::testing::Invoke([](AudioFrame* audio_frame) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | auto levels = ComputeChannelLevels(audio_frame); |
| 158 | EXPECT_LT(0u, levels[0]); |
| 159 | })); |
| 160 | EXPECT_CALL( |
| 161 | stream_2, |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 162 | SendAudioDataForMock(::testing::AllOf( |
| 163 | ::testing::Field(&AudioFrame::sample_rate_hz_, ::testing::Eq(16000)), |
| 164 | ::testing::Field(&AudioFrame::num_channels_, ::testing::Eq(1u))))) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 165 | .WillOnce( |
| 166 | // Verify that there is output signal. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 167 | ::testing::Invoke([](AudioFrame* audio_frame) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 168 | auto levels = ComputeChannelLevels(audio_frame); |
| 169 | EXPECT_LT(0u, levels[0]); |
| 170 | })); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 171 | MockAudioProcessing* ap = |
| 172 | static_cast<MockAudioProcessing*>(audio_state->audio_processing()); |
| 173 | EXPECT_CALL(*ap, set_stream_delay_ms(5)); |
| 174 | EXPECT_CALL(*ap, set_stream_key_pressed(true)); |
Per Åhgren | 71652f4 | 2020-03-17 13:23:58 +0100 | [diff] [blame] | 175 | EXPECT_CALL(*ap, ProcessStream(_, _, _, _, _)); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 176 | |
| 177 | constexpr int kSampleRate = 16000; |
| 178 | constexpr size_t kNumChannels = 1; |
| 179 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 180 | uint32_t new_mic_level = 667; |
| 181 | audio_state->audio_transport()->RecordedDataIsAvailable( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 182 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels, |
| 183 | kSampleRate, 5, 0, 0, true, new_mic_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 184 | EXPECT_EQ(667u, new_mic_level); |
| 185 | |
| 186 | audio_state->RemoveSendingStream(&stream_1); |
| 187 | audio_state->RemoveSendingStream(&stream_2); |
| 188 | } |
| 189 | |
| 190 | TEST(AudioStateTest, EnableChannelSwap) { |
| 191 | constexpr int kSampleRate = 16000; |
| 192 | constexpr size_t kNumChannels = 2; |
| 193 | |
| 194 | ConfigHelper helper; |
Niels Möller | ac63ac7 | 2019-01-08 13:47:12 +0100 | [diff] [blame] | 195 | rtc::scoped_refptr<internal::AudioState> audio_state( |
| 196 | new rtc::RefCountedObject<internal::AudioState>(helper.config())); |
| 197 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 198 | audio_state->SetStereoChannelSwapping(true); |
| 199 | |
| 200 | MockAudioSendStream stream; |
| 201 | audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels); |
| 202 | |
Per Åhgren | 71652f4 | 2020-03-17 13:23:58 +0100 | [diff] [blame] | 203 | EXPECT_CALL(stream, SendAudioDataForMock(_)) |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 204 | .WillOnce( |
| 205 | // Verify that channels are swapped. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 206 | ::testing::Invoke([](AudioFrame* audio_frame) { |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 207 | auto levels = ComputeChannelLevels(audio_frame); |
| 208 | EXPECT_EQ(0u, levels[0]); |
| 209 | EXPECT_LT(0u, levels[1]); |
| 210 | })); |
| 211 | |
| 212 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 213 | uint32_t new_mic_level = 667; |
| 214 | audio_state->audio_transport()->RecordedDataIsAvailable( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 215 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, kNumChannels, |
| 216 | kSampleRate, 0, 0, 0, false, new_mic_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 217 | EXPECT_EQ(667u, new_mic_level); |
| 218 | |
| 219 | audio_state->RemoveSendingStream(&stream); |
| 220 | } |
| 221 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 222 | TEST(AudioStateTest, |
| 223 | QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) { |
| 224 | ConfigHelper helper; |
| 225 | auto audio_state = AudioState::Create(helper.config()); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 226 | |
| 227 | FakeAudioSource fake_source; |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 228 | helper.mixer()->AddSource(&fake_source); |
| 229 | |
Per Åhgren | 71652f4 | 2020-03-17 13:23:58 +0100 | [diff] [blame] | 230 | EXPECT_CALL(fake_source, GetAudioFrameWithInfo(_, _)) |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 231 | .WillOnce( |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 232 | ::testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) { |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 233 | audio_frame->sample_rate_hz_ = sample_rate_hz; |
| 234 | audio_frame->samples_per_channel_ = sample_rate_hz / 100; |
| 235 | audio_frame->num_channels_ = kNumberOfChannels; |
| 236 | return AudioMixer::Source::AudioFrameInfo::kNormal; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 237 | })); |
| 238 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 239 | int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels]; |
| 240 | size_t n_samples_out; |
| 241 | int64_t elapsed_time_ms; |
| 242 | int64_t ntp_time_ms; |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 243 | audio_state->audio_transport()->NeedMorePlayData( |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 244 | kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate, |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 245 | audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 246 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 247 | } // namespace test |
| 248 | } // namespace webrtc |