Enumerating Video Capture Devices
This topic describes how to enumerate the video capture devices on the user's system, and how create an instance of a device.
To enumerate the video capture devices on the system, do the following:
- Call MFCreateAttributes to create an attribute store. This function receives an IMFAttributes pointer.
- Call IMFAttributes::SetGUID to set the MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE attribute. Set the attribute value to MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID.
- Call MFEnumDeviceSources. This function receives an array of IMFActivate pointers and the array size. Each pointer represents a distinct video capture device.
To create an instance of a capture device:
- Call IMFActivate::ActivateObject to get a pointer to the IMFMediaSource interface.
The following code shows these steps:
HRESULT CreateVideoDeviceSource(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: video capture devices
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
);
if (FAILED(hr))
{
goto done;
}
// Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
if (FAILED(hr))
{
goto done;
}
if (count == 0)
{
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;
}
After you create media source, release the interface pointers and free the memory for the array:
SafeRelease(&pAttributes);
for (DWORD i = 0; i < count; i++)
{
SafeRelease(&ppDevices[i]);
}
CoTaskMemFree(ppDevices);
Related topics
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for