610 likes | 857 Views
Microsoft DirectShow. Introduction. DirectShow 是 Microsoft 所提供的函式庫 透過 DirectShow, 我們可以很容易的處理電腦上的多媒體資料 : 如聲音 , 影像等. Software Requirements. Operation system Windows XP Development tool Microsoft Visual Studio .NET Visual C++ .NET. Install DirectX 9.0 SDK. 到網站下載 SDK 安裝程式
E N D
Introduction • DirectShow 是 Microsoft 所提供的函式庫 • 透過 DirectShow, 我們可以很容易的處理電腦上的多媒體資料: 如聲音,影像等
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
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
Interface 1 Interface 2 Interface n Preliminary • DirectShow • Based on Component Object Model(COM) • COM COM object
Coding Flowchart 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
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 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
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 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
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 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
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 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
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 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
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 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
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 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
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 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
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 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 • 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.
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);
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 // 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(); }
FilterGraph 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);
FilterGraph 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); 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);
FilterGraph 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); 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();
FilterGraph 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 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();
FilterGraph 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(); 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();