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 3 // found in the LICENSE file. 4 5 #include "media/audio/audio_io.h" 6 7 #include <windows.h> 8 #include <objbase.h> // This has to be before initguid.h 9 #include <initguid.h> 10 #include <mmsystem.h> 11 #include <setupapi.h> 12 13 #include "base/bind.h" 14 #include "base/bind_helpers.h" 15 #include "base/command_line.h" 16 #include "base/files/file_path.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/message_loop/message_loop.h" 19 #include "base/path_service.h" 20 #include "base/process/launch.h" 21 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_util.h" 23 #include "base/win/windows_version.h" 24 #include "media/audio/audio_parameters.h" 25 #include "media/audio/win/audio_device_listener_win.h" 26 #include "media/audio/win/audio_low_latency_input_win.h" 27 #include "media/audio/win/audio_low_latency_output_win.h" 28 #include "media/audio/win/audio_manager_win.h" 29 #include "media/audio/win/core_audio_util_win.h" 30 #include "media/audio/win/device_enumeration_win.h" 31 #include "media/audio/win/wavein_input_win.h" 32 #include "media/audio/win/waveout_output_win.h" 33 #include "media/base/bind_to_current_loop.h" 34 #include "media/base/channel_layout.h" 35 #include "media/base/limits.h" 36 #include "media/base/media_switches.h" 37 38 // Libraries required for the SetupAPI and Wbem APIs used here. 39 #pragma comment(lib, "setupapi.lib") 40 41 // The following are defined in various DDK headers, and we (re)define them here 42 // to avoid adding the DDK as a chrome dependency. 43 #define DRV_QUERYDEVICEINTERFACE 0x80c 44 #define DRVM_MAPPER_PREFERRED_GET 0x2015 45 #define DRV_QUERYDEVICEINTERFACESIZE 0x80d 46 DEFINE_GUID(AM_KSCATEGORY_AUDIO, 0x6994ad04, 0x93ef, 0x11d0, 47 0xa3, 0xcc, 0x00, 0xa0, 0xc9, 0x22, 0x31, 0x96); 48 49 namespace media { 50 51 // Maximum number of output streams that can be open simultaneously. 52 static const int kMaxOutputStreams = 50; 53 54 // Up to 8 channels can be passed to the driver. This should work, given the 55 // right drivers, but graceful error handling is needed. 56 static const int kWinMaxChannels = 8; 57 58 // We use 3 buffers for recording audio so that if a recording callback takes 59 // some time to return we won't lose audio. More buffers while recording are 60 // ok because they don't introduce any delay in recording, unlike in playback 61 // where you first need to fill in that number of buffers before starting to 62 // play. 63 static const int kNumInputBuffers = 3; 64 65 // Buffer size to use for input and output stream when a proper size can't be 66 // determined from the system 67 static const int kFallbackBufferSize = 2048; 68 69 static int GetVersionPartAsInt(DWORDLONG num) { 70 return static_cast<int>(num & 0xffff); 71 } 72 73 // Returns a string containing the given device's description and installed 74 // driver version. 75 static base::string16 GetDeviceAndDriverInfo(HDEVINFO device_info, 76 SP_DEVINFO_DATA* device_data) { 77 // Save the old install params setting and set a flag for the 78 // SetupDiBuildDriverInfoList below to return only the installed drivers. 79 SP_DEVINSTALL_PARAMS old_device_install_params; 80 old_device_install_params.cbSize = sizeof(old_device_install_params); 81 SetupDiGetDeviceInstallParams(device_info, device_data, 82 &old_device_install_params); 83 SP_DEVINSTALL_PARAMS device_install_params = old_device_install_params; 84 device_install_params.FlagsEx |= DI_FLAGSEX_INSTALLEDDRIVER; 85 SetupDiSetDeviceInstallParams(device_info, device_data, 86 &device_install_params); 87 88 SP_DRVINFO_DATA driver_data; 89 driver_data.cbSize = sizeof(driver_data); 90 base::string16 device_and_driver_info; 91 if (SetupDiBuildDriverInfoList(device_info, device_data, 92 SPDIT_COMPATDRIVER)) { 93 if (SetupDiEnumDriverInfo(device_info, device_data, SPDIT_COMPATDRIVER, 0, 94 &driver_data)) { 95 DWORDLONG version = driver_data.DriverVersion; 96 device_and_driver_info = base::string16(driver_data.Description) + L" v" + 97 base::IntToString16(GetVersionPartAsInt((version >> 48))) + L"." + 98 base::IntToString16(GetVersionPartAsInt((version >> 32))) + L"." + 99 base::IntToString16(GetVersionPartAsInt((version >> 16))) + L"." + 100 base::IntToString16(GetVersionPartAsInt(version)); 101 } 102 SetupDiDestroyDriverInfoList(device_info, device_data, SPDIT_COMPATDRIVER); 103 } 104 105 SetupDiSetDeviceInstallParams(device_info, device_data, 106 &old_device_install_params); 107 108 return device_and_driver_info; 109 } 110 111 static int NumberOfWaveOutBuffers() { 112 // Use the user provided buffer count if provided. 113 int buffers = 0; 114 std::string buffers_str(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 115 switches::kWaveOutBuffers)); 116 if (base::StringToInt(buffers_str, &buffers) && buffers > 0) { 117 return buffers; 118 } 119 120 // Use 4 buffers for Vista, 3 for everyone else: 121 // - The entire Windows audio stack was rewritten for Windows Vista and wave 122 // out performance was degraded compared to XP. 123 // - The regression was fixed in Windows 7 and most configurations will work 124 // with 2, but some (e.g., some Sound Blasters) still need 3. 125 // - Some XP configurations (even multi-processor ones) also need 3. 126 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; 127 } 128 129 AudioManagerWin::AudioManagerWin(AudioLogFactory* audio_log_factory) 130 : AudioManagerBase(audio_log_factory), 131 // |CoreAudioUtil::IsSupported()| uses static variables to avoid doing 132 // multiple initializations. This is however not thread safe. 133 // So, here we call it explicitly before we kick off the audio thread 134 // or do any other work. 135 enumeration_type_(CoreAudioUtil::IsSupported() ? 136 kMMDeviceEnumeration : kWaveEnumeration) { 137 SetMaxOutputStreamsAllowed(kMaxOutputStreams); 138 139 // WARNING: This is executed on the UI loop, do not add any code here which 140 // loads libraries or attempts to call out into the OS. Instead add such code 141 // to the InitializeOnAudioThread() method below. 142 143 // Task must be posted last to avoid races from handing out "this" to the 144 // audio thread. 145 GetTaskRunner()->PostTask(FROM_HERE, base::Bind( 146 &AudioManagerWin::InitializeOnAudioThread, base::Unretained(this))); 147 } 148 149 AudioManagerWin::~AudioManagerWin() { 150 // It's safe to post a task here since Shutdown() will wait for all tasks to 151 // complete before returning. 152 GetTaskRunner()->PostTask(FROM_HERE, base::Bind( 153 &AudioManagerWin::ShutdownOnAudioThread, base::Unretained(this))); 154 Shutdown(); 155 } 156 157 bool AudioManagerWin::HasAudioOutputDevices() { 158 return (::waveOutGetNumDevs() != 0); 159 } 160 161 bool AudioManagerWin::HasAudioInputDevices() { 162 return (::waveInGetNumDevs() != 0); 163 } 164 165 void AudioManagerWin::InitializeOnAudioThread() { 166 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); 167 168 if (core_audio_supported()) { 169 // AudioDeviceListenerWin must be initialized on a COM thread and should 170 // only be used if WASAPI / Core Audio is supported. 171 output_device_listener_.reset(new AudioDeviceListenerWin(BindToCurrentLoop( 172 base::Bind(&AudioManagerWin::NotifyAllOutputDeviceChangeListeners, 173 base::Unretained(this))))); 174 } 175 } 176 177 void AudioManagerWin::ShutdownOnAudioThread() { 178 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); 179 output_device_listener_.reset(); 180 } 181 182 base::string16 AudioManagerWin::GetAudioInputDeviceModel() { 183 // Get the default audio capture device and its device interface name. 184 DWORD device_id = 0; 185 waveInMessage(reinterpret_cast<HWAVEIN>(WAVE_MAPPER), 186 DRVM_MAPPER_PREFERRED_GET, 187 reinterpret_cast<DWORD_PTR>(&device_id), NULL); 188 ULONG device_interface_name_size = 0; 189 waveInMessage(reinterpret_cast<HWAVEIN>(device_id), 190 DRV_QUERYDEVICEINTERFACESIZE, 191 reinterpret_cast<DWORD_PTR>(&device_interface_name_size), 0); 192 size_t bytes_in_char16 = sizeof(base::string16::value_type); 193 DCHECK_EQ(0u, device_interface_name_size % bytes_in_char16); 194 if (device_interface_name_size <= bytes_in_char16) 195 return base::string16(); // No audio capture device. 196 197 base::string16 device_interface_name; 198 base::string16::value_type* name_ptr = WriteInto(&device_interface_name, 199 device_interface_name_size / bytes_in_char16); 200 waveInMessage(reinterpret_cast<HWAVEIN>(device_id), 201 DRV_QUERYDEVICEINTERFACE, 202 reinterpret_cast<DWORD_PTR>(name_ptr), 203 static_cast<DWORD_PTR>(device_interface_name_size)); 204 205 // Enumerate all audio devices and find the one matching the above device 206 // interface name. 207 HDEVINFO device_info = SetupDiGetClassDevs( 208 &AM_KSCATEGORY_AUDIO, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); 209 if (device_info == INVALID_HANDLE_VALUE) 210 return base::string16(); 211 212 DWORD interface_index = 0; 213 SP_DEVICE_INTERFACE_DATA interface_data; 214 interface_data.cbSize = sizeof(interface_data); 215 while (SetupDiEnumDeviceInterfaces(device_info, 0, &AM_KSCATEGORY_AUDIO, 216 interface_index++, &interface_data)) { 217 // Query the size of the struct, allocate it and then query the data. 218 SP_DEVINFO_DATA device_data; 219 device_data.cbSize = sizeof(device_data); 220 DWORD interface_detail_size = 0; 221 SetupDiGetDeviceInterfaceDetail(device_info, &interface_data, 0, 0, 222 &interface_detail_size, &device_data); 223 if (!interface_detail_size) 224 continue; 225 226 scoped_ptr<char[]> interface_detail_buffer(new char[interface_detail_size]); 227 SP_DEVICE_INTERFACE_DETAIL_DATA* interface_detail = 228 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>( 229 interface_detail_buffer.get()); 230 interface_detail->cbSize = interface_detail_size; 231 if (!SetupDiGetDeviceInterfaceDetail(device_info, &interface_data, 232 interface_detail, 233 interface_detail_size, NULL, 234 &device_data)) 235 return base::string16(); 236 237 bool device_found = (device_interface_name == interface_detail->DevicePath); 238 239 if (device_found) 240 return GetDeviceAndDriverInfo(device_info, &device_data); 241 } 242 243 return base::string16(); 244 } 245 246 void AudioManagerWin::ShowAudioInputSettings() { 247 std::wstring program; 248 std::string argument; 249 if (!core_audio_supported()) { 250 program = L"sndvol32.exe"; 251 argument = "-R"; 252 } else { 253 program = L"control.exe"; 254 argument = "mmsys.cpl,,1"; 255 } 256 257 base::FilePath path; 258 PathService::Get(base::DIR_SYSTEM, &path); 259 path = path.Append(program); 260 CommandLine command_line(path); 261 command_line.AppendArg(argument); 262 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); 263 } 264 265 void AudioManagerWin::GetAudioDeviceNamesImpl( 266 bool input, 267 AudioDeviceNames* device_names) { 268 DCHECK(device_names->empty()); 269 // Enumerate all active audio-endpoint capture devices. 270 if (enumeration_type() == kWaveEnumeration) { 271 // Utilize the Wave API for Windows XP. 272 if (input) 273 GetInputDeviceNamesWinXP(device_names); 274 else 275 GetOutputDeviceNamesWinXP(device_names); 276 } else { 277 // Utilize the MMDevice API (part of Core Audio) for Vista and higher. 278 if (input) 279 GetInputDeviceNamesWin(device_names); 280 else 281 GetOutputDeviceNamesWin(device_names); 282 } 283 284 // Always add default device parameters as first element. 285 if (!device_names->empty()) { 286 AudioDeviceName name; 287 name.device_name = AudioManagerBase::kDefaultDeviceName; 288 name.unique_id = AudioManagerBase::kDefaultDeviceId; 289 device_names->push_front(name); 290 } 291 } 292 293 void AudioManagerWin::GetAudioInputDeviceNames(AudioDeviceNames* device_names) { 294 GetAudioDeviceNamesImpl(true, device_names); 295 } 296 297 void AudioManagerWin::GetAudioOutputDeviceNames( 298 AudioDeviceNames* device_names) { 299 GetAudioDeviceNamesImpl(false, device_names); 300 } 301 302 AudioParameters AudioManagerWin::GetInputStreamParameters( 303 const std::string& device_id) { 304 AudioParameters parameters; 305 if (!core_audio_supported()) { 306 // Windows Wave implementation is being used. 307 parameters = AudioParameters( 308 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 0, 48000, 309 16, kFallbackBufferSize, AudioParameters::NO_EFFECTS); 310 } else { 311 parameters = WASAPIAudioInputStream::GetInputStreamParameters(device_id); 312 } 313 314 int user_buffer_size = GetUserBufferSize(); 315 if (user_buffer_size) { 316 parameters.Reset(parameters.format(), parameters.channel_layout(), 317 parameters.channels(), parameters.input_channels(), 318 parameters.sample_rate(), parameters.bits_per_sample(), 319 user_buffer_size); 320 } 321 322 return parameters; 323 } 324 325 std::string AudioManagerWin::GetAssociatedOutputDeviceID( 326 const std::string& input_device_id) { 327 if (!core_audio_supported()) { 328 NOTIMPLEMENTED() 329 << "GetAssociatedOutputDeviceID is not supported on this OS"; 330 return std::string(); 331 } 332 return CoreAudioUtil::GetMatchingOutputDeviceID(input_device_id); 333 } 334 335 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR 336 // mode. 337 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. 338 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream( 339 const AudioParameters& params) { 340 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); 341 if (params.channels() > kWinMaxChannels) 342 return NULL; 343 344 return new PCMWaveOutAudioOutputStream(this, 345 params, 346 NumberOfWaveOutBuffers(), 347 WAVE_MAPPER); 348 } 349 350 // Factory for the implementations of AudioOutputStream for 351 // AUDIO_PCM_LOW_LATENCY mode. Two implementations should suffice most 352 // windows user's needs. 353 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. 354 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API. 355 AudioOutputStream* AudioManagerWin::MakeLowLatencyOutputStream( 356 const AudioParameters& params, 357 const std::string& device_id) { 358 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); 359 if (params.channels() > kWinMaxChannels) 360 return NULL; 361 362 if (!core_audio_supported()) { 363 // Fall back to Windows Wave implementation on Windows XP or lower. 364 DLOG_IF(ERROR, !device_id.empty() && 365 device_id != AudioManagerBase::kDefaultDeviceId) 366 << "Opening by device id not supported by PCMWaveOutAudioOutputStream"; 367 DVLOG(1) << "Using WaveOut since WASAPI requires at least Vista."; 368 return new PCMWaveOutAudioOutputStream( 369 this, params, NumberOfWaveOutBuffers(), WAVE_MAPPER); 370 } 371 372 // Pass an empty string to indicate that we want the default device 373 // since we consistently only check for an empty string in 374 // WASAPIAudioOutputStream. 375 return new WASAPIAudioOutputStream(this, 376 device_id == AudioManagerBase::kDefaultDeviceId ? 377 std::string() : device_id, 378 params, 379 params.effects() & AudioParameters::DUCKING ? eCommunications : eConsole); 380 } 381 382 // Factory for the implementations of AudioInputStream for AUDIO_PCM_LINEAR 383 // mode. 384 AudioInputStream* AudioManagerWin::MakeLinearInputStream( 385 const AudioParameters& params, const std::string& device_id) { 386 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); 387 return CreatePCMWaveInAudioInputStream(params, device_id); 388 } 389 390 // Factory for the implementations of AudioInputStream for 391 // AUDIO_PCM_LOW_LATENCY mode. 392 AudioInputStream* AudioManagerWin::MakeLowLatencyInputStream( 393 const AudioParameters& params, const std::string& device_id) { 394 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); 395 DVLOG(1) << "MakeLowLatencyInputStream: " << device_id; 396 AudioInputStream* stream = NULL; 397 if (!core_audio_supported()) { 398 // Fall back to Windows Wave implementation on Windows XP or lower. 399 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista."; 400 stream = CreatePCMWaveInAudioInputStream(params, device_id); 401 } else { 402 stream = new WASAPIAudioInputStream(this, params, device_id); 403 } 404 405 return stream; 406 } 407 408 std::string AudioManagerWin::GetDefaultOutputDeviceID() { 409 if (!core_audio_supported()) 410 return std::string(); 411 return CoreAudioUtil::GetDefaultOutputDeviceID(); 412 } 413 414 AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters( 415 const std::string& output_device_id, 416 const AudioParameters& input_params) { 417 DLOG_IF(ERROR, !core_audio_supported() && !output_device_id.empty()) 418 << "CoreAudio is required to open non-default devices."; 419 420 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 421 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 422 int sample_rate = 48000; 423 int buffer_size = kFallbackBufferSize; 424 int bits_per_sample = 16; 425 int input_channels = 0; 426 int effects = AudioParameters::NO_EFFECTS; 427 bool use_input_params = !core_audio_supported(); 428 if (core_audio_supported()) { 429 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) { 430 // TODO(rtoy): tune these values for best possible WebAudio 431 // performance. WebRTC works well at 48kHz and a buffer size of 480 432 // samples will be used for this case. Note that exclusive mode is 433 // experimental. This sample rate will be combined with a buffer size of 434 // 256 samples, which corresponds to an output delay of ~5.33ms. 435 sample_rate = 48000; 436 buffer_size = 256; 437 if (input_params.IsValid()) 438 channel_layout = input_params.channel_layout(); 439 } else { 440 AudioParameters params; 441 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters( 442 output_device_id.empty() ? 443 GetDefaultOutputDeviceID() : output_device_id, 444 ¶ms); 445 if (SUCCEEDED(hr)) { 446 bits_per_sample = params.bits_per_sample(); 447 buffer_size = params.frames_per_buffer(); 448 channel_layout = params.channel_layout(); 449 sample_rate = params.sample_rate(); 450 effects = params.effects(); 451 } else { 452 // TODO(tommi): This should never happen really and I'm not sure that 453 // setting use_input_params is the right thing to do since WASAPI i 454 // definitely supported (see core_audio_supported() above) and 455 // |use_input_params| is only for cases when it isn't supported. 456 DLOG(ERROR) << "GetPreferredAudioParameters failed: " << std::hex << hr; 457 use_input_params = true; 458 } 459 } 460 } 461 462 if (input_params.IsValid()) { 463 // If the user has enabled checking supported channel layouts or we don't 464 // have a valid channel layout yet, try to use the input layout. See bugs 465 // http://crbug.com/259165 and http://crbug.com/311906 for more details. 466 if (core_audio_supported() && 467 (cmd_line->HasSwitch(switches::kTrySupportedChannelLayouts) || 468 channel_layout == CHANNEL_LAYOUT_UNSUPPORTED)) { 469 // Check if it is possible to open up at the specified input channel 470 // layout but avoid checking if the specified layout is the same as the 471 // hardware (preferred) layout. We do this extra check to avoid the 472 // CoreAudioUtil::IsChannelLayoutSupported() overhead in most cases. 473 if (input_params.channel_layout() != channel_layout) { 474 // TODO(henrika): Internally, IsChannelLayoutSupported does many of the 475 // operations that have already been done such as opening up a client 476 // and fetching the WAVEFORMATPCMEX format. Ideally we should only do 477 // that once. Then here, we can check the layout from the data we 478 // already hold. 479 if (CoreAudioUtil::IsChannelLayoutSupported( 480 output_device_id, eRender, eConsole, 481 input_params.channel_layout())) { 482 // Open up using the same channel layout as the source if it is 483 // supported by the hardware. 484 channel_layout = input_params.channel_layout(); 485 VLOG(1) << "Hardware channel layout is not used; using same layout" 486 << " as the source instead (" << channel_layout << ")"; 487 } 488 } 489 } 490 input_channels = input_params.input_channels(); 491 effects |= input_params.effects(); 492 if (use_input_params) { 493 // If WASAPI isn't supported we'll fallback to WaveOut, which will take 494 // care of resampling and bits per sample changes. By setting these 495 // equal to the input values, AudioOutputResampler will skip resampling 496 // and bit per sample differences (since the input parameters will match 497 // the output parameters). 498 bits_per_sample = input_params.bits_per_sample(); 499 buffer_size = input_params.frames_per_buffer(); 500 channel_layout = input_params.channel_layout(); 501 sample_rate = input_params.sample_rate(); 502 } 503 } 504 505 int user_buffer_size = GetUserBufferSize(); 506 if (user_buffer_size) 507 buffer_size = user_buffer_size; 508 509 return AudioParameters( 510 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, 511 sample_rate, bits_per_sample, buffer_size, effects); 512 } 513 514 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream( 515 const AudioParameters& params, 516 const std::string& device_id) { 517 std::string xp_device_id = device_id; 518 if (device_id != AudioManagerBase::kDefaultDeviceId && 519 enumeration_type_ == kMMDeviceEnumeration) { 520 xp_device_id = ConvertToWinXPInputDeviceId(device_id); 521 if (xp_device_id.empty()) { 522 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID " 523 << device_id; 524 return NULL; 525 } 526 } 527 528 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, 529 xp_device_id); 530 } 531 532 /// static 533 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) { 534 return new AudioManagerWin(audio_log_factory); 535 } 536 537 } // namespace media 538