BuildGraph.cpp
资源名称:PCBMatch.rar [点击查看]
上传用户:bxq2008bxq
上传日期:2022-07-18
资源大小:6138k
文件大小:7k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "BuildGraph.h"
- CBuildGraph::CBuildGraph(void)
- :pGraph(NULL)
- ,pGrabber(NULL)
- ,pControl(NULL)
- ,pEvent(NULL)
- ,pF(NULL)
- ,pNull(NULL)
- ,cbBuffer(0)
- {
- }
- CBuildGraph::~CBuildGraph(void)
- {
- if(pControl)
- pControl->Stop();
- if(pGraph)
- pGraph->Release();
- if(pGrabber)
- pGrabber->Release();
- if(pControl)
- pControl->Release();
- if(pEvent)
- pEvent->Release();
- if(pF)
- pF->Release();
- if(pNull)
- pNull->Release();
- }
- HRESULT CBuildGraph::GetUnconnectedPin(
- IBaseFilter *pFilter, // Pointer to the filter.
- PIN_DIRECTION PinDir, // Direction of the pin to find.
- IPin **ppPin) // Receives a pointer to the pin.
- {
- *ppPin = 0;
- IEnumPins *pEnum = 0;
- IPin *pPin = 0;
- HRESULT hr = pFilter->EnumPins(&pEnum);
- if (FAILED(hr))
- {
- return hr;
- }
- while (pEnum->Next(1, &pPin, NULL) == S_OK)
- {
- PIN_DIRECTION ThisPinDir;
- pPin->QueryDirection(&ThisPinDir);
- if (ThisPinDir == PinDir)
- {
- IPin *pTmp = 0;
- hr = pPin->ConnectedTo(&pTmp);
- if (SUCCEEDED(hr)) // Already connected, not the pin we want.
- {
- pTmp->Release();
- }
- else // Unconnected, this is the pin we want.
- {
- pEnum->Release();
- *ppPin = pPin;
- return S_OK;
- }
- }
- pPin->Release();
- }
- pEnum->Release();
- // Did not find a matching pin.
- return E_FAIL;
- }
- HRESULT CBuildGraph::GetCapturePin(
- IBaseFilter *pFilter, // Pointer to the filter.
- PIN_DIRECTION PinDir, // Direction of the pin to find.
- IPin **ppPin) // Receives a pointer to the pin.
- {
- *ppPin = 0;
- IEnumPins *pEnum = 0;
- IPin *pPin = 0;
- HRESULT hr = pFilter->EnumPins(&pEnum);
- if (FAILED(hr))
- {
- return hr;
- }
- while (pEnum->Next(1, &pPin, NULL) == S_OK)
- {
- PIN_DIRECTION ThisPinDir;
- pPin->QueryDirection(&ThisPinDir);
- if (ThisPinDir == PinDir)
- {
- IPin *pTmp = 0;
- hr = pPin->ConnectedTo(&pTmp);
- if (SUCCEEDED(hr)) // Already connected, not the pin we want.
- {
- pTmp->Release();
- }
- else // Unconnected, this is the pin we want.
- {
- pEnum->Release();
- GUID TempGuid;
- hr = GetPinCategory(pPin,&TempGuid);
- if(hr == S_OK)
- {
- if(TempGuid == PIN_CATEGORY_CAPTURE)
- {
- *ppPin = pPin;
- return S_OK;
- }
- }
- }
- }
- pPin->Release();
- }
- pEnum->Release();
- // Did not find a matching pin.
- return E_FAIL;
- }
- HRESULT CBuildGraph::ConnectFilters(
- IGraphBuilder *pGraph, // Filter Graph Manager.
- IPin *pOut, // Output pin on the upstream filter.
- IBaseFilter *pDest) // Downstream filter.
- {
- if ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL))
- {
- return E_POINTER;
- }
- #ifdef debug
- PIN_DIRECTION PinDir;
- pOut->QueryDirection(&PinDir);
- _ASSERTE(PinDir == PINDIR_OUTPUT);
- #endif
- // Find an input pin on the downstream filter.
- IPin *pIn = 0;
- HRESULT hr = GetUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
- if (FAILED(hr))
- {
- return hr;
- }
- // Try to connect them.
- hr = pGraph->Connect(pOut, pIn);
- pIn->Release();
- return hr;
- }
- HRESULT CBuildGraph::ConnectFilters(
- IGraphBuilder *pGraph,
- IBaseFilter *pSrc,
- IBaseFilter *pDest)
- {
- if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
- {
- return E_POINTER;
- }
- // Find an output pin on the first filter.
- IPin *pOut = 0;
- HRESULT hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
- if (FAILED(hr))
- {
- return hr;
- }
- hr = ConnectFilters(pGraph, pOut, pDest);
- pOut->Release();
- return hr;
- }
- HRESULT CBuildGraph::ConnectCaptureFilters(
- IGraphBuilder *pGraph,
- IBaseFilter *pSrc,
- IBaseFilter *pDest)
- {
- if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
- {
- return E_POINTER;
- }
- // Find an output pin on the first filter.
- IPin *pOut = 0;
- HRESULT hr = GetCapturePin(pSrc, PINDIR_OUTPUT, &pOut);
- if (FAILED(hr))
- {
- return hr;
- }
- hr = ConnectFilters(pGraph, pOut, pDest);
- pOut->Release();
- return hr;
- }
- HRESULT CBuildGraph::GetPinCategory(IPin *pPin, GUID *pPinCategory)
- {
- HRESULT hr;
- IKsPropertySet *pKs;
- hr = pPin->QueryInterface(IID_IKsPropertySet, (void **)&pKs);
- if (FAILED(hr))
- {
- // The pin does not support IKsPropertySet.
- return hr;
- }
- // Try to retrieve the pin category.
- DWORD cbReturned;
- hr = pKs->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
- pPinCategory, sizeof(GUID), &cbReturned);
- // If this succeeded, pPinCategory now contains the category GUID.
- pKs->Release();
- return hr;
- }
- HRESULT CBuildGraph::InitCaptureVideo(IBaseFilter *pSrc)
- {
- HRESULT hr;
- hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,IID_IGraphBuilder, (void **) &pGraph);
- hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
- IID_IBaseFilter, (void **)&pF);
- hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
- IID_IBaseFilter, reinterpret_cast<void**>(&pNull));
- hr = pF->QueryInterface(IID_ISampleGrabber,(void **)&pGrabber);
- hr = pGraph->QueryInterface(IID_IMediaControl,(LPVOID *)&pControl);
- hr = pGraph->QueryInterface(IID_IMediaEvent,(LPVOID *)&pEvent);
- hr = pGraph->AddFilter(pF, L"SampleGrabber");
- hr = pGraph->AddFilter(pSrc, L"Video Capture");
- hr = pGraph->AddFilter(pNull, L"NullRenderer");
- AM_MEDIA_TYPE pmt;
- ZeroMemory(&pmt, sizeof(AM_MEDIA_TYPE));
- pmt.majortype = MEDIATYPE_Video;
- pmt.subtype = MEDIASUBTYPE_RGB24;
- hr = pGrabber->SetMediaType(&pmt);
- hr = ConnectCaptureFilters(pGraph,pSrc, pF);
- hr = ConnectFilters(pGraph, pF, pNull);
- pControl->Run();
- hr = pGrabber->SetOneShot(TRUE);
- hr = pGrabber->SetBufferSamples(TRUE);
- long evCode;
- hr = pEvent->WaitForCompletion(INFINITE, &evCode); // Wait till it's done.
- hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);
- hr = pGrabber->SetOneShot(FALSE);
- OAFilterState pfs;
- hr = pControl->Stop();
- hr = pControl->GetState(INFINITE, &pfs);
- hr = pControl->Run();
- hr = pControl->GetState(INFINITE, &pfs);
- if(pfs == State_Running&&hr == S_OK)
- {
- hr = S_OK;
- }
- else
- {
- hr = E_FAIL;
- }
- return hr;
- }