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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/environment.h" | 6 #include "base/environment.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 284 |
285 class AudioInputStreamTraits { | 285 class AudioInputStreamTraits { |
286 public: | 286 public: |
287 typedef AudioInputStream StreamType; | 287 typedef AudioInputStream StreamType; |
288 | 288 |
289 static int HardwareSampleRate() { | 289 static int HardwareSampleRate() { |
290 return static_cast<int>(media::GetAudioInputHardwareSampleRate( | 290 return static_cast<int>(media::GetAudioInputHardwareSampleRate( |
291 AudioManagerBase::kDefaultDeviceId)); | 291 AudioManagerBase::kDefaultDeviceId)); |
292 } | 292 } |
293 | 293 |
| 294 // TODO(henrika): add support for GetAudioInputHardwareBufferSize in media. |
| 295 static int HardwareBufferSize() { |
| 296 return static_cast<int>(media::GetAudioHardwareBufferSize()); |
| 297 } |
| 298 |
294 static StreamType* CreateStream(AudioManager* audio_manager, | 299 static StreamType* CreateStream(AudioManager* audio_manager, |
295 const AudioParameters& params) { | 300 const AudioParameters& params) { |
296 return audio_manager->MakeAudioInputStream(params, | 301 return audio_manager->MakeAudioInputStream(params, |
297 AudioManagerBase::kDefaultDeviceId); | 302 AudioManagerBase::kDefaultDeviceId); |
298 } | 303 } |
299 }; | 304 }; |
300 | 305 |
301 class AudioOutputStreamTraits { | 306 class AudioOutputStreamTraits { |
302 public: | 307 public: |
303 typedef AudioOutputStream StreamType; | 308 typedef AudioOutputStream StreamType; |
304 | 309 |
305 static int HardwareSampleRate() { | 310 static int HardwareSampleRate() { |
306 return static_cast<int>(media::GetAudioHardwareSampleRate()); | 311 return static_cast<int>(media::GetAudioHardwareSampleRate()); |
307 } | 312 } |
308 | 313 |
| 314 static int HardwareBufferSize() { |
| 315 return static_cast<int>(media::GetAudioHardwareBufferSize()); |
| 316 } |
| 317 |
309 static StreamType* CreateStream(AudioManager* audio_manager, | 318 static StreamType* CreateStream(AudioManager* audio_manager, |
310 const AudioParameters& params) { | 319 const AudioParameters& params) { |
311 return audio_manager->MakeAudioOutputStream(params); | 320 return audio_manager->MakeAudioOutputStream(params); |
312 } | 321 } |
313 }; | 322 }; |
314 | 323 |
315 // Traits template holding a trait of StreamType. It encapsulates | 324 // Traits template holding a trait of StreamType. It encapsulates |
316 // AudioInputStream and AudioOutputStream stream types. | 325 // AudioInputStream and AudioOutputStream stream types. |
317 template <typename StreamTraits> | 326 template <typename StreamTraits> |
318 class StreamWrapper { | 327 class StreamWrapper { |
319 public: | 328 public: |
320 typedef typename StreamTraits::StreamType StreamType; | 329 typedef typename StreamTraits::StreamType StreamType; |
321 | 330 |
322 explicit StreamWrapper(AudioManager* audio_manager) | 331 explicit StreamWrapper(AudioManager* audio_manager) |
323 : | 332 : |
324 #if defined(OS_WIN) | 333 #if defined(OS_WIN) |
325 com_init_(base::win::ScopedCOMInitializer::kMTA), | 334 com_init_(base::win::ScopedCOMInitializer::kMTA), |
326 #endif | 335 #endif |
327 audio_manager_(audio_manager), | 336 audio_manager_(audio_manager), |
328 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | 337 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), |
329 #if defined(OS_ANDROID) | 338 #if defined(OS_ANDROID) |
330 channel_layout_(CHANNEL_LAYOUT_MONO), | 339 channel_layout_(CHANNEL_LAYOUT_MONO), |
331 #else | 340 #else |
332 channel_layout_(CHANNEL_LAYOUT_STEREO), | 341 channel_layout_(CHANNEL_LAYOUT_STEREO), |
333 #endif | 342 #endif |
334 bits_per_sample_(16) { | 343 bits_per_sample_(16) { |
335 // Use native/mixing sample rate and N*10ms frame size as default, | 344 // Use the preferred sample rate. |
336 // where N is platform dependent. | |
337 sample_rate_ = StreamTraits::HardwareSampleRate(); | 345 sample_rate_ = StreamTraits::HardwareSampleRate(); |
338 #if defined(OS_MACOSX) | 346 |
339 // 10ms buffer size works well for 44.1, 48, 96 and 192kHz. | 347 // Use the preferred buffer size. Note that the input side uses the same |
340 samples_per_packet_ = (sample_rate_ / 100); | 348 // size as the output side in this implementation. |
341 #elif defined(OS_LINUX) || defined(OS_OPENBSD) | 349 samples_per_packet_ = StreamTraits::HardwareBufferSize(); |
342 // 10ms buffer size works well for 44.1, 48, 96 and 192kHz. | |
343 samples_per_packet_ = (sample_rate_ / 100); | |
344 #elif defined(OS_WIN) | |
345 if (media::IsWASAPISupported()) { | |
346 // WASAPI is supported for Windows Vista and higher. | |
347 if (sample_rate_ == 44100) { | |
348 // Tests have shown that the shared mode WASAPI implementation | |
349 // works bests for a period size of ~10.15873 ms when the sample | |
350 // rate is 44.1kHz. | |
351 samples_per_packet_ = 448; | |
352 } else { | |
353 // 10ms buffer size works well for 48, 96 and 192kHz. | |
354 samples_per_packet_ = (sample_rate_ / 100); | |
355 } | |
356 } else { | |
357 // Low-latency Wave implementation needs 30ms buffer size to | |
358 // ensure glitch-free output audio. | |
359 samples_per_packet_ = 3 * (sample_rate_ / 100); | |
360 } | |
361 #elif defined(OS_ANDROID) | |
362 samples_per_packet_ = (sample_rate_ / 100); | |
363 #endif | |
364 } | 350 } |
365 | 351 |
366 virtual ~StreamWrapper() {} | 352 virtual ~StreamWrapper() {} |
367 | 353 |
368 // Creates an Audio[Input|Output]Stream stream object using default | 354 // Creates an Audio[Input|Output]Stream stream object using default |
369 // parameters. | 355 // parameters. |
370 StreamType* Create() { | 356 StreamType* Create() { |
371 return CreateStream(); | 357 return CreateStream(); |
372 } | 358 } |
373 | 359 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 454 |
469 // All Close() operations that run on the mocked audio thread, | 455 // All Close() operations that run on the mocked audio thread, |
470 // should be synchronous and not post additional close tasks to | 456 // should be synchronous and not post additional close tasks to |
471 // mocked the audio thread. Hence, there is no need to call | 457 // mocked the audio thread. Hence, there is no need to call |
472 // message_loop()->RunAllPending() after the Close() methods. | 458 // message_loop()->RunAllPending() after the Close() methods. |
473 aos->Close(); | 459 aos->Close(); |
474 ais->Close(); | 460 ais->Close(); |
475 } | 461 } |
476 | 462 |
477 } // namespace media | 463 } // namespace media |
OLD | NEW |