Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / audio / win / audio_manager_win.cc
blobc09fb46284d6d8f4d1126816bf87d3dc6a9bee2e
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/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);
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 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,
95 &driver_data)) {
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.
114 int buffers = 0;
115 std::string buffers_str(
116 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
117 switches::kWaveOutBuffers));
118 if (base::StringToInt(buffers_str, &buffers) && buffers > 0) {
119 return buffers;
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
146 // audio thread.
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)));
156 Shutdown();
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.
186 DWORD device_id = 0;
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 = 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
208 // interface name.
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)
226 continue;
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,
234 interface_detail,
235 interface_detail_size, NULL,
236 &device_data))
237 return base::string16();
239 bool device_found = (device_interface_name == interface_detail->DevicePath);
241 if (device_found)
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";
253 argument = "-R";
254 } else {
255 program = L"control.exe";
256 argument = "mmsys.cpl,,1";
259 base::FilePath path;
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(
268 bool input,
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.
274 if (input)
275 GetInputDeviceNamesWinXP(device_names);
276 else
277 GetOutputDeviceNamesWinXP(device_names);
278 } else {
279 // Utilize the MMDevice API (part of Core Audio) for Vista and higher.
280 if (input)
281 GetInputDeviceNamesWin(device_names);
282 else
283 GetOutputDeviceNamesWin(device_names);
286 // Always add default device parameters as first element.
287 if (!device_names->empty()) {
288 AudioDeviceName name;
289 name.device_name = AudioManagerBase::kDefaultDeviceName;
290 name.unique_id = AudioManagerBase::kDefaultDeviceId;
291 device_names->push_front(name);
295 void AudioManagerWin::GetAudioInputDeviceNames(AudioDeviceNames* device_names) {
296 GetAudioDeviceNamesImpl(true, device_names);
299 void AudioManagerWin::GetAudioOutputDeviceNames(
300 AudioDeviceNames* device_names) {
301 GetAudioDeviceNamesImpl(false, device_names);
304 AudioParameters AudioManagerWin::GetInputStreamParameters(
305 const std::string& device_id) {
306 AudioParameters parameters;
307 if (!core_audio_supported()) {
308 // Windows Wave implementation is being used.
309 parameters = AudioParameters(
310 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 48000,
311 16, kFallbackBufferSize, AudioParameters::NO_EFFECTS);
312 } else {
313 parameters = WASAPIAudioInputStream::GetInputStreamParameters(device_id);
316 int user_buffer_size = GetUserBufferSize();
317 if (user_buffer_size) {
318 parameters.Reset(parameters.format(), parameters.channel_layout(),
319 parameters.channels(), parameters.sample_rate(),
320 parameters.bits_per_sample(), user_buffer_size);
323 return parameters;
326 std::string AudioManagerWin::GetAssociatedOutputDeviceID(
327 const std::string& input_device_id) {
328 if (!core_audio_supported()) {
329 NOTIMPLEMENTED()
330 << "GetAssociatedOutputDeviceID is not supported on this OS";
331 return std::string();
333 return CoreAudioUtil::GetMatchingOutputDeviceID(input_device_id);
336 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR
337 // mode.
338 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
339 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream(
340 const AudioParameters& params) {
341 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
342 if (params.channels() > kWinMaxChannels)
343 return NULL;
345 return new PCMWaveOutAudioOutputStream(this,
346 params,
347 NumberOfWaveOutBuffers(),
348 WAVE_MAPPER);
351 // Factory for the implementations of AudioOutputStream for
352 // AUDIO_PCM_LOW_LATENCY mode. Two implementations should suffice most
353 // windows user's needs.
354 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
355 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API.
356 AudioOutputStream* AudioManagerWin::MakeLowLatencyOutputStream(
357 const AudioParameters& params,
358 const std::string& device_id) {
359 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
360 if (params.channels() > kWinMaxChannels)
361 return NULL;
363 if (!core_audio_supported()) {
364 // Fall back to Windows Wave implementation on Windows XP or lower.
365 DLOG_IF(ERROR, !device_id.empty() &&
366 device_id != AudioManagerBase::kDefaultDeviceId)
367 << "Opening by device id not supported by PCMWaveOutAudioOutputStream";
368 DVLOG(1) << "Using WaveOut since WASAPI requires at least Vista.";
369 return new PCMWaveOutAudioOutputStream(
370 this, params, NumberOfWaveOutBuffers(), WAVE_MAPPER);
373 // Pass an empty string to indicate that we want the default device
374 // since we consistently only check for an empty string in
375 // WASAPIAudioOutputStream.
376 return new WASAPIAudioOutputStream(this,
377 device_id == AudioManagerBase::kDefaultDeviceId ?
378 std::string() : device_id,
379 params,
380 params.effects() & AudioParameters::DUCKING ? eCommunications : eConsole);
383 // Factory for the implementations of AudioInputStream for AUDIO_PCM_LINEAR
384 // mode.
385 AudioInputStream* AudioManagerWin::MakeLinearInputStream(
386 const AudioParameters& params, const std::string& device_id) {
387 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
388 return CreatePCMWaveInAudioInputStream(params, device_id);
391 // Factory for the implementations of AudioInputStream for
392 // AUDIO_PCM_LOW_LATENCY mode.
393 AudioInputStream* AudioManagerWin::MakeLowLatencyInputStream(
394 const AudioParameters& params, const std::string& device_id) {
395 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
396 DVLOG(1) << "MakeLowLatencyInputStream: " << device_id;
397 AudioInputStream* stream = NULL;
398 UMA_HISTOGRAM_BOOLEAN("Media.WindowsCoreAudioInput", core_audio_supported());
399 if (!core_audio_supported()) {
400 // Fall back to Windows Wave implementation on Windows XP or lower.
401 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista.";
402 stream = CreatePCMWaveInAudioInputStream(params, device_id);
403 } else {
404 stream = new WASAPIAudioInputStream(this, params, device_id);
407 return stream;
410 std::string AudioManagerWin::GetDefaultOutputDeviceID() {
411 if (!core_audio_supported())
412 return std::string();
413 return CoreAudioUtil::GetDefaultOutputDeviceID();
416 AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters(
417 const std::string& output_device_id,
418 const AudioParameters& input_params) {
419 DLOG_IF(ERROR, !core_audio_supported() && !output_device_id.empty())
420 << "CoreAudio is required to open non-default devices.";
422 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
423 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
424 int sample_rate = 48000;
425 int buffer_size = kFallbackBufferSize;
426 int bits_per_sample = 16;
427 int effects = AudioParameters::NO_EFFECTS;
428 bool use_input_params = !core_audio_supported();
429 if (core_audio_supported()) {
430 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
431 // TODO(rtoy): tune these values for best possible WebAudio
432 // performance. WebRTC works well at 48kHz and a buffer size of 480
433 // samples will be used for this case. Note that exclusive mode is
434 // experimental. This sample rate will be combined with a buffer size of
435 // 256 samples, which corresponds to an output delay of ~5.33ms.
436 sample_rate = 48000;
437 buffer_size = 256;
438 if (input_params.IsValid())
439 channel_layout = input_params.channel_layout();
440 } else {
441 AudioParameters params;
442 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(
443 output_device_id.empty() ?
444 GetDefaultOutputDeviceID() : output_device_id,
445 &params);
446 if (SUCCEEDED(hr)) {
447 bits_per_sample = params.bits_per_sample();
448 buffer_size = params.frames_per_buffer();
449 channel_layout = params.channel_layout();
450 sample_rate = params.sample_rate();
451 effects = params.effects();
452 } else {
453 // TODO(tommi): This should never happen really and I'm not sure that
454 // setting use_input_params is the right thing to do since WASAPI i
455 // definitely supported (see core_audio_supported() above) and
456 // |use_input_params| is only for cases when it isn't supported.
457 DLOG(ERROR) << "GetPreferredAudioParameters failed: " << std::hex << hr;
458 use_input_params = true;
463 if (input_params.IsValid()) {
464 // If the user has enabled checking supported channel layouts or we don't
465 // have a valid channel layout yet, try to use the input layout. See bugs
466 // http://crbug.com/259165 and http://crbug.com/311906 for more details.
467 if (core_audio_supported() &&
468 (cmd_line->HasSwitch(switches::kTrySupportedChannelLayouts) ||
469 channel_layout == CHANNEL_LAYOUT_UNSUPPORTED)) {
470 // Check if it is possible to open up at the specified input channel
471 // layout but avoid checking if the specified layout is the same as the
472 // hardware (preferred) layout. We do this extra check to avoid the
473 // CoreAudioUtil::IsChannelLayoutSupported() overhead in most cases.
474 if (input_params.channel_layout() != channel_layout) {
475 // TODO(henrika): Internally, IsChannelLayoutSupported does many of the
476 // operations that have already been done such as opening up a client
477 // and fetching the WAVEFORMATPCMEX format. Ideally we should only do
478 // that once. Then here, we can check the layout from the data we
479 // already hold.
480 if (CoreAudioUtil::IsChannelLayoutSupported(
481 output_device_id, eRender, eConsole,
482 input_params.channel_layout())) {
483 // Open up using the same channel layout as the source if it is
484 // supported by the hardware.
485 channel_layout = input_params.channel_layout();
486 DVLOG(1) << "Hardware channel layout is not used; using same layout"
487 << " as the source instead (" << channel_layout << ")";
492 effects |= input_params.effects();
493 if (use_input_params) {
494 // If WASAPI isn't supported we'll fallback to WaveOut, which will take
495 // care of resampling and bits per sample changes. By setting these
496 // equal to the input values, AudioOutputResampler will skip resampling
497 // and bit per sample differences (since the input parameters will match
498 // the output parameters).
499 bits_per_sample = input_params.bits_per_sample();
500 buffer_size = input_params.frames_per_buffer();
501 channel_layout = input_params.channel_layout();
502 sample_rate = input_params.sample_rate();
506 int user_buffer_size = GetUserBufferSize();
507 if (user_buffer_size)
508 buffer_size = user_buffer_size;
510 return AudioParameters(
511 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
512 sample_rate, bits_per_sample, buffer_size, effects);
515 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream(
516 const AudioParameters& params,
517 const std::string& device_id) {
518 std::string xp_device_id = device_id;
519 if (device_id != AudioManagerBase::kDefaultDeviceId &&
520 enumeration_type_ == kMMDeviceEnumeration) {
521 xp_device_id = ConvertToWinXPInputDeviceId(device_id);
522 if (xp_device_id.empty()) {
523 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID "
524 << device_id;
525 return NULL;
529 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers,
530 xp_device_id);
533 /// static
534 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) {
535 return new AudioManagerWin(audio_log_factory);
538 } // namespace media