Some auxiliary functions for DirectShow

xiaoxiao2021-03-06  22

No packaged package, just concentrate some functions in a file.

One is a function of the declaration file DSHOWUTILITIES.H, one is the definition of the function DSHOWUTILITIES.CPP

This function is from Microsoft's own source code or helps COPY in the help document.

The first is DSHOWUTILITIES.H file

#ifndef dshowutilities_h_ # define dshowutilities_h_

#include

#pragma comment (Lib, "Strmiids.lib")

// This is just a small function, but it will be used in some examples.

#define safe_release (x) {if (x) x-> release (); x = NULL;}

HRESULT AddFilterByCLSID (. IGraphBuilder * pGraph, // Pointer to the Filter Graph Manager const GUID & clsid, // CLSID of the filter to create LPCWSTR wszName, // A name for the filter IBaseFilter ** ppF..); // Receives a Pointer to the filter.

HRESULT GETUNCONNECTEDPIN (IBasefilter * pfilter, // pointer to the filter. Pin_direction pindir, // Direction of the pin to find. Ipin ** pppin); // Receives a Pointer to the PIN.

HRESULT ConnectFilters (iGraphbuilder * pgraph, // filter graph manager. Ipin * pout, // output pin on the upstream filter. Ibasefilter * pdest); // DowNStream Filter.

HRESULT ConnectFilters (IGraphbuilder * pgraph, ibasefilter * pdest);

/ / Judgment whether the PIN has this MediaType, Stolen from SDK AmcapBool Hasmediatype (IPIN * PPIN, REFGUID Majortype);

// Stolen from SDK AMCAP, where the above function hasmediatypebool isvideopin (IPIN * PPIN);

// Tear Down Everything DownStream of a Given Filter // Removes all filters of a given filter, implement // Stolen from SDK Amcapvoid NukeDownStream (IBaseFilter * PF, IGRAPHBUILDER * PFG) by recursive call;

// Tear down everything downstream of the capture filters, so we can build // a different capture graph. Notice that we never destroy the capture filters // and WDM filters upstream of them, because then all the capture settings // we've SET Would Be lost.// Remove the filter behind the Capture Filter so that a new Capture Graph can be created. // The reason for not deleting the Capture Filter is that if you delete, then we originally set up // stolen from SDK Amcapvoid Teardowngraph (iGraphbuilder * PFG, IvideoWindow * Pvw, IBasefilter * pvcap); # ENDIF

The following code is DSHOWUTILITIES.CPP, which is the implementation of these functions.

//Dshowutilities.cpp --------------------------------- -----------

#include "stdafx.h" #include "dshowutilities.h"

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- The Following Function Creates A Filter with a Specified Class Identifier (CLSID) AND Adds it to the filter graph

Use excel: add avi MUX filter to the graph

Ibasefilter * pmux; hr = addfilterbyclsid (pgraph, clsid_avidest, l "avi mux", & pmux); if (succeededed (hr)) {// ............. PMUX-> Release ();} ---------------------------------------------- ---------------------------------- * / HRESULT AddFilterbyClsid (IgraphBuilder * pgraph, // Pointer to the the Filter Graph Manager. Const Guid & Clsid, // CLSID of the filter to create. Lpcwstr wszname, // a name for the filter. Ibasefilter ** ppf) // Receives a Pointer to the filter. {If (! Pgraph ||! ppF) return E_POINTER; * ppF = 0; IBaseFilter * pF = 0; HRESULT hr = CoCreateInstance (clsid, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast (& pF)); if (SUCCEEDED (hr)) {hr = PGRAPH-> AddFilter (PF, WSZNAME); if (successteded (HR)) * ppf = pf; Else Pf-> Release ();} return hr;} / * ------------- -------------------------------------------------- ------------------- This function Find An Unconnected Pin on A Filter

.................... ..

Use exced:

IPIN * Pout = NULL; HRESULT HR = Getunconnectedpin (Pfilter, Pindir_output, & Pout); if (succeededed (hr)) {// ......... Pout-> release ();}

-------------------------------------------------- ------------------------------- * / hResult getunconnectedpin (ibasefilter * pfilter, // Pointer to the filter. Pin_direction Pindir, // direction of the pin to find. Ipin ** PPPIN // Receives a Pointer to the pin. {* Pppin = 0; Ienumpins * penum = 0; ipin * ppin = 0; HRESULT HR = Pfilter-> ENUMPINS (& Penum); IF (Failed (HR)) {Return HR;} while (penum-> next (1, & ppin, null) == s_ok) {Pin_Direction thispindir; PPIN-> QueryDirection (& thispindir); if (thispindir == PINDIR) {iPin * ptmp = 0; hr = ppin-> connectTEDto (& PTMP); if (succeeded (hr)) // already connected, not the pin we want. {PTMP-> release ();} else // unconnected , this is the pin we want. {penum-> release (); * pppin = ppin; returnid;} N-> Release ();} penum-> release (); // DID NOT FIND A Matching Pin. Return E_FAIL;} / * -------------------- -------------------------------------------------- ---------- The Following Function Connects An Output Pin from One Filter to The First Available Input Pin on Another Filter. The previous definition is called getUnconnectedPin.

-------------------------------------------------- ------------------------------- * /

HRESULT ConnectFilters (IGraphBuilder * pGraph, // Filter Graph Manager. IPin * pOut, // Output pin on the upstream filter. IBaseFilter * pDest) // Downstream filter. {If ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL)) {return E_POINTER;} #ifdef debug PIN_DIRECTION PinDir; pOut-> QueryDirection (& PinDir); _ASSERTE (PinDir == PINDIR_OUTPUT); # endif // Find an input pin on the downstream filter Ipin * pin = 0; HRESULT HR = getUnconnectedpin (PDEST, PINDIR_INPUT, & PIN); // Call the previously defined function if (failed (hr)) {Return HR;} // Try to connect them. HR = PGRAPH- > Connect (Pout, PIN); PIN-> Release (); Return Hr;}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------- Here is an overloaded version of the Same Function. The second parameter is a pointer to A Filter, Rather Than a Pin.The Function Connects The First Filter to The Second Filter

Use Example: The Following Example Use this Example To Connect Avi Mux Filter To File Writer Filter

IBaseFilter * pMux, * pWrite; hr = AddFilterByCLSID (pGraph, CLSID_AviDest, L "AVI Mux", & pMux); if (SUCCEEDED (hr)) {hr = AddFilterByCLSID (pGraph, CLSID_FileWriter, L "File Writer", & pWrite); if (Ac) {HR = ConnectFilters (PGRAPH, PMUX, PWRITE); // Use ifilesinkfilter to set the file name (not shown). PWRITE-> Release ();} PMUX-> Release ();

This uses the CONNECTFILTERS function and getUnconnectedpin function in front of the previously defined --------------------------------------------------------------------------------------------------------------------------- ------------------------------------------- * / hResult ConnectFilters IGraphbuilder * pgraph, ibasefilter * psrc, ibasefilter * pdest) {if (pgraph == null) || (psrc == null) || (pdest == null) {Return E_POINTER;} // Find An Output Pin on ipin * pout = 0; HRESULT HR = getUnconnectedpin (PSRC, PINDIR_OUTPUT, & POUT); // The previously defined function IF (Failed (HR)) {Return HR;} hr = connectfilters (PGRAPH, POUT, PDEST); // Using the function of the previous definition Pout-> Release (); return HR;}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- Judgment PIN has this MediaType, Stolen from SDK AMCAP ------ -------------------------------------------------- ----------------------- * / Bool Hasmediatype (ipin * ppin, refguid majortype) {if (! Ppin) {returnaf false;}

IEnummediatypes * pmediatypes; hResult hr = ppin-> enummediatypes (& PMediaTypes); if (Failed (HR)) {Return False;}

HR = pmediatypes-> reset (); if (Failed (HR)) {Return False;}

Ulong fetched; am_media_type * mediatype;

while (S_OK == pMediaTypes-> Next (1, & mediaType, & fetched)) {if (IsEqualGUID (mediaType-> majortype, majorType)) {// DeleteMediaType (mediaType); return TRUE;} // DeleteMediaType (mediaType);}

Return False;

}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- Judgment is a video pin, Stolen from SDK Amcap This function is used to use the function Hasmediatype -------------------------------------------------- ----------------------------- * / bool isvideopin (ipin * ppin) {Return Hasmediatype (PPIN, MediaType_Video); // The above functions} / * ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------- Tear Down Everything DownStream of a Given Filter Put a Graphbuilder All Filter under a given filter is deleted, implementing Stolen from Amcap by recursive call ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- * / void NukeDownStream (IBasefilter * PF, IGRAPHBUILDER * PFG) {iPin * pp = 0, * pto = 0; Ulong u; ienumpins * pins = NULL; PIN_INFO PININFO;

IF (! pf) return;

HRESULT HR = PF-> Enumpins (& Pins); Pins-> Reset (); // Take this function to get the latest data?

While (hr == noerror) {hr = pins-> next (1, & pp, & u); // Find pin if (hr == s_ok && pp) {PP-> Connectedto (& PTO); // Find Pin if (pto) {hr = pto-> querypinInfo (& pinInfo) on another filter connected to this PIN; if (hr == noerror) {if (PinInfo.dir == Pindir_input) // only looking for Input PIN {NukeDownStream (PININFO.PFILTER, PFG); // Recursive call PFG-> disconnect (pto); // To remove PFG-> Disconnect (PP) at both ends of the PIN; PFG-> RemoveFilter (Pininfo.pfilter) // In recursive call, // first deleted only the filter} Pininfo.pfilter-> release ();} PP-> Release ();} PP-> Release (); End Whileif (Pins) Pins-> Release ();

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------- Tear Down Everything DownStream of the Capture Filters, So We can build a Different Capture Graph . Notice that we never destroy the capture filters and WDM filters upstream of them, because then all the capture settings we've set would be lost. the filter deletes the back of the capture filter, so that you can create a new capture graph. The reason why you don't delete the Capture Filter is that if you delete, then our original setting is white from SDK AMCAP ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ----- * / void TearDownGraph (IGraphBuilder * pFg, IVideoWindow * pVW, IBaseFilter * pVCap) {if (pVW) // IVideoWindow stop the video display {// stop drawing in our window, or we may get wierd repaint effects pVW -> put_owner; pvw-> put_visible (oafalse); pvw-> release (); pvw = null;} // destroy the graph downstream of} nukedownstream (PVCAP, PFG); // This function TEAR DOWN EVERYTHING DOWNSTREAM OF A GIVEN FILTER / / However, this filter is preserved // then it should be a release} for some filter.

转载请注明原文地址:https://www.9cbs.com/read-53627.html

New Post(0)