分享
 
 
 

Direct Show 开发的一个示例

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

经常在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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有