Directshow 注册 source filter

编写source filter最初需要接触的就是 filter的register和unregister,涉及到的函数有两个

DllRegisterServer()

以及

DllUnregisterServer()

在这两个函数中,完成了filter的register和unregister

而其中真正的注册,又是通过IFilterMapper2::RegisterFilter() 和 IFilterMapper2::UnregisterFilter()

来实现的。

我们需要在注册时将Filter放入不同的category,譬如 video capture source / directshow filters (使用GraphEdit可以看到,不同的category下的filters)。注册在video capture source下的filter可以在AMCapture / QQ 等AP,搜索video capture source filters出现。

  1. STDAPI DllRegisterServer()  
  2. {  
  3. //    return AMovieDllRegisterServer2(TRUE);   
  4.     HRESULT hr;  
  5.     IFilterMapper2 *pFM2 = NULL;  
  6.     hr = AMovieDllRegisterServer2(TRUE);  
  7.     if (FAILED(hr))  
  8.     {  
  9.         return hr;  
  10.     }  
  11.     hr = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, IID_IFilterMapper2, (void**)&pFM2);  
  12.     if (FAILED(hr))  
  13.     {  
  14.         return hr;  
  15.     }  
  16.     hr = pFM2->RegisterFilter(CLSID_UVCPreview, L"UVC preview", NULL, &CLSID_VideoInputDeviceCategory, L"UVC preview", &rf2FilterReg);  
  17.     pFM2->Release();  
  18.     return hr;  
  19. // DllRegisterServer   
  20.   
  21. //   
  22. // DllUnregisterServer   
  23. //   
  24. STDAPI DllUnregisterServer()  
  25. {  
  26. //    return AMovieDllRegisterServer2(FALSE);   
  27.     HRESULT hr;  
  28.     IFilterMapper2 *pFM2 = NULL;  
  29.     hr = AMovieDllRegisterServer2(FALSE);  
  30.     if (FAILED(hr))  
  31.     {  
  32.         return hr;  
  33.     }  
  34.     hr = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, IID_IFilterMapper2, (void **)&pFM2);  
  35.     if (FAILED(hr))  
  36.     {  
  37.         return hr;  
  38.     }  
  39.     hr = pFM2->UnregisterFilter(&CLSID_VideoInputDeviceCategory, L"UVC preview", CLSID_UVCPreview);  
  40.     pFM2->Release();  
  41.     return hr;  
  42. // DllUnregisterServer  

其中 IFilterMapper2::RegisterFilter()参数:

  1. HRESULT RegisterFilter(  
  2.   REFCLSID clsidFilter,  
  3.   LPCWSTR Name,  
  4.   IMoniker **ppMoniker,  
  5.   const CLSID *pclsidCategory,  
  6.   const OLECHAR *szInstance,  
  7.   const REGFILTER2 *prf2  
  8. );  
  9. Parameters  
  10. clsidFilter  
  11. [in] Class identifier (CLSID) of the filter.  
  12. Name  
  13. [in] Descriptive name for the filter.  
  14. ppMoniker  
  15. [inout] Address of a pointer to a device moniker that determines where this filter's data will be written. Can be NULL.  
  16. pclsidCategory  
  17. [in] Pointer to the filter category of the filter. If NULL, the default category is CLSID_ActiveMovieFilters. (See Filter Categories.)  
  18. szInstance  
  19. [in] Instance data for constructing the device moniker's display name. Can be the friendly name, or the string representation of the filter CLSID. If NULL, defaults to the filter CLSID.  
  20. prf2  
  21. [in] Pointer to a REGFILTER2 structure containing filter information.  
  22. Return Values  
  23. Returns an HRESULT value. Possible values include those shown in the following table.  
  24. Value Description   
  25. S_OK Success.   
  26. VFW_E_BAD_KEY Could not get registry key.   

而Category主要包括:

  1. The following categories are defined in Uuids.h. They are defiined when you include Dshow.h.  
  2. Friendly Name CLSID Merit   
  3. Audio Capture Sources CLSID_AudioInputDeviceCategory MERIT_DO_NOT_USE   
  4. Audio Compressors CLSID_AudioCompressorCategory MERIT_DO_NOT_USE   
  5. Audio Renderers CLSID_AudioRendererCategory MERIT_NORMAL   
  6. Device Control Filters CLSID_DeviceControlCategory MERIT_DO_NOT_USE   
  7. DirectShow Filters CLSID_LegacyAmFilterCategory MERIT_NORMAL   
  8. External Renderers CLSID_TransmitCategory MERIT_DO_NOT_USE   
  9. Midi Renderers CLSID_MidiRendererCategory MERIT_NORMAL   
  10. Video Capture Sources CLSID_VideoInputDeviceCategory MERIT_DO_NOT_USE   
  11. Video Compressors CLSID_VideoCompressorCategory MERIT_DO_NOT_USE   
  12. Video Effects (1 input) CLSID_VideoEffects1Category MERIT_DO_NOT_USE   
  13. Video Effects (2 inputs) CLSID_VideoEffects2Category MERIT_DO_NOT_USE   
  14. WDM Streaming Capture Devices AM_KSCATEGORY_CAPTURE MERIT_DO_NOT_USE   
  15. WDM Streaming Crossbar Devices AM_KSCATEGORY_CROSSBAR MERIT_DO_NOT_USE   
  16. WDM Streaming Rendering Devices AM_KSCATEGORY_RENDER MERIT_DO_NOT_USE   
  17. WDM Streaming Tee/Splitter Devices AM_KSCATEGORY_SPLITTER MERIT_DO_NOT_USE   
  18. WDM Streaming TV Audio Devices AM_KSCATEGORY_TVAUDIO MERIT_DO_NOT_USE   
  19. WDM Streaming TV Tuner Devices AM_KSCATEGORY_TVTUNER MERIT_DO_NOT_USE   
  20. WDM Streaming VBI Codecs AM_KSCATEGORY_VBICODEC MERIT_DO_NOT_USE   
  21. ActiveMovie Filter Categories CLSID_ActiveMovieCategories Not applicable   
  22.   
  23. The following categories are defined in the header file Ks.h:  
  24. Friendly Name CLSID Merit   
  25. WDM Streaming Communication Transforms KSCATEGORY_COMMUNICATIONSTRANSFORM MERIT_DO_NOT_USE   
  26. WDM Streaming Data Transforms KSCATEGORY_DATATRANSFORM MERIT_DO_NOT_USE   
  27. WDM Streaming Interface Transforms KSCATEGORY_INTERFACETRANSFORM MERIT_DO_NOT_USE   
  28. WDM Streaming Mixer Devices KSCATEGORY_MIXER MERIT_DO_NOT_USE   
  29.   
  30. The following categories are defined in the header file Ksmedia.h. Include these header files, in the order listed:  
  31. #include <ks.h>  
  32. #include <ksmedia.h>   
  33. Friendly Name CLSID Merit   
  34. WDM Streaming System Audio Devices KSCATEGORY_AUDIO_DEVICE MERIT_DO_NOT_USE   
  35.   
  36. The following categories are defined in the header file Bdamedia.h. Include these header files, in the order listed:  
  37. #include <ks.h>  
  38. #include <ksmedia.h>  
  39. #include <bdamedia.h>     
  40. Friendly Name CLSID Merit   
  41. BDA CP/CA Filters Category CLSID_CPCAFiltersCategory MERIT_NORMAL   
  42. BDA Network Providers KSCATEGORY_BDA_NETWORK_PROVIDER MERIT_NORMAL   
  43. BDA Receiver Components KSCATEGORY_BDA_RECEIVER_COMPONENT MERIT_NORMAL   
  44. BDA Rendering Filters KSCATEGORY_IP_SINK MERIT_DO_NOT_USE   
  45. BDA Source Filters KSCATEGORY_BDA_NETWORK_TUNER MERIT_DO_NOT_USE   
  46. BDA Transport Information Renderers KSCATEGORY_BDA_TRANSPORT_INFORMATION MERIT_NORMAL   

其中:

CLSID_VideoInputDeviceCategory, 注册到此category后,filter会出现在video capture source category下,被AMCapture / QQ等AP搜索到。

CLSID_LegacyAmFilterCategory, 注册到此category后,filter会出现在 directshow filters category,大部分的filter注册在此category。

需要注意的是 register / unregister 必须在同一个category, 使用同一 REFCLSID, 否则会出现register error。

譬如:

0x80070002 : register / unregister 未在同一个 category

0x80070005 : register / unregister 权限不够 (vista下权限管理,需要在administor下register/unregister filter)

原文地址:https://www.cnblogs.com/wqj1212/p/2441373.html