以播放一个以h264编码,声音以AAC编码的mp4文件为例
//09571A4B-F1FE-4C60-9760-DE6D310C7C31 如果是自己写的filter,肯定有GUID值,这是如果连接uuids.h中
//没有的CLSID,要自己重新定义一下 ,
static const GUID CLSID_coreAVC =
{ 0x09571A4B, 0xF1FE, 0x4C60, { 0x97, 0x60, 0xDE, 0x6D, 0x31, 0x0C, 0x7C, 0x31 } };
下面是完整的播放一个mp4文件(视频以h264编码,声音以AAC编码)的代码
play()
{
CoInitialize(NULL);
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
HRESULT 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;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"D://test_my.mp4", NULL);
IBaseFilter *spliteFilter;
hr=FindSplitterFilter(pGraph,&spliteFilter); //寻找分离器
NukeDownstream(pGraph,spliteFilter); //删除分离器以下的所有filter
IBaseFilter*pDecode,*pDisplay;
//能过CLSID加入视频显示的filter 如果是自己写的filter,替换成自己filter的CLSID即可
hr=AddFilterByCLSID(pGraph,CLSID_VideoRendererDefault,L"video Render",&pDisplay);
//能过CLSID加入coreAVC
hr=AddFilterByCLSID(pGraph,CLSID_coreAVC,L"core AVC",&pDecode);
IPin*pPin0=0;
IPin*pPin1=0;
if (spliteFilter)
{
hr=GetPin(spliteFilter,PINDIR_OUTPUT,0,&pPin0);
hr=GetPin(spliteFilter,PINDIR_OUTPUT,1,&pPin1);
}
if (pPin1)
{
hr=pGraph->Render(pPin0);
hr=pGraph->Render(pPin1);
pPin0->Release();
pPin1->Release();
}
pDecode->Release();
pDisplay->Release();
SaveGraphFile(pGraph,L"D://abc.grf"); //方便调试,看是否连接成功
//播放
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
HRESULT FindSplitterFilter(IGraphBuilder *pGraph,IBaseFilter **pSplitterFilter)
{
IEnumFilters *pEnumFilter=NULL;
HRESULT hr=pGraph->EnumFilters(&pEnumFilter);
if (SUCCEEDED(hr))
{
IBaseFilter *pFilter=NULL;
while (S_OK==pEnumFilter->Next(1,&pFilter,NULL))
{
int iOutPinCount=0;
// IPin *ppPin = 0;
IEnumPins *pEnumPin = 0;
IPin *pPin = 0;
hr = pFilter->EnumPins(&pEnumPin);
if (FAILED(hr))
{
return hr;
}
while (pEnumPin->Next(1, &pPin, NULL) == S_OK)
{
PIN_DIRECTION ThisPinDir;
pPin->QueryDirection(&ThisPinDir);
if (ThisPinDir == PINDIR_OUTPUT)
{
iOutPinCount++;
}
pPin->Release();
}
pEnumPin->Release();
if(iOutPinCount>=2)
{
*pSplitterFilter=pFilter;
return S_OK;
}
}
pEnumFilter->Release();
}
return E_FAIL;
}
void NukeDownstream(IGraphBuilder * inGraph, IBaseFilter * inFilter)
{
if (inGraph && inFilter)
{
IEnumPins * pinEnum = 0;
if (SUCCEEDED(inFilter->EnumPins(&pinEnum)))
{
pinEnum->Reset();
IPin * pin = 0;
ULONG cFetched = 0;
bool pass = true;
while (pass && SUCCEEDED(pinEnum->Next(1, &pin, &cFetched)))
{
if (pin && cFetched)
{
IPin * connectedPin = 0;
pin->ConnectedTo(&connectedPin);
if(connectedPin)
{
PIN_INFO pininfo;
if (SUCCEEDED(connectedPin->QueryPinInfo(&pininfo)))
{
if(pininfo.dir == PINDIR_INPUT)
{
NukeDownstream(inGraph, pininfo.pFilter);
inGraph->Disconnect(connectedPin);
inGraph->Disconnect(pin);
inGraph->RemoveFilter(pininfo.pFilter);
}
pininfo.pFilter->Release();
}
connectedPin->Release();
}
pin->Release();
}
else
{
pass = false;
}
}
pinEnum->Release();
}
}
}
HRESULT AddFilterByCLSID(IGraphBuilder*pGraph,const GUID& clsid,LPCWSTR wszName,IBaseFilter**ppF)
{
if (!pGraph||!ppF)
return E_POINTER;
*ppF=0;
IBaseFilter*pF=0;
HRESULT hr=CoCreateInstance(clsid,0,CLSCTX_INPROC_SERVER,IID_IBaseFilter,reinterpret_cast<void**>(&pF));
if (SUCCEEDED(hr))
{
hr=pGraph->AddFilter(pF,wszName);
if (SUCCEEDED(hr))
*ppF=pF;
else
pF->Release();
}
return hr;
}
HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath)
{
const WCHAR wszStreamName[] = L"ActiveMovieGraph";
HRESULT hr;
IStorage *pStorage = NULL;
hr = StgCreateDocfile(
wszPath,
STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, &pStorage);
if(FAILED(hr))
{
return hr;
}
IStream *pStream;
hr = pStorage->CreateStream(
wszStreamName,
STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
0, 0, &pStream);
if (FAILED(hr))
{
pStorage->Release();
return hr;
}
IPersistStream *pPersist = NULL;
pGraph->QueryInterface(IID_IPersistStream, (void**)&pPersist);
hr = pPersist->Save(pStream, TRUE);
pStream->Release();
pPersist->Release();
if (SUCCEEDED(hr))
{
hr = pStorage->Commit(STGC_DEFAULT);
}
pStorage->Release();
return hr;
}
Harries.bai
http://www.paipaitxt.com/u.php?a=info&uid=56587091
http://www.paipaitxt.com/u.php?a=info&uid=56587131
http://www.paipaitxt.com/u.php?a=info&uid=56587183
http://www.paipaitxt.com/u.php?a=info&uid=56587197
http://www.paipaitxt.com/u.php?a=info&uid=56587215
http://www.paipaitxt.com/u.php?a=info&uid=56611555
http://www.paipaitxt.com/u.php?a=info&uid=56611591
http://www.paipaitxt.com/u.php?a=info&uid=56611599
http://www.paipaitxt.com/u.php?a=info&uid=56611613
http://www.paipaitxt.com/u.php?a=info&uid=56611645
http://www.paipaitxt.com/u.php?a=info&uid=56611645
http://www.paipaitxt.com/u.php?a=info&uid=56611665
http://www.paipaitxt.com/u.php?a=info&uid=56611687
http://www.paipaitxt.com/u.php?a=info&uid=56611717
http://www.paipaitxt.com/u.php?a=info&uid=56611737
http://www.paipaitxt.com/u.php?a=info&uid=56611761
http://www.paipaitxt.com/u.php?a=info&uid=56611779
http://www.paipaitxt.com/u.php?a=info&uid=56611789
http://www.paipaitxt.com/u.php?a=info&uid=56587131
http://www.paipaitxt.com/u.php?a=info&uid=56587183
http://www.paipaitxt.com/u.php?a=info&uid=56587197
http://www.paipaitxt.com/u.php?a=info&uid=56587215
http://www.paipaitxt.com/u.php?a=info&uid=56611555
http://www.paipaitxt.com/u.php?a=info&uid=56611591
http://www.paipaitxt.com/u.php?a=info&uid=56611599
http://www.paipaitxt.com/u.php?a=info&uid=56611613
http://www.paipaitxt.com/u.php?a=info&uid=56611645
http://www.paipaitxt.com/u.php?a=info&uid=56611645
http://www.paipaitxt.com/u.php?a=info&uid=56611665
http://www.paipaitxt.com/u.php?a=info&uid=56611687
http://www.paipaitxt.com/u.php?a=info&uid=56611717
http://www.paipaitxt.com/u.php?a=info&uid=56611737
http://www.paipaitxt.com/u.php?a=info&uid=56611761
http://www.paipaitxt.com/u.php?a=info&uid=56611779
http://www.paipaitxt.com/u.php?a=info&uid=56611789