| 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..4ff164251be965f13700d7df5cce1a5dcecb4955
|
| --- /dev/null
|
| +++ b/media/audio/win/core_audio_util_win.cc
|
| @@ -0,0 +1,236 @@
|
| +// 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 {
|
| +namespace win {
|
| +
|
| +#define PRINT(sz, ...) wprintf(sz L"\n", __VA_ARGS__);
|
| +
|
| +class ScopedPropertyVariant {
|
| + public:
|
| + ScopedPropertyVariant() {
|
| + PropVariantInit(&prop_);
|
| + }
|
| + ~ScopedPropertyVariant() {
|
| + PropVariantClear(&prop_);
|
| + }
|
| +
|
| + PROPVARIANT* Get() {
|
| + return &prop_;
|
| + }
|
| +
|
| + private:
|
| + PROPVARIANT prop_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ScopedPropertyVariant);
|
| +};
|
| +
|
| +bool CoreAudioIsSupported() {
|
| + // Note that Windows Server 2003 does not support Windows Core Audio.
|
| + return (base::win::GetVersion() >= base::win::VERSION_VISTA);
|
| +}
|
| +
|
| +int NumberOfActiveAudioDevices(EDataFlow data_flow) {
|
| + // Create the IMMDeviceEnumerator interface.
|
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
|
| + CreateDeviceEnumerator();
|
| + 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] ";
|
| + DVLOG(1) << flow << "number of devices: " << number_of_active_devices;
|
| +
|
| + return static_cast<int>(number_of_active_devices);
|
| +}
|
| +
|
| +ScopedComPtr<IMMDeviceEnumerator> CreateDeviceEnumerator() {
|
| + 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): "
|
| + << std::hex << hr;
|
| + return device_enumerator;
|
| +}
|
| +
|
| +ScopedComPtr<IMMDevice> CreateDefaultAudioDevice(EDataFlow data_flow,
|
| + ERole role) {
|
| + ScopedComPtr<IMMDevice> endpoint_device;
|
| +
|
| + // Create the IMMDeviceEnumerator interface.
|
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
|
| + CreateDeviceEnumerator();
|
| + 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: "
|
| + << 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> CreateAudioDevice(const std::string& device_id) {
|
| + ScopedComPtr<IMMDevice> endpoint_device;
|
| +
|
| + // Create the IMMDeviceEnumerator interface.
|
| + ScopedComPtr<IMMDeviceEnumerator> device_enumerator =
|
| + CreateDeviceEnumerator();
|
| + 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 GetAudioDeviceName(IMMDevice* device, AudioDeviceName* name) {
|
| + 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));
|
| +
|
| + // 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;
|
| + 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) {
|
| + 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 GetFriendlyName(const std::string& device_id) {
|
| + std::string name;
|
| +
|
| + ScopedComPtr<IMMDevice> audio_device = CreateAudioDevice(device_id);
|
| + if (!audio_device)
|
| + return name;
|
| +
|
| + AudioDeviceName device_name;
|
| + HRESULT hr = GetAudioDeviceName(audio_device, &device_name);
|
| + if (FAILED(hr))
|
| + return name;
|
| +
|
| + name = device_name.device_name;
|
| + return name;
|
| +}
|
| +
|
| +bool IsDeviceDefault(EDataFlow flow, ERole role, std::string device_id) {
|
| + ScopedComPtr<IMMDevice> device = CreateDefaultAudioDevice(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));
|
| + if (device_id.compare(str_default) != 0) {
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +EDataFlow GetDataFlow(IMMDevice* device) {
|
| + if (!device)
|
| + return eAll;
|
| +
|
| + ScopedComPtr<IMMEndpoint> endpoint;
|
| + device->QueryInterface(endpoint.Receive());
|
| +
|
| + 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> CreateAudioClient(IMMDevice* audio_device) {
|
| + 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 win
|
| +} // namespace media
|
|
|