Ejemplo n.º 1
0
HRESULT
WasapiWrap::DoDeviceEnumeration(void)
{
    HRESULT hr = 0;
    IMMDeviceEnumerator *deviceEnumerator = nullptr;

    m_deviceInfo.clear();

    HRR(CoCreateInstance(__uuidof(MMDeviceEnumerator),
        nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator)));

    HRR(deviceEnumerator->EnumAudioEndpoints(
        eRender, DEVICE_STATE_ACTIVE, &m_deviceCollection));

    UINT nDevices = 0;
    HRG(m_deviceCollection->GetCount(&nDevices));

    for (UINT i = 0; i<nDevices; ++i) {
        wchar_t name[WW_DEVICE_NAME_COUNT];
        HRG(DeviceNameGet(m_deviceCollection, i, name, sizeof name));
        m_deviceInfo.push_back(WWDeviceInfo(i, name));
    }

end:
    SafeRelease(&deviceEnumerator);
    return hr;
}
HRESULT
WWAudioDeviceEnumerator::BuildDeviceList(WWDeviceType t)
{
    HRESULT hr = 0;

    switch (t) {
    case WWDTPlay:
        m_dataFlow = eRender;
        break;
    case WWDTRec:
        m_dataFlow = eCapture;
        break;
    default:
        assert(0);
        return E_FAIL;
    }

    HRR(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_deviceEnumerator)));

    if (m_deviceEnumerator) {
        for (auto it = m_notificationClientList.begin();
                it != m_notificationClientList.end(); ++it) {
            m_deviceEnumerator->RegisterEndpointNotificationCallback(*it);
        }
    }

    HRR(m_deviceEnumerator->EnumAudioEndpoints(m_dataFlow, DEVICE_STATE_ACTIVE, &m_deviceCollection));

    UINT nDevices = 0;
    HRG(m_deviceCollection->GetCount(&nDevices));

    for (UINT i=0; i<nDevices; ++i) {
        wchar_t name[WW_DEVICE_NAME_COUNT];
        wchar_t idStr[WW_DEVICE_IDSTR_COUNT];
        HRG(DeviceNameGet(m_deviceCollection, i, name, sizeof name));
        HRG(DeviceIdStringGet(m_deviceCollection, i, idStr, sizeof idStr));
        m_deviceInfo.push_back(WWDeviceInfo(i, name, idStr));
    }

end:
    return hr;
}