directshow应用开发的常用操作函数

获得一个filter的上游或者下游的filter:

static HRESULT GetNextFilter(IBaseFilter *pFilter,PIN_DIRECTION Dir,IBaseFilter **ppNext)
{
	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))
	{
		PIN_DIRECTION ThisPinDir;
		hr=pPin->QueryDirection(&ThisPinDir);
		if(FAILED(hr))
		{
			hr=E_UNEXPECTED;
			//pPin->Release();
			SAFE_RELEASE(pPin);
			break;
		}
		if(ThisPinDir==Dir)
		{
			IPin *pPinNext=0;
			hr=pPin->ConnectedTo(&pPinNext);
			if(SUCCEEDED(hr))
			{
				PIN_INFO PinInfo;
				hr=pPinNext->QueryPinInfo(&PinInfo);
				pPinNext->Release();
				pEnum->Release();
				if(FAILED(hr)||(PinInfo.pFilter==NULL))
				{
					return E_UNEXPECTED;
				}
				*ppNext=PinInfo.pFilter;
				return S_OK;
			}
		}
		pPin->Release();
	}
	pEnum->Release();
	return E_FAIL;
}

枚举一个graph图中的所有filter:

static HRESULT EnumFilters(IFilterGraph *pGraph)
{
	//枚举Filter Graph 中的所有Filter
	IEnumFilters *pEnum=NULL;
	HRESULT hr=pGraph->EnumFilters(&pEnum);
	if(SUCCEEDED(hr))
	{
		IBaseFilter* pFilter=NULL;
		FILTER_INFO Filter_Info;
		CLSID m_clsd;
		while(S_OK==pEnum->Next(1, &pFilter,NULL))
		{
			//pGraph->RemoveFilter(pFilter);//删除Filter
			//pEnum->Reset();               //复位枚举器内部状态
			pFilter->QueryFilterInfo(&Filter_Info);
			pFilter->GetClassID(&m_clsd);
			//pFilter->Release();
			SAFE_RELEASE(pFilter);
		}
		//pEnum->Release();
		SAFE_RELEASE(pEnum);
	}

	return hr;
}

在graphedit中动态显示应用程序中建的graph:

static HRESULT AddToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
	IMoniker * pMoniker;
	IRunningObjectTable *pROT;
	if (FAILED(GetRunningObjectTable(0, &pROT))) {
		return E_FAIL;
	}
	WCHAR wsz[256];
	wsprintfW(wsz, L"FilterGraph %08p pid %08x", (DWORD_PTR)pUnkGraph, GetCurrentProcessId());
	HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
	if (SUCCEEDED(hr)) {
		hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
		//pMoniker->Release();
		SAFE_RELEASE(pMoniker);
	}
	//pROT->Release();
	SAFE_RELEASE(pROT);
	return hr;
}

static void AddToObjectTable(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
	IMoniker * pMoniker = 0;
	IRunningObjectTable * objectTable = 0;
	if (SUCCEEDED(GetRunningObjectTable(0, &objectTable))) 
	{
		WCHAR wsz[256];
		wsprintfW(wsz, L"FilterGraph %08p pid %08x", (DWORD_PTR)pUnkGraph, GetCurrentProcessId());
		HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
		if (SUCCEEDED(hr)) 
		{
			hr = objectTable->Register(0, pUnkGraph, pMoniker, pdwRegister);
			//pMoniker->Release();
			SAFE_RELEASE(pMoniker);
		}
		//objectTable->Release();
		SAFE_RELEASE(objectTable);
	}
}

保存你所创建的graph:

static HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath) 
{
	const WCHAR wszStreamName[] = L"ActiveMovieGraph"; 
	HRESULT hr;

	IStorage *pStorage = NULL;
	hr = StgCreateDocfile(
		wszPath,
		STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
		0, &pStorage);
	if(FAILED(hr)) 
	{
		return hr;
	}

	IStream *pStream;
	hr = pStorage->CreateStream(
		wszStreamName,
		STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
		0, 0, &pStream);
	if (FAILED(hr)) 
	{
		SAFE_RELEASE(pStorage); 
		return hr;
	}

	IPersistStream *pPersist = NULL;
	pGraph->QueryInterface(IID_IPersistStream, (void**)&pPersist);
	hr = pPersist->Save(pStream, TRUE);

	SAFE_RELEASE(pStream);
	SAFE_RELEASE(pPersist);
	
	if (SUCCEEDED(hr)) 
	{
		hr = pStorage->Commit(STGC_DEFAULT);
	}
	SAFE_RELEASE(pStorage);

	return hr;
}


你可能感兴趣的:(filter,null,Graph)