Chromium Code Reviews| Index: media/audio/win/core_audio_util_win.cc |
| diff --git a/media/audio/win/core_audio_util_win.cc b/media/audio/win/core_audio_util_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..058074a791d5fa1576e5f99a5ac7a9a8bd4950f0 |
| --- /dev/null |
| +++ b/media/audio/win/core_audio_util_win.cc |
| @@ -0,0 +1,246 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/audio/win/core_audio_util_win.h" |
| + |
| +#include <Audioclient.h> |
| +#include <Functiondiscoverykeys_devpkey.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/win/scoped_co_mem.h" |
| +#include "base/win/windows_version.h" |
| + |
| +using base::win::ScopedCoMem; |
| + |
| +namespace media { |
| + |
| +#define PRINT(sz, ...) wprintf(sz L"\n", __VA_ARGS__); |
|
scherkus (not reviewing)
2012/10/24 16:48:04
I'm not seeing this used -- time to remove?
henrika (OOO until Aug 14)
2012/10/24 19:25:44
Correct. Used while developing.
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + |
| +class ScopedPropertyVariant { |
|
DaleCurtis
2012/10/25 01:08:08
Is this a pattern that other people would benefit
tommi (sloooow) - chröme
2012/10/25 08:42:11
PROPVARIANT isn't that widely used so having it he
henrika (OOO until Aug 14)
2012/10/26 17:51:56
I added it when I did enumeration for Dale's IMMNo
|
| + public: |
| + ScopedPropertyVariant() { |
| + PropVariantInit(&prop_); |
| + } |
| + ~ScopedPropertyVariant() { |
| + PropVariantClear(&prop_); |
| + } |
| + |
| + PROPVARIANT* Get() { |
| + return &prop_; |
| + } |
| + |
| + private: |
| + PROPVARIANT prop_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedPropertyVariant); |
| +}; |
| + |
| +bool CoreAudioIsSupported() { |
| + // Microsoft does not plan to make the Core Audio APIs available for use |
| + // with earlier versions of Windows, including Microsoft Windows Server 2003, |
| + // Windows XP, Windows Millennium Edition, Windows 2000, and Windows 98. |
| + return (base::win::GetVersion() >= base::win::VERSION_VISTA); |
|
scherkus (not reviewing)
2012/10/24 16:48:04
as I noted in dalecurtis' http://codereview.chromi
henrika (OOO until Aug 14)
2012/10/24 19:25:44
I can make a follow-up patch when this one has lan
DaleCurtis
2012/10/25 01:08:08
Is core audio always supported when this is true?
henrika (OOO until Aug 14)
2012/10/26 17:51:56
I don't have a better method. You could also do so
|
| +} |
| + |
| +int CoreAudioNumberOfActiveDevices(EDataFlow data_flow) { |
| + DCHECK(CoreAudioIsSupported()); |
| + // Create the IMMDeviceEnumerator interface. |
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator = |
| + CoreAudioCreateDeviceEnumerator(); |
| + if (!device_enumerator) |
| + return 0; |
| + |
| + // Generate a collection of active (present and not disabled) audio endpoint |
| + // devices for the specified data-flow direction. |
| + // This method will succeed even if all devices are disabled. |
| + ScopedComPtr<IMMDeviceCollection> collection; |
| + HRESULT hr = device_enumerator->EnumAudioEndpoints(data_flow, |
| + DEVICE_STATE_ACTIVE, |
| + collection.Receive()); |
| + if (FAILED(hr)) { |
| + LOG(ERROR) << "IMMDeviceCollection::EnumAudioEndpoints: " << std::hex << hr; |
| + return 0; |
| + } |
| + |
| + // Retrieve the number of active audio devices for the specified direction |
| + UINT number_of_active_devices = 0; |
| + collection->GetCount(&number_of_active_devices); |
| + std::string flow = (data_flow == eCapture) ? "[in ] " : "[out] "; |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
if this is only for debugging, keep it in an #ifnd
|
| + DVLOG(1) << flow << "number of devices: " << number_of_active_devices; |
| + return static_cast<int>(number_of_active_devices); |
| +} |
| + |
| +ScopedComPtr<IMMDeviceEnumerator> CoreAudioCreateDeviceEnumerator() { |
| + DCHECK(CoreAudioIsSupported()); |
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator; |
| + HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), |
| + NULL, |
| + CLSCTX_INPROC_SERVER, |
| + __uuidof(IMMDeviceEnumerator), |
| + device_enumerator.ReceiveVoid()); |
| + LOG_IF(ERROR, FAILED(hr)) << "CoCreateInstance(IMMDeviceEnumerator): " |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
I'm tempted to make this a CHECK(SUCCEEDED(hr)).
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Agree. We should actually.
|
| + << std::hex << hr; |
| + return device_enumerator; |
| +} |
| + |
| +ScopedComPtr<IMMDevice> CoreAudioCreateDefaultDevice(EDataFlow data_flow, |
| + ERole role) { |
| + DCHECK(CoreAudioIsSupported()); |
| + ScopedComPtr<IMMDevice> endpoint_device; |
| + |
| + // Create the IMMDeviceEnumerator interface. |
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator = |
| + CoreAudioCreateDeviceEnumerator(); |
| + if (!device_enumerator) |
| + return endpoint_device; |
| + |
| + // Retrieve the default audio endpoint for the specified data-flow |
| + // direction and role. |
| + HRESULT hr = device_enumerator->GetDefaultAudioEndpoint( |
| + data_flow, role, endpoint_device.Receive()); |
| + |
| + if (FAILED(hr)) { |
| + LOG(ERROR) << "IMMDeviceEnumerator::GetDefaultAudioEndpoint: " |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
DVLOG. There's a lot of logging in this file and
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + << std::hex << hr; |
| + return endpoint_device; |
| + } |
| + |
| + // Verify that the audio endpoint device is active, i.e., that the audio |
| + // adapter that connects to the endpoint device is present and enabled. |
| + DWORD state = DEVICE_STATE_DISABLED; |
| + hr = endpoint_device->GetState(&state); |
| + if (SUCCEEDED(hr)) { |
| + if (!(state & DEVICE_STATE_ACTIVE)) { |
| + LOG(ERROR) << "Selected endpoint device is not active"; |
| + endpoint_device.Release(); |
| + } |
| + } |
| + return endpoint_device; |
| +} |
| + |
| +ScopedComPtr<IMMDevice> CoreAudioCreateDevice(const std::string& device_id) { |
| + DCHECK(CoreAudioIsSupported()); |
| + ScopedComPtr<IMMDevice> endpoint_device; |
| + |
| + // Create the IMMDeviceEnumerator interface. |
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator = |
| + CoreAudioCreateDeviceEnumerator(); |
| + if (!device_enumerator) |
| + return endpoint_device; |
| + |
| + // Retrieve an audio device specified by an endpoint device-identification |
| + // string. |
| + HRESULT hr = device_enumerator->GetDevice(UTF8ToUTF16(device_id).c_str(), |
| + endpoint_device.Receive()); |
| + LOG_IF(ERROR, FAILED(hr)) << "IMMDeviceEnumerator::GetDevice: " |
| + << std::hex << hr; |
| + return endpoint_device; |
| +} |
| + |
| +HRESULT CoreAudioGetDeviceName(IMMDevice* device, AudioDeviceName* name) { |
| + DCHECK(CoreAudioIsSupported()); |
| + DCHECK(device); |
| + AudioDeviceName device_name; |
| + |
| + // Retrieve unique name of endpoint device. |
| + // Example: "{0.0.1.00000000}.{8db6020f-18e3-4f25-b6f5-7726c9122574}". |
| + ScopedCoMem<WCHAR> endpoint_device_id; |
| + HRESULT hr = device->GetId(&endpoint_device_id); |
| + if (FAILED(hr)) |
| + return hr; |
| + device_name.unique_id = WideToUTF8(static_cast<WCHAR*>(endpoint_device_id)); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
is the static_cast needed?
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Changed to
WideToUTF8(endpoint_device_id, wcslen(e
|
| + |
| + // Retrieve user-friendly name of endpoint device. |
| + // Example: "Microphone (Realtek High Definition Audio)". |
| + ScopedComPtr<IPropertyStore> properties; |
| + hr = device->OpenPropertyStore(STGM_READ, properties.Receive()); |
| + if (FAILED(hr)) |
| + return hr; |
| + PROPVARIANT friendly_name; |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
why not use the ScopedPropVariant class?
henrika (OOO until Aug 14)
2012/10/26 17:51:56
LOL ;-)
I used it for debugging only. Forgot I co
|
| + PropVariantInit(&friendly_name); |
| + hr = properties->GetValue(PKEY_Device_FriendlyName, &friendly_name); |
| + if (FAILED(hr)) |
| + return hr; |
| + if (friendly_name.vt == VT_LPWSTR && friendly_name.pwszVal) { |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
no {}
(as is done in the if() right above)
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + device_name.device_name = WideToUTF8(friendly_name.pwszVal); |
| + } |
| + PropVariantClear(&friendly_name); |
| + |
| + *name = device_name; |
| + DVLOG(1) << "friendly name: " << device_name.device_name; |
| + DVLOG(1) << "unique id : " << device_name.unique_id; |
| + return hr; |
| +} |
| + |
| +std::string CoreAudioGetFriendlyName(const std::string& device_id) { |
| + DCHECK(CoreAudioIsSupported()); |
| + std::string name; |
| + |
| + ScopedComPtr<IMMDevice> audio_device = CoreAudioCreateDevice(device_id); |
| + if (!audio_device) |
| + return name; |
| + |
| + AudioDeviceName device_name; |
| + HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + if (FAILED(hr)) |
| + return name; |
| + |
| + name = device_name.device_name; |
| + return name; |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
nit: you don't really need the name variable. coul
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| +} |
| + |
| +bool CoreAudioDeviceIsDefault(EDataFlow flow, |
| + ERole role, |
| + std::string device_id) { |
| + DCHECK(CoreAudioIsSupported()); |
| + ScopedComPtr<IMMDevice> device = CoreAudioCreateDefaultDevice(flow, role); |
| + if (!device) |
| + return false; |
| + |
| + ScopedCoMem<WCHAR> default_device_id; |
| + HRESULT hr = device->GetId(&default_device_id); |
| + if (FAILED(hr)) |
| + return false; |
| + |
| + std::string str_default = WideToUTF8(static_cast<WCHAR*>(default_device_id)); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
static_cast?
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Fixed as above.
|
| + if (device_id.compare(str_default) != 0) { |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
no {}
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +EDataFlow CoreAudioGetDataFlow(IMMDevice* device) { |
| + DCHECK(CoreAudioIsSupported()); |
| + if (!device) |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
should we instead DCHECK(device) here and assume t
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + return eAll; |
| + |
| + ScopedComPtr<IMMEndpoint> endpoint; |
| + device->QueryInterface(endpoint.Receive()); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
I have a feeling that this might fail unless it's
henrika (OOO until Aug 14)
2012/10/26 17:51:56
Done.
|
| + |
| + EDataFlow data_flow; |
| + HRESULT hr = endpoint->GetDataFlow(&data_flow); |
| + if (FAILED(hr)) { |
| + LOG(ERROR) << "IMMEndpoint::GetDataFlow: " << std::hex << hr; |
| + return eAll; |
| + } |
| + return data_flow; |
| +} |
| + |
| +ScopedComPtr<IAudioClient> CoreAudioCreateClient(IMMDevice* audio_device) { |
| + DCHECK(CoreAudioIsSupported()); |
| + ScopedComPtr<IAudioClient> audio_client; |
| + |
| + // Creates and activates an IAudioClient COM object given the selected |
| + // endpoint device. |
| + HRESULT hr = audio_device->Activate(__uuidof(IAudioClient), |
| + CLSCTX_INPROC_SERVER, |
| + NULL, |
| + audio_client.ReceiveVoid()); |
| + LOG_IF(ERROR, FAILED(hr)) << "IMMDevice::Activate: " << std::hex << hr; |
| + return audio_client; |
| +} |
| + |
| +} // namespace media |