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