2,554
社区成员
发帖
与我相关
我的任务
分享
//添加Capture Filter
hr = AddFilterByName(mGraph,CLSID_VideoInputDeviceCategory,wt,L"USB 视频设备",&pCap);
if (FAILED(hr))
{
AfxMessageBox("Can not add Capture Filter!");
return false;
}
//添加Infinite Pin Tee Filter
IBaseFilter *pInfinitePinTee;
hr = AddFilterByName(mGraph,CLSID_LegacyAmFilterCategory,L"Infinite Pin Tee Filter",L"Infinite Pin Tee Filter",&pInfinitePinTee);
if (FAILED(hr))
{
AfxMessageBox("Can not add Infinite Pin Tee Filter!");
return false;
}
//连接Capture Filter和Infinite Pin Tee Filter
//获取Capture Filter的捕获引脚
IPin *pCaptureOut = NULL;
pCaptureOut = GetOutPin(pCap,0);
if (FAILED(hr))
{
AfxMessageBox("Can not get Capture Filter capture Output pin!");
return false;
}
hr = ConnectFilters(mGraph,pCaptureOut,pInfinitePinTee);
if (FAILED(hr))
{
AfxMessageBox("Can not connect Capture Filter with Infinite Pin Tee Filter!");
return false;
}
//添加ffdshow Video Decoder Filter
IBaseFilter *pffdshowVdeoDecoder;
hr = AddFilterByName(mGraph,CLSID_LegacyAmFilterCategory,L"ffdshow Video Decoder",L"ffdshow Video Decoder",&pffdshowVdeoDecoder);
if (FAILED(hr))
{
AfxMessageBox("Can not add ffdshow Video Decoder Filter!");
return false;
}
EXTERN_C const GUID DECLSPEC_SELECTANY CLSID_FFDshowDecoder { 0x04FE9017, 0xF873, 0x410E, { 0x87, 0x1E, 0xAB, 0x91, 0x66, 0x1A, 0x4E, 0xF7 } };
//加入一个指定FriendlyName的Filter(只限添加DirectShow Filters)
HRESULT CDXGraph::AddFilterByName(IGraphBuilder *pGraph, REFCLSID clsidDeviceClass, LPWSTR szFriendlyName, LPCWSTR wszName, IBaseFilter **ppF)
{
if (!pGraph || !ppF)
return E_POINTER;
*ppF = 0;
IBaseFilter *pF = 0;
// 创建系统设备枚举器.
ICreateDevEnum *pDevEnum;
HRESULT hr =CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void**)&pDevEnum);
if ( FAILED(hr))
{
return hr;
}
// 创建所有指定Categorie的Filters 的枚举器.
IEnumMoniker *pEnum;
hr = pDevEnum->CreateClassEnumerator(clsidDeviceClass, &pEnum, 0);
if (FAILED(hr))
{
pDevEnum->Release ();
return hr;
}
// 逐一枚举DirectShow Filters,寻找指定FriendlyName的Filter
IMoniker *pMoniker;
ULONG cFetched;
while(pEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag *pBag;
pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
// 检查 FriendlyName.
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);
if(FAILED(hr))
{ //读FriendlyName错误
pBag->Release();
pMoniker->Release();
//继续枚举下一个Filter
continue;
}
if (lstrcmpW(var.bstrVal,szFriendlyName) == 0)
{
// 找到指定FriendlyName的Filter
IBaseFilter *pF = 0;
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pF);
//将找到的Filter加入Filter Graph
if (SUCCEEDED(hr))
{
hr = pGraph->AddFilter (pF, wszName);
if (SUCCEEDED(hr))
*ppF = pF;
else
pF->Release ();
}
SysFreeString(var.bstrVal);
pBag->Release();
pMoniker->Release();
pEnum->Release();
pDevEnum->Release();
return hr;
}
SysFreeString(var.bstrVal);
pBag->Release();
pMoniker->Release();
}
//没有找到指定FriendlyName的Filter
pEnum->Release();
pDevEnum->Release();
return E_FAIL;
}