DirectShow 预览时抓数据的问题
我现在用directshow在手机上作拍摄预览功能,为了能够在预览的画面上画东西我需要捕获每桢数据.我下了个demo,它里面实现了ISampleGrabber,能提供抓桢的功能。我现在的问题是无法建立流链接,在调用renderstream函数时,程序出错退出。以下是代码,请高手分析一下:
HRESULT
CGraphManager::CreateCaptureGraphInternal()
{
FILE* pFile = _tfopen(TEXT("ds_log.txt"), TEXT("a+w"));
HRESULT hr = S_OK;
CComVariant varCamName;
CPropertyBag PropBag;
OAEVENT oaEvent;
WCHARwzDeviceName[ MAX_PATH + 1 ];
CComPtr<IMediaEvent> pMediaEvent;
CComPtr<IPersistPropertyBag> pPropertyBag;
CComPtr<IBaseFilter>pH263Transform;
//
// Create the capture graph builder and register the filtergraph manager.
//
CHK( m_pCaptureGraphBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder ));
CHK( m_pFilterGraph.CoCreateInstance( CLSID_FilterGraph ));
CHK( m_pCaptureGraphBuilder->SetFiltergraph( m_pFilterGraph ));
// Get IMediaControl interface
CHK( m_pFilterGraph->QueryInterface( IID_IMediaControl, (void**)&m_pMediaControl ) );
fprintf(pFile, "QueryInterface m_pMediaControl\n");
//
// Create and initialize the video capture filter
//
CHK( m_pVideoCaptureFilter.CoCreateInstance( CLSID_VideoCapture ));
CHK( m_pVideoCaptureFilter.QueryInterface( &pPropertyBag ));
// We are loading the driver CAM1 in the video capture filter.
CHK( GetFirstCameraDriver( wzDeviceName ));
varCamName = wzDeviceName;
if( varCamName.vt != VT_BSTR )
{
ERR( E_OUTOFMEMORY );
}
CHK( PropBag.Write( L"VCapName", &varCamName ));
CHK( pPropertyBag->Load( &PropBag, NULL ));
// Everything succeeded, the video capture filter is added to the filtergraph
CHK( m_pFilterGraph->AddFilter( m_pVideoCaptureFilter, L"Video Capture Filter Source" ));
//
// Create and initialize the frame grabber filter
//
CHK( CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&pH263Transform ) );
CHK( m_pFilterGraph->AddFilter( pH263Transform, FILTERNAME ) );
CHK( pH263Transform->QueryInterface( IID_ISampleGrabber, (void**)&m_pISampleGrabber ) );
// render stream
fprintf(pFile, "before RenderStream\n");
CHK( m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_PREVIEW,
&MEDIATYPE_Video, m_pVideoCaptureFilter, pH263Transform, NULL ) );
fprintf(pFile, "after RenderStream\n");
//
// Let's get the handle for DShow events. The main loop will listen to both notifications from
// the UI thread and for DShow notifications
//
CHK( m_pFilterGraph->QueryInterface( IID_IMediaEvent, (void**) &pMediaEvent ));
CHK( pMediaEvent->GetEventHandle( &oaEvent ));
m_handle[1] = (HANDLE) oaEvent;
m_fGraphBuilt = TRUE;
NotifyMessage( MESSAGE_CREATE_FRAMECAPTUREGRAPH_OK, L"Framegraph created" );
return hr;
Cleanup:
if( FAILED( hr ))
{
NotifyMessage( MESSAGE_ERROR, L"Builing the graph failed" );
}
fprintf(pFile, "\n");
fclose(pFile);
return hr;
}
[解决办法]
CHK( m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_PREVIEW,
&MEDIATYPE_Video, m_pVideoCaptureFilter, pH263Transform, NULL ) );
这个要改成
CHK( m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video, m_pVideoCaptureFilter, pH263Transform, NULL ) );
它会自动加smartTee分流
[解决办法]