Obtain Filter Based on CLSID

Source: Internet
Author: User

 Coinitialize (null );

Hresult hr;

Igraphbuilder * pgraphbuilder = NULL;

Cocreateinstance (clsid_filtergraph, null, clsctx_inproc, iid_igraphbuilder, (void **) & pgraphbuilder );

ibasefilter * pmpeg4psfilestream;
ibasefilter * pmpegdemuxfilter;
ibasefilter * pmpeg4videodecoder;
ibasefilter * poverlaymaxer2;
ibasefilter * pvideorenderer;
hR = cocreateinstance (clsid_mpeg4psfilestream, null, clsctx_inproc_server, iid_ibasefilter, (void **) & pmpeg4psfilestream);
If (failed (HR)
afxmessagebox ("Err create clsid_mpeg4psfilestream");

HR = cocreateinstance (clsid_mpegdemuxfilter, null, clsctx_inproc_server, iid_ibasefilter, (void **) & pmpegdemuxfilter );

If (failed (HR ))

Afxmessagebox ("Err create clsid_mpegdemuxfilter ");

HR = cocreateinstance (clsid_mpeg4videodecoder, null, clsctx_inproc_server, iid_ibasefilter, (void **) & pmpeg4videodecoder );

If (failed (HR ))

Afxmessagebox ("Err create clsid_mpeg4videodecoder ");

HR = cocreateinstance (clsid_overlaymaxer2, null, clsctx_inproc_server, iid_ibasefilter, (void **) & poverlaymaxer2 );

If (failed (HR ))

Afxmessagebox ("Err create clsid_overlaymaxer2 ");

HR = cocreateinstance (clsid_videorenderer, null, clsctx_inproc_server, iid_ibasefilter, (void **) & pvideorenderer );

If (failed (HR ))

Afxmessagebox ("Err create clsid_videorenderer ");

Pgraphbuilder-> addfilter (pmpeg4psfilestream, null );

Pgraphbuilder-> addfilter (pmpegdemuxfilter, null );

Pgraphbuilder-> addfilter (pmpeg4videodecoder, null );

Pgraphbuilder-> addfilter (poverlaymaxer2, null );

Pgraphbuilder-> addfilter (pvideorenderer, null );

// Set the source file

Ifilesourcefilter * ploader;

Pmpeg4psfilestream-> QueryInterface (iid_ifilesourcefilter, (void **) & ploader );

HR = ploader-> load (L "E: \ temp \ mpeg4_ps.mpg", null );

If (failed (HR ))

Afxmessagebox ("file loading error! ");

// connect filter
char * strpinname = new char;
ienumpins * enumpins;
ipin * outpin;
ipin * inpin;
ulong fetched;
pin_info pinfo;
// find source output
pmpeg4psfilestream-> enumpins (& enumpins );
enumpins-> Reset ();
enumpins-> next (1, & outpin, & fetched ); // only 1 pin for source, so we know this is the one we need
enumpins-> release ();

// Find Demux filter input

Pmpegdemuxfilter-> enumpins (& enumpins );

Enumpins-> Reset ();

Enumpins-> next (1, & inpin, & fetched );

Inpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release (); // make sure you release the returned ibasefilter Interface

While (pinfo. dir = pindir_output) // check if we have wrong pin (not input pin), 2 output pin

{

Inpin-> release ();

Enumpins-> next (1, & inpin, & fetched); // If so, get next pin

Inpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release ();

}

// Connect -- 1

Pgraphbuilder-> connect (outpin, inpin );

Inpin-> release ();

Outpin-> release ();

// Find Demux filter output

Enumpins-> Reset ();

Enumpins-> next (1, & outpin, & fetched );

Outpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release ();

If (pinfo. dir = pindir_input) // check if we have wrong pin (not input pin), only 1 Input Pin

{

Inpin-> release ();

Enumpins-> next (1, & outpin, & fetched); // If so, get next pin

}

Enumpins-> release ();

// Find vweb MPEG4 Video Decoder Input

Pmpeg4videodecoder-> enumpins (& enumpins );

Enumpins-> Reset ();

Enumpins-> next (1, & inpin, & fetched );

Inpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release (); // make sure you release the returned ibasefilter Interface

If (pinfo. dir = pindir_output) // check if we have wrong pin (not input pin), only 1 Input Pin

{

Inpin-> release ();

Enumpins-> next (1, & inpin, & fetched); // If so, get next pin

}

// Connect -- 2

Pgraphbuilder-> connect (outpin, inpin );

Inpin-> release ();

Outpin-> release ();

// Find vweb MPEG4 Video Decoder output ******************************** *********

Enumpins-> Reset ();

Enumpins-> next (1, & outpin, & fetched );

Outpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release (); // make sure you release the returned ibasefilter Interface

If (pinfo. dir = pindir_input) // check if we have wrong pin (not input pin), only 1 Input Pin

{

Outpin-> release ();

Enumpins-> next (1, & outpin, & fetched); // If so, get next pin

Outpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release ();

Wchar * s = pinfo. achname;

}

Enumpins-> release ();

// Find overlay mixer2 Input

Poverlaymaxer2-> enumpins (& enumpins );

Enumpins-> Reset ();

Enumpins-> next (1, & inpin, & fetched );

Inpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release (); // make sure you release the returned ibasefilter Interface

Wchar * s = pinfo. achname;

If (pinfo. dir = pindir_output) // check if we have wrong pin (not input pin), 2 Input Pin

{

Inpin-> release ();

Enumpins-> next (1, & inpin, & fetched); // If so, get next pin

}

// Connect -- 3

Pgraphbuilder-> connect (outpin, inpin );

Inpin-> release ();

Outpin-> release ();

// Find overlay mixer2 output

Enumpins-> Reset ();

Enumpins-> next (1, & outpin, & fetched );

Outpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release (); // make sure you release the returned ibasefilter Interface

While (pinfo. dir = pindir_input) // check if we have wrong pin (not input pin), 2 Input Pin

{

Outpin-> release ();

Enumpins-> next (1, & outpin, & fetched); // If so, get next pin

Outpin-> querypininfo (& pinfo );

Pinfo. pfilter-> release ();

Wchar * s = pinfo. achname;

}

Enumpins-> release ();

// Find Renderer Input

Pvideorenderer-> enumpins (& enumpins );

Enumpins-> Reset ();

Enumpins-> next (1, & inpin, & fetched); // Renderer has only 1 pin, so this is the pin we need

Enumpins-> release ();

// Connect -- 4

Pgraphbuilder-> connect (outpin, inpin );

Inpin-> release ();

Outpin-> release ();

Imediaevent * pevent;

Imediacontrol * pmediacontrol;

// Create the filter graph manager and query for interfaces.



HR = pgraphbuilder-> QueryInterface (iid_imediacontrol, (void **) & pmediacontrol );

HR = pgraphbuilder-> QueryInterface (iid_imediaevent, (void **) & pevent );



// Run the graph.

Pmediacontrol-> Run ();

// Wait for completion.

Long evcode;

Pevent-> waitforcompletion (infinite, & evcode );

// Clean up.

Pmpeg4psfilestream-> release ();

Pmpegdemuxfilter-> release ();

Pmpeg4videodecoder-> release ();

Poverlaymaxer2-> release ();

Pvideorenderer-> release ();

Pmediacontrol-> release ();

Pevent-> release ();

Pgraphbuilder-> release ();

Delete strpinname;

Couninitialize ();

Long-gunCodeAll of them are included, but I have heard that the filters not in DirectShow filter must be enumerated before they can be used normally, but I have not tried it. I don't know if it is. The following code is used to enumerate the Capture Devices.

Hresult ccaptureex: findcapturedevice (ibasefilter ** ppsrcfilter)

{

Hresult hr;

Ibasefilter * psrc = NULL;

Ccomptr <imoniker> pmoniker = NULL;

Ulong cfetched;

If (! Ppsrcfilter)

Return e_pointer;



// Create the system device enumerator

Ccomptr <icreatedevenum> pdevenum = NULL;

HR = cocreateinstance (clsid_systemdeviceenum, null, clsctx_inproc,

Iid_icreatedevenum, (void **) & pdevenum );

If (failed (HR ))

{

MSG (text ("couldn't create system enumerator! HR = 0x % x "), HR );

Return hr;

}

// Create an enumerator for the Video Capture Devices

Ccomptr <ienummoniker> pclassenum = NULL;

HR = pdevenum-> createclassenumerator (clsid_videoinputdevicecategory, & pclassenum, 0 );

If (failed (HR ))

{

// MSG (text ("couldn't create class enumerator! HR = 0x % x "), HR );

Return hr;

}

// If there are no enumerators for the requested type, then

// Createclassenumerator will succeed, but pclassenum will be null.

If (pclassenum = NULL)

{

Return e_fail;

}

// Use the first video capture device on the device list.

// Note that if the next () call succeeds but there are no monikers,

// It will return s_false (which is not a failure). Therefore, we

// Check that the return code is s_ OK instead of using succeeded () Macro.

If (s_ OK = (pclassenum-> next (1, & pmoniker, & cfetched )))

{

// Bind Moniker to a filter object

HR = pmoniker-> bindtoobject (0, 0, iid_ibasefilter, (void **) & psrc );

If (failed (HR ))

{

// MSG (text ("couldn't bind Moniker to filter object! HR = 0x % x "), HR );

Return hr;

}

}

Else

{

// MSG (text ("unable to access video capture device! "));

Return e_fail;

}

// Copy the found filter pointer to the output parameter.

// Do not release () the reference, since it will still be used

// By the calling function.

* Ppsrcfilter = psrc;

Return hr;

}

 /Obtain the imoniker pointer Based on the displayname of the filter, and bind the imoniker pointer to an ibasefilter pointer.

Hresult cdxgraph: displaynametomoniker (wchar * szdisplayname, ibasefilter ** ppbf)

{

If (! Mgraph |! Szdisplayname |! Ppbf)

Return e_pointer;

Ibasefilter * pfilter = NULL;

Ibindctx * pbindctx;

Hresult hR = createbindctx (0, & pbindctx );

Ulong cheaten = 0;

Ccomptr <imoniker> pmoniker = NULL;

HR = mkparsedisplayname (pbindctx, szdisplayname, & cheaten, & pmoniker );

Pbindctx-> release ();

If (succeeded (HR)

{< br>
hR = pmoniker-> bindtoobject (null, null, iid_ibasefilter, (void **) & pfilter);
If (failed (HR ))

{< br>
afxmessagebox (text ("couldn't bind Moniker to filter object! ");
return e_fail;

}

// Copy the found filter pointer to the output parameter.

// Do not release () the reference, since it will still be used

// By the calling function.

* Ppbf = pfilter;

Return hr;

}

Return hr;

}

// use Xvid mpge4 code. szdisplay stores the displayname of the filter, you can view it in graphedit again
wchar szdisplay [] = l "@ device: Cm: {33d9a760-90c8-11d0-bd43-00a0c911ce86} \ Xvid ";
If (succeeded (mfiltergraph-> displaynametomoniker (szdisplay, & pvideocompressfilter )))

{< br>
If (succeeded (mfiltergraph-> getgraph ()-> addfilter (pvideocompressfilter, l "Xvid MPEG-4 codec ")))

{< br>
// pvideocompressfilter-> release ();

}

// Obtain the capture Source Filter and friendlyname for video collection.

Hresult cdxgraph: enumvideocapturesource (ibasefilter ** ppbf, wchar * szfiltername)

{

If (! Mgraph |! Ppbf)

Return e_pointer;

// Create the system device enumerator.

Ibasefilter * pfilter = NULL;

Hresult hr;

Ccomptr <icreatedevenum> psysdevenum = NULL;

HR = cocreateinstance (clsid_systemdeviceenum, null, clsctx_inproc_server,

Iid_icreatedevenum, (void **) & psysdevenum );

If (failed (HR ))

{

Return hr;

}

// Obtain a class enumerator for the video capture source category.

Ccomptr <ienummoniker> penumcat = NULL;

HR = psysdevenum-> createclassenumerator (clsid_videoinputdevicecategory, & penumcat, 0 );

If (failed (HR ))

{

Afxmessagebox ("create class enumerator for the video capture failed .");

Return hr;

}

// If there are no enumerators for the requested type, then

// Createclassenumerator will succeed, but pclassenum will be null.

If (penumcat = NULL)

{

Cstring STR ("");

STR + = text ("no video capture device was detected. \ r \ n ");

STR + = text ("this sample requires a video capture device, such as a USB webcam, \ r \ n ");

STR + = text ("this sample requires a video capture device, such as a USB webcam, \ r \ n ");

STR + = text ("this sample requires a video capture device, such as a USB webcam, \ r \ n ");

STR + = text ("to be installed and working properly. The sample will now close .");

Afxmessagebox (STR );

Return e_fail;

}

// Use the first video capture device on the device list.

// Note that if the next () call succeeds but there are no monikers,

// It will return s_false (which is not a failure). Therefore, we

// Check that the return code is s_ OK instead of using succeeded () Macro.

Ccomptr <imoniker> pmoniker = NULL;

Ulong cfetched;

If (penumcat-> next (1, & pmoniker, & cfetched) = s_ OK)

{

Ipropertybag * ppropbag;

HR = pmoniker-> bindtostorage (0, 0, iid_ipropertybag,

(Void **) & ppropbag );

If (succeeded (HR ))

{

// To retrieve the filter's friendly name, do the following:

Variant varname;

Variantinit (& varname );

HR = ppropbag-> Read (L "friendlyname", & varname, 0 );

If (succeeded (HR ))

{

// Display the name in your UI somehow.

// Use the uses_conversion statement to convert functions such as w2a (), a2w (), t2ole (), and ole2t ().

Uses_conversion;

// The value of varname. bstrval is a local variable. You need to copy the data to the array variable.

// If a pointer is used to point to a local variable, an error occurs.

WsprintfW (szfiltername, varname. bstrval );

// Afxmessagebox (w2a (szfiltername ));

}

Variantclear (& varname );

Ppropbag-> release ();

// To create an instance of the filter, do the following:

HR = pmoniker-> bindtoobject (null, null, iid_ibasefilter, (void **) & pfilter );

If (failed (HR ))

{

Afxmessagebox (text ("couldn't bind Moniker to filter object! "));

Return hr;

}

}

Else

{

Afxmessagebox (text ("unable to access video capture device! "));

Return e_fail;

}

// Copy the found filter pointer to the output parameter.

// Do not release () the reference, since it will still be used

// By the calling function.

* Ppbf = pfilter;

Return hr;

}

Return hr;

}

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.