《微软:DirectShow开发指南》第二章 GraphEdit

Chapter 2  GraphEdit

因为本章只是GraphEdit的使用,没有什么高深的技术,所以就不一一翻译了。

As you read Chapter 1, you might have visualized DirectShow filters as being boxes with input and output pins, connected with “wires,” one filter to another, across a filter graph. The DirectX SDK has a tool, GraphEdit, that makes these DirectShow visualizations explicit, executable filter graphs. GraphEdit is DirectShow’s visual filter graph editor, and this chapter covers the features and uses of this tool.

GraphEdit可以进行可视化的说明,执行filter graph

 

Introducing GraphEdit

The basic elements of DirectShow applications—filters, connections, and filter graphs—can be easily represented visually, and drawing a diagram of a DirectShow filter graph can be an important aid in the design process. GraphEdit can be thought of as a whiteboard on which prototype DirectShow filter graphs can be sketched. However, because GraphEdit is built using DirectShow components, these whiteboard designs are fully functional, executable DirectShow programs. More than just a design tool, GraphEdit is a rapid prototyping environment for DirectShow. Any DirectShow application, regardless of complexity, can be built and tested in GraphEdit before you write a single line of application code.

DS应用的所有基本元素—filter, 连接,和filter graph—都能在GraphEdit中可视化表示,因为它还能执行,所以可以用作写代码实现前,设计时的原型验证。

 

To launch GraphEdit, select Programs from the Start menu, select Microsoft DirectX 9.0 SDK, select DirectX Utilities, and then select GraphEdit. When the program launches, you’ll be presented with a large blank area, representative of an empty filter graph with no component filters, as shown in Figure 2-1.

Figure 2-1. GraphEdit’s startup state

 

Rendering Media Files

GraphEdit makes it easy to render a wide range of media files. From the File menu, select Render Media File. You’ll be presented with an Open File dialog box asking you to select a file to be rendered. Select the file John Boy 9 – Foggy Day.wav (included on the CD-ROM). If your DirectX installation is correct, you should see the the filter graph shown in Figure 2-2

《微软:DirectShow开发指南》第二章 GraphEdit_第1张图片

GraphEdit has created a complete filter graph, with three components. From left to right, the filter graphs are

A source filter that points to the WAV file

A transform filter that parses the file into a series of samples

A renderer filter that passes the stream along to the default DirectSound device

As explained in Chapter 1, these three types of filters—source, transform, and renderer—can be found in nearly every filter graph. The filter graph created by GraphEdit to render the WAV file is minimal but complete. To hear the WAV file being rendered to the speakers (or whichever DirectSound device has been selected as the default), click the Play button on the toolbar, immediately below the menu bar, as shown in Figure 2-3.

《微软:DirectShow开发指南》第二章 GraphEdit_第2张图片

Figure 2-2. Filter graph to render a WAV file

 

When you click the Play button, the WAV file begins to render and the control below the toolbar begins to fill with color, corresponding to how much of the WAV file has been read into the filter graph. You can pause the filter graph by clicking Pause or stop it by clicking Stop. (As you might expect, the Play, Pause, and Stop

buttons send the run, pause, and stop messages to the filter graph.) After the entire stream has rendered to the DirectSound device, GraphEdit stops the filter graph. Using the Render Media File option, a broad range of media files—all the types understood by DirectShow—can be rendered. In fact, trying to render these files is a good test of the DirectShow capabilities of your system. If you open a media file and DirectShow can’t render it, it’s likely that you don’t have the appropriate collection of DirectShow filters (which might encourage you to write a DirectShow filter that could render the media type you’re interested in).

Figure 2-3. Rendering the WAV file

 

Enumerating Filter Types

To understand what kinds of filter graphs you can create in GraphEdit, you have to enumerate (list) all the possible filters available for DirectShow’s use. From GraphEdit’s Graph menu, select the first item, Insert Filters, and you’ll see the dialog box shown in Figure 2-4.

Figure 2-4. GraphEdit’s Insert Filters dialog box enumerating all available DirectShow filters

This dialog box uses the Windows convention of expandable lists of items. To the left of each list entry in the dialog box is a plus sign, which hides a list of DirectShow filters underneath. Clicking on the DirectShow Filters entry will present the entire list of DirectShow filters available to GraphEdit. The other filters

enumerated in the Insert Filters dialog box are Windows Driver Model (WDM) devices (all valid WDM devices are available to DirectShow as filters), DirectX Media Objects (DMOs), Video for Windows devices, and so forth.

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.《微软:DirectShow开发指南》第二章 GraphEdit_第3张图片

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

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

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

 

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

 

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.

《微软:DirectShow开发指南》第二章 GraphEdit_第4张图片

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.

《微软:DirectShow开发指南》第二章 GraphEdit_第5张图片

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…


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.

 

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

 

Capturing Video from a Digital Camcorder to Disk

One of the most important capabilities of DirectShow is its ability to make quick work of the various audio and video capture devices, such as DV camcorders, which have become increasingly popular in the last few years. Many PCs have IEEE 1394 (FireWire) connectors so that DV camcorders can be plugged directly into a PC for digital capture, editing, and production of video material. In particular, Microsoft Windows XP makes it very easy to work with digital camcorders, and the inclusion of Windows Movie Maker provides a level of software support for the digital camcorder that hasn’t been seen before in Windows systems. For the rest of this chapter, we’ll assume that there’s a DV camcorder attached to your computer system, most probably through a high-speed IEEE 1394 interface.

 

You’ll now use GraphEdit to create a filter graph that will take the input from the capture source filter representing the DV camcorder and write that data out to disk as a high-resolution AVI movie file. To begin, clear the filter graph with the New item from the File menu. Now open the Insert Filters dialog box, and open the list of Video Capture Sources. Depending on how many video capture devices are attached to your system, this list could be quite long or could contain just one entry.

 

If you don’t see anything on the list, make sure your camera is on and plugged in to your computer correctly. If you still don’t see anything on the list,  you might be having a problem with the drivers associated with your DV camcorder. Windows XP includes a standard driver for camcorders

connected with IEEE 1394, but your camcorder won’t necessarily work correctly with that driver.

 

Somewhere in the list you’ll see the entry Microsoft DV Camera And VCR. This is the standard DirectShow filter that acts as a capture source filter for both digital camcorders and VCRs. Add the filter to the filter graph. Next add the DV Splitter from the list of DirectShow filters and connect the DV A/V Out pin from the source filter to the Input pin of the DV Splitter filter. The DV Splitter, as the name suggests, splits the audio/video (A/V) stream coming from the source filter into two streams, an audio stream identified on an output pin as AudOut00 and a video stream identified as DVVidOut0.

 

You would like to be able to monitor the video stream so that you can see what’s coming out of the camera as the video stream is being written to disk. For you to do so, the video stream must be split into two components, which can be done using a DirectShow transform filter known as the Smart Tee. When this transform filter is inserted into the filter graph, the DV Splitter output pin DVVidOut0 should be connected to its input pin. The Smart Tee has two output pins, appropriately labeled Capture and Preview. If you use the Render Pin function on the Preview pin of the Smart Tee, you get a filter graph that can monitor the output of the camcorder. The filter graph should now look like Figure 2-15.

Figure 2-15. An incomplete filter graph for DV camcorder capture and monitoring

 

At this point, the separate audio and video streams have to be recombined (or multiplexed) into a single stream so that they can be written to a file. Add the AVI Mux DirectShow transform filter to the graph. The AVI Mux has one input pin, Input 01. Connect the output pin Capture from the Smart Tee to this input pin, and a second input pin, Input 02, appears on the AVI Mux. (This process will keep going indefinitely; as you add inputs, the AVI Mux adds pins.) Connect the AudOut00 pin from the DV splitter to the Input 02 pin on the AVI Mux. Now the AVI Mux is mixing the video and audio streams from the camcorder into a single AVI-formatted stream. Finally add a File Writer renderer filter from the Insert Filters dialog box. Doing so will open a file-selection dialog box that asks for an output file name. Call this file DVCAM.AVI. Once the filter has been added to the filter graph, connect the AVI Out pin of the AVI Mux to the In pin of the DVCAM.AVI File Writer. The completedfilter graph should look like Figure 2-16.

Figure 2-16. The complete filter graph for DV camcorder capture and monitoring

 

When you click Play, a monitor window will open on the display, showing the live output from the camcorder, as shown in Figure 2-17.


Figure 2-17. The DV capture filter graph displaying the live output from the camcorder

 

AVI files get big very quickly, at the rate of about 250 MB per minute, so make sure you have plenty of disk space available when you run this filter graph. After you click the Stop button, an AVI file named DVCAM.AVI is on the disk. This file can be played in the Windows Media Player, or you can use the Render Media File menu item in GraphEdit to play it. You’ll see that the filter graph faithfully captured the camera’s output to disk.

 

Filter graphs created by GraphEdit can be saved to disk or opened from disk. If you choose Save Graph from the application’s File menu, you can save the filter graph to a file with a .GRF extension. GraphEdit will open .GRF files and other GraphEdit files, so if you’re tinkering with a filter graph design, you can save work in progress and return to it later.

 

Using Windows Media for File Storage

For a final example using GraphEdit, we’ll adopt a more streamlined approach to file storage. Although the high resolution of AVI files is attractive, AVI files quickly fill up an entire disk with video data. Windows Media is an alternative solution for video storage; it’s as much as several hundred times more efficient for the storage of A/V streams in disk files.

 

To convert your existing AVI-creating filter graph into one that creates Windows Media files, remove the AVI Mux and File Writer filters from the filter graph. Now you need to add a renderer filter that will create a Windows Media Advanced Streaming Format (ASF) stream. You’ll find the WM ASF Writer in the list of DirectShow filters. When you insert the filter, it will ask you to select an output file name. Use the name DVCAM.ASF for the output file. Now connect AudOut00 from the DV Splitter to the Audio Input 01 pin of the WM ASF Writer filter, and connect the Capture pin of the Smart Tee to the Video Input 01 pin of the WM ASF Writer. Intelligent Connect will automatically add a DV Video Decoder as an intermediate step between the Smart Tee and the WM ASF Writer. You should get a filter graph that looks like Figure 2-18.

 

When you click Play, the monitor window will once again show the live feed from the camcorder. After you click Stop, look for the file DVCAM.ASF. It should be much smaller. For example, a 10-second AVI file comes in at around 40 MB. A 10-second ASF file should be about 250 KB. These ASF files can be played in the Windows Media Player, or you can use the Render Media File capability inside GraphEdit.

Figure 2-18. DV capture filter graph that produces smaller ASF files

 

Setting Filter Properties

The WM ASF Writer offers a wide range of options for the degree of file compression in its output files. These options are accessible to the DirectShow programmer through COM, but they’re also available to the GraphEdit user. Right-click the WM ASF Writer and select Filter Properties. You’ll see the properties dialog box shown in Figure 2-19.

Figure 2-19. The filter properties dialog box for the WM ASF Writer renderer filter

 

In the Profile drop-down list, you can select from a broad array of compression techniques. The WM ASF Writer filter will create a 256-Kbps stream by default, but streams from 28.8 Kbps (suitable for modem users) all the way up to 1500 Kbps (for use on LANs) can be created. The greater the bandwidth, the better the image quality—and the larger the files.

 

Most DirectShow filters have properties that can be examined from GraphEdit, and each pin on a filter has its own pin properties, also accessible through a right-click of the mouse. The pin properties are interesting to look at because you can see which media types an output pin can transmit and which media types an input pin can receive. It’s an easy way to determine whether two filters are compatible. You can also examine the pin properties of connected pins, which will show you the actual media type of a pin-to-pin connection.

 

Summary

At this point, we’ve covered all the basics with GraphEdit, and you’ve created a number of DirectShow

filter graphs useful for both media playback and media capture. Now it’s time to move into the realm of

C++ programming where we can create some real-world DirectShow applications.

你可能感兴趣的:(filter,video,File,Graph,微软,output)