最簡單的基於DirectShow的樣本:視頻播放器,directshow樣本
=====================================================
最簡單的基於DirectShow的樣本文章列表:
最簡單的基於DirectShow的樣本:視頻播放器
最簡單的基於DirectShow的樣本:視頻播放器圖形介面版
最簡單的基於DirectShow的樣本:視頻播放器自訂版
最簡單的基於DirectShow的樣本:擷取Filter資訊
=====================================================
本文記錄一個最簡單的基於DirectShow的視頻播放器。DirectShow是一個龐大的架構,可以在Windows下實現多種多樣的視頻處理需求。但是它的“龐大”也使得新手不太容易學習它的使用。本文的例子正是為解決這一問題而做的,它只包含了使用DirectShow播放一個視頻檔案所需要的最重要的函數。
流程圖
最簡單的使用DirectShow播放視頻檔案的流程如所示。
流程圖中涉及到幾個介面如下所示。
IGraphBuilder:繼承自IFilterGraph,用於構建Filter Graph。相比於IFilterGraph來說IGraphBuilder提供了一些更加“智能”的方法,例如RenderFile()方法。
IMediaControl:提供和播放控制有關的一些介面。
IMediaEvent:用來處理Filter Graph發出的事件。
流程圖中關鍵函數的作用如下所示。
CoInitialize() :初始化COM運行環境。
CoCreateInstance(…,pGraph) :用指定的類別識別項建立一個Com對象。在該播放器中類別識別項為“CLSID_FilterGraph”,用於建立IGraphBuilder。
pGraph->QueryInterface(…,pControl) :通過QueryInterface()查詢某個組件是否支援某個特定的介面。在這裡查詢IMediaControl介面。
pGraph->QueryInterface(…,pEvent) :同上。在這裡查詢IMediaEvent介面。
pGraph->RenderFile("xxx.mkv"):為指定的檔案智能的構建一個Filter Graph。
pControl->Run() :開始運行Filter Graph中的所有Filter。
pEvent->WaitForCompletion() :等待Filter Graph處理完所有資料。
CoUninitialize():釋放CoInitialize()初始化的COM運行環境。
注意上述幾個函數是構建一個基於DirectShow的視頻播放器所必須的函數,除了上述幾個介面之外還經常用到以下幾個介面:
IBasicVideo:提供和視頻有關的一些介面。
IBasicAudio:提供和音頻有關的一些介面。
IVideoWindow:提供和視窗有關的一些介面。
IMediaSeeking:提供和播放位置有關的一些介面。
原始碼
/** * 最簡單的基於DirectShow的視頻播放器 * Simplest DirectShow Player * * 雷霄驊 Lei Xiaohua * leixiaohua1020@126.com * 中國傳媒大學/數字電視技術 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本程式是一個最簡單的基於DirectShow的播放器。 * 適合初學者學習DirectShow。 * * This example is the simplest Player based on DirectShow. * Suitable for the beginner of DirectShow. */#include "stdafx.h"#include <dshow.h>#include <atlconv.h>#define OUTPUT_INFO 1//Show Filter in FilterGpraghint show_filters_in_filtergraph(IGraphBuilder *pGraph){printf("Filters in FilterGpragh=======\n");USES_CONVERSION;IEnumFilters *pFilterEnum=NULL;if(FAILED(pGraph->EnumFilters(&pFilterEnum))){pFilterEnum->Release();return -1;}pFilterEnum->Reset();IBaseFilter * filter = NULL;ULONG fetchCount = 0;//Pin Infowhile (SUCCEEDED(pFilterEnum->Next(1, &filter, &fetchCount)) && fetchCount){if (!filter){continue;}FILTER_INFO FilterInfo;if (FAILED(filter->QueryFilterInfo(&FilterInfo))){continue;}printf("[%s]\n",W2A(FilterInfo.achName));filter->Release();}pFilterEnum->Release();printf("==============================\n");return 0;}int _tmain(int argc, _TCHAR* argv[]){IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; IMediaEvent *pEvent = NULL; //Get some param--------------HRESULT hr1;IBasicVideo *pVideo=NULL;IBasicAudio *pAudio=NULL;IVideoWindow *pWindow=NULL;IMediaSeeking *pSeeking=NULL; // Init COM HRESULT hr = CoInitialize(NULL); if (FAILED(hr)){ printf("Error - Can't init COM."); return -1; }// Create FilterGraph hr=CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder, (void **)&pGraph); if (FAILED(hr)){ printf("Error - Can't create Filter Graph."); return -1; } // Query Interface hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);// RenderFile hr = pGraph->RenderFile(L"cuc_ieschool.mov", NULL);if (FAILED(hr)){printf("Error - Can't Render File.");return -1;}#if OUTPUT_INFO//Get some information----------long video_w=0,video_h=0,video_bitrate=0,audio_volume=0;long long duration_1=0,position_1=0;REFTIME avgtimeperframe=0;float framerate=0,duration_sec=0,progress=0,position_sec=0;//Videohr1=pGraph->QueryInterface(IID_IBasicVideo, (void **)&pVideo);pVideo->get_VideoWidth(&video_w);pVideo->get_VideoHeight(&video_h);pVideo->get_AvgTimePerFrame(&avgtimeperframe);framerate=1/avgtimeperframe;//pVideo->get_BitRate(&video_bitrate);//Audiohr1=pGraph->QueryInterface(IID_IBasicAudio, (void **)&pAudio);//Mute//pAudio->put_Volume(-10000);printf("Some Information:\n");printf("Video Resolution:\t%dx%d\n",video_w,video_h);printf("Video Framerate:\t%.3f\n",framerate);//Windowhr1=pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWindow);pWindow->put_Caption(L"Simplest DirectShow Player");//pWindow->put_Width(480);//pWindow->put_Height(272);//Seekhr1=pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pSeeking);pSeeking->GetDuration(&duration_1);//time unit:100ns=0.0000001sduration_sec=(float)duration_1/10000000.0;printf("Duration:\t%.2f s\n",duration_sec);//pSeeking->SetPositions();//PlayBack Rate//pSeeking->SetRate(2.0);//Show Filter in FilterGpaghshow_filters_in_filtergraph(pGraph);//----------------------#endifprintf("Progress Info\n");printf("Position\tProgress\n"); if (SUCCEEDED(hr)){ // Run hr = pControl->Run(); if (SUCCEEDED(hr)){long evCode=0;//pEvent->WaitForCompletion(INFINITE, &evCode);while(evCode!=EC_COMPLETE){//Info#if OUTPUT_INFOpSeeking->GetCurrentPosition(&position_1);position_sec=(float)position_1/10000000.0;progress=position_sec*100/duration_sec;printf("%7.2fs\t%5.2f%%\n",position_sec,progress);#endif//1000mspEvent->WaitForCompletion(1000, &evCode);} } }// Release resource pControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize();return 0;}
運行結果程式運行後即可開始播放一個“cuc_ieschool.mov”檔案。程式運行時候的如下所示。由圖可見啟動並執行同時程式在控制台中列印出了兩種資訊:
(1)該視頻的相關資訊
(2)播放該視頻的 Filter Graph中的Filter(該功能通過函數show_filters_in_filtergraph()完成)。
可以通過定義在代碼最前面宏OUTPUT_INFO控制是否輸出視頻的資訊。定義成“0”的話則不會輸出視頻的資訊。如下所示。
#define OUTPUT_INFO 1
下載
Simplest DirectShow Example
SourceForge項目首頁: https://sourceforge.net/projects/simplestdirectshowexample/
CDSN:http://download.csdn.net/detail/leixiaohua1020/8348163
本程式包含了DirectShow開發的樣本程式。適合DirectShow初學者進行學習。
它包含了以下幾個子程式:
simplest_directshow_player: 最簡單的基於DirectShow的視頻播放器。
simplest_directshow_player_custom: 最簡單的基於DirectShow的視頻播放器(Custom)。
playerGUI: 最簡單的基於DirectShow的播放器-圖形介面版。
simplest_directshow_info: 最簡單的Directshow資訊顯樣本子。
simplest_directshow_filter: 目前還未完成。