Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / audio / win / audio_manager_win.cc
blob7da916d3f0783bea84d1c89fdce67bce075c881c
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 = 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
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 HRESULT hr = E_FAIL;
307 AudioParameters parameters;
308 if (core_audio_supported()) {
309 hr = CoreAudioUtil::GetPreferredAudioParameters(device_id, false,
310 &parameters);
313 if (FAILED(hr) || !parameters.IsValid()) {
314 // Windows Wave implementation is being used.
315 parameters = AudioParameters(
316 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 48000, 16,
317 kFallbackBufferSize, AudioParameters::NO_EFFECTS);
320 int user_buffer_size = GetUserBufferSize();
321 if (user_buffer_size) {
322 parameters.Reset(parameters.format(), parameters.channel_layout(),
323 parameters.channels(), parameters.sample_rate(),
324 parameters.bits_per_sample(), user_buffer_size);
327 return parameters;
330 std::string AudioManagerWin::GetAssociatedOutputDeviceID(
331 const std::string& input_device_id) {
332 if (!core_audio_supported()) {
333 NOTIMPLEMENTED()
334 << "GetAssociatedOutputDeviceID is not supported on this OS";
335 return std::string();
337 return CoreAudioUtil::GetMatchingOutputDeviceID(input_device_id);
340 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR
341 // mode.
342 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
343 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream(
344 const AudioParameters& params) {
345 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
346 if (params.channels() > kWinMaxChannels)
347 return NULL;
349 return new PCMWaveOutAudioOutputStream(this,
350 params,
351 NumberOfWaveOutBuffers(),
352 WAVE_MAPPER);
355 // Factory for the implementations of AudioOutputStream for
356 // AUDIO_PCM_LOW_LATENCY mode. Two implementations should suffice most
357 // windows user's needs.
358 // - PCMWaveOutAudioOutputStream: Based on the waveOut API.
359 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API.
360 AudioOutputStream* AudioManagerWin::MakeLowLatencyOutputStream(
361 const AudioParameters& params,
362 const std::string& device_id) {
363 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
364 if (params.channels() > kWinMaxChannels)
365 return NULL;
367 if (!core_audio_supported()) {
368 // Fall back to Windows Wave implementation on Windows XP or lower.
369 DLOG_IF(ERROR, !device_id.empty() &&
370 device_id != AudioManagerBase::kDefaultDeviceId)
371 << "Opening by device id not supported by PCMWaveOutAudioOutputStream";
372 DVLOG(1) << "Using WaveOut since WASAPI requires at least Vista.";
373 return new PCMWaveOutAudioOutputStream(
374 this, params, NumberOfWaveOutBuffers(), WAVE_MAPPER);
377 // Pass an empty string to indicate that we want the default device
378 // since we consistently only check for an empty string in
379 // WASAPIAudioOutputStream.
380 return new WASAPIAudioOutputStream(this,
381 device_id == AudioManagerBase::kDefaultDeviceId ?
382 std::string() : device_id,
383 params,
384 params.effects() & AudioParameters::DUCKING ? eCommunications : eConsole);
387 // Factory for the implementations of AudioInputStream for AUDIO_PCM_LINEAR
388 // mode.
389 AudioInputStream* AudioManagerWin::MakeLinearInputStream(
390 const AudioParameters& params, const std::string& device_id) {
391 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
392 return CreatePCMWaveInAudioInputStream(params, device_id);
395 // Factory for the implementations of AudioInputStream for
396 // AUDIO_PCM_LOW_LATENCY mode.
397 AudioInputStream* AudioManagerWin::MakeLowLatencyInputStream(
398 const AudioParameters& params, const std::string& device_id) {
399 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
400 DVLOG(1) << "MakeLowLatencyInputStream: " << device_id;
401 AudioInputStream* stream = NULL;
402 UMA_HISTOGRAM_BOOLEAN("Media.WindowsCoreAudioInput", core_audio_supported());
403 if (!core_audio_supported()) {
404 // Fall back to Windows Wave implementation on Windows XP or lower.
405 DVLOG(1) << "Using WaveIn since WASAPI requires at least Vista.";
406 stream = CreatePCMWaveInAudioInputStream(params, device_id);
407 } else {
408 stream = new WASAPIAudioInputStream(this, params, device_id);
411 return stream;
414 std::string AudioManagerWin::GetDefaultOutputDeviceID() {
415 if (!core_audio_supported())
416 return std::string();
417 return CoreAudioUtil::GetDefaultOutputDeviceID();
420 AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters(
421 const std::string& output_device_id,
422 const AudioParameters& input_params) {
423 DLOG_IF(ERROR, !core_audio_supported() && !output_device_id.empty())
424 << "CoreAudio is required to open non-default devices.";
426 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
427 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
428 int sample_rate = 48000;
429 int buffer_size = kFallbackBufferSize;
430 int bits_per_sample = 16;
431 int effects = AudioParameters::NO_EFFECTS;
432 bool use_input_params = !core_audio_supported();
433 if (core_audio_supported()) {
434 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
435 // TODO(rtoy): tune these values for best possible WebAudio
436 // performance. WebRTC works well at 48kHz and a buffer size of 480
437 // samples will be used for this case. Note that exclusive mode is
438 // experimental. This sample rate will be combined with a buffer size of
439 // 256 samples, which corresponds to an output delay of ~5.33ms.
440 sample_rate = 48000;
441 buffer_size = 256;
442 if (input_params.IsValid())
443 channel_layout = input_params.channel_layout();
444 } else {
445 AudioParameters params;
446 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(
447 output_device_id.empty() ? GetDefaultOutputDeviceID()
448 : output_device_id,
449 true, &params);
450 if (SUCCEEDED(hr)) {
451 bits_per_sample = params.bits_per_sample();
452 buffer_size = params.frames_per_buffer();
453 channel_layout = params.channel_layout();
454 sample_rate = params.sample_rate();
455 effects = params.effects();
456 } else {
457 // TODO(tommi): This should never happen really and I'm not sure that
458 // setting use_input_params is the right thing to do since WASAPI i
459 // definitely supported (see core_audio_supported() above) and
460 // |use_input_params| is only for cases when it isn't supported.
461 DLOG(ERROR) << "GetPreferredAudioParameters failed: " << std::hex << hr;
462 use_input_params = true;
467 if (input_params.IsValid()) {
468 // If the user has enabled checking supported channel layouts or we don't
469 // have a valid channel layout yet, try to use the input layout. See bugs
470 // http://crbug.com/259165 and http://crbug.com/311906 for more details.
471 if (core_audio_supported() &&
472 (cmd_line->HasSwitch(switches::kTrySupportedChannelLayouts) ||
473 channel_layout == CHANNEL_LAYOUT_UNSUPPORTED)) {
474 // Check if it is possible to open up at the specified input channel
475 // layout but avoid checking if the specified layout is the same as the
476 // hardware (preferred) layout. We do this extra check to avoid the
477 // CoreAudioUtil::IsChannelLayoutSupported() overhead in most cases.
478 if (input_params.channel_layout() != channel_layout) {
479 // TODO(henrika): Internally, IsChannelLayoutSupported does many of the
480 // operations that have already been done such as opening up a client
481 // and fetching the WAVEFORMATPCMEX format. Ideally we should only do
482 // that once. Then here, we can check the layout from the data we
483 // already hold.
484 if (CoreAudioUtil::IsChannelLayoutSupported(
485 output_device_id, eRender, eConsole,
486 input_params.channel_layout())) {
487 // Open up using the same channel layout as the source if it is
488 // supported by the hardware.
489 channel_layout = input_params.channel_layout();
490 DVLOG(1) << "Hardware channel layout is not used; using same layout"
491 << " as the source instead (" << channel_layout << ")";
496 effects |= input_params.effects();
497 if (use_input_params) {
498 // If WASAPI isn't supported we'll fallback to WaveOut, which will take
499 // care of resampling and bits per sample changes. By setting these
500 // equal to the input values, AudioOutputResampler will skip resampling
501 // and bit per sample differences (since the input parameters will match
502 // the output parameters).
503 bits_per_sample = input_params.bits_per_sample();
504 buffer_size = input_params.frames_per_buffer();
505 channel_layout = input_params.channel_layout();
506 sample_rate = input_params.sample_rate();
510 int user_buffer_size = GetUserBufferSize();
511 if (user_buffer_size)
512 buffer_size = user_buffer_size;
514 return AudioParameters(
515 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
516 sample_rate, bits_per_sample, buffer_size, effects);
519 AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream(
520 const AudioParameters& params,
521 const std::string& device_id) {
522 std::string xp_device_id = device_id;
523 if (device_id != AudioManagerBase::kDefaultDeviceId &&
524 enumeration_type_ == kMMDeviceEnumeration) {
525 xp_device_id = ConvertToWinXPInputDeviceId(device_id);
526 if (xp_device_id.empty()) {
527 DLOG(ERROR) << "Cannot find a waveIn device which matches the device ID "
528 << device_id;
529 return NULL;
533 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers,
534 xp_device_id);
537 /// static
538 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) {
539 return new AudioManagerWin(audio_log_factory);
542 } // namespace media