//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 04/03/2003 // PROJECT: // SUBSYSTEM: // FILE: DirectShowMovieStream.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of DirectShowMovieStream.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include #include #include #include "DirectShowStreamMovie.h" #include "DirectShowSampleGrabberCB.h" #include "DirectShowVideo.h" CDirectShowStreamMovie::CDirectShowStreamMovie() { m_pGraphBuilder = NULL; m_pMediaControl = NULL; m_pMediaEvent = NULL; m_pVideoWindow = NULL; m_pMediaSeeking = NULL; m_pSampleGrabber = NULL; m_pBitmapInfoHeader = NULL; ::CoInitializeEx( NULL, COINIT_MULTITHREADED ); } CDirectShowStreamMovie::~CDirectShowStreamMovie() { Destroy(); ::CoUninitialize(); } BOOL CDirectShowStreamMovie::Create( LPCTSTR pFileName ) { Destroy(); // Create the filter graph. HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&m_pGraphBuilder ); if( FAILED(hr) ) return FALSE; // TCHAR pName[MAX_PATH]; // ::WideCharToMultiByte( CP_ACP, 0, PinInfo.achName, -1, pName, MAX_PATH, 0, 0 ); WCHAR wFileName[MAX_PATH]; ::MultiByteToWideChar( CP_ACP, 0, pFileName, -1, wFileName, MAX_PATH ); // Have the graph builder construct its the appropriate graph automatically hr = m_pGraphBuilder->RenderFile( wFileName, NULL ); if( FAILED(hr) ) return FALSE; // QueryInterface for DirectShow interfaces hr = m_pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl); if( FAILED(hr) ) return FALSE; hr = m_pGraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&m_pMediaEvent); if( FAILED(hr) ) return FALSE; hr = m_pGraphBuilder->QueryInterface(IID_IMediaSeeking, (void **)&m_pMediaSeeking); if( FAILED(hr) ) return FALSE; /* hr = m_pGraphBuilder->QueryInterface(IID_IMediaPosition, (void **)&pMP); if( FAILED(hr) ) return FALSE; */ // create grabber filter IBaseFilter *pGrabberFilter = NULL; hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&pGrabberFilter ); if( FAILED(hr) ) return FALSE; // create sample grabber ISampleGrabber *pSampleGrabber = NULL; hr = pGrabberFilter->QueryInterface( IID_ISampleGrabber, (void **)&pSampleGrabber ); if( FAILED(hr) ) { pGrabberFilter->Release(); return FALSE; } // add grabber filter to filter graph hr = m_pGraphBuilder->AddFilter( pGrabberFilter, L"Grabber" ); if( FAILED(hr) ) { pGrabberFilter->Release(); pSampleGrabber->Release(); return FALSE; } // set video capture format AM_MEDIA_TYPE mt; ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE)); mt.majortype = MEDIATYPE_Video; mt.subtype = MEDIASUBTYPE_RGB24; mt.formattype = FORMAT_VideoInfo; hr = pSampleGrabber->SetMediaType(&mt); if( FAILED(hr) ) { pGrabberFilter->Release(); pSampleGrabber->Release(); return FALSE; } // Query for video interfaces IVideoWindow *pVW = NULL; hr = m_pGraphBuilder->QueryInterface(IID_IVideoWindow, (void **)&pVW); if( FAILED(hr) ) return FALSE; pVW->put_Visible( OAFALSE ); pVW->put_AutoShow( OAFALSE ); pVW->Release(); // insert grabber filter IBaseFilter *pVideoRenderer = FindVideoRenderer( m_pGraphBuilder ); if( !pVideoRenderer ) return FALSE; AppendGrabber( pVideoRenderer, pGrabberFilter ); pVideoRenderer->Release(); pGrabberFilter->Release(); // Run the graph to play the media file hr = m_pMediaControl->Run(); if( FAILED(hr) ) return FALSE; return CThread::Start( FALSE ); } void CDirectShowStreamMovie::Destroy() { CThread::Stop( TRUE ); } BOOL CDirectShowStreamMovie::AppendGrabber( IBaseFilter *pVideoRenderer, IBaseFilter *pGrabberFilter ) { if( !pVideoRenderer || !pGrabberFilter ) return FALSE; // get pins enumeration IEnumPins *pEnumPins = NULL; HRESULT hr = pVideoRenderer->EnumPins( &pEnumPins ); if( FAILED(hr) ) return FALSE; IPin *pPinIn = NULL; while( pEnumPins->Next( 1, &pPinIn, NULL ) == S_OK ) { // get pin info PIN_INFO PinInfo; hr = pPinIn->QueryPinInfo( &PinInfo ); if( FAILED( hr ) ) { pPinIn->Release(); pPinIn = NULL; continue; } // check if it is output pin if( PinInfo.dir == PINDIR_OUTPUT ) { pPinIn->Release(); pPinIn = NULL; continue; } // get connected pin IPin *pPinOut = NULL; hr = pPinIn->ConnectedTo( &pPinOut ); if( FAILED( hr ) ) { pPinIn->Release(); pPinIn = NULL; continue; } // get pin info hr = pPinIn->QueryPinInfo( &PinInfo ); if( FAILED( hr ) ) { pPinIn->Release(); pPinIn = NULL; pPinOut->Release(); continue; } // disconnect pins if( SUCCEEDED( m_pGraphBuilder->Disconnect( pPinOut ) ) && SUCCEEDED( m_pGraphBuilder->Disconnect( pPinIn ) ) ) { IEnumPins *pEnumGrabberPins = NULL; hr = pGrabberFilter->EnumPins( &pEnumGrabberPins ); if( SUCCEEDED(hr) ) { IPin *pGrabberPin = NULL; while( pEnumGrabberPins->Next( 1, &pGrabberPin, NULL ) == S_OK ) { // get pin info PIN_INFO GrabberPinInfo; hr = pGrabberPin->QueryPinInfo( &GrabberPinInfo ); if( FAILED( hr ) ) { pGrabberPin->Release(); pGrabberPin = NULL; continue; } // check if it is output pin if( GrabberPinInfo.dir == PINDIR_INPUT ) { hr = m_pGraphBuilder->Connect( pPinOut, pGrabberPin ); } else { hr = m_pGraphBuilder->Connect( pGrabberPin, pPinIn ); } GrabberPinInfo.pFilter->Release(); pGrabberPin->Release(); } } } PinInfo.pFilter->Release(); pPinIn->Release(); pPinOut->Release(); } pEnumPins->Release(); return TRUE; } DWORD CDirectShowStreamMovie::ThreadProc() { HANDLE hMediaEvent; m_pMediaEvent->GetEventHandle( (OAEVENT*) &hMediaEvent ); HANDLE pEvents[] = { CThread::GetThreadStopEvent(), hMediaEvent }; BOOL bRun = TRUE; while( bRun ) { switch( ::WaitForMultipleObjects( 2, pEvents, FALSE, INFINITE ) ) { case WAIT_OBJECT_0: case WAIT_FAILED: default: bRun = FALSE; continue; case WAIT_OBJECT_0+1: break; } LONG evCode, evParam1, evParam2; // Process all queued events while(SUCCEEDED(m_pMediaEvent->GetEvent(&evCode, (LONG_PTR *) &evParam1, (LONG_PTR *) &evParam2, 0))) { // Free memory associated with callback, since we're not using it HRESULT hr = m_pMediaEvent->FreeEventParams(evCode, evParam1, evParam2); // If this is the end of the clip, reset to beginning if(EC_COMPLETE == evCode) { LONGLONG pos=0; // Reset to first frame of movie hr = m_pMediaSeeking->SetPositions(&pos, AM_SEEKING_AbsolutePositioning , NULL, AM_SEEKING_NoPositioning); if (FAILED(hr)) { // Some custom filters (like the Windows CE MIDI filter) // may not implement seeking interfaces (IMediaSeeking) // to allow seeking to the start. In that case, just stop // and restart for the same effect. This should not be // necessary in most cases. if (FAILED(hr = m_pMediaControl->Stop())) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CDirectShowStreamMovie::ThreadProc"), _T("Failed(0x%08lx) to stop media clip"), hr ); break; } if (FAILED(hr = m_pMediaControl->Run())) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CDirectShowStreamMovie::ThreadProc"), _T("Failed(0x%08lx) to reset media clip"), hr); break; } } } } } return 0UL; } IBaseFilter *CDirectShowStreamMovie::FindVideoRenderer(IGraphBuilder *pGraphBuilder) { if( !pGraphBuilder ) return FALSE; // get filter graph IFilterGraph *pFilterGraph = NULL; HRESULT hr = pGraphBuilder->QueryInterface( IID_IFilterGraph, (void**)&pFilterGraph ); if( FAILED(hr) ) return FALSE; // enumerate all filters IEnumFilters *pEnumFilters = NULL; hr = pFilterGraph->EnumFilters( &pEnumFilters ); if( FAILED(hr) ) { pFilterGraph->Release(); return FALSE; } // free filter graph pFilterGraph->Release(); // step throught all filters IBaseFilter *pFilter = NULL; while( pEnumFilters->Next( 1, &pFilter, NULL ) == S_OK ) { IVideoWindow *pVideoWindow = NULL; hr = pFilter->QueryInterface( IID_IBasicVideo, (void**)pVideoWindow ); if( SUCCEEDED( hr ) ) { pVideoWindow->Release(); pEnumFilters->Release(); return pFilter; } FILTER_INFO fi; memset( &fi, 0, sizeof(fi) ); hr = pFilter->QueryFilterInfo( &fi ); if( FAILED(hr) ) { pFilter->Release(); if( fi.pGraph ) fi.pGraph->Release(); continue; } TCHAR pName[MAX_PATH]; ::WideCharToMultiByte( CP_ACP, 0, fi.achName, -1, pName, MAX_PATH, 0, 0 ); if( !_tcscmp( pName, _T("Video Renderer") ) ) { pEnumFilters->Release(); if( fi.pGraph ) fi.pGraph->Release(); return pFilter; } pFilter->Release(); if( fi.pGraph ) fi.pGraph->Release(); } return FALSE; } BOOL CDirectShowStreamMovie::InitCapturer() { if( !m_pSampleGrabber ) return FALSE; HRESULT hr = m_pSampleGrabber->SetOneShot( FALSE ); if( FAILED( hr ) ) return FALSE; hr = m_pSampleGrabber->SetBufferSamples( TRUE ); if( FAILED( hr ) ) return FALSE; // retrieve media type AM_MEDIA_TYPE mt; if( FAILED( m_pSampleGrabber->GetConnectedMediaType(&mt) ) ) return FALSE; if( mt.formattype != FORMAT_VideoInfo ) return FALSE; // store video format VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat; if( !pVideoHeader ) return FALSE; // store video format m_pBitmapInfoHeader = new BITMAPINFOHEADER; if( !m_pBitmapInfoHeader ) return FALSE; memcpy( m_pBitmapInfoHeader, &pVideoHeader->bmiHeader, sizeof(BITMAPINFOHEADER) ); // release video format ::CoTaskMemFree( mt.pbFormat ); // create ISampleGrabberCB object m_pDirectShowSampleGrabberCB = new CDirectShowSampleGrabberCB(); if( !m_pDirectShowSampleGrabberCB ) return FALSE; // link to itself m_pDirectShowSampleGrabberCB->SetDirectShowStream( this ); // install callback interface hr = m_pSampleGrabber->SetCallback( m_pDirectShowSampleGrabberCB, 1 ); if( FAILED( hr ) ) return FALSE; return TRUE; }