创建一个directshow应用程序一

创建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)