Live Video Sources

Live Video Sources

If an application is to use live video sources, at some point early in its initialization, it will need to determine what sources are available to it. DirectShow provides a method to enumerate, by category, the filters registered on a user's system. The System Device Enumerator is a DirectShow COM object to perform this task - let's see how it works in Delphi.

Start a New project in Delphi. Drop a TListBox onto a form as shown here. We'll use the ListBox to list the video input sources. (To be really 'posh', I've added a label as well):


Before we go any further, you'll recall that the DirectShow documentation shows the C++ language. Because we need to familiarize ourselves with it - at least to the extent of being able to convert it to Delphi - I think it will be useful if we follow part of the code here and compare it as we go along.

The original documentation and C++ code can be found here: Using the system device enumerator

As the System Device Enumerator is a DirectShow COM Object, the C++ code begins by creating an instance of it as follows:

// Create the System Device Enumerator.
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
    IID_ICreateDevEnum, (void **)&pSysDevEnum);
if (FAILED(hr))
    return hr;
// Obtain a class enumerator for the video input device category.
IEnumMoniker *pEnumCat = NULL;
hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);

So, how do we do it in Delphi?

Well, first we need to add a couple of DSPack units to our uses section. The System Device Enumerator is in the DSUtil unit and, as we'll also need to specify the video input-device category, we need to add the unit where that's defined (DirectShow9) as well:

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, DSUtil, DirectShow9;

The best place to create the System Device Enumerator is probably in the Form's FormCreate event. Compare the Delphi code below with the C++ code shown above:

  Form1: TForm1;
  SysDev: TSysDevEnum;


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
    SysDev:= TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);

You'll notice that what is, in effect, a two stage process in C++ has been simplified considerably in Delphi thanks to the way it's been implemented in DSPack. Aren't we lucky! I've declared our own SysDev as a TSysDevEnum outside the FormCreate procedure to widen its scope so we can use it in another procedure, later on.

The C++ code then iterates through the devices - the code doesn't show us how they're displayed to the user other than to comment: // Display the name in your UI somehow

We can do much better than that. Here's the complete Delphi code to populate the ListBox with our video input devices:

procedure TForm1.FormCreate(Sender: TObject);
    i: Integer;

    SysDev:= TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
    if SysDev.CountFilters > 0 then
      for i:= 0 to SysDev.CountFilters - 1 do

Run the project and the "FriendlyNames" of the video input devices will be listed in the ListBox:


So, now that we've enumerated the video input devices on our system, what's next? We need to be able to select one of the devices and display its video in a VideoWindow.

previous page | next page

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

This site and its contents are © Copyright 2005 - All Rights Reserved.