//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgHWVideoCamera.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgHWVideoCamera.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LgHWVideoCamera.h" #include "LgHWVideoEngine.h" #include "LgHWVideoCapture.h" #include "LgHWVideoPin.h" #include "LgHWVideoSwitch.h" #include "LgHWVideoJob.h" CLgHWVideoCamera::CLgHWVideoCamera() { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgHWVideoCamera::CLgHWVideoCamera") ); } CLgHWVideoCamera::~CLgHWVideoCamera() { Destroy(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgHWVideoCamera::~CLgHWVideoCamera") ); } BOOL CLgHWVideoCamera::Create() { // create video device with one video output return CLgHWVideoSwitch::Create( 0, 1 ); } void CLgHWVideoCamera::Destroy() { // unlock all locked pins CameraToPinLinkP::Unlink(); // clear all outstanding jobs while( CameraToJobLinkP::GetCount() ) delete *CameraToJobLinkP::GetAt(0); // nessesary to call for prevent pure functions call CLgHWVideoSwitch::Destroy(); } BOOL CLgHWVideoCamera::HWVideoCameraActivate( BOOL bActivate ) { if( bActivate ) { // if already linked if( CameraToCaptureLinkSP::GetLinkThis() ) { // notify about availibility OnHWVideoCameraActivate( bActivate ); return TRUE; } CLgHWVideoEngine *pEngine = *this; if( !pEngine ) return FALSE; // find availible video input CLgArray< CLgLink* >& arrInput = *pEngine; for( INT i = 0; i < arrInput.GetCount(); i++ ) { CLgHWVideoCapture *pCapture = *arrInput[i]; if( !pCapture ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_WARNING, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("No video capturers") ); return FALSE; } // if linked to another camera if( pCapture->CaptureToCameraLinkSP::GetLinkThis() ) continue; // prepare pointer to video input pin of video input CLgHWVideoPin *pPinInput = pCapture->GetVideoPinInput(); if( !pPinInput ) { // should be called CLgHWVideoInput::Create!!! LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("CLgHWVideoInput::Create was not called") ); return FALSE; } // prepare pointer to video input pin of video input CLgHWVideoPin *pPinOutput = GetVideoPinOutput(); if( !pPinOutput ) { // should be called CLgHWVideoCamera::Create!!! LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("CLgHWVideoCamera::Create was not called") ); return FALSE; } // clear graph (prepare for way calculation) if( !GraphClear( pPinInput, pPinOutput ) ) { // no way availible continue; } // calculate way length if( !GraphSetCost( pPinInput, pPinOutput, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("GraphSetCost failed") ); return FALSE; } // lock way if( !GraphWayLock( pPinInput, pPinOutput ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("GraphWayLock failed") ); return FALSE; } // if there are no switching jobs, then line is ready if( !CameraToJobLinkP::GetCount() ) OnHWVideoCameraActivate( bActivate ); // lock video input CameraToCaptureLinkSP::Link( pCapture ); break; } } else { // if already unlinked if( !CameraToCaptureLinkSP::GetLinkThis() ) { // notify about availibility OnHWVideoCameraActivate( bActivate ); return TRUE; } // unlock GraphWayUnlock(); } return TRUE; } BOOL CLgHWVideoCamera::GraphClear( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput ) { if( !pPinOutput || !pPinInput ) return FALSE; // get hard linked pin CLgHWVideoPin *pDevicePinInput = pPinOutput->PinToOutsideLinkSP::GetLinkThis(); if( !pDevicePinInput ) return FALSE; // set way cost pPinOutput->SetCost( INT_MAX ); // if pin reached if( pDevicePinInput == pPinInput ) return TRUE; // get device CLgHWVideoSwitch *pDevice = pDevicePinInput->PinToSwitchInputLinkSP::GetLinkThis(); if( !pDevice ) return FALSE; // enumerate all outputs of device BOOL bWayFound = FALSE; CLgArray< CLgLink* > &arrDeviceOutput = *pDevice; for( INT i = 0; i < arrDeviceOutput.GetCount(); i++ ) { CLgHWVideoPin *pDevicePinOutput = *arrDeviceOutput[i]; if( !pDevicePinOutput ) return FALSE; if( GraphClear( pPinInput, pDevicePinOutput ) ) bWayFound = TRUE; } return bWayFound; } BOOL CLgHWVideoCamera::GraphSetCost( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput, INT nCost ) { if( !pPinOutput || !pPinInput ) return FALSE; // get hard linked pin CLgHWVideoPin *pDevicePinInput = pPinOutput->PinToOutsideLinkSP::GetLinkThis(); if( !pDevicePinInput ) return FALSE; // set way cost pPinOutput->SetCost( nCost ); // connection established if( pPinInput == pDevicePinInput ) return TRUE; // get device CLgHWVideoSwitch *pDevice = pDevicePinInput->PinToSwitchInputLinkSP::GetLinkThis(); if( !pDevice ) return FALSE; // enumerate all outputs of device BOOL bInputFound = FALSE; CLgArray< CLgLink* > &arrDeviceOutput = *pDevice; for( INT i = 0; i < arrDeviceOutput.GetCount(); i++ ) { CLgHWVideoPin *pDevicePinOutput = *arrDeviceOutput[i]; if( !pDevicePinOutput ) return FALSE; // if pin already locked if( pDevicePinOutput->PinToCameraLinkSP::GetCount() ) continue; // if cost already had been set if( pDevicePinOutput->GetCost() != INT_MAX ) continue; // set cost in subgraph (true if pin is reached) if( GraphSetCost( pPinInput, pDevicePinOutput, nCost + 1 ) ) bInputFound = TRUE; } // input pin was not reached return bInputFound; } BOOL CLgHWVideoCamera::GraphWayLock( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput ) { if( !pPinOutput || !pPinInput ) return FALSE; // lock input pin CameraToPinLinkP::Link( pPinInput ); // get hard linked pin CLgHWVideoPin *pDevicePinOutput = pPinInput->PinToOutsideLinkSP::GetLinkThis(); if( !pDevicePinOutput ) return FALSE; // lock hard linked pin CameraToPinLinkP::Link( pDevicePinOutput ); // connection established if( pPinOutput == pDevicePinOutput ) return TRUE; // get device CLgHWVideoSwitch *pDevice = pDevicePinOutput->PinToSwitchOutputLinkSP::GetLinkThis(); if( !pDevice ) return FALSE; // choose appropriate input on device INT nMinWayCost = INT_MAX; CLgHWVideoPin *pDevicePinInputBest = NULL; CLgArray< CLgLink* > &arrDeviceInput = *pDevice; for( INT i = 0; i < arrDeviceInput.GetCount(); i++ ) { CLgHWVideoPin *pDevicePinInput = *arrDeviceInput[i]; if( !pDevicePinInput ) return FALSE; // if pin already locked if( pDevicePinOutput->PinToCameraLinkSP::GetCount() ) continue; if( pDevicePinInput->GetCost() < nMinWayCost ) { nMinWayCost = pDevicePinInput->GetCost(); pDevicePinInputBest = pDevicePinInput; } } // unexpected situation if( !pDevicePinInputBest ) return FALSE; // create job CLgHWVideoJob *pJob = new CLgHWVideoJob; if( !pJob ) return FALSE; // initialize job (order is very inportent!!!) pJob->JobToOutputLinkSP::Link( pDevicePinOutput ); pJob->JobToInputLinkSP::Link( pDevicePinInputBest ); pJob->JobToCameraLinkSP::Link( this ); pJob->JobToSwitchLinkSP::Link( pDevice ); // lock pin CameraToPinLinkP::Link( pDevicePinInputBest ); // find remaining way return GraphWayLock( pDevicePinInputBest, pPinOutput ); } BOOL CLgHWVideoCamera::GraphWayUnlock() { // unlock all switches CameraToPinLinkP::Unlink(); // clear all jobs while( CameraToJobLinkP::GetCount() ) delete *CameraToJobLinkP::GetAt(0); // unlink video input CameraToCaptureLinkSP::Unlink(); // notify about availibility OnHWVideoCameraActivate( FALSE ); return TRUE; } BOOL CLgHWVideoCamera::HWVideoCameraSampleGet( INT nSampleID ) { CLgHWVideoCapture *pCapture = *this; if( !pCapture ) return FALSE; // pass sample to connected camera return pCapture->HWVideoInputSampleGet( nSampleID ); }