Video capture and playback with DirectShow. Basic concepts
Currently there are a lot of video capture devices commercially available; anyone can buy a digital camera or a webcam at a reasonable price. All of these devices can be easily controlled from a program developed by ourselves using Microsoft DirectShow. DirectShow is a set of interfaces that provide a generic API with which you can capture and play audio and video regardless of the brand or model of camera you are using. It also allows recording and playback of files in any format.
The audio and video files are stored in compressed formats. To be able to record and play them, you need to have an appropriate codec. In this link you can download the VCL Codec Pack, a set of free codecs that provides support for many of these formats.
In this article, we will review at the basic DirectShow components using a tool called GraphEdit included in DirectX SDK. Once installed, you can find it in the path C:\Program Files (x86)\Windows Kits\8.1\bin\x86\graphedt.exe (8.1 is the version, maybe in your case the number is different, x86 is the directory for 32-bit systems, 64-bit systems use the version included in the x64 directory).
The basic elements of the system are the filters. Each of these elements is responsible for a specific task in the process of coding and decoding information from the device. There are filters to read video capture devices, read or write video files in different formats, compressors, color treatment and so on.
Each filter has different entry and exit points called pins, for connecting together the different components in a chain of operations called filter graph. For example, a filter video capture can have an output pin for the video channel and one for the audio channel, with which we can link it to a compressor filter and this in turn with another filter to write the compressed data in a file.
To build a filter graph, you have to use the Graph / Insert Filters... menu option:
Here you have to search in the Video Capture Sources element, in which should appear the list of capture devices installed on your system. In my case I will select the Logitech C510 webcam. With the Insert Filter button you can see that the first element of the filter graph is created:
On the right side you can see two output pins, one to capture video and another to capture a snapshot. Now let's add another filter to play the video from the camera. You have to search in the DirectShow filters section a device called Video Renderer, and then press again the Insert Filter button:
Now you just have to connect the camera output to the input of the player, to do that, click on the corresponding pin and drag the mouse towards the pin with which you want connect:
And that's all, simple is not it? Now you only have to press the play button and you can see the image from the webcam. We will now see a more complex example in which I will show you one of the most important and useful features of DirectShow. We will create a graph to play a MP4 video file. First, within the DirectShow Filters section, select the File Source (Async.) filter, which will prompt you to select a video file and, as above, select the Video Renderer filter to play it:
When connecting both filters, we can see that DirectShow automatically added new filters between them:
The LAV Splitter filter separates the video and audio channels, while MPEG Video Decoder decodes the data in MPEG format for the Video Renderer can play the movie. DirectShow automatically selected all intermediate elements necessary for proper playback! This makes it much easier the task when developing a program of video capture and playback, as you only have to worry about the initial and final filter of the graph, and the system will do all the hard work for you.
But DirectShow has many additional features, such as the ability to negotiate different video formats or frame size between the input and output pins. Here are links to a couple of books on programming with DirectShow where you can find everything you need to turn you into experts on the subject:
- Programming Microsoft DirectShow (Multimedia Programming)
- Programming Microsoft© DirectShow© for Digital Video and Television (Developer Reference)
In the next article I will show how to implement this in a program developed by yourselves, with a sample application that you can use as a starting point for yours.