|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DirectShowStream.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DirectShowStream.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\Version.h"
#include "DirectShowStream.h"
#include "DirectShowVideo.h"
#include "DirectShowSampleGrabberCB.h"
CDirectShowStream::CDirectShowStream(){ m_pSampleGrabber = NULL; m_pDirectShowSampleGrabberCB = NULL; m_pBitmapInfoHeader = NULL;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStream::CDirectShowStream" );}
CDirectShowStream::~CDirectShowStream(){ Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStream::~CDirectShowStream" );}
BOOL CDirectShowStream::Create( ISampleGrabber *pSampleGrabber ){ Destroy();
if( !pSampleGrabber ) return FALSE;
HRESULT hr = pSampleGrabber->QueryInterface( IID_ISampleGrabber, (void**)&m_pSampleGrabber ); if( FAILED(hr) ) return FALSE;
return InitCapturer();}
void CDirectShowStream::Destroy(){ LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CDirectShowStream::Destroy"), _T("Started...") );
// unlink all video
CLgLinkP<CDirectShowStream,CDirectShowVideo>::Unlink();
// unlink sample grabber
CLgLinkSP<CDirectShowStream,CDirectShowSampleGrabberCB>::Unlink();
if( m_pSampleGrabber ) { m_pSampleGrabber->SetCallback( NULL, 1 ); m_pSampleGrabber->Release(); m_pSampleGrabber = NULL; }
if( m_pDirectShowSampleGrabberCB ) { m_pDirectShowSampleGrabberCB->Release(); m_pDirectShowSampleGrabberCB = NULL; }
if( m_pBitmapInfoHeader ) { delete m_pBitmapInfoHeader; m_pBitmapInfoHeader = NULL; }
LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CDirectShowStream::Destroy"), _T("Done") );}
BOOL CDirectShowStream::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->Link( this );
// install callback interface
hr = m_pSampleGrabber->SetCallback( m_pDirectShowSampleGrabberCB, 1 ); if( FAILED( hr ) ) return FALSE;
return TRUE;}
BOOL CDirectShowStream::Link( CDirectShowVideo *pDirectShowVideo, INT nSource ){ if( !pDirectShowVideo ) return FALSE;
// protect
CLgLocker Lock( &CLgLinkP<CDirectShowStream,CDirectShowVideo>::GetLock() );
// make reference
CLgArray< CLgLink<CDirectShowVideo,CDirectShowStream>* >& arrVideo = *this;
// check for pin avalibility
for( INT i = 0; i < arrVideo.GetCount(); i++ ) { CDirectShowVideo *pTempDirectShowVideo = *arrVideo[i]; if( pTempDirectShowVideo->GetVideoInput() == nSource ) return pTempDirectShowVideo == pDirectShowVideo; }
// try to connect
if( !CLgLinkP<CDirectShowStream,CDirectShowVideo>::Link( pDirectShowVideo ) ) return FALSE;
// store video input index
pDirectShowVideo->SetVideoInput( nSource );
return TRUE;}
|