windows mobile平台上实例化Sample Grabber失败
如题,在VS2008中,windows mobile6平台下参考“C:\Program Files\Microsoft DirectX SDK (June 2010)\Samples\C++\DirectShow\Filters\Grabber”的写sample grabber 创建wince下的sample grabber,并根据这篇文章做了修改http://www.codeproject.com/Articles/28790/Creating-Custom-DirectShow-SampleGrabber-Filter-fo?fid=1525670&df=90&mpp=10&noise=1&prof=True&sort=Position&view=None&spc=Relaxed&fr=11#xx0xx
将其加入工程,并在使用sample grabber之前调用
STDAPI DllRegisterServer(void)
{
HRESULT hr = AMovieDllRegisterServer2( TRUE );
return hr;
}
注册成功(hr返回值为S_OK,并且可以在手机注册表中查到对应的Sample Grabber项)。
但是使用hr = CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&pH263Transform );实例化sample grabber的时候失败,并且函数不返回,程序出现data abort的错误。后修改,直接使用构造函数实例化,但是使用IFilterGraph->AddFilter将生成的sample grabber加入filter graph的时候又出现data abort的错误。
部分代码如下,请各位大侠帮我看看吧~~~~~~~谢谢先
samplegrabber.h
typedef void(CALLBACK* MANAGED_SAMPLEPROCESSEDPROC)(BYTE* pData, int width, int height, char** pResult);
typedef void (CALLBACK *MANAGED_INFORMMEDIATYPEPROC)(int height, int width, int size, int stride);
//{b62f694e-0593-4e60-aa1c-16af6496ac39}
DEFINE_GUID(CLSID_SampleGrabber,
0xb62f694e, 0x0593, 0x4e60, 0xaa, 0x1c, 0x16, 0xaf, 0x64, 0x96, 0xac, 0x39);
DEFINE_GUID(IID_ISampleGrabber,
0xbe5b5e, 0xcca0, 0x4a9f, 0xb1, 0x98, 0xdf, 0x2f, 0xac, 0xe8, 0x2d, 0x57);
DECLARE_INTERFACE_(ISampleGrabber, IUnknown)
{
STDMETHOD(RegisterCallback)(MANAGED_SAMPLEPROCESSEDPROC callback) PURE;
};
#pragma once
#include <assert.h>
#include <dshow.h>
#include "SampleGrabberGuid.h"
#define FILTERNAME L"SampleGrabber"
// Media types
const AMOVIESETUP_MEDIATYPE sudPinTypes [] =
{
//{&MEDIATYPE_Video,&MEDIASUBTYPE_RGB565},
//{&MEDIATYPE_Video,&MEDIASUBTYPE_YUY2},
{&MEDIATYPE_Video,&MEDIASUBTYPE_NULL},
};
// Pins
const AMOVIESETUP_PIN sudpPins[] =
{
{
L"VideoInput", // Pin string name
FALSE, // Is it rendered
FALSE, // Is it an output
FALSE, // Allowed none
FALSE, // Likewise many
&CLSID_NULL, // Connects to filter
NULL, // Connects to pin
1, // Number of types
sudPinTypes // Pin information
},
{
L"VideoOutput", // Pin string name
FALSE, // Is it rendered
TRUE, // Is it an output
FALSE, // Allowed none
FALSE, // Likewise many
&CLSID_NULL, // Connects to filter
NULL, // Connects to pin
1, // Number of types
sudPinTypes // Pin information
}
};
// Filters
const AMOVIESETUP_FILTER sudSampleGrabber =
{
&CLSID_SampleGrabber, // Filter CLSID
FILTERNAME, // String name
MERIT_NORMAL, //MERIT_DO_NOT_USE, // Filter merit
2, // Number of pins
sudpPins // Pin information
};
//#if 0
class CSampleGrabberInPin;
class CSampleGrabberAllocator : public CMemAllocator
{
friend class CSampleGrabberInPin;
friend class CSampleGrabber;
protected:
CSampleGrabberInPin* m_pPin;
public:
CSampleGrabberAllocator( CSampleGrabberInPin* pParent, HRESULT* phr )
: CMemAllocator( TEXT("SampleGrabberAllocator\0"), NULL, phr),
m_pPin(pParent)
{
};
~CSampleGrabberAllocator()
{
//wipe out m_pBuffer before we try to delete it. It's not an allocated
// buffer, and the default destructor will try to use it
m_pBuffer = NULL;
}
HRESULT Alloc();
void ReallyFree();
HRESULT SetProperties( ALLOCATOR_PROPERTIES* pRequest, ALLOCATOR_PROPERTIES* pActual );
HRESULT GetAllocatorRequirements( ALLOCATOR_PROPERTIES* pProps );
};
class CSampleGrabberInPin : public CTransInPlaceInputPin
{
friend class CSampleGrabberAllocator;
friend class CSampleGrabber;
CSampleGrabberAllocator * m_pPrivateAllocator;
ALLOCATOR_PROPERTIES m_allocprops;
BYTE * m_pBuffer;
BOOL m_bMediaTypeChanged;
BOOL refCnt;
protected:
CSampleGrabber * SampleGrabber( ) { return (CSampleGrabber*) m_pFilter; }
HRESULT SetDeliveryBuffer( ALLOCATOR_PROPERTIES props, BYTE * m_pBuffer );
public:
STDMETHODIMP_(ULONG) AddRef() { return 2; }
STDMETHODIMP_(ULONG) Release() { return 1; }
CSampleGrabberInPin( CTransInPlaceFilter * pFilter, HRESULT * pHr )
: CTransInPlaceInputPin( TEXT("SampleGrabberInputPin\0"), pFilter, pHr, L"Input\0" )
, m_pPrivateAllocator( NULL )
, m_pBuffer( NULL )
, m_bMediaTypeChanged( FALSE )
,refCnt(0)
{
memset( &m_allocprops, 0, sizeof( m_allocprops ) );
}
~CSampleGrabberInPin( )
{
if( m_pPrivateAllocator ) delete m_pPrivateAllocator;
}
HRESULT GetMediaType( int iPosition, CMediaType *pMediaType );
STDMETHODIMP EnumMediaTypes( IEnumMediaTypes **ppEnum );
STDMETHODIMP NotifyAllocator( IMemAllocator *pAllocator, BOOL bReadOnly );
STDMETHODIMP GetAllocator( IMemAllocator **ppAllocator );
HRESULT SetMediaType( const CMediaType *pmt );
STDMETHODIMP GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps );
};
class CSampleGrabber : public CTransInPlaceFilter, public ISampleGrabber
{
friend class CSampleGrabberAllocator;
friend class CSampleGrabberInPin;
private:
// filter variables
// CMediaType m_mt; //commented on 08/29
AM_MEDIA_TYPE* m_pAM_MEDIA_TYPE;
// callbacks
MANAGED_SAMPLEPROCESSEDPROC m_Callback;
public:
// video format
long m_SampleSize;
long m_Width;
long m_Height;
long m_Stride;
//added on 08/29
CMediaType m_mt;
char pProcessResult[1024];
VIDEOINFOHEADER m_VihIn; // Holds the current video format (input)
VIDEOINFOHEADER m_VihOut; // Holds the current video format (output)
public:
// IUnknown
//DECLARE_IUNKNOWN;
STDMETHOD (QueryInterface) (REFIID riid, LPVOID *ppv);
STDMETHOD_(ULONG, AddRef) (void);
STDMETHOD_(ULONG, Release) (void);
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
// instantiation
CSampleGrabber( IUnknown * pOuter, HRESULT * phr, BOOL ModifiesData );
~CSampleGrabber();
static CUnknown *WINAPI CreateInstance(LPUNKNOWN punk, HRESULT *phr);
// CTransInPlaceFilter
HRESULT CheckInputType(const CMediaType *pmt);
HRESULT SetMediaType(PIN_DIRECTION direction, const CMediaType *pmt);
STDMETHODIMP Pause(void);
STDMETHODIMP Stop(void);
HRESULT Transform(IMediaSample *pMediaSample);
HRESULT CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut) {
return NOERROR;
}
HRESULT DecideBufferSize(IMemAllocator *pAlloc,
ALLOCATOR_PROPERTIES *pProperties);
HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
STDMETHODIMP RegisterCallback(MANAGED_SAMPLEPROCESSEDPROC callback);
STDMETHODIMP_(ULONG) NonDelegatingRelease();
LPAMOVIESETUP_FILTER GetSetupData();
HRESULT Receive( IMediaSample * pms );
HRESULT SetDeliveryBuffer( ALLOCATOR_PROPERTIES props, BYTE* pBuffer );
protected:
ULONG m_cRef;
};
调用代码如下:
hr = CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&pH263Transform );// Create and initialize the frame grabber filter,这句代码死活运行不成功,也不返回
CHK( m_pFilterGraph->AddFilter( pH263Transform, FILTERNAME ) );
CHK( pH263Transform->QueryInterface( IID_ISampleGrabber, (void**)&m_pISampleGrabber ) );
报错代码出现在CoCreateInstance,错误如下:Data Abort: Thread=82876a40 Proc=80572e00 'XXXXXX.exe'
AKY=00100001 PC=0143f78c(cameracapturedlld.dll+0x0000f78c) RA=0143c180(cameracapturedlld.dll+0x0000c180) BVA=2a000004 FSR=00000007
后我改使用构造函数实例化sample grabber。相关代码如下
CMediaType VideoType;
ZeroMemory( &VideoType, sizeof(VideoType));
VideoType.SetType(&MEDIATYPE_Video);
//VideoType.SetSubtype(&MEDIASUBTYPE_RGB24);
//set up a partially specified media type
HDC hdc = GetDC(NULL);
int iBitDepth = GetDeviceCaps( hdc, BITSPIXEL);
switch (iBitDepth)
{
case 8:
VideoType.subtype = MEDIASUBTYPE_RGB8;
break;
case 16:
VideoType.subtype = MEDIASUBTYPE_RGB555;
break;
case 24:
VideoType.subtype = MEDIASUBTYPE_RGB24;
break;
case 32:
VideoType.subtype = MEDIASUBTYPE_RGB32;
break;
default:
OutputDebugString(L"get the subtype the media type FAILED\n");
break;
}
VideoType.SetSubtype(&(VideoType.subtype));
VideoType.SetFormatType(&FORMAT_VideoInfo);
ALLOCATOR_PROPERTIES props;
props.cBuffers = 1;
props.cbBuffer = 320*240*3;
props.cbAlign = 1;
props.cbPrefix = 0;
BYTE* pBuffer = new BYTE[320*240*3];
memset(pBuffer, 0, 320*240*3);
VideoType.cbFormat = 320*240*3;
VideoType.pbFormat = pBuffer;
。。。。。。。。。。。
CSampleGrabber* m_cSampleGrabber;
m_cSampleGrabber = new CSampleGrabber(NULL, &hr, FALSE);
if ( m_cSampleGrabber == NULL )
{
OutputDebugString(L"Construct CSampleGrabber FAILED\n");
}
hr = m_cSampleGrabber->QueryInterface(IID_IBaseFilter, (void**)&pH263Transform);
//hr = m_cSampleGrabber->SetDeliveryBuffer(props, pBuffer);
//memset(pBuffer, 0, 320*240*3);这两句加上也没用
hr = m_cSampleGrabber->SetMediaType( PINDIR_INPUT, &VideoType );
hr = m_pFilterGraph->AddFilter( pH263Transform, FILTERNAME);//这句失败,返回同样的错误,data abort
hr = m_cSampleGrabber->QueryInterface( IID_ISampleGrabber, (void**)&m_pISampleGrabber);
。。。。。。。。。。
求教各位大神,这个问题还怎么解决?为啥我写的sample grabber在注册后,仍然不能通过CoCreateInstance实例化?或者通过构造函数实例化后,仍然不能通过IFilterGraph->AddFilter加入filter graph去,也不能通过RenderStream实现智能连接(即使把引脚pin也注册了之后)??
代码在http://download.csdn.net/detail/liptonalice/6204285
麻烦各位大侠了~~~~~~