MFCreateDeviceSource为网络摄像头返回错误0x80070002

时间:2013-09-03 07:33:49

标签: windows visual-c++ video webcam ms-media-foundation


我目前正在尝试将网络摄像头视频捕获实施到项目中 首先,我尝试使用Direct Show,它在一台计算机上运行但在另一台计算机上运行 所以现在我正在尝试媒体基金会 我主要关注微软提供的例子
在调用MFCreateDeviceSource()时,我收到错误代码0x80070002(-2147024894)。
在此上下文中并未真正记录此错误代码 我得到了两个不同类型网络摄像头的三个独立网络摄像头的结果,所有这些都可以使用Direct Show(例如VLC)与其他程序一起使用。
谢谢你的提示。

操作系统:Windows7
SDK:Windows SDK v7.1
IDE:Visual Studio 2008

代码:

// 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;
}

1 个答案:

答案 0 :(得分:1)

我编辑了你的代码,通过枚举系统中存在的捕获设备来实际获取设备名称,它工作正常,缺乏正确的错误检查,但我建议你尝试相同,枚举设备并从中获取符号链接实际设备,看看是否是问题:

#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;
}

希望这有帮助。