看了DXSDK\Samples\C++\DirectShow\Editing\GrabBitmaps\的运行过程
把程序的枝叶去掉,看看程序主要是如何一下运行顺序,这样有利于明白其中的运行顺序,也好与看过它的朋友有个交流,其中的注释可能用得不对或不恰当,请指教或留言
声明对象:
CComPtr< ISampleGrabber > pGrabber;
CComPtr< IBaseFilter > pSource;
CComPtr< IGraphBuilder > pGraph;
CComPtr< IVideoWindow > pVideoWindow;
创建对象:
pGrabber.CoCreateInstance( CLSID_SampleGrabber );
CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabberBase( pGrabber );
pSource.CoCreateInstance( CLSID_AsyncReader );
pGraph.CoCreateInstance( CLSID_FilterGraph );
加入过滤器:
hr = pGraph->AddFilter( pSource, L"Source" );
hr = pGraph->AddFilter( pGrabberBase, L"Grabber" );
加载数据源:
CComQIPtr< IFileSourceFilter, &IID_IFileSourceFilter > pLoad( pSource );
hr = pLoad->Load( T2W( szFile ), NULL );
设置媒体类型:
CMediaType GrabType;
GrabType.SetType( &MEDIATYPE_Video );
GrabType.SetSubtype( &MEDIASUBTYPE_RGB24 );
hr = pGrabber->SetMediaType( &GrabType );
得到输出输入Pin
CComPtr< IPin > pSourcePin;
CComPtr< IPin > pGrabPin;
pSourcePin = GetOutPin( pSource, 0 );
pGrabPin = GetInPin( pGrabberBase, 0 );
连结输出输入Pin:
hr = pGraph->Connect( pSourcePin, pGrabPin );
CSampleGrabberCB CB;
得到源音频的长宽
AM_MEDIA_TYPE mt;
hr = pGrabber->GetConnectedMediaType( &mt );
VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;
CB.Width = vih->bmiHeader.biWidth;
CB.Height = vih->bmiHeader.biHeight;
FreeMediaType( mt );
得到输出Pin,以video renderer之用
CComPtr pGrabOutPin = GetOutPin( pGrabberBase, 0 );
hr = pGraph->Render( pGrabOutPin );
抓取设置
hr = pGrabber->SetBufferSamples( FALSE );
hr = pGrabber->SetOneShot( TRUE );
hr = pGrabber->SetCallback( &CB, 1 );
CComQIPtr< IMediaSeeking, &IID_IMediaSeeking > pSeeking( pGraph );
关于窗口:
CComQIPtr< IVideoWindow, &IID_IVideoWindow > pWindow = pGraph;
if (pWindow)
{
hr = pWindow->put_AutoShow(OAFALSE);
}
输出图片:
for( int i = 0 ; i < NUM_FRAMES_TO_GRAB ; i++ )
{
REFERENCE_TIME Start = i * UNITS;
输出位置
hr = pSeeking->SetPositions( &Start, AM_SEEKING_AbsolutePositioning,
NULL, AM_SEEKING_NoPositioning );
CComQIPtr< IMediaControl, &IID_IMediaControl > pControl( pGraph );
运行
hr = pControl->Run( );
消息处理
CComQIPtr< IMediaEvent, &IID_IMediaEvent > pEvent( pGraph );
long EvCode = 0;
hr = pEvent->WaitForCompletion( INFINITE, &EvCode );
}
阅读(2321) | 评论(0) | 转发(0) |