You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1605 lines
59 KiB

​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeCamera.h"
  2. #include "../../components/base/basicCameraInfo.h"
  3. #include "../../components/object/objectSystemAnchor.h"
  4. #include "../../hardware/uart/serialPort.h"
  5. #include "../../hardware/uart/serialSOL.h"
  6. #include "../../data/protocol/protocolPTZ_VISCA.h"
  7. #include "../../data/protocol/protocolPTZ_ONVIF.h"
  8. #include "../../data/protocol/protocolPTZ_PELCO.h"
  9. #include "hardwareManager.h"
  10. #include "hardwareResourceSerialProvider.h"
  11. #include "kernelModuleEmbedVideo.h"
  12. #include "../mojo/mojoKernel.h"
  13. #define LOG( X ) deviceNode::m_pLog->Log( "RCAM", this, X )
  14. #ifdef DEBUG
  15. #define LOG_ERROR( X ) {\
  16. char _tBfrErrMSG2w34[256];\
  17. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  18. deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  19. }
  20. #else
  21. #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, X );
  22. #endif
  23. // all sizes in mm
  24. 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 };
  25. 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 };
  26. 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 };
  27. #define CAM_FOCAL_LENGTH_CNT 49
  28. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={
  29. 2.7f,
  30. 3.0f,
  31. 3.2f,
  32. 3.5f,
  33. 3.7f,
  34. 3.9f,
  35. 4.0f,
  36. 4.3f,
  37. 4.8f,
  38. 5.0f,
  39. 5.1f,
  40. 5.5f,
  41. 5.8f,
  42. 6.0f,
  43. 6.2f,
  44. 6.6f,
  45. 7.0f,
  46. 7.1f,
  47. 7.5f,
  48. 7.6f,
  49. 8.0f,
  50. 8.6f,
  51. 8.9f,
  52. 9.0f,
  53. 9.2f,
  54. 9.3f,
  55. 10.0f,
  56. 10.5f,
  57. 10.6f,
  58. 10.7f,
  59. 11.0f,
  60. 11.3f,
  61. 12.0f,
  62. 12.9f,
  63. 13.0f,
  64. 13.5f,
  65. 13.6f,
  66. 14.0f,
  67. 14.6f,
  68. 15.0f,
  69. 15.1f,
  70. 15.5f,
  71. 16.0f,
  72. 17.0f,
  73. 17.5f,
  74. 17.7f,
  75. 18.0f,
  76. 18.2f,
  77. 19.0f
  78. };
  79. deviceNodeCamera::deviceNodeCamera( uint32_t NodeID ): deviceNode( NodeID ), basicThread(){
  80. //m_pLog = basicLogCollection::getInstance();
  81. m_pBEH = basicEventHeap::getInstance();
  82. m_bSkeepPositioningWait = true;
  83. m_tTimeTSeconds = 0;
  84. m_pProtocol = NULL;
  85. m_pTransport = NULL;
  86. m_pCurrentROI = NULL;
  87. m_nRealFrameWidth = 640;
  88. //m_bState = 0;
  89. //m_bBusy = true;
  90. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  91. m_nRelativeMoveCounter = 0;
  92. m_bRelativeMoveMatrix = 0;
  93. m_fSpeedRise = 0.03f;
  94. m_fZoomCorrection = 1.0f;
  95. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  96. //m_bSourceConnected = false;
  97. //m_pSource = NULL;
  98. };
  99. deviceNodeCamera::deviceNodeCamera( DBC_NODE_DATA *Data ): deviceNode( Data ), basicThread(){
  100. //m_pLog = basicLogCollection::getInstance();
  101. m_pBEH = basicEventHeap::getInstance();
  102. m_bSkeepPositioningWait = true;
  103. m_tTimeTSeconds = 0;
  104. m_pProtocol = NULL;
  105. m_pTransport = NULL;
  106. m_pCurrentROI = NULL;
  107. m_nRealFrameWidth = 640;
  108. //m_bState = 0;
  109. //m_bBusy = true;
  110. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  111. m_nRelativeMoveCounter = 0;
  112. m_bRelativeMoveMatrix = 0;
  113. m_fSpeedRise = 0.03f;
  114. m_fZoomCorrection = 1.0f;
  115. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  116. //m_bSourceConnected = false;
  117. //m_pSource = NULL;
  118. };
  119. deviceNodeCamera::~deviceNodeCamera(){
  120. LOG("Destruct");
  121. //m_mQPROTECTOR.unlock();
  122. waitThreadEnd( true );
  123. LOG("Thread Del OK");
  124. if( m_pTransport!= NULL ) {
  125. if( m_sCamData.uart_id ) {
  126. DBC_UART_DATA *tData = NULL;
  127. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  128. switch( tData->type ) {
  129. case UART_TYPE_HARDWARE:{
  130. delete((serialPort*)m_pTransport);
  131. break;
  132. };
  133. case UART_TYPE_SOL:{
  134. delete((serialSOL*)m_pTransport);
  135. break;
  136. };
  137. };
  138. }
  139. };
  140. LOG("Transport Del OK");
  141. if( m_pProtocol != NULL ) {
  142. switch( m_sCamData.protocol ) {
  143. case CAM_CTRL_PROTO_VISCA:
  144. delete( (protocolPTZ_VISCA *) m_pProtocol );
  145. break;
  146. case CAM_CTRL_PROTO_PELCO:
  147. delete( (protocolPTZ_PELCO *) m_pProtocol );
  148. break;
  149. case CAM_CTRL_PROTO_ONVIF:
  150. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  151. break;
  152. default:
  153. delete( m_pProtocol );
  154. };
  155. };
  156. LOG("Proto Del OK");
  157. //if( m_pPosition != NULL ) delete( m_pPosition );
  158. //LOG("Pos Del OK");
  159. //if( m_bSourceConnected ){
  160. // if( m_pSource != NULL ){
  161. // m_pSource->_close();
  162. // delete( m_pSource );
  163. // };
  164. //};
  165. //LOG("Stream Del OK");
  166. };
  167. void deviceNodeCamera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){
  168. #ifdef DEBUG
  169. char tB[128];
  170. LOG("Set Optical Parameters");
  171. sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth );
  172. LOG(tB);
  173. #endif
  174. if( frame_width > 0 && frame_heigth > 0 ) {
  175. m_nRealFrameWidth = frame_width;
  176. m_nRealFrameHeight = frame_heigth;
  177. float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth );
  178. switch( m_sCamData.zoom_model ) {
  179. case 0:{
  180. #ifdef DEBUG
  181. LOG("\tFixed Zoom");
  182. #endif
  183. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  184. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  185. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  186. m_fR2FL = .0f;
  187. m_fZoomCorrection = .0f;
  188. m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange );
  189. break;
  190. };
  191. case 1:{
  192. #ifdef DEBUG
  193. LOG("\tAutomated Zoom");
  194. #endif
  195. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  196. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  197. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  198. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  199. if( m_fMaxRange > m_fMinRange ){
  200. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  201. } else {
  202. m_fR2FL = .0f;
  203. };
  204. m_fZoomCorrection = 1.0f;
  205. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  206. break;
  207. };
  208. case 2:{
  209. #ifdef DEBUG
  210. LOG("\tManual Zoom");
  211. #endif
  212. m_fMinRange = 5.0f; //Minimal 5 meters
  213. if ( m_sCamData.max_range < m_fMinRange ) {
  214. m_fMaxRange = m_fMinRange;
  215. } else {
  216. m_fMaxRange = m_sCamData.max_range;
  217. };
  218. m_fZoomCorrection = m_sCamData.zoom * 0.01f;
  219. m_fMinFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag );
  220. m_fMaxFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag );
  221. break;
  222. };
  223. };
  224. m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL ));
  225. m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL ));
  226. m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL ));
  227. m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL ));
  228. #ifdef DEBUG
  229. sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL );
  230. LOG(tB);
  231. sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange );
  232. LOG(tB);
  233. sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov );
  234. LOG(tB);
  235. sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov );
  236. LOG(tB);
  237. #endif
  238. };
  239. };
  240. bool deviceNodeCamera::create(){
  241. setWM( CAM_WS_INIT );
  242. if( m_sNodeData.aux_id == 0 ) return false;
  243. DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id );
  244. if( tData != NULL ) {
  245. memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) );
  246. free( tData );
  247. } else {
  248. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  249. return false;
  250. };
  251. if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  252. updateOpticalParameters( 1280, 720 ); //HD Ready
  253. if( m_sCamData.ptz_speed_step > .0f ) {
  254. m_fSpeedRise = m_sCamData.ptz_speed_step;
  255. };
  256. //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  257. //if( m_pPosition == NULL ) {
  258. // LOG_ERROR("Position Constructor Failure!");
  259. // return false;
  260. //};
  261. //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  262. //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  263. systemAnchor * SA = systemAnchor::getInstance();
  264. if( SA ){
  265. LOG("SysAnchor OK");
  266. if ( SA->beLocal() ) {
  267. LOG("SysAnchor beLocal!");
  268. m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 ); //SA->getUTMZone() );
  269. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  270. } else {
  271. LOG("SysAnchor is Global Position System!");
  272. C3D_DECART LD = SA->getXYZ();
  273. m_oPosition.setPositionXYZ( LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone() );
  274. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  275. };
  276. } else {
  277. LOG("No SysAnchor!");
  278. m_oPosition.setPositionXYZ( m_sCamData.x , m_sCamData.y, m_sCamData.z );
  279. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  280. };
  281. m_oPosition.updateMatrix();
  282. #ifdef DEBUG
  283. char tB[128];
  284. sprintf( tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  285. LOG( tB );
  286. sprintf( tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  287. LOG( tB );
  288. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  289. LOG( tB );
  290. #endif
  291. if( m_pProtocol != NULL ){
  292. LOG_ERROR("Cannot Start: Service Already Running!");
  293. return false;
  294. };
  295. switch( m_sCamData.protocol ){
  296. case CAM_CTRL_PROTO_VISCA: {
  297. #ifdef DEBUG
  298. LOG("VISCA");
  299. #endif
  300. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  301. if( m_pProtocol!= NULL ) {
  302. basicTransport *tTransport = NULL;
  303. hardwareManager *tHM = hardwareManager::getInstance();
  304. if( tHM ){
  305. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  306. if( tRES != NULL ) {
  307. if( tRES->isValid()) {
  308. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  309. if( m_pProtocol->setTransport( tTransport )){
  310. if( !m_pProtocol->create() ){
  311. LOG_ERROR("Protocol <<< Create >>> Failure!");
  312. };
  313. } else {
  314. LOG_ERROR("Binding Port Failure!");
  315. };
  316. };
  317. #ifdef DEBUG
  318. } else {
  319. LOG("HWM Has No UART PORT!");
  320. #endif
  321. };
  322. #ifdef DEBUG
  323. } else {
  324. LOG_ERROR("Can not get Hardware Manager!");
  325. #endif
  326. };
  327. if( tTransport!= NULL ) {
  328. LOG( "Bind Transport Port!" );
  329. } else {
  330. LOG_ERROR("Transport Creation Failure!");
  331. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  332. m_pProtocol = NULL;
  333. };
  334. #ifdef DEBUG
  335. } else {
  336. LOG_ERROR("Protocol Creation failure!");
  337. #endif
  338. };
  339. break;
  340. };
  341. case CAM_CTRL_PROTO_PELCO:{
  342. #ifdef DEBUG
  343. LOG("PELCO");
  344. #endif
  345. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  346. if( m_pProtocol!= NULL ) {
  347. basicTransport *tTransport = NULL;
  348. hardwareManager *tHM = hardwareManager::getInstance();
  349. if ( tHM ) {
  350. hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  351. if( tRES != NULL ) {
  352. if( tRES->isValid()) {
  353. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  354. if( m_pProtocol->setTransport( tTransport )){
  355. if( !m_pProtocol->create() ){
  356. LOG_ERROR("Protocol <<< Create >>> Failure!");
  357. };
  358. } else {
  359. LOG_ERROR("Binding Port Failure!");
  360. };
  361. };
  362. #ifdef DEBUG
  363. } else {
  364. LOG("HWM Has No UART PORT!");
  365. #endif
  366. };
  367. #ifdef DEBUG
  368. } else {
  369. LOG_ERROR("Can not get Hardware Manager!");
  370. #endif
  371. };
  372. if( tTransport!= NULL ) {
  373. LOG( "Bind Transport Port!" );
  374. } else {
  375. LOG_ERROR("Transport Creation Failure!");
  376. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  377. m_pProtocol = NULL;
  378. };
  379. #ifdef DEBUG
  380. } else {
  381. LOG_ERROR("Protocol Creation failure!");
  382. #endif
  383. };
  384. break;
  385. };
  386. case CAM_CTRL_PROTO_ONVIF:{
  387. #ifdef DEBUG
  388. LOG("ONVIF");
  389. #endif
  390. //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  391. if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) {
  392. char tURI[256];
  393. if ( m_sCamData.port ) {
  394. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  395. } else {
  396. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  397. };
  398. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  399. } else {
  400. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  401. };
  402. #ifdef DEBUG
  403. LOG("ONVIF-END");
  404. #endif
  405. if( m_pProtocol != NULL ) {
  406. if( !m_pProtocol->create() ) {
  407. LOG_ERROR("Error Creating Proto!");
  408. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  409. m_pProtocol = NULL;
  410. };
  411. #ifdef DEBUG
  412. LOG("PROTO CREATE DONE");
  413. #endif
  414. };
  415. break;
  416. };
  417. default:{
  418. #ifdef DEBUG
  419. LOG("Camera Has no defined control protocol");
  420. #endif
  421. };
  422. };
  423. if( m_pProtocol == NULL ) {
  424. if( m_sCamData.protocol == 0 ) {
  425. m_sCamData.online = true;
  426. } else LOG_ERROR("No Proto Created!");
  427. } else {
  428. #ifdef DEBUG
  429. LOG("Proto is OK");
  430. #endif
  431. mojoKernel *tKRNL = mojoKernel::getInstance();
  432. if( tKRNL ) {
  433. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  434. #ifdef DEBUG
  435. char ttTT[128];
  436. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  437. LOG( ttTT );
  438. if ( m_bSkeepPositioningWait ) {
  439. LOG("Camera No Wait On Pos!");
  440. } else {
  441. LOG("Camera Will Wait On Pos!");
  442. };
  443. #endif
  444. } else {
  445. LOG_ERROR("No Kernel Instance!");
  446. };
  447. m_sCamData.online = init();
  448. };
  449. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  450. return m_sCamData.online;
  451. };
  452. //void deviceNodeCamera::setBusy( bool Status ){
  453. //m_bBusy = Status;
  454. //};
  455. bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  456. if( m_sCamData.online ) {
  457. uint32_t MoveMatrix = 0;
  458. if( PanSPD != .0f ){
  459. if( PanSPD > 0 ) {
  460. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  461. } else {
  462. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  463. };
  464. };
  465. if( TiltSPD != .0f ){
  466. if( TiltSPD > 0 ) {
  467. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  468. } else {
  469. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  470. };
  471. };
  472. if( ZoomSPD != .0f ){
  473. if( ZoomSPD > 0 ) {
  474. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  475. } else {
  476. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  477. };
  478. };
  479. if( MoveMatrix != 0 ) {
  480. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  481. };
  482. return m_pProtocol->move( MoveMatrix );
  483. };
  484. return false;
  485. };
  486. bool deviceNodeCamera::relativeStop(){
  487. if( m_sCamData.online ) {
  488. return m_pProtocol->move( 0 );
  489. };
  490. return false;
  491. };
  492. CAM_TASK *deviceNodeCamera::fetchCamTask( void ){
  493. CAM_TASK *rVal = NULL;
  494. m_mQPROTECTOR.lock();
  495. if (m_qCAM_TASK.size()) {
  496. rVal = m_qCAM_TASK.front();
  497. m_qCAM_TASK.pop();
  498. };
  499. m_mQPROTECTOR.unlock();
  500. return rVal;
  501. };
  502. bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){
  503. if ( ptr == NULL ) return false;
  504. m_mQPROTECTOR.lock();
  505. m_qCAM_TASK.push( ptr );
  506. m_mQPROTECTOR.unlock();
  507. return true;
  508. };
  509. bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  510. if( ptr == NULL ) return false;
  511. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  512. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  513. #ifdef DEBUG
  514. char tB[128];
  515. 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 );
  516. LOG(tB);
  517. #endif
  518. if( tFI > ptr->h_fov ) return false;
  519. if( tTETTA > ptr->v_fov ) return false;
  520. if( tp.r < ptr->min_range ) return false;
  521. if( tp.r > ptr->max_range ) return false;
  522. return true;
  523. };
  524. bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  525. if( db_id == 0 ) return false;
  526. CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD));
  527. if( tTrackRecord == NULL ) return false;
  528. #ifdef DEBUG_EXTRA
  529. LOG("Fusion: Consuming new Data!");
  530. #endif
  531. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  532. tTrackRecord->target_direction.fi = tOR.fi;
  533. tTrackRecord->target_direction.r = tOR.r;
  534. tTrackRecord->target_direction.tetta = tOR.tetta;
  535. tTrackRecord->evt_db_ID = db_id;
  536. tTrackRecord->ts_ms = getMilliseconds();
  537. m_mROIsProtector.lock();
  538. if( m_vROIS.size() ){
  539. vCAM_ROIS::iterator idx = m_vROIS.begin();
  540. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  541. if ( idx != m_vROIS.end() ) {
  542. // have Match!
  543. #ifdef DEBUG_EXTRA
  544. LOG("ROI MATCHED!");
  545. #endif
  546. (*idx)->targets.push_back( tTrackRecord );
  547. fusion_validateROI( tTrackRecord->ts_ms, (*idx) );
  548. } else {
  549. #ifdef DEBUG_EXTRA
  550. LOG("NO MATCH FOUND!");
  551. #endif
  552. // No mathced ROI -> create new one
  553. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  554. if ( tROI ) {
  555. memset( tROI, 0 , sizeof(CAM_ROI) );
  556. tROI->ts_ms = tTrackRecord->ts_ms;
  557. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  558. tROI->cam_position.fi = tOR.fi;
  559. tROI->cam_position.tetta = tOR.tetta;
  560. tROI->cam_position.r = tOR.r;
  561. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  562. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  563. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  564. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  565. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  566. tROI->targets.push_back( tTrackRecord );
  567. m_vROIS.push_back( tROI );
  568. };
  569. };
  570. } else {
  571. #ifdef DEBUG_EXTRA
  572. LOG("Fusion: NO ROI -> CREATE!");
  573. #endif
  574. //
  575. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  576. if ( tROI ) {
  577. memset( tROI, 0 , sizeof(CAM_ROI) );
  578. tROI->ts_ms = tTrackRecord->ts_ms;
  579. //Set vector Cam Coordinates
  580. tROI->cam_position.fi = tOR.fi;
  581. tROI->cam_position.tetta = tOR.tetta;
  582. tROI->cam_position.r = tOR.r;
  583. // Set vector OnVIF coordinates
  584. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  585. // make fusion data BOX
  586. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  587. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  588. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  589. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  590. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  591. // Add ROI to vector
  592. tROI->targets.push_back( tTrackRecord );
  593. m_vROIS.push_back( tROI );
  594. } else LOG_ERROR("No MEM4ROI");
  595. };
  596. m_mROIsProtector.unlock();
  597. return true;
  598. };
  599. bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  600. if( ptr == NULL ) return false;
  601. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  602. #ifdef DEBUG_EXTRA
  603. char tB[128];
  604. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  605. LOG( tB );
  606. #endif
  607. if( ptr->targets.size() ) {
  608. double tA = 0.0;
  609. double tB = 0.0;
  610. double tC = 0.0;
  611. uint32_t tCNT = 0;
  612. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  613. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  614. free( (*idx) );
  615. ptr->targets.erase( idx );
  616. } else {
  617. tA = tA + (*idx)->target_direction.fi;
  618. tB = tB + (*idx)->target_direction.tetta;
  619. tC = tC + (*idx)->target_direction.r;
  620. tCNT++;
  621. };
  622. };
  623. if( tCNT ) {
  624. C3D_SPH_DEGREES L = { 0 };
  625. L.fi = (float) ( tA /(double) tCNT );
  626. L.tetta = (float) ( tB /(double) tCNT );
  627. L.r = (float) ( tC /(double) tCNT );
  628. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  629. ptr->ts_ms = cts;
  630. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  631. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  632. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  633. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  634. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  635. return true;
  636. };
  637. };
  638. return false;
  639. };
  640. CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){
  641. CAM_ROI *tVAL = NULL;
  642. uint64_t cts = 0;
  643. m_mROIsProtector.lock();
  644. vCAM_ROIS::iterator sRec;
  645. if( m_vROIS.size() ){
  646. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  647. if ( tVAL == NULL ) {
  648. sRec = idx;
  649. tVAL = (*idx);
  650. cts = tVAL->ts_ms;
  651. } else {
  652. #ifdef CAM_FETCH_ROI_NEWEST
  653. if ( (*idx)->ts_ms > cts )
  654. #else
  655. if ( (*idx)->ts_ms < cts )
  656. #endif
  657. {
  658. sRec = idx;
  659. tVAL = (*idx);
  660. cts = tVAL->ts_ms;
  661. };
  662. };
  663. };
  664. m_vROIS.erase( sRec );
  665. };
  666. m_mROIsProtector.unlock();
  667. return tVAL;
  668. };
  669. DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  670. DBC_SNAPSHOT_DATA *rVal = NULL;
  671. if( ptr == NULL ) return NULL;
  672. if( ptr->targets.size() ){
  673. //if( m_bSourceConnected ){
  674. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  675. if( rVal ){
  676. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  677. //rVal->stream_id = m_nActiveStreamID;
  678. rVal->stream_id = m_sNodeData.aux_parameter;
  679. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  680. LOG("Added Event ID");
  681. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  682. };
  683. };
  684. //};
  685. ptr->targets.clear();
  686. } else {
  687. LOG("No Events ID to ROI");
  688. };
  689. // Destruct ROI MEM
  690. delete( (CAM_ROI *) ptr );
  691. ptr = NULL;
  692. // Not Setting MD5 and TS -> will set at capture time
  693. return rVal;
  694. };
  695. void deviceNodeCamera::fusion_validate( void ){
  696. m_mROIsProtector.lock();
  697. if( m_vROIS.size() ){
  698. #ifdef DEBUG_EXTRA
  699. char tB[128];
  700. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  701. LOG( tB );
  702. #endif
  703. uint64_t cts = getMilliseconds();
  704. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  705. if( !fusion_validateROI( cts, *idx ) ) {
  706. free( (*idx) );
  707. m_vROIS.erase( idx );
  708. };
  709. };
  710. };
  711. m_mROIsProtector.unlock();
  712. };
  713. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  714. if( m_sCamData.online ){
  715. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  716. if( tTask ){
  717. memset(tTask, 0, sizeof(CAM_TASK));
  718. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  719. if( tTask->CMD ){
  720. tTask->CMD->CMD = PTZ_ABSOLUTE;
  721. tTask->CMD->PAN = PanPOS;
  722. tTask->CMD->TILT = TiltPOS;
  723. tTask->CMD->ZOOM = ZoomPOS;
  724. tTask->movemet_needed = true;
  725. tTask->source_evt_id = TargetRefID;
  726. setWM( CAM_WS_BUSY );
  727. return pushCamTask( tTask );
  728. } else {
  729. LOG_ERROR("CMD malloc Error!");
  730. free( tTask );
  731. };
  732. } else LOG_ERROR("Task malloc error!");
  733. };
  734. return false;
  735. };
  736. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  737. if( m_sCamData.online ){
  738. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  739. if( tTask ){
  740. memset(tTask, 0, sizeof(CAM_TASK));
  741. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  742. if( tTask->CMD ){
  743. tTask->CMD->CMD = PTZ_ABSOLUTE;
  744. tTask->CMD->PAN = O.pan;
  745. tTask->CMD->TILT = O.tilt;
  746. tTask->CMD->ZOOM = O.zoom;
  747. tTask->movemet_needed = true;
  748. tTask->source_evt_id = TargetRefID;
  749. setWM( CAM_WS_BUSY );
  750. return pushCamTask( tTask );
  751. } else {
  752. LOG_ERROR("CMD malloc Error!");
  753. free( tTask );
  754. };
  755. } else LOG_ERROR("Task malloc error!");
  756. };
  757. return false;
  758. };
  759. void deviceNodeCamera::m_fThreadProc( void ){
  760. DBC_PUSHED_CMD * PUSHED = NULL;
  761. m_bThreadStatus = TH_WORKING;
  762. bool bNewSecondLoop = false;
  763. uint32_t cTimeSeconds = 0;
  764. uint32_t oTimeSeconds = 0;
  765. while( m_bThreadStatus < TH_ENDED ) {
  766. cTimeSeconds = (uint32_t) time( NULL );
  767. if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds;
  768. uint8_t tState = getWM();
  769. char tB[128];
  770. fusion_validate();
  771. switch( tState ) {
  772. case CAM_WS_INIT:{
  773. #ifdef DEBUG
  774. LOG("Init Camera - PowerMode");
  775. #endif
  776. //setBusy( true );
  777. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  778. //setBusy( false );
  779. break;
  780. };
  781. case CAM_WS_WHITE_BALLANCE:{
  782. #ifdef DEBUG
  783. LOG("Set WB");
  784. #endif
  785. //setBusy( true );
  786. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  787. //setBusy( false );
  788. break;
  789. };
  790. case CAM_WS_BACKLIGHT:{
  791. #ifdef DEBUG
  792. LOG("Set BLC");
  793. #endif
  794. //setBusy( true );
  795. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  796. //setBusy( false );
  797. break;
  798. };
  799. case CAM_WS_AUTO_FOCUS:{
  800. #ifdef DEBUG
  801. LOG("Set AF");
  802. #endif
  803. //setBusy( true );
  804. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  805. //setBusy( false );
  806. break;
  807. };
  808. case CAM_WS_INIT_GO_HOME:{
  809. #ifdef DEBUG
  810. LOG("INIT GO -1 0 -1");
  811. #endif
  812. m_nHomingCounter = 0;
  813. if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) {
  814. m_fCP_Pan = -1;
  815. m_fCP_Tilt = 0;
  816. m_fCP_Zoom = -1;
  817. setWM( CAM_WS_READY );
  818. };
  819. break;
  820. };
  821. case CAM_WS_AUTOMATED_MOVE:{
  822. m_nRelativeMoveCounter++;
  823. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  824. //Pan Move
  825. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  826. m_fRM_Pan -= m_fSpeedRise;
  827. } else {
  828. m_fRM_Pan += m_fSpeedRise;
  829. };
  830. };
  831. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  832. //TILT Move
  833. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  834. m_fRM_Tilt -= m_fSpeedRise;
  835. } else {
  836. m_fRM_Tilt += m_fSpeedRise;
  837. }
  838. };
  839. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  840. //ZOOM Move
  841. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  842. //Wide
  843. m_fRM_Zoom -= m_fSpeedRise;
  844. } else {
  845. //Tele
  846. m_fRM_Zoom += m_fSpeedRise;
  847. };
  848. };
  849. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  850. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  851. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  852. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  853. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  854. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  855. m_fCP_Zoom += 0.5f*m_fRM_Zoom;
  856. if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f;
  857. if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f;
  858. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  859. m_nRelativeMoveCounter = 0;
  860. setWM( CAM_WS_READY );
  861. break;
  862. };
  863. if( m_sCamData.ptz_axel ) { //Manual
  864. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) {
  865. m_nRelativeMoveCounter = 0;
  866. setWM( CAM_WS_READY );
  867. };
  868. } else {
  869. // Stepping Axel by Zoom
  870. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  871. if( m_nRelativeMoveCounter > tVal ) {
  872. m_nRelativeMoveCounter = 0;
  873. setWM( CAM_WS_READY );
  874. };
  875. };
  876. break;
  877. };
  878. case CAM_WS_STOP_MOVE:{
  879. m_nRelativeMoveCounter++;
  880. m_fRM_Pan = .0f;
  881. m_fRM_Pan = .0f;
  882. m_fRM_Tilt = .0f;
  883. m_fRM_Tilt = .0f;
  884. m_fRM_Zoom = .0f;
  885. m_fRM_Zoom = .0f;
  886. if( relativeStop() ) {
  887. m_nHomingCounter = 0;
  888. m_nRelativeMoveCounter = 0;
  889. setWM( CAM_WS_READY );
  890. break;
  891. };
  892. if( m_sCamData.ptz_axel ) {
  893. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY);
  894. } else {
  895. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  896. if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY);
  897. };
  898. break;
  899. };
  900. case CAM_WS_GO_ABS_ROI:
  901. case CAM_WS_GO_ABS_POSITION:{
  902. setWM( CAM_WS_BUSY );
  903. if( m_pCurrentROI != NULL ){
  904. m_nHomingCounter = 0;
  905. if( m_pBEH ){
  906. EVENT_CONTAINER *tEVT = createBasicEvent(
  907. m_sNodeData.node_id,
  908. EVT_CLASS_CAMERA_CTRL,
  909. EVT_CAMERA_MOVE_TO,
  910. NULL, NULL, NULL );
  911. if( tEVT ){
  912. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  913. m_pBEH->broadcast( tEVT );
  914. };
  915. };
  916. //update position Info
  917. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  918. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  919. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  920. // Do Movement
  921. if( !m_pProtocol->goAbsolute(
  922. m_pCurrentROI->cam_orientation.pan,
  923. m_pCurrentROI->cam_orientation.tilt,
  924. ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection )
  925. )){
  926. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  927. setWM( CAM_WS_END );
  928. } else {
  929. // Waiting Movement finish
  930. if ( m_bSkeepPositioningWait ) {
  931. // Send Capture event
  932. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  933. utilizeROI( m_pCurrentROI );
  934. setWM( CAM_WS_READY );
  935. if( tData != NULL ) {
  936. if( m_pBEH ){
  937. EVENT_CONTAINER *tEVT = createBasicEvent(
  938. m_sNodeData.node_id,
  939. EVT_CLASS_CAMERA_CTRL,
  940. EVT_CAMERA_ON_POSITION,
  941. NULL, NULL, tData );
  942. if( tEVT ){
  943. #ifdef DEBUG
  944. LOG("INSTANT BCAST CAM ONPOS");
  945. #endif
  946. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  947. m_pBEH->broadcast( tEVT );
  948. } else {
  949. free( tData );
  950. };
  951. } else {
  952. free( tData );
  953. };
  954. };
  955. } else {
  956. setWM(CAM_WS_WAIT_ONPOS);
  957. };
  958. };
  959. } else setWM( CAM_WS_READY );
  960. break;
  961. };
  962. case CAM_WS_WAIT_ONPOS:{
  963. DBC_SNAPSHOT_DATA *tData = NULL;
  964. if( m_pCurrentROI != NULL ){
  965. // Send Capture event
  966. tData = fusion_translate_ROI( m_pCurrentROI );
  967. utilizeROI(m_pCurrentROI);
  968. };
  969. // To Accept any new ROI's ??
  970. setWM( CAM_WS_READY );
  971. #ifdef DEBUG
  972. LOG("Start Waiting Cam On Position!");
  973. #endif
  974. if (!m_pProtocol->waitMoveIsDone()) {
  975. //Some Cameras does not upport that :(
  976. //LOG_ERROR("Cannot Issue getPosition Command!->Restart Camera?");
  977. //setWM( CAM_WS_END );
  978. };
  979. if( tData != NULL ) {
  980. if( m_pBEH ){
  981. EVENT_CONTAINER *tEVT = createBasicEvent(
  982. m_sNodeData.node_id,
  983. EVT_CLASS_CAMERA_CTRL,
  984. EVT_CAMERA_ON_POSITION,
  985. NULL, NULL, tData );
  986. if( tEVT ){
  987. #ifdef DEBUG
  988. LOG("BCAST CAM ONPOS");
  989. #endif
  990. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  991. m_pBEH->broadcast( tEVT );
  992. } else {
  993. free( tData );
  994. };
  995. } else {
  996. free( tData );
  997. };
  998. tData = NULL;
  999. };
  1000. break;
  1001. };
  1002. case CAM_WS_GO_HOMING_PRESET:{
  1003. if( m_sCamData.home_token[0] != (char)0 ) {
  1004. //LOG( m_sCamData.home_token );
  1005. if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1006. LOG_ERROR("Cannot Issue goPreset Command!");
  1007. };
  1008. };
  1009. setWM( CAM_WS_READY );
  1010. break;
  1011. };
  1012. case CAM_WS_BUSY:{
  1013. //Handle Operation TO
  1014. setWM( CAM_WS_READY );
  1015. break;
  1016. };
  1017. case CAM_WS_READY:{
  1018. m_pCurrentROI = fusion_fetchROI();
  1019. if( m_pCurrentROI != NULL ) {
  1020. #ifdef DEBUG_EXTRA__
  1021. LOG("HAVE ROI to see!");
  1022. #endif
  1023. setWM( CAM_WS_GO_ABS_POSITION );
  1024. } else {
  1025. //Idle Work --->
  1026. if( m_sCamData.homing_enable ){
  1027. if ( bNewSecondLoop ) {
  1028. if ( m_nHomingCounter < m_sCamData.homing_to ) {
  1029. m_nHomingCounter++;
  1030. } else {
  1031. //LOG("Go Home Position!");
  1032. m_nHomingCounter = 0;
  1033. setWM( CAM_WS_GO_HOMING_PRESET );
  1034. break;
  1035. };
  1036. };
  1037. };
  1038. CAM_TASK * tTASK = fetchCamTask();
  1039. if ( tTASK != NULL ) {
  1040. if( tTASK->movemet_needed ){
  1041. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  1042. if( tCMD ) {
  1043. switch( tCMD->CMD ) {
  1044. case PTZ_STOP:{
  1045. m_nHomingCounter = 0;
  1046. setWM( CAM_WS_STOP_MOVE );
  1047. break;
  1048. };
  1049. case PTZ_MOVE:{
  1050. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1051. m_bRelativeMoveMatrix = 0;
  1052. m_fRM_Pan = tCMD->PAN;
  1053. m_fRM_Tilt = tCMD->TILT;
  1054. m_fRM_Zoom = tCMD->ZOOM;
  1055. m_nHomingCounter = 0;
  1056. setWM( CAM_WS_AUTOMATED_MOVE );
  1057. break;
  1058. };
  1059. case PTZ_ABSOLUTE:{
  1060. m_pCurrentROI = createCAM_ROI();
  1061. if( m_pCurrentROI != NULL ){
  1062. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1063. if ( tTarget != NULL ) {
  1064. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1065. tTarget->evt_db_ID = tTASK->source_evt_id;
  1066. m_pCurrentROI->targets.push_back( tTarget );
  1067. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1068. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1069. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1070. m_nHomingCounter = 0;
  1071. setWM(CAM_WS_GO_ABS_POSITION);
  1072. } else {
  1073. utilizeROI( m_pCurrentROI );
  1074. };
  1075. };
  1076. break;
  1077. };
  1078. case PTZ_RELOAD_CAMERA:{
  1079. LOG("Exit Command Rcpt");
  1080. m_bThreadStatus = TH_ENDED;
  1081. };
  1082. };
  1083. free( tCMD );
  1084. tTASK->CMD = NULL;
  1085. };
  1086. } else {
  1087. LOG("No Movement -> INSTCAP" );
  1088. // No Movement -> Just Capture
  1089. if( m_pBEH ){
  1090. EVENT_CONTAINER *tEVT = createBasicEvent(
  1091. m_sNodeData.node_id,
  1092. EVT_CLASS_CAMERA_CTRL,
  1093. EVT_CAMERA_ON_POSITION,
  1094. NULL, NULL, NULL );
  1095. if( tEVT ){
  1096. tEVT->m_db_id = tTASK->source_evt_id;
  1097. m_pBEH->broadcast(tEVT);
  1098. };
  1099. };
  1100. };
  1101. free( tTASK );
  1102. } else {
  1103. // Lowest Priority to Web Client Commands
  1104. #ifdef DEBUG_EXTRA__
  1105. LOG("Check Web CMD!");
  1106. #endif
  1107. PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  1108. if( PUSHED != NULL ) {
  1109. #ifdef DEBUG_EXTRA__
  1110. LOG("HAVE Web CMD!");
  1111. #endif
  1112. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  1113. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  1114. switch( PUSHED->CMD ) {
  1115. case 0x80:{
  1116. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  1117. break;
  1118. };
  1119. case 0x81:{
  1120. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  1121. break;
  1122. };
  1123. case 0x82:{
  1124. break;
  1125. };
  1126. }; //?? Check if we don't miss something!!
  1127. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here
  1128. //Direct Movment Mode
  1129. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  1130. m_nHomingCounter = 0;
  1131. setWM( CAM_WS_STOP_MOVE );
  1132. } else {
  1133. char tB[64];
  1134. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1135. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  1136. m_nHomingCounter = 0;
  1137. setWM( CAM_WS_AUTOMATED_MOVE );
  1138. };
  1139. };
  1140. free( PUSHED );
  1141. PUSHED = NULL;
  1142. };
  1143. };
  1144. };
  1145. break;
  1146. };
  1147. case CAM_WS_END:{
  1148. LOG("Exit Command Rcpt");
  1149. m_bThreadStatus = TH_ENDED;
  1150. break;
  1151. };
  1152. };
  1153. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  1154. };
  1155. #ifdef DEBUG
  1156. LOG("CTRL Loop End!");
  1157. #endif
  1158. };
  1159. bool deviceNodeCamera::setNoPositionWait( bool FL ){
  1160. m_bSkeepPositioningWait = FL;
  1161. return true;
  1162. };
  1163. bool deviceNodeCamera::testFOV( C3D_DECART L ){
  1164. //System Coords -> trans to local and go to funct
  1165. systemAnchor* tSystem = systemAnchor::getInstance();
  1166. if ( tSystem == NULL ) return false;
  1167. C3D_SPH_DEGREES SL;
  1168. #ifdef DEBUG
  1169. LOG("Test FOV local Decart coords");
  1170. #endif
  1171. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1172. return testFOV( SL );
  1173. };
  1174. bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){
  1175. //Global Coords
  1176. systemAnchor* tSystem = systemAnchor::getInstance();
  1177. if ( tSystem == NULL ) return false;
  1178. C3D_SPH_DEGREES SL;
  1179. #ifdef DEBUG
  1180. LOG("Test FOV WGS coords");
  1181. #endif
  1182. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1183. return testFOV( SL );
  1184. };
  1185. bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){
  1186. //Local Coords
  1187. C3D_SPH_DEGREES SL;
  1188. #ifdef DEBUG
  1189. LOG("Test FOV local Spherical(rad) coords");
  1190. #endif
  1191. spherical2spherical( L, SL );
  1192. return testFOV( SL );
  1193. };
  1194. bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){
  1195. #ifdef DEBUG
  1196. char tB[128];
  1197. #endif
  1198. #ifdef DEBUG
  1199. LOG("Test FOV local Spherical(deg) coords");
  1200. #endif
  1201. switch ( m_sCamData.triggering_mode ) {
  1202. case CAM_TRIGGER_STRICT:{
  1203. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1204. #ifdef DEBUG
  1205. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1206. LOG( tB );
  1207. #endif
  1208. return false;
  1209. };
  1210. break;
  1211. };
  1212. case CAM_TRIGGER_ALL_NEAR:{
  1213. if( SL.r > m_fMaxRange ){
  1214. #ifdef DEBUG
  1215. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1216. LOG( tB );
  1217. #endif
  1218. return false;
  1219. };
  1220. break;
  1221. };
  1222. case CAM_TRIGGER_ALL_FAR:{
  1223. if( SL.r < m_fMinRange ){
  1224. #ifdef DEBUG
  1225. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1226. LOG( tB );
  1227. #endif
  1228. return false;
  1229. };
  1230. break;
  1231. };
  1232. case CAM_TRIGGER_ALL: return true;
  1233. };
  1234. float HS = AC_imager_width[m_sCamData.imager_type];
  1235. float VS = AC_imager_height[m_sCamData.imager_type];
  1236. float HS2 = HS * HS;
  1237. float VS2 = VS * VS;
  1238. float IR = HS / VS;
  1239. float IR2 = IR * IR;
  1240. if( m_sCamData.pan_angles == 0 ) {
  1241. // Fixed Camera
  1242. float tA = fabsf( SL.fi );
  1243. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1244. if ( tA > tZ ) return false;
  1245. } else {
  1246. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1247. // Partial Pan
  1248. switch( m_sCamData.pan_model ) {
  1249. case 1: {
  1250. // Regular Pan
  1251. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1252. float tRad = deg2rad( SL.fi );
  1253. #ifdef DEBUG
  1254. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1255. LOG( tB );
  1256. #endif
  1257. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1258. break;
  1259. };
  1260. case 2:{
  1261. break;
  1262. };
  1263. case 3:{
  1264. break;
  1265. };
  1266. };
  1267. };
  1268. };
  1269. if( m_sCamData.tilt_angles == 0 ){
  1270. //Fixed Camera
  1271. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1272. } else {
  1273. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1274. // Partial Tilt
  1275. switch( m_sCamData.tilt_model ) {
  1276. case 1: {
  1277. // Regular Tilt
  1278. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1279. float tRad = deg2rad( SL.tetta );
  1280. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1281. break;
  1282. };
  1283. case 2:{
  1284. break;
  1285. };
  1286. };
  1287. };
  1288. };
  1289. return true;
  1290. };
  1291. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1292. systemAnchor* tSystem = systemAnchor::getInstance();
  1293. if( tSystem == NULL ) return false;
  1294. C3D_SPH_DEGREES SL;
  1295. #ifdef DEBUG_EXTRA
  1296. LOG("Look at WGS coords!");
  1297. #endif
  1298. tSystem->convertToLocal(&m_oPosition, L, SL );
  1299. return lookAt(TargetRefID, SL );
  1300. };
  1301. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1302. systemAnchor* tSystem = systemAnchor::getInstance();
  1303. if( tSystem == NULL ) return false;
  1304. C3D_SPH_DEGREES SL;
  1305. #ifdef DEBUG_EXTRA
  1306. LOG("Look at System Decart coords!");
  1307. #endif
  1308. tSystem->convertToLocal( &m_oPosition, L, SL );
  1309. return lookAt(TargetRefID, SL );
  1310. };
  1311. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1312. C3D_SPH_DEGREES SL;
  1313. #ifdef DEBUG_EXTRA
  1314. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1315. #endif
  1316. spherical2spherical( L, SL );
  1317. return lookAt(TargetRefID, SL );
  1318. };
  1319. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1320. //char tB[128];
  1321. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1322. //LOG( tB );
  1323. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1324. //LOG( tB );
  1325. #ifdef DEBUG_EXTRA
  1326. LOG("Look at local spherical deg. coords!(final)");
  1327. #endif
  1328. //if( getWM() == CAM_WS_READY ){
  1329. // C3D_ORIENT_ONVIF LL;
  1330. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1331. // return absoluteMove( TargetRefID, LL );
  1332. //};
  1333. // Camera is Busy
  1334. return fusion_consumeData(TargetRefID, L);
  1335. };
  1336. bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){
  1337. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1338. if( tTask ){
  1339. memset(tTask, 0, sizeof(CAM_TASK));
  1340. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1341. if( tTask->CMD ){
  1342. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1343. tTask->source_evt_id = TargetRefID;
  1344. return pushCamTask( tTask );
  1345. } else {
  1346. free( tTask );
  1347. };
  1348. };
  1349. return false;
  1350. };
  1351. bool deviceNodeCamera::connectToStream( uint32_t id ){
  1352. /*
  1353. if( id ){
  1354. argusKernelModule * tKM = NULL;
  1355. argusKernel * tKRNL = NULL;
  1356. argusModuleFactory * tMF = NULL;
  1357. if( m_pDBC == NULL ) {
  1358. LOG_ERROR("No Database Connector!");
  1359. return false;
  1360. };
  1361. tKRNL = argusKernel::getInstance();
  1362. if( tKRNL ) {
  1363. tMF = tKRNL->getModuleFactory();
  1364. if( tMF ) {
  1365. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1366. } else {
  1367. LOG_ERROR("No ModuleFactory Instance!");
  1368. return false;
  1369. };
  1370. } else {
  1371. LOG_ERROR("No Kernel Instance!");
  1372. return false;
  1373. };
  1374. if( tKM ) {
  1375. // New Module
  1376. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1377. if( tDATA ) {
  1378. m_sMediaSource.mediaStreamId = id;
  1379. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1380. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1381. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1382. } else {
  1383. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1384. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1385. };
  1386. if( m_pSource != NULL ) {
  1387. char tMRL[1024];
  1388. #ifdef DEBUG
  1389. LOG("Source OK");
  1390. #endif
  1391. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1392. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1393. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1394. int pix = -1;
  1395. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1396. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1397. updateOpticalParameters( tW, tH );
  1398. if( strlen( tDATA->c_type ) ){
  1399. };
  1400. //if( tW > 0 ){
  1401. // //m_pSource->setProportionalByWidth( tW );
  1402. // m_pSource->setSize( tW, tH );
  1403. //};
  1404. if( in_16_9 != out_16_9 ){
  1405. if( in_16_9 ){
  1406. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1407. } else {
  1408. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1409. };
  1410. };
  1411. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1412. sprintf( tMRL, tDATA->mrl );
  1413. } else {
  1414. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1415. if( tCAM ) {
  1416. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1417. free( tCAM );
  1418. } else {
  1419. LOG_ERROR("Error Getting CAM Data!");
  1420. delete( m_pSource );
  1421. m_pSource = NULL;
  1422. return false;
  1423. };
  1424. };
  1425. LOG( tMRL );
  1426. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1427. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1428. if ( !m_bSourceConnected ) {
  1429. //delete( m_pSource );
  1430. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1431. delete((videoAVLIBSource*)m_pSource);
  1432. } else {
  1433. delete((videoLIVE555Source*)m_pSource);
  1434. };
  1435. m_pSource = NULL;
  1436. } else {
  1437. m_nActiveStreamID = id;
  1438. #ifdef DEBUG
  1439. LOG("Source is Connected to Output.");
  1440. #endif
  1441. };
  1442. } else {
  1443. LOG_ERROR("Can not Open Video Source!");
  1444. //delete( m_pSource );
  1445. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1446. delete((videoAVLIBSource*)m_pSource);
  1447. } else {
  1448. delete((videoLIVE555Source*)m_pSource);
  1449. };
  1450. m_pSource = NULL;
  1451. };
  1452. } else {
  1453. LOG_ERROR("Video Source Stream Failure!");
  1454. };
  1455. free( tDATA );
  1456. };
  1457. }
  1458. #ifdef DEBUG
  1459. else LOG("No Video Module!")
  1460. #endif
  1461. ;
  1462. };
  1463. LOG("Connect Finished!");
  1464. return m_bSourceConnected;
  1465. */
  1466. return true;
  1467. };
  1468. bool deviceNodeCamera::disconnectFromStream( void ){
  1469. /*if( m_bSourceConnected ) {
  1470. if( m_pSource ){
  1471. delete( m_pSource );
  1472. m_pSource = NULL;
  1473. };
  1474. m_bSourceConnected = false;
  1475. };
  1476. return !m_bSourceConnected;
  1477. */
  1478. return true;
  1479. };
  1480. bool deviceNodeCamera::validateStream( void ){
  1481. /*
  1482. if( m_bSourceConnected ) {
  1483. if( m_pSource ) {
  1484. if( m_pSource->isAttached() ){
  1485. if( m_pSource->getStatus() < MS_STOPED ) return true;
  1486. #ifdef DEBUG
  1487. LOG("Source is Finished!");
  1488. #endif
  1489. };
  1490. #ifdef DEBUG
  1491. LOG("Source is Disconnected!");
  1492. #endif
  1493. } else LOG_ERROR("No Source Object! Set Disconnected!");
  1494. disconnectFromStream();
  1495. };
  1496. return false;
  1497. */
  1498. return true;
  1499. };
  1500. bool deviceNodeCamera::validate(){
  1501. DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id );
  1502. if ( tDATA == NULL ) return false;
  1503. if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) {
  1504. //Settings Changed
  1505. memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  1506. };
  1507. free( tDATA );
  1508. if (!validateStream()) return false;
  1509. return deviceNode::validate();
  1510. };
  1511. #undef LOG
  1512. #undef LOG_ERROR