@ -82,6 +82,8 @@ pOnvif::pOnvif()
m_pHttpAccessManager = NULL ;
memset ( & m_sCapabilities , 0 , sizeof ( nodeCapability ) ) ;
m_bAuthMode = 0 ;
parser_done = false ;
m_pPendingRequest = NULL ;
LOG ( " Constructor Done! " ) ;
}
@ -1854,6 +1856,37 @@ CSTR pOnvif::media_GetSnapshotUri(CSTR profileToken)
return NULL ;
}
/*
Parses a tt : MoveStatus axis value . Returns 1 = MOVING , 0 = IDLE , - 1 = UNKNOWN / not reported .
*/
static int parseMoveStatusValue ( XMLNode & node )
{
if ( node . isEmpty ( ) )
{
return - 1 ;
}
LPCTSTR tText = node . getText ( 0 ) ;
if ( tText = = NULL )
{
return - 1 ;
}
if ( strstr ( tText , " MOVING " ) ! = NULL )
{
return 1 ;
}
if ( strstr ( tText , " IDLE " ) ! = NULL )
{
return 0 ;
}
return - 1 ;
}
/*
ONVIF PTZ GetStatus .
Position and MoveStatus are both optional in tt : PTZStatus , and inside them every axis is optional ,
so every element is checked before it is read ; tStatus - > has * tells the caller what the camera reported .
An axis whose MoveStatus is not reported is counted as idle .
*/
bool pOnvif : : ptz_GetStatus ( CSTR profileToken , ptzStatus * tStatus )
{
if ( m_pPTZURI = = NULL )
@ -1869,80 +1902,132 @@ bool pOnvif::ptz_GetStatus(CSTR profileToken, ptzStatus * tStatus)
tTOKEN = _makeToken ( ) ;
memset ( tStatus , 0 , sizeof ( ptzStatus ) ) ;
sprintf ( tBuffer , PONVIF_PACK_GET_PTZ_STATUS , tTOKEN . USERNAME , tTOKEN . PASSDIGEST , tTOKEN . NONCE , tTOKEN . TIMESTAMP , profileToken ) ;
if ( _send_request ( m_pPTZURI , tBuffer , true ) )
if ( ! _send_request ( m_pPTZURI , tBuffer , true ) )
{
XMLResults tParseResults ;
XMLNode response = XMLNode : : parseString ( m_pLastResponse , " root " , & tParseResults ) ;
if ( tParseResults . error )
{
char tBuf [ CTEXT ] ;
sprintf ( tBuf , " XML Parse Error %d at %d:%d " , tParseResults . error , tParseResults . nLine , tParseResults . nColumn ) ;
LOG_ERROR ( tBuf ) ;
}
else
return false ;
}
XMLResults tParseResults ;
XMLNode response = XMLNode : : parseString ( m_pLastResponse , " root " , & tParseResults ) ;
if ( tParseResults . error )
{
char tBuf [ CTEXT ] ;
sprintf ( tBuf , " XML Parse Error %d at %d:%d " , tParseResults . error , tParseResults . nLine , tParseResults . nColumn ) ;
LOG_ERROR ( tBuf ) ;
return false ;
}
XMLNode aNode = response . getChildNode ( " Body " , 0 ) ;
if ( aNode . isEmpty ( ) )
{
return false ;
}
XMLNode bNode = aNode . getChildNode ( " GetStatusResponse " , 0 ) ;
if ( bNode . isEmpty ( ) )
{
// SOAP Fault or unexpected answer
return false ;
}
XMLNode cNode = bNode . getChildNode ( " PTZStatus " , 0 ) ;
if ( cNode . isEmpty ( ) )
{
return false ;
}
XMLNode dNode = cNode . getChildNode ( " Position " , 0 ) ;
if ( ! dNode . isEmpty ( ) )
{
XMLNode eNode = dNode . getChildNode ( " PanTilt " , 0 ) ;
if ( ! eNode . isEmpty ( ) )
{
XMLNode aNode = response . getChildNode ( " Body " , 0 ) ;
if ( aNode . isEmpty ( ) )
LPCTSTR tX = eNode . getAttribute ( " x " ) ;
LPCTSTR tY = eNode . getAttribute ( " y " ) ;
if ( tX ! = NULL & & tY ! = NULL )
{
return NULL ;
}
XMLNode bNode = aNode . getChildNode ( " GetStatusResponse " , 0 ) ;
if ( ! bNode . isEmpty ( ) )
tStatus - > pan = ( float ) atof ( tX ) ;
tStatus - > tilt = ( float ) atof ( tY ) ;
tStatus - > hasPosition = true ;
} ;
} ;
XMLNode fNode = dNode . getChildNode ( " Zoom " , 0 ) ;
if ( ! fNode . isEmpty ( ) )
{
LPCTSTR tX = fNode . getAttribute ( " x " ) ;
if ( tX ! = NULL )
{
XMLNode cNode = bNode . getChildNode ( " PTZStatus " , 0 ) ;
if ( ! cNode . isEmpty ( ) )
{
XMLNode dNode = cNode . getChildNode ( " Position " , 0 ) ;
if ( ! dNode . isEmpty ( ) )
{
XMLNode eNode = dNode . getChildNode ( " PanTilt " , 0 ) ;
if ( ! eNode . isEmpty ( ) )
{
// XMLAttribute ZZ = eNode.getAttribute(1);
// LOG( ZZ.lpszName );
tStatus - > pan = atof ( eNode . getAttribute ( " x " ) ) ; // ZZ.lpszValue );
// ZZ = eNode.getAttribute(2);
// LOG( ZZ.lpszName );
tStatus - > tilt = atof ( eNode . getAttribute ( " y " ) ) ; // ZZ.lpszValue );
} ;
XMLNode fNode = dNode . getChildNode ( " Zoom " , 0 ) ;
if ( ! fNode . isEmpty ( ) )
{
// XMLAttribute ZZ = eNode.getAttribute(1);
// LOG( ZZ.lpszName );
tStatus - > zoom = atof ( fNode . getAttribute ( " x " ) ) ; // ZZ.lpszValue );
} ;
} ;
if ( tStatus - > pan ! = .0f | | tStatus - > tilt ! = .0f | | tStatus - > zoom ! = .0f )
{
XMLNode gNode = cNode . getChildNode ( " MoveStatus " , 0 ) ;
if ( ! gNode . isEmpty ( ) )
{
tStatus - > hasMoveStatus = true ;
XMLNode hNode = gNode . getChildNode ( " PanTilt " , 0 ) ;
if ( ! hNode . isEmpty ( ) )
{
if ( strstr ( hNode . getText ( 0 ) , " IDLE " ) ! = NULL )
{
tStatus - > PanTiltIdle = true ;
}
} ;
XMLNode iNode = gNode . getChildNode ( " Zoom " , 0 ) ;
if ( ! iNode . isEmpty ( ) )
{
if ( strstr ( iNode . getText ( 0 ) , " IDLE " ) ! = NULL )
{
tStatus - > ZoomIdle = true ;
}
} ;
} ;
}
return true ;
} ;
tStatus - > zoom = ( float ) atof ( tX ) ;
tStatus - > hasZoomPosition = true ;
} ;
} ;
} ;
return false ;
XMLNode gNode = cNode . getChildNode ( " MoveStatus " , 0 ) ;
if ( ! gNode . isEmpty ( ) )
{
XMLNode hNode = gNode . getChildNode ( " PanTilt " , 0 ) ;
XMLNode iNode = gNode . getChildNode ( " Zoom " , 0 ) ;
int tPT = parseMoveStatusValue ( hNode ) ;
int tZM = parseMoveStatusValue ( iNode ) ;
// Only a real IDLE / MOVING value counts as "reported"
tStatus - > hasMoveStatus = ( tPT > = 0 | | tZM > = 0 ) ;
tStatus - > PanTiltIdle = ( tPT ! = 1 ) ;
tStatus - > ZoomIdle = ( tZM ! = 1 ) ;
tStatus - > isMoving = ( tPT = = 1 | | tZM = = 1 ) ;
} ;
return true ;
}
bool pOnvif : : ptz_GetServiceCapabilities ( int8_t * moveStatus , int8_t * statusPosition )
{
if ( moveStatus )
{
* moveStatus = - 1 ;
}
if ( statusPosition )
{
* statusPosition = - 1 ;
}
if ( m_pPTZURI = = NULL )
{
return false ;
}
char tBuffer [ DEFAULT_BUFFER_SIZE ] ;
accessToken tTOKEN ;
tTOKEN = _makeToken ( ) ;
sprintf ( tBuffer , PONVIF_PACK_PTZ_GET_SERVICE_CAPS , tTOKEN . USERNAME , tTOKEN . PASSDIGEST , tTOKEN . NONCE , tTOKEN . TIMESTAMP ) ;
if ( ! _send_request ( m_pPTZURI , tBuffer , true ) )
{
return false ;
}
XMLResults tParseResults ;
XMLNode response = XMLNode : : parseString ( m_pLastResponse , " root " , & tParseResults ) ;
if ( tParseResults . error )
{
return false ;
}
XMLNode aNode = response . getChildNode ( " Body " , 0 ) ;
if ( aNode . isEmpty ( ) )
{
return false ;
}
XMLNode bNode = aNode . getChildNode ( " GetServiceCapabilitiesResponse " , 0 ) ;
if ( bNode . isEmpty ( ) )
{
// Older devices (PTZ service < 2.x) answer with a SOAP Fault
return false ;
}
XMLNode cNode = bNode . getChildNode ( " Capabilities " , 0 ) ;
if ( cNode . isEmpty ( ) )
{
return false ;
}
LPCTSTR tMS = cNode . getAttribute ( " MoveStatus " ) ;
LPCTSTR tSP = cNode . getAttribute ( " StatusPosition " ) ;
if ( moveStatus & & tMS ! = NULL )
{
* moveStatus = checkBool ( tMS ) ? 1 : 0 ;
}
if ( statusPosition & & tSP ! = NULL )
{
* statusPosition = checkBool ( tSP ) ? 1 : 0 ;
}
return true ;
}
bool pOnvif : : ptz_GetPresets ( CSTR profileToken )
@ -2312,6 +2397,14 @@ void * pOnvif::parser(void * obj, void * ptr)
if ( ME )
{
ME - > m_mParserLock . lock ( ) ;
if ( RQ ! = ME - > m_pPendingRequest )
{
// Late reply to a request that already timed out: ignore it, so it cannot be
// taken as the answer to the request that is waiting now.
ME - > m_mParserLock . unlock ( ) ;
return NULL ;
} ;
if ( ME - > m_pLastResponse )
{
free ( ME - > m_pLastResponse ) ;
@ -2331,6 +2424,7 @@ void * pOnvif::parser(void * obj, void * ptr)
# endif
} ;
ME - > parser_done = true ;
ME - > m_mParserLock . unlock ( ) ;
} ;
return NULL ;
}
@ -2361,16 +2455,29 @@ bool pOnvif::_send_request(CSTR url, CSTR post_string, bool mustReturnData)
if ( mustReturnData )
{
tRC - > setHandler ( ( void * ) this , pOnvif : : parser , ( void * ) tRC ) ;
// Arm before the request is queued: the reply can arrive before accomplish() returns.
m_mParserLock . lock ( ) ;
parser_done = false ;
m_pPendingRequest = ( void * ) tRC ;
m_mParserLock . unlock ( ) ;
if ( m_pHttpAccessManager - > accomplish ( tRC ) )
{
parser_done = false ;
uint8_t cnt = 200 ; // TO = 2 second
while ( ! RetVal & & cnt - - )
{
m_mParserLock . lock ( ) ;
RetVal = parser_done ;
SLEEP ( 10 ) ;
m_mParserLock . unlock ( ) ;
if ( ! RetVal )
{
SLEEP ( 10 ) ;
}
} ;
} ;
// From here on any reply to tRC is stale (tRC may already be deleted by the manager)
m_mParserLock . lock ( ) ;
m_pPendingRequest = NULL ;
m_mParserLock . unlock ( ) ;
}
else
{