Search code examples
c++windowswasapi

Windows Audio Endpoint API. Getting the names of my Audio Devices


My main goal at the moment is to get detailed information about all of the local machine's Audio Endpoint Devices. That is the objects representing the audio peripherals. I want to be able to choose which device to record from based on some logic (or eventually allow the user to manually do so).

Here's what I've got so far. I'm pretty new to c++ so dealing with all of these abstract classes is getting a bit tricky so feel free to comment on code quality as well.

//Create vector of IMMDevices
UINT endpointCount = NULL;
(*pCollection).GetCount(&endpointCount);
std::vector<IMMDevice**> IMMDevicePP;   //IMMDevice seems to contain all endpoint devices, so why have a collection here?
for (UINT i = 0; i < (endpointCount); i++)
{
IMMDevice* pp = NULL;
(*pCollection).Item(i, &pp);
IMMDevicePP.assign(1, &pp);
}

My more technical goal at present is to get objects that implement this interface: http://msdn.microsoft.com/en-us/library/windows/desktop/dd371414(v=vs.85).aspx This is a type that is supposed to represent a single Audio Endpoint device whereas the IMMDevice seems to contain a collection of devices. However IMMEndpoint only contains a method called GetDataFlow so I'm unsure if that will help me. Again the goal is to easily select which endpoint device to record and stream audio from.

Any suggestions? Am I using the wrong API? This API definitely has good commands for the actual streaming and sampling of the audio but I'm a bit lost as to how to make sure I'm using the desired device.


Solution

  • After enumerating your IMMDevices as Sjoerd stated it is a must to retrieve the IPropertyStore information for the device. From there you have to extract the PROPVARIANT object as such:

    PROPERTYKEY key;
    HRESULT keyResult = (*IMMDeviceProperties[i]).GetAt(p, &key);
    

    then

    PROPVARIANT propVari;
    HRESULT propVariResult = (*IMMDeviceProperties[i]).GetValue(key, &propVari);
    

    according to these documents:

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb761471(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/aa380072(v=vs.85).aspx

    And finally to navigate the large PROPVARIANT structure in order to get the friendly name of the audio endpoint device simply access the pwszVal member of the PROPVARIANT structure as illustrated here:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd316594(v=vs.85).aspx

    All about finding the right documentation!