Microsoft DirectShow
Introduction DirectShow 是 Microsoft 所提供的函式庫
Software Requirements Operation system Windows XP Development tool Microsoft Visual Studio .NET Visual C++ .NET
Install DirectX 9.0 SDK 到網站下載 SDK 安裝程式 執行所下載程式 http://msdn.microsoft.com/downloads/ 點選 DirectX 9.0 Software Development Kit with DirectX 9.0b Runtime 執行所下載程式 透過安裝介面選擇要安裝的目錄 程式將自動安裝 SDK
Preparing for Compilation 設定工作環境 Add include search paths Add linker search paths Add project link libraries
Add include search paths (1) choose Options from the Tools menu
Add include search paths (2) Select Directories tag Double click the left button on the mouse
Add include search paths (3) Select the SDK include search path Type or browse to select the search path
Add include search paths (4) Shift the include search path to the top of the search directories Press this icon
Add linker search paths (1) choose Options from the Tools menu
Add linker search paths (2) Select Directories tag Double click the left button on the mouse
Add linker search paths (3) Select the SDK include search path Type or browse to select the search path
Add linker search paths (4) Shift the include search path to the top of the search directories Press this icon
Add project link libraries (1) choose settings from the Project menu
Add project link libraries (2) Select the link tag type the name of the project link library
DirectShow Example
Filter DirectShow 中處理 media stream 的基本元件 根據功能,可分成三類 Source filter Transform filter Renderer filter
Filter Graph 將 source filter, transform filter, and Renderer filter 串接起來而形成的圖
Source Filter 從 data source 獲取資料,並將資料傳送到 filter graph 中 資料源可以是攝影機、網際網路、磁片檔等
Transform Filter 獲取、處理和傳送媒體資料 Examples Splitter transform filter Video decoder Audio decoder
Renderer Filter 在硬體上表現媒體資料,如顯示卡和音效卡,或者是任何可以接受媒體資料的地方,如磁片。 Example Video renderer filter Audio renderer filter。
Filter Graph Manager 掌管 Filter Graph 中 filters 的連接 控制 media stream 在 filter 之間的流動
Filter Graph Manager (cont.) 程式開發人員可透過 API 函數對media stream 控制 Examples Run: 啟動 media stream 在 Filter graph 中的流動 Pause:暫停 media stream 的播放 Stop:停止播放 media stream
DirectShow 工具示範 Graph Editor
Programming DirectShow Applications Play a File Building a Filter Graph
Preliminary DirectShow COM Based on Component Object Model(COM) Interface 1 COM object Interface 2 Interface n
Coding Flowchart Running some applications Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Running some applications Releasing the COM interfaces Releasing the COM facilities
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications Object name CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… Interface name pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM Interface name IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Example .…..……Some executions………… Initializing the COM facilities Creating an Instance of a COM Object Querying Interfaces in the COM Object Releasing the COM interfaces Releasing the COM facilities Running some applications CoInitialize (NULL) //Initializes COM IGraphBuilder *pGraphBuilder = NULL; // Pointer to created object HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder); IMediaControl *pMediaControl = NULL; // Store pointer to interface hr = pGraphBuilder->QueryInterface(IID_MediaControl, (void**)&pMediaControl); .…..……Some executions………… pMediaControl->Release(); // Release the object pMediaControl = NULL; // And set it to NULL pGraphBuilder->Release(); // Release the object pGraphBuilder = NULL; // And set it to NULL CoUninitialize(); // Releases COM
Some interfaces IGraphBuilder IMediaControl IMediaEvent provides methods that enable an application to build a filter graph methods: AddSourceFilter, Connect, RenderFile, etc. IMediaControl provides methods for controlling the flow of data through the filter graph methods: Run, Pause, Stop, etc. IMediaEvent contains methods for retrieving event notifications methods: GetEvent, WaitForCompletion, etc.
Query Interfaces in the COM object 1. Play a File #include <dshow.h> void main(void) { IGraphBuilder *pGraph; IMediaControl *pMediaControl; //Handles media streaming in the filter graph IMediaEvent *pEvent; //Handles filter graph events Create the COM object CoInitialize(NULL); // Create the filter graph manager and query for interfaces. CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); Query Interfaces in the COM object pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl); pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
Release the Interfaces 1. Play a File // Build the graph. IMPORTANT: Change string to a file on your system. pGraph->RenderFile(L"C:\\Example.avi", NULL); // Run the graph. pMediaControl->Run(); // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); Running …. Release the Interfaces // Clean up. pMediaControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); } Release the COM
1. Play a File pGraph->RenderFile(L"C:\\Example.avi", NULL); // Build the graph. //IMPORTANT: Change string to a file on your system. pGraph->RenderFile(L"C:\\Example.avi", NULL); // Run the graph. pMediaControl->Run(); // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); // Clean up. pMediaControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); }
2. Building a Filter Graph InputFileFilter DSoundRenderer
2. Building a Filter Graph #include <dshow.h> int main(int argc, char* argv[]) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; //Handles media streaming in the filter graph IMediaEvent *pEvent = NULL; //Handles filter graph events IBaseFilter *pInputFileFilter = NULL; IBaseFilter *pDSoundRenderer = NULL; IPin *pFileOut = NULL, *pWAVIn = NULL; // Initialize the COM library. HRESULT hr = CoInitialize(NULL); hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Now get the media control interface... hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); // And the media event interface. hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); hr = pGraph->AddSourceFilter(L“C:\\test.mpg", L“Source File", &pInputFileFilter);
2. Building a Filter Graph #include <dshow.h> int main(int argc, char* argv[]) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; //Handles media streaming in the filter graph IMediaEvent *pEvent = NULL; //Handles filter graph events IBaseFilter *pInputFileFilter = NULL; IBaseFilter *pDSoundRenderer = NULL; IPin *pFileOut = NULL, *pWAVIn = NULL;); hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Now get the media control interface... hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); // And the media event interface. hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); FilterGraph InputFileFilter DSoundRenderer hr = pGraph->AddSourceFilter(L“C:\\test.mpg", L“Source File", &pInputFileFilter);
2. Building a Filter Graph #include <dshow.h> int main(int argc, char* argv[]) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; //Handles media streaming in the filter graph IMediaEvent *pEvent = NULL; //Handles filter graph events IBaseFilter *pInputFileFilter = NULL; IBaseFilter *pDSoundRenderer = NULL; IPin *pFileOut = NULL, *pWAVIn = NULL; // Initialize the COM library. HRESULT hr = CoInitialize(NULL); hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Now get the media control interface... hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); // And the media event interface. hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); hr = pGraph->AddSourceFilter(L“C:\\test.mpg", L“Source File", &pInputFileFilter);
2. Building a Filter Graph #include <dshow.h> int main(int argc, char* argv[]) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; //Handles media streaming in the filter graph IMediaEvent *pEvent = NULL; //Handles filter graph events IBaseFilter *pInputFileFilter = NULL; IBaseFilter *pDSoundRenderer = NULL; IPin *pFileOut = NULL, *pWAVIn = NULL; // Initialize the COM library. HRESULT hr = CoInitialize(NULL); FilterGraph InputFileFilter File name hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Now get the media control interface... hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); // And the media event interface. hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); hr = pGraph->AddSourceFilter(L“C:\\test.mpg", L“Source File", &pInputFileFilter); Filter name
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop();
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); Object name // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); Filter name FilterGraph hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop(); InputFileFilter DSoundRenderer
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop();
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); FilterGraph hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop(); InputFileFilter DSoundRenderer
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop();
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); FilterGraph InputFileFilter DSoundRenderer // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop();
2. Building a Filter Graph hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pDSoundRenderer); // And add the filter to the filter graph // using the member function AddFilter. hr = pGraph->AddFilter(pDSoundRenderer, L"Audio Renderer"); // Obtain the output pin of the source filter. // The local function GetPin does this. pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT); // Obtain the input pin of the WAV renderer. pWAVIn = GetPin(pDSoundRenderer, PINDIR_INPUT); hr = pGraph->Connect(pFileOut, pWAVIn); hr = pControl->Run(); long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); hr = pControl->Stop();
2. Building a Filter Graph // Now release everything we instantiated-- // that is, if it got instantiated. pFileOut->Release(); pWAVIn->Release(); pInputFileFilter->Release(); pDSoundRenderer->Release(); pControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); return 0; }
上機實驗流程
下載程式 Play a File Building a Filter Graph 所需之video檔 http://nn.csie.nctu.edu.tw/course/MM2004/codes/DSRender.zip Building a Filter Graph http://nn.csie.nctu.edu.tw/course/MM2004/codes/DSBuild.zip 所需之video檔 http://nn.csie.nctu.edu.tw/course/MM2004/codes/ TEST.MPG
將程式載入 visual studio 中 解下載的檔案解壓縮 開啟 Visual Studio
將程式載入 visual studio 中 choose Open Workspace from the Files menu
將程式載入 visual studio 中 找尋剛才解壓縮時所產生的目錄並開啟 位於目錄內的 workspace
建立執行檔 choose Build xxx.exe from the Build menu
執行程式 choose Execute xxx.exe from the Build menu