Rendering a Live Source

A Live Video Source

In the last section, we saw how to use DirectShow's System Device Enumerator to populate a ListBox with the video input devices on our system. By using a different class ID when we created our own System Device Enumerator (SysDevEnum), we could have enumerated a different category.

For example, the following would have enumerated the Audio input devices:

    SysDev:= TSysDevEnum.Create(CLSID_AudioInputDeviceCategory);

You'll find a complete list of filter categories together with their class identifiers (CLSID) at the msdn website.

However, for now, we'll stick with our video input devices because we now need to be able to select a device from the ListBox and render its video in a VideoWindow.

Resize our project's Form and, from the DSPack component palette, add a VideoWindow, and a FilterGraph as in our earlier projects. This time, however, we need to add a Filter component which will 'represent' whichever input device we select (Our source filter).

project4

As in our earlier projects, we need to associate the VideoWindow with the FilterGraph. In this project, we also need to associate the new Filter1 with the FilterGraph. As before, we can associate them in code or in the Delphi Object Inspector:

   VideoWindow1.FilterGraph:= FilterGraph1;
   Filter1.FilterGraph:= FilterGraph1;

project4a

OK, so we want to be able to click on a video device listed in the ListBox and have its video display in the VideoWindow so we need some code in the ListBox's OnClick Event.

Let's take it step by step. The first thing we need to do is ensure that the FilterGraph isn't already active and not displaying video:

procedure TForm1.ListBox1Click(Sender: TObject);
begin
   if FilterGraph1.Active then FilterGraph1.Active:= False;
   FilterGraph1.ClearGraph;

end;

Provided we have clicked on a valid device in the ListBox, we now need to set the Filter1.BaseFilter to our selected video device and Activate the FilterGraph

    if ListBox1.ItemIndex > -1 then
    begin
      Filter1.BaseFilter.Moniker:= SysDev.GetMoniker(ListBox1.ItemIndex);
      FilterGraph1.Active:= True;

Once the FilterGraph is active, we can call on a second DirectShow COM Object called ICaptureGraphBuilder2. This is the core "worker" when building a Filter Graph. Since we're using DirectShow's ICaptureGraphBuilder2, we need to set our FilterGraph1.Mode to 'gmCapture'. We'll get an error if we forget this step. This is the Delphi code:

      FilterGraph1.Mode:= gmCapture;
      with FilterGraph1 as ICaptureGraphBuilder2 do
      begin
        RenderStream(@PIN_CATEGORY_PREVIEW, nil, Filter1 as IBaseFilter,
                     nil, VideoWindow1 as IBaseFilter);
      end;

And, finally, we need to set the FilterGraph running with:

      FilterGraph1.Play;

Let's put all the code together and run the program:

procedure TForm1.ListBox1Click(Sender: TObject);
begin
   if FilterGraph1.Active then FilterGraph1.Active:= False;
   FilterGraph1.ClearGraph;
   if ListBox1.ItemIndex > -1 then
   begin
      Filter1.BaseFilter.Moniker:= SysDev.GetMoniker(ListBox1.ItemIndex);
      FilterGraph1.Active:= True;

      FilterGraph1.Mode:= gmCapture;
      with FilterGraph1 as ICaptureGraphBuilder2 do
      begin
        RenderStream(@PIN_CATEGORY_PREVIEW, nil, Filter1 as IBaseFilter,
                     nil, VideoWindow1 as IBaseFilter);
      end;
 
      FilterGraph1.Play;
   end;
end;

 

Important note for Delphi 5 users


OK, we skipped over a couple of important lines of code there. They are important to understanding DirectShow and how it's implemented in DSPack so we'll take a longer look at those lines in the next section.

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.