Connect Two Filters
[The feature associated with this page, DirectShow, is a legacy feature. It has been superseded by MediaPlayer, IMFMediaEngine, and Audio/Video Capture in Media Foundation. Those features have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaPlayer, IMFMediaEngine and Audio/Video Capture in Media Foundation instead of DirectShow, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]
This topic shows some helper functions for connecting DirectShow filters.
To connect two filters, you must find an unconnected output pin on the upstream filter, and an unconnected input pin on the downstream filter.
If you already have pointers to both pins, call the IGraphBuilder::Connect method to connect them. If the pins cannot connect directly to each other, the IGraphBuilder::Connect method might insert additional filters, to complete the connection. For more information, see Intelligent Connect.
If you have a pointer to the filters but not the pins, you must use the IBaseFilter::EnumPins method to find the pins. (See Enumerating Pins.) The helper functions in this topic demonstrate this technique.
Output Pin to Filter
The following function takes two parameters: A pointer to an output pin, and a pointer to a filter. The function connects the output pin to the first available input pin on the filter.
// Connect output pin to filter.
HRESULT ConnectFilters(
IGraphBuilder *pGraph, // Filter Graph Manager.
IPin *pOut, // Output pin on the upstream filter.
IBaseFilter *pDest) // Downstream filter.
{
IPin *pIn = NULL;
// Find an input pin on the downstream filter.
HRESULT hr = FindUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
if (SUCCEEDED(hr))
{
// Try to connect them.
hr = pGraph->Connect(pOut, pIn);
pIn->Release();
}
return hr;
}
This function does the following:
- Calls the
FindUnconnectedPin
function to get an unconnected input pin. This function is shown in the topic Find an Unconnected Pin on a Filter. - Calls IGraphBuilder::Connect to connect the two pins.
Filter to Input Pin
The next function takes a pointer to a filter and a pointer to an input pin. It connects the input pin to the first available output pin on the filter.
// Connect filter to input pin.
HRESULT ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pSrc, IPin *pIn)
{
IPin *pOut = NULL;
// Find an output pin on the upstream filter.
HRESULT hr = FindUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
if (SUCCEEDED(hr))
{
// Try to connect them.
hr = pGraph->Connect(pOut, pIn);
pOut->Release();
}
return hr;
}
Filter to Filter
The third function takes a pointer to an upstream filter and a pointer to a downstream filter, and tries to connect both filters.
// Connect filter to filter
HRESULT ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pSrc, IBaseFilter *pDest)
{
IPin *pOut = NULL;
// Find an output pin on the first filter.
HRESULT hr = FindUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
if (SUCCEEDED(hr))
{
hr = ConnectFilters(pGraph, pOut, pDest);
pOut->Release();
}
return hr;
}
Related topics