我正在使用DirectShow来捕获视频。我正在将相机连接到样品采集器并保存我得到的图像。
在以下代码中,我将 IID_ICaptureGraphBuilder2 连接到连接到样本采集器( ISampleGrabber )的捕获设备( CLSID_VideoCaptureSources )。 该图表已连接到 IID_IMediaControl 。 我使用IID_IMediaControl运行和停止。大多数时候停止卡住了。 有某种僵局。我尝试添加 IID_IMediaEvent 和m_pEvent-> WaitForCompletion(INFINITE,& evCode);但它仍然无效。 暂停工作没有问题,但是在试图阻止软件时就卡住了
构建图表
ICaptureGraphBuilder2 *pBuilder;
IBaseFilter *pCamera;
IPin *pOutPin;
IPin *pInPin;
IBaseFilter *pSampleGrabberFilter;
IBaseFilter *pNullRendererFilter;
ISampleGrabber *pSampleGrabber;
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGraph);
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (LPVOID*)&pBuilder);
CHECK_HR(hr, L"Can't create Capture Graph Builder");
hr = pBuilder->SetFiltergraph(m_pGraph);
CHECK_HR(hr, L"Can't SetFiltergraph");
pCamera = CreateFilterByName(pCaptureDeviceName, CLSID_VideoCaptureSources);
WCHAR err[256];
wsprintf(err, L"Can't add Camera '%s' to graph", pCaptureDeviceName);
hr = m_pGraph->AddFilter(pCamera , pCaptureDeviceName);
CHECK_HR(hr, err);
WCHAR filterName[256];
pOutPin = GetPinCapture(pCamera, L"Capture", i);
if (!pOutPin)
continue;
IAMStreamConfig *pConfig = NULL;
hr = pOutPin->QueryInterface(IID_IAMStreamConfig, (void**)&pConfig);
CHECK_HR(hr, L"Can't get configuration");
AM_MEDIA_TYPE *pmt = NULL;
pConfig->GetFormat(&pmt);
VIDEOINFOHEADER *pFrmt = (VIDEOINFOHEADER *)pmt->pbFormat;
pFrmt->bmiHeader.biWidth = 1920;
pFrmt->bmiHeader.biHeight = 1080;
pConfig->SetFormat(pmt);
SAFE_RELEASE(pConfig);
SAFE_RELEASE(pOutPin);
// Create a sample grabber
wsprintf(filterName, L"Sample Grabber %d", i);
// Create a sample grabber
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pSampleGrabberFilter);
CHECK_HR(hr, L"Unable to create sample grabber filter");
// Initialize sample grabber
hr = pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (void **)&pSampleGrabber);
CHECK_HR(hr, L"Unable to get sample grabber");
hr = pSampleGrabber->SetMediaType(pmt);
CHECK_HR(hr, L"Unable to set media type");
hr = pSampleGrabber->SetBufferSamples(false);
CHECK_HR(hr, L"Unable to set buffer samples!");
hr = pSampleGrabber->SetOneShot(false);
CHECK_HR(hr, L"Unable to set one shot!");
// Add the sample grabber to the graph
hr = m_pGraph->AddFilter(pSampleGrabberFilter, filterName);
CHECK_HR(hr, L"Unable to add sample grabber to graph");
pOutPin = GetPinCapture(pCamera, L"Capture", i);
pInPin = GetPin(pSampleGrabberFilter, PINDIR_INPUT);
hr = m_pGraph->ConnectDirect(pOutPin, pInPin, 0);
CHECK_HR(hr, L"Unable to connect Camera to pSampleGrabberFilter");
SAFE_RELEASE(pOutPin);
SAFE_RELEASE(pInPin);
wsprintf(filterName, L"Null Renderer %d", i);
// Create a null renderer
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,(void **)&pNullRendererFilter);
CHECK_HR(hr, L"Unable to create null renderer filter");
hr = m_pGraph->AddFilter(pNullRendererFilter, filterName);
CHECK_HR(hr, L"Unable to add null renderer to graph");
pOutPin = GetPin(pSampleGrabberFilter, PINDIR_OUTPUT);
pInPin = GetPin(pNullRendererFilter, PINDIR_INPUT);
hr = m_pGraph->ConnectDirect(pOutPin, pInPin, 0);
SAFE_RELEASE(pOutPin);
SAFE_RELEASE(pInPin);
pFrmt = ((VIDEOINFOHEADER *)pmt->pbFormat);
// Initialize the capture grabber
m_pCapGrabber[i] = new CapGrabber(i);
m_pCapGrabber[i]->SetVideoInfoHeader(pFrmt);
m_pCapGrabber[i]->SetAttachGrabberCB(m_funcAttachGrabber);
m_pCapGrabber[i]->SetWidth((int)pFrmt->bmiHeader.biWidth);
m_pCapGrabber[i]->SetHeight((int)pFrmt->bmiHeader.biHeight);
// Set the capture callback
hr = pSampleGrabber->SetCallback(m_pCapGrabber[i], 1);
SAFE_RELEASE(pSampleGrabberFilter);
SAFE_RELEASE(pSampleGrabber);
SAFE_RELEASE(pNullRendererFilter);
答案 0 :(得分:0)
问题很典型,但如果没有您需要了解的其他详细信息,则需要进行猜测。很容易启动多线程操作,但是当它停止它时,需要同步线程并且不准确这样做是造成死锁的典型原因 - 你所看到的。
要解决此问题,通常会将调试程序附加到相关进程并检查其调用堆栈。您将看到一个线程在呼叫中深处呼叫停止和睡眠,等待其他事情发生。可能还有另一个线程正在做一些可疑的事情。
它们上的线程,调用堆栈和模块表明错误并将问题隔离到特定的过滤器或库。更重要的是,您可能希望开始缩小图表,暂时删除过滤器,直到您看到冻结消失为止,并确定可疑过滤器。
答案 1 :(得分:0)
有时最好在停止图表之前暂停图表并等待状态更改。 另请注意,根据msdn,stop不会将图形位置重置为开头。
http://msdn.microsoft.com/en-us/library/windows/desktop/dd390178(v=vs.85).aspx
所以要确保图表在停止后到达开头。我建议使用SetPosition接口。下面是我用来停止图表的一小段代码。我假设你有有效的媒体控制和媒体搜索界面。
IMediaControl *m_pControl = NULL;
IMediaSeeking *m_pMediaSeek = NULL;
//Assuming that you have valid media control interface and media seeking interface using QueryInterface
long long m_pStart = 0;
m_pControl->Pause();
m_pControl->GetState(1000, NULL);
m_pMediaSeek->SetPositions(&m_pStart, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
m_pControl->Run();
m_pControl->GetState(1000,NULL);
m_pControl->Stop();
m_pControl->GetState(1000,NULL);