Dynamically create multiple DirectShow virtual webcam devices

I am trying to create a virtual webcam DirectShow filter with multiple virtual devices.

The cameras are defined as follows:

CFactoryTemplate g_Templates[] =
{
    {
        SUB_DEVICE_NAME_0,
        &CLSID_VirtualCam_0,
        CVCam::CreateInstance0,
        NULL,
        &AMSFilterVCam_0
    },
    {
        SUB_DEVICE_NAME_1,
        &CLSID_VirtualCam_1,
        CVCam::CreateInstance1,
        NULL,
        &AMSFilterVCam_1
    }
};

The initialization is done with these static methods:

CUnknown * WINAPI CVCam::CreateInstance0(LPUNKNOWN lpunk, HRESULT *phr)
{
    return new CVCam(NAME(DEVICE_NAME_0), lpunk, phr, CLSID_VirtualCam_0, 0);
}

CUnknown * WINAPI CVCam::CreateInstance1(LPUNKNOWN lpunk, HRESULT *phr)
{
    return new CVCam(NAME(DEVICE_NAME_1), lpunk, phr, CLSID_VirtualCam_1, 1);
}

Is it possible to do this more dynamically so that we do not have to hard code each camera but can pass a parameter during runtime?

Regards,


Solution 1:

Dynamic addition and removal of the camera is possible. However it is quite different from what you though of in the body of the question. DirectShow BaseClasses offer you certain helpers to convert static declarations into registration and DirectShow sample code shows how DLL exports are connected to these helpers for registration needs.

To make it dynamic you need to expand what AMoveDllRegisterServer2 does for most of filter projects and adapt its implementation for your needs.

This part of RegisterFilters might be a good starting point, you would want to make it a loop or something. You will have to deal with additional fm->RegisterFilter(... calls respectively to additional virtual cameras you want.