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
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
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.
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.
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 *pMC = NULL;
pGB->QueryInterface(IID_IMediaControl, (void **)&pMC);
Step 4 (We are Done, Call Release)
After you have finished, just call
Release() for the interfaces used in the application to free resources.
Good luck. Happy programming with DirectShow.
- Sunday 30 July 2006: Just a bare minimum working model