1


I'm currently trying to implement webcam video capture into a project.
First I was trying Direct Show, which worked on one computer but not on another.
So now I'm trying Media Foundation.
I mostly followed the examples provided by Microsoft.

Upon calling MFCreateDeviceSource() I receive the error code 0x80070002 (-2147024894).
This error code is not really documented in this context.
I get this result for three separate webcams of two different types of webcam, all of which work with other programs using Direct Show (eg VLC).
Thank you for any hint.

Operating system: Windows7
SDK: Windows SDK v7.1
IDE: Visual Studio 2008

Code:

// MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET) -- is called successfully in a previous function)

IMFMediaSource*  media_source = 0;
IMFSourceReader* source_reader = 0;
IMFAttributes* pAttributes = 0;
hr = MFCreateAttributes(&pAttributes, 2);
if (FAILED(hr)) {
    return false;
}
// Set the device type to video.
hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
if (FAILED(hr)) {
    return false;
}
// Set the symbolic link.
hr = pAttributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,(LPCWSTR)&device_name);
if (FAILED(hr)) {
    return false;
}
// Create device source interface
hr = MFCreateDeviceSource(pAttributes, &media_source);
if (FAILED(hr)) {
    // HERE I RECEIVE 0x80070002
    return false;
}
// Create source reader
IMFAttributes* attr;
MFCreateAttributes(&attr,1);
attr->SetUINT32(MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING,1);
hr = MFCreateSourceReaderFromMediaSource(media_source,attr,&source_reader);
if(FAILED(hr)) {
    return false;
}
4

1 回答 1

1

I edited your code to actually get the device name through enumerating capture devices present in the system and it worked fine, it lacks proper error checking, but I suggest you try the same, enumerate the devices and get the symbolic link from the actual device to see if that is the issue:

#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>

#pragma comment(lib, "mfplat.lib")
#pragma comment(lib, "mf.lib")
#pragma comment(lib, "mfreadwrite.lib")

int main(int argc, char** argv)
{
    HRESULT hr;
    hr = ::CoInitialize(NULL);
    if (FAILED(hr))
        abort();
    hr = ::MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET);
    if (FAILED(hr))
        abort();
    IMFMediaSource*  media_source = 0;
    IMFSourceReader* source_reader = 0;
    IMFAttributes* pAttributes = 0;
    hr = MFCreateAttributes(&pAttributes, 2);
    if (FAILED(hr))
        abort();
    // Set the device type to video.
    hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
    if (FAILED(hr))
        abort();
    UINT32 count;
    IMFActivate **ppDevices = NULL;
    hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
    if (FAILED(hr))
        abort();
    if (count == 0)
        abort();
    // Create the media source object.
    IMFMediaSource *pSource = NULL;
    hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
    if (FAILED(hr))
        abort();
    pSource->AddRef();
    IMFAttributes* pSourceAttributes;
    hr = pSource->QueryInterface(__uuidof(IMFAttributes), (void**)&pSourceAttributes);
    if (FAILED(hr))
        abort();
    const size_t nDeviceNameSize = 1024;
    LPWSTR pDeviceName = new WCHAR[nDeviceNameSize];
    UINT32 nActualBufferSize;
    hr = pSourceAttributes->GetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, pDeviceName, nDeviceNameSize, &nActualBufferSize);
    if (FAILED(hr))
        abort();
    // Set the symbolic link.
    hr = pAttributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,pDeviceName);
    if (FAILED(hr))
        abort();
    // Create device source interface
    hr = MFCreateDeviceSource(pAttributes, &media_source);
    if (FAILED(hr)) // HERE I RECEIVE 0x80070002
        abort();
    // Create source reader
    IMFAttributes* attr;
    MFCreateAttributes(&attr,1);
    attr->SetUINT32(MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING,1);
    hr = MFCreateSourceReaderFromMediaSource(media_source,attr,&source_reader);
    if(FAILED(hr))
        abort();
    ::CoUninitialize();
    return EXIT_SUCCESS;
}

Hope this helps.

于 2013-09-03T21:10:50.127 回答