Video Program Development Based on directshow Platform

Source: Internet
Author: User
Tags error 0x80070005

1) first knowledge of DirectShow (2009.12.05)

To create a DirectShow application, follow these steps:

First, you need to call coinitialize for initialization, and then call cocreateinstance to create 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 );

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.

PS: hresult is a simple data type, which is usually used by attributes and ATL as return values.

The following table describes different values. The header file winerror. h contains more values.

Description

S_ OK operation successful 0x00000000

E_unexpected: unexpected failure 0x8000ffff

E_notimpl is not 0x80004001

E_outofmemory failed to allocate the required memory 0x8007000e

One or more e_invalidarg parameters are invalid: 0x80070057

E_nointerface does not support the interface 0x80004002

Invalid e_pointer pointer 0x80004003

Invalid e_handle handle 0x80070006

E_abort operation suspended 0x80004004

Failure 0x80004005 not specified by e_fail

E_accessdenied error 0x80070005

The returned values cannot be compared with s_ OK and s_false, but must be determined using succeeded and failed macros.

Step 2: Create a filter graph

The igraphbuilder interface obtained by calling cocreateinstance contains most of the filter graph creation methods. 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 );

Step 3: Create a filter graph

Only one simple call is required for file playback:

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

The above 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 creating a simple DirectShow player process 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 ();
}

PS:

1. The fiter graph manag used here is used to construct a graph Filter Using the igraphbuilder: renderfile method to build a complete file playback graph.

2. This program only supports video playback in. AVI format, and only supports playback up to the end.

 

2. Add "dependency", "library directory", and "include directory" to the vs2005 C ++ Project
1. Add the Lib file required for compilation.

Enter "Winsock. lib" in "Project-> property-> Configuration property-> connector-> input-> additional dependency". Separate multiple lib with spaces.
(Equivalent to the "# pragma comment (Lib," Winsock. lib ")" Statement)

2. Add the Library (libs) file directory

Method 1 "Project-> properties-> Configuration properties-> connector-> General-> additional library directory"
Method 2: [menu] "tools-> options-> projects and solutions-> C ++ directory", select the corresponding platform, and add the required "library files" Directory

3. Add the include file directory

Method 1: [solution Resource Manager] "project-> properties-> Configuration properties-> C/C ++-> General-> Add include Directory"
Method 2: [menu] "tools-> options-> projects and solutions-> C ++ directory", select the corresponding platform, and add the required "include files" Directory

 

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.