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 <windows.h> | 5 #include <windows.h> |
6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/memory/aligned_memory.h" | 11 #include "base/memory/aligned_memory.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/sync_socket.h" | 12 #include "base/sync_socket.h" |
14 #include "base/win/scoped_com_initializer.h" | 13 #include "base/win/scoped_com_initializer.h" |
15 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
16 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
17 #include "media/audio/audio_manager.h" | 16 #include "media/audio/audio_manager.h" |
18 #include "media/audio/audio_unittest_util.h" | 17 #include "media/audio/audio_unittest_util.h" |
19 #include "media/audio/mock_audio_source_callback.h" | 18 #include "media/audio/mock_audio_source_callback.h" |
20 #include "media/audio/simple_sources.h" | 19 #include "media/audio/simple_sources.h" |
21 #include "media/base/limits.h" | 20 #include "media/base/limits.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 uint32_t size() const { return size_; } | 136 uint32_t size() const { return size_; } |
138 // Returns the memory backing the file. | 137 // Returns the memory backing the file. |
139 const void* GetChunkAt(uint32_t offset) { return &start_[offset]; } | 138 const void* GetChunkAt(uint32_t offset) { return &start_[offset]; } |
140 | 139 |
141 private: | 140 private: |
142 HANDLE fmap_; | 141 HANDLE fmap_; |
143 char* start_; | 142 char* start_; |
144 uint32_t size_; | 143 uint32_t size_; |
145 }; | 144 }; |
146 | 145 |
147 class WinAudioTest : public ::testing::Test { | |
148 public: | |
149 WinAudioTest() { | |
150 audio_manager_ = | |
151 AudioManager::CreateForTesting(message_loop_.task_runner()); | |
152 message_loop_.RunUntilIdle(); | |
153 } | |
154 ~WinAudioTest() override { | |
155 audio_manager_.reset(); | |
156 message_loop_.RunUntilIdle(); | |
157 } | |
158 | |
159 protected: | |
160 base::MessageLoop message_loop_; | |
161 ScopedAudioManagerPtr audio_manager_; | |
162 }; | |
163 | |
164 // =========================================================================== | 146 // =========================================================================== |
165 // Validation of AudioManager::AUDIO_PCM_LINEAR | 147 // Validation of AudioManager::AUDIO_PCM_LINEAR |
166 // | 148 // |
167 // NOTE: | 149 // NOTE: |
168 // The tests can fail on the build bots when somebody connects to them via | 150 // The tests can fail on the build bots when somebody connects to them via |
169 // remote-desktop and the rdp client installs an audio device that fails to open | 151 // remote-desktop and the rdp client installs an audio device that fails to open |
170 // at some point, possibly when the connection goes idle. | 152 // at some point, possibly when the connection goes idle. |
171 | 153 |
172 // Test that can it be created and closed. | 154 // Test that can it be created and closed. |
173 TEST_F(WinAudioTest, PCMWaveStreamGetAndClose) { | 155 TEST(WinAudioTest, PCMWaveStreamGetAndClose) { |
174 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 156 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 157 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
175 | 158 |
176 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 159 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
177 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 160 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
178 8000, 16, 256), | 161 8000, 16, 256), |
179 std::string()); | 162 std::string()); |
180 ASSERT_TRUE(NULL != oas); | 163 ASSERT_TRUE(NULL != oas); |
181 oas->Close(); | 164 oas->Close(); |
182 } | 165 } |
183 | 166 |
184 // Test that can it be cannot be created with invalid parameters. | 167 // Test that can it be cannot be created with invalid parameters. |
185 TEST_F(WinAudioTest, SanityOnMakeParams) { | 168 TEST(WinAudioTest, SanityOnMakeParams) { |
186 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 169 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 170 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
187 | 171 |
188 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; | 172 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; |
189 EXPECT_TRUE( | 173 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
190 NULL == | 174 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), |
191 audio_manager_->MakeAudioOutputStream( | 175 std::string())); |
192 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), | 176 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
193 std::string())); | 177 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, 256), |
194 EXPECT_TRUE( | 178 std::string())); |
195 NULL == | 179 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
196 audio_manager_->MakeAudioOutputStream( | 180 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 256), |
197 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, 256), | 181 std::string())); |
198 std::string())); | 182 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
199 EXPECT_TRUE(NULL == | 183 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), |
200 audio_manager_->MakeAudioOutputStream( | 184 std::string())); |
201 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 256), | 185 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
202 std::string())); | 186 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, 256), |
203 EXPECT_TRUE( | 187 std::string())); |
204 NULL == | 188 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
205 audio_manager_->MakeAudioOutputStream( | 189 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, -100), |
206 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), | 190 std::string())); |
207 std::string())); | 191 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
208 EXPECT_TRUE(NULL == | 192 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, 0), |
209 audio_manager_->MakeAudioOutputStream( | 193 std::string())); |
210 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, 256), | 194 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( |
211 std::string())); | 195 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, |
212 EXPECT_TRUE(NULL == | 196 media::limits::kMaxSamplesPerPacket + 1), |
213 audio_manager_->MakeAudioOutputStream( | 197 std::string())); |
214 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, -100), | |
215 std::string())); | |
216 EXPECT_TRUE(NULL == | |
217 audio_manager_->MakeAudioOutputStream( | |
218 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, 0), | |
219 std::string())); | |
220 EXPECT_TRUE(NULL == | |
221 audio_manager_->MakeAudioOutputStream( | |
222 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, | |
223 media::limits::kMaxSamplesPerPacket + 1), | |
224 std::string())); | |
225 } | 198 } |
226 | 199 |
227 // Test that it can be opened and closed. | 200 // Test that it can be opened and closed. |
228 TEST_F(WinAudioTest, PCMWaveStreamOpenAndClose) { | 201 TEST(WinAudioTest, PCMWaveStreamOpenAndClose) { |
229 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 202 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 203 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
230 | 204 |
231 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 205 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
232 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 206 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
233 8000, 16, 256), | 207 8000, 16, 256), |
234 std::string()); | 208 std::string()); |
235 ASSERT_TRUE(NULL != oas); | 209 ASSERT_TRUE(NULL != oas); |
236 EXPECT_TRUE(oas->Open()); | 210 EXPECT_TRUE(oas->Open()); |
237 oas->Close(); | 211 oas->Close(); |
238 } | 212 } |
239 | 213 |
240 // Test that it has a maximum packet size. | 214 // Test that it has a maximum packet size. |
241 TEST_F(WinAudioTest, PCMWaveStreamOpenLimit) { | 215 TEST(WinAudioTest, PCMWaveStreamOpenLimit) { |
242 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 216 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 217 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
243 | 218 |
244 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 219 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
245 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 220 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
246 8000, 16, 1024 * 1024 * 1024), | 221 8000, 16, 1024 * 1024 * 1024), |
247 std::string()); | 222 std::string()); |
248 EXPECT_TRUE(NULL == oas); | 223 EXPECT_TRUE(NULL == oas); |
249 if (oas) | 224 if (oas) |
250 oas->Close(); | 225 oas->Close(); |
251 } | 226 } |
252 | 227 |
253 // Test potential deadlock situation if the source is slow or blocks for some | 228 // Test potential deadlock situation if the source is slow or blocks for some |
254 // time. The actual EXPECT_GT are mostly meaningless and the real test is that | 229 // time. The actual EXPECT_GT are mostly meaningless and the real test is that |
255 // the test completes in reasonable time. | 230 // the test completes in reasonable time. |
256 TEST_F(WinAudioTest, PCMWaveSlowSource) { | 231 TEST(WinAudioTest, PCMWaveSlowSource) { |
257 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 232 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 233 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
258 | 234 |
259 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 235 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
260 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 236 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
261 16000, 16, 256), | 237 16000, 16, 256), |
262 std::string()); | 238 std::string()); |
263 ASSERT_TRUE(NULL != oas); | 239 ASSERT_TRUE(NULL != oas); |
264 TestSourceLaggy test_laggy(90); | 240 TestSourceLaggy test_laggy(90); |
265 EXPECT_TRUE(oas->Open()); | 241 EXPECT_TRUE(oas->Open()); |
266 // The test parameters cause a callback every 32 ms and the source is | 242 // The test parameters cause a callback every 32 ms and the source is |
267 // sleeping for 90 ms, so it is guaranteed that we run out of ready buffers. | 243 // sleeping for 90 ms, so it is guaranteed that we run out of ready buffers. |
268 oas->Start(&test_laggy); | 244 oas->Start(&test_laggy); |
269 ::Sleep(500); | 245 ::Sleep(500); |
270 EXPECT_GT(test_laggy.callback_count(), 2); | 246 EXPECT_GT(test_laggy.callback_count(), 2); |
271 EXPECT_FALSE(test_laggy.had_error()); | 247 EXPECT_FALSE(test_laggy.had_error()); |
272 oas->Stop(); | 248 oas->Stop(); |
273 ::Sleep(500); | 249 ::Sleep(500); |
274 oas->Close(); | 250 oas->Close(); |
275 } | 251 } |
276 | 252 |
277 // Test another potential deadlock situation if the thread that calls Start() | 253 // Test another potential deadlock situation if the thread that calls Start() |
278 // gets paused. This test is best when run over RDP with audio enabled. See | 254 // gets paused. This test is best when run over RDP with audio enabled. See |
279 // bug 19276 for more details. | 255 // bug 19276 for more details. |
280 TEST_F(WinAudioTest, PCMWaveStreamPlaySlowLoop) { | 256 TEST(WinAudioTest, PCMWaveStreamPlaySlowLoop) { |
281 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 257 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 258 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
282 | 259 |
283 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 260 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
284 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 261 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
285 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 262 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
286 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 263 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
287 std::string()); | 264 std::string()); |
288 ASSERT_TRUE(NULL != oas); | 265 ASSERT_TRUE(NULL != oas); |
289 | 266 |
290 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 267 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
291 | 268 |
292 EXPECT_TRUE(oas->Open()); | 269 EXPECT_TRUE(oas->Open()); |
293 oas->SetVolume(1.0); | 270 oas->SetVolume(1.0); |
294 | 271 |
295 for (int ix = 0; ix != 5; ++ix) { | 272 for (int ix = 0; ix != 5; ++ix) { |
296 oas->Start(&source); | 273 oas->Start(&source); |
297 ::Sleep(10); | 274 ::Sleep(10); |
298 oas->Stop(); | 275 oas->Stop(); |
299 } | 276 } |
300 oas->Close(); | 277 oas->Close(); |
301 } | 278 } |
302 | 279 |
303 | 280 |
304 // This test produces actual audio for .5 seconds on the default wave | 281 // This test produces actual audio for .5 seconds on the default wave |
305 // device at 44.1K s/sec. Parameters have been chosen carefully so you should | 282 // device at 44.1K s/sec. Parameters have been chosen carefully so you should |
306 // not hear pops or noises while the sound is playing. | 283 // not hear pops or noises while the sound is playing. |
307 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzTone44Kss) { | 284 TEST(WinAudioTest, PCMWaveStreamPlay200HzTone44Kss) { |
308 if (!audio_manager_->HasAudioOutputDevices()) { | 285 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 286 if (!audio_man->HasAudioOutputDevices()) { |
309 LOG(WARNING) << "No output device detected."; | 287 LOG(WARNING) << "No output device detected."; |
310 return; | 288 return; |
311 } | 289 } |
312 | 290 |
313 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 291 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
314 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 292 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
315 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 293 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
316 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 294 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
317 std::string()); | 295 std::string()); |
318 ASSERT_TRUE(NULL != oas); | 296 ASSERT_TRUE(NULL != oas); |
319 | 297 |
320 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 298 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
321 | 299 |
322 EXPECT_TRUE(oas->Open()); | 300 EXPECT_TRUE(oas->Open()); |
323 oas->SetVolume(1.0); | 301 oas->SetVolume(1.0); |
324 oas->Start(&source); | 302 oas->Start(&source); |
325 ::Sleep(500); | 303 ::Sleep(500); |
326 oas->Stop(); | 304 oas->Stop(); |
327 oas->Close(); | 305 oas->Close(); |
328 } | 306 } |
329 | 307 |
330 // This test produces actual audio for for .5 seconds on the default wave | 308 // This test produces actual audio for for .5 seconds on the default wave |
331 // device at 22K s/sec. Parameters have been chosen carefully so you should | 309 // device at 22K s/sec. Parameters have been chosen carefully so you should |
332 // not hear pops or noises while the sound is playing. The audio also should | 310 // not hear pops or noises while the sound is playing. The audio also should |
333 // sound with a lower volume than PCMWaveStreamPlay200HzTone44Kss. | 311 // sound with a lower volume than PCMWaveStreamPlay200HzTone44Kss. |
334 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzTone22Kss) { | 312 TEST(WinAudioTest, PCMWaveStreamPlay200HzTone22Kss) { |
335 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 313 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 314 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
336 | 315 |
337 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 20; | 316 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 20; |
338 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 317 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
339 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 318 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
340 AudioParameters::kAudioCDSampleRate / 2, 16, | 319 AudioParameters::kAudioCDSampleRate / 2, 16, |
341 samples_100_ms), | 320 samples_100_ms), |
342 std::string()); | 321 std::string()); |
343 ASSERT_TRUE(NULL != oas); | 322 ASSERT_TRUE(NULL != oas); |
344 | 323 |
345 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate/2); | 324 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate/2); |
346 | 325 |
347 EXPECT_TRUE(oas->Open()); | 326 EXPECT_TRUE(oas->Open()); |
348 | 327 |
349 oas->SetVolume(0.5); | 328 oas->SetVolume(0.5); |
350 oas->Start(&source); | 329 oas->Start(&source); |
351 ::Sleep(500); | 330 ::Sleep(500); |
352 | 331 |
353 // Test that the volume is within the set limits. | 332 // Test that the volume is within the set limits. |
354 double volume = 0.0; | 333 double volume = 0.0; |
355 oas->GetVolume(&volume); | 334 oas->GetVolume(&volume); |
356 EXPECT_LT(volume, 0.51); | 335 EXPECT_LT(volume, 0.51); |
357 EXPECT_GT(volume, 0.49); | 336 EXPECT_GT(volume, 0.49); |
358 oas->Stop(); | 337 oas->Stop(); |
359 oas->Close(); | 338 oas->Close(); |
360 } | 339 } |
361 | 340 |
362 // Uses a restricted source to play ~2 seconds of audio for about 5 seconds. We | 341 // Uses a restricted source to play ~2 seconds of audio for about 5 seconds. We |
363 // try hard to generate situation where the two threads are accessing the | 342 // try hard to generate situation where the two threads are accessing the |
364 // object roughly at the same time. | 343 // object roughly at the same time. |
365 TEST_F(WinAudioTest, PushSourceFile16KHz) { | 344 TEST(WinAudioTest, PushSourceFile16KHz) { |
366 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 345 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 346 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
367 | 347 |
368 static const int kSampleRate = 16000; | 348 static const int kSampleRate = 16000; |
369 SineWaveAudioSource source(1, 200.0, kSampleRate); | 349 SineWaveAudioSource source(1, 200.0, kSampleRate); |
370 // Compute buffer size for 100ms of audio. | 350 // Compute buffer size for 100ms of audio. |
371 const uint32_t kSamples100ms = (kSampleRate / 1000) * 100; | 351 const uint32_t kSamples100ms = (kSampleRate / 1000) * 100; |
372 // Restrict SineWaveAudioSource to 100ms of samples. | 352 // Restrict SineWaveAudioSource to 100ms of samples. |
373 source.CapSamples(kSamples100ms); | 353 source.CapSamples(kSamples100ms); |
374 | 354 |
375 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 355 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
376 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 356 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
377 kSampleRate, 16, kSamples100ms), | 357 kSampleRate, 16, kSamples100ms), |
378 std::string()); | 358 std::string()); |
379 ASSERT_TRUE(NULL != oas); | 359 ASSERT_TRUE(NULL != oas); |
380 | 360 |
381 EXPECT_TRUE(oas->Open()); | 361 EXPECT_TRUE(oas->Open()); |
382 | 362 |
383 oas->SetVolume(1.0); | 363 oas->SetVolume(1.0); |
384 oas->Start(&source); | 364 oas->Start(&source); |
385 | 365 |
386 // We buffer and play at the same time, buffering happens every ~10ms and the | 366 // We buffer and play at the same time, buffering happens every ~10ms and the |
387 // consuming of the buffer happens every ~100ms. We do 100 buffers which | 367 // consuming of the buffer happens every ~100ms. We do 100 buffers which |
388 // effectively wrap around the file more than once. | 368 // effectively wrap around the file more than once. |
389 for (uint32_t ix = 0; ix != 100; ++ix) { | 369 for (uint32_t ix = 0; ix != 100; ++ix) { |
390 ::Sleep(10); | 370 ::Sleep(10); |
391 source.Reset(); | 371 source.Reset(); |
392 } | 372 } |
393 | 373 |
394 // Play a little bit more of the file. | 374 // Play a little bit more of the file. |
395 ::Sleep(500); | 375 ::Sleep(500); |
396 | 376 |
397 oas->Stop(); | 377 oas->Stop(); |
398 oas->Close(); | 378 oas->Close(); |
399 } | 379 } |
400 | 380 |
401 // This test is to make sure an AudioOutputStream can be started after it was | 381 // This test is to make sure an AudioOutputStream can be started after it was |
402 // stopped. You will here two .5 seconds wave signal separated by 0.5 seconds | 382 // stopped. You will here two .5 seconds wave signal separated by 0.5 seconds |
403 // of silence. | 383 // of silence. |
404 TEST_F(WinAudioTest, PCMWaveStreamPlayTwice200HzTone44Kss) { | 384 TEST(WinAudioTest, PCMWaveStreamPlayTwice200HzTone44Kss) { |
405 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 385 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 386 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
406 | 387 |
407 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 388 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
408 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 389 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
409 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 390 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
410 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 391 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
411 std::string()); | 392 std::string()); |
412 ASSERT_TRUE(NULL != oas); | 393 ASSERT_TRUE(NULL != oas); |
413 | 394 |
414 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 395 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
415 EXPECT_TRUE(oas->Open()); | 396 EXPECT_TRUE(oas->Open()); |
416 oas->SetVolume(1.0); | 397 oas->SetVolume(1.0); |
417 | 398 |
418 // Play the wave for .5 seconds. | 399 // Play the wave for .5 seconds. |
419 oas->Start(&source); | 400 oas->Start(&source); |
420 ::Sleep(500); | 401 ::Sleep(500); |
421 oas->Stop(); | 402 oas->Stop(); |
422 | 403 |
423 // Sleep to give silence after stopping the AudioOutputStream. | 404 // Sleep to give silence after stopping the AudioOutputStream. |
424 ::Sleep(250); | 405 ::Sleep(250); |
425 | 406 |
426 // Start again and play for .5 seconds. | 407 // Start again and play for .5 seconds. |
427 oas->Start(&source); | 408 oas->Start(&source); |
428 ::Sleep(500); | 409 ::Sleep(500); |
429 oas->Stop(); | 410 oas->Stop(); |
430 | 411 |
431 oas->Close(); | 412 oas->Close(); |
432 } | 413 } |
433 | 414 |
434 // With the low latency mode, WASAPI is utilized by default for Vista and | 415 // With the low latency mode, WASAPI is utilized by default for Vista and |
435 // higher and Wave is used for XP and lower. It is possible to utilize a | 416 // higher and Wave is used for XP and lower. It is possible to utilize a |
436 // smaller buffer size for WASAPI than for Wave. | 417 // smaller buffer size for WASAPI than for Wave. |
437 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzToneLowLatency) { | 418 TEST(WinAudioTest, PCMWaveStreamPlay200HzToneLowLatency) { |
438 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 419 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 420 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
439 | 421 |
440 // Use 10 ms buffer size for WASAPI and 50 ms buffer size for Wave. | 422 // Use 10 ms buffer size for WASAPI and 50 ms buffer size for Wave. |
441 // Take the existing native sample rate into account. | 423 // Take the existing native sample rate into account. |
442 const AudioParameters params = | 424 const AudioParameters params = audio_man->GetDefaultOutputStreamParameters(); |
443 audio_manager_->GetDefaultOutputStreamParameters(); | |
444 int sample_rate = params.sample_rate(); | 425 int sample_rate = params.sample_rate(); |
445 uint32_t samples_10_ms = sample_rate / 100; | 426 uint32_t samples_10_ms = sample_rate / 100; |
446 int n = 1; | 427 int n = 1; |
447 (base::win::GetVersion() <= base::win::VERSION_XP) ? n = 5 : n = 1; | 428 (base::win::GetVersion() <= base::win::VERSION_XP) ? n = 5 : n = 1; |
448 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 429 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
449 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 430 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
450 CHANNEL_LAYOUT_MONO, sample_rate, 16, n * samples_10_ms), | 431 CHANNEL_LAYOUT_MONO, sample_rate, |
| 432 16, n * samples_10_ms), |
451 std::string()); | 433 std::string()); |
452 ASSERT_TRUE(NULL != oas); | 434 ASSERT_TRUE(NULL != oas); |
453 | 435 |
454 SineWaveAudioSource source(1, 200, sample_rate); | 436 SineWaveAudioSource source(1, 200, sample_rate); |
455 | 437 |
456 bool opened = oas->Open(); | 438 bool opened = oas->Open(); |
457 if (!opened) { | 439 if (!opened) { |
458 // It was not possible to open this audio device in mono. | 440 // It was not possible to open this audio device in mono. |
459 // No point in continuing the test so let's break here. | 441 // No point in continuing the test so let's break here. |
460 LOG(WARNING) << "Mono is not supported. Skipping test."; | 442 LOG(WARNING) << "Mono is not supported. Skipping test."; |
461 oas->Close(); | 443 oas->Close(); |
462 return; | 444 return; |
463 } | 445 } |
464 oas->SetVolume(1.0); | 446 oas->SetVolume(1.0); |
465 | 447 |
466 // Play the wave for .8 seconds. | 448 // Play the wave for .8 seconds. |
467 oas->Start(&source); | 449 oas->Start(&source); |
468 ::Sleep(800); | 450 ::Sleep(800); |
469 oas->Stop(); | 451 oas->Stop(); |
470 oas->Close(); | 452 oas->Close(); |
471 } | 453 } |
472 | 454 |
473 // Check that the pending bytes value is correct what the stream starts. | 455 // Check that the pending bytes value is correct what the stream starts. |
474 TEST_F(WinAudioTest, PCMWaveStreamPendingBytes) { | 456 TEST(WinAudioTest, PCMWaveStreamPendingBytes) { |
475 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 457 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 458 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
476 | 459 |
477 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 460 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
478 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( | 461 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( |
479 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 462 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
480 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 463 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
481 std::string()); | 464 std::string()); |
482 ASSERT_TRUE(NULL != oas); | 465 ASSERT_TRUE(NULL != oas); |
483 | 466 |
484 NiceMock<MockAudioSourceCallback> source; | 467 NiceMock<MockAudioSourceCallback> source; |
485 EXPECT_TRUE(oas->Open()); | 468 EXPECT_TRUE(oas->Open()); |
486 | 469 |
487 uint32_t bytes_100_ms = samples_100_ms * 2; | 470 uint32_t bytes_100_ms = samples_100_ms * 2; |
488 | 471 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } | 578 } |
596 | 579 |
597 // Test the basic operation of AudioOutputStream used with a SyncSocket. | 580 // Test the basic operation of AudioOutputStream used with a SyncSocket. |
598 // The emphasis is to verify that it is possible to feed data to the audio | 581 // The emphasis is to verify that it is possible to feed data to the audio |
599 // layer using a source based on SyncSocket. In a real situation we would | 582 // layer using a source based on SyncSocket. In a real situation we would |
600 // go for the low-latency version in combination with SyncSocket, but to keep | 583 // go for the low-latency version in combination with SyncSocket, but to keep |
601 // the test more simple, AUDIO_PCM_LINEAR is utilized instead. The main | 584 // the test more simple, AUDIO_PCM_LINEAR is utilized instead. The main |
602 // principle of the test still remains and we avoid the additional complexity | 585 // principle of the test still remains and we avoid the additional complexity |
603 // related to the two different audio-layers for AUDIO_PCM_LOW_LATENCY. | 586 // related to the two different audio-layers for AUDIO_PCM_LOW_LATENCY. |
604 // In this test you should hear a continuous 200Hz tone for 2 seconds. | 587 // In this test you should hear a continuous 200Hz tone for 2 seconds. |
605 TEST_F(WinAudioTest, SyncSocketBasic) { | 588 TEST(WinAudioTest, SyncSocketBasic) { |
606 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); | 589 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); |
| 590 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); |
607 | 591 |
608 static const int sample_rate = AudioParameters::kAudioCDSampleRate; | 592 static const int sample_rate = AudioParameters::kAudioCDSampleRate; |
609 static const uint32_t kSamples20ms = sample_rate / 50; | 593 static const uint32_t kSamples20ms = sample_rate / 50; |
610 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 594 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
611 CHANNEL_LAYOUT_MONO, sample_rate, 16, kSamples20ms); | 595 CHANNEL_LAYOUT_MONO, sample_rate, 16, kSamples20ms); |
612 | 596 |
613 AudioOutputStream* oas = | 597 |
614 audio_manager_->MakeAudioOutputStream(params, std::string()); | 598 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(params, |
| 599 std::string()); |
615 ASSERT_TRUE(NULL != oas); | 600 ASSERT_TRUE(NULL != oas); |
616 | 601 |
617 ASSERT_TRUE(oas->Open()); | 602 ASSERT_TRUE(oas->Open()); |
618 | 603 |
619 base::SyncSocket sockets[2]; | 604 base::SyncSocket sockets[2]; |
620 ASSERT_TRUE(base::SyncSocket::CreatePair(&sockets[0], &sockets[1])); | 605 ASSERT_TRUE(base::SyncSocket::CreatePair(&sockets[0], &sockets[1])); |
621 | 606 |
622 SyncSocketSource source(&sockets[0], params); | 607 SyncSocketSource source(&sockets[0], params); |
623 | 608 |
624 SyncThreadContext thread_context; | 609 SyncThreadContext thread_context; |
(...skipping 10 matching lines...) Expand all Loading... |
635 oas->Start(&source); | 620 oas->Start(&source); |
636 | 621 |
637 ::WaitForSingleObject(thread, INFINITE); | 622 ::WaitForSingleObject(thread, INFINITE); |
638 ::CloseHandle(thread); | 623 ::CloseHandle(thread); |
639 | 624 |
640 oas->Stop(); | 625 oas->Stop(); |
641 oas->Close(); | 626 oas->Close(); |
642 } | 627 } |
643 | 628 |
644 } // namespace media | 629 } // namespace media |
OLD | NEW |