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 "content/renderer/media/audio_renderer_mixer_manager.h" | 5 #include "content/renderer/media/audio_renderer_mixer_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "content/renderer/media/audio_device_factory.h" | 13 #include "content/renderer/media/audio_renderer_sink_cache.h" |
13 #include "media/audio/audio_device_description.h" | 14 #include "media/audio/audio_device_description.h" |
14 #include "media/base/audio_capturer_source.h" | |
15 #include "media/base/audio_parameters.h" | 15 #include "media/base/audio_parameters.h" |
16 #include "media/base/audio_renderer_mixer.h" | 16 #include "media/base/audio_renderer_mixer.h" |
17 #include "media/base/audio_renderer_mixer_input.h" | 17 #include "media/base/audio_renderer_mixer_input.h" |
18 #include "media/base/fake_audio_render_callback.h" | 18 #include "media/base/fake_audio_render_callback.h" |
19 #include "media/base/mock_audio_renderer_sink.h" | 19 #include "media/base/mock_audio_renderer_sink.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 static const int kBitsPerChannel = 16; | 26 namespace { |
27 static const int kSampleRate = 48000; | 27 const int kBitsPerChannel = 16; |
28 static const int kBufferSize = 8192; | 28 const int kSampleRate = 48000; |
29 static const media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO; | 29 const int kBufferSize = 8192; |
30 static const media::ChannelLayout kAnotherChannelLayout = | 30 const media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO; |
31 media::CHANNEL_LAYOUT_2_1; | 31 const media::ChannelLayout kAnotherChannelLayout = media::CHANNEL_LAYOUT_2_1; |
32 static const char* const kDefaultDeviceId = | 32 const char* const kDefaultDeviceId = |
33 media::AudioDeviceDescription::kDefaultDeviceId; | 33 media::AudioDeviceDescription::kDefaultDeviceId; |
34 static const char kAnotherDeviceId[] = "another-device-id"; | 34 const char kAnotherDeviceId[] = "another-device-id"; |
35 static const char kMatchedDeviceId[] = "matched-device-id"; | 35 const char kMatchedDeviceId[] = "matched-device-id"; |
36 static const char kNonexistentDeviceId[] = "nonexistent-device-id"; | 36 const char kNonexistentDeviceId[] = "nonexistent-device-id"; |
37 | 37 |
38 static const int kRenderFrameId = 124; | 38 const int kRenderFrameId = 124; |
39 static const int kAnotherRenderFrameId = 678; | 39 const int kAnotherRenderFrameId = 678; |
| 40 } // namespace; |
40 | 41 |
41 using media::AudioParameters; | 42 using media::AudioParameters; |
42 | 43 |
43 class AudioRendererMixerManagerTest : public testing::Test, | 44 class FakeAudioRendererSinkCache : public AudioRendererSinkCache { |
44 public AudioDeviceFactory { | 45 public: |
| 46 using GetSinkCallback = |
| 47 base::Callback<scoped_refptr<media::AudioRendererSink>( |
| 48 int render_frame_id, |
| 49 int session_id, |
| 50 const std::string& device_id, |
| 51 const url::Origin& security_origin)>; |
| 52 |
| 53 using ReleaseSinkCallback = |
| 54 base::Callback<void(const media::AudioRendererSink*)>; |
| 55 |
| 56 FakeAudioRendererSinkCache(const GetSinkCallback& get_sink_cb, |
| 57 const ReleaseSinkCallback& release_sink_cb) |
| 58 : get_sink_cb_(get_sink_cb), release_sink_cb_(release_sink_cb) {} |
| 59 |
| 60 media::OutputDeviceInfo GetSinkInfo( |
| 61 int source_render_frame_id, |
| 62 int session_id, |
| 63 const std::string& device_id, |
| 64 const url::Origin& security_origin) final { |
| 65 return get_sink_cb_ |
| 66 .Run(source_render_frame_id, session_id, device_id, security_origin) |
| 67 ->GetOutputDeviceInfo(); |
| 68 } |
| 69 |
| 70 scoped_refptr<media::AudioRendererSink> GetSink( |
| 71 int source_render_frame_id, |
| 72 const std::string& device_id, |
| 73 const url::Origin& security_origin) final { |
| 74 return get_sink_cb_.Run(source_render_frame_id, 0, device_id, |
| 75 security_origin); |
| 76 } |
| 77 |
| 78 void ReleaseSink(const media::AudioRendererSink* sink) final { |
| 79 release_sink_cb_.Run(sink); |
| 80 } |
| 81 |
| 82 private: |
| 83 GetSinkCallback get_sink_cb_; |
| 84 ReleaseSinkCallback release_sink_cb_; |
| 85 }; |
| 86 |
| 87 class AudioRendererMixerManagerTest : public testing::Test { |
45 public: | 88 public: |
46 AudioRendererMixerManagerTest() | 89 AudioRendererMixerManagerTest() |
47 : manager_(new AudioRendererMixerManager()), | 90 : manager_(new AudioRendererMixerManager( |
| 91 std::unique_ptr<AudioRendererSinkCache>( |
| 92 new FakeAudioRendererSinkCache( |
| 93 base::Bind(&AudioRendererMixerManagerTest::GetSinkPtr, |
| 94 base::Unretained(this)), |
| 95 base::Bind(&AudioRendererMixerManagerTest::ReleaseSinkPtr, |
| 96 base::Unretained(this)))))), |
48 mock_sink_(new media::MockAudioRendererSink()), | 97 mock_sink_(new media::MockAudioRendererSink()), |
49 mock_sink_no_device_(new media::MockAudioRendererSink( | 98 mock_sink_no_device_(new media::MockAudioRendererSink( |
50 kNonexistentDeviceId, | 99 kNonexistentDeviceId, |
51 media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND)), | 100 media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND)), |
52 mock_sink_matched_device_( | 101 mock_sink_matched_device_( |
53 new media::MockAudioRendererSink(kMatchedDeviceId, | 102 new media::MockAudioRendererSink(kMatchedDeviceId, |
54 media::OUTPUT_DEVICE_STATUS_OK)), | 103 media::OUTPUT_DEVICE_STATUS_OK)), |
55 mock_sink_for_session_id_( | |
56 new media::MockAudioRendererSink(kMatchedDeviceId, | |
57 media::OUTPUT_DEVICE_STATUS_OK)), | |
58 kSecurityOrigin2(GURL("http://localhost")) {} | 104 kSecurityOrigin2(GURL("http://localhost")) {} |
59 | 105 |
60 media::AudioRendererMixer* GetMixer( | 106 media::AudioRendererMixer* GetMixer( |
61 int source_render_frame_id, | 107 int source_render_frame_id, |
62 const media::AudioParameters& params, | 108 const media::AudioParameters& params, |
63 const std::string& device_id, | 109 const std::string& device_id, |
64 const url::Origin& security_origin, | 110 const url::Origin& security_origin, |
65 media::OutputDeviceStatus* device_status) { | 111 media::OutputDeviceStatus* device_status) { |
66 return manager_->GetMixer(source_render_frame_id, params, device_id, | 112 return manager_->GetMixer(source_render_frame_id, params, device_id, |
67 security_origin, device_status); | 113 security_origin, device_status); |
68 } | 114 } |
69 | 115 |
70 void RemoveMixer(int source_render_frame_id, | 116 void ReturnMixer(int source_render_frame_id, |
71 const media::AudioParameters& params, | 117 const media::AudioParameters& params, |
72 const std::string& device_id, | 118 const std::string& device_id, |
73 const url::Origin& security_origin) { | 119 const url::Origin& security_origin) { |
74 return manager_->RemoveMixer(source_render_frame_id, params, device_id, | 120 return manager_->ReturnMixer(source_render_frame_id, params, device_id, |
75 security_origin); | 121 security_origin); |
76 } | 122 } |
77 | 123 |
78 // Number of instantiated mixers. | 124 // Number of instantiated mixers. |
79 int mixer_count() { | 125 int mixer_count() { |
80 return manager_->mixers_.size(); | 126 return manager_->mixers_.size(); |
81 } | 127 } |
82 | 128 |
83 protected: | 129 protected: |
84 MOCK_METHOD1(CreateAudioCapturerSource, | 130 scoped_refptr<media::AudioRendererSink> GetSinkPtr( |
85 scoped_refptr<media::AudioCapturerSource>(int)); | 131 int source_render_frame_id, |
86 MOCK_METHOD5( | |
87 CreateSwitchableAudioRendererSink, | |
88 scoped_refptr<media::SwitchableAudioRendererSink>(SourceType, | |
89 int, | |
90 int, | |
91 const std::string&, | |
92 const url::Origin&)); | |
93 MOCK_METHOD5(CreateAudioRendererSink, | |
94 scoped_refptr<media::AudioRendererSink>(SourceType, | |
95 int, | |
96 int, | |
97 const std::string&, | |
98 const url::Origin&)); | |
99 | |
100 scoped_refptr<media::AudioRendererSink> CreateFinalAudioRendererSink( | |
101 int render_frame_id, | |
102 int session_id, | 132 int session_id, |
103 const std::string& device_id, | 133 const std::string& device_id, |
104 const url::Origin& security_origin) { | 134 const url::Origin& security_origin) { |
105 if ((device_id == kDefaultDeviceId) || (device_id == kAnotherDeviceId)) { | 135 if ((device_id == kDefaultDeviceId) || (device_id == kAnotherDeviceId)) { |
106 // We don't care about separate sinks for these devices. | 136 // We don't care about separate sinks for these devices. |
107 return mock_sink_; | 137 return mock_sink_; |
108 } | 138 } |
109 if (device_id == kNonexistentDeviceId) | 139 if (device_id == kNonexistentDeviceId) |
110 return mock_sink_no_device_; | 140 return mock_sink_no_device_; |
111 if (device_id.empty()) { | 141 if (device_id.empty()) { |
112 // The sink used to get device ID from session ID if it's not empty | 142 // The sink used to get device ID from session ID if it's not empty |
113 return session_id ? mock_sink_for_session_id_ : mock_sink_; | 143 return session_id ? mock_sink_matched_device_ : mock_sink_; |
114 } | 144 } |
115 if (device_id == kMatchedDeviceId) | 145 if (device_id == kMatchedDeviceId) |
116 return mock_sink_matched_device_; | 146 return mock_sink_matched_device_; |
117 | 147 |
118 NOTREACHED(); | 148 NOTREACHED(); |
119 return nullptr; | 149 return nullptr; |
120 } | 150 } |
121 | 151 |
| 152 MOCK_METHOD1(ReleaseSinkPtr, void(const media::AudioRendererSink*)); |
| 153 |
122 std::unique_ptr<AudioRendererMixerManager> manager_; | 154 std::unique_ptr<AudioRendererMixerManager> manager_; |
| 155 |
123 scoped_refptr<media::MockAudioRendererSink> mock_sink_; | 156 scoped_refptr<media::MockAudioRendererSink> mock_sink_; |
124 scoped_refptr<media::MockAudioRendererSink> mock_sink_no_device_; | 157 scoped_refptr<media::MockAudioRendererSink> mock_sink_no_device_; |
125 scoped_refptr<media::MockAudioRendererSink> mock_sink_matched_device_; | 158 scoped_refptr<media::MockAudioRendererSink> mock_sink_matched_device_; |
126 scoped_refptr<media::MockAudioRendererSink> mock_sink_for_session_id_; | |
127 | 159 |
128 // To avoid global/static non-POD constants. | 160 // To avoid global/static non-POD constants. |
129 const url::Origin kSecurityOrigin; | 161 const url::Origin kSecurityOrigin; |
130 const url::Origin kSecurityOrigin2; | 162 const url::Origin kSecurityOrigin2; |
131 | 163 |
132 private: | 164 private: |
133 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManagerTest); | 165 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManagerTest); |
134 }; | 166 }; |
135 | 167 |
136 // Verify GetMixer() and RemoveMixer() both work as expected; particularly with | 168 // Verify GetMixer() and ReturnMixer() both work as expected; particularly with |
137 // respect to the explicit ref counting done. | 169 // respect to the explicit ref counting done. |
138 TEST_F(AudioRendererMixerManagerTest, GetRemoveMixer) { | 170 TEST_F(AudioRendererMixerManagerTest, GetReturnMixer) { |
139 // Since we're testing two different sets of parameters, we expect | 171 // Since we're testing two different sets of parameters, we expect |
140 // AudioRendererMixerManager to call Start and Stop on our mock twice. | 172 // AudioRendererMixerManager to call Start and Stop on our mock twice. |
141 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); | 173 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); |
142 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); | 174 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); |
143 | 175 |
| 176 // We expect 2 mixers to be created; each of them should release the sink. |
| 177 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(2); |
| 178 |
144 // There should be no mixers outstanding to start with. | 179 // There should be no mixers outstanding to start with. |
145 EXPECT_EQ(0, mixer_count()); | 180 EXPECT_EQ(0, mixer_count()); |
146 | 181 |
147 media::AudioParameters params1( | 182 media::AudioParameters params1( |
148 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 183 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
149 kBitsPerChannel, kBufferSize); | 184 kBitsPerChannel, kBufferSize); |
150 | 185 |
151 media::AudioRendererMixer* mixer1 = GetMixer( | 186 media::AudioRendererMixer* mixer1 = GetMixer( |
152 kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin, nullptr); | 187 kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin, nullptr); |
153 ASSERT_TRUE(mixer1); | 188 ASSERT_TRUE(mixer1); |
154 EXPECT_EQ(1, mixer_count()); | 189 EXPECT_EQ(1, mixer_count()); |
155 | 190 |
156 // The same parameters should return the same mixer1. | 191 // The same parameters should return the same mixer1. |
157 EXPECT_EQ(mixer1, GetMixer(kRenderFrameId, params1, kDefaultDeviceId, | 192 EXPECT_EQ(mixer1, GetMixer(kRenderFrameId, params1, kDefaultDeviceId, |
158 kSecurityOrigin, nullptr)); | 193 kSecurityOrigin, nullptr)); |
159 EXPECT_EQ(1, mixer_count()); | 194 EXPECT_EQ(1, mixer_count()); |
160 | 195 |
161 // Remove the extra mixer we just acquired. | 196 // Return the extra mixer we just acquired. |
162 RemoveMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); | 197 ReturnMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); |
163 EXPECT_EQ(1, mixer_count()); | 198 EXPECT_EQ(1, mixer_count()); |
164 | 199 |
165 media::AudioParameters params2( | 200 media::AudioParameters params2( |
166 AudioParameters::AUDIO_PCM_LINEAR, kAnotherChannelLayout, kSampleRate * 2, | 201 AudioParameters::AUDIO_PCM_LINEAR, kAnotherChannelLayout, kSampleRate * 2, |
167 kBitsPerChannel, kBufferSize * 2); | 202 kBitsPerChannel, kBufferSize * 2); |
168 media::AudioRendererMixer* mixer2 = GetMixer( | 203 media::AudioRendererMixer* mixer2 = GetMixer( |
169 kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin, nullptr); | 204 kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin, nullptr); |
170 ASSERT_TRUE(mixer2); | 205 ASSERT_TRUE(mixer2); |
171 EXPECT_EQ(2, mixer_count()); | 206 EXPECT_EQ(2, mixer_count()); |
172 | 207 |
173 // Different parameters should result in a different mixer1. | 208 // Different parameters should result in a different mixer1. |
174 EXPECT_NE(mixer1, mixer2); | 209 EXPECT_NE(mixer1, mixer2); |
175 | 210 |
176 // Remove both outstanding mixers. | 211 // Return both outstanding mixers. |
177 RemoveMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); | 212 ReturnMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); |
178 EXPECT_EQ(1, mixer_count()); | 213 EXPECT_EQ(1, mixer_count()); |
179 RemoveMixer(kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin); | 214 ReturnMixer(kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin); |
180 EXPECT_EQ(0, mixer_count()); | 215 EXPECT_EQ(0, mixer_count()); |
181 } | 216 } |
182 | 217 |
183 // Verify GetMixer() correctly deduplicates mixer with irrelevant AudioParameter | 218 // Verify GetMixer() correctly deduplicates mixer with irrelevant AudioParameter |
184 // differences. | 219 // differences. |
185 TEST_F(AudioRendererMixerManagerTest, MixerReuse) { | 220 TEST_F(AudioRendererMixerManagerTest, MixerReuse) { |
186 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); | 221 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); |
187 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); | 222 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); |
188 EXPECT_EQ(mixer_count(), 0); | 223 EXPECT_EQ(mixer_count(), 0); |
189 | 224 |
| 225 // We expect 2 mixers to be created; each of them should release the sink. |
| 226 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(2); |
| 227 |
190 media::AudioParameters params1(AudioParameters::AUDIO_PCM_LINEAR, | 228 media::AudioParameters params1(AudioParameters::AUDIO_PCM_LINEAR, |
191 kChannelLayout, | 229 kChannelLayout, |
192 kSampleRate, | 230 kSampleRate, |
193 kBitsPerChannel, | 231 kBitsPerChannel, |
194 kBufferSize); | 232 kBufferSize); |
195 media::AudioRendererMixer* mixer1 = GetMixer( | 233 media::AudioRendererMixer* mixer1 = GetMixer( |
196 kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin, nullptr); | 234 kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin, nullptr); |
197 ASSERT_TRUE(mixer1); | 235 ASSERT_TRUE(mixer1); |
198 EXPECT_EQ(1, mixer_count()); | 236 EXPECT_EQ(1, mixer_count()); |
199 | 237 |
200 // Different sample rates, formats, bit depths, and buffer sizes should not | 238 // Different sample rates, formats, bit depths, and buffer sizes should not |
201 // result in a different mixer. | 239 // result in a different mixer. |
202 media::AudioParameters params2(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 240 media::AudioParameters params2(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
203 kChannelLayout, | 241 kChannelLayout, |
204 kSampleRate * 2, | 242 kSampleRate * 2, |
205 kBitsPerChannel * 2, | 243 kBitsPerChannel * 2, |
206 kBufferSize * 2); | 244 kBufferSize * 2); |
207 EXPECT_EQ(mixer1, GetMixer(kRenderFrameId, params2, kDefaultDeviceId, | 245 EXPECT_EQ(mixer1, GetMixer(kRenderFrameId, params2, kDefaultDeviceId, |
208 kSecurityOrigin, nullptr)); | 246 kSecurityOrigin, nullptr)); |
209 EXPECT_EQ(1, mixer_count()); | 247 EXPECT_EQ(1, mixer_count()); |
210 RemoveMixer(kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin); | 248 ReturnMixer(kRenderFrameId, params2, kDefaultDeviceId, kSecurityOrigin); |
211 EXPECT_EQ(1, mixer_count()); | 249 EXPECT_EQ(1, mixer_count()); |
212 | 250 |
213 // Modify some parameters that do matter: channel layout | 251 // Modify some parameters that do matter: channel layout |
214 media::AudioParameters params3(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 252 media::AudioParameters params3(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
215 kAnotherChannelLayout, | 253 kAnotherChannelLayout, |
216 kSampleRate, | 254 kSampleRate, |
217 kBitsPerChannel, | 255 kBitsPerChannel, |
218 kBufferSize); | 256 kBufferSize); |
219 ASSERT_NE(params3.channel_layout(), params1.channel_layout()); | 257 ASSERT_NE(params3.channel_layout(), params1.channel_layout()); |
220 | 258 |
221 EXPECT_NE(mixer1, GetMixer(kRenderFrameId, params3, kDefaultDeviceId, | 259 EXPECT_NE(mixer1, GetMixer(kRenderFrameId, params3, kDefaultDeviceId, |
222 kSecurityOrigin, nullptr)); | 260 kSecurityOrigin, nullptr)); |
223 EXPECT_EQ(2, mixer_count()); | 261 EXPECT_EQ(2, mixer_count()); |
224 RemoveMixer(kRenderFrameId, params3, kDefaultDeviceId, kSecurityOrigin); | 262 ReturnMixer(kRenderFrameId, params3, kDefaultDeviceId, kSecurityOrigin); |
225 EXPECT_EQ(1, mixer_count()); | 263 EXPECT_EQ(1, mixer_count()); |
226 | 264 |
227 // Remove final mixer. | 265 // Return final mixer. |
228 RemoveMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); | 266 ReturnMixer(kRenderFrameId, params1, kDefaultDeviceId, kSecurityOrigin); |
229 EXPECT_EQ(0, mixer_count()); | 267 EXPECT_EQ(0, mixer_count()); |
230 } | 268 } |
231 | 269 |
232 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate | 270 // Verify CreateInput() provides AudioRendererMixerInput with the appropriate |
233 // callbacks and they are working as expected. Also, verify that separate | 271 // callbacks and they are working as expected. Also, verify that separate |
234 // mixers are created for separate RenderFrames, even though the | 272 // mixers are created for separate RenderFrames, even though the |
235 // AudioParameters are the same. | 273 // AudioParameters are the same. |
236 TEST_F(AudioRendererMixerManagerTest, CreateInput) { | 274 TEST_F(AudioRendererMixerManagerTest, CreateInput) { |
237 // Expect AudioRendererMixerManager to call Start and Stop on our mock twice | 275 // Expect AudioRendererMixerManager to call Start and Stop on our mock twice |
238 // each. Note: Under normal conditions, each mixer would get its own sink! | 276 // each. Note: Under normal conditions, each mixer would get its own sink! |
239 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); | 277 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); |
240 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); | 278 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); |
241 | 279 |
| 280 // We expect 2 mixers to be created; each of them should release the sink. |
| 281 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(2); |
| 282 |
242 media::AudioParameters params( | 283 media::AudioParameters params( |
243 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, | 284 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, |
244 kBitsPerChannel, kBufferSize); | 285 kBitsPerChannel, kBufferSize); |
245 | 286 |
246 // Create two mixer inputs and ensure this doesn't instantiate any mixers yet. | 287 // Create two mixer inputs and ensure this doesn't instantiate any mixers yet. |
247 EXPECT_EQ(0, mixer_count()); | 288 EXPECT_EQ(0, mixer_count()); |
248 media::FakeAudioRenderCallback callback(0); | 289 media::FakeAudioRenderCallback callback(0); |
249 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput( | 290 scoped_refptr<media::AudioRendererMixerInput> input(manager_->CreateInput( |
250 kRenderFrameId, 0, kDefaultDeviceId, kSecurityOrigin)); | 291 kRenderFrameId, 0, kDefaultDeviceId, kSecurityOrigin)); |
251 input->Initialize(params, &callback); | 292 input->Initialize(params, &callback); |
252 EXPECT_EQ(0, mixer_count()); | 293 EXPECT_EQ(0, mixer_count()); |
253 media::FakeAudioRenderCallback another_callback(1); | 294 media::FakeAudioRenderCallback another_callback(1); |
254 scoped_refptr<media::AudioRendererMixerInput> another_input( | 295 scoped_refptr<media::AudioRendererMixerInput> another_input( |
255 manager_->CreateInput(kAnotherRenderFrameId, 0, kDefaultDeviceId, | 296 manager_->CreateInput(kAnotherRenderFrameId, 0, kDefaultDeviceId, |
256 kSecurityOrigin)); | 297 kSecurityOrigin)); |
257 another_input->Initialize(params, &another_callback); | 298 another_input->Initialize(params, &another_callback); |
258 EXPECT_EQ(0, mixer_count()); | 299 EXPECT_EQ(0, mixer_count()); |
259 | 300 |
260 // Implicitly test that AudioRendererMixerInput was provided with the expected | 301 // Implicitly test that AudioRendererMixerInput was provided with the expected |
261 // callbacks needed to acquire an AudioRendererMixer and remove it. | 302 // callbacks needed to acquire an AudioRendererMixer and return it. |
262 input->Start(); | 303 input->Start(); |
263 EXPECT_EQ(1, mixer_count()); | 304 EXPECT_EQ(1, mixer_count()); |
264 another_input->Start(); | 305 another_input->Start(); |
265 EXPECT_EQ(2, mixer_count()); | 306 EXPECT_EQ(2, mixer_count()); |
266 | 307 |
267 // Destroying the inputs should destroy the mixers. | 308 // Destroying the inputs should destroy the mixers. |
268 input->Stop(); | 309 input->Stop(); |
269 input = nullptr; | 310 input = nullptr; |
270 EXPECT_EQ(1, mixer_count()); | 311 EXPECT_EQ(1, mixer_count()); |
271 another_input->Stop(); | 312 another_input->Stop(); |
272 another_input = nullptr; | 313 another_input = nullptr; |
273 EXPECT_EQ(0, mixer_count()); | 314 EXPECT_EQ(0, mixer_count()); |
274 } | 315 } |
275 | 316 |
276 // Verify CreateInput() provided with session id creates AudioRendererMixerInput | 317 // Verify CreateInput() provided with session id creates AudioRendererMixerInput |
277 // with the appropriate callbacks and they are working as expected. | 318 // with the appropriate callbacks and they are working as expected. |
278 TEST_F(AudioRendererMixerManagerTest, CreateInputWithSessionId) { | 319 TEST_F(AudioRendererMixerManagerTest, CreateInputWithSessionId) { |
279 // Expect AudioRendererMixerManager to call Start and Stop on our mock twice | 320 // Expect AudioRendererMixerManager to call Start and Stop on our mock twice |
280 // each: for kDefaultDeviceId and for kAnotherDeviceId. Note: Under normal | 321 // each: for kDefaultDeviceId and for kAnotherDeviceId. Note: Under normal |
281 // conditions, each mixer would get its own sink! | 322 // conditions, each mixer would get its own sink! |
282 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); | 323 EXPECT_CALL(*mock_sink_.get(), Start()).Times(2); |
283 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); | 324 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(2); |
284 | 325 |
285 // Expect AudioRendererMixerManager to call Start and Stop on the matched sink | 326 // Expect AudioRendererMixerManager to call Start and Stop on the matched sink |
286 // once. | 327 // once. |
287 EXPECT_CALL(*mock_sink_matched_device_.get(), Start()).Times(1); | 328 EXPECT_CALL(*mock_sink_matched_device_.get(), Start()).Times(1); |
288 EXPECT_CALL(*mock_sink_matched_device_.get(), Stop()).Times(1); | 329 EXPECT_CALL(*mock_sink_matched_device_.get(), Stop()).Times(1); |
289 | 330 |
| 331 // We expect 3 mixers to be created; each of them should release a sink. |
| 332 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(2); |
| 333 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_matched_device_.get())).Times(1); |
| 334 |
290 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 335 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
291 kChannelLayout, kSampleRate, kBitsPerChannel, | 336 kChannelLayout, kSampleRate, kBitsPerChannel, |
292 kBufferSize); | 337 kBufferSize); |
293 media::FakeAudioRenderCallback callback(0); | 338 media::FakeAudioRenderCallback callback(0); |
294 EXPECT_EQ(0, mixer_count()); | 339 EXPECT_EQ(0, mixer_count()); |
295 | 340 |
296 // Empty device id, zero session id; | 341 // Empty device id, zero session id; |
297 scoped_refptr<media::AudioRendererMixerInput> input_to_default_device( | 342 scoped_refptr<media::AudioRendererMixerInput> input_to_default_device( |
298 manager_->CreateInput(kRenderFrameId, 0, // session_id | 343 manager_->CreateInput(kRenderFrameId, 0, // session_id |
299 std::string(), kSecurityOrigin)); | 344 std::string(), kSecurityOrigin)); |
(...skipping 16 matching lines...) Expand all Loading... |
316 | 361 |
317 // Empty device id, non-zero session id; | 362 // Empty device id, non-zero session id; |
318 scoped_refptr<media::AudioRendererMixerInput> | 363 scoped_refptr<media::AudioRendererMixerInput> |
319 input_to_matched_device_with_session_id( | 364 input_to_matched_device_with_session_id( |
320 manager_->CreateInput(kRenderFrameId, 2, // session id | 365 manager_->CreateInput(kRenderFrameId, 2, // session id |
321 std::string(), kSecurityOrigin)); | 366 std::string(), kSecurityOrigin)); |
322 input_to_matched_device_with_session_id->Initialize(params, &callback); | 367 input_to_matched_device_with_session_id->Initialize(params, &callback); |
323 EXPECT_EQ(0, mixer_count()); | 368 EXPECT_EQ(0, mixer_count()); |
324 | 369 |
325 // Implicitly test that AudioRendererMixerInput was provided with the expected | 370 // Implicitly test that AudioRendererMixerInput was provided with the expected |
326 // callbacks needed to acquire an AudioRendererMixer and remove it. | 371 // callbacks needed to acquire an AudioRendererMixer and return it. |
327 input_to_default_device->Start(); | 372 input_to_default_device->Start(); |
328 EXPECT_EQ(1, mixer_count()); | 373 EXPECT_EQ(1, mixer_count()); |
329 | 374 |
330 input_to_another_device->Start(); | 375 input_to_another_device->Start(); |
331 EXPECT_EQ(2, mixer_count()); | 376 EXPECT_EQ(2, mixer_count()); |
332 | 377 |
333 input_to_matched_device->Start(); | 378 input_to_matched_device->Start(); |
334 EXPECT_EQ(3, mixer_count()); | 379 EXPECT_EQ(3, mixer_count()); |
335 | 380 |
336 // Should go to the same device as the input above. | 381 // Should go to the same device as the input above. |
(...skipping 15 matching lines...) Expand all Loading... |
352 EXPECT_EQ(0, mixer_count()); | 397 EXPECT_EQ(0, mixer_count()); |
353 } | 398 } |
354 | 399 |
355 // Verify GetMixer() correctly creates different mixers with the same | 400 // Verify GetMixer() correctly creates different mixers with the same |
356 // parameters, but different device ID and/or security origin | 401 // parameters, but different device ID and/or security origin |
357 TEST_F(AudioRendererMixerManagerTest, MixerDevices) { | 402 TEST_F(AudioRendererMixerManagerTest, MixerDevices) { |
358 EXPECT_CALL(*mock_sink_.get(), Start()).Times(3); | 403 EXPECT_CALL(*mock_sink_.get(), Start()).Times(3); |
359 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(3); | 404 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(3); |
360 EXPECT_EQ(0, mixer_count()); | 405 EXPECT_EQ(0, mixer_count()); |
361 | 406 |
| 407 // We expect 3 mixers to be created; each of them should release a sink. |
| 408 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(3); |
| 409 |
362 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 410 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
363 kChannelLayout, kSampleRate, kBitsPerChannel, | 411 kChannelLayout, kSampleRate, kBitsPerChannel, |
364 kBufferSize); | 412 kBufferSize); |
365 media::AudioRendererMixer* mixer1 = GetMixer( | 413 media::AudioRendererMixer* mixer1 = GetMixer( |
366 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin, nullptr); | 414 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin, nullptr); |
367 ASSERT_TRUE(mixer1); | 415 ASSERT_TRUE(mixer1); |
368 EXPECT_EQ(1, mixer_count()); | 416 EXPECT_EQ(1, mixer_count()); |
369 | 417 |
370 media::AudioRendererMixer* mixer2 = GetMixer( | 418 media::AudioRendererMixer* mixer2 = GetMixer( |
371 kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin, nullptr); | 419 kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin, nullptr); |
372 ASSERT_TRUE(mixer2); | 420 ASSERT_TRUE(mixer2); |
373 EXPECT_EQ(2, mixer_count()); | 421 EXPECT_EQ(2, mixer_count()); |
374 EXPECT_NE(mixer1, mixer2); | 422 EXPECT_NE(mixer1, mixer2); |
375 | 423 |
376 media::AudioRendererMixer* mixer3 = GetMixer( | 424 media::AudioRendererMixer* mixer3 = GetMixer( |
377 kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin2, nullptr); | 425 kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin2, nullptr); |
378 ASSERT_TRUE(mixer3); | 426 ASSERT_TRUE(mixer3); |
379 EXPECT_EQ(3, mixer_count()); | 427 EXPECT_EQ(3, mixer_count()); |
380 EXPECT_NE(mixer1, mixer3); | 428 EXPECT_NE(mixer1, mixer3); |
381 EXPECT_NE(mixer2, mixer3); | 429 EXPECT_NE(mixer2, mixer3); |
382 | 430 |
383 RemoveMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin); | 431 ReturnMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin); |
384 EXPECT_EQ(2, mixer_count()); | 432 EXPECT_EQ(2, mixer_count()); |
385 RemoveMixer(kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin); | 433 ReturnMixer(kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin); |
386 EXPECT_EQ(1, mixer_count()); | 434 EXPECT_EQ(1, mixer_count()); |
387 RemoveMixer(kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin2); | 435 ReturnMixer(kRenderFrameId, params, kAnotherDeviceId, kSecurityOrigin2); |
388 EXPECT_EQ(0, mixer_count()); | 436 EXPECT_EQ(0, mixer_count()); |
389 } | 437 } |
390 | 438 |
391 // Verify GetMixer() correctly deduplicate mixers with the same | 439 // Verify GetMixer() correctly deduplicate mixers with the same |
392 // parameters, different security origins but default device ID | 440 // parameters, different security origins but default device ID |
393 TEST_F(AudioRendererMixerManagerTest, OneMixerDifferentOriginsDefaultDevice) { | 441 TEST_F(AudioRendererMixerManagerTest, OneMixerDifferentOriginsDefaultDevice) { |
394 EXPECT_CALL(*mock_sink_.get(), Start()).Times(1); | 442 EXPECT_CALL(*mock_sink_.get(), Start()).Times(1); |
395 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(1); | 443 EXPECT_CALL(*mock_sink_.get(), Stop()).Times(1); |
396 EXPECT_EQ(0, mixer_count()); | 444 EXPECT_EQ(0, mixer_count()); |
397 | 445 |
| 446 // We expect 1 mixer to be created; it should release its sink. |
| 447 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_.get())).Times(1); |
| 448 |
398 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 449 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
399 kChannelLayout, kSampleRate, kBitsPerChannel, | 450 kChannelLayout, kSampleRate, kBitsPerChannel, |
400 kBufferSize); | 451 kBufferSize); |
401 media::AudioRendererMixer* mixer1 = GetMixer( | 452 media::AudioRendererMixer* mixer1 = GetMixer( |
402 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin, nullptr); | 453 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin, nullptr); |
403 ASSERT_TRUE(mixer1); | 454 ASSERT_TRUE(mixer1); |
404 EXPECT_EQ(1, mixer_count()); | 455 EXPECT_EQ(1, mixer_count()); |
405 | 456 |
406 media::AudioRendererMixer* mixer2 = | 457 media::AudioRendererMixer* mixer2 = |
407 GetMixer(kRenderFrameId, params, std::string(), kSecurityOrigin, nullptr); | 458 GetMixer(kRenderFrameId, params, std::string(), kSecurityOrigin, nullptr); |
408 ASSERT_TRUE(mixer2); | 459 ASSERT_TRUE(mixer2); |
409 EXPECT_EQ(1, mixer_count()); | 460 EXPECT_EQ(1, mixer_count()); |
410 EXPECT_EQ(mixer1, mixer2); | 461 EXPECT_EQ(mixer1, mixer2); |
411 | 462 |
412 media::AudioRendererMixer* mixer3 = GetMixer( | 463 media::AudioRendererMixer* mixer3 = GetMixer( |
413 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin2, nullptr); | 464 kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin2, nullptr); |
414 ASSERT_TRUE(mixer3); | 465 ASSERT_TRUE(mixer3); |
415 EXPECT_EQ(1, mixer_count()); | 466 EXPECT_EQ(1, mixer_count()); |
416 EXPECT_EQ(mixer1, mixer3); | 467 EXPECT_EQ(mixer1, mixer3); |
417 | 468 |
418 media::AudioRendererMixer* mixer4 = GetMixer( | 469 media::AudioRendererMixer* mixer4 = GetMixer( |
419 kRenderFrameId, params, std::string(), kSecurityOrigin2, nullptr); | 470 kRenderFrameId, params, std::string(), kSecurityOrigin2, nullptr); |
420 ASSERT_TRUE(mixer4); | 471 ASSERT_TRUE(mixer4); |
421 EXPECT_EQ(1, mixer_count()); | 472 EXPECT_EQ(1, mixer_count()); |
422 EXPECT_EQ(mixer1, mixer4); | 473 EXPECT_EQ(mixer1, mixer4); |
423 | 474 |
424 RemoveMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin); | 475 ReturnMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin); |
425 EXPECT_EQ(1, mixer_count()); | 476 EXPECT_EQ(1, mixer_count()); |
426 RemoveMixer(kRenderFrameId, params, std::string(), kSecurityOrigin); | 477 ReturnMixer(kRenderFrameId, params, std::string(), kSecurityOrigin); |
427 EXPECT_EQ(1, mixer_count()); | 478 EXPECT_EQ(1, mixer_count()); |
428 RemoveMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin2); | 479 ReturnMixer(kRenderFrameId, params, kDefaultDeviceId, kSecurityOrigin2); |
429 EXPECT_EQ(1, mixer_count()); | 480 EXPECT_EQ(1, mixer_count()); |
430 RemoveMixer(kRenderFrameId, params, std::string(), kSecurityOrigin2); | 481 ReturnMixer(kRenderFrameId, params, std::string(), kSecurityOrigin2); |
431 EXPECT_EQ(0, mixer_count()); | 482 EXPECT_EQ(0, mixer_count()); |
432 } | 483 } |
433 | 484 |
434 // Verify that GetMixer() correctly returns a null mixer and an appropriate | 485 // Verify that GetMixer() correctly returns a null mixer and an appropriate |
435 // status code when a nonexistent device is requested. | 486 // status code when a nonexistent device is requested. |
436 TEST_F(AudioRendererMixerManagerTest, NonexistentDevice) { | 487 TEST_F(AudioRendererMixerManagerTest, NonexistentDevice) { |
437 EXPECT_EQ(0, mixer_count()); | 488 EXPECT_EQ(0, mixer_count()); |
| 489 |
| 490 // Mixer manager should release a not-ok sink when failing to create a mixer. |
| 491 EXPECT_CALL(*this, ReleaseSinkPtr(mock_sink_no_device_.get())).Times(1); |
| 492 |
438 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 493 media::AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
439 kChannelLayout, kSampleRate, kBitsPerChannel, | 494 kChannelLayout, kSampleRate, kBitsPerChannel, |
440 kBufferSize); | 495 kBufferSize); |
441 media::OutputDeviceStatus device_status = media::OUTPUT_DEVICE_STATUS_OK; | 496 media::OutputDeviceStatus device_status = media::OUTPUT_DEVICE_STATUS_OK; |
442 EXPECT_CALL(*mock_sink_no_device_.get(), Stop()); | 497 |
443 media::AudioRendererMixer* mixer = | 498 media::AudioRendererMixer* mixer = |
444 GetMixer(kRenderFrameId, params, kNonexistentDeviceId, kSecurityOrigin, | 499 GetMixer(kRenderFrameId, params, kNonexistentDeviceId, kSecurityOrigin, |
445 &device_status); | 500 &device_status); |
| 501 |
446 EXPECT_FALSE(mixer); | 502 EXPECT_FALSE(mixer); |
447 EXPECT_EQ(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, device_status); | 503 EXPECT_EQ(media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, device_status); |
448 EXPECT_EQ(0, mixer_count()); | 504 EXPECT_EQ(0, mixer_count()); |
449 } | 505 } |
450 | 506 |
451 } // namespace content | 507 } // namespace content |
OLD | NEW |