Visual c++.net DirectShow Programming (1)

Source: Internet
Author: User
Tags visual studio

DirectShow is a Microsoft-provided development package for streaming media processing on Windows platforms, published with the DirectX development package. DirectShow provides strong support for multimedia capture and playback. Using DirectShow, we can easily capture data from the acquisition card that supports the WDM drive model, and do the corresponding post-processing and even store it in the file. It supports a wide variety of media formats, including ASF, Mpeg, Avi, Dv, MP3, wave, etc., making the playback of multimedia data easier. In addition, DirectShow integrates technologies from other parts of DirectX (such as DirectDraw, DirectSound), directly supporting DVD playback, non-linear editing of video, and swapping with data cameras. What's more, DirectShow provides an open development environment where we can tailor our own components to our needs.

This article will introduce the application of DirectShow to an entry-level level.

Entry

DirectShow uses a model called filter graph to manage the processing of the entire data stream, each function module that participates in the data stream processing is called filter, and each filter in filter graph is connected in a certain order into an assembly line coordination work, Complete some of the relatively independent functions, such as filter can accomplish some of the following functions:

Read files

Get video from the video device

Decoding the video stream

Send data to a sound card or video card

Each filter has an input and an output port, such as a MPEG-1 decoding filter. Its input is MPEG-encoded stream data, and its output is a decoded stream data. DirectShow it is by connecting different filter together to accomplish certain functions, we call these filter connections the filter graph, and the following figure a shows the filter graph that plays AVI:


Graph a plays the graph filter graph of the AVI file

The image above each module represents different filter, media file filter from the hard drive to read AVI file, AVI separate filter files to the audio stream and video streaming, AVI decoding filter to the video stream decoding and sent to the display filter, The latter displays each frame on the monitor, and the default DirectSound device outputs the audio stream with DirectSound.

Our application does not need to manage this data stream, providing an advanced component called the Filter Graph manager in DirectShow. In our application we only need to call its APIs, such as run, stop, and so on, if you want to make further control over the data flow, you can access the filter directly through the COM interface.

The Filter Graph Manager also provides another feature: an application can control how filter graph is generated through the manager.

DirectShow application

Broadly speaking, all DirectShow applications must complete three things, as shown in Figure B below:


Figure B

1. Generates an instance of the Filter Graph manager.

2. Using the filter grapth instance to generate filter graph, which filter should be composed of filter graph depends on the needs of our application.

3. Controls the filter Graph manager's method invocation and the response of the message from filter graph to the filter graph and the data flow. The

DirectShow is COM based, and the Filter Graph Manager and filter are COM objects, and you should have a basic understanding of COM before you start.

Let's start with a simple DirectShow application in which we implement the function of opening a media file and playing it.

Setting the environment

before using DirectShow to process streaming media, the Sdk,directx SDK for DirectX must be installed properly to download on Microsoft's Web site, and currently, DirectX has the latest version of 9.0.

After the DirectX SDK is properly installed, we must set up the header and library files for the DirectX SDK to be within the search path of Visual Studio. For Visual Studio. NET 2003, you can set the following: Menu → tools → options → item →vc++ directory, add D:\DXSDK\Include to include file, add D:\DXSDK\lib in library file (my SDK installation path is D:\ DXSDK):


Header file

File name Describe
Dshow.h All DirectShow applications must contain

Library files

File name Describe
Strmiids.lib The class identification (CLSID) and Interface identity (IID) are exported in this library file, and all DirectShow applications must contain this file.
Quartz.lib Export function Amgeterrortext in this library file, if this function is called in your program, you must include this library file.

Start Engineering
Open Visual Studio. NET 2003, file → new → project

1. Generate an MFC based application with the name Playwnd.

2. Select the application type based on the dialog box, click Finish.

3. Set engineering properties

Project →playwnd properties → configuration properties → linker → input → additional dependencies, adding library files Strmiids.lib and Quartz.lib.

Since the Dshow.h header file is used in any DirectShow project, we stdafx.h add the following line: #include
4. Design dialog box, as follows:


Idc_static control type Static Text
Caption Media file name:
Idc_mediafile_edit control type Edit Control
Idc_browse_button control type Button
Caption Browse
Idc_vw_frame control type Picture control
Type Rectangle
Idc_play_button control type Button
Caption Play
Idc_pause_button control type Button
Caption Time out
IDCANCEL control type Button
Caption Shut down

5.COM initialization and Uninstall,

Modify PlayWnd.cpp Add initialization code (the added code in bold bold, below)

BOOL cplaywndapp::initinstance ()
{
If an application manifest running on Windows XP specifies that you want to
Using ComCtl32.dll version 6 or later to enable visualization,
You need InitCommonControls (). Otherwise, the window cannot be created.

InitCommonControls ();

Initializing COM interfaces

HRESULT hr = CoInitialize (NULL);

if (FAILED (HR))
{
TRACE ("Error-could not initialize COM library.\n");
return FALSE;
}

CWinApp::InitInstance ();

AfxEnableControlContainer ();

Modify PlayWnd.cpp Add uninstall COM code, note that you need to overload the virtual function ExitInstance

int Cplayfileapp::exitinstance ()
{
TODO: Add private code here and/or call base class
Turn off COM

CoUninitialize ();
return CWinApp::ExitInstance ();
}

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.