The class im using directly implements ISampleGrabberCB, so no inheritance needed.
Code:
extern BYTE DrawBuffer[];
extern AM_MEDIA_TYPE mt;
#ifndef CALLBACK_CLASS
#define CALLBACK_CLASS
// Class to hold the callback function for the Sample Grabber filter.
class SampleGrabberCallback : public ISampleGrabberCB {
public:
// Fake referance counting.
STDMETHODIMP_(ULONG) AddRef() { return 1; }
STDMETHODIMP_(ULONG) Release() { return 2; }
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject){
if (NULL == ppvObject) return E_POINTER;
if (riid == __uuidof(IUnknown)){
*ppvObject = static_cast<IUnknown*>(this);
return S_OK;
}
if (riid == __uuidof(ISampleGrabberCB)){
*ppvObject = static_cast<ISampleGrabberCB*>(this);
return S_OK;
}
return E_NOTIMPL;
}
STDMETHODIMP SampleCB(double Time, IMediaSample *pSample){
BYTE* pBuffer;
DWORD dwLength;
dwLength = pSample->GetActualDataLength();
pSample->GetPointer(&pBuffer);
if(pSample->IsPreroll() == S_OK){
return S_OK;
}
CopyMemory(&DrawBuffer[54] , pBuffer , dwLength);
return S_OK;
}
STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen){
if ((mt.majortype != MEDIATYPE_Video) || (mt.formattype != FORMAT_VideoInfo) || (mt.cbFormat < sizeof(VIDEOINFOHEADER)) || (mt.pbFormat == NULL)){
return VFW_E_INVALIDMEDIATYPE;
}
HANDLE hf = CreateFile("C:\\Example.bmp", GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
if (hf == INVALID_HANDLE_VALUE){
return E_FAIL;
}
long cbBitmapInfoSize = mt.cbFormat - SIZE_PREHEADER;
VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat;
BITMAPFILEHEADER bfh;
ZeroMemory(&bfh, sizeof(bfh));
bfh.bfType = 'MB'; // Little-endian for "MB".
bfh.bfSize = sizeof( bfh ) + BufferLen + cbBitmapInfoSize;
bfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + cbBitmapInfoSize;
// Write the file header.
DWORD dwWritten = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &dwWritten, NULL );
WriteFile(hf, HEADER(pVideoHeader), cbBitmapInfoSize, &dwWritten, NULL);
WriteFile( hf, pBuffer, BufferLen, &dwWritten, NULL );
CloseHandle( hf );
return S_OK;
}
};
#endif