Freigeben über


Connect Two Filters

 
Microsoft DirectShow 9.0

Connect Two Filters

The following function connects an output pin from one filter to the first available input pin on another filter:

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

To retrieve an available input pin on the downstream filter, this function calls the GetUnconnectedPin function, which is described in Find an Unconnected Pin on a Filter. If there is an unconnected input pin on the downstream filter, the function calls IGraphBuilder::Connect to connect the two pins. If the pins cannot connect directly to each other, the Connect method may put additional filters between them, in order to complete the connection.

Here is an overloaded version of the same function. The second parameter is a pointer to a filter, rather than a pin. The function connects the first filter to the second filter:

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

The following example uses this function to connect the AVI Mux filter to the File Writer filter. This example also uses the AddFilterByCLSID function, which is described in Add a Filter by CLSID.

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);
        /* Use IFileSinkFilter to set the file name (not shown). */
       pWrite->Release();
    }
    pMux->Release();
}

See Also