Define and Instantiate Your Filter Class
The following steps show you how to define and instantiate your filter class.
Determine the base classes from which to derive your filter class (and pin classes, if necessary). Typically, your transform filter class derives from the CTransformFilter, CTransInPlaceFilter, or CVideoTransformFilter transform base classes, or from the more generic CBaseFilter class. If you want to transform video media (especially AVI data), derive from CVideoTransformFilter. If your filter must copy the input media samples, derive from CTransformFilter. If your filter can transform the sampled media in place, derive from CTransInPlaceFilter. If you do not want the simple transform filter support provided in the transform base classes, but prefer to implement your own member functions, derive from CBaseFilter. See Determine Which Base Classes to Use for more information.
In the following example, the filter class derives from CTransInPlaceFilter.
class CMyFilter : public CTransInPlaceFilter
Implement the IUnknown interface for your object.
In the public section of your filter class definition, create an instance of CUnknown, and then call the DECLARE_IUNKNOWN macro.
public:
static CUnknown *WINAPI CreateInstance(LPUNKNOWN punk, HRESULT
*phr);
DECLARE_IUNKNOWN;
Define your constructor. Also, define your Transform and CheckInputType functions (this does not apply if your filter class is derived from CBaseFilter).
In the private section of your filter class definition, define your constructor by calling the constructor of the transform filter class you derived from, and then add code to perform the transform and check the input type. For example:
// Define your constructor by calling the constructor of
// CTransInPlaceFilter.
CMyFilter(TCHAR *tszName, LPUNKNOWN punk, HRESULT *phr)
: CTransInPlaceFilter (tszName, punk, CLSID_MyFilter, phr)
{ }
// Add the transform code.
HRESULT Transform(IMediaSample *pSample){
// Transform code here
}
// Add code to check the input type.
HRESULT CheckInputType(const CMediaType* mtIn) {
// Input checking code here
}
Implement CreateInstance for your filter object. Typically, CreateInstance calls the constructor of your filter class. For example:
CUnknown * WINAPI CMyFilter::CreateInstance(LPUNKNOWN punk, HRESULT *phr) {
CMyFilter *pNewObject = new CMyFilter(NAME("Description of My Filter"),
punk, phr );
if (pNewObject == NULL) {
*phr = E_OUTOFMEMORY;
}
return pNewObject;
}
Declare a global array g_Templates of CFactoryTemplate objects to inform the default class factory code how to access the CreateInstance function.
CFactoryTemplate g_Templates[]=
{ { L"My Filter"
, &CLSID_MyFilter
, CMyFilter::CreateInstance // function called by class factory
, NULL
, &sudMyFilter } // address of the AMOVIESETUP_FILTER structure,
// or NULL if no structure exists
};
int g_cTemplates = sizeof(g_Templates)/sizeof(g_Templates[0]);
The g_cTemplates variable defines the number of class factory templates for the filter. Each of these templates provides a link between COM and the filter and are used to create the base object for the filter. At a minimum, the filter has one template that provides the address of its own CreateInstance function, which, when called, creates the base object.
For information about using CFactoryTemplate in registration, see Registering DirectShow Filters.
Generate a GUID for your filter object using either uuidgen.exe or guidgen.exe, which are available in the Microsoft Windows SDK or with Visual Studio. For more information, see MSDN.
Insert the code for the GUID before the class definition in the header file or main file. The following code shows an example of a GUID definition.
// {3FA5D260-AF2F-11d0-AE9C-00A0C91F0841}
DEFINE_GUID(CLSID_MyFilter,
0x3fa5d260, 0xaf2f, 0x11d0, 0xae, 0x9c, 0x0, 0xa0, 0xc9, 0x1f, 0x8, 0x41);
Last updated on Wednesday, April 13, 2005
© 2005 Microsoft Corporation. All rights reserved.