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 "base/memory/ref_counted.h" |
5 #include "base/memory/scoped_ptr.h" | 6 #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" | 7 #include "content/renderer/media/audio_renderer_mixer_manager.h" |
8 #include "media/base/audio_renderer_mixer.h" | 8 #include "media/base/audio_renderer_mixer.h" |
9 #include "media/base/audio_renderer_mixer_input.h" | 9 #include "media/base/audio_renderer_mixer_input.h" |
10 #include "media/base/fake_audio_render_callback.h" | 10 #include "media/base/fake_audio_render_callback.h" |
11 #include "media/base/mock_audio_renderer_sink.h" | 11 #include "media/base/mock_audio_renderer_sink.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 static const int kBitsPerChannel = 16; | 17 static const int kBitsPerChannel = 16; |
18 static const int kSampleRate = 48000; | 18 static const int kSampleRate = 48000; |
19 static const int kBufferSize = 8192; | 19 static const int kBufferSize = 8192; |
20 static const media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO; | 20 static const media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO; |
21 | 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* CreateOutputDevice() OVERRIDE { | |
31 media::MockAudioRendererSink* sink = new media::MockAudioRendererSink(); | |
32 EXPECT_CALL(*sink, Start()); | |
33 EXPECT_CALL(*sink, Stop()); | |
34 return sink; | |
35 } | |
36 | |
37 virtual media::AudioInputDevice* CreateInputDevice() OVERRIDE { | |
38 ADD_FAILURE(); | |
39 return NULL; | |
40 } | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(MockAudioRenderSinkFactory); | |
43 }; | |
44 | |
45 class AudioRendererMixerManagerTest : public testing::Test { | 22 class AudioRendererMixerManagerTest : public testing::Test { |
46 public: | 23 public: |
47 AudioRendererMixerManagerTest() { | 24 AudioRendererMixerManagerTest() { |
| 25 manager_.reset(new AudioRendererMixerManager(kSampleRate, kBufferSize)); |
| 26 |
48 // We don't want to deal with instantiating a real AudioOutputDevice since | 27 // We don't want to deal with instantiating a real AudioOutputDevice since |
49 // it's not important to our testing, so use a mock AudioDeviceFactory. | 28 // it's not important to our testing, so we inject a mock. |
50 mock_sink_factory_.reset(new MockAudioRenderSinkFactory()); | 29 mock_sink_ = new media::MockAudioRendererSink(); |
51 manager_.reset(new AudioRendererMixerManager(kSampleRate, kBufferSize)); | 30 manager_->SetAudioRendererSinkForTesting(mock_sink_); |
52 } | 31 } |
53 | 32 |
54 media::AudioRendererMixer* GetMixer(const media::AudioParameters& params) { | 33 media::AudioRendererMixer* GetMixer(const media::AudioParameters& params) { |
55 return manager_->GetMixer(params); | 34 return manager_->GetMixer(params); |
56 } | 35 } |
57 | 36 |
58 void RemoveMixer(const media::AudioParameters& params) { | 37 void RemoveMixer(const media::AudioParameters& params) { |
59 return manager_->RemoveMixer(params); | 38 return manager_->RemoveMixer(params); |
60 } | 39 } |
61 | 40 |
62 // Number of instantiated mixers. | 41 // Number of instantiated mixers. |
63 int mixer_count() { | 42 int mixer_count() { |
64 return manager_->mixers_.size(); | 43 return manager_->mixers_.size(); |
65 } | 44 } |
66 | 45 |
67 protected: | 46 protected: |
68 scoped_ptr<MockAudioRenderSinkFactory> mock_sink_factory_; | |
69 scoped_ptr<AudioRendererMixerManager> manager_; | 47 scoped_ptr<AudioRendererMixerManager> manager_; |
| 48 scoped_refptr<media::MockAudioRendererSink> mock_sink_; |
70 | 49 |
71 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManagerTest); | 50 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManagerTest); |
72 }; | 51 }; |
73 | 52 |
74 // Verify GetMixer() and RemoveMixer() both work as expected; particularly with | 53 // Verify GetMixer() and RemoveMixer() both work as expected; particularly with |
75 // respect to the explicit ref counting done. | 54 // respect to the explicit ref counting done. |
76 TEST_F(AudioRendererMixerManagerTest, GetRemoveMixer) { | 55 TEST_F(AudioRendererMixerManagerTest, GetRemoveMixer) { |
| 56 // Since we're testing two different sets of parameters, we expect |
| 57 // AudioRendererMixerManager to call Start and Stop on our mock twice. |
| 58 EXPECT_CALL(*mock_sink_, Start()).Times(2); |
| 59 EXPECT_CALL(*mock_sink_, Stop()).Times(2); |
| 60 |
77 // There should be no mixers outstanding to start with. | 61 // There should be no mixers outstanding to start with. |
78 EXPECT_EQ(mixer_count(), 0); | 62 EXPECT_EQ(mixer_count(), 0); |
79 | 63 |
80 media::AudioParameters params1( | 64 media::AudioParameters params1( |
81 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 65 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
82 kBitsPerChannel, kBufferSize); | 66 kBitsPerChannel, kBufferSize); |
83 | 67 |
84 media::AudioRendererMixer* mixer1 = GetMixer(params1); | 68 media::AudioRendererMixer* mixer1 = GetMixer(params1); |
85 ASSERT_TRUE(mixer1); | 69 ASSERT_TRUE(mixer1); |
86 EXPECT_EQ(mixer_count(), 1); | 70 EXPECT_EQ(mixer_count(), 1); |
(...skipping 19 matching lines...) Expand all Loading... |
106 // Remove both outstanding mixers. | 90 // Remove both outstanding mixers. |
107 RemoveMixer(params1); | 91 RemoveMixer(params1); |
108 EXPECT_EQ(mixer_count(), 1); | 92 EXPECT_EQ(mixer_count(), 1); |
109 RemoveMixer(params2); | 93 RemoveMixer(params2); |
110 EXPECT_EQ(mixer_count(), 0); | 94 EXPECT_EQ(mixer_count(), 0); |
111 } | 95 } |
112 | 96 |
113 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate | 97 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate |
114 // callbacks and they are working as expected. | 98 // callbacks and they are working as expected. |
115 TEST_F(AudioRendererMixerManagerTest, CreateInput) { | 99 TEST_F(AudioRendererMixerManagerTest, CreateInput) { |
| 100 // Since we're testing only one set of parameters, we expect |
| 101 // AudioRendererMixerManager to call Start and Stop on our mock once each. |
| 102 EXPECT_CALL(*mock_sink_, Start()).Times(1); |
| 103 EXPECT_CALL(*mock_sink_, Stop()).Times(1); |
| 104 |
116 media::AudioParameters params( | 105 media::AudioParameters params( |
117 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 106 media::AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
118 kBitsPerChannel, kBufferSize); | 107 kBitsPerChannel, kBufferSize); |
119 | 108 |
120 // Create a mixer input and ensure it doesn't instantiate a mixer yet. | 109 // Create a mixer input and ensure it doesn't instantiate a mixer yet. |
121 EXPECT_EQ(mixer_count(), 0); | 110 EXPECT_EQ(mixer_count(), 0); |
122 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput()); | 111 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput()); |
123 EXPECT_EQ(mixer_count(), 0); | 112 EXPECT_EQ(mixer_count(), 0); |
124 | 113 |
125 // Implicitly test that AudioRendererMixerInput was provided with the expected | 114 // Implicitly test that AudioRendererMixerInput was provided with the expected |
126 // callbacks needed to acquire an AudioRendererMixer and remove it. | 115 // callbacks needed to acquire an AudioRendererMixer and remove it. |
127 media::FakeAudioRenderCallback callback(0); | 116 media::FakeAudioRenderCallback callback(0); |
128 input->Initialize(params, &callback); | 117 input->Initialize(params, &callback); |
129 EXPECT_EQ(mixer_count(), 1); | 118 EXPECT_EQ(mixer_count(), 1); |
130 | 119 |
131 // Destroying the input should destroy the mixer. | 120 // Destroying the input should destroy the mixer. |
132 input = NULL; | 121 input = NULL; |
133 EXPECT_EQ(mixer_count(), 0); | 122 EXPECT_EQ(mixer_count(), 0); |
134 } | 123 } |
135 | 124 |
136 } // namespace content | 125 } // namespace content |
OLD | NEW |