Device Enumeration

This section describes device enumeration, querying information about attached devices and their connection topology.

Simple Device Enumeration

The simplest way to gather information about the available video capture devices is querying DeviceEnum.Devices. The static property returns a flat list of DeviceInfo objects.

The following code snippet shows how to get the list of devices and print device information to the console:

static void PrintDeviceList()
{
    Console.WriteLine("Enumerating all attached video capture devices...");

    var deviceList = ic4.DeviceEnum.Devices;

    if (deviceList.Count == 0)
    {
        Console.WriteLine("No devices found");
    }

    Console.WriteLine($"Found {deviceList.Count} devices:");

    foreach (var deviceInfo in deviceList)
    {
        Console.WriteLine($"\t{FormatDeviceInfo(deviceInfo)}");
    }

    Console.WriteLine();
}

The device information is printed using a simple helper function:

static string FormatDeviceInfo(ic4.DeviceInfo deviceInfo)
{
    return $"Model: {deviceInfo.ModelName} Serial: {deviceInfo.Serial}";
}

Query Interface/Device Topology

Device enumeration can also be performed on a per-interface basis. Interfaces are the physical pieces of hardware that cameras are attached to, e.g. USB controllers or network adapters.

To get a list of the available interfaces in the system, query DeviceEnum.Interfaces. Each interface can in turn be queried for the currently attached video capture devices using Interface.Devices.

The following code snippet shows how to get the list of interfaces. It then prints interface information and device information to the console:

static void PrintInterfaceDeviceTree()
{
    Console.WriteLine("Enumerating video capture devices by interface...");

    var interfaceList = ic4.DeviceEnum.Interfaces.ToList();

    foreach (var itf in interfaceList)
    {
        Console.WriteLine($"Interface: {itf.DisplayName}");
        Console.Write($"\tProvided by {itf.TransportLayerName}");
        Console.WriteLine($" [TLType: {itf.TransportLayerType}]");

        var deviceList = itf.Devices.ToList();

        if (deviceList.Count == 0)
        {
            Console.WriteLine("\tNo devices found");
        }

        Console.WriteLine($"\tFound {deviceList.Count} devices:");

        foreach (var deviceInfo in deviceList)
        {
            Console.WriteLine($"\t\t{FormatDeviceInfo(deviceInfo)}");
        }
    }

    Console.WriteLine();
}

Receiving Device Arrival and Removal Notifications

DeviceEnum objects allow registering an event handler that notifies the program of changes in the list of available devices. To get notified about changes to the connected devices, assign a method to DeviceEnum.DeviceListChanged.

The following code snippet shows how to register and finally unregister a device-list-changed event handler:

static void RegisterDeviceListChanged()
{
    var enumerator = new ic4.DeviceEnum();
    enumerator.DeviceListChanged += HandleDeviceListChanged;

    Console.WriteLine("Waiting for DeviceListChanged event");
    Console.WriteLine("Press any key to exit");
    Console.ReadKey();
}

An example device-list-changed handler can look like this:

static void HandleDeviceListChanged(object sender, EventArgs e)
{
    Console.WriteLine("Device list changed!");
    PrintDeviceList();
}