DirectShow中,数据流(Data Flow)都是依次流过各个Filter的.它对数据的管理也有自己的方法,而且并没有向用户提供一个统一的接口,供用户操作数据流.这里以提取视频采集在的每帧为位图数据为例,说说如何在Directshow中提取数据.
这里我们用到了DirectShow提供给我们的接口ISampleGrabber,并定义了一个供它回调的CSampleGrabberCB对象(继承ISampleGrabberCB接口).
我们知道,DirectShow中的数据存储是通过Sample完成的,所以提取数据也需要通过SampleGrabber.
步骤如下:
1.建立CSampleGrabberCB对象.
class CSampleGrabberCB : public ISampleGrabberCB
{
STDMETHODIMP BufferCB( double dblSampleTime, BYTE * pBuffer, long lBufferSize )
{
//Callback method that receives a pointer to the sample buffer.
}
STDMETHODIMP SampleCB( double SampleTime, IMediaSample * pSample )
{
//Callback method that receives a pointer to the media sample.
}
}
2.定义ISampleGrabber接口并初始化
CComPtr< ISampleGrabber > m_pGrabber;
HRESULT hr;
hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber );
if(FAILED(hr))
//error action;
3.定义Grabber Filter,设置它的媒体类型,并将它加入Graph中
CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber );
CMediaType VideoType;
VideoType.SetType(&MEDIATYPE_Video);
VideoType.SetSubtype(&MEDIASUBTYPE_RGB24);
hr = m_pGrabber->SetMediaType(&VideoType);
hr = pGraph->AddFilter(pGrabBase,L"Grabber");
4.设置回调(CallBack),使Grabber能够通过BufferCB自动完成采集数据.
// don‘t buffer the samples as they pass through
//
hr = m_pGrabber->SetBufferSamples( FALSE );
// only grab one at a time, stop stream after
// grabbing one sample
//
hr = m_pGrabber->SetOneShot( FALSE );
// set the callback, so we can grab the one sample
//
hr = m_pGrabber->SetCallback( &mCB, 1 ); //mCB为CSampleGrabber对象
这样,在DirectShow数据流动过程中,mCB.bufferCB会自动执行,提取Graph中的数据.