Microsoft DirectX 8.1 (C++) |
The following steps show you how to define and instantiate your filter class.
In the following example, the filter class derives from CTransInPlaceFilter:
class CMyFilter : public CtransInPlaceFilter
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;
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
}
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;
}
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 (CFactoryTemplate) 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.
You can add additional parameters to the CFactoryTemplate templates to add property pages. For information about using CFactoryTemplate in registration, see How to Register DirectShow Filters.
For information about generating GUIDs in general, see "GUID Creation and Optimizations" and "The uuidgen Utility" in the Platform SDK.
To generate a GUID in Microsoft® Visual C++® 5.x, choose Create GUID from the Tools menu. By default, the GUID is in DEFINE_GUID format, which is the format you want. Click the Copy button. Put the cursor in your source file beneath the include statements, and choose Paste from the Edit menu. The inserted code will look like the following example, except that it will have its own unique number and CLSID. Insert the code before your class definition in the header file or main file.
// {3FA5D260-AF2F-11d0-AE9C-00A0C91F0841}
DEFINE_GUID(CLSID_MyFilter,
0x3fa5d260, 0xaf2f, 0x11d0, 0xae, 0x9c, 0x0, 0xa0, 0xc9, 0x1f, 0x8, 0x41);