| 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 "media/audio/audio_util.h" |
| 6 #include "media/audio/null_audio_sink.h" | 6 #include "media/audio/null_audio_sink.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 "testing/gtest/include/gtest/gtest.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 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 // TODO(dalecurtis): Extract this out to a mock class, |
| 16 // used by AudioRendererImplTest too. |
| 17 class MockAudioSink : public media::AudioRendererSink { |
| 18 public: |
| 19 MOCK_METHOD2(Initialize, void(const media::AudioParameters& params, |
| 20 RenderCallback* callback)); |
| 21 MOCK_METHOD0(Start, void()); |
| 22 MOCK_METHOD0(Stop, void()); |
| 23 MOCK_METHOD1(Pause, void(bool flush)); |
| 24 MOCK_METHOD0(Play, void()); |
| 25 MOCK_METHOD1(SetPlaybackRate, void(float rate)); |
| 26 MOCK_METHOD1(SetVolume, bool(double volume)); |
| 27 MOCK_METHOD1(GetVolume, void(double* volume)); |
| 28 |
| 29 protected: |
| 30 virtual ~MockAudioSink() {} |
| 31 }; |
| 32 |
| 15 static const int kBitsPerChannel = 16; | 33 static const int kBitsPerChannel = 16; |
| 16 static const int kSampleRate = 48000; | 34 static const int kSampleRate = 48000; |
| 17 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 35 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 18 | 36 |
| 19 class AudioRendererMixerInputTest : public ::testing::Test { | 37 class AudioRendererMixerInputTest |
| 38 : public AudioRendererSink::RenderCallback, |
| 39 public ::testing::Test { |
| 20 public: | 40 public: |
| 21 AudioRendererMixerInputTest() { | 41 AudioRendererMixerInputTest() { |
| 22 audio_parameters_ = AudioParameters( | 42 audio_parameters_ = AudioParameters( |
| 23 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 43 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
| 24 kBitsPerChannel, GetHighLatencyOutputBufferSize(kSampleRate)); | 44 kBitsPerChannel, GetHighLatencyOutputBufferSize(kSampleRate)); |
| 25 | 45 |
| 26 mixer_ = new AudioRendererMixer(audio_parameters_, new NullAudioSink()); | 46 sink_ = new MockAudioSink(); |
| 47 EXPECT_CALL(*sink_, Start()); |
| 48 EXPECT_CALL(*sink_, Stop()); |
| 49 EXPECT_CALL(*sink_, Initialize(::testing::_, ::testing::_)); |
| 50 |
| 51 mixer_ = new AudioRendererMixer(audio_parameters_, sink_); |
| 27 mixer_input_ = new AudioRendererMixerInput(mixer_); | 52 mixer_input_ = new AudioRendererMixerInput(mixer_); |
| 28 fake_callback_.reset(new FakeAudioRenderCallback(audio_parameters_)); | 53 mixer_input_->Initialize(audio_parameters_, this); |
| 29 mixer_input_->Initialize(audio_parameters_, fake_callback_.get()); | |
| 30 } | |
| 31 | |
| 32 // Render audio_parameters_.frames_per_buffer() frames into |audio_data_| and | |
| 33 // verify the result against |check_value|. | |
| 34 void RenderAndValidateAudioData(float check_value) { | |
| 35 const std::vector<float*>& audio_data = mixer_input_->audio_data(); | |
| 36 | |
| 37 ASSERT_EQ(fake_callback_->Render( | |
| 38 audio_data, audio_parameters_.frames_per_buffer(), 0), | |
| 39 audio_parameters_.frames_per_buffer()); | |
| 40 | |
| 41 for (size_t i = 0; i < audio_data.size(); ++i) | |
| 42 for (int j = 0; j < audio_parameters_.frames_per_buffer(); j++) | |
| 43 ASSERT_FLOAT_EQ(check_value, audio_data[i][j]); | |
| 44 } | 54 } |
| 45 | 55 |
| 46 protected: | 56 protected: |
| 47 virtual ~AudioRendererMixerInputTest() {} | 57 virtual ~AudioRendererMixerInputTest() {} |
| 48 | 58 |
| 59 // AudioRendererSink::RenderCallback implementation. |
| 60 MOCK_METHOD3(Render, int(const std::vector<float*>&, int, int)); |
| 61 MOCK_METHOD0(OnRenderError, void()); |
| 62 |
| 49 AudioParameters audio_parameters_; | 63 AudioParameters audio_parameters_; |
| 50 scoped_refptr<AudioRendererMixer> mixer_; | 64 scoped_refptr<AudioRendererMixer> mixer_; |
| 51 scoped_refptr<AudioRendererMixerInput> mixer_input_; | 65 scoped_refptr<AudioRendererMixerInput> mixer_input_; |
| 52 scoped_ptr<FakeAudioRenderCallback> fake_callback_; | 66 scoped_refptr<MockAudioSink> sink_; |
| 53 | 67 |
| 54 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInputTest); | 68 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInputTest); |
| 55 }; | 69 }; |
| 56 | 70 |
| 71 // Test callback() works as expected. |
| 72 TEST_F(AudioRendererMixerInputTest, GetCallback) { |
| 73 EXPECT_EQ(mixer_input_->callback(), this); |
| 74 } |
| 75 |
| 57 // Test that getting and setting the volume work as expected. | 76 // Test that getting and setting the volume work as expected. |
| 58 TEST_F(AudioRendererMixerInputTest, GetSetVolume) { | 77 TEST_F(AudioRendererMixerInputTest, GetSetVolume) { |
| 59 // Starting volume should be 0. | 78 // Starting volume should be 0. |
| 60 double volume = 1.0f; | 79 double volume = 1.0f; |
| 61 mixer_input_->GetVolume(&volume); | 80 mixer_input_->GetVolume(&volume); |
| 62 EXPECT_EQ(volume, 1.0f); | 81 EXPECT_EQ(volume, 1.0f); |
| 63 | 82 |
| 64 const double kVolume = 0.5f; | 83 const double kVolume = 0.5f; |
| 65 EXPECT_TRUE(mixer_input_->SetVolume(kVolume)); | 84 EXPECT_TRUE(mixer_input_->SetVolume(kVolume)); |
| 66 mixer_input_->GetVolume(&volume); | 85 mixer_input_->GetVolume(&volume); |
| 67 EXPECT_EQ(volume, kVolume); | 86 EXPECT_EQ(volume, kVolume); |
| 68 } | 87 } |
| 69 | 88 |
| 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 | 89 // Test Start()/Play()/Pause()/Stop()/playing() all work as expected. Also |
| 85 // implicitly tests that AddMixerInput() and RemoveMixerInput() work without | 90 // implicitly tests that AddMixerInput() and RemoveMixerInput() work without |
| 86 // crashing; functional tests for these methods are in AudioRendererMixerTest. | 91 // crashing; functional tests for these methods are in AudioRendererMixerTest. |
| 87 TEST_F(AudioRendererMixerInputTest, StartPlayPauseStopPlaying) { | 92 TEST_F(AudioRendererMixerInputTest, StartPlayPauseStopPlaying) { |
| 88 mixer_input_->Start(); | 93 mixer_input_->Start(); |
| 89 EXPECT_FALSE(mixer_input_->playing()); | 94 EXPECT_FALSE(mixer_input_->playing()); |
| 90 mixer_input_->Play(); | 95 mixer_input_->Play(); |
| 91 EXPECT_TRUE(mixer_input_->playing()); | 96 EXPECT_TRUE(mixer_input_->playing()); |
| 92 mixer_input_->Pause(false); | 97 mixer_input_->Pause(false); |
| 93 EXPECT_FALSE(mixer_input_->playing()); | 98 EXPECT_FALSE(mixer_input_->playing()); |
| 94 mixer_input_->Play(); | 99 mixer_input_->Play(); |
| 95 EXPECT_TRUE(mixer_input_->playing()); | 100 EXPECT_TRUE(mixer_input_->playing()); |
| 96 mixer_input_->Stop(); | 101 mixer_input_->Stop(); |
| 97 EXPECT_FALSE(mixer_input_->playing()); | 102 EXPECT_FALSE(mixer_input_->playing()); |
| 98 } | 103 } |
| 99 | 104 |
| 100 } // namespace media | 105 } // namespace media |
| OLD | NEW |