作者:智慧的鱼
摘要:本文主要讲述了Graph图构建过程中常用的技巧,主要的内容如下:
·如何根据CLSID向graph中添加filter
·如何查找filter空闲的pin
·如何连接两个Filter
·如何获得filter或者pin的接口指针
·如何查找和某个filter的上下相连的filter
·如何删除graph中的所有filter
·如何利用Capture Graph Builder构建Graph图表
如何根据CLSID向graph中添加filter
下面的代码演示了如何利用CLSID生成一个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(&pF));
if (SUCCEEDED(hr))
{
hr = pGraph->AddFilter(pF, wszName);
if (SUCCEEDED(hr))
*ppF = pF;
else
pF->Release();
}
return hr;
}
在你的应用程序中,你可以这样用这个函数
IBaseFilter *pMux;
hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux);
if (SUCCEEDED(hr))
{
/* ... */
pMux->Release();
}
注:有些filter是不能通过with CoCreateInstance方法创建的。例如AVI Compressor Filter和WDM Video Capture filter
如何查找filter空闲的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, 这就是我们想要的pin,空闲的pin
{
pEnum->Release();
*ppPin = pPin;
return S_OK;
}
}
pPin->Release();
}
pEnum->Release();
// Did not find a matching pin.
return E_FAIL;
}
下面的代码演示了如何利用上面的函数来在一个filter查找一个输出的空闲的pin。
IPin *pOut = NULL;
HRESULT hr = GetUnconnectedPin(pFilter, PINDIR_OUTPUT, &pOut);
if (SUCCEEDED(hr))
{
/* ... */
pOut->Release();
}
如何连接两个Filter
下面的函数演示了如何将一个filter的输出pin和另一个filter的第一个空闲的输入pin进行连接。
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
//找一个空闲的输入pin
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的一个重载函数,但是第二个参数是一个指向filter的指针,而不是指向pin的指针,这个函数将两个filter连接起来。
HRESULT ConnectFilters(
IGraphBuilder *pGraph,
IBaseFilter *pSrc,
IBaseFilter *pDest)
{
if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
{
return E_POINTER;
}
// 首先在第一个filter上查询一个输出的pin接口
IPin *pOut = 0;
HRESULT hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
if (FAILED(hr))
{
return hr;
}
//然后将它和第二个filter的输入接口衔接。
hr = ConnectFilters(pGraph, pOut, pDest);
pOut->Release();
return hr;
}
下面的函数演示了利用这个函数来连接AVIMux 过滤器和File Writer过滤器,这个例子也使用了AddFilterByCLSID函数。
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();
}
如何获得filter或者pin的接口指针
一般来说,我们都是通过Filter图表管理器来进行一些操作,但是,有时候,我们也直接调用filter或者pin的一些方法,因此,我们需要获取filter或pin的接口指针。
对于filter的接口指针,可以通过IEnumFilters来枚举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;
}
用IEnumPins来获得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;
}
下面的代码演示了如何搜索任意的filter和pin的接口:
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. //调用的是上面的搜索pin的函数
hr = FindPinInterface(pF, iid, ppUnk);
}
pF->Release();
if (SUCCEEDED(hr))
{
break;
}
}
pEnum->Release();
return hr;
} 如何查找和某个filter的上下相连的filter
给你一个filter,你可以沿着graph图找到和它相联结的filter。首先枚举filter的pin,检查每一个pin是否有其他的pin的和它连接,如果有就检查连接pin属于哪个filter,你可以通过输入pin检查上游的filter,通过输出pin来检查下游的filter。
下面的函数返回上游或者下游的和本filter连接的filter,只要有一个match,就返回。
// Get the first upstream or downstream filter
HRESULT GetNextFilter(
IBaseFilter *pFilter, // 开始的filter
PIN_DIRECTION Dir, // 搜索的方向 (upstream 还是 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;
}
下面演示如何使用这个函数:
IBaseFilter *pF; // Pointer to some filter.
IBaseFilter *pUpstream = NULL;
if (SUCCEEDED(GetNextFilter(pF, PINDIR_INPUT, &pUpstream)))
{
// Use pUpstream ...
pUpstream->Release();
}
但是,一个filter可能在某个方向同时连接着两个或者更多个filter,例如一个分割filter,就有好几个filter与之相联。 因此,你可能想将所有的filter通过一个集合都搜集到。下面的例子代码就演示了如何通过CGenericList结构来实现这个方法。
#include <streams.h> // Link to the DirectShow base class library
// Define a typedef for a list of filters.
typedef CGenericList<IBaseFilter> CFilterList;
// Forward declaration. Adds a filter to the list unless it's a duplicate.
void AddFilterUnique(CFilterList &FilterList, IBaseFilter *pNew);
// Find all the immediate upstream or downstream peers of a filter.
HRESULT GetPeerFilters(
IBaseFilter *pFilter, // Pointer to the starting filter
PIN_DIRECTION Dir, // Direction to search (upstream or downstream)
CFilterList &FilterList) // Collect the results in this list.
{
if (!pFilter) 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();
if (FAILED(hr) || (PinInfo.pFilter == NULL))
{
// Something strange happened.
pPin->Release();
pEnum->Release();
return E_UNEXPECTED;
}
// 将符合的filter添加到list中
AddFilterUnique(FilterList, PinInfo.pFilter);
PinInfo.pFilter->Release();
}
}
pPin->Release();
}
pEnum->Release();
return S_OK;
}
void AddFilterUnique(CFilterList &FilterList, IBaseFilter *pNew)
{
if (pNew == NULL) return;
POSITION pos = FilterList.GetHeadPosition();
while (pos)
{
IBaseFilter *pF = FilterList.GetNext(pos);
if (IsEqualObject(pF, pNew))
{
return;
}
}
pNew->AddRef(); // The caller must release everything in the list.
FilterList.AddTail(pNew);
}
如何应用上面的函数呢?看看下面就知道了:
IBaseFilter *pF; // Pointer to some filter.
CFilterList FList(NAME("MyList")); // List to hold the downstream peers.
hr = GetPeerFilters(pF, PINDIR_OUTPUT, FList);
if (SUCCEEDED(hr)) //解析filter 的集合。
{
POSITION pos = FList.GetHeadPosition();
while (pos)
{
IBaseFilter *pDownstream = FList.GetNext(pos);
pDownstream->Release();
}
} 如何删除graph中的所有filter
很简单的,采用IFilterGraph::RemoveFilter函数
// Stop the graph.
pControl->Stop();
// Enumerate the filters in the graph.
IEnumFilters *pEnum = NULL;
HRESULT hr = pGraph->EnumFilters(&pEnum);
if (SUCCEEDED(hr))
{
IBaseFilter *pFilter = NULL;
while (S_OK == pEnum->Next(1, &pFilter, NULL))
{
// Remove the filter.
pGraph->RemoveFilter(pFilter);
// Reset the enumerator.
pEnum->Reset();
pFilter->Release();
}
pEnum->Release();
}
如何利用Capture Graph Builder构建Graph图表
Capture Graph Builder可以用来构建大多数的filter图表,并不仅仅是捕捉graph。本文简单介绍了如何利用Capture Graph Builder来构建graph。
Capture Graph Builder暴露了ICaptureGraphBuilder2接口指针,首先创建一个capture builder,和一个filter图表管理器对象,然后用图表管理器对象指针初始化Capture Graph Builder。代码如下:
IGraphBuilder *pGraph = NULL;
ICaptureGraphBuilder2 *pBuilder = NULL;
// Create the Filter Graph Manager.
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
if (SUCCEEDED(hr))
{
// Create the Capture Graph Builder.
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2,
(void **)&pBuilder);
if (SUCCEEDED(hr))
{
pBuilder->SetFiltergraph (pGraph);
}
};
连接filter
ICaptureGraphBuilder2::RenderStream方法可以同时将两个或者三个filter连接成一个链(chain)。通常情况下,当每个filter只有一个输出pin和一个输入pin时,这个方法就才,适用。
我们现在先忽略前两个参数,第三个参数是一个IUnknown指针,指向一个filter或者输出pin。第五,六个参数指向IBaseFilter指针。RenderStream就将三个filter连接成一个链。例如,假设A,B,C 是三个filter,每个filter只有一个输出pin和一个输入pin。
下面的代码可以将B连接到A上,将B连接到C上。
RenderStream(NULL, NULL, A, B, C)
所有的连接都是智能化的,如果是将两个filter相连,你可以将中间的参数设置为NULL:
RenderStream(NULL, NULL, A, NULL, C)
也可以调用两次这个函数创建一个更长的链条。
RenderStream(NULL, NULL, A, B, C)
RenderStream(NULL, NULL, C, D, E)
如果最后的一个参数设置为NULL,这个方法就自动的为graph设置一个renderer filter。如果是视频就设置成Video Renderer,如果是音频就设置为DirectSoundRenderer。因此RenderStream(NULL, NULL, A, NULL, NULL)等价于 RenderStream(NULL, NULL, A, NULL, R)
这里R指的是Render Filter。
如果你在第三个参数指定的是filter,而不是pin,你就要在第一二个参数里指定使用那个输出pin用于连接。
第一个参数只适用于捕捉filter,它指定pin的所属种类的GUID,具体的设置可以参考Pin Property Set.,但是下面的两个种类对于所有的filter都有效。
PIN_CATEGORY_CAPTURE
PIN_CATEGORY_PREVIEW
如果捕捉filter不支持捕捉和预览,RenderStream方法就增加一个Smart Tee来分割数据流。
如果播放文件,要将捕捉filter和一个mux filter连接起来,第二个参数指明了媒体类型:
MEDIATYPE_Audio
MEDIATYPE_Video
MEDIATYPE_Interleaved (DV)
查询filter和pin的接口指针
当你建立一个graph后,也许你需要查询graph中的filter和pin暴露的接口指针。例如,一个捕捉filter也许暴露了IAMDroppedFrames接口,它的输出pin也许暴露了IAMStreamConfig接口。
查询接口最简单地方法就是使用ICaptureGraphBuilder2::FindInterface方法。这个方法遍历整个graph的filter和pin,直到他找到合适的filter。你可以指定开始的filter,然后指定搜索的方向,(向上搜索还是向下搜索)。
下面的代码在一个视频预览pin上搜索IAMStreamConfig接口
IAMStreamConfig *pConfig = NULL;
HRESULT hr = pBuild->FindInterface(&PIN_CATEGORY_PREVIEW,&MEDIATYPE_Video,pVCap,IID_IAMStreamConfig,
(void**)&pConfig
);
if (SUCCESSFUL(hr))
{
/* ... */
pConfig->Release();
}
查找pin
如果你需要在某个filter上查询某个接口,可以用ICaptureGraphBuilder2::FindPin方法,代码如下:
IPin *pPin = NULL;
hr = pBuild->FindPin(
pCap, // Pointer to the filter to search.
PINDIR_OUTPUT, // Search for an output pin.
&PIN_CATEGORY_PREVIEW, // Search for a preview pin.
&MEDIATYPE_Video, // Search for a video pin.
TRUE, // The pin must be unconnected.
0, // Return the first matching pin (index 0).
&pPin); // This variable receives the IPin pointer.
if (SUCCESSFUL(hr))
{
/* ... */
pPin->Release();
}