DirectShow programming (3.3)-about DirectShow-building filter graph

Source: Internet
Author: User

3.3. Construct a filter graph
3.3.1. component used to build graph

DirectShow provides a series of components used to build the filter graph, including:
* Filter graph Manager. This object is used to control the filter graph and supports igraphbuilder, imediacontrol, imediaeventex, and many other interfaces. All DirectShow applications need to use this object in some places, although in some cases, it is another object that creates filter graph manager for the application.
* Capture graph Builder. This object provides additional methods for building a filter graph. It was initially designed for building a graph that provides video collection (this is where it comes from), but it is also useful for building many other types of filter graphs. It supports the icapturegraphbuilder2 interface.
* Filter Mapper and system device enumerator. These objects are used to find the filters registered in the system or representing the hardware driver.
* DVD graph Builder. This object is used to build a filter graph for playback and navigation of DVDs. It supports the idvdgraphbuilder interface. Script-based applications can use the mswebdvd ActiveX Control to control DVD playback.
* Video control. WINXP provides this ActiveX Control for manipulating data in DirectShow and analog TV.

Intelligent connect)
The term smart connection covers a series of filter graph manager algorithms used to build all or some filter graphs. At any time, when the filter graph manager needs to add a filter to complete the graph, it roughly does the following:
1. If a filter exists in graph and the filter has at least an unconnected input pin, filter graph manager tries to use the filter.
2. Otherwise, filter graph manager can accept a suitable media filter when searching for connections in the registered filter. Each filter registers a merit value, which is used to mark which filter is most easily selected by the filter graph manager to complete the graph. Filter graph manager selects the filter value in the Order of Merit values. The larger the merit value, the larger the chance of being selected. For each stream type (such as audio, video, and midi), the default Renderer has a high merit value. The decoder is also a dedicated filter with a low merit value.
If the filter graph manager is sleepy because the selected filter is not suitable, it will return to try another filter combination.

3.3.2 grap construction Overview
Create a filter graph and start from creating a filter graph manager instance:

Igraphbuilder * pigb;
Hresult hR = cocreateinstance (clsid_filtergraph,
Null, clsctx_inproc_server, iid_igraphbuilder, (void **) & pigb );

Filter graph Manager supports the following graph construction methods:
* Ifiltergraph: connectdirect, Directly connect between two pins. If the connection fails, return failure.
* Ifiltergraph: connect, Connect two pins. If possible, connect them directly. Otherwise, add other filters in the middle to complete the connection.
* Igraphbuilder: renderTo build the remaining graph from an output pin. This method automatically adds the required filter after the output pin until the Renderer Filter.
* Igraphbuilder: renderfileTo build a complete file playback graph.
* Igraphbuilder: addfilterTo add a filter to graph. It does not connect to the filter. Before calling this method, the filter must have been created. You can use the cocreateinstance method or filter er or system device enumerator to create a filter ).
These methods provide three ways to build a graph:
1. filter graph manager to build the entire Graph
2. filter graph manager build part of Graph
3. The application builds the entire graph.

Filter graph manager to build the entire Graph
If you only want to play back a file in a known format, such as Avi, MPEG, WAV, or MP3, use the renderfile method.
The renderfile method first looks for the filter that can be registered in the system to analyze the source file. It uses the protocol name (such as http ://), the file extension or the first few bytes of the file determine which source filter to select.
Filter graph Manager uses an iteration process to build the remaining graph. In this iteration, it lists the media types supported by the output pin of the filter one by one, and searches for the input pin of the registered filter to accept the media type. It uses a series of rules to narrow the filter range and prioritize:
* General functions of filters identified by the filter category (category)
* Media type description the data type that the filter can accept or output
* The merit value determines the order in which the filter is tried. If two filters have the same filter category and support the same input type at the same time, filter graph manager selects the one with a large merit value. Some filters intentionally give a small merit value because they are designed for special purposes and can only be added to the graph by applications.
Filter graph Manager uses the filter mapper object to search for registered filters.
When each filter is added, filter graph manager tries to connect it to the output pin of the previous filter. They negotiate to determine whether they can connect, and if so, which media type is used for connection. If the new filter cannot be connected, filter graph manager discards it and tries to separate it. This process continues until every stream is render.

Filter graph manager build part Graph
If you do not just play a file, your application must do some graph construction work. For example, a video collection application must first select a source filter and add it to graph. If you want to write data to an AVI file, you must add an avi mux and file write filter. However, it is often possible that the filter graph manager can complete the entire graph. For example, you can use the render method to render a pin for preview.

The application builds the entire Graph
In some cases, your application needs to add and connect each filter to build a graph. In this case, you may clearly know which filters need to be added to the graph. In this way, the application adds each filter by calling the addfilter method, enumerates the pin on the filter, and CALLS connect or connectdirect to connect to them.

3.3.3. Smart connection
Smart connection is a mechanism used by filter graph manager to build filter graph. It contains a series of related algorithms used to select filters and add them to the graph. As an application developer, you do not need to understand the details of smart connection. If you encounter problems when building a filter graph and want to solve it, or you are writing your own filter and want it to be automatically built by graph, read this section.
Smart connection involves the following igraphbuilder methods:
* Igraphbuilder: render
* Igraphbuilder: addsourcefilter
* Igraphbuilder: renderfile
* Igraphbuilder: connect
The render method is used to construct a part of the graph. It starts from an unconnected output pin and follows the direction of the data stream. Add the necessary filter, and the filter starting from must have been added to the graph. The render method searches for a filter that can be connected to the previous filter. If the newly connected filter has multiple output pins, the data stream can be automatically diverted until each stream is Renderer. If the filter searched by the render method cannot be used, it will return to try another filter.
To connect each output pin, the render method does the following:
1. If the pin supports the istreambuilder interface, filter graph Manager enables the istreambuilder: render method of the pin to complete the entire process. By exposing this interface, the pin undertakes all the work of building the remaining part of the graph. However, only a few filters support this interface.
2. filter graph manager tries to use any filter in the cache. During the entire smart connection process, filter graph manager can cache the filter in the early stages.
3. If the filter graph contains any filter with an unconnected input pin, the filter graph Manager uses it as the next filter to try to connect. You can force the render method to try this filter by adding a specific filter before calling render.
4. finally, filter graph Manager uses the ifiltermapper2: enummatchingfilters method to search for all registered filters, match each media type of the output pin one by one based on the List of Registered media types (sorted by priority ).
Each registered filter has a merit value. This is a number used to indicate the filter priority. The higher the maximum priority, the filter set returned by the enummatchingfilters method is arranged based on the merit value, until the smallest merit value is merit_do_not_use + 1, it ignores merit as merit_do_not_usr or a smaller filter. Filters are also classified by guid, and the category itself also has merit values. The enummatchingfilters method ignores any merit value as merit_do_not_use or smaller, even if the filter in that category has a higher merit value.
To sum up, follow these steps to try filter in the render method:
1. Use istreambuilder
2. Try the cached Filter
3. Try to add a filter in graph
4. Search
Addsourcefilter adds a source filter that can render a specific file. First, it searches for the matched one in the registered filter based on the protocol name (such as http: //), file extension, or file header. If this method finds a suitable source filter, it immediately creates an instance of the filter, adds it to the graph, and then calls the ifilesourcefilter: Load Method of the filter.
The renderfile method constructs a default playback Graph Based on a file name. Inside the renderfile method, the addsourcefilter method is called to locate the source filter and render is used to construct the remaining part of the graph.
The Connect Method connects the output pin to the input pin. This method automatically adds the necessary intermediate filter to graph and uses the series of algorithms described in the render method:
1. Use istreambuilder
2. Try the cached Filter
3. Try to add a filter in graph
4. Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.