Language : English Indonesia
Article URL : http://juhara.com/article-en-20-DirectShow-Playing-with-Camera.html

DirectShow: Playing with Camera

Zamrony P. Juhara
16 July 2008 00:00:00
 (1597 views)
Category: DirectX
Tutorial on how to use DirectShow to capture images from web camera

Introduction

Price of PC camera is getting lower, affordable by everybody. From cheap camera, enough for video conferencing in messaging tool such as Yahoo Messengger or IM, to expensive and sophisticated ones for recording high quality video. Even, current notebooks are equipped with built-in camera.

Now I will take you to program the camera and how to integrate it into software application. Of course with DirectShow and Delphi. In this article, I use Turbo Delphi Explorer 2006. I change unit header to the DirectX 9.0 header written by Alexey Barkovoy.

Application demo, which we are going to build, is still making use of uDirectShowPlayer.pas unit which contains implementation of TBasicPlayer class, just like previous DirectShow article series. Because this unit previously using DirectX 8.0 header unit written by Project JEDI team, We need to make some minor change due to naming differences on some data types.

You may need to read Multimedia Player with DirectShow part 1 and Multimedia Player with DirectShow part 2 before continue reading this article, because I will not explain basic steps to get started with DirectShow.

Video Capture in DirectShow

In DirectShow, video capture refer to process of recording with video input device such as camera, TV tuner, video tape recorder etc. Camera manufacturer usually already ships filter for capturing input from video camera. You can find video capture filters in CLSID_VideoInputDeviceCategory category. There is good chance that there is one filter in this category when you have one video input device, but it may be more if you have more video input devices installed in your system. What you need to do is enumerating to get instance of video input device filter.

Before we dive in camera programming, it is better if you try it first with GraphEdit utility shipped along with DirectX SDK. In GraphEdit, add filter by clicking Graph->Insert filters. You will face dialog as in Figure 1. Click and expand Video Capture Sources and pick one of video input filter you want. Click Insert filter button.

Insert video capture filter dialog

Figure 1. Insert video capture filter dialog.

Next, add Video Renderer filter inside DirectShow Filters category. Connect output pin of video input filter with input pin of video renderer filter. If succeed, you will get something like in Figure 2. Your system may be a little bit different. Try to run filter graph by clicking Run button (green triangle button).

Filter graph of video capture application

Figure 2. Filter graph of video capture application.

Video Capture Implementation with Delphi

As usual, wrapper class of camera is derived from TBasicPlayer by overriding BuildFilterGraph() (Listing 1).

Listing 1

     TDSCamera=class(TBasicPlayer)
     public
       procedure BuildFilterGraph;override;
     end;


Implementation of BuildFilterGraph()

In BuildFilterGraph() method, we compose filter just like in Figure 2. We get video capture filter instance from filter enumeration on video capture source category with class ID CLSID_VideoInputDeviceCategory (Listing 2). In this demo, I assume camera devices is already installed. You shoud check whether filter is installed and camera is ready.

Listing 2

{ TDSCamera }

procedure TDSCamera.BuildFilterGraph;
var CameraFilter,VideoRenderer,AVIDecompressor:IBaseFilter;
    hr:HResult;

begin
  if (FFilterGraph<>nil) then
  begin
    //Add video capture source ie, the camera
    CameraFilter:=FindFilterByIndex(CLSID_VideoInputDeviceCategory,0);
    FFilterGraph.AddFilter(CameraFilter,'Camera');

    //add AVI Decompressor filter
    AVIDecompressor:=AddFilterByCLSID(CLSID_AVIDec,'AVI Decompressor');
    //add Video Renderer filter
    VideoRenderer:=AddFilterByCLSID(CLSID_VideoRenderer,'Video Renderer');

    //connect output Camera to
    //input pin AVI Decompressor
    hr:=ConnectFilter(CameraFilter,AVIDecompressor);
    if hr<>S_OK then
        RaiseDirectShowException(hr,'Koneksi Camera ke AVI Decompressor gagal. ');

    //connect output AVI Decompressor to
    //input pin Video Renderer
    hr:=ConnectFilter(AVIDecompressor,VideoRenderer);
    if hr<>S_OK then
        RaiseDirectShowException(hr,'Koneksi AVI Decompressor ke Video renderer gagal. ');

  end;

end;

Ok that's it. Easy, isn't it? To download source code click here.

Related Article

Do you like this article? Help this website improve by donating. Any amounts is appreciated.

Or you can help by bookmarking this page. Delicious Bookmark this on Delicious