| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "content/renderer/media/audio_device_factory.h" |
| 7 #include "content/renderer/media/audio_renderer_mixer_manager.h" |
| 8 #include "media/base/audio_renderer_mixer.h" |
| 9 #include "media/base/audio_renderer_mixer_input.h" |
| 10 #include "media/base/fake_audio_render_callback.h" |
| 11 #include "media/base/mock_audio_renderer_sink.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 static const int kBitsPerChannel = 16; |
| 18 static const int kSampleRate = 48000; |
| 19 static const int kBufferSize = 8192; |
| 20 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 21 |
| 22 // By sub-classing AudioDeviceFactory we've overridden the factory to use our |
| 23 // CreateAudioDevice() method globally. |
| 24 class MockAudioRenderSinkFactory : public AudioDeviceFactory { |
| 25 public: |
| 26 MockAudioRenderSinkFactory() {} |
| 27 virtual ~MockAudioRenderSinkFactory() {} |
| 28 |
| 29 protected: |
| 30 virtual media::MockAudioRendererSink* CreateAudioDevice() { |
| 31 media::MockAudioRendererSink* sink = new media::MockAudioRendererSink(); |
| 32 EXPECT_CALL(*sink, Start()); |
| 33 EXPECT_CALL(*sink, Stop()); |
| 34 return sink; |
| 35 } |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(MockAudioRenderSinkFactory); |
| 38 }; |
| 39 |
| 40 // Specialization of AudioRendererMixerManager which removes all dependencies on |
| 41 // real hardware. |
| 42 class HardwareFreeARM : public AudioRendererMixerManager { |
| 43 public: |
| 44 HardwareFreeARM() {} |
| 45 virtual ~HardwareFreeARM() {} |
| 46 |
| 47 protected: |
| 48 virtual media::AudioParameters GetAudioHardwareParameters( |
| 49 const media::AudioParameters& params) { |
| 50 static const int kSampleRate = 48000; |
| 51 static const int kBitsPerChannel = 16; |
| 52 static const int kBufferSize = 1024; |
| 53 return media::AudioParameters( |
| 54 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, params.channel_layout(), |
| 55 kSampleRate, kBitsPerChannel, kBufferSize); |
| 56 } |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(HardwareFreeARM); |
| 59 }; |
| 60 |
| 61 class AudioRendererMixerManagerTest : public testing::Test { |
| 62 public: |
| 63 AudioRendererMixerManagerTest() { |
| 64 // We don't want to deal with instantiating a real AudioDevice since it's |
| 65 // not important to our testing, so use a mock AudioDeviceFactory. |
| 66 mock_sink_factory_.reset(new MockAudioRenderSinkFactory()); |
| 67 manager_.reset(new HardwareFreeARM()); |
| 68 } |
| 69 |
| 70 media::AudioRendererMixer* GetMixer(const media::AudioParameters& params) { |
| 71 return manager_->GetMixer(params); |
| 72 } |
| 73 |
| 74 void RemoveMixer(const media::AudioParameters& params) { |
| 75 return manager_->RemoveMixer(params); |
| 76 } |
| 77 |
| 78 // Number of instantiated mixers. |
| 79 int mixer_count() { |
| 80 return manager_->mixers_.size(); |
| 81 } |
| 82 |
| 83 protected: |
| 84 scoped_ptr<MockAudioRenderSinkFactory> mock_sink_factory_; |
| 85 scoped_ptr<AudioRendererMixerManager> manager_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManagerTest); |
| 88 }; |
| 89 |
| 90 // Verify GetMixer() and RemoveMixer() both work as expected; particularly with |
| 91 // respect to the explicit ref counting done. |
| 92 TEST_F(AudioRendererMixerManagerTest, GetRemoveMixer) { |
| 93 // There should be no mixers outstanding to start with. |
| 94 EXPECT_EQ(mixer_count(), 0); |
| 95 |
| 96 media::AudioParameters params1( |
| 97 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
| 98 kBitsPerChannel, kBufferSize); |
| 99 |
| 100 media::AudioRendererMixer* mixer1 = GetMixer(params1); |
| 101 ASSERT_TRUE(mixer1); |
| 102 EXPECT_EQ(mixer_count(), 1); |
| 103 |
| 104 // The same parameters should return the same mixer1. |
| 105 EXPECT_EQ(mixer1, GetMixer(params1)); |
| 106 EXPECT_EQ(mixer_count(), 1); |
| 107 |
| 108 // Remove the extra mixer we just acquired. |
| 109 RemoveMixer(params1); |
| 110 EXPECT_EQ(mixer_count(), 1); |
| 111 |
| 112 media::AudioParameters params2( |
| 113 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate * 2, |
| 114 kBitsPerChannel, kBufferSize * 2); |
| 115 media::AudioRendererMixer* mixer2 = GetMixer(params2); |
| 116 ASSERT_TRUE(mixer2); |
| 117 EXPECT_EQ(mixer_count(), 2); |
| 118 |
| 119 // Different parameters should result in a different mixer1. |
| 120 EXPECT_NE(mixer1, mixer2); |
| 121 |
| 122 // Remove both outstanding mixers. |
| 123 RemoveMixer(params1); |
| 124 EXPECT_EQ(mixer_count(), 1); |
| 125 RemoveMixer(params2); |
| 126 EXPECT_EQ(mixer_count(), 0); |
| 127 |
| 128 // Verify the old mixer was actually removed and we get a new one if we send |
| 129 // the same audio parameters again. |
| 130 EXPECT_NE(mixer1, GetMixer(params1)); |
| 131 EXPECT_EQ(mixer_count(), 1); |
| 132 RemoveMixer(params1); |
| 133 } |
| 134 |
| 135 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate |
| 136 // callbacks and they are working as expected. |
| 137 TEST_F(AudioRendererMixerManagerTest, CreateInput) { |
| 138 media::AudioParameters params( |
| 139 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
| 140 kBitsPerChannel, kBufferSize); |
| 141 |
| 142 // Create a mixer input and ensure it doesn't instantiate a mixer yet. |
| 143 EXPECT_EQ(mixer_count(), 0); |
| 144 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput()); |
| 145 EXPECT_EQ(mixer_count(), 0); |
| 146 |
| 147 // Implicitly test that AudioRendererMixerInput was provided with the expected |
| 148 // callbacks needed to acquire an AudioRendererMixer and remove it. |
| 149 media::FakeAudioRenderCallback callback(0); |
| 150 input->Initialize(params, &callback); |
| 151 EXPECT_EQ(mixer_count(), 1); |
| 152 |
| 153 // Destroying the input should destroy the mixer. |
| 154 input = NULL; |
| 155 EXPECT_EQ(mixer_count(), 0); |
| 156 } |
| 157 |
| 158 } // namespace content |
| OLD | NEW |