DirectShow programming (2)-start the DirectShow journey

Source: Internet
Author: User

2. Start the DirectShow journey
The content of this chapter is mainly about some basic concepts required to compile the DirectShow application. It can be considered as an advanced introduction. To understand this content, you only need to have general programming and multimedia knowledge.
2.1. Set the compiling environment for DirectShow Development
This section describes how to compile the DirectShow application. You can use the command line to compile a project, or in the Microsoft Visual Studio integrated environment (including VC ++.
Header file:
All DirectShow applications require the dshow. h header file. For some DirectShow interfaces, the header file needs to be appended. The description of the interface depends on the actual situation.
Library file:
DirectShow uses the following library files:
The output class ID (CLSID) and Interface ID (IID) of strmiids. This library is required for all DirectShow applications.
Quartz. Lib outputs the amgeterrortext function. If this function is not called, this library is not required.
With these header files and library files, you can write DirectShow applications. However, Microsoft recommends that you use the DirectShow base class library to write filters, which greatly reduces the workload of programming. To use the DirectShow base class library, you need to compile it first. The base class library is located in the SDK samples/multimedia/DirectShow/baseclasses folder and contains two versions of the Library: release version (retail version) strmbase. lib and debug version strmbasd. lib. For more information, see "create DirectShow filter.
2.2 DirectShow Application Programming Overview
This section describes some basic terms and concepts used by DirectShow. After reading this section, you will be able to write your first DirectShow application.
Filter and filter graph
A DirectShow application is calledFilterAnd filter to perform operations on Multimedia Streams, such: reads files, obtains videos from Video Capture Devices, decodes streams of different formats such as mpeg1, and sends data to the graphics card or sound card.
Filter receives input and generates output. For example, a filter that decodes the mpeg1 video stream, inputs the video stream in mpeg1 format, and outputs a series of uncompressed video frames.
In DirectShow, the application must link these filters to implement functions, so the output of one filter becomes the input of another filter. The filters that are serialized together are calledFilter graph. For example, a filter graph of the AVI file is displayed:

The file source (async) filter reads the AVI file from the hard disk. The AVI splitter filter analyzes the file and splits it into two streams: a compressed video stream and an audio stream; avi decompressor filter decodes video frames. The video Renderer Filter displays the decoded video frames through DirectDraw or GDI. The default directsound device filter uses directsound to play audio streams.
The application does not need to manage these data streams, but controls these filters through an upper layer component named filter graph manager. Applications call upper-layer APIs such as "run" (moving data through graph) or "stop" (stopping moving data ). If you need to perform more operations on the data stream, you can directly access the filter through the COM interface. Filter graph Manager also sends Event Notifications to applications.
Another use of filter graph is to connect a filter graph together to create a filter graph.
Writing a DirectShow application requires three steps:
1. Create a filter graph manager instance
2. Use filter graph manager to create a filter graph. At this time, all necessary filters are required.
3. Use filter graph manager to control the filter graph and the stream through these filters. During this process, the application will receive the event sent by filter graph manager.
After completing these steps, the application needs to publish the filter graph manager and all filters.
2.3. Play a file
This chapter ends with this interesting example. This example is a simple console program for playing audio or video files. The program has only a few lines, but shows the powerful capabilities of DirectShow programming.
As described in the previous section, the first step is to create a DirectShow application. First, you need to call coinitialize for initialization and then call cocreateinstance to create a filter graph Manager:

Hresult hR = coinitialize (null );
If (failed (HR ))
{
Return;
}

Igraphbuilder * pgraph;
Hresult hR = cocreateinstance (clsid_filtergraph, null,
Clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph );


As shown above, the Class Identifier (CLSID) is clsid_filtergraph. Filter graph manager is provided by In-process DLL. Therefore, the value of dwclscontext in parameter 3 is clsctx_inproc_server. Because DirectShow runs the free-threading model, you can also use the coinit_multithreaded parameter to call coinitializeex.
The second step is to create a filter graph. The igraphbuilder interface obtained by calling cocreateinstance contains most of the methods for creating a filter graph. In this example, two more interfaces are required: imediacontrol and imediaevent.
Imediacontrol controls the data stream. It contains methods for enabling and stopping the graph. imediaevent contains methods for obtaining events from the filter graph manager. In this example, this interface is used to obtain the Playback End event.
All these interfaces are provided by the filter graph manager and obtained through the obtained igraphbuiler interface pointer.

Imediacontrol * pcontrol;
Imediaevent * pevent;
HR = pgraph-> QueryInterface (iid_imediacontrol, (void **) & pcontrol );
HR = pgraph-> QueryInterface (iid_imediaevent, (void **) & pevent );

Now you can create a filter graph. For file playback, you only need a simple call:

HR = pgraph-> renderfile (L "C: // example. Avi", null );


Igraphbuilder: The renderfile method creates a filter graph that can play a specified file. In fact, some of the work that needs to be done originally, such as creating a filter instance and connecting these filters, this method is automatically used. If it is a video file, the filter graph should look like this:
-> [Here is a decoder for the thumbnail]-> [video Renderer]
To start playback, call the imediacontrol: Run method:

HR = pcontrol-> Run ();

When the filter graph is running, the data is played back as video or audio after each filter. Playback occurs in a separate thread. You can wait until the playback ends by calling the imediaevent: waitforcompletion method:

Long evcode = 0;
Pevent-> waitforcompletion (infinite, & evcode );

This method is blocked during playback until the playback ends or times out.
When the application ends, you need to release the interface pointer and close the com Library:

Pcontrol-> release ();
Pevent-> release ();
Pgraph-> release ();
Couninitialize ();

The complete code for this example is as follows:

# Include <dshow. h>
Void main (void)
{
Igraphbuilder * pgraph = NULL;
Imediacontrol * pcontrol = NULL;
Imediaevent * pevent = NULL;

// Initialize the com library.
Hresult hR = coinitialize (null );
If (failed (HR ))
{
Printf ("error-cocould not initialize com library ");
Return;
}

// Create the filter graph manager and query for interfaces.
HR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server,
Iid_igraphbuilder, (void **) & pgraph );
If (failed (HR ))
{
Printf ("error-cocould not create the filter graph manager .");
Return;
}

HR = pgraph-> QueryInterface (iid_imediacontrol, (void **) & pcontrol );
HR = pgraph-> QueryInterface (iid_imediaevent, (void **) & pevent );

// Build the graph. Important: change this string to a file on your system.
HR = pgraph-> renderfile (L "C: // example. Avi", null );
If (succeeded (HR ))
{
// Run the graph.
HR = pcontrol-> Run ();
If (succeeded (HR ))
{
// Wait for completion.
Long evcode;
Pevent-> waitforcompletion (infinite, & evcode );

// Note: Do not use infinite in a real application, because it
// Can block indefinitely.
}
}
Pcontrol-> release ();
Pevent-> release ();
Pgraph-> release ();
Couninitialize ();
}

 

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.