ICreateDevEnum *pDevEnum = NULL; IEnumMoniker *pEnum = NULL; // Create the System Device Enumerator. HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast<void**>(&pDevEnum)); if (SUCCEEDED(hr)) { // Create an enumerator for the video capture category. hr = pDevEnum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory, &pEnum, 0); } |
IGraphBuilder *pGraph = 0; ICaptureGraphBuilder2 *pBuild = 0; // Create the Capture Graph Builder. HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, 0, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pGraph); if (SUCCEEDED(hr)) { // Create the Filter Graph Manager. hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph); if (SUCCEEDED(hr)) { // Initialize the Capture Graph Builder. pBuild->SetFiltergraph(pGraph); } } |
ICaptureGraphBuilder2 *pBuild; // Capture Graph Builder // Initialize pBuild (not shown). IBaseFilter *pCap; // Video capture filter. /* Initialize pCap and add it to the filter graph (not shown). */ hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, NULL, NULL); |
调用RenderStream实现Preview链路,不管Capture Filter是否有Preview Pin或者只有VP Pin,Capture Graph Builder都能自动正确地处理。(如果只有VP Pin,则自动连接VP Pin;如果Capture Filter只有一个Capture Output Pin,则自动插入一个Smart Tee Filter然后再连接。)
要实现视频捕捉到文件,最简单的方法也是使用ICaptureGraphBuilder2::RenderStream。如下(假设生成的是AVI文件):
IBaseFilter *pMux; hr = pBuild->SetOutputFileName( &MEDIASUBTYPE_Avi, // Specifies AVI for the target file. L"C://Example.avi", // File name. &pMux, // Receives a pointer to the mux. NULL); hr = pBuild->RenderStream( &PIN_CATEGORY_CAPTURE, // Pin category. &MEDIATYPE_Video, // Media type. pCap, // Capture filter. NULL, // Intermediate filter (optional). pMux); // Mux or file sink filter. // Release the mux filter. pMux->Release(); |
pControl->Stop(); // Stop the graph. // Query the capture filter for the IAMVfwCaptureDialogs interface. IAMVfwCaptureDialogs *pVfw = 0; hr = pCap->QueryInterface(IID_IAMVfwCaptureDialogs, (void**)&pVfw); if (SUCCEEDED(hr)) { // Check if the device supports this dialog box. if (S_OK == pVfw->HasDialog(VfwCaptureDialog_Source)) { // Show the dialog box. hr = pVfw->ShowDialog(VfwCaptureDialog_Source, hwndParent); } } pControl->Run(); |