DirectShow application Quick Start

Source: Internet
Author: User
This document describes how to compile a DirectShow application. Program , Entry level Article .

I. Environment Settings

This section describes how to create a DirectShow application. You can create a console program or other visual C ++ projects in the Visual Studio environment.

Header file
All DirectShow programs use the header files in the following table.

The header file must be
Dshow. h All DirectShow programs.
Some DirectShow interfaces require other header files. You can view the reference manuals of these interfaces.

Library files
The DirectShow program uses the following library files:
Library File description
Strmiids. Lib provides the class ID (clsids) and interface representation (IIDS ). All DirectShow programs require this library file.
Quartz. Lib provides the amgeterrortext function. If you do not call this function, you can not load the library file.
The include and Lib directories of DirectX SDK can be placed first in the search path of Visual Studio. To ensure that you can use the latest version!

Introduction to DirectShow Programming
This section describes the basic terms and concepts of DirectShow programming. by reading this section, you can write a DirectShow application.

Filter Graphs)
A filter is a software component that performs operations on Multimedia Streams. For example:
Read files
Get a video from a video capture device
Decoding multiple stream formats, such as MPEG-1
Transmit data to the video card and sound card

The filter can receive input and provide output, for example, a MPEG-1 video decoding filter, which receives MPEG-encoded data streams and outputs non-compressed video image frames after processing.

In DirectShow, some of the work executed by the application is completed in a chain of filter links. Maybe the output of a filter to the next step is the input of another filter. This group of connections is called filter charts.

For example, a filter chart showing the playback AVI file is displayed.

The file source filter reads the AVI file from the hard disk. The AVI splitter filter parses the file into two data streams (compressed video streams and audio streams ). The AVI decompressor filter decodes video streams. Video rendere displays the video data (through DirectDraw or GDI ). Default directsound device filter uses directsound to broadcast audio data

Applications do not need to manage data flows. These filters are controlled by higher-level components and managed by the filter graph manager. In this way, you can use more advanced APIs to control (such as "run" and "stop"). If you want to control the flow of operations, you can also use the COM interface of the filter directly. The filter chart manager notifies the application of events.

Another purpose of the filter chart manager is to connect the filter to provide the application with a method to create a filter chart.

Write DirectShow Program

In most cases, the DirectShow application must perform the following three steps:

1. The application creates an instance of filter graph manager.
2. The application uses filter graph manager to create a filter graph. filter graph. The filter dependency and application requirements are met.
3. The application uses filter graph manager to control the filter graph and use the filter to parse and distribute data. During the entire process, the application will respond to the filter graph manager event.

After processing, the application releases the filter graph manager and all filters.
DirectShow is based on COM. Filter graph manager and filter are both COM objects. You should have a comprehensive understanding of COM programming.

Three-Playback file routine
A console application is provided to play a sound and video file. This program has only a few presidents.
The following describes a DirectShow-based application:
1. Create a filter graph manager instance.
2. Use filter graph manager to create a filter graph.
3. Run this graph.

Call coinitialize to initialize a com library.
Hresult hR = coinitialize (null );
If (failed (HR ))
{
// Add error handling here
}

Here, we skip the check on the return value. When you call any method, you should check the return value. The following code calls cocreateinstance to create a filter graph manager.
Igraphbuilder * pgraph;
Hresult hR = cocreateinstance (clsid_filtergraph, null,
Clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph );
The class ID is clsid_filtergraph. Because the filter graph manager is provided by the dynamic link library (DLL), clsctx_inproc_server is used.

Cocreateinstance returns the igraphbuilder interface. In this example, two more interfaces are required:
L imediacontrol is used to control data streams. It provides methods for stopping and starting operations.
L imediaevent can obtain the filter graph manager event. For example, you can obtain a playback completion event.

Both interfaces are provided by the filter graph manager and can be obtained through the igraphbuilder 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 chart (fileter graph ). For file playback, you only need to call one method:

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

The igraphbuilder: renderfile method creates a filter chart to play the specified file. The first parameter specifies the name of the file to be played. It is a wide character string. The second parameter is reserved by the system and must be null. If the specified file does not exist or the file format is unknown, this method fails to be called.

Now the filter chart is ready to play the file, but you must call the imediacontrol: Run method to play the file.

HR = pcontrol-> Run ();

When the filter chart starts to run, the data is played out through the filter. The playing action is performed in an independent thread. Call the imediaevent: waitforcompletion method to wait for the file to finish playing.

Long evcode = 0;

Pevent-> waitforcompletion (infinite, & evcode );

This method will not be returned until the playback of the file ends. Infinite indicates that the playback duration of the file cannot be determined. After the application finishes playing the video, release the interface pointer and close the com library.

Pcontrol-> release ();

Pevent-> release ();

Pgraph-> release ();

Couninitialize ();


The source code 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 a filter chart Manager

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 );

// Create a filter chart

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

If (succeeded (HR ))

{

// Play

HR = pcontrol-> Run ();

If (succeeded (HR ))

{

// Wait until the playback ends.

Long evcode;

Pevent-> waitforcompletion (infinite, & evcode );

}

}

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.