capture filter
capture filter属于source filter。我用到的是usb接口的摄像头。
capture filter 一般有capture 的pin。preview的pin可有可无。
若没有preview,当需要捕获视频到文件和预览同时进行,则加上smart tee的filter,将capture filter的capture pin 连接到 smart tee的输入pin,smart tee的两个输出pin为 preview和capture。
有的capture filter有静态pin(PIN_CATEGORY_STILL)。 拍照用,一般提供比视频流更高质量的图片。
renderstream
ICaptureGraphBuilder2::RenderStream方法可以同时将两个或者三个filter连接成一个链(chain)。通常情况下,当每个filter只有一个输出pin和一个输入pin时,这个方法就才适用。
RenderStream(NULL, NULL, A, B, C)连接A->B->C;
RenderStream(NULL, NULL, A, B, C);RenderStream(NULL, NULL, C, D, E);连接 A->B->C->D->E;
RenderStream(NULL, NULL, A, NULL, NULL);连接A->render,视频就连video render,音频 audio render。
若A是capture filter。则需要指定前两个参数。
RenderStream(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,A, NULL,NULL);
msdn详细介绍:https://msdn.microsoft.com/en-us/library/aa930715.aspx
RenderStream()是智能连接,所以在程序中使用时,可能在你的两个filter之间加上合适的filter。和在graphedit中的直接手动连接得到的graph可能不同。
下面看看程序中怎么手动连接。
手动连接filter
//get unconnected pin of the defined filter
HRESULT CCapture::GetUnconnectedPin(IBaseFilter * pFilter, PIN_DIRECTION PinDir, IPin ** ppPin)
{
//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;
hr = pPin->ConnectedTo(&pTmp);//already connected,not the pin we want.
if (SUCCEEDED(hr)) pTmp->Release();
else //未连接,就是我们要找的pin,空闲的pin
{
pEnum->Release();
*ppPin = pPin;
return S_OK; //程序找到的是第一个未连接的空闲品
}
}
pPin->Release();//作用是 是pPin = NULL,pPin这个变量还存在,不会被销毁。
}
//获取空闲pin失败
pEnum->Release();
return E_NOTIMPL;
}
HRESULT CCapture::ConnectFilters(IGraphBuilder * pGraph, IPin * pOut, IBaseFilter * pDst)
{
if (pGraph == NULL || pOut == NULL || pDst == NULL) return E_NOTIMPL;
//检查上级pinout的合法性
PIN_DIRECTION PinDir;
pOut->QueryDirection(&PinDir);
if (PinDir != PINDIR_OUTPUT) return E_NOTIMPL;
//获得第一个空闲的pin in
IPin *PinIn = NULL;
HRESULT hr = GetUnconnectedPin(pDst,PINDIR_INPUT,&PinIn);
if (FAILED(hr)) return hr;
//try to connect them (pin in 和 pin out)
hr = pGraph->Connect(pOut,PinIn);
PinIn->Release();
return hr;
}
//重载+1
HRESULT CCapture::ConnectFilters(IGraphBuilder * pGraph, IBaseFilter * pSrc, IBaseFilter * pDst)
{
if (pGraph == NULL || pSrc == NULL || pDst == NULL) return E_NOTIMPL;
HRESULT hr;
IPin *pinSrc = NULL;
//out pin
hr = GetUnconnectedPin(pSrc,PINDIR_OUTPUT,&pinSrc);
if (FAILED(hr)) return hr;
//调用重载函数connectFilter
hr = ConnectFilters(pGraph,pinSrc,pDst);
if (FAILED(hr)) return hr;
pinSrc->Release();
return hr;
}
connect to remote graph
可以在graph中查看程序中构建的graph图。
首先需要写graphedit的入口函数和出口函数:
HRESULT CCapture::AddToRot(IUnknown * pUnkGraph, DWORD * pdwRegister)
{
IMoniker *pMoniker;
IRunningObjectTable *pROT;
if (FAILED(GetRunningObjectTable(0, &pROT))) {
return E_FAIL;
}
const size_t STRING_LENGTH = 256;
WCHAR wsz[STRING_LENGTH];
StringCchPrintfW(wsz, STRING_LENGTH,L"FilterGraph %08x pid %08x",
(DWORD_PTR)pUnkGraph,GetCurrentProcessId());
HRESULT hr = CreateItemMoniker(L"!",wsz,&pMoniker);
if (SUCCEEDED(hr))
{
hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE,pUnkGraph,
pMoniker,pdwRegister);
pMoniker->Release();
}
pROT->Release();
return hr;
}
void CCapture::RemoveFromRot(DWORD pdwRegister)
{
IRunningObjectTable *pROT;
if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
pROT->Revoke(pdwRegister);
pROT->Release();
}
}
调用:
IGraphBuilder *pGraph;
DWORD dwRegister;
// Create the filter graph manager.
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
#ifdef _DEBUG
hr = AddToRot(pGraph, &dwRegister);
#endif
// Rest of the application (not shown).
#ifdef _DEBUG
RemoveFromRot(dwRegister);
#endif
pGraph->Release();
msdn详细内容:https://msdn.microsoft.com/en-us/library/windows/desktop/dd390650(v=vs.85).aspx
bug:graphedit中如果不显示远程graph图。则 使用管理员模式 运行 regsvr32 proppage.dll(在graphedit的路径中,win7和xp的proppage.dll有区别)
调试自己编写的filter
工程->属性->调试->命令 :graphedt.exe。