Direct Show 開發的一個示例 (轉)
經常在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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 求一個獨立模型的開發示例模型
- 郵件開發:複雜郵件的一個示例
- 用wxPython開發GUI第一個示例講解PythonGUI
- Github團隊開發示例(一)Github
- Vscode開發第一個Vue+Element Plus示例VSCodeVue
- 轉載:mysql的show processlistMySql
- 如何組織一個高效的開發團隊(轉)
- Hive 高階應用開發示例(一)Hive
- 遊戲開發學堂:Direct3D入門之我見(轉)遊戲開發3D
- 關於多個開發中心開發同一軟體的配置管理(轉)
- 軟體開發中的矛盾——一個簡單的例子 (轉)
- 一個簡易的proxy程式的開發過程(2)(轉)
- JavaScript:一個滑鼠動態跟隨文字特效的示例 (轉)JavaScript特效
- 一個Wpf的開發框架框架
- 一個使用示例,五個操作步驟!從此輕鬆掌握專案中工作流的開發
- 從一個簡單的Java單例示例談談併發Java單例
- Oracle中的direct path read事件(轉)Oracle事件
- 玩轉iOS開發:iOS中的GCD開發(一)iOSGC
- 開發一個自己的 CSS 框架(一)CSS框架
- Winform開發--資料從一個DataGridView轉移到另一個DataGridViewORMView
- 開發一個抽獎大轉盤 Lucky-spin
- TLS示例開發-golang版本TLSGolang
- Scala開發Hadoop示例Hadoop
- 重構 第一個示例
- 一個程式設計師在IBM的開發經驗 (轉)程式設計師IBM
- 測試轉開發,一個女孩子短短的工作心得
- 什麼時候該終止一個專案的開發(轉)
- 專案回顧:一個開發人員的觀察與思考(轉)
- 玩轉iOS開發:iOS中的NSOperation開發(一)iOS
- 一個良好的開發理念TDD
- 開發一個完整的JavaScript元件JavaScript元件
- 如何開發一個自己的 RubyGem?
- [deviceone開發]-HeaderView和FooterView的示例devHeaderView
- 微信開發示例(連結資訊的接收)
- 11g中direct path read事件等待很高的一個案例事件
- 一個小而全的Python專案示例Python
- [譯]ViewModels:一個簡單的示例View
- 一個簡單的狀態列示例