This source file includes following definitions.
- GetDeviceNamesWinImpl
- GetDeviceNamesWinXPImpl
- GetInputDeviceNamesWin
- GetOutputDeviceNamesWin
- GetInputDeviceNamesWinXP
- GetOutputDeviceNamesWinXP
- ConvertToWinXPInputDeviceId
#include <MMDeviceAPI.h>
#include <mmsystem.h>
#include <Functiondiscoverykeys_devpkey.h>
#include "media/audio/win/audio_manager_win.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_propvariant.h"
using base::win::ScopedComPtr;
using base::win::ScopedCoMem;
#define DRV_RESERVED 0x0800
#define DRV_QUERYFUNCTIONINSTANCEID (DRV_RESERVED + 17)
#define DRV_QUERYFUNCTIONINSTANCEIDSIZE (DRV_RESERVED + 18)
namespace media {
static bool GetDeviceNamesWinImpl(EDataFlow data_flow,
AudioDeviceNames* device_names) {
ScopedComPtr<IMMDeviceEnumerator> enumerator;
HRESULT hr = enumerator.CreateInstance(__uuidof(MMDeviceEnumerator), NULL,
CLSCTX_INPROC_SERVER);
DCHECK_NE(CO_E_NOTINITIALIZED, hr);
if (FAILED(hr)) {
LOG(WARNING) << "Failed to create IMMDeviceEnumerator: " << std::hex << hr;
return false;
}
ScopedComPtr<IMMDeviceCollection> collection;
hr = enumerator->EnumAudioEndpoints(data_flow,
DEVICE_STATE_ACTIVE,
collection.Receive());
if (FAILED(hr))
return false;
UINT number_of_active_devices = 0;
collection->GetCount(&number_of_active_devices);
if (number_of_active_devices == 0)
return true;
AudioDeviceName device;
for (UINT i = 0; i < number_of_active_devices; ++i) {
ScopedComPtr<IMMDevice> audio_device;
hr = collection->Item(i, audio_device.Receive());
if (FAILED(hr))
continue;
ScopedCoMem<WCHAR> endpoint_device_id;
audio_device->GetId(&endpoint_device_id);
device.unique_id =
base::WideToUTF8(static_cast<WCHAR*>(endpoint_device_id));
ScopedComPtr<IPropertyStore> properties;
hr = audio_device->OpenPropertyStore(STGM_READ, properties.Receive());
if (SUCCEEDED(hr)) {
base::win::ScopedPropVariant friendly_name;
hr = properties->GetValue(PKEY_Device_FriendlyName,
friendly_name.Receive());
if (SUCCEEDED(hr) &&
friendly_name.get().vt == VT_LPWSTR && friendly_name.get().pwszVal) {
device.device_name = base::WideToUTF8(friendly_name.get().pwszVal);
}
}
device_names->push_back(device);
}
return true;
}
template <UINT (__stdcall *NumDevsFunc)(),
typename CAPSSTRUCT,
MMRESULT (__stdcall *DevCapsFunc)(UINT_PTR, CAPSSTRUCT*, UINT)>
static bool GetDeviceNamesWinXPImpl(AudioDeviceNames* device_names) {
UINT number_of_active_devices = NumDevsFunc();
if (number_of_active_devices == 0)
return true;
AudioDeviceName device;
CAPSSTRUCT capabilities;
MMRESULT err = MMSYSERR_NOERROR;
for (UINT i = 0; i < number_of_active_devices; ++i) {
err = DevCapsFunc(i, &capabilities, sizeof(capabilities));
if (err != MMSYSERR_NOERROR)
continue;
device.device_name = base::WideToUTF8(capabilities.szPname);
device.unique_id = device.device_name;
device_names->push_back(device);
}
return true;
}
bool GetInputDeviceNamesWin(AudioDeviceNames* device_names) {
return GetDeviceNamesWinImpl(eCapture, device_names);
}
bool GetOutputDeviceNamesWin(AudioDeviceNames* device_names) {
return GetDeviceNamesWinImpl(eRender, device_names);
}
bool GetInputDeviceNamesWinXP(AudioDeviceNames* device_names) {
return GetDeviceNamesWinXPImpl<
waveInGetNumDevs, WAVEINCAPSW, waveInGetDevCapsW>(device_names);
}
bool GetOutputDeviceNamesWinXP(AudioDeviceNames* device_names) {
return GetDeviceNamesWinXPImpl<
waveOutGetNumDevs, WAVEOUTCAPSW, waveOutGetDevCapsW>(device_names);
}
std::string ConvertToWinXPInputDeviceId(const std::string& device_id) {
UINT number_of_active_devices = waveInGetNumDevs();
MMRESULT result = MMSYSERR_NOERROR;
UINT i = 0;
for (; i < number_of_active_devices; ++i) {
size_t size = 0;
result = waveInMessage(reinterpret_cast<HWAVEIN>(i),
DRV_QUERYFUNCTIONINSTANCEIDSIZE,
reinterpret_cast<DWORD_PTR>(&size), NULL);
if (result != MMSYSERR_NOERROR)
continue;
ScopedCoMem<WCHAR> id;
id.Reset(static_cast<WCHAR*>(CoTaskMemAlloc(size)));
if (!id)
continue;
result = waveInMessage(
reinterpret_cast<HWAVEIN>(i), DRV_QUERYFUNCTIONINSTANCEID,
reinterpret_cast<DWORD_PTR>(static_cast<WCHAR*>(id)), size);
if (result != MMSYSERR_NOERROR)
continue;
std::string utf8_id = base::WideToUTF8(static_cast<WCHAR*>(id));
if (device_id == utf8_id)
break;
}
if (i < number_of_active_devices) {
WAVEINCAPS capabilities;
result = waveInGetDevCaps(i, &capabilities, sizeof(capabilities));
if (result == MMSYSERR_NOERROR)
return base::WideToUTF8(capabilities.szPname);
}
return std::string();
}
}