| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "webrtc/audio/audio_state.h" | 13 #include "webrtc/audio/audio_state.h" |
| 14 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h" | 14 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h" |
| 15 #include "webrtc/test/gtest.h" | 15 #include "webrtc/test/gtest.h" |
| 16 #include "webrtc/test/mock_voice_engine.h" | 16 #include "webrtc/test/mock_voice_engine.h" |
| 17 | 17 |
| 18 namespace webrtc { | 18 namespace webrtc { |
| 19 namespace test { | 19 namespace test { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const int kSampleRate = 8000; |
| 23 const int kNumberOfChannels = 1; |
| 24 const int kBytesPerSample = 2; |
| 25 |
| 22 struct ConfigHelper { | 26 struct ConfigHelper { |
| 23 ConfigHelper() { | 27 ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) { |
| 24 EXPECT_CALL(voice_engine_, | 28 EXPECT_CALL(mock_voice_engine, RegisterVoiceEngineObserver(testing::_)) |
| 25 RegisterVoiceEngineObserver(testing::_)).WillOnce(testing::Return(0)); | 29 .WillOnce(testing::Return(0)); |
| 26 EXPECT_CALL(voice_engine_, | 30 EXPECT_CALL(mock_voice_engine, DeRegisterVoiceEngineObserver()) |
| 27 DeRegisterVoiceEngineObserver()).WillOnce(testing::Return(0)); | 31 .WillOnce(testing::Return(0)); |
| 28 EXPECT_CALL(voice_engine_, audio_device_module()); | 32 EXPECT_CALL(mock_voice_engine, audio_device_module()) |
| 29 EXPECT_CALL(voice_engine_, audio_processing()); | 33 .Times(testing::AtLeast(1)); |
| 30 EXPECT_CALL(voice_engine_, audio_transport()); | 34 EXPECT_CALL(mock_voice_engine, audio_processing()) |
| 35 .Times(testing::AtLeast(1)); |
| 36 EXPECT_CALL(mock_voice_engine, audio_transport()) |
| 37 .WillRepeatedly(testing::Return(&audio_transport)); |
| 31 | 38 |
| 32 config_.voice_engine = &voice_engine_; | 39 auto device = static_cast<MockAudioDeviceModule*>( |
| 33 config_.audio_mixer = AudioMixerImpl::Create(); | 40 voice_engine().audio_device_module()); |
| 41 |
| 42 // Populate the audio transport proxy pointer to the most recent |
| 43 // transport connected to the Audio Device. |
| 44 ON_CALL(*device, RegisterAudioCallback(testing::_)) |
| 45 .WillByDefault(testing::Invoke([this](AudioTransport* transport) { |
| 46 registered_audio_transport = transport; |
| 47 return 0; |
| 48 })); |
| 49 |
| 50 audio_state_config.voice_engine = &mock_voice_engine; |
| 51 audio_state_config.audio_mixer = audio_mixer; |
| 34 } | 52 } |
| 35 AudioState::Config& config() { return config_; } | 53 AudioState::Config& config() { return audio_state_config; } |
| 36 MockVoiceEngine& voice_engine() { return voice_engine_; } | 54 MockVoiceEngine& voice_engine() { return mock_voice_engine; } |
| 55 rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; } |
| 56 MockAudioTransport& original_audio_transport() { return audio_transport; } |
| 57 AudioTransport* audio_transport_proxy() { return registered_audio_transport; } |
| 37 | 58 |
| 38 private: | 59 private: |
| 39 testing::StrictMock<MockVoiceEngine> voice_engine_; | 60 testing::StrictMock<MockVoiceEngine> mock_voice_engine; |
| 40 AudioState::Config config_; | 61 AudioState::Config audio_state_config; |
| 62 rtc::scoped_refptr<AudioMixer> audio_mixer; |
| 63 MockAudioTransport audio_transport; |
| 64 AudioTransport* registered_audio_transport = nullptr; |
| 41 }; | 65 }; |
| 66 |
| 67 class FakeAudioSource : public AudioMixer::Source { |
| 68 public: |
| 69 // TODO(aleloi): Valid overrides commented out, because the gmock |
| 70 // methods don't use any override declarations, and we want to avoid |
| 71 // warnings from -Winconsistent-missing-override. See |
| 72 // http://crbug.com/428099. |
| 73 int Ssrc() const /*override*/ { return 0; } |
| 74 |
| 75 int PreferredSampleRate() const /*override*/ { return kSampleRate; } |
| 76 |
| 77 MOCK_METHOD2(GetAudioFrameWithInfo, |
| 78 AudioFrameInfo(int sample_rate_hz, AudioFrame* audio_frame)); |
| 79 }; |
| 80 |
| 42 } // namespace | 81 } // namespace |
| 43 | 82 |
| 44 TEST(AudioStateTest, Create) { | 83 TEST(AudioStateTest, Create) { |
| 45 ConfigHelper helper; | 84 ConfigHelper helper; |
| 46 rtc::scoped_refptr<AudioState> audio_state = | 85 rtc::scoped_refptr<AudioState> audio_state = |
| 47 AudioState::Create(helper.config()); | 86 AudioState::Create(helper.config()); |
| 48 EXPECT_TRUE(audio_state.get()); | 87 EXPECT_TRUE(audio_state.get()); |
| 49 } | 88 } |
| 50 | 89 |
| 51 TEST(AudioStateTest, ConstructDestruct) { | 90 TEST(AudioStateTest, ConstructDestruct) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 77 voe_observer->CallbackOnError(-1, VE_NOT_INITED); | 116 voe_observer->CallbackOnError(-1, VE_NOT_INITED); |
| 78 EXPECT_TRUE(audio_state->typing_noise_detected()); | 117 EXPECT_TRUE(audio_state->typing_noise_detected()); |
| 79 | 118 |
| 80 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING); | 119 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING); |
| 81 EXPECT_FALSE(audio_state->typing_noise_detected()); | 120 EXPECT_FALSE(audio_state->typing_noise_detected()); |
| 82 voe_observer->CallbackOnError(-1, VE_NOT_INITED); | 121 voe_observer->CallbackOnError(-1, VE_NOT_INITED); |
| 83 EXPECT_FALSE(audio_state->typing_noise_detected()); | 122 EXPECT_FALSE(audio_state->typing_noise_detected()); |
| 84 } | 123 } |
| 85 | 124 |
| 86 // Test that RecordedDataIsAvailable calls get to the original transport. | 125 // Test that RecordedDataIsAvailable calls get to the original transport. |
| 87 TEST(AudioStateTest, RecordedAudioArrivesAtOriginalTransport) { | 126 TEST(AudioStateAudioPathTest, RecordedAudioArrivesAtOriginalTransport) { |
| 88 using testing::_; | 127 ConfigHelper helper; |
| 89 | 128 |
| 129 rtc::scoped_refptr<AudioState> audio_state = |
| 130 AudioState::Create(helper.config()); |
| 131 |
| 132 // Setup completed. Ensure call of original transport is forwarded to new. |
| 133 uint32_t new_mic_level; |
| 134 EXPECT_CALL( |
| 135 helper.original_audio_transport(), |
| 136 RecordedDataIsAvailable(nullptr, kSampleRate / 100, kBytesPerSample, |
| 137 kNumberOfChannels, kSampleRate, 0, 0, 0, false, |
| 138 testing::Ref(new_mic_level))); |
| 139 |
| 140 helper.audio_transport_proxy()->RecordedDataIsAvailable( |
| 141 nullptr, kSampleRate / 100, kBytesPerSample, kNumberOfChannels, |
| 142 kSampleRate, 0, 0, 0, false, new_mic_level); |
| 143 } |
| 144 |
| 145 TEST(AudioStateAudioPathTest, |
| 146 QueryingProxyForAudioShouldResultInGetAudioCallOnMixerSource) { |
| 90 ConfigHelper helper; | 147 ConfigHelper helper; |
| 91 auto& voice_engine = helper.voice_engine(); | |
| 92 auto device = | |
| 93 static_cast<MockAudioDeviceModule*>(voice_engine.audio_device_module()); | |
| 94 | 148 |
| 95 AudioTransport* audio_transport_proxy = nullptr; | 149 rtc::scoped_refptr<AudioState> audio_state = |
| 96 ON_CALL(*device, RegisterAudioCallback(_)) | 150 AudioState::Create(helper.config()); |
| 97 .WillByDefault( | 151 |
| 98 testing::Invoke([&audio_transport_proxy](AudioTransport* transport) { | 152 FakeAudioSource fake_source; |
| 99 audio_transport_proxy = transport; | 153 |
| 100 return 0; | 154 helper.mixer()->AddSource(&fake_source); |
| 155 |
| 156 EXPECT_CALL(fake_source, GetAudioFrameWithInfo(testing::_, testing::_)) |
| 157 .WillOnce( |
| 158 testing::Invoke([](int sample_rate_hz, AudioFrame* audio_frame) { |
| 159 audio_frame->sample_rate_hz_ = sample_rate_hz; |
| 160 audio_frame->samples_per_channel_ = sample_rate_hz / 100; |
| 161 audio_frame->num_channels_ = kNumberOfChannels; |
| 162 return AudioMixer::Source::AudioFrameInfo::kNormal; |
| 101 })); | 163 })); |
| 102 | 164 |
| 103 MockAudioTransport original_audio_transport; | 165 int16_t audio_buffer[kSampleRate / 100 * kNumberOfChannels]; |
| 104 ON_CALL(voice_engine, audio_transport()) | 166 size_t n_samples_out; |
| 105 .WillByDefault(testing::Return(&original_audio_transport)); | 167 int64_t elapsed_time_ms; |
| 106 | 168 int64_t ntp_time_ms; |
| 107 EXPECT_CALL(voice_engine, audio_device_module()); | 169 helper.audio_transport_proxy()->NeedMorePlayData( |
| 108 std::unique_ptr<internal::AudioState> audio_state( | 170 kSampleRate / 100, kBytesPerSample, kNumberOfChannels, kSampleRate, |
| 109 new internal::AudioState(helper.config())); | 171 audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms); |
| 110 | |
| 111 // Setup completed. Ensure call of old transport is forwarded to new. | |
| 112 uint32_t new_mic_level; | |
| 113 EXPECT_CALL(original_audio_transport, | |
| 114 RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0, 0, false, | |
| 115 testing::Ref(new_mic_level))); | |
| 116 | |
| 117 audio_transport_proxy->RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0, | |
| 118 0, false, new_mic_level); | |
| 119 } | 172 } |
| 120 } // namespace test | 173 } // namespace test |
| 121 } // namespace webrtc | 174 } // namespace webrtc |
| OLD | NEW |