| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/audio_util.h" | 5 #include "base/bind.h" |
| 6 #include "media/audio/null_audio_sink.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "media/base/audio_renderer_mixer.h" | 7 #include "media/base/audio_renderer_mixer.h" |
| 8 #include "media/base/audio_renderer_mixer_input.h" | 8 #include "media/base/audio_renderer_mixer_input.h" |
| 9 #include "media/base/fake_audio_render_callback.h" | 9 #include "media/base/fake_audio_render_callback.h" |
| 10 #include "media/base/mock_audio_renderer_sink.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace media { | 14 namespace media { |
| 14 | 15 |
| 15 static const int kBitsPerChannel = 16; | 16 static const int kBitsPerChannel = 16; |
| 16 static const int kSampleRate = 48000; | 17 static const int kSampleRate = 48000; |
| 18 static const int kBufferSize = 8192; |
| 17 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 19 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 18 | 20 |
| 19 class AudioRendererMixerInputTest : public ::testing::Test { | 21 class AudioRendererMixerInputTest : public testing::Test { |
| 20 public: | 22 public: |
| 21 AudioRendererMixerInputTest() { | 23 AudioRendererMixerInputTest() { |
| 22 audio_parameters_ = AudioParameters( | 24 audio_parameters_ = AudioParameters( |
| 23 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 25 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
| 24 kBitsPerChannel, GetHighLatencyOutputBufferSize(kSampleRate)); | 26 kBitsPerChannel, kBufferSize); |
| 25 | 27 |
| 26 mixer_ = new AudioRendererMixer(audio_parameters_, new NullAudioSink()); | 28 mixer_input_ = new AudioRendererMixerInput( |
| 27 mixer_input_ = new AudioRendererMixerInput(mixer_); | 29 base::Bind( |
| 28 fake_callback_.reset(new FakeAudioRenderCallback(audio_parameters_)); | 30 &AudioRendererMixerInputTest::GetMixer, base::Unretained(this)), |
| 31 base::Bind( |
| 32 &AudioRendererMixerInputTest::RemoveMixer, base::Unretained(this))); |
| 33 fake_callback_.reset(new FakeAudioRenderCallback(0)); |
| 29 mixer_input_->Initialize(audio_parameters_, fake_callback_.get()); | 34 mixer_input_->Initialize(audio_parameters_, fake_callback_.get()); |
| 35 EXPECT_CALL(*this, RemoveMixer(testing::_)); |
| 30 } | 36 } |
| 31 | 37 |
| 32 // Render audio_parameters_.frames_per_buffer() frames into |audio_data_| and | 38 AudioRendererMixer* GetMixer(const AudioParameters& params) { |
| 33 // verify the result against |check_value|. | 39 if (!mixer_.get()) { |
| 34 void RenderAndValidateAudioData(float check_value) { | 40 scoped_refptr<MockAudioRendererSink> sink = new MockAudioRendererSink(); |
| 35 const std::vector<float*>& audio_data = mixer_input_->audio_data(); | 41 EXPECT_CALL(*sink, Start()); |
| 42 EXPECT_CALL(*sink, Stop()); |
| 36 | 43 |
| 37 ASSERT_EQ(fake_callback_->Render( | 44 mixer_.reset(new AudioRendererMixer( |
| 38 audio_data, audio_parameters_.frames_per_buffer(), 0), | 45 audio_parameters_, audio_parameters_, sink)); |
| 39 audio_parameters_.frames_per_buffer()); | 46 } |
| 47 return mixer_.get(); |
| 48 } |
| 40 | 49 |
| 41 for (size_t i = 0; i < audio_data.size(); ++i) | 50 MOCK_METHOD1(RemoveMixer, void(const AudioParameters&)); |
| 42 for (int j = 0; j < audio_parameters_.frames_per_buffer(); j++) | |
| 43 ASSERT_FLOAT_EQ(check_value, audio_data[i][j]); | |
| 44 } | |
| 45 | 51 |
| 46 protected: | 52 protected: |
| 47 virtual ~AudioRendererMixerInputTest() {} | 53 virtual ~AudioRendererMixerInputTest() {} |
| 48 | 54 |
| 49 AudioParameters audio_parameters_; | 55 AudioParameters audio_parameters_; |
| 50 scoped_refptr<AudioRendererMixer> mixer_; | 56 scoped_ptr<AudioRendererMixer> mixer_; |
| 51 scoped_refptr<AudioRendererMixerInput> mixer_input_; | 57 scoped_refptr<AudioRendererMixerInput> mixer_input_; |
| 52 scoped_ptr<FakeAudioRenderCallback> fake_callback_; | 58 scoped_ptr<FakeAudioRenderCallback> fake_callback_; |
| 53 | 59 |
| 54 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInputTest); | 60 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInputTest); |
| 55 }; | 61 }; |
| 56 | 62 |
| 63 // Test callback() works as expected. |
| 64 TEST_F(AudioRendererMixerInputTest, GetCallback) { |
| 65 EXPECT_EQ(mixer_input_->callback(), fake_callback_.get()); |
| 66 } |
| 67 |
| 57 // Test that getting and setting the volume work as expected. | 68 // Test that getting and setting the volume work as expected. |
| 58 TEST_F(AudioRendererMixerInputTest, GetSetVolume) { | 69 TEST_F(AudioRendererMixerInputTest, GetSetVolume) { |
| 59 // Starting volume should be 0. | 70 // Starting volume should be 0. |
| 60 double volume = 1.0f; | 71 double volume = 1.0f; |
| 61 mixer_input_->GetVolume(&volume); | 72 mixer_input_->GetVolume(&volume); |
| 62 EXPECT_EQ(volume, 1.0f); | 73 EXPECT_EQ(volume, 1.0f); |
| 63 | 74 |
| 64 const double kVolume = 0.5f; | 75 const double kVolume = 0.5f; |
| 65 EXPECT_TRUE(mixer_input_->SetVolume(kVolume)); | 76 EXPECT_TRUE(mixer_input_->SetVolume(kVolume)); |
| 66 mixer_input_->GetVolume(&volume); | 77 mixer_input_->GetVolume(&volume); |
| 67 EXPECT_EQ(volume, kVolume); | 78 EXPECT_EQ(volume, kVolume); |
| 68 } | 79 } |
| 69 | 80 |
| 70 // Test audio_data() is allocated correctly. | |
| 71 TEST_F(AudioRendererMixerInputTest, GetAudioData) { | |
| 72 RenderAndValidateAudioData(fake_callback_->fill_value()); | |
| 73 | |
| 74 // TODO(dalecurtis): Perform alignment and size checks when we switch over to | |
| 75 // FFmpeg optimized vector_fmac. | |
| 76 } | |
| 77 | |
| 78 // Test callback() works as expected. | |
| 79 TEST_F(AudioRendererMixerInputTest, GetCallback) { | |
| 80 EXPECT_EQ(mixer_input_->callback(), fake_callback_.get()); | |
| 81 RenderAndValidateAudioData(fake_callback_->fill_value()); | |
| 82 } | |
| 83 | |
| 84 // Test Start()/Play()/Pause()/Stop()/playing() all work as expected. Also | 81 // Test Start()/Play()/Pause()/Stop()/playing() all work as expected. Also |
| 85 // implicitly tests that AddMixerInput() and RemoveMixerInput() work without | 82 // implicitly tests that AddMixerInput() and RemoveMixerInput() work without |
| 86 // crashing; functional tests for these methods are in AudioRendererMixerTest. | 83 // crashing; functional tests for these methods are in AudioRendererMixerTest. |
| 87 TEST_F(AudioRendererMixerInputTest, StartPlayPauseStopPlaying) { | 84 TEST_F(AudioRendererMixerInputTest, StartPlayPauseStopPlaying) { |
| 88 mixer_input_->Start(); | 85 mixer_input_->Start(); |
| 89 EXPECT_FALSE(mixer_input_->playing()); | 86 EXPECT_FALSE(mixer_input_->playing()); |
| 90 mixer_input_->Play(); | 87 mixer_input_->Play(); |
| 91 EXPECT_TRUE(mixer_input_->playing()); | 88 EXPECT_TRUE(mixer_input_->playing()); |
| 92 mixer_input_->Pause(false); | 89 mixer_input_->Pause(false); |
| 93 EXPECT_FALSE(mixer_input_->playing()); | 90 EXPECT_FALSE(mixer_input_->playing()); |
| 94 mixer_input_->Play(); | 91 mixer_input_->Play(); |
| 95 EXPECT_TRUE(mixer_input_->playing()); | 92 EXPECT_TRUE(mixer_input_->playing()); |
| 96 mixer_input_->Stop(); | 93 mixer_input_->Stop(); |
| 97 EXPECT_FALSE(mixer_input_->playing()); | 94 EXPECT_FALSE(mixer_input_->playing()); |
| 98 } | 95 } |
| 99 | 96 |
| 100 } // namespace media | 97 } // namespace media |
| OLD | NEW |