Building a Filter Graph from Scratch

Building a Filter Graph from Scratch

Now that you ve enumerated the filters available to DirectShow, it s possible to build up a filter graph say, one that will render an AVI movie from scratch. Begin by selecting New from the File menu, which will clear out any existing filter graph. Next you ll need a source filter that points to the AVI file. From the list of DirectShow filters, select the entry labeled File Source (Async) and click the button marked Insert Filter. You ll immediately be presented with a file selection dialog box with the message Select an input file for this filter to use. Select the AVI file Sunset.avi from the CD-ROM, and you should see the filter within GraphEdit, as shown in Figure 2-5.

figure 2-5 a source filter the beginning of every directshow filter graph

Figure 2-5. A source filter the beginning of every DirectShow filter graph

The AVI file in the source filter needs to be divided into two streams, one with video data and one with audio data. The DirectShow transform filter AVI Splitter makes the division; insert it into the filter graph.

The source filter and the transform filter need to be connected, from the output pin of the source filter to the input pin of the transform filter. To make this connection, click the output pin of the source filter, drag the mouse pointer over the input pin of the transform filter, and release the mouse button, as shown in Figure 2-6.

figure 2-6 creating a connection between an output pin and an input pin

Figure 2-6. Creating a connection between an output pin and an input pin

GraphEdit automatically rearranges the filters so that they fit together smoothly, as Figure 2-7 shows.

figure 2-7 graphedit adjusting the position of the filter

Figure 2-7. GraphEdit adjusting the position of the filter

The AVI Splitter transform filter produces two streams, which are available on output pins Stream 00 and Stream 01. Although it s less than clear from these labels, Stream 00 is a video stream and Stream 01 is an audio stream. (This confusing nomenclature is a result of the fact that an AVI file can have a wide variety of media types stored within, which means you couldn t appropriately name a pin VideoOut or AudioOut because there might be no video or audio in any given AVI file.) To render the audio stream, you need to insert the renderer filter Default DirectSound Device from the list of Audio Renderers in the Insert Filters dialog box. Connect output pin Stream 01 to the Audio Input pin of the renderer filter. Now your filter graph should look like the one shown in Figure 2-8.

figure 2-8 a directsound renderer filter added

Figure 2-8. A DirectSound renderer filter added

Rendering the video is a two-step process. Because this AVI file contains digital video (DV) and audio data, a transform filter known as the DV Video Decoder must be added to the filter graph. Add the filter, and connect pin Stream 00 from the AVI Splitter filter to the XForm In pin of the DV Video Decoder. Finally a video renderer can be selected from the list of DirectShow filters. Although several types of video renderers are available including full-screen and off-screen renderers we ll use the generic Video Renderer, which sends the video to a window on the display that will be opened when the filter graph begins to run. Add the filter, and connect the output pin XForm Out from the DV Video Decoder to the Input pin of the Video Renderer. When it s all done, the filter graph will look like Figure 2-9.

figure 2-9 a complete filter graph rendering both audio and video streams of the avi file

Figure 2-9. A complete filter graph rendering both audio and video streams of the AVI file

At this point, click Play. A new window titled ActiveMovie Window will open on the screen and display the AVI file as it plays, as shown in Figure 2-10. The window s title bar is a holdover from the time, a few years back, when DirectShow was known as ActiveMovie. (The movie has a silent soundtrack, so don t expect to hear anything from the speakers as the movie is playing.)

That s what it takes to create a filter graph from scratch; filters have to be inserted into the filter graph manually and connected appropriately. However, GraphEdit provides two alternative techniques to speed the creation of filter graphs: rendering pins and Intelligent Connect.

figure 2-10 the avi file in mid-playback

Figure 2-10. The AVI file in mid-playback

Creating Filter Graphs with Rendering Pins

Another technique that quickly renders media files from within GraphEdit uses rendering pins. Starting from an empty filter graph, add a File Source (Async) source filter to the filter graph and select Sunset.avi as the input file. Right-click the output pin of the filter. You ll see the menu shown in Figure 2-11.

figure 2-11 the pin menu with per-pin options on each filter

Figure 2-11. The pin menu with per-pin options on each filter

Select Render Pin. The resulting filter graph should be identical to the filter graph you built from scratch without the additional steps of finding the correct DirectShow filters, inserting them into the filter graph, and connecting them appropriately. You can Render Pin from any output pin in the filter graph, not just a source filter. You can use this feature to finish a partially complete filter graph or, as in this case, to build the entire filter graph.

Simplifying Design Tasks with Intelligent Connect

In Chapter 1, you used the filter graph to connect output pins that accepted different media types by stringing a set of intermediate transform filters between the pins. This feature, known as Intelligent Connect, allows the filter graph to assume most of the heavy lifting involved in graph building. This same feature can be put to work in your DirectShow programs to simplify coding tasks in many situations. Intelligent Connect is turned on in GraphEdit by default. (To turn it off, clear the Connect Intelligent menu option in the Graph menu.)

To use Intelligent Connect, begin by creating a filter graph with two filters: first a File Source (Async) filter, pointing to the file Sunset.avi, and next a Video Renderer filter. You ve now got a filter graph with a source filter and a renderer filter, but no transform filters. However, GraphEdit will let you create a connection from the output pin of the source filter to the input pin of the renderer filter, as shown in Figure 2-12.

figure 2-12 a source filter can t be connected directly to a renderer filter, unless

Figure 2-12. A source filter can t be connected directly to a renderer filter, unless

This connection won t work on its own. The media type of the output pin of the source filter does not agree with the media type required by the input pin of the video renderer. That s when Intelligent Connect steps in and adds the intermediate filters needed to transform AVI file data into video that can be rendered to the display, as shown in Figure 2-13.

figure 2-13  intelligent connect is used, in which case intermediate transform filters are added.

Figure 2-13. Intelligent Connect is used, in which case intermediate transform filters are added.

The filter graph has added AVI Splitter and DV Video Decoder transform filters, both of which are needed to translate the output of the source filter into video data that can be rendered to the display. Note that the audio stream hasn t been rendered. Rather than rendering the media file, Intelligent Connect simply found a path to connect a source filter to a renderer filter. If the renderer filter had been a Default DirectSound Device rather than a Video Renderer, the resulting filter graph would have looked like Figure 2-14.

figure 2-14 a source filter connected to an audio renderer requiring one transform filter

Figure 2-14. A source filter connected to an audio renderer requiring one transform filter



Programming Microsoft DirectShow for Digital Video and Television
Programming Microsoft DirectShow for Digital Video and Television (Pro-Developer)
ISBN: 0735618216
EAN: 2147483647
Year: 2002
Pages: 108
Authors: Mark D. Pesce

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net