You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
612 lines
18 KiB
612 lines
18 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 03/12/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgDirectShowCaptureGraph.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgDirectShowCaptureGraph.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LgDirectShowCaptureGraph.h"
|
|
#include "LgDirectShowVideoInputEnum.h"
|
|
|
|
#ifdef REGISTER_FILTERGRAPH
|
|
|
|
HRESULT CLgDirectShowCaptureGraph::AddGraphToRot( IUnknown *pUnkGraph, DWORD *pdwRegister )
|
|
{
|
|
IMoniker * pMoniker;
|
|
IRunningObjectTable *pROT;
|
|
WCHAR wsz[128];
|
|
HRESULT hr;
|
|
|
|
if (FAILED(GetRunningObjectTable(0, &pROT)))
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
|
|
_snwprintf( wsz, 128, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
|
|
GetCurrentProcessId());
|
|
|
|
hr = CreateItemMoniker(L"!", wsz, &pMoniker);
|
|
if (SUCCEEDED(hr))
|
|
{
|
|
hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
|
|
pMoniker->Release();
|
|
}
|
|
pROT->Release();
|
|
return hr;
|
|
}
|
|
|
|
void CLgDirectShowCaptureGraph::RemoveGraphFromRot( DWORD pdwRegister )
|
|
{
|
|
IRunningObjectTable *pROT;
|
|
|
|
if (SUCCEEDED(GetRunningObjectTable(0, &pROT)))
|
|
{
|
|
pROT->Revoke(pdwRegister);
|
|
pROT->Release();
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
CLgDirectShowCaptureGraph::CLgDirectShowCaptureGraph()
|
|
{
|
|
m_pGraphBuilder = NULL;
|
|
m_pCaptureGraphBuilder = NULL;
|
|
m_pMediaControl = NULL;
|
|
|
|
#ifdef REGISTER_FILTERGRAPH
|
|
m_dwGraphRegister = 0;
|
|
#endif
|
|
|
|
::CoInitializeEx( NULL, COINIT_MULTITHREADED );
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::CLgDirectShowCaptureGraph") );
|
|
}
|
|
|
|
CLgDirectShowCaptureGraph::~CLgDirectShowCaptureGraph()
|
|
{
|
|
Destroy();
|
|
|
|
::CoUninitialize();
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::~CLgDirectShowCaptureGraph") );
|
|
}
|
|
|
|
BOOL CLgDirectShowCaptureGraph::Create()
|
|
{
|
|
Destroy();
|
|
|
|
// Create the filter graph.
|
|
HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&m_pGraphBuilder );
|
|
if( FAILED(hr) || !m_pGraphBuilder )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("CLgDirectShowCaptureGraph::Create(IID_IGraphBuilder) failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
// Create the capture graph builder.
|
|
hr = ::CoCreateInstance( CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&m_pCaptureGraphBuilder );
|
|
if( FAILED(hr) || !m_pCaptureGraphBuilder )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("CLgDirectShowCaptureGraph::Create(IID_ICaptureGraphBuilder2) failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
// connect filter graph with capture filter graph
|
|
hr = m_pCaptureGraphBuilder->SetFiltergraph(m_pGraphBuilder);
|
|
if( FAILED(hr) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("m_pCaptureGraphBuilder->SetFiltergraph failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
// Add our graph to the running object table, which will allow
|
|
// the GraphEdit application to "spy" on our graph
|
|
#ifdef REGISTER_FILTERGRAPH
|
|
hr = AddGraphToRot( m_pGraphBuilder, &m_dwGraphRegister );
|
|
if (FAILED(hr))
|
|
m_dwGraphRegister = 0;
|
|
#endif
|
|
|
|
m_pGraphBuilder->QueryInterface( IID_IMediaControl, (void**)&m_pMediaControl );
|
|
if( !m_pMediaControl )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("m_pGraphBuilder->QueryInterface(IID_IMediaControl) failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::Create"), _T("Succeeded") );
|
|
return TRUE;
|
|
}
|
|
|
|
void CLgDirectShowCaptureGraph::Destroy()
|
|
{
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Destroy", "start..." );
|
|
|
|
#ifdef REGISTER_FILTERGRAPH
|
|
// Remove filter graph from the running object table
|
|
if( m_dwGraphRegister )
|
|
{
|
|
RemoveGraphFromRot( m_dwGraphRegister );
|
|
m_dwGraphRegister = FALSE;
|
|
}
|
|
#endif
|
|
|
|
Stop();
|
|
|
|
while( m_StreamArray.GetCount() )
|
|
{
|
|
m_StreamArray[0].pStream->Destroy();
|
|
delete m_StreamArray[0].pStream;
|
|
m_StreamArray.Remove(0);
|
|
}
|
|
|
|
if( m_pMediaControl )
|
|
{
|
|
m_pMediaControl->Release();
|
|
m_pMediaControl = NULL;
|
|
}
|
|
|
|
if( m_pCaptureGraphBuilder )
|
|
{
|
|
m_pCaptureGraphBuilder->Release();
|
|
m_pCaptureGraphBuilder = NULL;
|
|
}
|
|
|
|
if( m_pGraphBuilder )
|
|
{
|
|
m_pGraphBuilder->Release();
|
|
m_pGraphBuilder = NULL;
|
|
}
|
|
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Destroy", "done" );
|
|
}
|
|
|
|
CDirectShowStreamVideo *CLgDirectShowCaptureGraph::CreateStream( LPWSTR pDisplayName )
|
|
{
|
|
if( !m_pCaptureGraphBuilder || !m_pGraphBuilder || !m_pMediaControl )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::CreateStream"), _T("Capture graph was not built yet") );
|
|
return NULL;
|
|
}
|
|
|
|
for( INT i = 0; i < m_StreamArray.GetCount(); i++ )
|
|
{
|
|
if( !wcscmp( m_StreamArray[i].pDisplayName, pDisplayName ) )
|
|
{
|
|
return m_StreamArray[i].pStream;
|
|
}
|
|
}
|
|
|
|
// Create capture filter
|
|
IBaseFilter *pCaptureFilter = CLgDirectShowVideoInputEnum::CreateCaptureFilter( pDisplayName );
|
|
if( !pCaptureFilter )
|
|
return NULL;
|
|
|
|
// add capture filter into graph
|
|
WCHAR pName[MAX_PATH];
|
|
static INT nStreamVideo = 0;
|
|
_snwprintf( pName, MAX_PATH, L"Capture Source Filter %d", nStreamVideo );
|
|
HRESULT hr = m_pGraphBuilder->AddFilter( pCaptureFilter, pName );
|
|
if( FAILED(hr) )
|
|
{
|
|
pCaptureFilter->Release();
|
|
return NULL;
|
|
}
|
|
|
|
// select best quality
|
|
// SelectBestResolution( pCaptureFilter );
|
|
|
|
// create grabber filter
|
|
IBaseFilter *pGrabberFilter = NULL;
|
|
hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&pGrabberFilter );
|
|
if( FAILED(hr) || !pGrabberFilter )
|
|
{
|
|
pCaptureFilter->Release();
|
|
return NULL;
|
|
}
|
|
|
|
// add grabber filter to filter graph
|
|
_snwprintf( pName, MAX_PATH, L"Sample Grabber Filter %d", nStreamVideo );
|
|
hr = m_pGraphBuilder->AddFilter( pGrabberFilter, pName );
|
|
if( FAILED(hr) )
|
|
{
|
|
pCaptureFilter->Release();
|
|
pGrabberFilter->Release();
|
|
return NULL;
|
|
}
|
|
|
|
// create sample grabber
|
|
ISampleGrabber *pSampleGrabber = NULL;
|
|
hr = pGrabberFilter->QueryInterface( IID_ISampleGrabber, (void **)&pSampleGrabber );
|
|
if( FAILED(hr) || !pSampleGrabber )
|
|
{
|
|
pCaptureFilter->Release();
|
|
pGrabberFilter->Release();
|
|
return NULL;
|
|
}
|
|
|
|
// 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) )
|
|
{
|
|
pCaptureFilter->Release();
|
|
pGrabberFilter->Release();
|
|
pSampleGrabber->Release();
|
|
return FALSE;
|
|
}
|
|
pSampleGrabber->Release();
|
|
|
|
// add crossbar
|
|
IBaseFilter *pCrossbarFilter = NULL;
|
|
IAMCrossbar *pCrossbar = NULL;
|
|
hr = m_pCaptureGraphBuilder->FindInterface( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCaptureFilter, IID_IAMCrossbar, (void **)&pCrossbar );
|
|
if( SUCCEEDED(hr) && pCrossbar )
|
|
{
|
|
hr = pCrossbar->QueryInterface( IID_IBaseFilter, (void**)&pCrossbarFilter );
|
|
if( FAILED(hr) || !pCrossbarFilter )
|
|
pCrossbarFilter = NULL;
|
|
pCrossbar->Release();
|
|
}
|
|
|
|
// add null renderer
|
|
IBaseFilter *pNullRenderer = NULL;
|
|
hr = ::CoCreateInstance( CLSID_NullRenderer, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pNullRenderer );
|
|
if( SUCCEEDED(hr) || !pNullRenderer )
|
|
{
|
|
_snwprintf( pName, MAX_PATH, L"Null Renderer Filter %d", nStreamVideo++ );
|
|
hr = m_pGraphBuilder->AddFilter( pNullRenderer, pName );
|
|
if( FAILED(hr) )
|
|
{
|
|
pNullRenderer->Release();
|
|
pNullRenderer = NULL;
|
|
}
|
|
}
|
|
|
|
// render filter graph set
|
|
hr = m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCaptureFilter, pGrabberFilter, pNullRenderer );
|
|
if( pNullRenderer )
|
|
pNullRenderer->Release();
|
|
if( FAILED(hr) )
|
|
{
|
|
hr = m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCaptureFilter, pGrabberFilter, pNullRenderer );
|
|
if( FAILED(hr) )
|
|
{
|
|
if( pCrossbarFilter )
|
|
pCrossbarFilter->Release();
|
|
pCaptureFilter->Release();
|
|
pGrabberFilter->Release();
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// create video stream
|
|
CDirectShowStreamVideo *pStream = new CDirectShowStreamVideo;
|
|
if( pStream )
|
|
{
|
|
if( pStream->Create( pCrossbarFilter ? pCrossbarFilter : pCaptureFilter, pCaptureFilter ) )
|
|
{
|
|
VIDEO_STREAM_INFO vsi;
|
|
vsi.pStream = pStream;
|
|
wcsncpy( vsi.pDisplayName, pDisplayName, MAX_PATH );
|
|
m_StreamArray.Add( vsi );
|
|
}
|
|
else
|
|
{
|
|
delete pStream;
|
|
pStream = NULL;
|
|
}
|
|
}
|
|
|
|
// relase everythink
|
|
if( pCrossbarFilter )
|
|
pCrossbarFilter->Release();
|
|
pCaptureFilter->Release();
|
|
pGrabberFilter->Release();
|
|
|
|
return pStream;
|
|
}
|
|
|
|
BOOL CLgDirectShowCaptureGraph::CreateStreamAll()
|
|
{
|
|
CLgDirectShowVideoInputEnum VideoInputEnum;
|
|
if( !VideoInputEnum.Create() )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Stop"), _T("VideoInputEnum.Create failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
for( INT i = 0; i < VideoInputEnum.GetVideoInputDeviceCount(); i++ )
|
|
// for( INT i = 4; i < 5; i++ )
|
|
{
|
|
CLgDirectShowVideoInputEnum::DSVID_INFO DeviceInfo;
|
|
if( VideoInputEnum.GetVideoInputDeviceInfo( i, &DeviceInfo ) )
|
|
CreateStream( DeviceInfo.pDisplayName );
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgDirectShowCaptureGraph::Stop()
|
|
{
|
|
if( !m_pMediaControl )
|
|
return FALSE;
|
|
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "start..." );
|
|
|
|
OAFilterState fs;
|
|
while( true )
|
|
{
|
|
if( m_pMediaControl->Stop() != S_OK )
|
|
{
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "can't stop" );
|
|
return FALSE;
|
|
}
|
|
|
|
if( m_pMediaControl->GetState( 1000UL, &fs ) == S_OK )
|
|
{
|
|
if( fs == State_Stopped )
|
|
break;
|
|
}
|
|
}
|
|
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "done" );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgDirectShowCaptureGraph::Start()
|
|
{
|
|
if( !m_pMediaControl )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Start"), _T("m_pMediaControl is NULL") );
|
|
return FALSE;
|
|
}
|
|
|
|
HRESULT hr = m_pMediaControl->Run();
|
|
if( FAILED( hr ) )
|
|
{
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Start", "run failed" );
|
|
return FALSE;
|
|
}
|
|
|
|
// log info
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Start" );
|
|
return TRUE;
|
|
}
|
|
|
|
INT CLgDirectShowCaptureGraph::GetDirectShowStreamCount()
|
|
{
|
|
return m_StreamArray.GetCount();
|
|
}
|
|
|
|
CDirectShowStreamVideo* CLgDirectShowCaptureGraph::GetDirectShowStream( INT nStream )
|
|
{
|
|
return ( nStream >= 0 && nStream < m_StreamArray.GetCount() ) ? m_StreamArray[nStream].pStream : NULL;
|
|
}
|
|
|
|
BOOL CLgDirectShowCaptureGraph::SelectBestResolution( IBaseFilter *pCaptureFilter )
|
|
{
|
|
/*
|
|
IEnumPins *pEnumPins = NULL;
|
|
HRESULT hr = pCaptureFilter->EnumPins( &pEnumPins );
|
|
if( SUCCEEDED(hr) && pEnumPins )
|
|
{
|
|
IPin *pPin = NULL;
|
|
while( pEnumPins->Next( 1, &pPin, NULL ) == S_OK )
|
|
{
|
|
PIN_DIRECTION PinDir;
|
|
hr = pPin->QueryDirection( &PinDir );
|
|
if( FAILED(hr) || PinDir != PINDIR_OUTPUT )
|
|
{
|
|
pPin->Release();
|
|
continue;
|
|
}
|
|
|
|
PIN_INFO PinInfo;
|
|
memset( &PinInfo, 0, sizeof(PinInfo) );
|
|
hr = pPin->QueryPinInfo( &PinInfo );
|
|
if( PinInfo.pFilter )
|
|
PinInfo.pFilter->Release();
|
|
if( FAILED(hr) || PinInfo.dir != PINDIR_OUTPUT )
|
|
{
|
|
pPin->Release();
|
|
continue;
|
|
}
|
|
|
|
TCHAR pPinName[MAX_PATH];
|
|
WideCharToMultiByte( CP_OEMCP, 0, PinInfo.achName, -1, pPinName, MAX_PATH, NULL, NULL );
|
|
|
|
// select best quality
|
|
IAMStreamConfig *pConfig = NULL;
|
|
hr = pPin->QueryInterface( IID_IAMStreamConfig, (void**)&pConfig );
|
|
if( FAILED(hr) || !pConfig )
|
|
{
|
|
pPin->Release();
|
|
continue;
|
|
}
|
|
|
|
int iCount = 0, iSize = 0;
|
|
hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);
|
|
// Check the size to make sure we pass in the correct structure.
|
|
if( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) )
|
|
{
|
|
// Use the video capabilities structure.
|
|
AM_MEDIA_TYPE *pmtBestConfig = NULL;
|
|
VIDEO_STREAM_CONFIG_CAPS sccBestConfig;
|
|
for (int iFormat = 0; iFormat < iCount; iFormat++)
|
|
{
|
|
VIDEO_STREAM_CONFIG_CAPS scc;
|
|
AM_MEDIA_TYPE *pmtConfig;
|
|
hr = pConfig->GetStreamCaps( iFormat, &pmtConfig, (BYTE*)&scc );
|
|
if (SUCCEEDED(hr))
|
|
{
|
|
BOOL bChange = FALSE;
|
|
|
|
// Examine the format, and possibly use it.
|
|
if( (pmtConfig->majortype == MEDIATYPE_Video) &&
|
|
(pmtConfig->subtype == MEDIASUBTYPE_RGB24) &&
|
|
(pmtConfig->formattype == FORMAT_VideoInfo) &&
|
|
(pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) &&
|
|
(pmtConfig->pbFormat != NULL) )
|
|
{
|
|
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
|
|
|
|
if( pmtBestConfig )
|
|
{
|
|
VIDEOINFOHEADER *pBestVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
|
|
if( pVih->bmiHeader.biWidth > pBestVih->bmiHeader.biWidth &&
|
|
pVih->bmiHeader.biHeight > pBestVih->bmiHeader.biHeight )
|
|
{
|
|
bChange = TRUE;
|
|
}
|
|
}
|
|
else
|
|
bChange = TRUE;
|
|
|
|
// pVih contains the detailed format information.
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::CreateStream", "Found resolution %dx%dx%dx", pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight, pVih->bmiHeader.biBitCount );
|
|
}
|
|
|
|
if( bChange )
|
|
{
|
|
if( pmtBestConfig )
|
|
{
|
|
::CoTaskMemFree( pmtBestConfig->pbFormat );
|
|
::CoTaskMemFree( pmtBestConfig );
|
|
}
|
|
pmtBestConfig = pmtConfig;
|
|
sccBestConfig = scc;
|
|
}
|
|
else
|
|
{
|
|
// Delete the media type when you are done.
|
|
::CoTaskMemFree( pmtConfig->pbFormat );
|
|
::CoTaskMemFree( pmtConfig );
|
|
}
|
|
}
|
|
}
|
|
|
|
if( pmtBestConfig )
|
|
{
|
|
// VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
|
|
// pVih->bmiHeader.biWidth = sccBestConfig.MaxOutputSize.cx;
|
|
// pVih->bmiHeader.biHeight = sccBestConfig.MaxOutputSize.cy;
|
|
// pVih->bmiHeader.biSizeImage = WIDTHBYTES( pVih->bmiHeader.biBitCount * sccBestConfig.MinOutputSize.cx ) * sccBestConfig.MinOutputSize.cx;
|
|
|
|
hr = pConfig->SetFormat( pmtBestConfig );
|
|
// Delete the media type when you are done.
|
|
::CoTaskMemFree( pmtBestConfig->pbFormat );
|
|
::CoTaskMemFree( pmtBestConfig );
|
|
}
|
|
}
|
|
|
|
pConfig->Release();
|
|
pPin->Release();
|
|
}
|
|
pEnumPins->Release();
|
|
}
|
|
*/
|
|
IAMStreamConfig *pConfig = NULL;
|
|
HRESULT hr = m_pCaptureGraphBuilder->FindInterface( &PIN_CATEGORY_PREVIEW, 0, pCaptureFilter, IID_IAMStreamConfig, (void**)&pConfig );
|
|
if( SUCCEEDED( hr ) )
|
|
{
|
|
int iCount = 0, iSize = 0;
|
|
hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);
|
|
// Check the size to make sure we pass in the correct structure.
|
|
if( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) )
|
|
{
|
|
// Use the video capabilities structure.
|
|
AM_MEDIA_TYPE *pmtBestConfig = NULL;
|
|
VIDEO_STREAM_CONFIG_CAPS sccBestConfig;
|
|
for (int iFormat = 0; iFormat < iCount; iFormat++)
|
|
{
|
|
VIDEO_STREAM_CONFIG_CAPS scc;
|
|
AM_MEDIA_TYPE *pmtConfig;
|
|
hr = pConfig->GetStreamCaps( iFormat, &pmtConfig, (BYTE*)&scc );
|
|
if (SUCCEEDED(hr))
|
|
{
|
|
BOOL bChange = FALSE;
|
|
|
|
// Examine the format, and possibly use it.
|
|
if( (pmtConfig->majortype == MEDIATYPE_Video) &&
|
|
(pmtConfig->subtype == MEDIASUBTYPE_RGB24) &&
|
|
(pmtConfig->formattype == FORMAT_VideoInfo) &&
|
|
(pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) &&
|
|
(pmtConfig->pbFormat != NULL) )
|
|
{
|
|
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
|
|
|
|
if( pmtBestConfig )
|
|
{
|
|
VIDEOINFOHEADER *pBestVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
|
|
if( pVih->bmiHeader.biWidth > pBestVih->bmiHeader.biWidth &&
|
|
pVih->bmiHeader.biHeight > pBestVih->bmiHeader.biHeight )
|
|
{
|
|
bChange = TRUE;
|
|
}
|
|
}
|
|
else
|
|
bChange = TRUE;
|
|
|
|
// pVih contains the detailed format information.
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::CreateStream", "Found resolution %dx%dx%dx", pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight, pVih->bmiHeader.biBitCount );
|
|
}
|
|
|
|
if( bChange )
|
|
{
|
|
if( pmtBestConfig )
|
|
{
|
|
::CoTaskMemFree( pmtBestConfig->pbFormat );
|
|
::CoTaskMemFree( pmtBestConfig );
|
|
}
|
|
pmtBestConfig = pmtConfig;
|
|
sccBestConfig = scc;
|
|
}
|
|
else
|
|
{
|
|
// Delete the media type when you are done.
|
|
::CoTaskMemFree( pmtConfig->pbFormat );
|
|
::CoTaskMemFree( pmtConfig );
|
|
}
|
|
}
|
|
}
|
|
|
|
if( pmtBestConfig )
|
|
{
|
|
// VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
|
|
// pVih->bmiHeader.biWidth = sccBestConfig.MaxOutputSize.cx;
|
|
// pVih->bmiHeader.biHeight = sccBestConfig.MaxOutputSize.cy;
|
|
// pVih->bmiHeader.biSizeImage = WIDTHBYTES( pVih->bmiHeader.biBitCount * sccBestConfig.MinOutputSize.cx ) * sccBestConfig.MinOutputSize.cx;
|
|
// pVih->dwBitRate *= 4;
|
|
|
|
hr = pConfig->SetFormat( pmtBestConfig );
|
|
// Delete the media type when you are done.
|
|
::CoTaskMemFree( pmtBestConfig->pbFormat );
|
|
::CoTaskMemFree( pmtBestConfig );
|
|
}
|
|
}
|
|
|
|
pConfig->Release();
|
|
}
|
|
|
|
return TRUE;
|
|
}
|