//创建graph图表
IGraphBuilder *pFilterGraph;
ICaptureGraphBuilder2 *pCaptureGraphBuilder;
HRESULT hr = CoCreateInstance(CLSID_FilterGraph,0,CLSCTX_INPROC,IID_IGraphBuilder,(void**)&pFilterGraph);
hr = CoCreateInstance(CLSID_CaptureGraphBuilder,0,CLSCTX_INPROC,IID_ICaptureGraphBuilder2,(void**)&pCaptureGraphBuilder);
pCaptureGraphBuilder->SetFiltergraph(pFilterGraph);
//创建捕捉filter
IBaseFilter *pVideoCaptureFilter;
hr = CoCreateInstance(CLSID_VideoCapture,0,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pVideoCaptureFilter);
IPersistPropertyBag *pPropertyBag;
pVideoCaptureFilter->QueryInterface(&pPropertyBag);
DEVMGR_DEVICE_INFORMATION devInfo;
CComVariant CamName;
IPropertyBag *propbag; //这里 别人说是 CPropertyBag 去实现IPropertyBag
GUID guidCamera = {}; //这里是camera 注册在系统里的GUID值
devInfo.dwSize = sizeof(devInfo);
//查找camera device
FindFirstDevice(DeviceSearchByGuid,&guidCamera,&devInfo);
CamName = devInfo.szLegacyName;
propbag->Write(L"VcapName",&CamName);
pPropertyBag->Load(propbag,NULL);
//添加捕捉filter
pFilterGraph->AddFilter(pVideoCaptureFilter,L"MyCapture");
pPropertyBag->Release();
propbag->Release();
//创建解码filter
IBaseFilter *pVideoEncode;
IDMOWrapperFilter *pWrapperFilter; //接口
hr = CoCreateInstance(CLSID_DMOWrapperFilter,0,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pVideoEncode);
pVideoEncode->QueryInterface(&pWrapperFilter);
pWrapperFilter->Init(CLSID_CWMV9EncMediaObject,DMOCATEGORY_VIDEO_ENCODER);
//添加解码filter
pFilterGraph->AddFilter(pVideoEncode,L"video encode");
//建立asf流生成filter和file write filter
IBaseFilter *pAsfMultiplexer;
IFileSinkFilter *pFileSinkFilter; //接口
pCaptureGraphBuilder->SetOutputFileName(&MEDIASUBTYPE_Asf,L"C:\\test.asf",&pAsfMultiplexer,&pFileSinkFilter);
//连接所有的filter
pCaptureGraphBuilder->RenderStream(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,pVideoCaptureFilter,pVideoEncode,pAsfMultiplexer);
//启动Graph
IMediaControl *pMC;
pFilterGraph->QueryInterface(IID_IMediaControl,(void**)&pMC);
pMC->Run();
MSDN
ICaptureGraphBuilder2::RenderStream
HRESULT RenderStream( const GUID* pCategory, const GUID* pType, IUnknown* pSource, IBaseFilter* pIntermediate, IBaseFilter* pSink);
pCategory
[in] A pointer to a pin category from the AMPROPERTY_PIN_CATEGORY property set (seePin Property Set). Use NULL to match any category. The following list shows typical categories.
- PIN_CATEGORY_CAPTURE //捕捉
- PIN_CATEGORY_PREVIEW //预览
- PIN_CATEGORY_CC
-
pType
-
[in] Pointer to a major-type GUID that specifies the media type of the output pin; or NULL to use any pin, regardless of media type. For a list of possible values, seeMedia Types and Sub Types.
-
pSource
-
[in] Specifies a pointer to the starting filter for the connection, or to an output pin.
-
pIntermediate
-
[in] Pointer to the IBaseFilter Interface of an intermediate filter, such as a compression filter. Can be NULL.
-
pSink
-
[in] Pointer to the IBaseFilter interface of a sink filter, such asa renderer or mux filter. If the value is NULL, the method uses a default renderer (see Remarks).
想预览的话自己加个videoRender filter 使用Preview Pin 预览画面