Click here to Skip to main content
Licence CPOL
First Posted 10 Aug 2006
Views 102,374
Downloads 2,669
Bookmarked 69 times

DirectShow Filters - What They Are

By | 16 Sep 2006 | Article
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.

    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.

  // 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.

    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.

    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)

About the Author

tanvon malik



Pakistan Pakistan

Member

Follow on Twitter Follow on Twitter
tanvon malik ( real name Tanveer Ahmad )I am a CNC Programmer, cnc setter, cnc operator. want to know about me visit my blog.
CNC Programming Blog
 
Reach Me
cnc blog | facebook | twitter | linkedin | flickr | google+
 
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.
 
my vc++ blog tanvon++
my blog about DirectShow in my national language URDU DirectShow.wordpress.com
and below is the place where we discuss DirectShow and VC++
http://groups.yahoo.com/group/tanvon

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralScreen capture using Windows media encoder and render it using direct show.. Pinmemberkalyanikalakonda23:44 8 Jul '09  
GeneralLive audio/video stream capture Pinmemberkazim bhai1:38 8 Jun '09  
Questionrequirements to run a direct show mobile 5.0 application Pinmembersreejit P0:52 1 Oct '08  
Generalnice tutorial Pinmemberwhitehat.c22:32 7 Sep '08  
GeneralI want to take the codec name from a filter... PinmemberVasi Floroiu23:30 19 Aug '08  
GeneralMultiple Sample Grabber Objects Pinmemberkazim bhai5:42 11 Jun '08  
QuestionWhat Filter to use for mms stream? Pinmemberajovanov8:13 26 Mar '08  
QuestionSound filter ? PinmemberSukhjinder_K1:47 24 Aug '07  
Questionhelp me Pinmembervikram panwar22:43 25 Jul '07  
QuestionDirectShow and Windows Media Encoder Pinmembershfnet21:52 21 Apr '07  
QuestionRe: DirectShow and Windows Media Encoder Pinmemberfatihsen4:34 14 Jun '07  
AnswerRe: DirectShow and Windows Media Encoder Pinmemberjcaple@cisco.com11:24 19 Jul '07  
Generalhey... Pinmemberemadpejman0:31 2 Feb '07  
GeneralRe: hey... PinmemberFlyingTinman12:58 26 Feb '07  
GeneralRe: hey... Pinmembertanvon malik21:52 6 Sep '08  
GeneralRegarding adding Transform filter Pinmemberericlnl20:26 7 Jan '07  
GeneralFastforward / Rewind of Playback PinmemberCookie8083:30 25 Oct '06  
GeneralRe: Fastforward / Rewind of Playback Pinmembertanvon5:41 25 Oct '06  
GeneralRe: Fastforward / Rewind of Playback PinmemberCookie8087:03 25 Oct '06  
GeneralRe: Fastforward / Rewind of Playback Pinmembertanvon19:36 25 Oct '06  
Generalmp3 Pinmembersurfman1913:36 17 Sep '06  
GeneralRe: mp3 PinmemberTanvon15:12 17 Sep '06  
surfman19 wrote:
how could i play an .mp3 file with DirectShow?

 
You can play a .mp3 or any media file in DirectShow in a easy way, But the way you want to create the filter graph.
If you choose the automatic way
pGB->RenderFile(L"your file.mp3", NULL);
pMC->Run();
it is all OK, But the problem here is, if you are missing some codec on your computer, DirectShow will not be able to properly handle that file whose codec(s)are missing.
Sometimes this happen a file is playing but you can't see its video just audio is playing properly, But on any other computer it shows its video also, this is becsuse of missing codec..

 
Tanvon
the brain behind ...
 
I Blog here

QuestionMulti player Pinmembertokayadmin2:57 29 Aug '06  
GeneralNice introduction PinmemberS.H.Bouwhuis12:41 15 Aug '06  
GeneralRe: Nice introduction PinmemberTanvon7:51 16 Aug '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 16 Sep 2006
Article Copyright 2006 by tanvon malik
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid