Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / media / audio / win / audio_manager_win.cc
blob71d9dd80f186186ccf89360fa6f09f15241265a2
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"
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>
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/audio_unified_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_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);
50 namespace media {
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
63 // play.
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
75 // driver version.
76 static 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 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,
95 &driver_data)) {
96 DWORDLONG version = driver_data.DriverVersion;
97 device_and_driver_info = 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.
114 int buffers = 0;
115 std::string buffers_str(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
116 switches::kWaveOutBuffers));
117 if (base::StringToInt(buffers_str, &buffers) && buffers > 0) {
118 return buffers;
121 // Use 4 buffers for Vista, 3 for everyone else:
122 // - The entire Windows audio stack was rewritten for Windows Vista and wave
123 // out performance was degraded compared to XP.
124 // - The regression was fixed in Windows 7 and most configurations will work
125 // with 2, but some (e.g., some Sound Blasters) still need 3.
126 // - Some XP configurations (even multi-processor ones) also need 3.
127 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3;
130 AudioManagerWin::AudioManagerWin() {
131 if (!CoreAudioUtil::IsSupported()) {
132 // Use the Wave API for device enumeration if XP or lower.
133 enumeration_type_ = kWaveEnumeration;
134 } else {
135 // Use the MMDevice API for device enumeration if Vista or higher.
136 enumeration_type_ = kMMDeviceEnumeration;
139 SetMaxOutputStreamsAllowed(kMaxOutputStreams);
141 // Task must be posted last to avoid races from handing out "this" to the
142 // audio thread.
143 GetMessageLoop()->PostTask(FROM_HERE, base::Bind(
144 &AudioManagerWin::CreateDeviceListener, base::Unretained(this)));
147 AudioManagerWin::~AudioManagerWin() {
148 // It's safe to post a task here since Shutdown() will wait for all tasks to
149 // complete before returning.
150 GetMessageLoop()->PostTask(FROM_HERE, base::Bind(
151 &AudioManagerWin::DestroyDeviceListener, base::Unretained(this)));
152 Shutdown();
155 bool AudioManagerWin::HasAudioOutputDevices() {
156 return (::waveOutGetNumDevs() != 0);
159 bool AudioManagerWin::HasAudioInputDevices() {
160 return (::waveInGetNumDevs() != 0);
163 void AudioManagerWin::CreateDeviceListener() {
164 // AudioDeviceListenerWin must be initialized on a COM thread and should only
165 // be used if WASAPI / Core Audio is supported.
166 if (CoreAudioUtil::IsSupported()) {
167 output_device_listener_.reset(new AudioDeviceListenerWin(BindToLoop(
168 GetMessageLoop(), base::Bind(
169 &AudioManagerWin::NotifyAllOutputDeviceChangeListeners,
170 base::Unretained(this)))));
174 void AudioManagerWin::DestroyDeviceListener() {
175 output_device_listener_.reset();
178 string16 AudioManagerWin::GetAudioInputDeviceModel() {
179 // Get the default audio capture device and its device interface name.
180 DWORD device_id = 0;
181 waveInMessage(reinterpret_cast<HWAVEIN>(WAVE_MAPPER),
182 DRVM_MAPPER_PREFERRED_GET,
183 reinterpret_cast<DWORD_PTR>(&device_id), NULL);
184 ULONG device_interface_name_size = 0;
185 waveInMessage(reinterpret_cast<HWAVEIN>(device_id),
186 DRV_QUERYDEVICEINTERFACESIZE,
187 reinterpret_cast<DWORD_PTR>(&device_interface_name_size), 0);
188 size_t bytes_in_char16 = sizeof(string16::value_type);
189 DCHECK_EQ(0u, device_interface_name_size % bytes_in_char16);
190 if (device_interface_name_size <= bytes_in_char16)
191 return string16(); // No audio capture device.
193 string16 device_interface_name;
194 string16::value_type* name_ptr = WriteInto(&device_interface_name,
195 device_interface_name_size / bytes_in_char16);
196 waveInMessage(reinterpret_cast<HWAVEIN>(device_id),
197 DRV_QUERYDEVICEINTERFACE,
198 reinterpret_cast<DWORD_PTR>(name_ptr),
199 static_cast<DWORD_PTR>(device_interface_name_size));
201 // Enumerate all audio devices and find the one matching the above device
202 // interface name.
203 HDEVINFO device_info = SetupDiGetClassDevs(
204 &AM_KSCATEGORY_AUDIO, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
205 if (device_info == INVALID_HANDLE_VALUE)
206 return string16();
208 DWORD interface_index = 0;
209 SP_DEVICE_INTERFACE_DATA interface_data;
210 interface_data.cbSize = sizeof(interface_data);
211 while (SetupDiEnumDeviceInterfaces(device_info, 0, &AM_KSCATEGORY_AUDIO,
212 interface_index++, &interface_data)) {
213 // Query the size of the struct, allocate it and then query the data.
214 SP_DEVINFO_DATA device_data;
215 device_data.cbSize = sizeof(device_data);
216 DWORD interface_detail_size = 0;
217 SetupDiGetDeviceInterfaceDetail(device_info, &interface_data, 0, 0,
218 &interface_detail_size, &device_data);
219 if (!interface_detail_size)
220 continue;
222 scoped_ptr<char[]> interface_detail_buffer(new char[interface_detail_size]);
223 SP_DEVICE_INTERFACE_DETAIL_DATA* interface_detail =
224 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(
225 interface_detail_buffer.get());
226 interface_detail->cbSize = interface_detail_size;
227 if (!SetupDiGetDeviceInterfaceDetail(device_info, &interface_data,
228 interface_detail,
229 interface_detail_size, NULL,
230 &device_data))
231 return string16();
233 bool device_found = (device_interface_name == interface_detail->DevicePath);
235 if (device_found)
236 return GetDeviceAndDriverInfo(device_info, &device_data);
239 return string16();
242 void AudioManagerWin::ShowAudioInputSettings() {
243 std::wstring program;
244 std::string argument;
245 if (!CoreAudioUtil::IsSupported()) {
246 program = L"sndvol32.exe";
247 argument = "-R";
248 } else {
249 program = L"control.exe";
250 argument = "mmsys.cpl,,1";
253 base::FilePath path;
254 PathService::Get(base::DIR_SYSTEM, &path);
255 path = path.Append(program);
256 CommandLine command_line(path);
257 command_line.AppendArg(argument);
258 base::LaunchProcess(command_line, base::LaunchOptions(), NULL);
261 void AudioManagerWin::GetAudioDeviceNamesImpl(
262 bool input,
263 AudioDeviceNames* device_names) {
264 DCHECK(device_names->empty());
265 DCHECK(enumeration_type() != kUninitializedEnumeration);
266 // Enumerate all active audio-endpoint capture devices.
267 if (enumeration_type() == kWaveEnumeration) {
268 // Utilize the Wave API for Windows XP.
269 if (input)
270 GetInputDeviceNamesWinXP(device_names);
271 else
272 GetOutputDeviceNamesWinXP(device_names);
273 } else {
274 // Utilize the MMDevice API (part of Core Audio) for Vista and higher.
275 if (input)
276 GetInputDeviceNamesWin(device_names);
277 else
278 GetOutputDeviceNamesWin(device_names);
281 // Always add default device parameters as first element.
282 if (!device_names->empty()) {
283 AudioDeviceName name;
284 name.device_name = AudioManagerBase::kDefaultDeviceName;
285 name.unique_id = AudioManagerBase::kDefaultDeviceId;
286 device_names->push_front(name);
290 void AudioManagerWin::GetAudioInputDeviceNames(AudioDeviceNames* device_names) {
291 GetAudioDeviceNamesImpl(true, device_names);
294 void AudioManagerWin::GetAudioOutputDeviceNames(
295 AudioDeviceNames* device_names) {
296 GetAudioDeviceNamesImpl(false, device_names);
299 AudioParameters AudioManagerWin::GetInputStreamParameters(
300 const std::string& device_id) {
301 int sample_rate = 48000;
302 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
303 if (CoreAudioUtil::IsSupported()) {
304 int hw_sample_rate = WASAPIAudioInputStream::HardwareSampleRate(device_id);
305 if (hw_sample_rate)
306 sample_rate = hw_sample_rate;
307 channel_layout =
308 WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ?
309 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
312 // TODO(Henrika): improve the default buffer size value for input stream.
313 return AudioParameters(
314 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
315 sample_rate, 16, kFallbackBufferSize);
318 std::string AudioManagerWin::GetAssociatedOutputDeviceID(
319 const std::string& input_device_id) {
320 if (!CoreAudioUtil::IsSupported()) {
321 NOTIMPLEMENTED()
322 << "GetAssociatedOutputDeviceID is not supported on this OS";
323 return std::string();
325 return CoreAudioUtil::GetMatchingOutputDeviceID(input_device_id);
328 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR
329 // mode.
330 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
331 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream(
332 const AudioParameters& params) {
333 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
334 if (params.channels() > kWinMaxChannels)
335 return NULL;
337 return new PCMWaveOutAudioOutputStream(this,
338 params,
339 NumberOfWaveOutBuffers(),
340 WAVE_MAPPER);
343 // Factory for the implementations of AudioOutputStream for
344 // AUDIO_PCM_LOW_LATENCY mode. Two implementations should suffice most
345 // windows user's needs.
346 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
347 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API.
348 AudioOutputStream* AudioManagerWin::MakeLowLatencyOutputStream(
349 const AudioParameters& params,
350 const std::string& device_id,
351 const std::string& input_device_id) {
352 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
353 if (params.channels() > kWinMaxChannels)
354 return NULL;
356 if (!CoreAudioUtil::IsSupported()) {
357 // Fall back to Windows Wave implementation on Windows XP or lower.
358 DLOG_IF(ERROR, !device_id.empty())
359 << "Opening by device id not supported by PCMWaveOutAudioOutputStream";
360 DVLOG(1) << "Using WaveOut since WASAPI requires at least Vista.";
361 return new PCMWaveOutAudioOutputStream(
362 this, params, NumberOfWaveOutBuffers(), WAVE_MAPPER);
365 // TODO(rtoy): support more than stereo input.
366 if (params.input_channels() > 0) {
367 DVLOG(1) << "WASAPIUnifiedStream is created.";
368 DLOG_IF(ERROR, !device_id.empty())
369 << "Opening by device id not supported by WASAPIUnifiedStream";
370 return new WASAPIUnifiedStream(this, params, input_device_id);
373 return new WASAPIAudioOutputStream(this, device_id, params, eConsole);
376 // Factory for the implementations of AudioInputStream for AUDIO_PCM_LINEAR
377 // mode.
378 AudioInputStream* AudioManagerWin::MakeLinearInputStream(
379 const AudioParameters& params, const std::string& device_id) {
380 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
381 return CreatePCMWaveInAudioInputStream(params, device_id);
384 // Factory for the implementations of AudioInputStream for
385 // AUDIO_PCM_LOW_LATENCY mode.
386 AudioInputStream* AudioManagerWin::MakeLowLatencyInputStream(
387 const AudioParameters& params, const std::string& device_id) {
388 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
389 AudioInputStream* stream = NULL;
390 if (!CoreAudioUtil::IsSupported()) {
391 // Fall back to Windows Wave implementation on Windows XP or lower.
392 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista.";
393 stream = CreatePCMWaveInAudioInputStream(params, device_id);
394 } else {
395 stream = new WASAPIAudioInputStream(this, params, device_id);
398 return stream;
401 std::string AudioManagerWin::GetDefaultOutputDeviceID() {
402 if (!CoreAudioUtil::IsSupported())
403 return std::string();
404 return CoreAudioUtil::GetDefaultOutputDeviceID();
407 AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters(
408 const std::string& output_device_id,
409 const AudioParameters& input_params) {
410 const bool core_audio_supported = CoreAudioUtil::IsSupported();
411 DLOG_IF(ERROR, !core_audio_supported && !output_device_id.empty())
412 << "CoreAudio is required to open non-default devices.";
414 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
415 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
416 int sample_rate = 48000;
417 int buffer_size = kFallbackBufferSize;
418 int bits_per_sample = 16;
419 int input_channels = 0;
420 bool use_input_params = !core_audio_supported;
421 if (core_audio_supported) {
422 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
423 // TODO(rtoy): tune these values for best possible WebAudio
424 // performance. WebRTC works well at 48kHz and a buffer size of 480
425 // samples will be used for this case. Note that exclusive mode is
426 // experimental. This sample rate will be combined with a buffer size of
427 // 256 samples, which corresponds to an output delay of ~5.33ms.
428 sample_rate = 48000;
429 buffer_size = 256;
430 if (input_params.IsValid())
431 channel_layout = input_params.channel_layout();
432 } else {
433 AudioParameters params;
434 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(
435 output_device_id.empty() ?
436 GetDefaultOutputDeviceID() : output_device_id,
437 &params);
438 if (SUCCEEDED(hr)) {
439 bits_per_sample = params.bits_per_sample();
440 buffer_size = params.frames_per_buffer();
441 channel_layout = params.channel_layout();
442 sample_rate = params.sample_rate();
443 } else {
444 use_input_params = true;
449 if (input_params.IsValid()) {
450 if (core_audio_supported &&
451 cmd_line->HasSwitch(switches::kTrySupportedChannelLayouts)) {
452 // Check if it is possible to open up at the specified input channel
453 // layout but avoid checking if the specified layout is the same as the
454 // hardware (preferred) layout. We do this extra check to avoid the
455 // CoreAudioUtil::IsChannelLayoutSupported() overhead in most cases.
456 if (input_params.channel_layout() != channel_layout) {
457 // TODO(henrika): Use |output_device_id| here.
458 // Internally, IsChannelLayoutSupported does many of the operations
459 // that have already been done such as opening up a client and fetching
460 // the WAVEFORMATPCMEX format. Ideally we should only do that once and
461 // do it for the requested device. Then here, we can check the layout
462 // from the data we already hold.
463 if (CoreAudioUtil::IsChannelLayoutSupported(
464 eRender, eConsole, input_params.channel_layout())) {
465 // Open up using the same channel layout as the source if it is
466 // supported by the hardware.
467 channel_layout = input_params.channel_layout();
468 VLOG(1) << "Hardware channel layout is not used; using same layout"
469 << " as the source instead (" << channel_layout << ")";
473 input_channels = input_params.input_channels();
474 if (use_input_params) {
475 // If WASAPI isn't supported we'll fallback to WaveOut, which will take
476 // care of resampling and bits per sample changes. By setting these
477 // equal to the input values, AudioOutputResampler will skip resampling
478 // and bit per sample differences (since the input parameters will match
479 // the output parameters).
480 bits_per_sample = input_params.bits_per_sample();
481 buffer_size = input_params.frames_per_buffer();
482 channel_layout = input_params.channel_layout();
483 sample_rate = input_params.sample_rate();
487 int user_buffer_size = GetUserBufferSize();
488 if (user_buffer_size)
489 buffer_size = user_buffer_size;
491 return AudioParameters(
492 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
493 sample_rate, bits_per_sample, buffer_size);
496 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream(
497 const AudioParameters& params,
498 const std::string& device_id) {
499 std::string xp_device_id = device_id;
500 if (device_id != AudioManagerBase::kDefaultDeviceId &&
501 enumeration_type_ == kMMDeviceEnumeration) {
502 xp_device_id = ConvertToWinXPInputDeviceId(device_id);
503 if (xp_device_id.empty()) {
504 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID "
505 << device_id;
506 return NULL;
510 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers,
511 xp_device_id);
514 /// static
515 AudioManager* CreateAudioManager() {
516 return new AudioManagerWin();
519 } // namespace media