DirectShow Series II -- Filter Principle

Source: Internet
Author: User
Tags microsoft website

In the previous lecture, I introduced the overall system framework of DirectShow. From this point on Program From the perspective of Members, we will further discuss the DirectShow application and the development of filter.
In
Before doing so, I should first mention graphedit, a filter test tool provided by Microsoft. Its path is in dxsdk/bin/dxutils
/Graphedit.exe. (If you have not installed DirectX
Go to the Microsoft website to download the SDK .) With this tool, we can intuitively see the filter
Graph operation and processing process to facilitate program debugging. (If you have a computer at hand and you have something to wait for, try it now: Run graphedit and execute
File-> render media file... Select a media file. When the filter
After graph is successfully built, press the run button on the toolbar. You can see that the selected media file has been played back! See it. That's the case for writing a media player !)
Next, let's talk about the development of filter.
Learning
XI DirectShow
The development of filter is similar to the following methods: Reading help documentation and viewing examples Code And view the SDK base classSource code . The help document should focus on the overall concept.
The key first step is to select a suitable filter base class and a pin base class. To grasp the filter, we generally think that we should master the following
Three aspects: pin connection between filters, data transmission between filters, and random access to streaming media (or stream location ). The following sections describe them respectively.
Institute
That is, the connection between filter pin is actually the media between pin.
A negotiation process of type (media type. The connection always points from the output pin to the input pin. To gain a deeper understanding of the specific connection process, you must carefully study the SDK's basic class source code (located in
Dxsdk/samples/multimedia/DirectShow/baseclasses/amfilter. cpp, class cbasepin
Connect Method ). The general process of the connection is to enumerate all media types on the input pin to be connected, and connect them one by one with the output pin. If the output pin accepts this
Media type, the connection between the pins is declared successful. If none of the output pins of the media types enumerated in the input pin are supported, all media types on the output pin are enumerated, and use these media classes one by one
And the input pin. If the Input Pin accepts one of the media types, the connection between the pins is also successful. If all media types on the output pin are not supported
Yes, the connection process between the two pins fails.
Note that the input pin and output pin do not belong to the same filter. A typical filter is upstream filter) the output pin of the downstream filter (also called downstream filter. As shown in:

When the filter pin is connected, that is, after both parties have obtained a media type that everyone supports through negotiation, they begin to prepare for data transmission. These preparations
In work, the most important thing is the negotiation of the memory distributor on the pin, which is generally initiated by the output pin. In DirectShow
Data is transmitted through one data packet, which is called sample. Sample itself is a COM object with a memory segment for loading
The sample is centrally managed by the memory distributor (Allocator. A pair of output and input pins that have been successfully connected use the same memory distributor, so data is transmitted from the output pin
The Input Pin does not require memory copy. A typical data copy usually occurs inside the filter. After reading data from the input pin of the filter, the data is processed with certain intent.
Filter output pin to fill the data, and then continue to transfer. Next, we will elaborate on data transmission between filters.
First, we need to differentiate the two main data transmission modes of filter: Push model and pull model ). See the following figure for reference:



Institute
Push mode, that is, Source Filter (Source
Filters) can generate data by themselves, and generally there is an independent sub-thread on its output pin to send data. common situations such as the live of the WDM model acquisition card
Source
The so-called PULL mode, that is, the source filter does not have the ability to send its own data, in this case, the source filter is followed by a parser
Filter or Splitter
Filter: this filter generally has an independent sub-thread on the input pin, which is responsible for continuously retrieving data from the source filter and then transmitting the data after processing. common situations are as follows:
File Source. In push mode, the source filter is active; in PULL mode, the source filter is passive. In fact, if the source filter and Splitter In the PULL mode are
As another virtual source filter, the data transmission between the filters is also the same as the push mode.
Then, how is the data connected through the pin?
What about the transfer? First, let's look at the push mode. In the filter entry pin after the source filter, an imeminputpin interface must be implemented.
Filter calls the receive method of this interface for transmission. It is worth noting that (as mentioned above) data is transmitted from the output pin to the input through the receive method call.
On the pin, there is no memory copy, it is just a "notification" equivalent to the arrival of data ". Let's look at the PULL mode. The output pin of the Source Filter in PULL mode must implement
Iasyncreader interface; the splitter behind it
Filter is to obtain data by calling the request method or syncread method of this interface. Splitter
The filter then, like the push mode, calls the receive method of the imeminputpin interface on the input pin of the next filter to implement data transmission. Learn more about this
The SDK source code (in dxsdk/samples/multimedia/DirectShow/baseclasses)
/Source. cpp and pullpin. cpp ).
Next, let's talk about the positioning of the stream (Media
Seeking ). In graphedit, when we successfully construct a filter
After graph, we can play it. During playback, we can see that the progress bar is moving forward accordingly. Of course, we can also drag the progress bar to achieve random access. To achieve this, in the Application
The program level should be able to know how long the filter graph will play, and where it is currently played. So how is this implemented at the filter level?
Me
We know that several filters are connected by pin to form a filter graph. The filter graph is filtered by another COM object.
Graph manager. Filter graph
Manager, we can get an imediaseeking interface to locate streaming media. At the filter level, we can see that the filter
Graph manager starts from the last filter (Renderer
Filter) to check whether the output pin of the last filter supports the imediaseeking interface. If yes, this interface is returned. If not, continue to the previous step.
Level filter query until the source filter. Generally, the imediaseeking interface is implemented on the output pin of the source filter, which tells the caller how long the media has been
Content, current playback position, and other information. (If it is a file source, it is usually in the parser filter or Splitter
Filter .) For filter developers, if we write the source filter, we need to implement it on the output pin of the filter.
Imediaseeking interface; if you are writing an intermediate transmission filter, you only need to pass the user's access interface request to the output of the filter at the upper level on the output pin.
Pin; If Renderer is written
Filter, you need to pass the user's get Interface request to the output pin of the last filter on the filter. For more information, please carefully study the SDK source code (located in
Dxsdk/samples/multimedia/DirectShow/baseclasses/transfrm. cpp Class Method
Ctransformoutputpin: nondelegatingqueryinterface implementation and class in ctlutil. cpp
Cpospassthru implementation ).
The above describes how to learn about DirectShow filter development and some preparations before writing your own filter. Next, I will teach you how to write your own Filter Based on my own development experience.

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.