DirectShow streaming media data collection and image capturing

Source: Internet
Author: User
Tags types of filters
Streaming Media Processing has always been widely concerned by the industry for its complexity and technology. With the popularity of the Internet, streaming media has been widely used on the network. How to Make Streaming Media Processing simple and effective has gradually become the focus. Choosing an appropriate application solution will get twice the result with half the effort. At this time, Microsoft DirectShow gave us a good choice.

DirectShow is a new generation of com-based Streaming Media Processing Development Kit launched by Microsoft Based on activemovie and video for Windows. It is released together with the DirectX development kit. Currently, the latest version of DirectX is 9.0. DirectShow provides powerful support for capturing and playback of Multimedia Streams. Using DirectShow, we can easily capture data from the acquisition card that supports the WDM driver model, and perform corresponding post-processing and even storage to files. This makes it easier to access multimedia data in the multimedia database management system (mdbms.

DirectShow principles and important interfaces

1. how DirectShow works

1) system structure of DirectShow

The architecture 1 of DirectShow is shown in.


Figure 1 DirectShow System


DirectShow is located in the application layer. It uses a model called filter graph to manage the processing process of the entire data stream. The functional modules involved in data processing are called filter; each filter is connected to a "Pipeline" in a certain order in the filter graph. Filters are divided into three categories by function: Source filters, transform filters, and rendering filters. Source filters is mainly responsible for obtaining data. The data source can be a file, an Internet, a computer acquisition card, a digital camera, etc. Then, the data is transmitted down; transform fitlers is mainly responsible for data format conversion and transmission; rendering filtes is mainly responsible for the final destination of data. We can send the data to the sound card and video card for Multimedia demonstration, it can also be output to files for storage.

In the DirectShow system, what we see is our application.Program(Application ). The application needs to establish the corresponding filter graph according to certain intentions, and then control the entire data processing process through the filter graph manager. DirectShow can receive various events when the filter graph is running and send messages to our applications. In this way, the interaction between the application and the DirectShow system is realized.

2) filter overview and Connection

Filter is the most basic concept in DirectShow. DirectShow uses filter graph to manage filters. Filter graph is the "Container" of filter, while filter is the smallest functional module in filter graph. Filter is a COM component. Each filter has its own PIN, which is a COM object created by the filter. Filter uses a pin to connect them. There are two types of Pin: Output pin and input pin. The output pin transmits the data processed by the filter to the external filter, and the input pin receives the data from the external filter to the filter, so that the filter can process the data. The connection diagram of three types of filters (Source Filter, transform filter, and rendering filter) is as follows:


Figure 2 filter connection

2. how DirectShow supports hardware

As you know, the Windows operating system isolates hardware operations to improve system stability. Applications generally cannot directly access the hardware. DirectShow filter works in user mode (the operating system privilege level is ring 3), while the hardware works in kernel mode (the operating system privilege level is ring 0 ), the DirectShow solution is to design a packaging filter for these hardware. This filter can work in user mode, and its appearance and control methods are the same as ordinary filters, the packaging filter completes interaction with the hardware driver. This design frees developers who write DirectShow applications from the special processing required to support hardware. DirectShow has integrated packaging filters, including audio capture filte (qcap. DLL), VFW capture filter (qcap. DLL, the filter class ID is clsid_vfwcapture), TV tuner filter (kstvtune. ax, the filter class ID is clsid_ctvtunerfilter), analog video crossbar filter (ksxbar. ax), TV audio filter (the class ID of the filter is clsid_tvaudiofilter), etc. In addition, DirectShow designed ksproxy filter (ksproxy) for the hardware using the WDM driver. ax ,). The structure of the interaction between each packaging filter and hardware:

Applications
DirectShow filter graph
Kstune. Ax Ksxbar. Ax Kscap. Ax Other common Filters
Stream class
Tuner Minidriver Crossbar mindriver Capture Minidriver Tuner, crossbar, capture Minidriver

3. Important DirectShow Interfaces

DirectShow adopts the com standard, so many important functions are completed through the COM interface. The following lists some important DirectShow interfaces.

(1) igraphbuilder Interface

Used to construct the filter graph interface, create and manage a series of filters, filter and process source media streams.

(2) imediacontrol Interface

Controls the flow of Multimedia Streams in the filter chart, such as starting and stopping a stream.

(3) imediaevent Interface

Capture events that occur during playback and notify applications, such as ec_complete.

(4) ivideowindow Interface

Controls the attributes of a video window.

(5) imeadiaseeking Interface

APIs used to find media, locate streaming media, and control the playback of multimedia data to provide precise control.

(6) ibasefilter Interface

Inherited from the imediafilter interface, it is used to define a specific filter pointer and process multimedia data.

(7) ipin Interface

Used to manage the pin between two filters to connect to the filter.

(8) isamplegrabbercb Interface

It is an interface of the sample grabber filter. It is used to sample streaming media data through the filter to obtain frame images.

 

When using DirectShow to use a camera, it is generally required that the camera driver be In the WDM format. Of course, some older driver formats, DirectShow, are also supported. In DirectShow, there is a sample grabber filter, which is a filter that can be inserted into the stream. It has its own buffer and stores samples. We can use it to easily obtain a seek from a video file. DirectShow uses the filter graph manager to communicate with upper-layer applications and lower-layer drivers. DirectShow uses the capture filter to capture the camera. A capture filter has multiple pins, And the preview plug can be used to display the preview image.

1. Create a filter graph

As described above, you must first create a filter graph:

Ccomptr <igraphbuilder> m_pgraph;
HR = m_pgraph.cocreateinstance (clsid_filtergraph );

2. Connect the device

You also need to create a system enumerator component object:

Ccomptr <icreatedevenum> pcreatedevenum; pcreatedevenum. cocreateinstance (clsid_systemdeviceenum );

Then, use createclassenumerator () to create an enumerator for the specified filter registration type directory and obtain the ienummoniker interface:

Ccomptr <ienummoniker> PEM;
Pcreatedevenum-> createclassenumerator (clsid_videoinputdevicecategory, & PEM, 0 );

After calling bindtoobject (), you can generate a DirectShow filter for the device identifier and add it to the filter graph to participate in the work.

Ccomptr <imoniker> PM;
Ccomptr <ipropertybag> pbag;
HR = PM-> bindtostorage (0, 0, id_ipropertybag, (void **) & pbag );

3. Create a sample grabber filter.

Ccomptr <isamplegrabber> m_pgrabber
HR = m_pgrabber.cocreateinstance (clsid_samplegrabber );

After samplegrabber is created, you must configure it before the sample grabber filter is connected to another filter. Then query the isamplegrabber interface and set the streaming media type:

M_pgrabber-> setmediatype ();

You can only specify the primary media type, primary media type, or primary media type, submedia type, and Type format. Then load it to filtergraph:

M_pgraph-> addfilter (pgrabbase, "Grabber ");

4. Search for the filter graph pin and complete subsequent connections.

Next, you can call the findpin () interface of igraphbuilder to find the pin interface in the filter manager, and use renderstream () in icapturegraphbuilder2 to complete subsequent connections.

HR = pcgb2-> findpin (pcap, pindir_output, & pin_category_videoport, null, false, 0, & pvppin );
HR = pcgb2-> renderstream (& pin_category_capture, & mediatype_video, pcap, pgrabbase, prenderer );

5. Obtain and run the streaming media type

After obtaining the connected streaming media type through getconnectedmediatype (), we can use the isamplegrabbercb class interface buffercb () to copy the video data to the custom buffer, then, you can copy the video to the image data by copying the video in the buffer zone. Last run ﹕

Ccomqiptr <imediacontrol, & iid_imediacontrol> pcontrol = m_pgraph;
HR = pcontrol-> Run ();

Conclusion

This article discusses the basic principles of DirectShow, the basic method for creating a filter graph, and the use of DirectShow to capture video data, and then save it as the desired image, the multimedia database management system is a very beneficial supplement. For example, the software system for examination registration is highly adaptive and can reduce development costs. Improve user practicability.

DirectShow is an effective method for developing multimedia. In the next few years, the development prospects of DirectShow technology are quite broad. Mastering DirectShow technology will be of great practical significance.

Related Article

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.