From: http://www.cnblogs.com/qiufa/archive/2006/12/19/596949.html
Summary : DirectShow technology is a multimedia application launched by DirectX Based on DirectDraw and directsound components. Program The Development Kit provides high-quality capture and playback of multimedia data streams, representing the future direction of multimedia application development. Describes in detail the system structure of DirectShow and discusses the key steps for developing a simple media player on the Visual C ++ 6.0 platform using DirectShow.
Keywords: DirectShow; com; filter; pin; filter chart Manager
DirectX Introduction
DirectX is a set of underlying application programming interfaces provided by Microsoft for games and other high-performance multimedia applications. These interfaces support 2D and 3D graphics, sound effects and music, input devices, and multi-player online games. Currently, the maximum version of DirectX is DirectX 9.0.
1 DirectX Composition
DirectX 9.0 consists of the following components:
(1) DirectX graphics: this component combines the DirectDraw and direct3d components in the earlier version of DirectX to form a separate application interface suitable for all graphics programs. The direct3d extension (d3dx) application library simplifies the work of most graphics programs.
(2) directinput: supports various input devices and fully supports force feedback technology.
(3) directplay: supports multi-player online games.
(4) directsound: supports the development of high-performance audio application software for playing and capturing audio waveforms.
(5) directmusic: provides a complete set of solutions for music audio tracks and non-music audio tracks created based on table, Midi (Musical Instrument Devices Interface), or other directmusic producer.
(6) DirectShow: Provides high-quality capture and playback of multimedia data streams.
(7) directsetup: a simple application interface that provides automatic installation of DirectX components.
(8) DirectX Media objects: Provides read and write support for data stream objects, including video and audio codecs and their effects.
2 com Introduction
DirectX functions are provided in the form of COM components. Com is short for the Component Object Model (Component Object Model). It is a protocol that serves as the basis for Object Linking and Embedding. Com usually exists in the form of dynamic link library (DLL), which is an object built on Binary specifications. COM defines and implements software components (such as applications, Data Objects, controls, and services) and collectively refers to them as "objects ". Each soft part object consists of data and data access functions. The function set that accesses the soft part object data is called "interface ". In the application's opinion, COM is a black box. You can call the method provided by com but do not know its specific implementation. When using DirectShow programming, the custom components created by the user must be implemented in the form of COM, so you must know how to implement COM, for general applications, you only need to understand the COM interface and usage.
DirectShow system composition
DirectShow technology is based on the DirectDraw and directsound components. It controls the video card through DirectDraw to display the video, and controls the sound card through directsound to play the sound. DirectShow provides high-quality multimedia stream capture and playback functions. It supports multiple media formats, including ASF (Advanced Systems format), MPEG (Motion Picture Experts Group ), AVI (audio-video interleaved), MP3 (MPEG audio layer-3), and WAV audio files; can capture media data streams from hardware; can automatically detect and use video and audio acceleration hardware. Therefore, DirectShow can give full play to media performance and improve the running speed. It can simplify media playback, format conversion between media, and media capturing. At the same time, it has great scalability and flexibility. You can create components by yourself and add these components to the DirectShow structure to support new formats or special effects.
The relationship between applications and the DirectShow component and the software and hardware supported by DirectShow is shown in 1.
Figure 1 DirectShow system diagram |
1. Filter)
As shown in figure 1, the filter is the most basic component of DirectShow. A filter is a COM component and is the basic unit for completing the DirectShow processing process. DirectShow provides a set of standard filters for use by programs. program developers can also create custom filters to expand the DirectShow function, but must be created in the form of COM. DirectX provides you with the DirectShow base class library. All user-defined filters can be derived from the base classes and interfaces provided by the base class library.
Filters are mainly divided into the following types:
(1) Source Filter: The Source Filter imports data into the filter chart. The data source can be files, networks, cameras, etc. Different Source filters process different types of data sources.
(2) transform filter: the function of the transform filter is to obtain the input stream, process the data, and generate the output stream. The conversion filter processes data by encoding/decoding, format conversion, compression, and decompression.
(3) Renderer Filter: The submit filter is at the last level in the filter chart. They receive data and submit the data to the peripherals.
(4) splitter filter: The splitter filter divides the input stream into multiple outputs. For example, the AVI split filter splits an AVI byte stream into a video stream and an audio stream.
(5) MUX filter: A hybrid filter combines multiple inputs into a separate data stream. For example, the AVI hybrid filter combines video streams and audio streams into an AVI byte stream.
These types of filters are not absolute. For example, an ASF reader filter is both a source filter and a segmentation filter.
In DirectShow, a set of filters are called a filter graph ). Filter charts are used to connect filters to control media streams. They can also return data to applications and search for supported filters. The filter has three possible statuses: Run, stop, and pause. Pausing is an intermediate state. The stopping State to running state must be paused. The pause can be understood as the data readiness status, designed to quickly switch to the running status. In the paused state, the data thread is started, but it is blocked by the commit filter. In general, the status of all filters in the filter chart is consistent.
2. Pin)
A filter can be connected to one or more filters. The connected interfaces are also in the form of COM, known as pins. The filter uses pins to transmit data between filters. Each pin is derived from the COM Object ipin. Each pin is a private object of the filter. The filter can dynamically create pins, destroy pins, and freely control the pin survival time. Pins can be divided into two types: Input Pin and output pin. The two connected pins must be of different types, that is, the input pin can only be connected to the output pin, the connection always points from the output pin to the input pin.
The connection between filters (that is, the connection between pins) is actually the process of media type negotiation between the two parties. The general process of the connection is: if the complete media type has been specified when the connection function is called, the connection is connected using this media type. If the connection is successful, the connection process ends; if the media type is not specified or not completely specified, the following enumeration process is used to enumerate all media types on the input pin to be connected, use these media types to connect to the output pins one by one (if the connection function provides incomplete media types, match each enumerated media type with it ), if the output pin accepts this media type, the connection between the pins is declared successful. If all media types listed on the input pins are not supported, enumerate all media types on the output pin and connect them one by one with the input pin. If the Input Pin accepts one of the media types, the connection between the pins is declared successful. If all media types on the output pins are not supported by the input pins, the connection process between the two pins fails. The connection between the filter and pin 2 is shown.
Figure 2 filter and Pin Connection |
3. Media type)
Media type is a common and extensible method used to describe digital media formats. When two filters are connected, the same media type must be used; otherwise, the two filters cannot be connected. The media type can recognize the data types that the upper-level filter transfers to the lower-level filter and classify the data.
In many applications, you do not need to worry about the media type. DirectShow handles all the details. However, some applications need to operate on media types. The media type can be am_media_type or cmediatype. The former is a structure, and the latter is a class inherited from this structure.
Each am_media_type consists of three parts: Major type, subtype, and format type. All three parts are uniquely identified using GUID (globally unique identifier. The major type mainly describes a media type, which can be video, audio, BIT data streams, or MIDI data. The subtype further refines the media type, for a video, you can further specify a RGB-24, A RGB-32, or uyvy, and so on; format type further refines the media type with a structure.
If a specific guid value is specified for each of the three parts of the media type, the media type is completely specified. If any of the three parts of the media type is guid_null, this media type is not completely specified. Guid_null is used as a wildcard.
4. filter graph Manager)
DirectShow uses the filter chart manager to control the filters in the filter chart. The filter chart manager is in the form of COM. Its functions include coordinating status changes between filters, establishing a reference clock, and transmitting events to applications; provides an application with a method to create a filter chart.
Some common filter chart manager interfaces are as follows:
Igraphbuilder: provides an application with the method to create a filter chart.
Imediacontrol: provides methods to control multimedia data streams in a filter chart, including running, pausing, and stopping.
Imediaeventex: inherits from the imediaevent interface to process filter chart events.
Ivideowindow: used to set the properties of the multimedia player window. The application can use it to set properties such as the owner, position, and size of the window.
Ibasicaudio: used to control the volume and balance of audio streams.
Ibasicvideo: Used to set video features, such as the target area and Source Area of the video.
Imediaseeking: provides methods for searching data stream locations and setting playback rates.
Imediaposition: used to locate data streams.
Ivideoframestep: Used to play video streams in a step-by-step manner. This allows DirectShow applications, including DVD players to play only one frame of video at a time.
5. Data Flow in the filter chart
To create a custom filter, You Need To Know How media data is transmitted in the filter chart. In order to transmit media data in the filter chart, the DirectShow filter must support some protocols, called transport ). The connected filter must support the same transmission protocol; otherwise, media data cannot be exchanged.
Most DirectShow filters store media data in the primary storage and submit the data to other filters through pins. This transmission is called local memory transport ). Although local storage is most commonly used in DirectShow, not all filters use it. For example, some filters transmit media data through hardware and pins are only used to submit control information, such as the ioverlay interface.
DirectShow defines two mechanisms for local memory transmission: Push model and pull model ). In push mode, the source filter generates data and submits it to the next filter. The next-level filter passively receives data and sends it to the next-level filter after processing. In PULL mode, the source filter is connected to an analysis filter. After the analysis filter requests data from the source filter, the source filter sends data to respond to the request. The push mode uses the imeminputpin interface and the PULL mode uses the iasyncreader interface. The Push mode is more commonly used.
This section describes the key steps for developing a simple Media Player Based on DirectShow.
1. initialize DirectShow
Because the DirectShow components all exist in the form of COM, you must first call the coinitializeex function to initialize the com library and embed all dynamic link libraries and resources. Otherwise, all calls to QueryInterface will fail.
2. Create a filter chart Manager interface
First, declare and initialize the required interfaces:
// DirectShow Interfaces Igraphbuilder * pgb = NULL; Imediacontrol * PMC = NULL; Imediaeventex * PME = NULL; Ivideowindow * pvw = NULL; Ibasicaudio * PBA = NULL; Ibasicvideo * pbv = NULL; Imediaseeking * PMS = NULL; Imediaposition * PMP = NULL; Ivideoframestep * PFS = NULL; Then, instantiate a filter chart manager and query each interface: // Get the interface for DirectShow's graphbuilder Cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server, Iid_igraphbuilder, (void **) & pgb ); // QueryInterface for DirectShow Interfaces Pgb-> QueryInterface (iid_imediacontrol, (void **) & PMC ); Pgb-> QueryInterface (iid_imediaeventex, (void **) & PME ); Pgb-> QueryInterface (iid_imediaseeking, (void **) & PMS ); Pgb-> QueryInterface (iid_imediaposition, (void **) & PMP ); // Query for video interfaces, which may not be relevant for audio files Pgb-> QueryInterface (iid_ivideowindow, (void **) & pvw ); Pgb-> QueryInterface (iid_ibasicvideo, (void **) & pbv ); // Query for audio interfaces, which may not be relevant for video-only files Pgb-> QueryInterface (iid_ibasicaudio, (void **) & PBA ); |
3. Create a filter chart
When using DirectShow to create a filter chart, you do not need to worry about which filters are used by the system and how the filters are connected. You can call the igraphbuilder: renderfile function to create a complete filter chart.
// Have the graph builder construct its the appropriate graph automatically Pgb-> renderfile (wfile, null ); |
After being created, the filter chart can be used to play multimedia files. DirectShow calls the imediacontrol: Run function to play media files.
// Run the graph to play the media file PMC-> Run (); |
4. Use DirectShow's Event Response Mechanism
DirectShow's Event Response Mechanism is an interface for the filter chart manager to interact with users. DirectShow can process events that can be expected in advance, such as the end of a data stream; it can also be unexpected errors. Some events can be handled by the filter chart manager, but if the filter chart manager cannot handle these events by itself, it puts event notifications in the event queue. The user program can get the event through the imediaeventex interface and process it accordingly.
5. Clear DirectShow
At the end of the program, you must call the release function to release the DirectShow interface pointer, call the couninitialize function to uninstall the com library, and release all dynamic linked libraries and resources.
Conclusion
Using DirectShow, a component of DirectX, to develop multimedia applications requires a wide range of knowledge. However, the use of DirectShow in many applications can reduce the workload and improve the running efficiency. In the development environment of Visual C ++ 6.0, you can use a simple media player developed by DirectShow to play, pause, and adjust the playback speed at random, and play multiple media files, the playback effect is very smooth. Therefore, developing multimedia applications based on DirectShow is simple and efficient, and is a recommended method.