Direct Show 開發的一個示例 (轉)

amyz發表於2007-08-15
Direct Show 開發的一個示例 (轉)[@more@]

  經常在CSDN 得到知識以及廣大同胞的幫助,在此深表感謝!在很多時候都希望自己也可以為大家做點,只是....呵呵,本菜鳥實在是墨水有限,說不上什麼東東。

下面是我在學習Directshow寫的一個示例,結構簡單,貼之。望可以為一些初學者提供一個感性的認識。

相關的技術文章建議看看陸其明寫的文章,嘿嘿,我就是看看他的文章開始學習Directshow程式設計的。


//程式碼如下
//Layer3Decoder.h
#ifndef LAYER3DECODER__H__
#define LAYER3DECODER__H__

#include
#include

#pragma comment(lib,"stids.lib")

#define S_RELEASE(x)  if((x)){(x)->Release();(x)=NULL;}

class CComInit //初始化Com環境
{
public:
 CComInit();
 ~CComInit();
};

class CLayer3Decoder // -> wav
{
public:
 CLayer3Decoder();
 ~CLayer3Decoder();

public:
 static HRESULT GetPin(IBaseFilter *pFilter, PIN_DIRECTION PinDir, IPin **ppPin);
 static HRESULT ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pFirst, IBaseFilter *pSecond);

public:
 void Decoder(LPCTSTR lpszSrc,LPCTSTR lpszDest);

private:
 void Release(void);

private:
 IGraphBuilder * m_pGraphBuilder;
 IMediaControl * m_pMediaControl;

 IBaseFilter * m_pFile;
 IBaseFilter * m_pFileDest;

 IBaseFilter * m_pStreamSplitter;
 IBaseFilter * m_3Decoder;

 IBaseFilter * m_pWaveDest;
};

#endif //LAYER3DECODER__H__


//////////////////////
//Layer3Decoder.cpp
//#include "stdafx.h"

#include

//#include
//#include

#include "Layer3Decoder.h"

//WavDest.ax
//這個filter是DirectShow SDK Example 中的一個例子
//編譯後註冊一下就可以了
// {3C78B8E2-6C4D-11d1-ADE2-0000F8754B99}
static const GUID CLSID_WavDest =
{ 0x3c78b8e2, 0x6c4d, 0x11d1, { 0xad, 0xe2, 0x0, 0x0, 0xf8, 0x75, 0x4b, 0x99 } };

//{38BE3000-F4-11D0-860E-00A024CFEF6D}
static const GUID CLSID_Layer3Decoder =
{ 0x38BE3000, 0xDBF4, 0x11D0, { 0x86, 0x0e, 0x0, 0xa0, 0x24, 0xcf, 0xef, 0x6d}};

CComInit::CComInit()
{
 ::CoInitialize(NULL);
}

CComInit::~CComInit()
{
 ::CoUninitialize();
}

const CComInit  comInit = CComInit();//自動初始化Com環境

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();
  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 ");
 Release();
 return;
 }

 ////////////////////////////////////////////////
 //
 hr = m_pGraphBuilder->QueryInterface(IID_IMediaControl,(void**)&m_pMediaControl);
 if(hr!=S_OK)
 {
 TRACE("Create MediaControl => Failsed ");
 Release();
 return;
 }

 /////////////////////////////////////////
 //建立讀入
 hr = ::CoCreateInstance(CLSID_AsyncReader,NULL,CLSCTX_ALL,IID_IBaseFilter,(void**)&m_pFileSource);
 if(hr!=S_OK)
 {
 TRACE("Create File Async Reader => Failsed ");
 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 ");
 S_RELEASE(psrc);
 Release();
 return;
 }

 hr = psrc->Load(wFileSrc,NULL);//讀入檔案
 if(hr!=S_OK)
 {
 TRACE("Load Source File => Failsed ");
 S_RELEASE(psrc);
 Release();
 return;
 }

 S_RELEASE(psrc);//釋放檔案讀入
 
 ////////////////////////////////////////////////////
 //建立檔案寫入
 hr = ::CoCreateInstance(CLSID_FileWriter,NULL,CLSCTX_ALL,IID_IBaseFilter,(void**)&m_pFileDest);
 if(hr!=S_OK)
 {
 TRACE("Create File Write => Failsed ");
 Release();
 return;
 }

 IFileSinkFilter * pdes = NULL;

 hr = m_pFileDest->QueryInterface(IID_IFileSinkFilter,(void**)&pdes);
 if(hr!=S_OK)
 {
 TRACE("Create File Write Interface => Failsed ");
 S_RELEASE(pdes);
 Release();
 return;
 }

 hr = pdes->SetFileName(wFileDes,NULL);
 if(hr!=S_OK)
 {
 TRACE("Set File Write => Failsed ");
 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 ");
 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 ");
 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 ");
 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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957926/,如需轉載,請註明出處,否則將追究法律責任。

相關文章