An example of Direct Show development

zhaozj2021-02-16  59

I often get knowledge in 9cbs.net and the help of the majority of compatriots, I am deeply grateful here! Many times I hope I can do some points for everyone, just .... huh, this rookie is really ink limited, I can't say anything.

Below is a sample program I have written in learning DirectShow programming, simple structure, and post. I hope that I can provide a sense of sensibility for some beginners.

Relevant technical articles suggests to see Lu Qiweng's article, hey, I just see his article began to learn DirectShow programming.

// The code is as follows //Layer3Decoder.h#ifndef layer3decoder__h __ # define layer3decoder__h__

#include #include

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

#define s_release (x) ife ((x) -> release (); (x) = null;}

Class ccominit // Initialization COM environment {public: ccominit (); ~ ccominit ();

Class Clayer3Decoder // MP3 -> WAV {public: Clayer3Decoder (); ~ Clayer3Decoder ();

Public: Static HRESULT GETPIN (IBasefilter * Pfilter, Pin_Direction Pindir, IPIN ** PPPIN); Static HRESULT ConnectFilters (iGraphbuilder * PGRAPH, IBASEFILTER * PSECOND);

PUBLIC: Void Decoder (LPCTSTR LPSZSRC, LPCTSTSTR LPSZDEST);

Private: Void Release (Void);

Private: igraphbuilder * m_pgraphbuilder; IMEDIACONTROL * m_pmediacontrol;

IBaseFilter * m_pfilesource; ibasefilter * m_pfiledest;

IBaseFilter * m_pstreamsplitter; ibasefilter * m_player3decoder;

Ibasefilter * m_pwavedest;};

#ENDIF / / LAYER3DECODER__H__

////Layer3decoder.cpp//include "stdafx.h"

#include

// # include // # include

#include "layer3decoder.h"

//Wavdest.ax// This filter is an example of DirectShow SDK EXAMPLE // After compiling, you can register it. {0xAD, 0xE2, 0x0, 0x0, 0xF8, 0x75, 0x4b, 0x99}};

// {38BE3000-DBF4-11D0-860E-00A024CFEF6D} Static const guid clsid_layer3decoder = {0x38be3000, 0x86, 0x11d0, {0x86, 0x0e, 0x0, 0xA0, 0x24, 0xcf, 0xef, 0x6d}};

CCominit :: ccominit () {:: coinitialize (null);

CCominit :: ~ ccominit () {:: couninitialize ();

Const ccominit COMINIT = ccominit (); // Automatically initialize COM environment

CLAYER3DECODER :: Clayer3Decoder () {m_pgraphbuilder = null; m_pmediacontrol = NULL;

m_pfilesource = null; m_pfiledest = null;

M_PStreamsplitter = null; m_player3decoder = null;

m_pwavedest = null;}

CLAYER3DECODER :: ~ Clayer3Decoder () {release ();

Void Clayer3Decoder :: release () {s_release (m_pgraphbuilder); s_release (m_pmediacontrol);

S_release (m_pfilesource); s_release (m_pfiledest);

S_release (m_pstreamsplitter); s_release (m_player3decoder);

S_release (m_pwavedest);

HRESULT CLAYER3DECODER :: getpin (ibasefilter * pfilter, pin_direction pindir, ipin ** pppin) {ienumpins * penum; ipin * ppin; pfilter-> enumpins (& penum); while (penum-> next (1, & ppin, 0) == S_ok) {Pin_Direction PindiRTHIS; PPIN-> QueryDirection (& PindiRTHIS); if (Pindir == PindirtHis) {Penum-> Release (); * PPPIN = PPIN; RETURN S_OK;} PPIN-> Release ();} Penum-> Release (); RETURN E_FAIL;}

HRESULT CLayer3Decoder :: ConnectFilters (IGraphBuilder * pGraph, IBaseFilter * pFirst, IBaseFilter * pSecond) {IPin * pOut = NULL, * pIn = NULL; HRESULT hr = GetPin (pFirst, PINDIR_OUTPUT, & pOut); if (FAILED (hr)) return Hr; hr = getpin (psecond, pindir_input, & pin); if (failed (hr)) {pout-> release-> release (); return e_fail;} hr = pgraph-> connect (pout, pin); pin-> release () Pout-> Release (); return hr;}

void CLayer3Decoder :: Decoder (LPCTSTR lpszSrc, LPCTSTR lpszDest) {HRESULT hr = S_OK; WCHAR wFileSrc [MAX_PATH]; WCHAR wFileDes [MAX_PATH]; USES_CONVERSION; wcsncpy (wFileSrc, T2W (lpszSrc), MAX_PATH); wcsncpy (wFileDes, T2W ( Lpszdest), MAX_PATH);

/// // hr = :: CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_ALL, IID_IGraphBuilder, (void **) & m_pGraphBuilder); if (hr = S_OK!) {TRACE ( "Crate Graph Builder => Failsed / n"); Release (); Return;

// hr = m_pgraphbuilder-> queryinterface (IID_IMEDIACONTROL, (void **) & m_pmediacontrol); if (hr! = S_ok) {trace ("Create MediaControl => Failsed / N"); release (); return;}

/ / / Establish a file read HR = :: COCREATEINSTANCE (CLSID_ASYNCREADER, NULL, CLSCTX_ALL, IID_IBASEFILTER, (Void **) & m_pfilesource); if (hr! = S_ok) {trace ("create file async reader => failsed / n" Release (); return;

IFileSourceFilter * psrc = NULL; hr = m_pFileSource-> QueryInterface (IID_IFileSourceFilter, (void **) & psrc); if (hr = S_OK!) {TRACE ( "Create File Source Load's Filter => Failsed / n"); S_RELEASE (psrc Release (); return;

HR = psrc-> loading (wfilesrc, null); // read file IF (HR! = S_OK) {trace ("Load Source File => Failsed / N"); S_RELEASE (PSRC); Release (); Return; }

S_RELEASE (PSRC); // Release File Read // Setup file Write HR =: COCREATEINSTANCE (CLSID_FILEWRITER, NULL, CLSCTX_ALL, IID_IBASEFILTER, (Void **) & m_pfiledEST; if (hr! = S_ok) {trace (" CREATE FILE WRITE => Failsed / N "); Release (); Return;}

Ifilesinkfilter * pdes = NULL;

HR = m_pfiledest-> queryinterface (IID_IFILESINKFILTER, (void **) & pdes); if (HR! = S_OK) {trace ("create file write interface => failsed / n"); S_RELEASE (PDES); Release (); Return HR = PDES-> setFileName (WFILEDES, NULL); if (HR! = S_OK) {Trace ("set file write => failsed / n"); s_release (pdes); Release (); return;} s_release PDES);

// Steam splitter hr = :: CoCreateInstance (CLSID_MPEG1Splitter, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **) & m_pStreamSplitter); if (hr = S_OK!) {TRACE ( "Create Stream Splitter => Failsed / n"); Release ( Return;}

// Layer 3 Decoder hr = :: CoCreateInstance (CLSID_Layer3Decoder, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **) & m_pLayer3Decoder); if (hr = S_OK!) {TRACE ( "Create Layer3 Decoder => Failsed / n"); Release (); Return;

/////Wave dest HR = :: COCREATEINSTANCE (CLSID_WAVDEST, NULL, CLSCTX_ALL, IID_IBASEFILTER, (Void **) & m_pwavedest); if (HR! = S_OK) {Trace ("Create Wave Dest => Failsed / N"); RELEASE (); RETURN;

/// // add filter into GraphBuilder hr = m_pGraphBuilder-> AddFilter (m_pFileSource, L "Source File"); hr = m_pGraphBuilder-> AddFilter (m_pFileDest, L "Dest File"); hr = m_pGraphBuilder-> AddFilter (m_pStreamSplitter, L "stream splitter"); hr = m_pgraphbuilder-> addfilter (m_player3decoder, l "layer3 decoder"); hr = m_pgraphbuilder-> addfilter (M_PwaveDest, L "Wave Dest");

/ // connection filter hr = ConnectFilters (m_pGraphBuilder, m_pFileSource, m_pStreamSplitter); hr = ConnectFilters (m_pGraphBuilder, m_pStreamSplitter, m_pLayer3Decoder); hr = ConnectFilters (m_pGraphBuilder, m_pLayer3Decoder, m_pWaveDest); hr = ConnectFilters (m_pGraphBuilder, m_pWaveDest, m_pFileDest); / / // Run m_pmediacontrol-> run ();

// the end

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

New Post(0)