@ -50,6 +50,10 @@ deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) :
m_sPtzHealth . state = PTZ_HEALTH_INIT ;
m_sPtzHealth . since = time ( NULL ) ;
m_tPtzLastProbe = 0 ;
m_bPendingCamData = false ;
m_bPendingProtoReinit = false ;
m_bPtzEverOk = false ;
memset ( & m_sPendingCamData , 0 , sizeof ( m_sPendingCamData ) ) ;
m_bNoAbsPositionning = false ;
m_bCameraOnHomePosition = false ;
m_bCameraAIControled = false ;
@ -88,6 +92,10 @@ deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) :
m_sPtzHealth . state = PTZ_HEALTH_INIT ;
m_sPtzHealth . since = time ( NULL ) ;
m_tPtzLastProbe = 0 ;
m_bPendingCamData = false ;
m_bPendingProtoReinit = false ;
m_bPtzEverOk = false ;
memset ( & m_sPendingCamData , 0 , sizeof ( m_sPendingCamData ) ) ;
m_bNoAbsPositionning = false ;
m_bCameraOnHomePosition = false ;
m_tTimeTSeconds = 0 ;
@ -1256,6 +1264,7 @@ void deviceNodeCamera::m_fThreadProc(void)
if ( ( bNewSecondLoop = ( cTimeSeconds ! = oTimeSeconds ) ) ) oTimeSeconds = cTimeSeconds ;
applyPendingCamData ( ) ; // settings changed in the DB that need no rebuild (validate)
uint8_t tState = getWM ( ) ;
ptzCheck ( tState ) ;
tState = getWM ( ) ;
@ -1525,7 +1534,20 @@ void deviceNodeCamera::m_fThreadProc(void)
LOG_ERROR ( " No Kernel Instance! " ) ;
} ;
m_sCamData . online = true ;
setWM ( CAM_WS_POWER_INIT ) ;
m_mPtzHealth . lock ( ) ;
bool tRecovery = m_bPtzEverOk & & ( m_sPtzHealth . state = = PTZ_HEALTH_FAILED ) ;
m_mPtzHealth . unlock ( ) ;
if ( tRecovery )
{
// The camera kept its settings: only the connection was lost - no full initialisation
// (power mode, WB, BLC, AF, home) in the middle of the work
LOG ( " PTZ protocol re-created - camera settings kept, back to ready " ) ;
setWM ( CAM_WS_READY ) ;
}
else
{
setWM ( CAM_WS_POWER_INIT ) ;
} ;
} ;
m_pDBC - > updateCamOnlineStatus ( m_sCamData . id , m_sCamData . online ) ;
if ( m_sCamData . protocol = = 0 )
@ -1536,6 +1558,7 @@ void deviceNodeCamera::m_fThreadProc(void)
{
m_tPtzLastProbe = time ( NULL ) ;
ptzHealthSet ( PTZ_HEALTH_OK ) ;
m_bPtzEverOk = true ;
}
else
{
@ -1906,6 +1929,17 @@ void deviceNodeCamera::m_fThreadProc(void)
m_fCP_Tilt = m_pCurrentROI - > cam_orientation . tilt ;
m_fCP_Zoom = m_pCurrentROI - > cam_orientation . zoom ;
// Do Movement
{
char tB [ CTEXT * 3 ] ;
snprintf ( tB , sizeof ( tB ) ,
" Go to ROI (%u targets): pan=%0.6f tilt=%0.6f zoom=%0.6f (x zoom correction %0.4f = %0.6f); target fi=%0.2f tetta=%0.2f r=%0.1f; "
" camera pan model %u / %u deg, tilt model %u / %u deg " ,
( unsigned ) m_pCurrentROI - > targets . size ( ) , m_pCurrentROI - > cam_orientation . pan , m_pCurrentROI - > cam_orientation . tilt ,
m_pCurrentROI - > cam_orientation . zoom , m_fZoomCorrection , m_pCurrentROI - > cam_orientation . zoom * m_fZoomCorrection ,
m_pCurrentROI - > cam_position . fi , m_pCurrentROI - > cam_position . tetta , m_pCurrentROI - > cam_position . r , ( unsigned ) m_sCamData . pan_model ,
( unsigned ) m_sCamData . pan_angles , ( unsigned ) m_sCamData . tilt_model , ( unsigned ) m_sCamData . tilt_angles ) ;
LOG ( tB ) ;
}
if ( ! m_pProtocol - > goAbsolute ( m_pCurrentROI - > cam_orientation . pan , m_pCurrentROI - > cam_orientation . tilt , m_pCurrentROI - > cam_orientation . zoom * m_fZoomCorrection ) )
{
ptzCommandFailed ( " goAbsolute " ) ;
@ -2880,6 +2914,113 @@ bool deviceNodeCamera::validateStream(void)
return true ;
}
/*
Which camera fields changed ( DB record a - > b ) ?
- runtime state written while running ( online , status , session , max_range ) and discovery data ( uuid , mac ) :
ignored . Before , any difference rebuilt the node : CAM_WS_INIT set m_sCamData . online = false before
re - creating the PTZ protocol and wrote the DB only afterwards , so a validate ( ) during a slow ONVIF
re - init deleted the camera ( and dropped its video stream ) ; so did the ONVIF discovery writing online = 1.
- cosmetic ( name , hardware ) : taken over , no action
- PTZ protocol only ( onvif_ctrl , onvif_access , port , auth_mode , ptz_token ) : the protocol is re - created
- anything else ( ip , credentials and stream_id used by the stream , type , geometry . . . ) : node rebuilt
*/
typedef enum E_CAM_DATA_CHANGE
{
CAM_CHANGE_NONE = 0 ,
CAM_CHANGE_COSMETIC = 1 ,
CAM_CHANGE_PROTOCOL = 2 ,
CAM_CHANGE_NODE = 3
} CAM_DATA_CHANGE ;
static CAM_DATA_CHANGE camDataChange ( const DBC_CAM_DATA * a , const DBC_CAM_DATA * b , const char * * what )
{
DBC_CAM_DATA tA , tB ;
memcpy ( & tA , a , sizeof ( tA ) ) ;
memcpy ( & tB , b , sizeof ( tB ) ) ;
* what = " " ;
// runtime / discovery fields: never a change
tA . online = tB . online ;
tA . status = tB . status ;
memcpy ( tA . session , tB . session , sizeof ( tA . session ) ) ;
tA . max_range = tB . max_range ;
memcpy ( tA . uuid , tB . uuid , sizeof ( tA . uuid ) ) ;
memcpy ( tA . mac , tB . mac , sizeof ( tA . mac ) ) ;
// PTZ protocol fields
bool tProto = ( tA . port ! = tB . port ) | | ( tA . auth_mode ! = tB . auth_mode ) | | strncmp ( tA . onvif_ctrl , tB . onvif_ctrl , sizeof ( tA . onvif_ctrl ) ) ! = 0 | |
strncmp ( tA . onvif_access , tB . onvif_access , sizeof ( tA . onvif_access ) ) ! = 0 | | strncmp ( tA . ptz_token , tB . ptz_token , sizeof ( tA . ptz_token ) ) ! = 0 ;
tA . port = tB . port ;
tA . auth_mode = tB . auth_mode ;
memcpy ( tA . onvif_ctrl , tB . onvif_ctrl , sizeof ( tA . onvif_ctrl ) ) ;
memcpy ( tA . onvif_access , tB . onvif_access , sizeof ( tA . onvif_access ) ) ;
memcpy ( tA . ptz_token , tB . ptz_token , sizeof ( tA . ptz_token ) ) ;
// cosmetic fields
bool tCosm = strncmp ( tA . name , tB . name , sizeof ( tA . name ) ) ! = 0 | | strncmp ( tA . hardware , tB . hardware , sizeof ( tA . hardware ) ) ! = 0 ;
memcpy ( tA . name , tB . name , sizeof ( tA . name ) ) ;
memcpy ( tA . hardware , tB . hardware , sizeof ( tA . hardware ) ) ;
// everything else (also fields added later) needs the node rebuilt
if ( memcmp ( & tA , & tB , sizeof ( tA ) ) ! = 0 )
{
if ( strncmp ( a - > ip , b - > ip , sizeof ( a - > ip ) ) ! = 0 )
{
* what = " ip " ;
}
else if ( strncmp ( a - > username , b - > username , sizeof ( a - > username ) ) ! = 0 | | strncmp ( a - > password , b - > password , sizeof ( a - > password ) ) ! = 0 )
{
* what = " credentials " ;
}
else if ( a - > stream_id ! = b - > stream_id )
{
* what = " stream " ;
}
else if ( a - > protocol ! = b - > protocol )
{
* what = " control protocol " ;
}
else
{
* what = " camera parameters " ;
} ;
return CAM_CHANGE_NODE ;
} ;
if ( tProto )
{
* what = " PTZ access " ;
return CAM_CHANGE_PROTOCOL ;
} ;
return tCosm ? CAM_CHANGE_COSMETIC : CAM_CHANGE_NONE ;
}
// Camera thread: take over the fields validate() found changed (only cosmetic / PTZ protocol fields)
void deviceNodeCamera : : applyPendingCamData ( void )
{
if ( ! m_bPendingCamData )
{
return ;
} ;
m_mPendingCamData . lock ( ) ;
DBC_CAM_DATA * tN = & m_sPendingCamData ;
memcpy ( m_sCamData . name , tN - > name , sizeof ( m_sCamData . name ) ) ;
memcpy ( m_sCamData . hardware , tN - > hardware , sizeof ( m_sCamData . hardware ) ) ;
m_sCamData . port = tN - > port ;
m_sCamData . auth_mode = tN - > auth_mode ;
memcpy ( m_sCamData . onvif_ctrl , tN - > onvif_ctrl , sizeof ( m_sCamData . onvif_ctrl ) ) ;
memcpy ( m_sCamData . onvif_access , tN - > onvif_access , sizeof ( m_sCamData . onvif_access ) ) ;
memcpy ( m_sCamData . ptz_token , tN - > ptz_token , sizeof ( m_sCamData . ptz_token ) ) ;
bool tReinit = m_bPendingProtoReinit ;
m_bPendingCamData = false ;
m_bPendingProtoReinit = false ;
m_mPendingCamData . unlock ( ) ;
if ( tReinit & & m_sCamData . protocol ! = 0 )
{
LOG ( " PTZ access settings changed - re-initialising the PTZ protocol (camera and stream kept) " ) ;
m_mPtzHealth . lock ( ) ;
m_sPtzHealth . next_retry = 0 ; // new settings: no back-off
m_sPtzHealth . failed_inits = 0 ;
m_mPtzHealth . unlock ( ) ;
setWM ( CAM_WS_INIT ) ;
} ;
}
bool deviceNodeCamera : : validate ( )
{
if ( m_pDBC = = NULL )
@ -2889,16 +3030,46 @@ bool deviceNodeCamera::validate()
DBC_CAM_DATA * tDATA = m_pDBC - > getCamData ( m_sCamData . id ) ;
if ( tDATA = = NULL )
{
// Probably a failed read (SQLite busy), not a deleted camera: rebuild only when it stays missing
if ( + + m_nNodeDataMisses < NODE_DATA_MISSES_TO_REBUILD )
{
return true ;
} ;
LOG_ERROR ( " Camera record not found - rebuilding the camera " ) ;
return false ;
}
if ( memcmp ( tDATA , & m_sCamData , sizeof ( DBC_CAM_DATA ) ) ! = 0 )
const char * tWhat = " " ;
CAM_DATA_CHANGE tChange = camDataChange ( & m_sCamData , tDATA , & tWhat ) ;
if ( tChange = = CAM_CHANGE_NODE )
{
// Settings Changed
// memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
char tB [ 128 ] ;
snprintf ( tB , sizeof ( tB ) , " Camera settings changed (%s) - rebuilding the camera " , tWhat ) ;
LOG ( tB ) ;
free ( tDATA ) ;
return false ;
} ;
if ( tChange ! = CAM_CHANGE_NONE )
{
m_mPendingCamData . lock ( ) ;
memcpy ( & m_sPendingCamData , tDATA , sizeof ( DBC_CAM_DATA ) ) ;
m_bPendingProtoReinit = m_bPendingProtoReinit | | ( tChange = = CAM_CHANGE_PROTOCOL ) ;
m_bPendingCamData = true ;
m_mPendingCamData . unlock ( ) ;
} ;
free ( tDATA ) ;
// A major PTZ problem (the protocol worked, then CAM_REBUILD_AFTER_FAILED_INITS re-creations failed in a
// row: power supply / network) rebuilds the camera - the new node fully initialises it
m_mPtzHealth . lock ( ) ;
bool tGiveUp = m_bPtzEverOk & & ( m_sPtzHealth . state = = PTZ_HEALTH_FAILED ) & & ( m_sPtzHealth . failed_inits > = CAM_REBUILD_AFTER_FAILED_INITS ) ;
uint32_t tFails = m_sPtzHealth . failed_inits ;
m_mPtzHealth . unlock ( ) ;
if ( tGiveUp )
{
char tB [ 160 ] ;
snprintf ( tB , sizeof ( tB ) , " PTZ protocol re-creation failed %u times in a row (camera power / network?) - rebuilding the camera " , tFails ) ;
LOG_ERROR ( tB ) ;
return false ;
} ;
if ( ! validateStream ( ) )
{
return false ;