Download presentation
Presentation is loading. Please wait.
1
Microsoft DirectShow
2
Introduction DirectShow 是 Microsoft 所提供的函式庫
3
Software Requirements
Operation system Windows XP Development tool Microsoft Visual Studio .NET Visual C++ .NET
4
Install DirectX 9.0 SDK 到網站下載 SDK 安裝程式 執行所下載程式
點選 DirectX 9.0 Software Development Kit with DirectX 9.0b Runtime 執行所下載程式 透過安裝介面選擇要安裝的目錄 程式將自動安裝 SDK
5
Preparing for Compilation
設定工作環境 Add include search paths Add linker search paths Add project link libraries
6
Add include search paths (1)
choose Options from the Tools menu
7
Add include search paths (2)
Select Directories tag Double click the left button on the mouse
8
Add include search paths (3)
Select the SDK include search path Type or browse to select the search path
9
Add include search paths (4)
Shift the include search path to the top of the search directories Press this icon
10
Add linker search paths (1)
choose Options from the Tools menu
11
Add linker search paths (2)
Select Directories tag Double click the left button on the mouse
12
Add linker search paths (3)
Select the SDK include search path Type or browse to select the search path
13
Add linker search paths (4)
Shift the include search path to the top of the search directories Press this icon
14
Add project link libraries (1)
choose settings from the Project menu
15
Add project link libraries (2)
Select the link tag type the name of the project link library
16
DirectShow Example
17
Filter DirectShow 中處理 media stream 的基本元件 根據功能,可分成三類 Source filter
Transform filter Renderer filter
18
Filter Graph 將 source filter, transform filter, and Renderer filter 串接起來而形成的圖
19
Source Filter 從 data source 獲取資料,並將資料傳送到 filter graph 中
資料源可以是攝影機、網際網路、磁片檔等
20
Transform Filter 獲取、處理和傳送媒體資料 Examples Splitter transform filter
Video decoder Audio decoder
21
Renderer Filter 在硬體上表現媒體資料,如顯示卡和音效卡,或者是任何可以接受媒體資料的地方,如磁片。 Example
Video renderer filter Audio renderer filter。
22
Filter Graph Manager 掌管 Filter Graph 中 filters 的連接
控制 media stream 在 filter 之間的流動
23
Filter Graph Manager (cont.)
程式開發人員可透過 API 函數對media stream 控制 Examples Run: 啟動 media stream 在 Filter graph 中的流動 Pause:暫停 media stream 的播放 Stop:停止播放 media stream
24
DirectShow 工具示範 Graph Editor
25
Programming DirectShow Applications
Play a File Building a Filter Graph
26
Preliminary DirectShow COM Based on Component Object Model(COM)
Interface 1 COM object Interface 2 Interface n
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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.
38
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);
39
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
40
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(); }
41
2. Building a Filter Graph
InputFileFilter DSoundRenderer
42
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);
43
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);
44
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);
45
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
46
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();
47
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
48
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();
49
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
50
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();
51
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();
52
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();
53
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; }
54
上機實驗流程
55
下載程式 Play a File Building a Filter Graph 所需之video檔
Building a Filter Graph 所需之video檔 TEST.MPG
56
將程式載入 visual studio 中 解下載的檔案解壓縮 開啟 Visual Studio
57
將程式載入 visual studio 中 choose Open Workspace from the Files menu
58
將程式載入 visual studio 中 找尋剛才解壓縮時所產生的目錄並開啟 位於目錄內的 workspace
59
建立執行檔 choose Build xxx.exe from the Build menu
60
執行程式 choose Execute xxx.exe from the Build menu
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.