From c5208f435c932fba3877f4ca1d424be9bcc2f71a Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Wed, 24 Apr 2024 10:11:23 -0700 Subject: [PATCH] Added Picture support for SQLite --- Projects/la_mojo/build | 4 +- lib/App/common/SQLite_dataBinder.cpp | 32 + lib/App/common/SQLite_dataBinder.h | 2 +- lib/App/common/dataBinder_structures.h | 2 +- lib/App/common/deviceNodeCamera.cpp | 3101 +++++++++++---------- lib/App/common/deviceNodeCamera.h | 1 + lib/App/common/kernelModuleEmbedVideo.cpp | 40 +- lib/App/common/kernelModuleEmbedVideo.h | 2 +- lib/data/protocol/protocol.cpp | 18 +- lib/data/protocol/protocolPTZ.h | 1 + lib/data/protocol/protocolPTZ_ONVIF.cpp | 51 +- lib/data/protocol/protocolPTZ_ONVIF.h | 1 + lib/rgoose/rgooseCamera.h | 1 + 13 files changed, 1678 insertions(+), 1578 deletions(-) diff --git a/Projects/la_mojo/build b/Projects/la_mojo/build index 98e25a11..59699e61 100755 --- a/Projects/la_mojo/build +++ b/Projects/la_mojo/build @@ -4,8 +4,8 @@ #make clean -e "CFG=Debug" #make -e "CFG=Debug" -j6 -#make clean -e "CFG=Debug86" -make -e "CFG=Debug86" -j2 +make clean -e "CFG=Debug86" +make -e "CFG=Debug86" -j4 #make clean -e "CFG=Release86" #make -e "CFG=Release86" -j4 diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index f8e3b11b..494988b6 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -1299,6 +1299,38 @@ bool sqliteDBC::addLRFWidthTable( uint32_t sensorID, PWIDTH_TABLE ptr ){ return false; }; +bool sqliteDBC::addPicture( DBC_SNAPSHOT_DATA *ptr, CSTR ext ){ + bool rVal = false; + if( ptr != NULL ){ + if( m_bRepIsConnected ){ + char tQBuf[MAX_PATH_LEN]; + uint32_t pic_id; + sprintf( tQBuf, "%s/%s/%s/%s/%s.%s", + m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY, ptr->md5, ext ); + FILE *fd = fopen( tQBuf, "wb+" ); + if( fd ){ + fwrite( ptr->data, 1, ptr->data_size, fd ); + fclose(fd); + chown( tQBuf, web_access_user, web_access_group ); + } else { + LOG_ERROR("Error Saving Captured picture!"); + return rVal; + }; + sprintf( tQBuf, "INSERT INTO pictures(`media_stream_id`,`uts`,`uts_ms`,`md5`,`ext`) VALUES(%d,%d,0,'%s','%s')", + ptr->stream_id, ptr->ts, ptr->md5, ext ); + pic_id = (uint32_t) m_pDataReports->QueryID( tQBuf ); + if ( pic_id ) { + for( DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++ ) { + sprintf( tQBuf, "INSERT INTO `events_2_pictures`(`evt_id`,`picture_id`) VALUES(%d,%d)", (*idx), pic_id ); + m_pDataReports->SilentQuery( tQBuf ); + }; + rVal = true; + }; + }; + }; + return rVal; +}; + uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){ uint32_t RetVal = 0; if( m_bStoIsConnected ){ diff --git a/lib/App/common/SQLite_dataBinder.h b/lib/App/common/SQLite_dataBinder.h index 9c915c3d..8c9cdfe0 100644 --- a/lib/App/common/SQLite_dataBinder.h +++ b/lib/App/common/SQLite_dataBinder.h @@ -79,7 +79,7 @@ class sqliteDBC: public dataBinder { virtual bool addLRFMaskPolygonePoint( uint32_t polyID, int16_t x, int16_t y ); virtual bool addLRFWidthTable(uint32_t sensorID, PWIDTH_TABLE ptr ); - + virtual bool addPicture( DBC_SNAPSHOT_DATA *ptr, CSTR ext ); virtual uint32_t addResource( uint8_t type, CSTR devName, uint32_t auxID = 0 ); virtual bool addVideoEvent( uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms ); virtual uint32_t addVideoRecord( uint32_t streamID, CSTR md5, CSTR ext, uint32_t ts, uint32_t tsms, uint32_t fps ); diff --git a/lib/App/common/dataBinder_structures.h b/lib/App/common/dataBinder_structures.h index 70bda847..9ee7bf68 100644 --- a/lib/App/common/dataBinder_structures.h +++ b/lib/App/common/dataBinder_structures.h @@ -293,7 +293,7 @@ typedef struct S_DBC_MODULE_DATA{ }DBC_MODULE_DATA; typedef struct S_DBC_MODULE_EMBED_VIDEO_DATA{ - uint8_t output_mode; + uint8_t output_mode;// Video-Picture uint16_t port; bool show_ts; bool show_label; diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index 078f08f5..eb0cb711 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -1,1529 +1,1572 @@ -#include "deviceNodeCamera.h" -#include "../../components/base/basicCameraInfo.h" -#include "../../components/object/objectSystemAnchor.h" - -#include "../../hardware/uart/serialPort.h" -#include "../../hardware/uart/serialSOL.h" - -#include "../../data/protocol/protocolPTZ_VISCA.h" -#include "../../data/protocol/protocolPTZ_ONVIF.h" -#include "../../data/protocol/protocolPTZ_PELCO.h" - -#include "hardwareManager.h" -#include "hardwareResourceSerialProvider.h" -//#include "rgooseModuleEmbedVideo.h" - -#define LOG( X ) deviceNode::m_pLog->Log( "RCAM", this, X ) - -#ifdef DEBUG - #define LOG_ERROR( X ) {\ - char _tBfrErrMSG2w34[256];\ - sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ - deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ - } -#else - #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, X ); -#endif - -// all sizes in mm - -static const float AC_imager_diag[12] = { 5.0f, 5.68f, 6.0f, 6.592f, 8.0f, 8.933f, 11.0f, 16.0f, 22.5f, 30.1f, 43.3f, 69.7f }; -static const float AC_imager_width[12] = { 4.0f, 4.536f, 4.8f, 5.27f, 6.4f, 7.176f, 8.8f, 12.8f, 18.0f, 25.1f, 36.0f, 56.0f }; -static const float AC_imager_height[12] = { 3.0f, 3.416f, 3.6f, 3.96f, 4.8f, 5.319f, 6.6f, 9.6f, 13.5f, 16.7f, 24.0f, 41.5f }; - -#define CAM_FOCAL_LENGTH_CNT 49 -static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={ - 2.7f, - 3.0f, - 3.2f, - 3.5f, - 3.7f, - 3.9f, - 4.0f, - 4.3f, - 4.8f, - 5.0f, - 5.1f, - 5.5f, - 5.8f, - 6.0f, - 6.2f, - 6.6f, - 7.0f, - 7.1f, - 7.5f, - 7.6f, - 8.0f, - 8.6f, - 8.9f, - 9.0f, - 9.2f, - 9.3f, - 10.0f, - 10.5f, - 10.6f, - 10.7f, - 11.0f, - 11.3f, - 12.0f, - 12.9f, - 13.0f, - 13.5f, - 13.6f, - 14.0f, - 14.6f, - 15.0f, - 15.1f, - 15.5f, - 16.0f, - 17.0f, - 17.5f, - 17.7f, - 18.0f, - 18.2f, - 19.0f -}; - - -deviceNodeCamera::deviceNodeCamera( uint32_t NodeID ): deviceNode( NodeID ), basicThread(){ - //m_pLog = basicLogCollection::getInstance(); - m_pBEH = basicEventHeap::getInstance(); - - m_tTimeTSeconds = 0; - m_pProtocol = NULL; - m_pTransport = NULL; - m_pCurrentROI = NULL; - m_nRealFrameWidth = 640; - //m_bState = 0; - //m_bBusy = true; - m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f; - m_nRelativeMoveCounter = 0; - m_bRelativeMoveMatrix = 0; - m_fSpeedRise = 0.03f; - m_fZoomCorrection = 1.0f; - m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f; - //m_bSourceConnected = false; - //m_pSource = NULL; -}; - -deviceNodeCamera::deviceNodeCamera( DBC_NODE_DATA *Data ): deviceNode( Data ), basicThread(){ - //m_pLog = basicLogCollection::getInstance(); - m_pBEH = basicEventHeap::getInstance(); - - m_tTimeTSeconds = 0; - m_pProtocol = NULL; - m_pTransport = NULL; - m_pCurrentROI = NULL; - m_nRealFrameWidth = 640; - //m_bState = 0; - //m_bBusy = true; - m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f; - m_nRelativeMoveCounter = 0; - m_bRelativeMoveMatrix = 0; - m_fSpeedRise = 0.03f; - m_fZoomCorrection = 1.0f; - m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f; - //m_bSourceConnected = false; - //m_pSource = NULL; -}; - -deviceNodeCamera::~deviceNodeCamera(){ - LOG("Destruct"); - //m_mQPROTECTOR.unlock(); - waitThreadEnd( true ); - LOG("Thread Del OK"); - if( m_pTransport!= NULL ) { - if( m_sCamData.uart_id ) { - DBC_UART_DATA *tData = NULL; - tData = m_pDBC->getUartData( m_sCamData.uart_id ); - switch( tData->type ) { - case UART_TYPE_HARDWARE:{ - delete((serialPort*)m_pTransport); - break; - }; - case UART_TYPE_SOL:{ - delete((serialSOL*)m_pTransport); - break; - }; - }; - } - }; - LOG("Transport Del OK"); - if( m_pProtocol != NULL ) { - switch( m_sCamData.protocol ) { - case CAM_CTRL_PROTO_VISCA: - delete( (protocolPTZ_VISCA *) m_pProtocol ); - break; - case CAM_CTRL_PROTO_PELCO: - delete( (protocolPTZ_PELCO *) m_pProtocol ); - break; - case CAM_CTRL_PROTO_ONVIF: - delete( (protocolPTZ_ONVIF *) m_pProtocol ); - break; - default: - delete( m_pProtocol ); - }; - }; - LOG("Proto Del OK"); - //if( m_pPosition != NULL ) delete( m_pPosition ); - //LOG("Pos Del OK"); - //if( m_bSourceConnected ){ - // if( m_pSource != NULL ){ - // m_pSource->_close(); - // delete( m_pSource ); - // }; - //}; - //LOG("Stream Del OK"); -}; - -void deviceNodeCamera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){ -#ifdef DEBUG - char tB[128]; - LOG("Set Optical Parameters"); - sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth ); - LOG(tB); -#endif - if( frame_width > 0 && frame_heigth > 0 ) { - m_nRealFrameWidth = frame_width; - m_nRealFrameHeight = frame_heigth; - float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth ); - switch( m_sCamData.zoom_model ) { - case 0:{ -#ifdef DEBUG - LOG("\tFixed Zoom"); -#endif - m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f]; - m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag ); - m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag ); - m_fR2FL = .0f; - m_fZoomCorrection = .0f; - m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange ); - break; - }; - case 1:{ -#ifdef DEBUG - LOG("\tAutomated Zoom"); -#endif - m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f]; - m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag ); - m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom; - m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag ); - if( m_fMaxRange > m_fMinRange ){ - m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange)); - } else { - m_fR2FL = .0f; - }; - m_fZoomCorrection = 1.0f; - m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange); - break; - }; - case 2:{ -#ifdef DEBUG - LOG("\tManual Zoom"); -#endif - m_fMinRange = 5.0f; //Minimal 5 meters - if ( m_sCamData.max_range < m_fMinRange ) { - m_fMaxRange = m_fMinRange; - } else { - m_fMaxRange = m_sCamData.max_range; - }; - m_fZoomCorrection = m_sCamData.zoom * 0.01f; - m_fMinFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag ); - m_fMaxFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag ); - break; - }; - }; - m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL )); - m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL )); - m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL )); - m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL )); -#ifdef DEBUG - sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL ); - LOG(tB); - sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange ); - LOG(tB); - sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov ); - LOG(tB); - sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov ); - LOG(tB); -#endif - }; -}; - -bool deviceNodeCamera::create(){ - - setWM( CAM_WS_INIT ); - - if( m_sNodeData.aux_id == 0 ) return false; - - DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id ); - if( tData != NULL ) { - memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) ); - free( tData ); - } else { - LOG_ERROR("Cannot Fetch CAM Data from Database!"); - return false; - }; - if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1; - - updateOpticalParameters( 1280, 720 ); //HD Ready - - if( m_sCamData.ptz_speed_step > .0f ) { - m_fSpeedRise = m_sCamData.ptz_speed_step; - }; - - //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw ); - - //if( m_pPosition == NULL ) { - // LOG_ERROR("Position Constructor Failure!"); - // return false; - //}; - - //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z ); - //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot ); - - - - systemAnchor * SA = systemAnchor::getInstance(); - if( SA ){ - LOG("SysAnchor OK"); - if ( SA->beLocal() ) { - LOG("SysAnchor beLocal!"); - m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 ); //SA->getUTMZone() ); - m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); - } else { - LOG("SysAnchor is Global Position System!"); - C3D_DECART LD = SA->getXYZ(); - m_oPosition.setPositionXYZ( LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone() ); - m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); - }; - } else { - LOG("No SysAnchor!"); - m_oPosition.setPositionXYZ( m_sCamData.x , m_sCamData.y, m_sCamData.z ); - m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); - }; - m_oPosition.updateMatrix(); - -#ifdef DEBUG - char tB[128]; - sprintf( tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() ); - LOG( tB ); - sprintf( tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() ); - LOG( tB ); - sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() ); - LOG( tB ); -#endif - - if( m_pProtocol != NULL ){ - LOG_ERROR("Cannot Start: Service Already Running!"); - return false; - }; - switch( m_sCamData.protocol ){ - case CAM_CTRL_PROTO_VISCA: { -#ifdef DEBUG - LOG("VISCA"); -#endif - m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux ); - if( m_pProtocol!= NULL ) { - basicTransport *tTransport = NULL; - hardwareManager *tHM = hardwareManager::getInstance(); - if( tHM ){ - hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id ); - if( tRES != NULL ) { - if( tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if( m_pProtocol->setTransport( tTransport )){ - if( !m_pProtocol->create() ){ - LOG_ERROR("Protocol <<< Create >>> Failure!"); - }; - } else { - LOG_ERROR("Binding Port Failure!"); - }; - }; -#ifdef DEBUG - } else { - LOG("HWM Has No UART PORT!"); -#endif - }; -#ifdef DEBUG - } else { - LOG_ERROR("Can not get Hardware Manager!"); -#endif - }; - if( tTransport!= NULL ) { - LOG( "Bind Transport Port!" ); - } else { - LOG_ERROR("Transport Creation Failure!"); - delete( ( protocolPTZ_VISCA * )m_pProtocol ); - m_pProtocol = NULL; - }; -#ifdef DEBUG - } else { - LOG_ERROR("Protocol Creation failure!"); -#endif - }; - break; - }; - case CAM_CTRL_PROTO_PELCO:{ -#ifdef DEBUG - LOG("PELCO"); -#endif - m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux ); - - if( m_pProtocol!= NULL ) { - basicTransport *tTransport = NULL; - hardwareManager *tHM = hardwareManager::getInstance(); - if ( tHM ) { - hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id ); - if( tRES != NULL ) { - if( tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if( m_pProtocol->setTransport( tTransport )){ - if( !m_pProtocol->create() ){ - LOG_ERROR("Protocol <<< Create >>> Failure!"); - }; - } else { - LOG_ERROR("Binding Port Failure!"); - }; - }; -#ifdef DEBUG - } else { - LOG("HWM Has No UART PORT!"); -#endif - }; -#ifdef DEBUG - } else { - LOG_ERROR("Can not get Hardware Manager!"); -#endif - }; - if( tTransport!= NULL ) { - LOG( "Bind Transport Port!" ); - } else { - LOG_ERROR("Transport Creation Failure!"); - delete( ( protocolPTZ_PELCO * )m_pProtocol ); - m_pProtocol = NULL; - }; -#ifdef DEBUG - } else { - LOG_ERROR("Protocol Creation failure!"); -#endif - }; - break; - }; - case CAM_CTRL_PROTO_ONVIF:{ -#ifdef DEBUG - LOG("ONVIF"); -#endif - //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode ); - if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) { - char tURI[256]; - if ( m_sCamData.port ) { - sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl); - } else { - sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl); - }; - m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode); - } else { - m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode); - } - - if( m_pProtocol!=NULL ) { - if( !m_pProtocol->create() ) { - LOG_ERROR("Error Creating Proto!"); - delete( (protocolPTZ_ONVIF*) m_pProtocol ); - m_pProtocol = NULL; - }; - }; - break; - }; - default:{ -#ifdef DEBUG - LOG("Camera Has no defined control protocol"); -#endif - }; - }; - if( m_pProtocol == NULL ) { - if( m_sCamData.protocol == 0 ) { - m_sCamData.online = true; - } else LOG_ERROR("No Proto Created!"); - } else { -#ifdef DEBUG - LOG("Proto is OK"); -#endif - m_sCamData.online = init(); - }; - m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online ); - return m_sCamData.online; -}; - - - -//void deviceNodeCamera::setBusy( bool Status ){ - //m_bBusy = Status; -//}; - - -bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){ - if( m_sCamData.online ) { - uint32_t MoveMatrix = 0; - if( PanSPD != .0f ){ - if( PanSPD > 0 ) { - MoveMatrix |= PTZ_DRIVE_PAN_RIGHT; - } else { - MoveMatrix |= PTZ_DRIVE_PAN_LEFT; - }; - }; - if( TiltSPD != .0f ){ - if( TiltSPD > 0 ) { - MoveMatrix |= PTZ_DRIVE_TILT_UP; - } else { - MoveMatrix |= PTZ_DRIVE_TILT_DOWN; - }; - }; - if( ZoomSPD != .0f ){ - if( ZoomSPD > 0 ) { - MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE; - } else { - MoveMatrix |= PTZ_DRIVE_ZOOM_TELE; - }; - }; - if( MoveMatrix != 0 ) { - m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) ); - }; - return m_pProtocol->move( MoveMatrix ); - }; - return false; -}; - -bool deviceNodeCamera::relativeStop(){ - if( m_sCamData.online ) { - return m_pProtocol->move( 0 ); - }; - return false; -}; - -CAM_TASK *deviceNodeCamera::fetchCamTask( void ){ - CAM_TASK *rVal = NULL; - m_mQPROTECTOR.lock(); - if (m_qCAM_TASK.size()) { - rVal = m_qCAM_TASK.front(); - m_qCAM_TASK.pop(); - }; - m_mQPROTECTOR.unlock(); - return rVal; -}; - -bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){ - if ( ptr == NULL ) return false; - m_mQPROTECTOR.lock(); - m_qCAM_TASK.push( ptr ); - m_mQPROTECTOR.unlock(); - return true; -}; - -bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){ - if( ptr == NULL ) return false; - float tFI = fabsf( ptr->cam_position.fi - tp.fi ); - float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta ); -#ifdef DEBUG - char tB[128]; - sprintf(tB,"FC: FOV:[%.5f:%.5f] dTG:[%.5f:%.5f] %.2f<%.2f>%.2f",ptr->h_fov ,ptr->v_fov, tFI, tTETTA, ptr->min_range, tp.r, ptr->max_range ); - LOG(tB); -#endif - if( tFI > ptr->h_fov ) return false; - if( tTETTA > ptr->v_fov ) return false; - if( tp.r < ptr->min_range ) return false; - if( tp.r > ptr->max_range ) return false; - return true; -}; - -bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){ - if( db_id == 0 ) return false; - CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD)); - if( tTrackRecord == NULL ) return false; -#ifdef DEBUG_EXTRA - LOG("Fusion: Consuming new Data!"); -#endif - memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD)); - tTrackRecord->target_direction.fi = tOR.fi; - tTrackRecord->target_direction.r = tOR.r; - tTrackRecord->target_direction.tetta = tOR.tetta; - tTrackRecord->evt_db_ID = db_id; - tTrackRecord->ts_ms = getMilliseconds(); - m_mROIsProtector.lock(); - if( m_vROIS.size() ){ - vCAM_ROIS::iterator idx = m_vROIS.begin(); - for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break; - if ( idx != m_vROIS.end() ) { - // have Match! -#ifdef DEBUG_EXTRA - LOG("ROI MATCHED!"); -#endif - (*idx)->targets.push_back( tTrackRecord ); - fusion_validateROI( tTrackRecord->ts_ms, (*idx) ); - } else { -#ifdef DEBUG_EXTRA - LOG("NO MATCH FOUND!"); -#endif - // No mathced ROI -> create new one - CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI)); - if ( tROI ) { - memset( tROI, 0 , sizeof(CAM_ROI) ); - tROI->ts_ms = tTrackRecord->ts_ms; - spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); - - tROI->cam_position.fi = tOR.fi; - tROI->cam_position.tetta = tOR.tetta; - tROI->cam_position.r = tOR.r; - - float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange ); - - tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function - tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen - - tROI->targets.push_back( tTrackRecord ); - m_vROIS.push_back( tROI ); - }; - }; - } else { -#ifdef DEBUG_EXTRA - LOG("Fusion: NO ROI -> CREATE!"); -#endif - // - CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI)); - if ( tROI ) { - memset( tROI, 0 , sizeof(CAM_ROI) ); - tROI->ts_ms = tTrackRecord->ts_ms; - //Set vector Cam Coordinates - tROI->cam_position.fi = tOR.fi; - tROI->cam_position.tetta = tOR.tetta; - tROI->cam_position.r = tOR.r; - // Set vector OnVIF coordinates - spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); - - // make fusion data BOX - float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange ); - - tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation - tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen - - // Add ROI to vector - tROI->targets.push_back( tTrackRecord ); - m_vROIS.push_back( tROI ); - } else LOG_ERROR("No MEM4ROI"); - }; - m_mROIsProtector.unlock(); - return true; -}; - -bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){ - if( ptr == NULL ) return false; - if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false; - -#ifdef DEBUG_EXTRA - char tB[128]; - sprintf( tB, "\tROI [%d]", ptr->targets.size() ); - LOG( tB ); -#endif - - if( ptr->targets.size() ) { - double tA = 0.0; - double tB = 0.0; - double tC = 0.0; - uint32_t tCNT = 0; - for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) { - if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) { - free( (*idx) ); - ptr->targets.erase( idx ); - } else { - tA = tA + (*idx)->target_direction.fi; - tB = tB + (*idx)->target_direction.tetta; - tC = tC + (*idx)->target_direction.r; - tCNT++; - }; - }; - if( tCNT ) { - C3D_SPH_DEGREES L = { 0 }; - L.fi = (float) ( tA /(double) tCNT ); - L.tetta = (float) ( tB /(double) tCNT ); - L.r = (float) ( tC /(double) tCNT ); - spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles); - ptr->ts_ms = cts; - - float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange ); - - ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); - ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation - ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen - return true; - }; - }; - return false; -}; - -CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){ - CAM_ROI *tVAL = NULL; - uint64_t cts = 0; - m_mROIsProtector.lock(); - vCAM_ROIS::iterator sRec; - if( m_vROIS.size() ){ - for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){ - if ( tVAL == NULL ) { - sRec = idx; - tVAL = (*idx); - cts = tVAL->ts_ms; - } else { -#ifdef CAM_FETCH_ROI_NEWEST - if ( (*idx)->ts_ms > cts ) -#else - if ( (*idx)->ts_ms < cts ) -#endif - { - sRec = idx; - tVAL = (*idx); - cts = tVAL->ts_ms; - }; - }; - }; - m_vROIS.erase( sRec ); - }; - m_mROIsProtector.unlock(); - return tVAL; -}; - -DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){ - DBC_SNAPSHOT_DATA *rVal = NULL; - if( ptr == NULL ) return NULL; - if( ptr->targets.size() ){ - //if( m_bSourceConnected ){ - rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA)); - if( rVal ){ - memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) ); - //rVal->stream_id = m_nActiveStreamID; - rVal->stream_id = m_sNodeData.aux_parameter; - for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) { - LOG("Added Event ID"); - rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID ); - }; - }; - //}; - ptr->targets.clear(); - } else { - LOG("No Events ID to ROI"); - }; - // Destruct ROI MEM - delete( (CAM_ROI *) ptr ); - ptr = NULL; - // Not Setting MD5 and TS -> will set at capture time - return rVal; -}; - -void deviceNodeCamera::fusion_validate( void ){ - m_mROIsProtector.lock(); - if( m_vROIS.size() ){ -#ifdef DEBUG_EXTRA - char tB[128]; - sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() ); - LOG( tB ); -#endif - uint64_t cts = getMilliseconds(); - for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){ - if( !fusion_validateROI( cts, *idx ) ) { - free( (*idx) ); - m_vROIS.erase( idx ); - }; - }; - }; - m_mROIsProtector.unlock(); -}; - -bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){ - if( m_sCamData.online ){ - CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); - if( tTask ){ - memset(tTask, 0, sizeof(CAM_TASK)); - tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD)); - if( tTask->CMD ){ - tTask->CMD->CMD = PTZ_ABSOLUTE; - tTask->CMD->PAN = PanPOS; - tTask->CMD->TILT = TiltPOS; - tTask->CMD->ZOOM = ZoomPOS; - tTask->movemet_needed = true; - tTask->source_evt_id = TargetRefID; - setWM( CAM_WS_BUSY ); - return pushCamTask( tTask ); - } else { - LOG_ERROR("CMD malloc Error!"); - free( tTask ); - }; - } else LOG_ERROR("Task malloc error!"); - }; - return false; -}; - -bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){ - if( m_sCamData.online ){ - - CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); - if( tTask ){ - memset(tTask, 0, sizeof(CAM_TASK)); - tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD)); - if( tTask->CMD ){ - tTask->CMD->CMD = PTZ_ABSOLUTE; - tTask->CMD->PAN = O.pan; - tTask->CMD->TILT = O.tilt; - tTask->CMD->ZOOM = O.zoom; - tTask->movemet_needed = true; - tTask->source_evt_id = TargetRefID; - setWM( CAM_WS_BUSY ); - return pushCamTask( tTask ); - } else { - LOG_ERROR("CMD malloc Error!"); - free( tTask ); - }; - } else LOG_ERROR("Task malloc error!"); - }; - return false; -}; - -void deviceNodeCamera::m_fThreadProc( void ){ - DBC_PUSHED_CMD * PUSHED = NULL; - m_bThreadStatus = TH_WORKING; - bool bNewSecondLoop = false; - uint32_t cTimeSeconds = 0; - uint32_t oTimeSeconds = 0; - while( m_bThreadStatus < TH_ENDED ) { - - cTimeSeconds = (uint32_t) time( NULL ); - - if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds; - - uint8_t tState = getWM(); - - char tB[128]; - - fusion_validate(); - - switch( tState ) { - case CAM_WS_INIT:{ -#ifdef DEBUG - LOG("Init Camera - PowerMode"); -#endif - //setBusy( true ); - if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE ); - //setBusy( false ); - break; - }; - case CAM_WS_WHITE_BALLANCE:{ -#ifdef DEBUG - LOG("Set WB"); -#endif - //setBusy( true ); - if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT ); - //setBusy( false ); - break; - }; - case CAM_WS_BACKLIGHT:{ -#ifdef DEBUG - LOG("Set BLC"); -#endif - //setBusy( true ); - if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS ); - //setBusy( false ); - break; - }; - case CAM_WS_AUTO_FOCUS:{ -#ifdef DEBUG - LOG("Set AF"); -#endif - //setBusy( true ); - if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME ); - //setBusy( false ); - break; - }; - case CAM_WS_INIT_GO_HOME:{ -#ifdef DEBUG - LOG("INIT GO -1 0 -1"); -#endif - m_nHomingCounter = 0; - if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) { - m_fCP_Pan = -1; - m_fCP_Tilt = 0; - m_fCP_Zoom = -1; - setWM( CAM_WS_READY ); - }; - break; - }; - case CAM_WS_AUTOMATED_MOVE:{ - m_nRelativeMoveCounter++; - if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) { - //Pan Move - if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) { - m_fRM_Pan -= m_fSpeedRise; - } else { - m_fRM_Pan += m_fSpeedRise; - }; - }; - if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) { - //TILT Move - if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) { - m_fRM_Tilt -= m_fSpeedRise; - } else { - m_fRM_Tilt += m_fSpeedRise; - } - }; - if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) { - //ZOOM Move - if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) { - //Wide - m_fRM_Zoom -= m_fSpeedRise; - } else { - //Tele - m_fRM_Zoom += m_fSpeedRise; - }; - }; - if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f; - if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f; - if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f; - if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f; - if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f; - if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f; - - m_fCP_Zoom += 0.5f*m_fRM_Zoom; - - if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f; - if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f; - - if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){ - m_nRelativeMoveCounter = 0; - setWM( CAM_WS_READY ); - break; - }; - - if( m_sCamData.ptz_axel ) { //Manual - if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) { - m_nRelativeMoveCounter = 0; - setWM( CAM_WS_READY ); - }; - } else { - // Stepping Axel by Zoom - uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) )); - if( m_nRelativeMoveCounter > tVal ) { - m_nRelativeMoveCounter = 0; - setWM( CAM_WS_READY ); - }; - }; - break; - }; - case CAM_WS_STOP_MOVE:{ - m_nRelativeMoveCounter++; - m_fRM_Pan = .0f; - m_fRM_Pan = .0f; - m_fRM_Tilt = .0f; - m_fRM_Tilt = .0f; - m_fRM_Zoom = .0f; - m_fRM_Zoom = .0f; - if( relativeStop() ) { - m_nHomingCounter = 0; - m_nRelativeMoveCounter = 0; - setWM( CAM_WS_READY ); - break; - }; - if( m_sCamData.ptz_axel ) { - if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY); - } else { - uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) )); - if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY); - }; - break; - }; - case CAM_WS_GO_ABS_ROI: - case CAM_WS_GO_ABS_POSITION:{ - setWM( CAM_WS_BUSY ); - if( m_pCurrentROI != NULL ){ - m_nHomingCounter = 0; - if( m_pBEH ){ - EVENT_CONTAINER *tEVT = createBasicEvent( - m_sNodeData.node_id, - EVT_CLASS_CAMERA_CTRL, - EVT_CAMERA_MOVE_TO, - NULL, NULL, NULL ); - if( tEVT ){ - //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA - m_pBEH->broadcast( tEVT ); - }; - }; - //update position Info - m_fCP_Pan = m_pCurrentROI->cam_orientation.pan; - m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt; - m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom; - - // Do Movement - if( !m_pProtocol->goAbsolute( - m_pCurrentROI->cam_orientation.pan, - m_pCurrentROI->cam_orientation.tilt, - ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection ) - )){ - LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?"); - setWM( CAM_WS_END );//CAM_WS_READY ); - } else { - // Send Capture event - DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI ); - if( tData != NULL ) { - if( m_pBEH ){ - EVENT_CONTAINER *tEVT = createBasicEvent( - m_sNodeData.node_id, - EVT_CLASS_CAMERA_CTRL, - EVT_CAMERA_ON_POSITION, - NULL, NULL, tData ); - if( tEVT ){ -#ifdef DEBUG_EXTRA - LOG("BCAST CAM ONPOS"); -#endif - //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA - m_pBEH->broadcast( tEVT ); - }; - }; - }; - // Waiting capture finish - //setWM( CAM_WS_BUSY ); - }; - utilizeROI( m_pCurrentROI ); - } else setWM( CAM_WS_READY ); - break; - }; - case CAM_WS_GO_HOMING_PRESET:{ - if( m_sCamData.home_token[0] != (char)0 ) { - //LOG( m_sCamData.home_token ); - if( !m_pProtocol->goPreset( m_sCamData.home_token )){ - LOG_ERROR("Cannot Issue goPreset Command!"); - }; - }; - setWM( CAM_WS_READY ); - break; - }; - case CAM_WS_BUSY:{ - //Handle Operation TO - setWM( CAM_WS_READY ); - break; - }; - case CAM_WS_READY:{ - m_pCurrentROI = fusion_fetchROI(); - if( m_pCurrentROI != NULL ) { -#ifdef DEBUG_EXTRA__ - LOG("HAVE ROI to see!"); -#endif - setWM( CAM_WS_GO_ABS_POSITION ); - } else { - //Idle Work ---> - if( m_sCamData.homing_enable ){ - if ( bNewSecondLoop ) { - if ( m_nHomingCounter < m_sCamData.homing_to ) { - m_nHomingCounter++; - } else { - //LOG("Go Home Position!"); - m_nHomingCounter = 0; - setWM( CAM_WS_GO_HOMING_PRESET ); - break; - }; - }; - }; - CAM_TASK * tTASK = fetchCamTask(); - if ( tTASK != NULL ) { - if( tTASK->movemet_needed ){ - CAM_PTZ_CMD *tCMD = tTASK->CMD; - if( tCMD ) { - switch( tCMD->CMD ) { - case PTZ_STOP:{ - m_nHomingCounter = 0; - setWM( CAM_WS_STOP_MOVE ); - break; - }; - case PTZ_MOVE:{ - if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0; - m_bRelativeMoveMatrix = 0; - m_fRM_Pan = tCMD->PAN; - m_fRM_Tilt = tCMD->TILT; - m_fRM_Zoom = tCMD->ZOOM; - m_nHomingCounter = 0; - setWM( CAM_WS_AUTOMATED_MOVE ); - break; - }; - case PTZ_ABSOLUTE:{ - m_pCurrentROI = createCAM_ROI(); - if( m_pCurrentROI != NULL ){ - CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD)); - if ( tTarget != NULL ) { - memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) ); - tTarget->evt_db_ID = tTASK->source_evt_id; - m_pCurrentROI->targets.push_back( tTarget ); - m_pCurrentROI->cam_orientation.pan = tCMD->PAN; - m_pCurrentROI->cam_orientation.tilt = tCMD->TILT; - m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM; - m_nHomingCounter = 0; - setWM(CAM_WS_GO_ABS_POSITION); - } else { - utilizeROI( m_pCurrentROI ); - }; - }; - break; - }; - case PTZ_RELOAD_CAMERA:{ - LOG("Exit Command Rcpt"); - m_bThreadStatus = TH_ENDED; - }; - }; - free( tCMD ); - tTASK->CMD = NULL; - }; - } else { - // No Movement -> Just Capture - if( m_pBEH ){ - EVENT_CONTAINER *tEVT = createBasicEvent( - m_sNodeData.node_id, - EVT_CLASS_CAMERA_CTRL, - EVT_CAMERA_ON_POSITION, - NULL, NULL, NULL ); - if( tEVT ){ - tEVT->m_db_id = tTASK->source_evt_id; - m_pBEH->broadcast(tEVT); - }; - }; - }; - free( tTASK ); - } else { - // Lowest Priority to Web Client Commands - #ifdef DEBUG_EXTRA__ - LOG("Check Web CMD!"); - #endif - PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id ); - if( PUSHED != NULL ) { - #ifdef DEBUG_EXTRA__ - LOG("HAVE Web CMD!"); - #endif - m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN ); - if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) { - switch( PUSHED->CMD ) { - case 0x80:{ - //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET; - break; - }; - case 0x81:{ - //m_bWM = ANODE_WM_2D_GET_FAST_DRAW; - break; - }; - case 0x82:{ - break; - }; - }; //?? Check if we don't miss something!! - } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here - //Direct Movment Mode - if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) { - m_nHomingCounter = 0; - setWM( CAM_WS_STOP_MOVE ); - } else { - char tB[64]; - if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0; - m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1; - m_nHomingCounter = 0; - setWM( CAM_WS_AUTOMATED_MOVE ); - }; - }; - free( PUSHED ); - PUSHED = NULL; - }; - }; - }; - break; - }; - case CAM_WS_END:{ - LOG("Exit Command Rcpt"); - m_bThreadStatus = TH_ENDED; - break; - }; - }; - SLEEP( DEFAULT_THREAD_SLEEP_TIME ); - }; -#ifdef DEBUG - LOG("CTRL Loop End!"); -#endif -}; - -bool deviceNodeCamera::testFOV( C3D_DECART L ){ - //System Coords -> trans to local and go to funct - systemAnchor* tSystem = systemAnchor::getInstance(); - if ( tSystem == NULL ) return false; - C3D_SPH_DEGREES SL; -#ifdef DEBUG - LOG("Test FOV local Decart coords"); -#endif - if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false; - return testFOV( SL ); -}; - -bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){ - //Global Coords - systemAnchor* tSystem = systemAnchor::getInstance(); - if ( tSystem == NULL ) return false; - C3D_SPH_DEGREES SL; -#ifdef DEBUG - LOG("Test FOV WGS coords"); -#endif - if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false; - return testFOV( SL ); -}; - -bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){ - //Local Coords - C3D_SPH_DEGREES SL; -#ifdef DEBUG - LOG("Test FOV local Spherical(rad) coords"); -#endif - spherical2spherical( L, SL ); - return testFOV( SL ); -}; - -bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){ -#ifdef DEBUG - char tB[128]; -#endif -#ifdef DEBUG - LOG("Test FOV local Spherical(deg) coords"); -#endif - switch ( m_sCamData.triggering_mode ) { - case CAM_TRIGGER_STRICT:{ - if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){ -#ifdef DEBUG - sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); - LOG( tB ); -#endif - return false; - }; - break; - }; - case CAM_TRIGGER_ALL_NEAR:{ - if( SL.r > m_fMaxRange ){ -#ifdef DEBUG - sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); - LOG( tB ); -#endif - return false; - }; - break; - }; - case CAM_TRIGGER_ALL_FAR:{ - if( SL.r < m_fMinRange ){ -#ifdef DEBUG - sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); - LOG( tB ); -#endif - return false; - }; - break; - }; - case CAM_TRIGGER_ALL: return true; - }; - - float HS = AC_imager_width[m_sCamData.imager_type]; - float VS = AC_imager_height[m_sCamData.imager_type]; - float HS2 = HS * HS; - float VS2 = VS * VS; - float IR = HS / VS; - float IR2 = IR * IR; - - if( m_sCamData.pan_angles == 0 ) { - // Fixed Camera - float tA = fabsf( SL.fi ); - float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL ); - if ( tA > tZ ) return false; - } else { - if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED - // Partial Pan - switch( m_sCamData.pan_model ) { - case 1: { - // Regular Pan - float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL ); - float tRad = deg2rad( SL.fi ); -#ifdef DEBUG - sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad ); - LOG( tB ); -#endif - if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false; - break; - }; - case 2:{ - - break; - }; - case 3:{ - - break; - }; - }; - }; - }; - - if( m_sCamData.tilt_angles == 0 ){ - //Fixed Camera - if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false; - } else { - if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass - // Partial Tilt - switch( m_sCamData.tilt_model ) { - case 1: { - // Regular Tilt - float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ; - float tRad = deg2rad( SL.tetta ); - if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false; - break; - }; - case 2:{ - break; - }; - }; - }; - }; - return true; -}; - -bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){ - systemAnchor* tSystem = systemAnchor::getInstance(); - if( tSystem == NULL ) return false; - C3D_SPH_DEGREES SL; -#ifdef DEBUG_EXTRA - LOG("Look at WGS coords!"); -#endif - tSystem->convertToLocal(&m_oPosition, L, SL ); - return lookAt(TargetRefID, SL ); -}; - -bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){ - systemAnchor* tSystem = systemAnchor::getInstance(); - if( tSystem == NULL ) return false; - C3D_SPH_DEGREES SL; -#ifdef DEBUG_EXTRA - LOG("Look at System Decart coords!"); -#endif - tSystem->convertToLocal( &m_oPosition, L, SL ); - return lookAt(TargetRefID, SL ); -}; - -bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){ - C3D_SPH_DEGREES SL; -#ifdef DEBUG_EXTRA - LOG("Look at spherical(rad) 2 spherical(deg) coords!"); -#endif - spherical2spherical( L, SL ); - return lookAt(TargetRefID, SL ); -}; - -bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){ - //char tB[128]; - //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta ); - //LOG( tB ); - //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ ); - //LOG( tB ); -#ifdef DEBUG_EXTRA - LOG("Look at local spherical deg. coords!(final)"); -#endif - //if( getWM() == CAM_WS_READY ){ - // C3D_ORIENT_ONVIF LL; - // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); - // return absoluteMove( TargetRefID, LL ); - //}; - // Camera is Busy - return fusion_consumeData(TargetRefID, L); -}; - -bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){ - CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); - if( tTask ){ - memset(tTask, 0, sizeof(CAM_TASK)); - tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) ); - if( tTask->CMD ){ - tTask->CMD->CMD = PTZ_CAPTURE_ONLY; - tTask->source_evt_id = TargetRefID; - return pushCamTask( tTask ); - } else { - free( tTask ); - }; - }; - return false; -}; - -bool deviceNodeCamera::connectToStream( uint32_t id ){ - /* - if( id ){ - argusKernelModule * tKM = NULL; - argusKernel * tKRNL = NULL; - argusModuleFactory * tMF = NULL; - if( m_pDBC == NULL ) { - LOG_ERROR("No Database Connector!"); - return false; - }; - tKRNL = argusKernel::getInstance(); - if( tKRNL ) { - tMF = tKRNL->getModuleFactory(); - if( tMF ) { - tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO ); - } else { - LOG_ERROR("No ModuleFactory Instance!"); - return false; - }; - } else { - LOG_ERROR("No Kernel Instance!"); - return false; - }; - if( tKM ) { - // New Module - DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id ); - if( tDATA ) { - m_sMediaSource.mediaStreamId = id; - if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { - if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource); - m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource); - } else { - if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource); - m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource ); - }; - - if( m_pSource != NULL ) { - char tMRL[1024]; -#ifdef DEBUG - LOG("Source OK"); -#endif - ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES ); - - int tW = ((argusModuleEmbedVideo*)tKM)->getWidh(); - int tH = ((argusModuleEmbedVideo*)tKM)->getHeight(); - int pix = -1; - bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 ); - bool out_16_9 = (((tW*100) / tH ) > 140 ); - updateOpticalParameters( tW, tH ); - if( strlen( tDATA->c_type ) ){ - - }; - //if( tW > 0 ){ - // //m_pSource->setProportionalByWidth( tW ); - // m_pSource->setSize( tW, tH ); - //}; - if( in_16_9 != out_16_9 ){ - if( in_16_9 ){ - tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000; - } else { - tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000; - }; - }; - if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) { - sprintf( tMRL, tDATA->mrl ); - } else { - DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id ); - if( tCAM ) { - sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl ); - free( tCAM ); - } else { - LOG_ERROR("Error Getting CAM Data!"); - delete( m_pSource ); - m_pSource = NULL; - return false; - }; - }; - LOG( tMRL ); - if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){ - m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM ); - if ( !m_bSourceConnected ) { - //delete( m_pSource ); - if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { - delete((videoAVLIBSource*)m_pSource); - } else { - delete((videoLIVE555Source*)m_pSource); - }; - m_pSource = NULL; - } else { - m_nActiveStreamID = id; -#ifdef DEBUG - LOG("Source is Connected to Output."); -#endif - }; - } else { - LOG_ERROR("Can not Open Video Source!"); - //delete( m_pSource ); - if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { - delete((videoAVLIBSource*)m_pSource); - } else { - delete((videoLIVE555Source*)m_pSource); - }; - m_pSource = NULL; - }; - } else { - LOG_ERROR("Video Source Stream Failure!"); - }; - free( tDATA ); - }; - } -#ifdef DEBUG - else LOG("No Video Module!") -#endif - ; - }; - LOG("Connect Finished!"); - return m_bSourceConnected; - */ - return true; -}; - -bool deviceNodeCamera::disconnectFromStream( void ){ - /*if( m_bSourceConnected ) { - if( m_pSource ){ - delete( m_pSource ); - m_pSource = NULL; - }; - m_bSourceConnected = false; - }; - return !m_bSourceConnected; - */ - return true; -}; - -bool deviceNodeCamera::validateStream( void ){ - /* - if( m_bSourceConnected ) { - if( m_pSource ) { - if( m_pSource->isAttached() ){ - if( m_pSource->getStatus() < MS_STOPED ) return true; -#ifdef DEBUG - LOG("Source is Finished!"); -#endif - }; -#ifdef DEBUG - LOG("Source is Disconnected!"); -#endif - } else LOG_ERROR("No Source Object! Set Disconnected!"); - - disconnectFromStream(); - }; - return false; - */ - return true; -}; - -bool deviceNodeCamera::validate(){ - DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id ); - if ( tDATA == NULL ) return false; - if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) { - //Settings Changed - memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) ); - }; - free( tDATA ); - if (!validateStream()) return false; - return deviceNode::validate(); -}; - -#undef LOG -#undef LOG_ERROR - +#include "deviceNodeCamera.h" +#include "../../components/base/basicCameraInfo.h" +#include "../../components/object/objectSystemAnchor.h" + +#include "../../hardware/uart/serialPort.h" +#include "../../hardware/uart/serialSOL.h" + +#include "../../data/protocol/protocolPTZ_VISCA.h" +#include "../../data/protocol/protocolPTZ_ONVIF.h" +#include "../../data/protocol/protocolPTZ_PELCO.h" + +#include "hardwareManager.h" +#include "hardwareResourceSerialProvider.h" +//#include "rgooseModuleEmbedVideo.h" + +#define LOG( X ) deviceNode::m_pLog->Log( "RCAM", this, X ) + +#ifdef DEBUG + #define LOG_ERROR( X ) {\ + char _tBfrErrMSG2w34[256];\ + sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ + deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ + } +#else + #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, X ); +#endif + +// all sizes in mm + +static const float AC_imager_diag[12] = { 5.0f, 5.68f, 6.0f, 6.592f, 8.0f, 8.933f, 11.0f, 16.0f, 22.5f, 30.1f, 43.3f, 69.7f }; +static const float AC_imager_width[12] = { 4.0f, 4.536f, 4.8f, 5.27f, 6.4f, 7.176f, 8.8f, 12.8f, 18.0f, 25.1f, 36.0f, 56.0f }; +static const float AC_imager_height[12] = { 3.0f, 3.416f, 3.6f, 3.96f, 4.8f, 5.319f, 6.6f, 9.6f, 13.5f, 16.7f, 24.0f, 41.5f }; + +#define CAM_FOCAL_LENGTH_CNT 49 +static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={ + 2.7f, + 3.0f, + 3.2f, + 3.5f, + 3.7f, + 3.9f, + 4.0f, + 4.3f, + 4.8f, + 5.0f, + 5.1f, + 5.5f, + 5.8f, + 6.0f, + 6.2f, + 6.6f, + 7.0f, + 7.1f, + 7.5f, + 7.6f, + 8.0f, + 8.6f, + 8.9f, + 9.0f, + 9.2f, + 9.3f, + 10.0f, + 10.5f, + 10.6f, + 10.7f, + 11.0f, + 11.3f, + 12.0f, + 12.9f, + 13.0f, + 13.5f, + 13.6f, + 14.0f, + 14.6f, + 15.0f, + 15.1f, + 15.5f, + 16.0f, + 17.0f, + 17.5f, + 17.7f, + 18.0f, + 18.2f, + 19.0f +}; + + +deviceNodeCamera::deviceNodeCamera( uint32_t NodeID ): deviceNode( NodeID ), basicThread(){ + //m_pLog = basicLogCollection::getInstance(); + m_pBEH = basicEventHeap::getInstance(); + + m_tTimeTSeconds = 0; + m_pProtocol = NULL; + m_pTransport = NULL; + m_pCurrentROI = NULL; + m_nRealFrameWidth = 640; + //m_bState = 0; + //m_bBusy = true; + m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f; + m_nRelativeMoveCounter = 0; + m_bRelativeMoveMatrix = 0; + m_fSpeedRise = 0.03f; + m_fZoomCorrection = 1.0f; + m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f; + //m_bSourceConnected = false; + //m_pSource = NULL; +}; + +deviceNodeCamera::deviceNodeCamera( DBC_NODE_DATA *Data ): deviceNode( Data ), basicThread(){ + //m_pLog = basicLogCollection::getInstance(); + m_pBEH = basicEventHeap::getInstance(); + + m_tTimeTSeconds = 0; + m_pProtocol = NULL; + m_pTransport = NULL; + m_pCurrentROI = NULL; + m_nRealFrameWidth = 640; + //m_bState = 0; + //m_bBusy = true; + m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f; + m_nRelativeMoveCounter = 0; + m_bRelativeMoveMatrix = 0; + m_fSpeedRise = 0.03f; + m_fZoomCorrection = 1.0f; + m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f; + //m_bSourceConnected = false; + //m_pSource = NULL; +}; + +deviceNodeCamera::~deviceNodeCamera(){ + LOG("Destruct"); + //m_mQPROTECTOR.unlock(); + waitThreadEnd( true ); + LOG("Thread Del OK"); + if( m_pTransport!= NULL ) { + if( m_sCamData.uart_id ) { + DBC_UART_DATA *tData = NULL; + tData = m_pDBC->getUartData( m_sCamData.uart_id ); + switch( tData->type ) { + case UART_TYPE_HARDWARE:{ + delete((serialPort*)m_pTransport); + break; + }; + case UART_TYPE_SOL:{ + delete((serialSOL*)m_pTransport); + break; + }; + }; + } + }; + LOG("Transport Del OK"); + if( m_pProtocol != NULL ) { + switch( m_sCamData.protocol ) { + case CAM_CTRL_PROTO_VISCA: + delete( (protocolPTZ_VISCA *) m_pProtocol ); + break; + case CAM_CTRL_PROTO_PELCO: + delete( (protocolPTZ_PELCO *) m_pProtocol ); + break; + case CAM_CTRL_PROTO_ONVIF: + delete( (protocolPTZ_ONVIF *) m_pProtocol ); + break; + default: + delete( m_pProtocol ); + }; + }; + LOG("Proto Del OK"); + //if( m_pPosition != NULL ) delete( m_pPosition ); + //LOG("Pos Del OK"); + //if( m_bSourceConnected ){ + // if( m_pSource != NULL ){ + // m_pSource->_close(); + // delete( m_pSource ); + // }; + //}; + //LOG("Stream Del OK"); +}; + +void deviceNodeCamera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){ +#ifdef DEBUG + char tB[128]; + LOG("Set Optical Parameters"); + sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth ); + LOG(tB); +#endif + if( frame_width > 0 && frame_heigth > 0 ) { + m_nRealFrameWidth = frame_width; + m_nRealFrameHeight = frame_heigth; + float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth ); + switch( m_sCamData.zoom_model ) { + case 0:{ +#ifdef DEBUG + LOG("\tFixed Zoom"); +#endif + m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f]; + m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag ); + m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag ); + m_fR2FL = .0f; + m_fZoomCorrection = .0f; + m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange ); + break; + }; + case 1:{ +#ifdef DEBUG + LOG("\tAutomated Zoom"); +#endif + m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f]; + m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag ); + m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom; + m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag ); + if( m_fMaxRange > m_fMinRange ){ + m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange)); + } else { + m_fR2FL = .0f; + }; + m_fZoomCorrection = 1.0f; + m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange); + break; + }; + case 2:{ +#ifdef DEBUG + LOG("\tManual Zoom"); +#endif + m_fMinRange = 5.0f; //Minimal 5 meters + if ( m_sCamData.max_range < m_fMinRange ) { + m_fMaxRange = m_fMinRange; + } else { + m_fMaxRange = m_sCamData.max_range; + }; + m_fZoomCorrection = m_sCamData.zoom * 0.01f; + m_fMinFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag ); + m_fMaxFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag ); + break; + }; + }; + m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL )); + m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL )); + m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL )); + m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL )); +#ifdef DEBUG + sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL ); + LOG(tB); + sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange ); + LOG(tB); + sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov ); + LOG(tB); + sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov ); + LOG(tB); +#endif + }; +}; + +bool deviceNodeCamera::create(){ + + setWM( CAM_WS_INIT ); + + if( m_sNodeData.aux_id == 0 ) return false; + + DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id ); + if( tData != NULL ) { + memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) ); + free( tData ); + } else { + LOG_ERROR("Cannot Fetch CAM Data from Database!"); + return false; + }; + if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1; + + updateOpticalParameters( 1280, 720 ); //HD Ready + + if( m_sCamData.ptz_speed_step > .0f ) { + m_fSpeedRise = m_sCamData.ptz_speed_step; + }; + + //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw ); + + //if( m_pPosition == NULL ) { + // LOG_ERROR("Position Constructor Failure!"); + // return false; + //}; + + //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z ); + //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot ); + + + + systemAnchor * SA = systemAnchor::getInstance(); + if( SA ){ + LOG("SysAnchor OK"); + if ( SA->beLocal() ) { + LOG("SysAnchor beLocal!"); + m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 ); //SA->getUTMZone() ); + m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); + } else { + LOG("SysAnchor is Global Position System!"); + C3D_DECART LD = SA->getXYZ(); + m_oPosition.setPositionXYZ( LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone() ); + m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); + }; + } else { + LOG("No SysAnchor!"); + m_oPosition.setPositionXYZ( m_sCamData.x , m_sCamData.y, m_sCamData.z ); + m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot ); + }; + m_oPosition.updateMatrix(); + +#ifdef DEBUG + char tB[128]; + sprintf( tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() ); + LOG( tB ); + sprintf( tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() ); + LOG( tB ); + sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() ); + LOG( tB ); +#endif + + if( m_pProtocol != NULL ){ + LOG_ERROR("Cannot Start: Service Already Running!"); + return false; + }; + switch( m_sCamData.protocol ){ + case CAM_CTRL_PROTO_VISCA: { +#ifdef DEBUG + LOG("VISCA"); +#endif + m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux ); + if( m_pProtocol!= NULL ) { + basicTransport *tTransport = NULL; + hardwareManager *tHM = hardwareManager::getInstance(); + if( tHM ){ + hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id ); + if( tRES != NULL ) { + if( tRES->isValid()) { + tTransport = static_cast(static_cast(tRES)); + if( m_pProtocol->setTransport( tTransport )){ + if( !m_pProtocol->create() ){ + LOG_ERROR("Protocol <<< Create >>> Failure!"); + }; + } else { + LOG_ERROR("Binding Port Failure!"); + }; + }; +#ifdef DEBUG + } else { + LOG("HWM Has No UART PORT!"); +#endif + }; +#ifdef DEBUG + } else { + LOG_ERROR("Can not get Hardware Manager!"); +#endif + }; + if( tTransport!= NULL ) { + LOG( "Bind Transport Port!" ); + } else { + LOG_ERROR("Transport Creation Failure!"); + delete( ( protocolPTZ_VISCA * )m_pProtocol ); + m_pProtocol = NULL; + }; +#ifdef DEBUG + } else { + LOG_ERROR("Protocol Creation failure!"); +#endif + }; + break; + }; + case CAM_CTRL_PROTO_PELCO:{ +#ifdef DEBUG + LOG("PELCO"); +#endif + m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux ); + + if( m_pProtocol!= NULL ) { + basicTransport *tTransport = NULL; + hardwareManager *tHM = hardwareManager::getInstance(); + if ( tHM ) { + hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id ); + if( tRES != NULL ) { + if( tRES->isValid()) { + tTransport = static_cast(static_cast(tRES)); + if( m_pProtocol->setTransport( tTransport )){ + if( !m_pProtocol->create() ){ + LOG_ERROR("Protocol <<< Create >>> Failure!"); + }; + } else { + LOG_ERROR("Binding Port Failure!"); + }; + }; +#ifdef DEBUG + } else { + LOG("HWM Has No UART PORT!"); +#endif + }; +#ifdef DEBUG + } else { + LOG_ERROR("Can not get Hardware Manager!"); +#endif + }; + if( tTransport!= NULL ) { + LOG( "Bind Transport Port!" ); + } else { + LOG_ERROR("Transport Creation Failure!"); + delete( ( protocolPTZ_PELCO * )m_pProtocol ); + m_pProtocol = NULL; + }; +#ifdef DEBUG + } else { + LOG_ERROR("Protocol Creation failure!"); +#endif + }; + break; + }; + case CAM_CTRL_PROTO_ONVIF:{ +#ifdef DEBUG + LOG("ONVIF"); +#endif + //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode ); + if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) { + char tURI[256]; + if ( m_sCamData.port ) { + sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl); + } else { + sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl); + }; + m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode); + } else { + m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode); + } + + if( m_pProtocol!=NULL ) { + if( !m_pProtocol->create() ) { + LOG_ERROR("Error Creating Proto!"); + delete( (protocolPTZ_ONVIF*) m_pProtocol ); + m_pProtocol = NULL; + }; + }; + break; + }; + default:{ +#ifdef DEBUG + LOG("Camera Has no defined control protocol"); +#endif + }; + }; + if( m_pProtocol == NULL ) { + if( m_sCamData.protocol == 0 ) { + m_sCamData.online = true; + } else LOG_ERROR("No Proto Created!"); + } else { +#ifdef DEBUG + LOG("Proto is OK"); +#endif + m_sCamData.online = init(); + }; + m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online ); + return m_sCamData.online; +}; + + + +//void deviceNodeCamera::setBusy( bool Status ){ + //m_bBusy = Status; +//}; + + +bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){ + if( m_sCamData.online ) { + uint32_t MoveMatrix = 0; + if( PanSPD != .0f ){ + if( PanSPD > 0 ) { + MoveMatrix |= PTZ_DRIVE_PAN_RIGHT; + } else { + MoveMatrix |= PTZ_DRIVE_PAN_LEFT; + }; + }; + if( TiltSPD != .0f ){ + if( TiltSPD > 0 ) { + MoveMatrix |= PTZ_DRIVE_TILT_UP; + } else { + MoveMatrix |= PTZ_DRIVE_TILT_DOWN; + }; + }; + if( ZoomSPD != .0f ){ + if( ZoomSPD > 0 ) { + MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE; + } else { + MoveMatrix |= PTZ_DRIVE_ZOOM_TELE; + }; + }; + if( MoveMatrix != 0 ) { + m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) ); + }; + return m_pProtocol->move( MoveMatrix ); + }; + return false; +}; + +bool deviceNodeCamera::relativeStop(){ + if( m_sCamData.online ) { + return m_pProtocol->move( 0 ); + }; + return false; +}; + +CAM_TASK *deviceNodeCamera::fetchCamTask( void ){ + CAM_TASK *rVal = NULL; + m_mQPROTECTOR.lock(); + if (m_qCAM_TASK.size()) { + rVal = m_qCAM_TASK.front(); + m_qCAM_TASK.pop(); + }; + m_mQPROTECTOR.unlock(); + return rVal; +}; + +bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){ + if ( ptr == NULL ) return false; + m_mQPROTECTOR.lock(); + m_qCAM_TASK.push( ptr ); + m_mQPROTECTOR.unlock(); + return true; +}; + +bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){ + if( ptr == NULL ) return false; + float tFI = fabsf( ptr->cam_position.fi - tp.fi ); + float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta ); +#ifdef DEBUG + char tB[128]; + sprintf(tB,"FC: FOV:[%.5f:%.5f] dTG:[%.5f:%.5f] %.2f<%.2f>%.2f",ptr->h_fov ,ptr->v_fov, tFI, tTETTA, ptr->min_range, tp.r, ptr->max_range ); + LOG(tB); +#endif + if( tFI > ptr->h_fov ) return false; + if( tTETTA > ptr->v_fov ) return false; + if( tp.r < ptr->min_range ) return false; + if( tp.r > ptr->max_range ) return false; + return true; +}; + +bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){ + if( db_id == 0 ) return false; + CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD)); + if( tTrackRecord == NULL ) return false; +#ifdef DEBUG_EXTRA + LOG("Fusion: Consuming new Data!"); +#endif + memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD)); + tTrackRecord->target_direction.fi = tOR.fi; + tTrackRecord->target_direction.r = tOR.r; + tTrackRecord->target_direction.tetta = tOR.tetta; + tTrackRecord->evt_db_ID = db_id; + tTrackRecord->ts_ms = getMilliseconds(); + m_mROIsProtector.lock(); + if( m_vROIS.size() ){ + vCAM_ROIS::iterator idx = m_vROIS.begin(); + for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break; + if ( idx != m_vROIS.end() ) { + // have Match! +#ifdef DEBUG_EXTRA + LOG("ROI MATCHED!"); +#endif + (*idx)->targets.push_back( tTrackRecord ); + fusion_validateROI( tTrackRecord->ts_ms, (*idx) ); + } else { +#ifdef DEBUG_EXTRA + LOG("NO MATCH FOUND!"); +#endif + // No mathced ROI -> create new one + CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI)); + if ( tROI ) { + memset( tROI, 0 , sizeof(CAM_ROI) ); + tROI->ts_ms = tTrackRecord->ts_ms; + spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); + + tROI->cam_position.fi = tOR.fi; + tROI->cam_position.tetta = tOR.tetta; + tROI->cam_position.r = tOR.r; + + float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange ); + + tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function + tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen + + tROI->targets.push_back( tTrackRecord ); + m_vROIS.push_back( tROI ); + }; + }; + } else { +#ifdef DEBUG_EXTRA + LOG("Fusion: NO ROI -> CREATE!"); +#endif + // + CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI)); + if ( tROI ) { + memset( tROI, 0 , sizeof(CAM_ROI) ); + tROI->ts_ms = tTrackRecord->ts_ms; + //Set vector Cam Coordinates + tROI->cam_position.fi = tOR.fi; + tROI->cam_position.tetta = tOR.tetta; + tROI->cam_position.r = tOR.r; + // Set vector OnVIF coordinates + spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); + + // make fusion data BOX + float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange ); + + tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation + tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen + + // Add ROI to vector + tROI->targets.push_back( tTrackRecord ); + m_vROIS.push_back( tROI ); + } else LOG_ERROR("No MEM4ROI"); + }; + m_mROIsProtector.unlock(); + return true; +}; + +bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){ + if( ptr == NULL ) return false; + if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false; + +#ifdef DEBUG_EXTRA + char tB[128]; + sprintf( tB, "\tROI [%d]", ptr->targets.size() ); + LOG( tB ); +#endif + + if( ptr->targets.size() ) { + double tA = 0.0; + double tB = 0.0; + double tC = 0.0; + uint32_t tCNT = 0; + for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) { + if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) { + free( (*idx) ); + ptr->targets.erase( idx ); + } else { + tA = tA + (*idx)->target_direction.fi; + tB = tB + (*idx)->target_direction.tetta; + tC = tC + (*idx)->target_direction.r; + tCNT++; + }; + }; + if( tCNT ) { + C3D_SPH_DEGREES L = { 0 }; + L.fi = (float) ( tA /(double) tCNT ); + L.tetta = (float) ( tB /(double) tCNT ); + L.r = (float) ( tC /(double) tCNT ); + spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles); + ptr->ts_ms = cts; + + float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange ); + + ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth ); + ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation + ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen + return true; + }; + }; + return false; +}; + +CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){ + CAM_ROI *tVAL = NULL; + uint64_t cts = 0; + m_mROIsProtector.lock(); + vCAM_ROIS::iterator sRec; + if( m_vROIS.size() ){ + for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){ + if ( tVAL == NULL ) { + sRec = idx; + tVAL = (*idx); + cts = tVAL->ts_ms; + } else { +#ifdef CAM_FETCH_ROI_NEWEST + if ( (*idx)->ts_ms > cts ) +#else + if ( (*idx)->ts_ms < cts ) +#endif + { + sRec = idx; + tVAL = (*idx); + cts = tVAL->ts_ms; + }; + }; + }; + m_vROIS.erase( sRec ); + }; + m_mROIsProtector.unlock(); + return tVAL; +}; + +DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){ + DBC_SNAPSHOT_DATA *rVal = NULL; + if( ptr == NULL ) return NULL; + if( ptr->targets.size() ){ + //if( m_bSourceConnected ){ + rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA)); + if( rVal ){ + memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) ); + //rVal->stream_id = m_nActiveStreamID; + rVal->stream_id = m_sNodeData.aux_parameter; + for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) { + LOG("Added Event ID"); + rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID ); + }; + }; + //}; + ptr->targets.clear(); + } else { + LOG("No Events ID to ROI"); + }; + // Destruct ROI MEM + delete( (CAM_ROI *) ptr ); + ptr = NULL; + // Not Setting MD5 and TS -> will set at capture time + return rVal; +}; + +void deviceNodeCamera::fusion_validate( void ){ + m_mROIsProtector.lock(); + if( m_vROIS.size() ){ +#ifdef DEBUG_EXTRA + char tB[128]; + sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() ); + LOG( tB ); +#endif + uint64_t cts = getMilliseconds(); + for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){ + if( !fusion_validateROI( cts, *idx ) ) { + free( (*idx) ); + m_vROIS.erase( idx ); + }; + }; + }; + m_mROIsProtector.unlock(); +}; + +bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){ + if( m_sCamData.online ){ + CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); + if( tTask ){ + memset(tTask, 0, sizeof(CAM_TASK)); + tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD)); + if( tTask->CMD ){ + tTask->CMD->CMD = PTZ_ABSOLUTE; + tTask->CMD->PAN = PanPOS; + tTask->CMD->TILT = TiltPOS; + tTask->CMD->ZOOM = ZoomPOS; + tTask->movemet_needed = true; + tTask->source_evt_id = TargetRefID; + setWM( CAM_WS_BUSY ); + return pushCamTask( tTask ); + } else { + LOG_ERROR("CMD malloc Error!"); + free( tTask ); + }; + } else LOG_ERROR("Task malloc error!"); + }; + return false; +}; + +bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){ + if( m_sCamData.online ){ + + CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); + if( tTask ){ + memset(tTask, 0, sizeof(CAM_TASK)); + tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD)); + if( tTask->CMD ){ + tTask->CMD->CMD = PTZ_ABSOLUTE; + tTask->CMD->PAN = O.pan; + tTask->CMD->TILT = O.tilt; + tTask->CMD->ZOOM = O.zoom; + tTask->movemet_needed = true; + tTask->source_evt_id = TargetRefID; + setWM( CAM_WS_BUSY ); + return pushCamTask( tTask ); + } else { + LOG_ERROR("CMD malloc Error!"); + free( tTask ); + }; + } else LOG_ERROR("Task malloc error!"); + }; + return false; +}; + +void deviceNodeCamera::m_fThreadProc( void ){ + DBC_PUSHED_CMD * PUSHED = NULL; + m_bThreadStatus = TH_WORKING; + bool bNewSecondLoop = false; + uint32_t cTimeSeconds = 0; + uint32_t oTimeSeconds = 0; + while( m_bThreadStatus < TH_ENDED ) { + + cTimeSeconds = (uint32_t) time( NULL ); + + if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds; + + uint8_t tState = getWM(); + + char tB[128]; + + fusion_validate(); + + switch( tState ) { + case CAM_WS_INIT:{ +#ifdef DEBUG + LOG("Init Camera - PowerMode"); +#endif + //setBusy( true ); + if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE ); + //setBusy( false ); + break; + }; + case CAM_WS_WHITE_BALLANCE:{ +#ifdef DEBUG + LOG("Set WB"); +#endif + //setBusy( true ); + if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT ); + //setBusy( false ); + break; + }; + case CAM_WS_BACKLIGHT:{ +#ifdef DEBUG + LOG("Set BLC"); +#endif + //setBusy( true ); + if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS ); + //setBusy( false ); + break; + }; + case CAM_WS_AUTO_FOCUS:{ +#ifdef DEBUG + LOG("Set AF"); +#endif + //setBusy( true ); + if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME ); + //setBusy( false ); + break; + }; + case CAM_WS_INIT_GO_HOME:{ +#ifdef DEBUG + LOG("INIT GO -1 0 -1"); +#endif + m_nHomingCounter = 0; + if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) { + m_fCP_Pan = -1; + m_fCP_Tilt = 0; + m_fCP_Zoom = -1; + setWM( CAM_WS_READY ); + }; + break; + }; + case CAM_WS_AUTOMATED_MOVE:{ + m_nRelativeMoveCounter++; + if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) { + //Pan Move + if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) { + m_fRM_Pan -= m_fSpeedRise; + } else { + m_fRM_Pan += m_fSpeedRise; + }; + }; + if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) { + //TILT Move + if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) { + m_fRM_Tilt -= m_fSpeedRise; + } else { + m_fRM_Tilt += m_fSpeedRise; + } + }; + if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) { + //ZOOM Move + if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) { + //Wide + m_fRM_Zoom -= m_fSpeedRise; + } else { + //Tele + m_fRM_Zoom += m_fSpeedRise; + }; + }; + if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f; + if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f; + if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f; + if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f; + if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f; + if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f; + + m_fCP_Zoom += 0.5f*m_fRM_Zoom; + + if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f; + if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f; + + if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){ + m_nRelativeMoveCounter = 0; + setWM( CAM_WS_READY ); + break; + }; + + if( m_sCamData.ptz_axel ) { //Manual + if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) { + m_nRelativeMoveCounter = 0; + setWM( CAM_WS_READY ); + }; + } else { + // Stepping Axel by Zoom + uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) )); + if( m_nRelativeMoveCounter > tVal ) { + m_nRelativeMoveCounter = 0; + setWM( CAM_WS_READY ); + }; + }; + break; + }; + case CAM_WS_STOP_MOVE:{ + m_nRelativeMoveCounter++; + m_fRM_Pan = .0f; + m_fRM_Pan = .0f; + m_fRM_Tilt = .0f; + m_fRM_Tilt = .0f; + m_fRM_Zoom = .0f; + m_fRM_Zoom = .0f; + if( relativeStop() ) { + m_nHomingCounter = 0; + m_nRelativeMoveCounter = 0; + setWM( CAM_WS_READY ); + break; + }; + if( m_sCamData.ptz_axel ) { + if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY); + } else { + uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) )); + if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY); + }; + break; + }; + case CAM_WS_GO_ABS_ROI: + case CAM_WS_GO_ABS_POSITION:{ + setWM( CAM_WS_BUSY ); + if( m_pCurrentROI != NULL ){ + m_nHomingCounter = 0; + if( m_pBEH ){ + EVENT_CONTAINER *tEVT = createBasicEvent( + m_sNodeData.node_id, + EVT_CLASS_CAMERA_CTRL, + EVT_CAMERA_MOVE_TO, + NULL, NULL, NULL ); + if( tEVT ){ + //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA + m_pBEH->broadcast( tEVT ); + }; + }; + //update position Info + m_fCP_Pan = m_pCurrentROI->cam_orientation.pan; + m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt; + m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom; + + // Do Movement + LOG("GO ABS Movement -> INSTCAP??" ); + if( !m_pProtocol->goAbsolute( + m_pCurrentROI->cam_orientation.pan, + m_pCurrentROI->cam_orientation.tilt, + ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection ) + )){ + LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?"); + setWM( CAM_WS_END );//CAM_WS_READY ); + } else { + // Waiting Movement finish + setWM( CAM_WS_WAIT_ONPOS ); + /* + // Send Capture event + DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI ); + if( tData != NULL ) { + if( m_pBEH ){ + EVENT_CONTAINER *tEVT = createBasicEvent( + m_sNodeData.node_id, + EVT_CLASS_CAMERA_CTRL, + EVT_CAMERA_ON_POSITION, + NULL, NULL, tData ); + if( tEVT ){ +#ifdef DEBUG + LOG("BCAST CAM ONPOS"); +#endif + //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA + m_pBEH->broadcast( tEVT ); + } else { + free( tData ); + }; + } else { + free( tData ); + }; + }; + */ + }; + //utilizeROI( m_pCurrentROI ); + } else setWM( CAM_WS_READY ); + break; + }; + case CAM_WS_WAIT_ONPOS:{ + if( m_pCurrentROI != NULL ){ + m_pProtocol->waitMoveIsDone(); + if( 0 ){ + LOG_ERROR("Cannot Issue getPosition Command!->Restart Camera?"); + setWM( CAM_WS_END );//CAM_WS_READY ); + } else { + // Send Capture event + DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI ); + if( tData != NULL ) { + if( m_pBEH ){ + EVENT_CONTAINER *tEVT = createBasicEvent( + m_sNodeData.node_id, + EVT_CLASS_CAMERA_CTRL, + EVT_CAMERA_ON_POSITION, + NULL, NULL, tData ); + if( tEVT ){ + #ifdef DEBUG + LOG("BCAST CAM ONPOS"); + #endif + //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA + m_pBEH->broadcast( tEVT ); + } else { + free( tData ); + }; + } else { + free( tData ); + }; + }; + }; + utilizeROI( m_pCurrentROI ); + }; + setWM( CAM_WS_READY ); + break; + }; + case CAM_WS_GO_HOMING_PRESET:{ + if( m_sCamData.home_token[0] != (char)0 ) { + //LOG( m_sCamData.home_token ); + if( !m_pProtocol->goPreset( m_sCamData.home_token )){ + LOG_ERROR("Cannot Issue goPreset Command!"); + }; + }; + setWM( CAM_WS_READY ); + break; + }; + case CAM_WS_BUSY:{ + //Handle Operation TO + setWM( CAM_WS_READY ); + break; + }; + case CAM_WS_READY:{ + m_pCurrentROI = fusion_fetchROI(); + if( m_pCurrentROI != NULL ) { +#ifdef DEBUG_EXTRA__ + LOG("HAVE ROI to see!"); +#endif + setWM( CAM_WS_GO_ABS_POSITION ); + } else { + //Idle Work ---> + if( m_sCamData.homing_enable ){ + if ( bNewSecondLoop ) { + if ( m_nHomingCounter < m_sCamData.homing_to ) { + m_nHomingCounter++; + } else { + //LOG("Go Home Position!"); + m_nHomingCounter = 0; + setWM( CAM_WS_GO_HOMING_PRESET ); + break; + }; + }; + }; + CAM_TASK * tTASK = fetchCamTask(); + if ( tTASK != NULL ) { + if( tTASK->movemet_needed ){ + CAM_PTZ_CMD *tCMD = tTASK->CMD; + if( tCMD ) { + switch( tCMD->CMD ) { + case PTZ_STOP:{ + m_nHomingCounter = 0; + setWM( CAM_WS_STOP_MOVE ); + break; + }; + case PTZ_MOVE:{ + if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0; + m_bRelativeMoveMatrix = 0; + m_fRM_Pan = tCMD->PAN; + m_fRM_Tilt = tCMD->TILT; + m_fRM_Zoom = tCMD->ZOOM; + m_nHomingCounter = 0; + setWM( CAM_WS_AUTOMATED_MOVE ); + break; + }; + case PTZ_ABSOLUTE:{ + m_pCurrentROI = createCAM_ROI(); + if( m_pCurrentROI != NULL ){ + CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD)); + if ( tTarget != NULL ) { + memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) ); + tTarget->evt_db_ID = tTASK->source_evt_id; + m_pCurrentROI->targets.push_back( tTarget ); + m_pCurrentROI->cam_orientation.pan = tCMD->PAN; + m_pCurrentROI->cam_orientation.tilt = tCMD->TILT; + m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM; + m_nHomingCounter = 0; + setWM(CAM_WS_GO_ABS_POSITION); + } else { + utilizeROI( m_pCurrentROI ); + }; + }; + break; + }; + case PTZ_RELOAD_CAMERA:{ + LOG("Exit Command Rcpt"); + m_bThreadStatus = TH_ENDED; + }; + }; + free( tCMD ); + tTASK->CMD = NULL; + }; + } else { + LOG("No Movement -> INSTCAP" ); + // No Movement -> Just Capture + if( m_pBEH ){ + EVENT_CONTAINER *tEVT = createBasicEvent( + m_sNodeData.node_id, + EVT_CLASS_CAMERA_CTRL, + EVT_CAMERA_ON_POSITION, + NULL, NULL, NULL ); + if( tEVT ){ + tEVT->m_db_id = tTASK->source_evt_id; + m_pBEH->broadcast(tEVT); + }; + }; + }; + free( tTASK ); + } else { + // Lowest Priority to Web Client Commands + #ifdef DEBUG_EXTRA__ + LOG("Check Web CMD!"); + #endif + PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id ); + if( PUSHED != NULL ) { + #ifdef DEBUG_EXTRA__ + LOG("HAVE Web CMD!"); + #endif + m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN ); + if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) { + switch( PUSHED->CMD ) { + case 0x80:{ + //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET; + break; + }; + case 0x81:{ + //m_bWM = ANODE_WM_2D_GET_FAST_DRAW; + break; + }; + case 0x82:{ + break; + }; + }; //?? Check if we don't miss something!! + } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here + //Direct Movment Mode + if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) { + m_nHomingCounter = 0; + setWM( CAM_WS_STOP_MOVE ); + } else { + char tB[64]; + if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0; + m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1; + m_nHomingCounter = 0; + setWM( CAM_WS_AUTOMATED_MOVE ); + }; + }; + free( PUSHED ); + PUSHED = NULL; + }; + }; + }; + break; + }; + case CAM_WS_END:{ + LOG("Exit Command Rcpt"); + m_bThreadStatus = TH_ENDED; + break; + }; + }; + SLEEP( DEFAULT_THREAD_SLEEP_TIME ); + }; +#ifdef DEBUG + LOG("CTRL Loop End!"); +#endif +}; + +bool deviceNodeCamera::testFOV( C3D_DECART L ){ + //System Coords -> trans to local and go to funct + systemAnchor* tSystem = systemAnchor::getInstance(); + if ( tSystem == NULL ) return false; + C3D_SPH_DEGREES SL; +#ifdef DEBUG + LOG("Test FOV local Decart coords"); +#endif + if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false; + return testFOV( SL ); +}; + +bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){ + //Global Coords + systemAnchor* tSystem = systemAnchor::getInstance(); + if ( tSystem == NULL ) return false; + C3D_SPH_DEGREES SL; +#ifdef DEBUG + LOG("Test FOV WGS coords"); +#endif + if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false; + return testFOV( SL ); +}; + +bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){ + //Local Coords + C3D_SPH_DEGREES SL; +#ifdef DEBUG + LOG("Test FOV local Spherical(rad) coords"); +#endif + spherical2spherical( L, SL ); + return testFOV( SL ); +}; + +bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){ +#ifdef DEBUG + char tB[128]; +#endif +#ifdef DEBUG + LOG("Test FOV local Spherical(deg) coords"); +#endif + switch ( m_sCamData.triggering_mode ) { + case CAM_TRIGGER_STRICT:{ + if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){ +#ifdef DEBUG + sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); + LOG( tB ); +#endif + return false; + }; + break; + }; + case CAM_TRIGGER_ALL_NEAR:{ + if( SL.r > m_fMaxRange ){ +#ifdef DEBUG + sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); + LOG( tB ); +#endif + return false; + }; + break; + }; + case CAM_TRIGGER_ALL_FAR:{ + if( SL.r < m_fMinRange ){ +#ifdef DEBUG + sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange ); + LOG( tB ); +#endif + return false; + }; + break; + }; + case CAM_TRIGGER_ALL: return true; + }; + + float HS = AC_imager_width[m_sCamData.imager_type]; + float VS = AC_imager_height[m_sCamData.imager_type]; + float HS2 = HS * HS; + float VS2 = VS * VS; + float IR = HS / VS; + float IR2 = IR * IR; + + if( m_sCamData.pan_angles == 0 ) { + // Fixed Camera + float tA = fabsf( SL.fi ); + float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL ); + if ( tA > tZ ) return false; + } else { + if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED + // Partial Pan + switch( m_sCamData.pan_model ) { + case 1: { + // Regular Pan + float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL ); + float tRad = deg2rad( SL.fi ); +#ifdef DEBUG + sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad ); + LOG( tB ); +#endif + if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false; + break; + }; + case 2:{ + + break; + }; + case 3:{ + + break; + }; + }; + }; + }; + + if( m_sCamData.tilt_angles == 0 ){ + //Fixed Camera + if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false; + } else { + if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass + // Partial Tilt + switch( m_sCamData.tilt_model ) { + case 1: { + // Regular Tilt + float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ; + float tRad = deg2rad( SL.tetta ); + if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false; + break; + }; + case 2:{ + break; + }; + }; + }; + }; + return true; +}; + +bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){ + systemAnchor* tSystem = systemAnchor::getInstance(); + if( tSystem == NULL ) return false; + C3D_SPH_DEGREES SL; +#ifdef DEBUG_EXTRA + LOG("Look at WGS coords!"); +#endif + tSystem->convertToLocal(&m_oPosition, L, SL ); + return lookAt(TargetRefID, SL ); +}; + +bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){ + systemAnchor* tSystem = systemAnchor::getInstance(); + if( tSystem == NULL ) return false; + C3D_SPH_DEGREES SL; +#ifdef DEBUG_EXTRA + LOG("Look at System Decart coords!"); +#endif + tSystem->convertToLocal( &m_oPosition, L, SL ); + return lookAt(TargetRefID, SL ); +}; + +bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){ + C3D_SPH_DEGREES SL; +#ifdef DEBUG_EXTRA + LOG("Look at spherical(rad) 2 spherical(deg) coords!"); +#endif + spherical2spherical( L, SL ); + return lookAt(TargetRefID, SL ); +}; + +bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){ + //char tB[128]; + //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta ); + //LOG( tB ); + //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ ); + //LOG( tB ); +#ifdef DEBUG_EXTRA + LOG("Look at local spherical deg. coords!(final)"); +#endif + //if( getWM() == CAM_WS_READY ){ + // C3D_ORIENT_ONVIF LL; + // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles ); + // return absoluteMove( TargetRefID, LL ); + //}; + // Camera is Busy + return fusion_consumeData(TargetRefID, L); +}; + +bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){ + CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK)); + if( tTask ){ + memset(tTask, 0, sizeof(CAM_TASK)); + tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) ); + if( tTask->CMD ){ + tTask->CMD->CMD = PTZ_CAPTURE_ONLY; + tTask->source_evt_id = TargetRefID; + return pushCamTask( tTask ); + } else { + free( tTask ); + }; + }; + return false; +}; + +bool deviceNodeCamera::connectToStream( uint32_t id ){ + /* + if( id ){ + argusKernelModule * tKM = NULL; + argusKernel * tKRNL = NULL; + argusModuleFactory * tMF = NULL; + if( m_pDBC == NULL ) { + LOG_ERROR("No Database Connector!"); + return false; + }; + tKRNL = argusKernel::getInstance(); + if( tKRNL ) { + tMF = tKRNL->getModuleFactory(); + if( tMF ) { + tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO ); + } else { + LOG_ERROR("No ModuleFactory Instance!"); + return false; + }; + } else { + LOG_ERROR("No Kernel Instance!"); + return false; + }; + if( tKM ) { + // New Module + DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id ); + if( tDATA ) { + m_sMediaSource.mediaStreamId = id; + if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { + if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource); + m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource); + } else { + if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource); + m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource ); + }; + + if( m_pSource != NULL ) { + char tMRL[1024]; +#ifdef DEBUG + LOG("Source OK"); +#endif + ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES ); + + int tW = ((argusModuleEmbedVideo*)tKM)->getWidh(); + int tH = ((argusModuleEmbedVideo*)tKM)->getHeight(); + int pix = -1; + bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 ); + bool out_16_9 = (((tW*100) / tH ) > 140 ); + updateOpticalParameters( tW, tH ); + if( strlen( tDATA->c_type ) ){ + + }; + //if( tW > 0 ){ + // //m_pSource->setProportionalByWidth( tW ); + // m_pSource->setSize( tW, tH ); + //}; + if( in_16_9 != out_16_9 ){ + if( in_16_9 ){ + tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000; + } else { + tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000; + }; + }; + if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) { + sprintf( tMRL, tDATA->mrl ); + } else { + DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id ); + if( tCAM ) { + sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl ); + free( tCAM ); + } else { + LOG_ERROR("Error Getting CAM Data!"); + delete( m_pSource ); + m_pSource = NULL; + return false; + }; + }; + LOG( tMRL ); + if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){ + m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM ); + if ( !m_bSourceConnected ) { + //delete( m_pSource ); + if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { + delete((videoAVLIBSource*)m_pSource); + } else { + delete((videoLIVE555Source*)m_pSource); + }; + m_pSource = NULL; + } else { + m_nActiveStreamID = id; +#ifdef DEBUG + LOG("Source is Connected to Output."); +#endif + }; + } else { + LOG_ERROR("Can not Open Video Source!"); + //delete( m_pSource ); + if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) { + delete((videoAVLIBSource*)m_pSource); + } else { + delete((videoLIVE555Source*)m_pSource); + }; + m_pSource = NULL; + }; + } else { + LOG_ERROR("Video Source Stream Failure!"); + }; + free( tDATA ); + }; + } +#ifdef DEBUG + else LOG("No Video Module!") +#endif + ; + }; + LOG("Connect Finished!"); + return m_bSourceConnected; + */ + return true; +}; + +bool deviceNodeCamera::disconnectFromStream( void ){ + /*if( m_bSourceConnected ) { + if( m_pSource ){ + delete( m_pSource ); + m_pSource = NULL; + }; + m_bSourceConnected = false; + }; + return !m_bSourceConnected; + */ + return true; +}; + +bool deviceNodeCamera::validateStream( void ){ + /* + if( m_bSourceConnected ) { + if( m_pSource ) { + if( m_pSource->isAttached() ){ + if( m_pSource->getStatus() < MS_STOPED ) return true; +#ifdef DEBUG + LOG("Source is Finished!"); +#endif + }; +#ifdef DEBUG + LOG("Source is Disconnected!"); +#endif + } else LOG_ERROR("No Source Object! Set Disconnected!"); + + disconnectFromStream(); + }; + return false; + */ + return true; +}; + +bool deviceNodeCamera::validate(){ + DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id ); + if ( tDATA == NULL ) return false; + if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) { + //Settings Changed + memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) ); + }; + free( tDATA ); + if (!validateStream()) return false; + return deviceNode::validate(); +}; + +#undef LOG +#undef LOG_ERROR + diff --git a/lib/App/common/deviceNodeCamera.h b/lib/App/common/deviceNodeCamera.h index c879f084..f2584a8d 100644 --- a/lib/App/common/deviceNodeCamera.h +++ b/lib/App/common/deviceNodeCamera.h @@ -75,6 +75,7 @@ typedef enum E_CAM_WORKING_STATE{ CAM_WS_GO_ABS_ROI = 15, CAM_WS_READY = 64, CAM_WS_BUSY = 128, + CAM_WS_WAIT_ONPOS = 129, CAM_WS_END = 255 }CAM_WORK_STATE; diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 83264ca6..fb022616 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -97,8 +97,10 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo( uint32_t moduleID ): kernelModul sprintf( tB, "Web Streams Port: %d", m_sEmbedVideoData.port ); LOG( tB ); sprintf( tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height ); - LOG( tB ); - if( m_sEmbedVideoData.show_ts ) LOG("Show TimeStamp"); + LOG( tB ); + if( m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES ) LOG("Output Pictures"); + if( m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO ) LOG("Output Video"); + if (m_sEmbedVideoData.show_ts) LOG("Show TimeStamp"); if( m_sEmbedVideoData.show_label ) LOG("Show Label"); if( m_sEmbedVideoData.show_logo ) LOG("Show Logo"); #endif @@ -380,7 +382,7 @@ void *kernelModuleEmbedVideo::webClientProc( void *ptr ){ #ifdef HAVE_SMAL_PREVIEW_STREAMS }; #endif -#ifdef DEBUG +#ifdef DEBUG_EXTRA TLOG("VFEED Sent No Signal Image!"); #endif } else { @@ -461,7 +463,7 @@ void *kernelModuleEmbedVideo::webClientProc( void *ptr ){ }; } else { -#ifdef DEBUG +#ifdef DEBUG_EXTRA TLOG("VFEED Sent No Signal Image!"); #endif CTX->ModulePtr->m_mEncodedProtector.unlock(); @@ -1434,15 +1436,13 @@ void kernelModuleEmbedVideo::dispatchEvent( EVENT_CONTAINER *Evt ){ //https://www.mikekohn.net/software/playstation2.php bool kernelModuleEmbedVideo::saveStreamPicture( DBC_SNAPSHOT_DATA *ptr ){ - if( ptr == NULL ) return false; - - if( m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES ){ + if( ( m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES ) == EVE_OUTPUT_PICTURES ){ LOG("Saving Picture!"); - saveStreamPicture( ptr->stream_id ); + //saveStreamPicture( ptr->stream_id ); - /* + //* if( ptr->assoc_event_ids.size() ){ if( m_mapCAPTURES.find( ptr->stream_id ) != m_mapCAPTURES.end() ){ m_mPictureProtector.lock(); @@ -1468,9 +1468,9 @@ bool kernelModuleEmbedVideo::saveStreamPicture( DBC_SNAPSHOT_DATA *ptr ){ #endif }; }; - */ + //*/ }; - if( 1 || m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO ){ + if( ( m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO ){ LOG("Saving Video!"); if( ptr->assoc_event_ids.size() ){ m_mStreamHandlersProtector.lock(); @@ -1480,10 +1480,22 @@ bool kernelModuleEmbedVideo::saveStreamPicture( DBC_SNAPSHOT_DATA *ptr ){ uint32_t cfrm; for( DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++ ) (*hnd).second->triggerEvent( (*idx), db_id, cfrm ); - } else LOG("No Stream Found!"); +#ifdef DEBUG_EXTRA + } else { + LOG("No Stream Found!"); +#endif + }; m_mStreamHandlersProtector.unlock(); - } else LOG("No Assoc EVTIDS!"); - } else LOG("No Video Mode!"); +#ifdef DEBUG_EXTRA + } else { + LOG("No Assoc EVTIDS!"); +#endif + }; +#ifdef DEBUG_EXTRA + } else { + LOG("No Video Mode!"); +#endif + }; return true; }; diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index 33cb72bb..031581a9 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -23,7 +23,7 @@ class kernelModuleEmbedVideo; #define FILESTREAMBUFFERSZ 1048576 -#ifndef MEKOROT_PROJECT +#ifndef MEKOROT_PROJECT__ #define EVE_ENABLE_VIDEO #endif //#define USE_SCALING_IN diff --git a/lib/data/protocol/protocol.cpp b/lib/data/protocol/protocol.cpp index eee3f0af..6be3e9cf 100644 --- a/lib/data/protocol/protocol.cpp +++ b/lib/data/protocol/protocol.cpp @@ -122,9 +122,11 @@ basicTransport *protocolParser::getCurrentTransport( void ){ bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){ bool rv = false; if( m_pParserBuffer != NULL ) { -#ifdef DUBUG_COMMUNICATION - char ttZZ[256]; +#ifdef DEBUG + char ttZZ[513]; sprintf( ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount ); + LOG(ttZZ); + for ( uint8_t i = 0; i < BufferSize; i++ ) sprintf( ttZZ+(i*2), "%02X", Buffer[i] ); LOG(ttZZ); #endif m_mBufferProtectorMutex.lock(); @@ -150,8 +152,8 @@ bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){ protocolPackage* tVal = NULL; tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount ); if (tVal != NULL) { - #ifdef DUBUG_COMMUNICATION - LOG("Package Handling"); + #ifdef DUBUG + LOG("Package Accepted"); char tBuf[256]; sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount); LOG(tBuf); @@ -159,14 +161,14 @@ bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){ //Move unused data toward buffer's begin to parse at next parse attempt m_nParserDataCount -= tVal->m_nSize; if( m_nParserDataCount > 0 ) { - memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount); + memcpy( m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount ); } else { // Clean Buff; memset( m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE); m_nParserDataCount = 0; }; // In Case implementation of packetHandler - handler must return !true! - if (!packetHandler(tVal)) { + if( !packetHandler(tVal) ){ LOG("Handler Not Consumed Data!"); m_vPackagePool.push_back(tVal); if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) { @@ -177,10 +179,12 @@ bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){ free(tVal); }; } else { + LOG("Handler Consumed Data!"); dataEraser(tVal->m_pData); free(tVal); }; - } else { + } else { + LOG_ERROR("Packet Not Extracted!"); // All data consumed in MAVLINK proto but not parsed into message m_nParserDataCount = 0; }; diff --git a/lib/data/protocol/protocolPTZ.h b/lib/data/protocol/protocolPTZ.h index 4248d06f..e13244ab 100644 --- a/lib/data/protocol/protocolPTZ.h +++ b/lib/data/protocol/protocolPTZ.h @@ -194,6 +194,7 @@ class protocolPTZ: public protocolParser { virtual bool goAbsolute( float dPan, float dTilt, float dZoom ){ return true; }; virtual bool goPreset( char *PresetToken ){ return true; }; virtual bool setOSD( bool nMode ){ return true; }; + virtual bool waitMoveIsDone( void ){ return false; }; }; #endif // NO_PTZ_CAMERAS diff --git a/lib/data/protocol/protocolPTZ_ONVIF.cpp b/lib/data/protocol/protocolPTZ_ONVIF.cpp index 63e1740d..9b0d8d0c 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.cpp +++ b/lib/data/protocol/protocolPTZ_ONVIF.cpp @@ -265,38 +265,43 @@ bool protocolPTZ_ONVIF::move( uint32_t MoveMatrix ){ #include + +bool protocolPTZ_ONVIF::waitMoveIsDone( void ){ + ptzStatus tVal; + uint8_t TO = 10; // 3.6 second max + SLEEP( 333 ); + while( m_pOnVif->ptz_GetStatus( m_pProfile->profiletoken, &tVal ) && TO ) { + if( tVal.hasMoveStatus ) { + if( tVal.PanTiltIdle && tVal.ZoomIdle ) break; + } else { + //if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break; + }; + SLEEP( 333 ); + TO--; + }; + if( TO ) { +#ifdef DEBUG_EXTRA + LOG("Finished Move!"); +#endif + return true; + } else LOG_ERROR("Error Fetching PTZ Status!"); + return false; +}; + bool protocolPTZ_ONVIF::goAbsolute( float dPan, float dTilt, float dZoom ){ #ifdef USE_PONVIF //if (m_bStatus == 0) return false; -#ifdef DEBUG_EXTRA - LOG("ABS Move"); -#endif + #ifdef DEBUG_EXTRA + LOG("ABS Move"); + #endif if( m_pProfile == NULL ){ LOG_ERROR("No PTZ Profile!!"); return false; }; if ( dZoom < 0.0f ) dZoom = 0.0f; - uint8_t TO = 10; // 3.6 second max + if ( m_pOnVif->ptz_AbsoluteMove( m_pProfile->profiletoken, dPan, dTilt, dZoom ) ){ -/* No Wait Status! - SLEEP( 333 ); - ptzStatus tVal; - while( m_pOnVif->ptz_GetStatus( m_pProfile->profiletoken, &tVal ) && TO ) { - if( tVal.hasMoveStatus ) { - if( tVal.PanTiltIdle && tVal.ZoomIdle ) break; - } else { - if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break; - }; - SLEEP( 333 ); - TO--; - }; - if( TO ) { -#ifdef DEBUG_EXTRA - LOG("Finished Move!"); -#endif - return true; - } else LOG_ERROR("Error Fetching PTZ Status!"); - */ + }; return true; #else diff --git a/lib/data/protocol/protocolPTZ_ONVIF.h b/lib/data/protocol/protocolPTZ_ONVIF.h index 990f4203..68d3bbc3 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.h +++ b/lib/data/protocol/protocolPTZ_ONVIF.h @@ -45,6 +45,7 @@ class protocolPTZ_ONVIF: public protocolPTZ{ bool setUsername( CSTR username ); bool setPassword( CSTR password ); bool setAuthMode( uint8_t Mode ); + virtual bool waitMoveIsDone( void ); }; diff --git a/lib/rgoose/rgooseCamera.h b/lib/rgoose/rgooseCamera.h index f33f061f..88955250 100644 --- a/lib/rgoose/rgooseCamera.h +++ b/lib/rgoose/rgooseCamera.h @@ -72,6 +72,7 @@ typedef enum E_CAM_WORKING_STATE{ CAM_WS_GO_ABS_POSITION = 12, CAM_WS_READY = 64, CAM_WS_BUSY = 128, + CAM_WS_WAIT_ONPOS = 129, CAM_WS_END = 255 }CAM_WORK_STATE;