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

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,调用PinInactive
// 如果所有的调用均成功,设置m_StateState_Stopped.
}
STDMETHODIMP Pause();
{
//
如果当前状态m_StateState_Stopped,则遍历每个连接的Pin,调用PinActive
//
如果所有调用均成功,设置m_StateState_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,调用PinRun
//
如果所有调用均成功,设置m_StateState_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_pGraphm_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
}

 


前一篇:(二)CBasePin类、CBaseOutputPin类和CBaseInputPin类源代码解析

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
//指定视频采集设备的友好名字,为它创建一个Filter IBaseFilter * CTestPreviewDlg::CreateVideoDevice(const char * inFriendlyName) { return CreateHardwareFilter(CLSID_VideoInputDeviceCategory,inFriendlyName); } //根据设备的友好名字,创建一个代表该设备的Filter IBaseFilter * CTestPreviewDlg::CreateHardwareFilter(GUID inCategory,const char * inFriendlyName) { //创建一个系统枚举组件对象 ICreateDevEnum * enumHardware = NULL; HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_ALL, IID_ICreateDevEnum,(void**)&enumHardware); if(FAILED(hr)) { return NULL; } IBaseFilter * hardwareFilter = NULL; IEnumMoniker * enumMoniker = NULL; //为指定的目录创建枚举器 hr = enumHardware->CreateClassEnumerator(inCategory,&enumMoniker,0); if(enumMoniker) { enumMoniker->Reset(); ULONG fetched = 0; IMoniker * moniker = NULL; char friendlyName[256]; //枚举得到该目录下所有的设备,逐个进行名字匹配 while(!hardwareFilter && SUCCEEDED(enumMoniker->Next(1,&moniker, &fetched)) && fetched) { if(moniker) { IPropertyBag * propertyBag = NULL; VARIANT name; friendlyName[0] = 0; hr = moniker->BindToStorage(0,0,IID_IPropertyBag,(void**)&propertyBag); //读取设备的友好名字 if(SUCCEEDED(hr)) { name.vt = VT_BSTR; hr = propertyBag->Read(L"Friendlyname",&name,NULL); } if(SUCCEEDED(hr)) { WideCharToMultiByte(CP_ACP,0,name.bstrVal,-1, friendlyName,256,NULL,NULL); //如果当前设备的友好名字与用户指定的设备名字相同, //则将当前设备标识绑定为Filter形式 if(strcmp(friendlyName,inFriendlyName) == 0) { moniker->BindToObject(0,0,IID_IBaseFilter, (void**)&hardwareFilter); } } //释放使用过的接口 if(propertyBag) { propertyBag->Release(); propertyBag = NULL; } moniker->Release(); } } enumMoniker->Release(); } enumHardware->Release(); return hardwareFilter; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值