Direct Show 开发的一个示例

类别:VC语言 点击:0 评论:0 推荐:

  经常在CSDN.NET 得到知识以及广大同胞的帮助,在此深表感谢!在很多时候都希望自己也可以为大家做点,只是....呵呵,本菜鸟实在是墨水有限,说不上什么东东。

下面是我在学习Directshow编程写的一个示例程序,结构简单,贴之。望可以为一些初学者提供一个感性的认识。

相关的技术文章建议看看陆其明写的文章,嘿嘿,我就是看看他的文章开始学习Directshow编程的。


//代码如下
//Layer3Decoder.h
#ifndef LAYER3DECODER__H__
#define LAYER3DECODER__H__

#include <atlbase.h>
#include <dshow.h>

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

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

class CComInit //初始化Com环境
{
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 *pFirst, IBaseFilter *pSecond);

public:
 void Decoder(LPCTSTR lpszSrc,LPCTSTR 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 <tchar.h>

//#include <atlbase.h>
//#include <dshow.h>

#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-DBF4-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\n");
  Release();
  return;
 }

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

 /////////////////////////////////////////
 //建立文件读入
 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->Load(wFileSrc,NULL);//读入文件
 if(hr!=S_OK)
 {
  TRACE("Load Source File => Failsed\n");
  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\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

本文地址:http://com.8s8s.com/it/it1746.htm