Reputation: 33
I used a tutorial, provided by Microsoft MSDN, to Enumerate audio devices.
Here is enumeration code:
HRESULT CreateAudioDeviceSource(IMFMediaSource **ppSource)
{
*ppSource = NULL;
IMFMediaSource *pSource = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL;
// Create an attribute store to specify the enumeration parameters.
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (FAILED(hr))
{
goto done;
}
// Source type: audio capture devices
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID
);
if (FAILED(hr))
{
goto done;
}
// Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
std::cout << count;
if (FAILED(hr))
{
std::cout << "Enum Failed";
goto done;
}
if (count == 0)
{
std::cout << "empty";
hr = E_FAIL;
goto done;
}
// Create the media source object.
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
{
goto done;
}
*ppSource = pSource;
(*ppSource)->AddRef();
done:
SafeRelease(&pAttributes);
for (DWORD i = 0; i < count; i++)
{
SafeRelease(&ppDevices[i]);
}
CoTaskMemFree(ppDevices);
SafeRelease(&pSource);
return hr;
}
But I failed to call the function to enumerate the device. I got message "Enum Failed". So I don't know why the problem happened.
Please, Thank you so much !
Reference: https://msdn.microsoft.com/en-us/library/dd317912(v=vs.85).aspx
Upvotes: 1
Views: 1340
Reputation: 647
Sample code for Windows Media Foundation enumerate audio devices, the device capture struct
struct CaptureDeviceParam
{
/// <summary>
/// The array of devices.
/// </summary>
IMFActivate **ppDevices;
/// <summary>
/// Device count.
/// </summary>
UINT32 count;
/// <summary>
/// Device selection.
/// </summary>
UINT32 selection;
};
And the enum
device method.
/// <summary>
/// Get the audio capture devices.
/// </summary>
/// <param name="param">The capture device param.</param>
void MediaCapture::GetAudioCaptureDevices(CaptureDeviceParam *param)
{
HRESULT hr = S_OK;
IMFAttributes *pAttributes = NULL;
// Initialize an attribute store to specify enumeration parameters.
hr = MFCreateAttributes(&pAttributes, 1);
// Ask for source type = audio capture devices
if (SUCCEEDED(hr))
{
// Set the device attribute.
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID
);
}
// Enumerate devices.
if (SUCCEEDED(hr))
{
// Enumerate the device list.
hr = MFEnumDeviceSources(pAttributes, &(*param).ppDevices, &(*param).count);
}
// Safe release.
SafeRelease(&pAttributes);
}
GetAudioCaptureDevices
is a static method in the 'MediaCapture' class, which can be called anytime.
Upvotes: 1