创建一个directshow应用程序一

原创 2014年12月26日 09:28:00

创建dshow应用程序一般有三个步骤:

1.创建一个Filter Graph Manager组件。

IGraphBuilder * pGraph = NULL;

HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL,

CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph);,

2.根据实际的应用,创建一个filter链,比如播放一个本地文件,最简单快速的代码如下:

hr = pGraph->RenderFile(L"path", NULL);

3.调用Filter Graph Manager上的各个接口进行控制,并且完成Filter Graph Manager与应用程序的交互,比如调用IMediaControl接口方法控制Filter Graph的状态转换。

IMediaControl *pControl = NULL;

hr = pGraph->QueryInterface(IID_IMediaControl, (void**)pControl);

hr = pControl->Run();


通用的Filter Graph构建技术:

1.加入一个指定CLSID的Filter

给定了一个Filter的CLSID,就可以调用CoCreateInstance来创建它。并使用IFilterGraph::AddFilter接口将其加入到Filter Graph中。

代码如下:

HRESULT AddFilterByCLSID(
    IGraphBuilder *pGraph,  // Pointer to the Filter Graph Manager.
    const GUID& clsid,      // CLSID of the filter to create.
    LPCWSTR wszName,        // A name for the filter.
    IBaseFilter **ppF)      // Receives a pointer to the filter.
{
    if (!pGraph || ! ppF) return E_POINTER;
    *ppF = 0;
    IBaseFilter *pF = 0;
    HRESULT hr = CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,
        IID_IBaseFilter, reinterpret_cast<void**>(&pF));
    if (SUCCEEDED(hr))
    {
        hr = pGraph->AddFilter(pF, wszName);
        if (SUCCEEDED(hr))
            *ppF = pF;
        else
            pF->Release();
    }
    return hr;
}



要在Filter Graph中加入一个AVI Mux Filter

IBaseFilter *pMux;

hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux);

if(SUCCEEDED(hr))

{

/*...*/

pMux->Release();

}


2.得到Filter上的未连接的Pin

当在程序中连接filter的时候,首先要取得filter上未连接的Pin或者输出Pin。方法是,枚举Filter上的所有的Pin,调用IPin::QueryDirection查询Pin的方向。然后调用IPin::ConnectedTo查看Pin的连接状态。

HRESULT GetUnconnectedPin(
    IBaseFilter *pFilter,   // Pointer to the filter.
    PIN_DIRECTION PinDir,   // Direction of the pin to find.
    IPin **ppPin)           // Receives a pointer to the pin.
{
    *ppPin = 0;
    IEnumPins *pEnum = 0;
    IPin *pPin = 0;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if (FAILED(hr))
    {
        return hr;
    }
    while (pEnum->Next(1, &pPin, NULL) == S_OK)
    {
        PIN_DIRECTION ThisPinDir;
        pPin->QueryDirection(&ThisPinDir);
        if (ThisPinDir == PinDir)
        {
            IPin *pTmp = 0;
            hr = pPin->ConnectedTo(&pTmp);
            if (SUCCEEDED(hr))  // Already connected, not the pin we want.
            {
                pTmp->Release();
            }
            else  // Unconnected, this is the pin we want.
            {
                pEnum->Release();
                *ppPin = pPin;
                return S_OK;
            }
        }
        pPin->Release();
    }
    pEnum->Release();
    // Did not find a matching pin.
    return E_FAIL;
}


比如现在要得到一个未连接的输出Pin

IPin *pOut = NULL;

HRESULT hr = GetUnConnectedPin(pFilter, PINDIR_OUTPUT, &pOut);

if(SUCCEEDED(hr))

{

/*...*/

pOut->Release();

}


3.连接两个Filter

Filter Graph中连接两个Filter的函数有IFilterGraph::ConnectDirect和IFilterGraph::Connect

HRESULT ConnectFilters(
    IGraphBuilder *pGraph, // Filter Graph Manager.
    IPin *pOut,            // Output pin on the upstream filter.
    IBaseFilter *pDest)    // Downstream filter.
{
    if ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL))
    {
        return E_POINTER;
    }
#ifdef debug
        PIN_DIRECTION PinDir;
        pOut->QueryDirection(&PinDir);
        _ASSERTE(PinDir == PINDIR_OUTPUT);
#endif

    // Find an input pin on the downstream filter.
    IPin *pIn = 0;
    HRESULT hr = GetUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
    if (FAILED(hr))
    {
        return hr;
    }
    // Try to connect them.
    hr = pGraph->Connect(pOut, pIn);
    pIn->Release();
    return hr;
}

不同参数的ConnectFilters 的函数重载形式:

HRESULT ConnectFilters(
    IGraphBuilder *pGraph, 
    IBaseFilter *pSrc, 
    IBaseFilter *pDest)
{
    if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
    {
        return E_POINTER;
    }

    // Find an output pin on the first filter.
    IPin *pOut = 0;
    HRESULT hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
    if (FAILED(hr)) 
    {
        return hr;
    }
    hr = ConnectFilters(pGraph, pOut, pDest);
    pOut->Release();
    return hr;
}
例如现在要加入一个AVI Mux Filter 和 一个File Writer Filter,然后将他们相连起来。代码如下:
IBaseFilter *pMux, *pWrite;

hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux);

if(SUCCEEDED(hr))

{

hr = AddFilterByCLSID(pGraph, CLSID_FileWriter, L"File Writer", &pWrite);

if(SUCCEEDED(hr))

{

hr = ConnectFilters(pGraph, pMux, pWrite);

pWrite->Release();

}

pMux->Release();

}


4.查找Filter和Pin上的接口


我们需要枚举Graph中的所有的Filter,或者枚举Filter上所有的Pin,一个一个的找接口。

//查找Filter上实现的某个接口

HRESULT FindFilterInterface(
    IGraphBuilder *pGraph, // Pointer to the Filter Graph Manager.
    REFGUID iid,           // IID of the interface to retrieve.
    void **ppUnk)          // Receives the interface pointer.
{
    if (!pGraph || !ppUnk) return E_POINTER;

    HRESULT hr = E_FAIL;
    IEnumFilters *pEnum = NULL;
    IBaseFilter *pF = NULL;
    if (FAILED(pGraph->EnumFilters(&pEnum)))
    {
        return E_FAIL;
    }
    // Query every filter for the interface.
    while (S_OK == pEnum->Next(1, &pF, 0))
    {
        hr = pF->QueryInterface(iid, ppUnk);
        pF->Release();
        if (SUCCEEDED(hr))
        {
            break;
        }
    }
    pEnum->Release();
    return hr;
}

//查找给定filter的Pin上实现的某个接口

HRESULT FindPinInterface(
    IBaseFilter *pFilter,  // Pointer to the filter to search.
    REFGUID iid,           // IID of the interface.
    void **ppUnk)          // Receives the interface pointer.
{
    if (!pFilter || !ppUnk) return E_POINTER;


    HRESULT hr = E_FAIL;
    IEnumPins *pEnum = 0;
    if (FAILED(pFilter->EnumPins(&pEnum)))
    {
        return E_FAIL;
    }
    // Query every pin for the interface.
    IPin *pPin = 0;
    while (S_OK == pEnum->Next(1, &pPin, 0))
    {
        hr = pPin->QueryInterface(iid, ppUnk);
        pPin->Release();
        if (SUCCEEDED(hr))
        {
            break;
        }
    }
    pEnum->Release();
    return hr;
}
//综合了FindFilterInterface和FindPinInterfae两个函数的综合功能

HRESULT FindInterfaceAnywhere(
    IGraphBuilder *pGraph, 
    REFGUID iid, 
    void **ppUnk)
{
    if (!pGraph || !ppUnk) return E_POINTER;
    HRESULT hr = E_FAIL;
    IEnumFilters *pEnum = 0;
    if (FAILED(pGraph->EnumFilters(&pEnum)))
    {
        return E_FAIL;
    }
    // Loop through every filter in the graph.
    IBaseFilter *pF = 0;
    while (S_OK == pEnum->Next(1, &pF, 0))
    {
        hr = pF->QueryInterface(iid, ppUnk);
        if (FAILED(hr))
        {
            // The filter does not expose the interface, but maybe
            // one of its pins does.
            hr = FindPinInterface(pF, iid, ppUnk);
        }
        pF->Release();
        if (SUCCEEDED(hr))
        {
            break;
        }
    }
    pEnum->Release();
    return hr;
}

比如现在通过IGraphBuilder::RenderFile函数,构建了一条内含DV格式数据的AVI文件的回放链路。我们想要得到其中DV视频解码Filter上的IIPDVDec接口,以设置DV解码输出的图像大小为原始图像的四分之一,代码如下:
hr = pGraph->RenderFile(L"C:\\example.avi", 0);

if(SUCCEEDED(hr))

{

IIPDVDec* pDvDec;

hr = FindFilterInterface(pGraph, IID_IIPDVDec, (void**)&pDvDec);

if(SUCCEEDED(hr))

{

........

}

}


5.遍历Filter链路

给定一个Filter链路上的某个Filter,我们可以向上或者向下得到所有的其他的Filter。

HRESULT GetNextFilter(
    IBaseFilter *pFilter, // Pointer to the starting filter
    PIN_DIRECTION Dir,    // Direction to search (upstream or downstream)
    IBaseFilter **ppNext) // Receives a pointer to the next filter.
{
    if (!pFilter || !ppNext) return E_POINTER;

    IEnumPins *pEnum = 0;
    IPin *pPin = 0;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if (FAILED(hr)) return hr;
    while (S_OK == pEnum->Next(1, &pPin, 0))
    {
        // See if this pin matches the specified direction.
        PIN_DIRECTION ThisPinDir;
        hr = pPin->QueryDirection(&ThisPinDir);
        if (FAILED(hr))
        {
            // Something strange happened.
            hr = E_UNEXPECTED;
            pPin->Release();
            break;
        }
        if (ThisPinDir == Dir)
        {
            // Check if the pin is connected to another pin.
            IPin *pPinNext = 0;
            hr = pPin->ConnectedTo(&pPinNext);
            if (SUCCEEDED(hr))
            {
                // Get the filter that owns that pin.
                PIN_INFO PinInfo;
                hr = pPinNext->QueryPinInfo(&PinInfo);
                pPinNext->Release();
                pPin->Release();
                pEnum->Release();
                if (FAILED(hr) || (PinInfo.pFilter == NULL))
                {
                    // Something strange happened.
                    return E_UNEXPECTED;
                }
                // This is the filter we're looking for.
                *ppNext = PinInfo.pFilter; // Client must release.
                return S_OK;
            }
        }
        pPin->Release();
    }
    pEnum->Release();
    // Did not find a matching filter.
    return E_FAIL;
}







































版权声明:本文为博主原创文章,未经博主允许不得转载。

DirectShow应用程序快速入门

DirectShow应用程序快速入门

Win7下VS2010配置DirectShow (WIN32 控制台应用程序)

1.下载DirectShow 2.配置:       2.1 将DirectShow解压,我的解压目录为D:\DirectShow;       2.2 配置VS2010的项目"属性":   ...

cocos2dx-lua开发旅程(一)

最近要做一个手游项目,打算用cocos2dx来做,我也是初学者,网上很多书籍和资料都是采用cocos2dx的c++版本来做开发示例的 很少关于cocos2dx和lua怎么结合在实际项目中的教程,这使...
  • a6627651
  • a6627651
  • 2015年03月05日 16:37
  • 18799

cocos2d-x lua 贪吃蛇完整项目

cocos2d-x lua 贪吃蛇游戏 一步一步开发学习 本文地址: http://blog.csdn.net/qq_26437925/article/details/51842647开发环境,项目...

DirectShow视频捕获应用程序编写(SDK翻译)(转载)

转自:http://www.cnblogs.com/zhmx/archive/2012/09/06/2672927.html 正文: DirectShow DirectS...

DirectShow应用程序快速入门

摘要:该文介绍如何编写DirectShow应用程序,属于入门级文章。如果你感兴趣,可以来看看这些东西。欢迎到我的博客blog.csdn.net/suntaoznz看其他文章! 一环境设置... 1二 ...

DIRECTSHOW平台下的应用程序开发

2007-01-20 12:13 Filter Graph Manager过滤器图形管理器主要接口分析 IAMGraphStreams 控制一个渲染实时(live)源的过滤器图...

wince下的directshow摄像头应用程序介绍

转自:http://blog.csdn.net/northcan/article/details/7268745 WinCE系统下基于DirectShow的摄像头应用编程    在WinCE设...

最简单的DirectShow应用程序(转载)

转自:http://blog.sina.com.cn/s/blog_4d8205e40100q4gw.html 正文: 1 最简单的DirectShow应用程序 — 播放视频 1.简...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:创建一个directshow应用程序一
举报原因:
原因补充:

(最多只允许输入30个字)