directshow,filter: 什么时候调用 m_pGraphBuilder->RemoveFilter
code如下: 如果用下列函数,调用系统自带的File Source,就无需 调用 m_pGraph->RemoveFilter,只要
调用 pFIFileSource->release();就可以了。
但是,如果不是系统的,必需用m_pGraphBuilder->RemoveFilter(),否则,filter的析构函数不会被调
用。
if (SUCCEEDED(hr))
hr = CoCreateInstance(CLSID_AsyncSample, NULL, CLSCTX_INPROC,
IID_IBaseFilter, (LPVOID *)
&pFIFileSource) ;
if (SUCCEEDED(hr))
hr = m_pGraphBuilder->AddFilter(pFIFileSource,L"Sample File Source");
IFileSourceFilter *pAVIFileSource = NULL;
if (SUCCEEDED(hr))
hr = pFIFileSource->QueryInterface(IID_IFileSourceFilter,(void **)
&pAVIFileSource);
同样:
if (SUCCEEDED(hr))
hr = CoCreateInstance(CLSID_MYSocketSource, NULL, CLSCTX_INPROC,
IID_IBaseFilter, (LPVOID *)
&pFIFileSource) ;
if (SUCCEEDED(hr))
hr = m_pGraphBuilder->AddFilter(pFIFileSource,L"MY Socket Source");
...
最后必需用m_pGraphBuilder->RemoveFilter(),否则,filter的析构函数不会被调用。否则有很大的内
存泄露。
不知道是什么原因:
因为 www.directshow.cn: 这个网址不行了,所以贴出下列code,请大家分享。
HRESULT ConnectPins( IGraphBuilder* pGraphBuilder,
IBaseFilter* pOutput,
IBaseFilter* pInput,
int nOutIndex,
int nInIndex)
{
HRESULT hr = NOERROR;
ULONG cFetched = 0;
IEnumPins* pEnumPins = 0;
IPin* pPinOutput = 0;
IPin* pPinInput = 0;
//enum output filter pins.
hr = pOutput->EnumPins(&pEnumPins);
if(FAILED(hr))
{
return hr;
}
//if the pin to connect is not the first one, we must skip nOutIndex-1 pins.
if(nOutIndex>1)
{
hr = pEnumPins->Skip(nOutIndex-1);
if(FAILED(hr))
{
pEnumPins->Release();
return hr;
}
}
//now we can get the output pin we wanted.
hr = pEnumPins->Next(1,&pPinOutput,&cFetched);
if(FAILED(hr))
{
pEnumPins->Release();
return hr;
}
pEnumPins->Release();
//enum input filter pins.
hr = pInput->EnumPins(&pEnumPins);
if(FAILED(hr))
{
pPinOutput->Release();
return hr;
}
//if input pin is not the first to connect, we must skip nInIndex-1 pins.
if(nInIndex>1)
{
hr = pEnumPins->Skip(nInIndex-1);
if( FAILED(hr) )
{
pPinOutput->Release();
pEnumPins->Release();
return hr;
}
}
//now we can get the input pin we wanted.
hr = pEnumPins->Next(1,&pPinInput,&cFetched);
if(FAILED(hr))
{
pPinOutput->Release();
pEnumPins->Release();
return hr;
}
pEnumPins->Release();
//now connect from output pin to input pin.
hr = pGraphBuilder->Connect(pPinOutput,pPinInput);
pPinOutput->Release();
pPinInput->Release();
return hr;
}
IPin* xMyFindPin(IBaseFilter *pFilter, const WCHAR achName[])
{
BOOL bFound = FALSE;
IEnumPins *pEnum;
IPin *pPin;
PIN_INFO pininfo;
if(pFilter==NULL)
return NULL;
pFilter->EnumPins(&pEnum);
while(pEnum->Next(1, &pPin, 0) == S_OK)
{
pPin->QueryPinInfo(&pininfo);
pininfo.pFilter->Release();
if (bFound = (0 == wcscmp(pininfo.achName,achName)))
break;
pPin->Release();
}
pEnum->Release();
return (bFound ? pPin : 0);
}
static const GUID CLSID_NULL_USER1=
{0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
HRESULT GetUnconnectedPin( IBaseFilter *pFilter,
PIN_DIRECTION PinDir,
IPin **ppPin,GUID gMajortype,WCHAR *achName)
{
*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 ) )
{
pTmp->Release( );
}
else
{
PIN_INFO pininfo;
pPin->QueryPinInfo(&pininfo);
pininfo.pFilter->Release();
if(achName && (wcslen(achName)>= wcslen(pininfo.achName)))
wcscpy(achName,pininfo.achName);
if(gMajortype==CLSID_NULL_USER1)
{
pEnum->Release( );
*ppPin = pPin;
return S_OK;
}
else
{
IEnumMediaTypes* pTypeEnum;
hr = pPin->EnumMediaTypes (&pTypeEnum);
hr =pTypeEnum->Reset();
AM_MEDIA_TYPE* pMediaType;
ULONG ul ;
do
{
hr = pTypeEnum->Next(1, &pMediaType, &ul) ;
if (FAILED(hr) || 0 == ul)
{
pTypeEnum->Release();
//pEnum->Release( );
if(pTmp)
pTmp->Release();
break ;
}
if (pMediaType->majortype == gMajortype)
// || pMediaType->subtype == MEDIASUBTYPE_DVSD)
{ pTypeEnum->Release();
pEnum->Release( );
*ppPin = pPin;
return S_OK;
}
}while (1) ;
}
}
if(pTmp)
pTmp->Release();
}
pPin->Release( );
}
pEnum->Release( );
return E_FAIL;
}
文章来源:
http://www.cnitblog.com/dvb-dvb/archive/2009/10/21/directshow.html