Old fashion design for PC-104
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.
 
 
 
 
 

603 lines
15 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 07/12/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DirectShowStreamVideo.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DirectShowStreamVideo.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\Version.h"
#include "DirectShowStreamVideo.h"
#include "DirectShowSampleGrabberCB.h"
#include "DirectShowVideo.h"
CDirectShowStreamVideo::CDirectShowStreamVideo()
{
m_pVideoWindow = NULL;
m_pAMVideoProcAmp = NULL;
m_pAMAnalogVideoDecoder = NULL;
m_pAMCrossbar = NULL;
m_pInputPin = NULL;
m_pCaptureFilter = NULL;
m_nInputPinCount = 0;
m_nOutputPin = -1;
m_nDirectShowVideoSwitch = 0;
m_nDirectShowVideoSource = -1;
m_nDirectShowVideoFrame = 0;
m_nDirectShowVideoStandard = AnalogVideo_None;
m_nVideoFPS = 3;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStreamVideo::CDirectShowStreamVideo" );
}
CDirectShowStreamVideo::~CDirectShowStreamVideo()
{
Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStreamVideo::~CDirectShowStreamVideo" );
}
BOOL CDirectShowStreamVideo::Create( IBaseFilter *pSourceFilter, IBaseFilter *pCaptureFilter )
{
Destroy();
if( !pCaptureFilter || FAILED( pCaptureFilter->QueryInterface( IID_IBaseFilter, (void**)&m_pCaptureFilter ) ) )
m_pCaptureFilter = NULL;
// make copy of crossbar if exist
if( FAILED( pSourceFilter->QueryInterface( IID_IAMCrossbar, (void**)&m_pAMCrossbar ) ) )
m_pAMCrossbar = NULL;
IBaseFilter *pMemberFilter = NULL;
if( FAILED(pSourceFilter->QueryInterface( IID_IBaseFilter, (void**)&pMemberFilter ) ) )
return FALSE;
// search for video window
m_pVideoWindow = (IVideoWindow*)FindInterface( pMemberFilter, IID_IVideoWindow );
if( m_pVideoWindow )
{
// hide video window
m_pVideoWindow->put_Visible( OAFALSE );
m_pVideoWindow->put_AutoShow( OAFALSE );
}
// search for analog video amplifier
m_pAMVideoProcAmp = (IAMVideoProcAmp*)FindInterface( pMemberFilter, IID_IAMVideoProcAmp );
if( m_pAMVideoProcAmp && 0 )
{
// make gray-scale video
SetVideoSaturation( 0 );
// set contrast
SetVideoContrast( 50 );
// set brightness
SetVideoBrightness( 50 );
}
// search for analog video decoder
m_pAMAnalogVideoDecoder = (IAMAnalogVideoDecoder*)FindInterface( pMemberFilter, IID_IAMAnalogVideoDecoder );
// search for sample grabber
ISampleGrabber *pSampleGrabber = (ISampleGrabber*)FindInterface( pMemberFilter, IID_ISampleGrabber );
if( !pSampleGrabber )
{
pMemberFilter->Release();
return FALSE;
}
if( !CDirectShowStream::Create( pSampleGrabber ) )
{
pSampleGrabber->Release();
pMemberFilter->Release();
return FALSE;
}
pSampleGrabber->Release();
pMemberFilter->Release();
// prepare swiching
if( m_pAMCrossbar )
{
if( !InitSwitcher() )
return FALSE;
}
else
{
m_nInputPinCount = 1;
m_pInputPin = new LONG[m_nInputPinCount];
if( !m_pInputPin )
{
m_nInputPinCount = 0;
return FALSE;
}
m_pInputPin[0] = 0;
m_nOutputPin = 0;
}
#ifdef __INCLUDE__VIDEO__MEDIA__SERVER__
m_MediaServer.Create( m_pBitmapInfoHeader );
#endif // __INCLUDE__VIDEO__MEDIA__SERVER__
return TRUE;
}
void CDirectShowStreamVideo::Destroy()
{
CDirectShowStream::Destroy();
#ifdef __INCLUDE__VIDEO__MEDIA__SERVER__
m_MediaServer.Destroy();
#endif // __INCLUDE__VIDEO__MEDIA__SERVER__
if( m_pAMCrossbar )
{
m_pAMCrossbar->Release();
m_pAMCrossbar = NULL;
}
if( m_pVideoWindow )
{
m_pVideoWindow->Release();
m_pVideoWindow = NULL;
}
if( m_pAMVideoProcAmp )
{
m_pAMVideoProcAmp->Release();
m_pAMVideoProcAmp = NULL;
}
if( m_pAMAnalogVideoDecoder )
{
m_pAMAnalogVideoDecoder->Release();
m_pAMAnalogVideoDecoder = NULL;
}
if( m_pInputPin )
{
delete [] m_pInputPin;
m_pInputPin = NULL;
}
if( m_pCaptureFilter )
{
m_pCaptureFilter->Release();
m_pCaptureFilter = NULL;
}
m_nInputPinCount = 0;
m_nOutputPin = -1;
m_nDirectShowVideoSwitch = 0;
m_nDirectShowVideoSource = -1;
m_nDirectShowVideoFrame = 0;
}
BOOL CDirectShowStreamVideo::SetVideoStandart( long lVideoStandard )
{
if( !m_pAMAnalogVideoDecoder )
return FALSE;
if( FAILED( m_pAMAnalogVideoDecoder->put_TVFormat( lVideoStandard ) ) )
return FALSE;
m_nDirectShowVideoStandard = lVideoStandard;
return TRUE;
}
BOOL CDirectShowStreamVideo::SetVideoFPS( INT nFPS )
{
m_nVideoFPS = nFPS;
return TRUE;
}
BOOL CDirectShowStreamVideo::IsSwitchable()
{
return m_pAMCrossbar != NULL;
}
INT CDirectShowStreamVideo::GetVideoSourceCount()
{
return m_nInputPinCount;
}
BOOL CDirectShowStreamVideo::SetVideoSource( INT nSource )
{
if( !m_pAMCrossbar && !nSource )
return TRUE;
if( nSource < 0 || nSource >= m_nInputPinCount || !m_pAMCrossbar )
return FALSE;
if( m_nOutputPin == -1 )
return FALSE;
if( !m_pAMCrossbar )
return FALSE;
if( FAILED( m_pAMCrossbar->Route( m_nOutputPin, m_pInputPin[ nSource ] ) ) )
return FALSE;
m_nDirectShowVideoSource = nSource;
return TRUE;
}
PVOID CDirectShowStreamVideo::FindInterface( IBaseFilter *pFilter, REFIID iid )
{
if( !pFilter )
return NULL;
IEnumPins *pEnumPins = NULL;
HRESULT hr = pFilter->EnumPins( &pEnumPins );
if( FAILED(hr) )
return NULL;
PVOID pInterface = NULL;
while( !pInterface )
{
IPin *pPinOut = NULL;
// get next pin
if( pEnumPins->Next( 1, &pPinOut, NULL ) != S_OK )
break;
// get pin information
PIN_INFO PinInfo;
hr = pPinOut->QueryPinInfo( &PinInfo );
if( FAILED( hr ) )
{
pPinOut->Release();
continue;
}
if( PinInfo.pFilter )
PinInfo.pFilter->Release();
// check direction
if( PinInfo.dir == PINDIR_INPUT )
{
pPinOut->Release();
continue;
}
// get pin connected to this pin
IPin *pPinIn = NULL;
hr = pPinOut->ConnectedTo( &pPinIn );
if( FAILED( hr ) )
{
pPinOut->Release();
continue;
}
hr = pPinIn->QueryPinInfo( &PinInfo );
if( FAILED( hr ) )
{
pPinOut->Release();
pPinIn->Release();
continue;
}
/*
TCHAR pName[MAX_PATH];
::WideCharToMultiByte( CP_ACP, 0, PinInfo.achName, -1, pName, MAX_PATH, 0, 0 );
FILTER_INFO fi;
PinInfo.pFilter->QueryFilterInfo( &fi );
::WideCharToMultiByte( CP_ACP, 0, fi.achName, -1, pName, MAX_PATH, 0, 0 );
if( fi.pGraph )
fi.pGraph->Release();
*/
if( (!PinInfo.pFilter) || FAILED( PinInfo.pFilter->QueryInterface( iid, &pInterface ) ) )
pInterface = FindInterface( PinInfo.pFilter, iid );
if( PinInfo.pFilter )
PinInfo.pFilter->Release();
pPinOut->Release();
pPinIn->Release();
}
pEnumPins->Release();
return pInterface;
}
BOOL CDirectShowStreamVideo::InitSwitcher()
{
if( !m_pAMCrossbar )
return FALSE;
long OutputPinCount;
long InputPinCount;
HRESULT hr = m_pAMCrossbar->get_PinCounts( &OutputPinCount, &InputPinCount );
if( FAILED( hr ) )
return FALSE;
for( long i = 0; i < OutputPinCount && m_nOutputPin < 0; i++ )
{
long PinIndexRelated;
long PhysicalType;
HRESULT hr = m_pAMCrossbar->get_CrossbarPinInfo( FALSE, i, &PinIndexRelated, &PhysicalType );
if( FAILED( hr ) )
return FALSE;
switch( PhysicalType )
{
case PhysConn_Video_Tuner:
case PhysConn_Video_Composite:
case PhysConn_Video_SVideo:
case PhysConn_Video_RGB:
case PhysConn_Video_YRYBY:
case PhysConn_Video_SerialDigital:
case PhysConn_Video_ParallelDigital:
case PhysConn_Video_SCSI:
case PhysConn_Video_AUX:
case PhysConn_Video_1394:
case PhysConn_Video_USB:
case PhysConn_Video_VideoDecoder:
case PhysConn_Video_VideoEncoder:
case PhysConn_Video_SCART:
m_nOutputPin = i;
break;
}
}
PLONG pInputPin = new LONG[ InputPinCount ];
if( !pInputPin )
return FALSE;
long nInput = 0;
for( i = 0; i < InputPinCount; i++ )
{
long PinIndexRelated = 0;
long PhysicalType = -1;
HRESULT hr = m_pAMCrossbar->get_CrossbarPinInfo( TRUE, i, &PinIndexRelated, &PhysicalType );
if( FAILED( hr ) )
{
delete [] pInputPin;
return FALSE;
}
switch( PhysicalType )
{
case PhysConn_Video_Tuner:
case PhysConn_Video_Composite:
case PhysConn_Video_SVideo:
case PhysConn_Video_RGB:
case PhysConn_Video_YRYBY:
case PhysConn_Video_SerialDigital:
case PhysConn_Video_ParallelDigital:
case PhysConn_Video_SCSI:
case PhysConn_Video_AUX:
case PhysConn_Video_1394:
case PhysConn_Video_USB:
case PhysConn_Video_VideoDecoder:
case PhysConn_Video_VideoEncoder:
case PhysConn_Video_SCART:
pInputPin[ nInput++ ] = i;
break;
}
}
m_nInputPinCount = nInput;
m_pInputPin = new LONG[ m_nInputPinCount ];
if( !m_pInputPin )
{
delete [] pInputPin;
return FALSE;
}
::CopyMemory( m_pInputPin, pInputPin, sizeof(LONG) * m_nInputPinCount );
delete [] pInputPin;
return TRUE;
}
void CDirectShowStreamVideo::OnVideoSample( double SampleTime, BYTE *pBuffer, long BufferLen )
{
if( !m_pBitmapInfoHeader || !pBuffer )
return;
#ifdef __INCLUDE__VIDEO__MEDIA__SERVER__
QWORD qTime = 10000i64 * (QWORD)GetTickCount();
if( !m_MediaServer.WriteSample( qTime, pBuffer, BufferLen ) )
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CDirectShowStreamVideo::OnVideoSample"), _T("m_MediaServer.WriteSample failed") );
#endif
// protect
CLgLocker Lock( &CLgLinkP<CDirectShowStream,CDirectShowVideo>::GetLock() );
// make reference
CLgArray< CLgLink<CDirectShowVideo,CDirectShowStream>* >& arrVideo = *this;
if( arrVideo.GetCount() )
{
// determine number of visible windows
INT nVisibleCount = 0;
for( INT i = 0; i < arrVideo.GetCount(); i++ )
{
CDirectShowVideo *pDirectShowVideo = *arrVideo[i];
if( pDirectShowVideo->IsVideoShow() )
nVisibleCount ++;
}
// no visible windows
if( !nVisibleCount )
return;
// take every second frame
if( ++m_nDirectShowVideoFrame >= m_nVideoFPS || nVisibleCount == 1 )
{
// update current input
if( m_nDirectShowVideoSwitch < arrVideo.GetCount() )
{
// update image
CDirectShowVideo *pDirectShowVideo = *arrVideo[ m_nDirectShowVideoSwitch ];
pDirectShowVideo->UpdateVideo( m_pBitmapInfoHeader, pBuffer, SampleTime );
}
// get next input
for( INT i = 0; i < arrVideo.GetCount(); i++ )
{
// take next window
m_nDirectShowVideoSwitch = (m_nDirectShowVideoSwitch + 1) % arrVideo.GetCount();
CDirectShowVideo *pDirectShowVideo = *arrVideo[ m_nDirectShowVideoSwitch ];
// if new source differs from old source then switch
if( m_nDirectShowVideoSource != pDirectShowVideo->GetVideoInput() && pDirectShowVideo->IsVideoShow() )
{
// check video standard
if( m_nDirectShowVideoStandard != pDirectShowVideo->GetVideoStandard() &&
pDirectShowVideo->GetVideoStandard() != AnalogVideo_None )
{
// switch video standard
SetVideoStandart( pDirectShowVideo->GetVideoStandard() );
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CDirectShowStreamVideo::OnVideoSample", "video standard switched to %d", m_nDirectShowVideoStandard );
}
// switch video source
SetVideoSource( pDirectShowVideo->GetVideoInput() );
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CDirectShowStreamVideo::OnVideoSample", "video input switched to %d", m_nDirectShowVideoSource );
break;
}
}
// reset frame counter
m_nDirectShowVideoFrame = 0;
}
}
}
BOOL CDirectShowStreamVideo::SetVideoContrast(double dPercent)
{
if( !m_pAMVideoProcAmp )
return FALSE;
// get limits
LONG nMin = 0;
LONG nMax = 0;
LONG nSteppingDelta = 0;
LONG nDefault = 0;
LONG nCapsFlags = 0;
HRESULT hr = m_pAMVideoProcAmp->GetRange( VideoProcAmp_Contrast, &nMin, &nMax, &nSteppingDelta, &nDefault, &nCapsFlags );
if( FAILED(hr) )
return FALSE;
// prepare new value
LONG nContrast = (LONG)( nMin + ( nMax - nMin ) * dPercent / 100.0 );
// check bounds
if( nContrast > nMax ) nContrast = nMax;
if( nContrast < nMin ) nContrast = nMin;
// make contrast
hr = m_pAMVideoProcAmp->Set( VideoProcAmp_Contrast, nContrast, VideoProcAmp_Flags_Manual );
return SUCCEEDED(hr);
}
BOOL CDirectShowStreamVideo::SetVideoBrightness( double dPercent )
{
if( !m_pAMVideoProcAmp )
return FALSE;
// get limits
LONG nMin = 0;
LONG nMax = 0;
LONG nSteppingDelta = 0;
LONG nDefault = 0;
LONG nCapsFlags = 0;
HRESULT hr = m_pAMVideoProcAmp->GetRange( VideoProcAmp_Brightness, &nMin, &nMax, &nSteppingDelta, &nDefault, &nCapsFlags );
if( FAILED(hr) )
return FALSE;
// prepare new value
LONG nBrightness = (LONG)( nMin + ( nMax - nMin ) * dPercent / 100.0 );
// check bounds
if( nBrightness > nMax ) nBrightness = nMax;
if( nBrightness < nMin ) nBrightness = nMin;
// make bright
hr = m_pAMVideoProcAmp->Set( VideoProcAmp_Brightness, nBrightness, VideoProcAmp_Flags_Manual );
return SUCCEEDED(hr);
}
BOOL CDirectShowStreamVideo::SetVideoSaturation( double dPercent )
{
if( !m_pAMVideoProcAmp )
return FALSE;
// get limits
LONG nMin = 0;
LONG nMax = 0;
LONG nSteppingDelta = 0;
LONG nDefault = 0;
LONG nCapsFlags = 0;
HRESULT hr = m_pAMVideoProcAmp->GetRange( VideoProcAmp_Saturation, &nMin, &nMax, &nSteppingDelta, &nDefault, &nCapsFlags );
if( FAILED(hr) )
return FALSE;
// prepare new value
LONG nSaturation = (LONG)( nMin + ( nMax - nMin ) * dPercent / 100.0 );
// check bounds
if( nSaturation > nMax ) nSaturation = nMax;
if( nSaturation < nMin ) nSaturation = nMin;
// make bright
hr = m_pAMVideoProcAmp->Set( VideoProcAmp_Saturation, nSaturation, VideoProcAmp_Flags_Manual );
return SUCCEEDED(hr);
}
BOOL CDirectShowStreamVideo::ShowPropertyPage( HWND hParentWnd )
{
IBaseFilter *pFilter = m_pCaptureFilter;
/* Obtain the filter's IBaseFilter interface. (Not shown) */
ISpecifyPropertyPages *pProp;
HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pProp);
if (SUCCEEDED(hr))
{
// Get the filter's name and IUnknown pointer.
FILTER_INFO FilterInfo;
pFilter->QueryFilterInfo(&FilterInfo);
IUnknown *pFilterUnk;
pFilter->QueryInterface(IID_IUnknown, (void **)&pFilterUnk);
// Show the page.
CAUUID caGUID;
pProp->GetPages(&caGUID);
pProp->Release();
OleCreatePropertyFrame(
hParentWnd, // Parent window
0, 0, // (Reserved)
FilterInfo.achName, // Caption for the dialog box
1, // Number of objects (just the filter)
&pFilterUnk, // Array of object pointers.
caGUID.cElems, // Number of property pages
caGUID.pElems, // Array of property page CLSIDs
0, // Locale identifier
0, NULL // Reserved
);
// Clean up.
pFilterUnk->Release();
FilterInfo.pGraph->Release();
CoTaskMemFree(caGUID.pElems);
}
return TRUE;
}