如何给filter添加自定义接口及调用

时间:2021-03-12 21:40:24

本例子是在VirtualCamera的基础上添加的自定义接口用来实现exe控制osd的显示。

1. 接口部分

#ifndef __H_MyFilter__
#define __H_MyFilter__

#ifdef __cplusplus
extern "C" {
#endif
//#include <initguid.h>
//EXTERN_GUID
EXTERN_GUID(IID_IMyFilter,  
0xef31f705, 0x32a7, 0x477a, 0x90, 0xc9, 0x2c, 0x7e, 0x81, 0xeb, 0x2, 0x5d);

DECLARE_INTERFACE_(IMyFilter,IUnknown)
{
STDMETHOD(setOSD) (THIS_ int osd_en) PURE;
};
#ifdef __cplusplus
}
#endif


#endif  

2.filter继承接口


#include "myfilter.h"
class CVCam : public CSource ,public IMyFilter
{
public:


    static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr); 
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv);//????
DECLARE_IUNKNOWN;
STDMETHODIMP  setOSD(int osd_en);
    IFilterGraph *GetGraph() {return m_pGraph;}
    int osd_enable;
private:
    CVCam(LPUNKNOWN lpunk, HRESULT *phr);
CVCamPin *m_pPin;

public:

};

/////////////////////////////////////

STDMETHODIMP  CVCam::setOSD(int osd_en)
{
osd_enable = osd_en;
return S_OK;
}


STDMETHODIMP CVCam::NonDelegatingQueryInterface(REFIID riid, void **ppv)
{
if(riid == _uuidof(IAMStreamConfig) || riid == _uuidof(IKsPropertySet))
{
// m_paStreams[0]->QueryInterface(riid, ppv);
return m_paStreams[0]->QueryInterface(riid, ppv);
}
else if (riid == IID_IMyFilter)
{
return GetInterface((IMyFilter *) this, ppv);
}
else
return CBaseFilter::NonDelegatingQueryInterface(riid, ppv);//修改
}

3.应用程序调用

        //根据guid 查找filter

 hr = CoCreateInstance(CLSID_myFilter, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast<void**>(&pSrcFilter));  

         IMyFilter *pInterface = NULL; 

         hr = pSrcFilter->QueryInterface(IID_IMyFilter, (void **)&pInterface);//从filter上查询接口

        pInterface->setOSD(0);//使用接口函数


如何给filter添加自定义接口及调用