| 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 "media/audio/audio_io.h" | 5 #include "media/audio/audio_io.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <objbase.h> // This has to be before initguid.h | 8 #include <objbase.h> // This has to be before initguid.h |
| 9 #include <initguid.h> | 9 #include <initguid.h> |
| 10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
| 11 #include <setupapi.h> | 11 #include <setupapi.h> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
| 21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
| 22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
| 23 #include "media/audio/audio_parameters.h" |
| 23 #include "media/audio/audio_util.h" | 24 #include "media/audio/audio_util.h" |
| 24 #include "media/audio/win/audio_device_listener_win.h" | 25 #include "media/audio/win/audio_device_listener_win.h" |
| 25 #include "media/audio/win/audio_low_latency_input_win.h" | 26 #include "media/audio/win/audio_low_latency_input_win.h" |
| 26 #include "media/audio/win/audio_low_latency_output_win.h" | 27 #include "media/audio/win/audio_low_latency_output_win.h" |
| 27 #include "media/audio/win/audio_manager_win.h" | 28 #include "media/audio/win/audio_manager_win.h" |
| 28 #include "media/audio/win/audio_unified_win.h" | 29 #include "media/audio/win/audio_unified_win.h" |
| 29 #include "media/audio/win/core_audio_util_win.h" | 30 #include "media/audio/win/core_audio_util_win.h" |
| 30 #include "media/audio/win/device_enumeration_win.h" | 31 #include "media/audio/win/device_enumeration_win.h" |
| 31 #include "media/audio/win/wavein_input_win.h" | 32 #include "media/audio/win/wavein_input_win.h" |
| 32 #include "media/audio/win/waveout_output_win.h" | 33 #include "media/audio/win/waveout_output_win.h" |
| 33 #include "media/base/bind_to_loop.h" | 34 #include "media/base/bind_to_loop.h" |
| 35 #include "media/base/channel_layout.h" |
| 34 #include "media/base/limits.h" | 36 #include "media/base/limits.h" |
| 35 #include "media/base/media_switches.h" | 37 #include "media/base/media_switches.h" |
| 36 | 38 |
| 37 // Libraries required for the SetupAPI and Wbem APIs used here. | 39 // Libraries required for the SetupAPI and Wbem APIs used here. |
| 38 #pragma comment(lib, "setupapi.lib") | 40 #pragma comment(lib, "setupapi.lib") |
| 39 | 41 |
| 40 // The following are defined in various DDK headers, and we (re)define them here | 42 // The following are defined in various DDK headers, and we (re)define them here |
| 41 // to avoid adding the DDK as a chrome dependency. | 43 // to avoid adding the DDK as a chrome dependency. |
| 42 #define DRV_QUERYDEVICEINTERFACE 0x80c | 44 #define DRV_QUERYDEVICEINTERFACE 0x80c |
| 43 #define DRVM_MAPPER_PREFERRED_GET 0x2015 | 45 #define DRVM_MAPPER_PREFERRED_GET 0x2015 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 // right drivers, but graceful error handling is needed. | 56 // right drivers, but graceful error handling is needed. |
| 55 static const int kWinMaxChannels = 8; | 57 static const int kWinMaxChannels = 8; |
| 56 | 58 |
| 57 // We use 3 buffers for recording audio so that if a recording callback takes | 59 // We use 3 buffers for recording audio so that if a recording callback takes |
| 58 // some time to return we won't lose audio. More buffers while recording are | 60 // some time to return we won't lose audio. More buffers while recording are |
| 59 // ok because they don't introduce any delay in recording, unlike in playback | 61 // ok because they don't introduce any delay in recording, unlike in playback |
| 60 // where you first need to fill in that number of buffers before starting to | 62 // where you first need to fill in that number of buffers before starting to |
| 61 // play. | 63 // play. |
| 62 static const int kNumInputBuffers = 3; | 64 static const int kNumInputBuffers = 3; |
| 63 | 65 |
| 66 // Buffer size to use for input and output stream when a proper size can't be |
| 67 // determined from the system |
| 68 static const int kFallbackBufferSize = 2048; |
| 69 |
| 64 static int GetVersionPartAsInt(DWORDLONG num) { | 70 static int GetVersionPartAsInt(DWORDLONG num) { |
| 65 return static_cast<int>(num & 0xffff); | 71 return static_cast<int>(num & 0xffff); |
| 66 } | 72 } |
| 67 | 73 |
| 68 // Returns a string containing the given device's description and installed | 74 // Returns a string containing the given device's description and installed |
| 69 // driver version. | 75 // driver version. |
| 70 static string16 GetDeviceAndDriverInfo(HDEVINFO device_info, | 76 static string16 GetDeviceAndDriverInfo(HDEVINFO device_info, |
| 71 SP_DEVINFO_DATA* device_data) { | 77 SP_DEVINFO_DATA* device_data) { |
| 72 // Save the old install params setting and set a flag for the | 78 // Save the old install params setting and set a flag for the |
| 73 // SetupDiBuildDriverInfoList below to return only the installed drivers. | 79 // SetupDiBuildDriverInfoList below to return only the installed drivers. |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 254 |
| 249 // Always add default device parameters as first element. | 255 // Always add default device parameters as first element. |
| 250 if (!device_names->empty()) { | 256 if (!device_names->empty()) { |
| 251 media::AudioDeviceName name; | 257 media::AudioDeviceName name; |
| 252 name.device_name = AudioManagerBase::kDefaultDeviceName; | 258 name.device_name = AudioManagerBase::kDefaultDeviceName; |
| 253 name.unique_id = AudioManagerBase::kDefaultDeviceId; | 259 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
| 254 device_names->push_front(name); | 260 device_names->push_front(name); |
| 255 } | 261 } |
| 256 } | 262 } |
| 257 | 263 |
| 264 AudioParameters AudioManagerWin::GetInputStreamParameters( |
| 265 const std::string& device_id) { |
| 266 int sample_rate = 0; |
| 267 ChannelLayout channel_layout = CHANNEL_LAYOUT_NONE; |
| 268 if (!CoreAudioUtil::IsSupported()) { |
| 269 sample_rate = 48000; |
| 270 channel_layout = CHANNEL_LAYOUT_STEREO; |
| 271 } else { |
| 272 sample_rate = WASAPIAudioInputStream::HardwareSampleRate(device_id); |
| 273 channel_layout = |
| 274 WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ? |
| 275 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; |
| 276 } |
| 277 |
| 278 // TODO(Henrika): improve the default buffer size value for input stream. |
| 279 return AudioParameters( |
| 280 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 281 sample_rate, 16, kFallbackBufferSize); |
| 282 } |
| 283 |
| 258 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR | 284 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR |
| 259 // mode. | 285 // mode. |
| 260 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. | 286 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. |
| 261 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream( | 287 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream( |
| 262 const AudioParameters& params) { | 288 const AudioParameters& params) { |
| 263 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 289 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 264 if (params.channels() > kWinMaxChannels) | 290 if (params.channels() > kWinMaxChannels) |
| 265 return NULL; | 291 return NULL; |
| 266 | 292 |
| 267 return new PCMWaveOutAudioOutputStream(this, | 293 return new PCMWaveOutAudioOutputStream(this, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 // Fall back to Windows Wave implementation on Windows XP or lower. | 348 // Fall back to Windows Wave implementation on Windows XP or lower. |
| 323 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista."; | 349 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista."; |
| 324 stream = CreatePCMWaveInAudioInputStream(params, device_id); | 350 stream = CreatePCMWaveInAudioInputStream(params, device_id); |
| 325 } else { | 351 } else { |
| 326 stream = new WASAPIAudioInputStream(this, params, device_id); | 352 stream = new WASAPIAudioInputStream(this, params, device_id); |
| 327 } | 353 } |
| 328 | 354 |
| 329 return stream; | 355 return stream; |
| 330 } | 356 } |
| 331 | 357 |
| 358 AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters( |
| 359 const AudioParameters& input_params) { |
| 360 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 361 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 362 int sample_rate = 0; |
| 363 int buffer_size = 0; |
| 364 int bits_per_sample = 16; |
| 365 int input_channels = 0; |
| 366 if (!CoreAudioUtil::IsSupported()) { |
| 367 // Fall back to Windows Wave implementation on Windows XP or lower. |
| 368 // Use 48kHz as default input sample rate, kFallbackBufferSize as |
| 369 // default buffer size. |
| 370 sample_rate = 48000; |
| 371 buffer_size = kFallbackBufferSize; |
| 372 } else if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) { |
| 373 // TODO(crogers): tune these values for best possible WebAudio performance. |
| 374 // WebRTC works well at 48kHz and a buffer size of 480 samples will be used |
| 375 // for this case. Note that exclusive mode is experimental. |
| 376 // This sample rate will be combined with a buffer size of 256 samples, |
| 377 // which corresponds to an output delay of ~5.33ms. |
| 378 sample_rate = 48000; |
| 379 buffer_size = 256; |
| 380 } else { |
| 381 // Hardware sample-rate on Windows can be configured, so we must query. |
| 382 // TODO(henrika): improve possibility to specify an audio endpoint. |
| 383 // Use the default device (same as for Wave) for now to be compatible. |
| 384 sample_rate = WASAPIAudioOutputStream::HardwareSampleRate(); |
| 385 |
| 386 AudioParameters params; |
| 387 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(eRender, eConsole, |
| 388 ¶ms); |
| 389 buffer_size = FAILED(hr) ? kFallbackBufferSize : params.frames_per_buffer(); |
| 390 channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout(); |
| 391 } |
| 392 |
| 393 if (input_params.IsValid()) { |
| 394 input_channels = input_params.input_channels(); |
| 395 if (!CoreAudioUtil::IsSupported()) { |
| 396 // If WASAPI isn't supported we'll fallback to WaveOut, which will take |
| 397 // care of resampling and bits per sample changes. By setting these |
| 398 // equal to the input values, AudioOutputResampler will skip resampling |
| 399 // and bit per sample differences (since the input parameters will match |
| 400 // the output parameters). |
| 401 sample_rate = input_params.sample_rate(); |
| 402 bits_per_sample = input_params.bits_per_sample(); |
| 403 channel_layout = input_params.channel_layout(); |
| 404 } |
| 405 } |
| 406 |
| 407 return AudioParameters( |
| 408 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, |
| 409 sample_rate, bits_per_sample, buffer_size); |
| 410 } |
| 411 |
| 332 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream( | 412 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream( |
| 333 const AudioParameters& params, | 413 const AudioParameters& params, |
| 334 const std::string& device_id) { | 414 const std::string& device_id) { |
| 335 std::string xp_device_id = device_id; | 415 std::string xp_device_id = device_id; |
| 336 if (device_id != AudioManagerBase::kDefaultDeviceId && | 416 if (device_id != AudioManagerBase::kDefaultDeviceId && |
| 337 enumeration_type_ == kMMDeviceEnumeration) { | 417 enumeration_type_ == kMMDeviceEnumeration) { |
| 338 xp_device_id = media::ConvertToWinXPDeviceId(device_id); | 418 xp_device_id = media::ConvertToWinXPDeviceId(device_id); |
| 339 if (xp_device_id.empty()) { | 419 if (xp_device_id.empty()) { |
| 340 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID " | 420 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID " |
| 341 << device_id; | 421 << device_id; |
| 342 return NULL; | 422 return NULL; |
| 343 } | 423 } |
| 344 } | 424 } |
| 345 | 425 |
| 346 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | 426 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, |
| 347 xp_device_id); | 427 xp_device_id); |
| 348 } | 428 } |
| 349 | 429 |
| 350 /// static | 430 /// static |
| 351 AudioManager* CreateAudioManager() { | 431 AudioManager* CreateAudioManager() { |
| 352 return new AudioManagerWin(); | 432 return new AudioManagerWin(); |
| 353 } | 433 } |
| 354 | 434 |
| 355 AudioParameters AudioManagerWin::GetPreferredLowLatencyOutputStreamParameters( | |
| 356 const AudioParameters& input_params) { | |
| 357 // If WASAPI isn't supported we'll fallback to WaveOut, which will take care | |
| 358 // of resampling and bits per sample changes. By setting these equal to the | |
| 359 // input values, AudioOutputResampler will skip resampling and bit per sample | |
| 360 // differences (since the input parameters will match the output parameters). | |
| 361 int sample_rate = input_params.sample_rate(); | |
| 362 int bits_per_sample = input_params.bits_per_sample(); | |
| 363 ChannelLayout channel_layout = input_params.channel_layout(); | |
| 364 int input_channels = input_params.input_channels(); | |
| 365 if (CoreAudioUtil::IsSupported()) { | |
| 366 sample_rate = GetAudioHardwareSampleRate(); | |
| 367 bits_per_sample = 16; | |
| 368 channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout(); | |
| 369 } | |
| 370 | |
| 371 // TODO(dalecurtis): This should include hardware bits per channel eventually. | |
| 372 return AudioParameters( | |
| 373 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, | |
| 374 sample_rate, bits_per_sample, GetAudioHardwareBufferSize()); | |
| 375 } | |
| 376 | |
| 377 } // namespace media | 435 } // namespace media |
| OLD | NEW |