blob: 28b0a715f610ab269d0230cba015f61bd077d2aa [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
kwibergfffa42b2016-02-23 10:46:32 -080011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "audio/audio_state.h"
14#include "modules/audio_mixer/audio_mixer_impl.h"
15#include "modules/audio_processing/include/mock_audio_processing.h"
16#include "test/gtest.h"
17#include "test/mock_voice_engine.h"
solenberg566ef242015-11-06 15:34:49 -080018
19namespace webrtc {
20namespace test {
21namespace {
22
aleloi04c07222016-11-22 06:42:53 -080023const int kSampleRate = 8000;
24const int kNumberOfChannels = 1;
25const int kBytesPerSample = 2;
aleloidd310712016-11-17 06:28:59 -080026
aleloi04c07222016-11-22 06:42:53 -080027struct ConfigHelper {
28 ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) {
aleloi04c07222016-11-22 06:42:53 -080029 EXPECT_CALL(mock_voice_engine, audio_transport())
30 .WillRepeatedly(testing::Return(&audio_transport));
31
aleloi04c07222016-11-22 06:42:53 -080032 audio_state_config.voice_engine = &mock_voice_engine;
33 audio_state_config.audio_mixer = audio_mixer;
peaha9cc40b2017-06-29 08:32:09 -070034 audio_state_config.audio_processing =
35 new rtc::RefCountedObject<MockAudioProcessing>();
solenberg566ef242015-11-06 15:34:49 -080036 }
aleloi04c07222016-11-22 06:42:53 -080037 AudioState::Config& config() { return audio_state_config; }
38 MockVoiceEngine& voice_engine() { return mock_voice_engine; }
39 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
40 MockAudioTransport& original_audio_transport() { return audio_transport; }
solenberg566ef242015-11-06 15:34:49 -080041
42 private:
aleloi04c07222016-11-22 06:42:53 -080043 testing::StrictMock<MockVoiceEngine> mock_voice_engine;
44 AudioState::Config audio_state_config;
45 rtc::scoped_refptr<AudioMixer> audio_mixer;
46 MockAudioTransport audio_transport;
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
solenberg566ef242015-11-06 15:34:49 -080063} // namespace
64
65TEST(AudioStateTest, Create) {
66 ConfigHelper helper;
67 rtc::scoped_refptr<AudioState> audio_state =
68 AudioState::Create(helper.config());
69 EXPECT_TRUE(audio_state.get());
70}
71
72TEST(AudioStateTest, ConstructDestruct) {
73 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080074 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080075 new internal::AudioState(helper.config()));
76}
77
78TEST(AudioStateTest, GetVoiceEngine) {
79 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080080 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080081 new internal::AudioState(helper.config()));
82 EXPECT_EQ(audio_state->voice_engine(), &helper.voice_engine());
83}
84
aleloidd310712016-11-17 06:28:59 -080085// Test that RecordedDataIsAvailable calls get to the original transport.
aleloi04c07222016-11-22 06:42:53 -080086TEST(AudioStateAudioPathTest, RecordedAudioArrivesAtOriginalTransport) {
aleloidd310712016-11-17 06:28:59 -080087 ConfigHelper helper;
aleloidd310712016-11-17 06:28:59 -080088
aleloi04c07222016-11-22 06:42:53 -080089 rtc::scoped_refptr<AudioState> audio_state =
90 AudioState::Create(helper.config());
91
92 // Setup completed. Ensure call of original transport is forwarded to new.
93 uint32_t new_mic_level;
94 EXPECT_CALL(
95 helper.original_audio_transport(),
96 RecordedDataIsAvailable(nullptr, kSampleRate / 100, kBytesPerSample,
97 kNumberOfChannels, kSampleRate, 0, 0, 0, false,
98 testing::Ref(new_mic_level)));
99
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100100 audio_state->audio_transport()->RecordedDataIsAvailable(
aleloi04c07222016-11-22 06:42:53 -0800101 nullptr, kSampleRate / 100, kBytesPerSample, kNumberOfChannels,
102 kSampleRate, 0, 0, 0, false, new_mic_level);
103}
104
105TEST(AudioStateAudioPathTest,
106 QueryingProxyForAudioShouldResultInGetAudioCallOnMixerSource) {
107 ConfigHelper helper;
108
109 rtc::scoped_refptr<AudioState> audio_state =
110 AudioState::Create(helper.config());
111
112 FakeAudioSource fake_source;
113
114 helper.mixer()->AddSource(&fake_source);
115
116 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_))
117 .WillOnce(
118 testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) {
119 audio_frame->sample_rate_hz_ = sample_rate_hz;
120 audio_frame->samples_per_channel_ = sample_rate_hz / 100;
121 audio_frame->num_channels_ = kNumberOfChannels;
122 return AudioMixer::Source::AudioFrameInfo::kNormal;
aleloidd310712016-11-17 06:28:59 -0800123 }));
124
aleloi04c07222016-11-22 06:42:53 -0800125 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels];
126 size_t n_samples_out;
127 int64_t elapsed_time_ms;
128 int64_t ntp_time_ms;
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100129 audio_state->audio_transport()->NeedMorePlayData(
aleloi04c07222016-11-22 06:42:53 -0800130 kSampleRate / 100, kBytesPerSample, kNumberOfChannels, kSampleRate,
131 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
aleloidd310712016-11-17 06:28:59 -0800132}
solenberg566ef242015-11-06 15:34:49 -0800133} // namespace test
134} // namespace webrtc