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 | |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 11 | #include <memory> |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 12 | #include <vector> |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "audio/audio_state.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 15 | #include "call/test/mock_audio_send_stream.h" |
| 16 | #include "modules/audio_device/include/mock_audio_device.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 18 | #include "modules/audio_processing/include/mock_audio_processing.h" |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 19 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "test/gtest.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace test { |
| 24 | namespace { |
| 25 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 26 | constexpr int kSampleRate = 16000; |
| 27 | constexpr int kNumberOfChannels = 1; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 28 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 29 | struct ConfigHelper { |
| 30 | ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) { |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 31 | audio_state_config.audio_mixer = audio_mixer; |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 32 | audio_state_config.audio_processing = |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 33 | new rtc::RefCountedObject<testing::NiceMock<MockAudioProcessing>>(); |
| 34 | audio_state_config.audio_device_module = |
| 35 | new rtc::RefCountedObject<MockAudioDeviceModule>(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 36 | } |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 37 | AudioState::Config& config() { return audio_state_config; } |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 38 | rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 41 | AudioState::Config audio_state_config; |
| 42 | rtc::scoped_refptr<AudioMixer> audio_mixer; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 43 | }; |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 44 | |
| 45 | class 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 Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 59 | std::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 | |
| 66 | std::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) { |
| 74 | audio_data[i * num_channels] = |
| 75 | static_cast<int16_t>(32767.f * std::sin(w)); |
| 76 | w += inc; |
| 77 | } |
| 78 | return audio_data; |
| 79 | } |
| 80 | |
| 81 | std::vector<uint32_t> ComputeChannelLevels(AudioFrame* audio_frame) { |
| 82 | const size_t num_channels = audio_frame->num_channels_; |
| 83 | const size_t samples_per_channel = audio_frame->samples_per_channel_; |
| 84 | std::vector<uint32_t> levels(num_channels, 0); |
| 85 | for (size_t i = 0; i < samples_per_channel; ++i) { |
| 86 | for (size_t j = 0; j < num_channels; ++j) { |
| 87 | levels[j] += std::abs(audio_frame->data()[i * num_channels + j]); |
| 88 | } |
| 89 | } |
| 90 | return levels; |
| 91 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | TEST(AudioStateTest, Create) { |
| 95 | ConfigHelper helper; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 96 | auto audio_state = AudioState::Create(helper.config()); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(audio_state.get()); |
| 98 | } |
| 99 | |
| 100 | TEST(AudioStateTest, ConstructDestruct) { |
| 101 | ConfigHelper helper; |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 102 | std::unique_ptr<internal::AudioState> audio_state( |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 103 | new internal::AudioState(helper.config())); |
| 104 | } |
| 105 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 106 | TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) { |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 107 | ConfigHelper helper; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 108 | std::unique_ptr<internal::AudioState> audio_state( |
| 109 | new internal::AudioState(helper.config())); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 110 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 111 | MockAudioSendStream stream; |
| 112 | audio_state->AddSendingStream(&stream, 8000, 2); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 113 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 114 | EXPECT_CALL(stream, SendAudioDataForMock(testing::AllOf( |
| 115 | testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(8000)), |
| 116 | testing::Field(&AudioFrame::num_channels_, testing::Eq(2u))))) |
| 117 | .WillOnce( |
| 118 | // Verify that channels are not swapped by default. |
| 119 | testing::Invoke([](AudioFrame* audio_frame) { |
| 120 | auto levels = ComputeChannelLevels(audio_frame); |
| 121 | EXPECT_LT(0u, levels[0]); |
| 122 | EXPECT_EQ(0u, levels[1]); |
| 123 | })); |
| 124 | MockAudioProcessing* ap = |
| 125 | static_cast<MockAudioProcessing*>(audio_state->audio_processing()); |
| 126 | EXPECT_CALL(*ap, set_stream_delay_ms(0)); |
| 127 | EXPECT_CALL(*ap, set_stream_key_pressed(false)); |
| 128 | EXPECT_CALL(*ap, ProcessStream(testing::_)); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 129 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 130 | constexpr int kSampleRate = 16000; |
| 131 | constexpr size_t kNumChannels = 2; |
| 132 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 133 | uint32_t new_mic_level = 667; |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 134 | audio_state->audio_transport()->RecordedDataIsAvailable( |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 135 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, |
| 136 | kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level); |
| 137 | EXPECT_EQ(667u, new_mic_level); |
| 138 | |
| 139 | audio_state->RemoveSendingStream(&stream); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 142 | TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) { |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 143 | ConfigHelper helper; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 144 | std::unique_ptr<internal::AudioState> audio_state( |
| 145 | new internal::AudioState(helper.config())); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 146 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 147 | MockAudioSendStream stream_1; |
| 148 | MockAudioSendStream stream_2; |
| 149 | audio_state->AddSendingStream(&stream_1, 8001, 2); |
| 150 | audio_state->AddSendingStream(&stream_2, 32000, 1); |
| 151 | |
| 152 | EXPECT_CALL(stream_1, SendAudioDataForMock(testing::AllOf( |
| 153 | testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)), |
| 154 | testing::Field(&AudioFrame::num_channels_, testing::Eq(1u))))) |
| 155 | .WillOnce( |
| 156 | // Verify that there is output signal. |
| 157 | testing::Invoke([](AudioFrame* audio_frame) { |
| 158 | auto levels = ComputeChannelLevels(audio_frame); |
| 159 | EXPECT_LT(0u, levels[0]); |
| 160 | })); |
| 161 | EXPECT_CALL(stream_2, SendAudioDataForMock(testing::AllOf( |
| 162 | testing::Field(&AudioFrame::sample_rate_hz_, testing::Eq(16000)), |
| 163 | testing::Field(&AudioFrame::num_channels_, testing::Eq(1u))))) |
| 164 | .WillOnce( |
| 165 | // Verify that there is output signal. |
| 166 | testing::Invoke([](AudioFrame* audio_frame) { |
| 167 | auto levels = ComputeChannelLevels(audio_frame); |
| 168 | EXPECT_LT(0u, levels[0]); |
| 169 | })); |
| 170 | MockAudioProcessing* ap = |
| 171 | static_cast<MockAudioProcessing*>(audio_state->audio_processing()); |
| 172 | EXPECT_CALL(*ap, set_stream_delay_ms(5)); |
| 173 | EXPECT_CALL(*ap, set_stream_key_pressed(true)); |
| 174 | EXPECT_CALL(*ap, ProcessStream(testing::_)); |
| 175 | |
| 176 | constexpr int kSampleRate = 16000; |
| 177 | constexpr size_t kNumChannels = 1; |
| 178 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 179 | uint32_t new_mic_level = 667; |
| 180 | audio_state->audio_transport()->RecordedDataIsAvailable( |
| 181 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, |
| 182 | kNumChannels, kSampleRate, 5, 0, 0, true, new_mic_level); |
| 183 | EXPECT_EQ(667u, new_mic_level); |
| 184 | |
| 185 | audio_state->RemoveSendingStream(&stream_1); |
| 186 | audio_state->RemoveSendingStream(&stream_2); |
| 187 | } |
| 188 | |
| 189 | TEST(AudioStateTest, EnableChannelSwap) { |
| 190 | constexpr int kSampleRate = 16000; |
| 191 | constexpr size_t kNumChannels = 2; |
| 192 | |
| 193 | ConfigHelper helper; |
| 194 | std::unique_ptr<internal::AudioState> audio_state( |
| 195 | new internal::AudioState(helper.config())); |
| 196 | audio_state->SetStereoChannelSwapping(true); |
| 197 | |
| 198 | MockAudioSendStream stream; |
| 199 | audio_state->AddSendingStream(&stream, kSampleRate, kNumChannels); |
| 200 | |
| 201 | EXPECT_CALL(stream, SendAudioDataForMock(testing::_)) |
| 202 | .WillOnce( |
| 203 | // Verify that channels are swapped. |
| 204 | testing::Invoke([](AudioFrame* audio_frame) { |
| 205 | auto levels = ComputeChannelLevels(audio_frame); |
| 206 | EXPECT_EQ(0u, levels[0]); |
| 207 | EXPECT_LT(0u, levels[1]); |
| 208 | })); |
| 209 | |
| 210 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 211 | uint32_t new_mic_level = 667; |
| 212 | audio_state->audio_transport()->RecordedDataIsAvailable( |
| 213 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, |
| 214 | kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level); |
| 215 | EXPECT_EQ(667u, new_mic_level); |
| 216 | |
| 217 | audio_state->RemoveSendingStream(&stream); |
| 218 | } |
| 219 | |
| 220 | TEST(AudioStateTest, InputLevelStats) { |
| 221 | constexpr int kSampleRate = 16000; |
| 222 | constexpr size_t kNumChannels = 1; |
| 223 | |
| 224 | ConfigHelper helper; |
| 225 | std::unique_ptr<internal::AudioState> audio_state( |
| 226 | new internal::AudioState(helper.config())); |
| 227 | |
| 228 | // Push a silent buffer -> Level stats should be zeros except for duration. |
| 229 | { |
| 230 | auto audio_data = Create10msSilentTestData(kSampleRate, kNumChannels); |
| 231 | uint32_t new_mic_level = 667; |
| 232 | audio_state->audio_transport()->RecordedDataIsAvailable( |
| 233 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, |
| 234 | kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level); |
| 235 | auto stats = audio_state->GetAudioInputStats(); |
| 236 | EXPECT_EQ(0, stats.audio_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 237 | EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.0)); |
| 238 | EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.01)); |
| 239 | } |
| 240 | |
| 241 | // Push 10 non-silent buffers -> Level stats should be non-zero. |
| 242 | { |
| 243 | auto audio_data = Create10msTestData(kSampleRate, kNumChannels); |
| 244 | uint32_t new_mic_level = 667; |
| 245 | for (int i = 0; i < 10; ++i) { |
| 246 | audio_state->audio_transport()->RecordedDataIsAvailable( |
| 247 | &audio_data[0], kSampleRate / 100, kNumChannels * 2, |
| 248 | kNumChannels, kSampleRate, 0, 0, 0, false, new_mic_level); |
| 249 | } |
| 250 | auto stats = audio_state->GetAudioInputStats(); |
| 251 | EXPECT_EQ(32767, stats.audio_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 252 | EXPECT_THAT(stats.total_energy, testing::DoubleEq(0.01)); |
| 253 | EXPECT_THAT(stats.total_duration, testing::DoubleEq(0.11)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | TEST(AudioStateTest, |
| 258 | QueryingTransportForAudioShouldResultInGetAudioCallOnMixerSource) { |
| 259 | ConfigHelper helper; |
| 260 | auto audio_state = AudioState::Create(helper.config()); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 261 | |
| 262 | FakeAudioSource fake_source; |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 263 | helper.mixer()->AddSource(&fake_source); |
| 264 | |
| 265 | EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_)) |
| 266 | .WillOnce( |
| 267 | testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) { |
| 268 | audio_frame->sample_rate_hz_ = sample_rate_hz; |
| 269 | audio_frame->samples_per_channel_ = sample_rate_hz / 100; |
| 270 | audio_frame->num_channels_ = kNumberOfChannels; |
| 271 | return AudioMixer::Source::AudioFrameInfo::kNormal; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 272 | })); |
| 273 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 274 | int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels]; |
| 275 | size_t n_samples_out; |
| 276 | int64_t elapsed_time_ms; |
| 277 | int64_t ntp_time_ms; |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 278 | audio_state->audio_transport()->NeedMorePlayData( |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 279 | kSampleRate / 100, kNumberOfChannels * 2, kNumberOfChannels, kSampleRate, |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 280 | audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 281 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 282 | } // namespace test |
| 283 | } // namespace webrtc |