Posted on 2008-09-24 16:28
kainster 阅读(301)
评论(0) 编辑 收藏
第一个DirectShow程序,来自于DirectX SDK的文档,要引入strmiids.lib
主要步骤为:
1. 创建 Filter Graph Manager 的实例
2. 用 Filter Graph Manager 建立 filter graph.
3. Runs the graph, which causes data to move through the filters.
主要用到的接口:
IGraphBuilder 创建Filter Graph Manager
IMediaControl 控制streaming,程序中用来控制视频的播放和停止
IMediaEvent 处理视频事件,程序中用来等待视频播放完毕
#include <dshow.h>
#include <stdio.h>
/**////////////////////////////////////////////////////////////////////////////
//基本步骤:
//1. 创建 Filter Graph Manager 的实例
//2. 用 Filter Graph Manager 建立 filter graph.
//3. Runs the graph, which causes data to move through the filters.
/**//////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// 初始化COM库.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}
//MediaControl controls streaming. It contains methods for stopping and starting the graph.
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
//IMediaEvent has methods for getting events from the Filter Graph Manager.
//In this example, the interface is used to wait for playback to complete.
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph.
//The IGraphBuilder::RenderFile method builds a filter graph that can play the specified file.
//The first parameter is the file name, represented as a wide character (2-byte) string.
//The second parameter is reserved and must equal NULL.
hr = pGraph->RenderFile(L"D:\\yf.avi", NULL);
if (SUCCEEDED(hr))
{
//This method can fail if the specified file does not exist, or the file format is not recognized.
//Assuming that the method succeeds, however, the filter graph is now ready for playback.
//To run the graph, call the IMediaControl::Run method:
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
//When the application is finished, release the interface pointers and close the COM library:
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}