Click here to Skip to main content
15,884,838 members
Articles / Desktop Programming / MFC

DirectShow Filters - What They Are

Rate me:
Please Sign up or sign in to vote.
2.87/5 (26 votes)
16 Sep 2006CPOL4 min read 176.1K   5.5K   80   28
This article is about DirectShow filters and how to create them

Introduction

DirectShow is an architecture for media streaming on Windows platform. With its help, you can do the following things:

  • Play a media stream
  • Capture a media stream
  • Media editing

Building Environment

All applications which want to use DirectShow must include the header Dshow.h, and use the library Strmiids.lib.

First Things First - Initialize the COM

DirectShow is based on COM model, therefore it is a must to initialize the COM before using it. You can do it with CoInitialize(). You must also uninitialize the COM after you have finished using COM. You can do it with CoUninitialize(). This means that all DirectShow calls are sandwiched in COM CoInitialize() and CoUninitialize() calls.

Let Us Start a Direct Show

Let us know what is DirectShow.

DirectShow's Building Block "Filters"

As already described, DirectShow is based on COM architecture. DirectShow consists of a wide range of COM objects, which do a specific work (e.g. reading data). These COM objects are called "filters" in DirectShow. DirectShow provides a set of standard filters, and developers can write their own to extend DirectShow. For a simple example, let us examine how an AVI file is played with filters that play a role in this operation.

  • File source filter (Reads data from file)
  • AVI splitter filter (Separates audio and video)
  • Decoder filters (Decodes video frames based on the compression used)
  • Video renderer filter (Draws Video frames)
  • DirectSound Device filter (Sends audio to sound card)

From the above description, it is clear that DirectShow is made of small components which do their work separately from one another, but are joined together to do a complex operation.

Pins

As stated, these COM components are joined together to perform an operation, the joining points are also COM objects called "pins".

DirectShow's "Filter Graph"

As you have seen, to play an AVI file approximately five filters have worked together. All these  filters are a must for this operation. And if a single filter gets missed, the file will not be played as required. So a set of filters required to run an operation successfully is called a "Filter Graph".

How to Build a "Filter Graph"

Building a filter graph means, creating appropriate filters and joining them through pins so that they perform the required operation successfully. This sounds like a complex operation, but DirectShow provides components which can help us in building a filter graph. Some are listed below:

  • Filter Graph Manager (Used for file playback and for controlling filter graph)
  • Capture Graph Builder (Used for capturing)
  • DVD Graph Builder (For DVD playback)

DirectShow's Backbone "Filter Graph Manager"

Filter Graph Manager is the basic component in DirectShow, and is used in almost all DirectShow applications. Whether you want to play a file, capture with a device or want a DVD playback, filter graph manager is the component which is a must to create. It is always not necessary to create this object, sometimes it is created for us by some other objects. Filter graph manager does the following things:

  • Provides us ways to build a filter graph (to add, remove, connect filters)
  • Coordinates state changes among filters (play, pause, stop, seek)
  • Handles synchronization of filters (with a reference clock)
  • Does event notification (for applications to know about state changes and other events)

Interfaces Exposed by "Filter Graph Manager"

Following are some of the important interfaces exposed by filter graph manager:

  • IBasicAudio (controls the volume and balance of audio stream)
  • IBasicVideo (sets video properties)
  • IGraphBuilder (help building filter graph)
  • IMediaControl (controls flow of data in filter graph)
  • IMediaEventEx (for event notifications and for overriding default event handling)
  • IMediaSeeking (for seeking a position in stream)
  • IVideoWindow (sets video window properties)

Actual Creation of "Filter Graph Manager"

Step 1 (Creation of Object)

The following code creates an object of filter graph manager and also gives us an interface pointer to IGraphBuilder interface exposed by filter graph manager object.

C++
IGraphBuilder *pGB   = NULL;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                     IID_IGraphBuilder, (void **)&pGB);

In the above code, an instance of filter graph manager is created. Now the actual work starts. You now have to decide who will build the graph. Filter graph manager supports the following ways:

  • Filter Graph Manager builds the entire graph
  • Filter Graph Manager builds the partial graph
  • The application builds the entire graph

Here I am going to use the first method.

Step 2 (Building the Graph Automatically)

IGraphBuilder::RenderFile method is used to build a filter graph automatically for a specified file. In this way, filter graph manager connects appropriate filters for the specified media file. This is called "Intelligent connect" in DirectShow.

C++
// Have the graph builder construct the appropriate graph automatically
      pGB->RenderFile(L"J:\\VIdeo\\ruby.avi", NULL);

Step 3 (Everything is OK, Just RUN)

IMediaControl::Run method is used to start the data flow in filter graph. IMediaControl provides the methods to control data flow in filter graph such as run, pause or stop. You first have to get IMediaControl interface, then just call IMediaControl::Run.

C++
IMediaControl *pMC   = NULL;
pGB->QueryInterface(IID_IMediaControl, (void **)&pMC);
pMC->Run();

Step 4 (We are Done, Call Release)

After you have finished, just call Release() for the interfaces used in the application to free resources.

C++
pMC->Release();
pGB->Release();

Good luck. Happy programming with DirectShow.

History

  • Sunday 30 July 2006: Just a bare minimum working model

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
tanvon malik ( real name Tanveer Ahmad )I am a CNC Programmer, cnc setter, cnc operator. want to know about me visit my websites.
CNC Programming
CNC Manuals
DXF & GCode Files with Online Viewers
Komment.me

I been in Switzerland MAG former +FMS+ for CNC training.


Most interesting technologies I like COM MFC DirectShow such as filter development. I am from Pakistan.
Have worked on projects mostly related video capturing, video data processing and real time object tracking on videos. For these I have worked on projects which use "Open CV Library". Which is mostly used for capturing data and its processing.

Comments and Discussions

 
QuestionGood Review Pin
h.ahmadi8628-May-14 3:57
h.ahmadi8628-May-14 3:57 
QuestionHow Can I Render To A Section Of The Screen? Pin
Member 98407435-Feb-14 19:18
Member 98407435-Feb-14 19:18 
GeneralScreen capture using Windows media encoder and render it using direct show.. Pin
kk17128-Jul-09 23:44
kk17128-Jul-09 23:44 
GeneralLive audio/video stream capture Pin
kazim bhai8-Jun-09 1:38
kazim bhai8-Jun-09 1:38 
Questionrequirements to run a direct show mobile 5.0 application Pin
sreejit P1-Oct-08 0:52
sreejit P1-Oct-08 0:52 
Generalnice tutorial Pin
whitehat.c7-Sep-08 22:32
whitehat.c7-Sep-08 22:32 
GeneralI want to take the codec name from a filter... Pin
Vasi Floroiu19-Aug-08 23:30
Vasi Floroiu19-Aug-08 23:30 
GeneralMultiple Sample Grabber Objects Pin
kazim bhai11-Jun-08 5:42
kazim bhai11-Jun-08 5:42 
QuestionWhat Filter to use for mms stream? Pin
ajovanov26-Mar-08 8:13
ajovanov26-Mar-08 8:13 
QuestionSound filter ? Pin
Sukhjinder_K24-Aug-07 1:47
Sukhjinder_K24-Aug-07 1:47 
Questionhelp me Pin
vikram panwar25-Jul-07 22:43
vikram panwar25-Jul-07 22:43 
QuestionDirectShow and Windows Media Encoder Pin
shfnet21-Apr-07 21:52
shfnet21-Apr-07 21:52 
QuestionRe: DirectShow and Windows Media Encoder Pin
fatihsen14-Jun-07 4:34
fatihsen14-Jun-07 4:34 
AnswerRe: DirectShow and Windows Media Encoder Pin
jcaple@cisco.com19-Jul-07 11:24
jcaple@cisco.com19-Jul-07 11:24 
Generalhey... Pin
emad.pejman2-Feb-07 0:31
emad.pejman2-Feb-07 0:31 
GeneralRe: hey... Pin
FlyingTinman26-Feb-07 12:58
FlyingTinman26-Feb-07 12:58 
GeneralRe: hey... Pin
tanvon malik6-Sep-08 21:52
tanvon malik6-Sep-08 21:52 
GeneralRegarding adding Transform filter Pin
ericlnl7-Jan-07 20:26
ericlnl7-Jan-07 20:26 
GeneralFastforward / Rewind of Playback Pin
Cookie80825-Oct-06 3:30
Cookie80825-Oct-06 3:30 
GeneralRe: Fastforward / Rewind of Playback Pin
tanvon malik25-Oct-06 5:41
tanvon malik25-Oct-06 5:41 
GeneralRe: Fastforward / Rewind of Playback Pin
Cookie80825-Oct-06 7:03
Cookie80825-Oct-06 7:03 
GeneralRe: Fastforward / Rewind of Playback Pin
tanvon malik25-Oct-06 19:36
tanvon malik25-Oct-06 19:36 
Generalmp3 Pin
surfman1917-Sep-06 13:36
surfman1917-Sep-06 13:36 
GeneralRe: mp3 Pin
tanvon malik17-Sep-06 15:12
tanvon malik17-Sep-06 15:12 
QuestionMulti player Pin
tokayadmin29-Aug-06 2:57
tokayadmin29-Aug-06 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.