- BaseClasses中实现COM的部分源代码分析[combase.h/combase.cpp]
- class CBaseObject是BaseClasses中的基类,它只维护一个m_cObjects的计数信息。同时它只支持输入名称的Constructor。
- CUnknown是实现了COM的基类,它从INonDelegationUnknown接口继承支持Aggregation。同时它使用Member作为Delegation Unknown接口:
const LPUNKNOWN m_pUnknown;
同时它维护了COM的Reference Count - volatile LONG m_cRef;
它的Constructor为:
CUnknown(const TCHAR *pName, LPUNKNOWN pUnk);
CUnknown(TCHAR *pName, LPUNKNOWN pUnk,HRESULT *phr);
当然Unicode版本还要多两个支持CHAR类型的Constructor. - 类厂模板定义(真正写DirectShow可能用不到该模板,主要是它自己的实现,下面将会见到):
class CFactoryTemplate {
public:
const WCHAR * m_Name; // 名称
const CLSID * m_ClsID; // CLSID
LPFNNewCOMObject m_lpfnNew; // 真正的Create函数
LPFNInitRoutine m_lpfnInit;
const AMOVIESETUP_FILTER * m_pAMovieSetup_Filter;
BOOL IsClassID(REFCLSID rclsid) const {return (IsEqualCLSID(*m_ClsID,rclsid));}
CUnknown *CreateInstance(LPUNKNOWN pUnk, HRESULT *phr) const
{
CheckPointer(phr,NULL);
return m_lpfnNew(pUnk, phr);
};
其中LPFNNewCOMObject和LPFNInitRoutine的宏定义如下:
typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(LPUNKNOWN pUnkOuter, HRESULT *phr);
/* A function (can be NULL) which is called from the DLL entrypoint routine for each factory template:
bLoading - TRUE on DLL load, FALSE on DLL unload
rclsid - the m_ClsID of the entry*/
typedef void (CALLBACK *LPFNInitRoutine)(BOOL bLoading, const CLSID *rclsid);
AMOVIESETUP_FILTER的定义稍微复杂一点:
// Pin上的Media type
typedef REGPINTYPES
AMOVIESETUP_MEDIATYPE, * PAMOVIESETUP_MEDIATYPE, * FAR LPAMOVIESETUP_MEDIATYPE;
typedef REGFILTERPINS
AMOVIESETUP_PIN, * PAMOVIESETUP_PIN, * FAR LPAMOVIESETUP_PIN;
// 真正的Filter结构定义
typedef struct _AMOVIESETUP_FILTER {
const CLSID * clsID;
const WCHAR * strName;
DWORD dwMerit;
UINT nPins;
const AMOVIESETUP_PIN * lpPin;
} AMOVIESETUP_FILTER, * PAMOVIESETUP_FILTER, * FAR LPAMOVIESETUP_FILTER; - 实现Delegation Unknown接口的宏:
#define DECLARE_IUNKNOWN
STDMETHODIMP QueryInterface(REFIID riid, void **ppv) {
return GetOwner()->QueryInterface(riid,ppv);
};
STDMETHODIMP_(ULONG) AddRef() {
return GetOwner()->AddRef();
};
STDMETHODIMP_(ULONG) Release() {
return GetOwner()->Release();
};
- DirectShow Filter实现COM自注册机理[dllsetup.h/dllsetup.cpp/dllentry.cpp]
- 作为自注册的COM组件,必须支持DllRegisterServer和DllUnregisterServer两个导出函数。而基类的实现中只是简单调用了AMovieDllRegisterServer2函数。
- AMovieDllRegisterServer2函数的伪代码,其参数Flag控制了Register和Unregister的切换。
{
// 获取当前Filter自身的名称,使用了全局变量g_hInit
// 调用RegisterAllServers(Filter的名称,Flag) 注册所有 OLE servers
// 通过取得IFilterMapper2或者IFilterMapper接口来调用AMovieSetupRegisterFilter2或者
// AMovieSetupRegisterFilter完成注册,注意这里使用了全局变量(必须由Filter实现)
// extern int g_cTemplates;
// extern CFactoryTemplate g_Templates[];
其中RegisterAllServers将调用AMovieSetupRegisterServer或者AMovieSetupUnregisterServer。这两个函数负责该Filter 注册为标准的COM组件或者注销,而AMovieSetupRegisterFilter2则把Filter注册在DirectShow的Category下,该Category支持Filter的Enum等操作。
- DirectShow中常用结构定义[strmif.h]
- AM_MEDIA_TYPE以及PIN_DIRECTION结构定义
typedef struct _AMMediaType
{
GUID majortype;// 主类型
GUID subtype;// 子类型
BOOL bFixedSizeSamples;
BOOL bTemporalCompression;
ULONG lSampleSize;
GUID formattype;
IUnknown *pUnk;
ULONG cbFormat;
/* [size_is] */ BYTE *pbFormat;
} AM_MEDIA_TYPE;
typedef enum _PinDirection
{ PINDIR_INPUT = 0,
PINDIR_OUTPUT = PINDI
- AM_MEDIA_TYPE以及PIN_DIRECTION结构定义