020 DirectShow 学习(三) CBaseFilter类源代码解析

Post date: 2015/4/10 上午 02:26:06

1. IAMovieSetup接口定义

IAMovieSetup : public IUnknown

{

public:

// Adds the filter to the registry.

virtual HRESULT STDMETHODCALLTYPE Register( void) = 0;

// Removes the filter from the registry.

virtual HRESULT STDMETHODCALLTYPE Unregister( void) = 0;

};

2. IPersist 接口定义

IPersist : public IUnknown

{

public:

virtual HRESULT STDMETHODCALLTYPE GetClassID(

/* [out] */ CLSID *pClassID) = 0;

};

3. IMediaFilter 接口定义

IMediaFilter : public IPersist

{

public:

// Stops the filter.

virtual HRESULT STDMETHODCALLTYPE Stop( void) = 0;

// Pauses the filter.

virtual HRESULT STDMETHODCALLTYPE Pause( void) = 0;

// Runs the filter.

virtual HRESULT STDMETHODCALLTYPE Run(

REFERENCE_TIME tStart) = 0;

// Retrieves the state of the filter (running, stopped, or paused).

virtual HRESULT STDMETHODCALLTYPE GetState(

/* [in] */ DWORD dwMilliSecsTimeout,

/* [out] */ FILTER_STATE *State) = 0;

// Sets the reference clock for the filter or the filter graph.

virtual HRESULT STDMETHODCALLTYPE SetSyncSource(

/* [in] */ IReferenceClock *pClock) = 0;

// Retrieves the current reference clock.

virtual HRESULT STDMETHODCALLTYPE GetSyncSource(

/* [out] */ IReferenceClock **pClock) = 0;

};

4. IBaseFilter接口定义

IBaseFilter : public IMediaFilter

{

public:

// Enumerates the pins on this filter.

virtual HRESULT STDMETHODCALLTYPE EnumPins(

/* [out] */ IEnumPins **ppEnum) = 0;

// Retrieves the pin with the specified identifier.

virtual HRESULT STDMETHODCALLTYPE FindPin(

/* [string][in] */ LPCWSTR Id,

/* [out] */ IPin **ppPin) = 0;

// Notifies the filter that it has joined or left the filter graph.

virtual HRESULT STDMETHODCALLTYPE QueryFilterInfo(

/* [out] */ FILTER_INFO *pInfo) = 0;

// Retrieves information about the filter.

virtual HRESULT STDMETHODCALLTYPE JoinFilterGraph(

/* [in] */ IFilterGraph *pGraph,

/* [string][in] */ LPCWSTR pName) = 0;

// Retrieves a string containing vendor information.

virtual HRESULT STDMETHODCALLTYPE QueryVendorInfo(

/* [string][out] */ LPWSTR *pVendorInfo) = 0;

};

5. CBaseFilter类 [amfilter.h/amfilter.cpp]

o CBaseFilter类的的具体实现伪代码

派生自 CUnknown, IBaseFilter, IAMovieSetup

友元类: friend class CBasePin;

成员变量部分:

FILTER_STATE m_State; // current state: running, paused

IReferenceClock *m_pClock; // this graph's ref clock

CRefTime m_tStart; // offset from stream time to reference time

CLSID m_clsid; // This filters clsid used for serialization

CCritSec *m_pLock; // Object we use for locking

WCHAR *m_pName; // Full filter name

IFilterGraph *m_pGraph; // Graph we belong to

IMediaEventSink *m_pSink; // Called with notify events

LONG m_PinVersion; // Current pin version

其中,m_pLock必须通过Constructor赋值。其余指针初始化为NULL。

o 继承的接口函数实现

l IPersist接口:

STDMETHODIMP GetClassID(CLSID *pClsID);{ m_clsid }

l IAMovieSetup接口:

STDMETHODIMP Register(); // ask filter to register itself

{

// 通过调用virtual函数GetSetupData得到数据,如果为空返回S_FALSE

// 生成IFilterMapper接口,调用AMovieSetupRegisterFilter完成注册

}

STDMETHODIMP Unregister(); // and unregister itself

l IMediaFilter接口:

STDMETHODIMP GetState(DWORD dwMSecs, FILTER_STATE *State);

{ m_State }

STDMETHODIMP SetSyncSource(IReferenceClock *pClock);

{ pClock->AddRef, m_pClock->Release, m_pClock = pClock;}

STDMETHODIMP GetSyncSource(IReferenceClock **pClock);

{ m_pClock->AddRef, *pCLock = m_pClock; }

// override Stop and Pause so we can activate the pins.

STDMETHODIMP Stop();

{

// 如果当前状态m_State不是State_Stopped,则遍历每个连接的Pin,调用Pin的Inactive

// 如果所有的调用均成功,设置m_State为State_Stopped.

}

STDMETHODIMP Pause();

{

// 如果当前状态m_State是State_Stopped,则遍历每个连接的Pin,调用Pin的Active

// 如果所有调用均成功,设置m_State为State_Paused.

}

// the start parameter is the difference to be added to the

// sample's stream time to get the reference time for its presentation

STDMETHODIMP Run(REFERENCE_TIME tStart);

{

// 首先记住Stream的开始时间 m_tStart = tStart;

// 如果当前状态不是State_Running,则遍历每个连接的Pin,调用Pin的Run

// 如果所有调用均成功,设置m_State为State_Running.

}

l IBaseFilter接口:

// pin enumerator

STDMETHODIMP EnumPins(IEnumPins ** ppEnum);

{

// 通过Helper Class CEnumPins来完成 *ppEnum = new CEnumPins(this,

}

// default behaviour of FindPin assumes pin ids are their names

STDMETHODIMP FindPin(LPCWSTR Id, IPin ** ppPin);

{

// 遍历每个Pin,比较名称。如果找到,则对该Pin AddRef后返回。

}

STDMETHODIMP QueryFilterInfo(FILTER_INFO * pInfo);{ 填充FILTER_INFO }

STDMETHODIMP JoinFilterGraph(IFilterGraph * pGraph, LPCWSTR pName);

{

// 简单的复制m_pGraph和m_pSink(如果输入的pGraph支持IMediaEventSink),但不对其AddRef

// 将输入的pName复制到m_pName

}

// return a Vendor information string. Optional - may return E_NOTIMPL.

// memory returned should be freed using CoTaskMemFree

STDMETHODIMP QueryVendorInfo(LPWSTR* pVendorInfo);{ E_NOTIMPL }

o 新增加的virtual函数

// return the current stream time - ie find out what stream time should be appearing now

virtual HRESULT StreamTime(CRefTime& rtStream);

{

// 首先得到当前Clock的时间 m_pClock->GetTime ((REFERENCE_TIME*)&rtStream)

// 然后矫正偏移 rsStream -= m_tStart

}

// find out the current pin version (used by enumerators)

virtual LONG GetPinVersion();{m_PinVersion }

// you need to supply these to access the pins from the enumerator

virtual int GetPinCount() PURE;

virtual CBasePin *GetPin(int n) PURE;

// (override to return filters setup data)

virtual LPAMOVIESETUP_FILTER GetSetupData(){ return NULL; }

o 其他一些相关函数

// send an event notification to the filter graph if we know about it. returns S_OK if delivered,

// S_FALSE if the filter graph does not sink events, or an error otherwise.

HRESULT NotifyEvent(long EventCode, LONG_PTR EventParam1, LONG_PTR EventParam2);

{

// 如果m_pSink不为空,则处理消息pSink->Notify(EventCode, EventParam1, EventParam2);

// 否则返回E_NOTIMPL

}

// Request reconnect

// pPin is the pin to reconnect, pmt is the type to reconnect with - can be NULL

// Calls ReconnectEx on the filter graph

HRESULT ReconnectPin(IPin *pPin, AM_MEDIA_TYPE const *pmt);

{

// 如果m_pGraph不为空,则通过IFilterGraph/IFilterGraph2接口调用Reconnect/ReconnectEx

// 否则返回E_NOINTERFACE

}