Chromium Code Reviews| 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 virtual media::MockAudioRendererSink* CreateAudioDevice() { | |
| 26 media::MockAudioRendererSink* sink = new media::MockAudioRendererSink(); | |
| 27 EXPECT_CALL(*sink, Start()); | |
| 28 EXPECT_CALL(*sink, Stop()); | |
| 29 return sink; | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 class AudioRendererMixerManagerTest : public testing::Test { | |
| 34 public: | |
| 35 AudioRendererMixerManagerTest() { | |
| 36 // We don't want to deal with instantiating a real AudioDevice since it's | |
| 37 // not important to our testing, so use a mock AudioDeviceFactory. | |
| 38 mock_sink_factory_.reset(new MockAudioRenderSinkFactory()); | |
| 39 manager_.reset(new AudioRendererMixerManager()); | |
| 40 } | |
| 41 | |
| 42 media::AudioRendererMixer* GetMixer(const media::AudioParameters& params) { | |
| 43 return manager_->GetMixer(params); | |
| 44 } | |
| 45 | |
| 46 void RemoveMixer(const media::AudioParameters& params) { | |
| 47 return manager_->RemoveMixer(params); | |
| 48 } | |
| 49 | |
| 50 // Number of instantiated mixers. | |
| 51 int mixer_count() { | |
| 52 return manager_->mixer_map_.size(); | |
| 53 } | |
| 54 | |
| 55 protected: | |
| 56 scoped_ptr<MockAudioRenderSinkFactory> mock_sink_factory_; | |
| 57 scoped_ptr<AudioRendererMixerManager> manager_; | |
| 58 }; | |
|
scherkus (not reviewing)
2012/07/13 23:17:38
DISALLOW etc ... yes, even for testing code :)
DaleCurtis
2012/07/14 00:39:26
Done.
| |
| 59 | |
| 60 // Verify GetMixer() and RemoveMixer() both work as expected; particularly with | |
| 61 // respect to the explicit ref counting done. | |
| 62 TEST_F(AudioRendererMixerManagerTest, GetRemoveMixer) { | |
| 63 // There should be no mixers outstanding to start with. | |
|
scherkus (not reviewing)
2012/07/13 23:17:38
nice tests!
| |
| 64 EXPECT_EQ(mixer_count(), 0); | |
| 65 | |
| 66 media::AudioParameters params1( | |
| 67 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | |
| 68 kBitsPerChannel, kBufferSize); | |
| 69 | |
| 70 media::AudioRendererMixer* mixer1 = GetMixer(params1); | |
| 71 ASSERT_TRUE(mixer1); | |
| 72 EXPECT_EQ(mixer_count(), 1); | |
| 73 | |
| 74 // The same parameters should return the same mixer1. | |
| 75 EXPECT_EQ(mixer1, GetMixer(params1)); | |
| 76 EXPECT_EQ(mixer_count(), 1); | |
| 77 | |
| 78 // Remove the extra mixer we just acquired. | |
| 79 RemoveMixer(params1); | |
| 80 EXPECT_EQ(mixer_count(), 1); | |
| 81 | |
| 82 media::AudioParameters params2( | |
| 83 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate * 2, | |
| 84 kBitsPerChannel, kBufferSize * 2); | |
| 85 media::AudioRendererMixer* mixer2 = GetMixer(params2); | |
| 86 ASSERT_TRUE(mixer2); | |
| 87 EXPECT_EQ(mixer_count(), 2); | |
| 88 | |
| 89 // Different parameters should result in a different mixer1. | |
| 90 EXPECT_NE(mixer1, mixer2); | |
| 91 | |
| 92 // Remove both outstanding mixers. | |
| 93 RemoveMixer(params1); | |
| 94 EXPECT_EQ(mixer_count(), 1); | |
| 95 RemoveMixer(params2); | |
| 96 EXPECT_EQ(mixer_count(), 0); | |
| 97 | |
| 98 // Verify the old mixer was actually removed and we get a new one if we send | |
| 99 // the same audio parameters again. | |
| 100 EXPECT_NE(mixer1, GetMixer(params1)); | |
| 101 EXPECT_EQ(mixer_count(), 1); | |
| 102 RemoveMixer(params1); | |
| 103 } | |
| 104 | |
| 105 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate | |
| 106 // callbacks and they are working as expected. | |
| 107 TEST_F(AudioRendererMixerManagerTest, CreateInput) { | |
| 108 media::AudioParameters params( | |
| 109 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | |
| 110 kBitsPerChannel, kBufferSize); | |
| 111 | |
| 112 // Create an a mixer input and ensure it doesn't instantiate a mixer yet. | |
| 113 EXPECT_EQ(mixer_count(), 0); | |
| 114 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput()); | |
| 115 EXPECT_EQ(mixer_count(), 0); | |
| 116 | |
| 117 // Implicitly test that AudioRendererMixerInput was provided with the expected | |
| 118 // callbacks needed to acquire an AudioRendererMixer and remove it. | |
| 119 media::FakeAudioRenderCallback callback(0); | |
| 120 input->Initialize(params, &callback); | |
| 121 EXPECT_EQ(mixer_count(), 1); | |
| 122 | |
| 123 // Destroying the input should destroy the mixer. | |
| 124 input = NULL; | |
| 125 EXPECT_EQ(mixer_count(), 0); | |
| 126 } | |
| 127 | |
| 128 } // namespace content | |
| OLD | NEW |