简体   繁体   English

在DirectShow中使用ICapturegraphBuilder

[英]Using ICapturegraphBuilder in DirectShow

I am working on Direct Show samples, and have run into a problem. 我正在研究Direct Show示例,但遇到了问题。 I want to multiplex video recievedfrom capture device to both preview and file, but do not know how. 我想将从捕获设备收到的视频多路复用到预览和文件,但是不知道如何。

The sample i use as base is playcap. 我用作基础的样本是playcap。 In there, ICaptureGraphBuilder2 is used; 在那里,使用了ICaptureGraphBuilder2 in its documentation is function SetOutputFileName that presumably turns on file output. 它的文档中包含函数SetOutputFileName ,该函数可能会打开文件输出。

In practive, though, file appears in the file system, but is always empty and is deleted when the application exits. 但是,在主动状态下,文件出现在文件系统中,但始终为空,并在应用程序退出时被删除。 I suspect wrong usage of the API on my part. 我怀疑API的使用错误。

Here is the code i am using: 这是我正在使用的代码:

    HRESULT hr;
IBaseFilter *pSrcFilter=NULL;

// Get DirectShow interfaces
hr = GetInterfaces();
if (FAILED(hr))
{
    Msg(TEXT("Failed to get video interfaces!  hr=0x%x"), hr);
    return hr;
}

// Attach the filter graph to the capture graph
hr = g_pCapture->SetFiltergraph(g_pGraph);
if (FAILED(hr))
{
    Msg(TEXT("Failed to set capture filter graph!  hr=0x%x"), hr);
    return hr;
}

// Use the system device enumerator and class enumerator to find
// a video capture/preview device, such as a desktop USB video camera.
hr = FindCaptureDevice(&pSrcFilter);
if (FAILED(hr))
{
    // Don't display a message because FindCaptureDevice will handle it
    return hr;
}

// Add Capture filter to our graph.
hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture");
if (FAILED(hr))
{
    Msg(TEXT("Couldn't add the capture filter to the graph!  hr=0x%x\r\n\r\n") 
        TEXT("If you have a working video capture device, please make sure\r\n")
        TEXT("that it is connected and is not being used by another application.\r\n\r\n")
        TEXT("The sample will now close."), hr);
    pSrcFilter->Release();
    return hr;
}
IBaseFilter * pBaseFilter;
IFileSinkFilter  *pfSink;
// Attach the file writerto the capture graph
hr = g_pCapture->SetOutputFileName (&MEDIASUBTYPE_Avi,L"output.avi",&pBaseFilter,&pfSink);
if (FAILED(hr))
{
    Msg(TEXT("Failed to set capture filter graph!  hr=0x%x"), hr);
    return hr;
}
// Render the preview pin on the video capture filter
// Use this instead of g_pGraph->RenderFile
hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
                               pSrcFilter, NULL, NULL);
if (FAILED(hr))
{
    Msg(TEXT("Couldn't render the video capture stream.  hr=0x%x\r\n")
        TEXT("The capture device may already be in use by another application.\r\n\r\n")
        TEXT("The sample will now close."), hr);
    pSrcFilter->Release();
    return hr;
}
// Now that the filter has been added to the graph and we have
// rendered its stream, we can release this reference to the filter.
pSrcFilter->Release();

// Set video window style and position
hr = SetupVideoWindow();
if (FAILED(hr))
{
    Msg(TEXT("Couldn't initialize video window!  hr=0x%x"), hr);
    return hr;
}

// Add our graph to the running object table, which will allow
// the GraphEdit application to "spy" on our graph
hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister);
if (FAILED(hr))
{
    Msg(TEXT("Failed to register filter graph with ROT!  hr=0x%x"), hr);
    g_dwGraphRegister = 0;
}
// Start previewing video data
hr = g_pMC->Run();
if (FAILED(hr))
{
    Msg(TEXT("Couldn't run the graph!  hr=0x%x"), hr);
    return hr;
}

// Remember current state
g_psCurrent = Running;

return S_OK;

How should i achieve functionality i want? 我应该如何实现我想要的功能?

In above code you are passing NULL to RenderStream as last parameter. 在上面的代码中,您将NULL作为最后一个参数传递给RenderStream。

At RenderStream msdn documentation RenderStream msdn文档

it is stated: 声明:

If the pSink parameter is NULL, the method tries to use a default renderer. 如果pSink参数为NULL,则该方法尝试使用默认渲染器。 For video it uses the Video Renderer, and for audio it uses the Audio Renderer (WaveOut) Filter. 对于视频,它使用视频渲染器,对于音频,它使用音频渲染器(WaveOut)过滤器。

On the same above link, please have a look at the code snippet: 在以上相同的链接上,请查看代码段:

pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, L"C:\\Example.avi",
    &ppf, &pSink);
pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
    pCaptureFilter, NULL, ppf);

You might want to look at AmCap also. 您可能还想看看AmCap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM