// Get DirectShow interfaces hr = GetInterfaces(); if (FAILED(hr)) { Msg(TEXT("Failed to get video interfaces! hr=0x%x"), hr); return hr; }
// Attach the filter graph to the capture graph hr = g_pCapture->SetFiltergraph(g_pGraph); if (FAILED(hr)) { Msg(TEXT("Failed to set capture filter graph! hr=0x%x"), hr); return hr; }
// Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. hr = FindCaptureDevice(&pSrcFilter); if (FAILED(hr)) { // Don't display a message because FindCaptureDevice will handle it return hr; }
// Add Capture filter to our graph. hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture"); if (FAILED(hr)) { Msg(TEXT("Couldn't add capture filter to graph! hr=0x%x"), hr); pSrcFilter->Release(); return hr; }
// Render the preview pin on the video capture filter // Use this instead of g_pGraph->RenderFile hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter, NULL, NULL); if (FAILED(hr)) { Msg(TEXT("Couldn't render capture stream. ") TEXT("The device may already be in use.\r\n\r\nhr=0x%x"), hr); pSrcFilter->Release(); return hr; }
// Now that the filter has been added to the graph and we have // rendered its stream, we can release this reference to the filter. pSrcFilter->Release();
// Set video window style and position hr = SetupVideoWindow(); if (FAILED(hr)) { Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr); return hr; }
// Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph #ifdef REGISTER_FILTERGRAPH hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister); if (FAILED(hr)) { Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr); g_dwGraphRegister = 0; } #endif
// Start previewing video data hr = g_pMC->Run(); if (FAILED(hr)) { Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr); return hr; }
// Create an enumerator for the video capture devices CComPtr <IEnumMoniker> pClassEnum = NULL;
hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0); if (FAILED(hr)) { Msg(TEXT("Couldn't create class enumerator! hr=0x%x"), hr); return hr; }
// If there are no enumerators for the requested type, then // CreateClassEnumerator will succeed, but pClassEnum will be NULL. if (pClassEnum == NULL) { return E_FAIL; }
// check that the return code is S_OK instead of using SUCCEEDED() macro. if (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched))) { // Bind Moniker to a filter object hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc); if (FAILED(hr)) { Msg(TEXT("Couldn't bind moniker to filter object! hr=0x%x"), hr); return hr; } } else { Msg(TEXT("Unable to access video capture device!")); return E_FAIL; }
// Copy the found filter pointer to the output parameter. // Do NOT Release() the reference, since it will still be used // by the calling function. *ppSrcFilter = pSrc;
return hr; }
HRESULT GetInterfaces(void) { HRESULT hr;
// Create the filter graph hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **) &g_pGraph); if (FAILED(hr)) return hr;
if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) { pROT->Revoke(pdwRegister); pROT->Release(); } }
#endif
HRESULT HandleGraphEvent(void) { LONG evCode, evParam1, evParam2; HRESULT hr=S_OK;
while(SUCCEEDED(g_pME->GetEvent(&evCode, (LONG_PTR *) &evParam1, (LONG_PTR *) &evParam2, 0))) { // // Free event parameters to prevent memory leaks associated with // event parameter data. While this application is not interested // in the received events, applications should always process them. // hr = g_pME->FreeEventParams(evCode, evParam1, evParam2);
// Insert event processing code here, if desired }