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.

1600 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. if( m_pProtocol!=NULL ) {
  403. if( !m_pProtocol->create() ) {
  404. LOG_ERROR("Error Creating Proto!");
  405. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  406. m_pProtocol = NULL;
  407. };
  408. };
  409. break;
  410. };
  411. default:{
  412. #ifdef DEBUG
  413. LOG("Camera Has no defined control protocol");
  414. #endif
  415. };
  416. };
  417. if( m_pProtocol == NULL ) {
  418. if( m_sCamData.protocol == 0 ) {
  419. m_sCamData.online = true;
  420. } else LOG_ERROR("No Proto Created!");
  421. } else {
  422. #ifdef DEBUG
  423. LOG("Proto is OK");
  424. #endif
  425. mojoKernel *tKRNL = mojoKernel::getInstance();
  426. if( tKRNL ) {
  427. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  428. #ifdef DEBUG
  429. char ttTT[128];
  430. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  431. LOG( ttTT );
  432. if ( m_bSkeepPositioningWait ) {
  433. LOG("Camera No Wait On Pos!");
  434. } else {
  435. LOG("Camera Will Wait On Pos!");
  436. };
  437. #endif
  438. } else {
  439. LOG_ERROR("No Kernel Instance!");
  440. };
  441. m_sCamData.online = init();
  442. };
  443. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  444. return m_sCamData.online;
  445. };
  446. //void deviceNodeCamera::setBusy( bool Status ){
  447. //m_bBusy = Status;
  448. //};
  449. bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  450. if( m_sCamData.online ) {
  451. uint32_t MoveMatrix = 0;
  452. if( PanSPD != .0f ){
  453. if( PanSPD > 0 ) {
  454. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  455. } else {
  456. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  457. };
  458. };
  459. if( TiltSPD != .0f ){
  460. if( TiltSPD > 0 ) {
  461. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  462. } else {
  463. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  464. };
  465. };
  466. if( ZoomSPD != .0f ){
  467. if( ZoomSPD > 0 ) {
  468. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  469. } else {
  470. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  471. };
  472. };
  473. if( MoveMatrix != 0 ) {
  474. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  475. };
  476. return m_pProtocol->move( MoveMatrix );
  477. };
  478. return false;
  479. };
  480. bool deviceNodeCamera::relativeStop(){
  481. if( m_sCamData.online ) {
  482. return m_pProtocol->move( 0 );
  483. };
  484. return false;
  485. };
  486. CAM_TASK *deviceNodeCamera::fetchCamTask( void ){
  487. CAM_TASK *rVal = NULL;
  488. m_mQPROTECTOR.lock();
  489. if (m_qCAM_TASK.size()) {
  490. rVal = m_qCAM_TASK.front();
  491. m_qCAM_TASK.pop();
  492. };
  493. m_mQPROTECTOR.unlock();
  494. return rVal;
  495. };
  496. bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){
  497. if ( ptr == NULL ) return false;
  498. m_mQPROTECTOR.lock();
  499. m_qCAM_TASK.push( ptr );
  500. m_mQPROTECTOR.unlock();
  501. return true;
  502. };
  503. bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  504. if( ptr == NULL ) return false;
  505. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  506. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  507. #ifdef DEBUG
  508. char tB[128];
  509. 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 );
  510. LOG(tB);
  511. #endif
  512. if( tFI > ptr->h_fov ) return false;
  513. if( tTETTA > ptr->v_fov ) return false;
  514. if( tp.r < ptr->min_range ) return false;
  515. if( tp.r > ptr->max_range ) return false;
  516. return true;
  517. };
  518. bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  519. if( db_id == 0 ) return false;
  520. CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD));
  521. if( tTrackRecord == NULL ) return false;
  522. #ifdef DEBUG_EXTRA
  523. LOG("Fusion: Consuming new Data!");
  524. #endif
  525. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  526. tTrackRecord->target_direction.fi = tOR.fi;
  527. tTrackRecord->target_direction.r = tOR.r;
  528. tTrackRecord->target_direction.tetta = tOR.tetta;
  529. tTrackRecord->evt_db_ID = db_id;
  530. tTrackRecord->ts_ms = getMilliseconds();
  531. m_mROIsProtector.lock();
  532. if( m_vROIS.size() ){
  533. vCAM_ROIS::iterator idx = m_vROIS.begin();
  534. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  535. if ( idx != m_vROIS.end() ) {
  536. // have Match!
  537. #ifdef DEBUG_EXTRA
  538. LOG("ROI MATCHED!");
  539. #endif
  540. (*idx)->targets.push_back( tTrackRecord );
  541. fusion_validateROI( tTrackRecord->ts_ms, (*idx) );
  542. } else {
  543. #ifdef DEBUG_EXTRA
  544. LOG("NO MATCH FOUND!");
  545. #endif
  546. // No mathced ROI -> create new one
  547. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  548. if ( tROI ) {
  549. memset( tROI, 0 , sizeof(CAM_ROI) );
  550. tROI->ts_ms = tTrackRecord->ts_ms;
  551. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  552. tROI->cam_position.fi = tOR.fi;
  553. tROI->cam_position.tetta = tOR.tetta;
  554. tROI->cam_position.r = tOR.r;
  555. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  556. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  557. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  558. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  559. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  560. tROI->targets.push_back( tTrackRecord );
  561. m_vROIS.push_back( tROI );
  562. };
  563. };
  564. } else {
  565. #ifdef DEBUG_EXTRA
  566. LOG("Fusion: NO ROI -> CREATE!");
  567. #endif
  568. //
  569. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  570. if ( tROI ) {
  571. memset( tROI, 0 , sizeof(CAM_ROI) );
  572. tROI->ts_ms = tTrackRecord->ts_ms;
  573. //Set vector Cam Coordinates
  574. tROI->cam_position.fi = tOR.fi;
  575. tROI->cam_position.tetta = tOR.tetta;
  576. tROI->cam_position.r = tOR.r;
  577. // Set vector OnVIF coordinates
  578. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  579. // make fusion data BOX
  580. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  581. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  582. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  583. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  584. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  585. // Add ROI to vector
  586. tROI->targets.push_back( tTrackRecord );
  587. m_vROIS.push_back( tROI );
  588. } else LOG_ERROR("No MEM4ROI");
  589. };
  590. m_mROIsProtector.unlock();
  591. return true;
  592. };
  593. bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  594. if( ptr == NULL ) return false;
  595. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  596. #ifdef DEBUG_EXTRA
  597. char tB[128];
  598. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  599. LOG( tB );
  600. #endif
  601. if( ptr->targets.size() ) {
  602. double tA = 0.0;
  603. double tB = 0.0;
  604. double tC = 0.0;
  605. uint32_t tCNT = 0;
  606. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  607. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  608. free( (*idx) );
  609. ptr->targets.erase( idx );
  610. } else {
  611. tA = tA + (*idx)->target_direction.fi;
  612. tB = tB + (*idx)->target_direction.tetta;
  613. tC = tC + (*idx)->target_direction.r;
  614. tCNT++;
  615. };
  616. };
  617. if( tCNT ) {
  618. C3D_SPH_DEGREES L = { 0 };
  619. L.fi = (float) ( tA /(double) tCNT );
  620. L.tetta = (float) ( tB /(double) tCNT );
  621. L.r = (float) ( tC /(double) tCNT );
  622. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  623. ptr->ts_ms = cts;
  624. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  625. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  626. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  627. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  628. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  629. return true;
  630. };
  631. };
  632. return false;
  633. };
  634. CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){
  635. CAM_ROI *tVAL = NULL;
  636. uint64_t cts = 0;
  637. m_mROIsProtector.lock();
  638. vCAM_ROIS::iterator sRec;
  639. if( m_vROIS.size() ){
  640. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  641. if ( tVAL == NULL ) {
  642. sRec = idx;
  643. tVAL = (*idx);
  644. cts = tVAL->ts_ms;
  645. } else {
  646. #ifdef CAM_FETCH_ROI_NEWEST
  647. if ( (*idx)->ts_ms > cts )
  648. #else
  649. if ( (*idx)->ts_ms < cts )
  650. #endif
  651. {
  652. sRec = idx;
  653. tVAL = (*idx);
  654. cts = tVAL->ts_ms;
  655. };
  656. };
  657. };
  658. m_vROIS.erase( sRec );
  659. };
  660. m_mROIsProtector.unlock();
  661. return tVAL;
  662. };
  663. DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  664. DBC_SNAPSHOT_DATA *rVal = NULL;
  665. if( ptr == NULL ) return NULL;
  666. if( ptr->targets.size() ){
  667. //if( m_bSourceConnected ){
  668. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  669. if( rVal ){
  670. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  671. //rVal->stream_id = m_nActiveStreamID;
  672. rVal->stream_id = m_sNodeData.aux_parameter;
  673. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  674. LOG("Added Event ID");
  675. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  676. };
  677. };
  678. //};
  679. ptr->targets.clear();
  680. } else {
  681. LOG("No Events ID to ROI");
  682. };
  683. // Destruct ROI MEM
  684. delete( (CAM_ROI *) ptr );
  685. ptr = NULL;
  686. // Not Setting MD5 and TS -> will set at capture time
  687. return rVal;
  688. };
  689. void deviceNodeCamera::fusion_validate( void ){
  690. m_mROIsProtector.lock();
  691. if( m_vROIS.size() ){
  692. #ifdef DEBUG_EXTRA
  693. char tB[128];
  694. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  695. LOG( tB );
  696. #endif
  697. uint64_t cts = getMilliseconds();
  698. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  699. if( !fusion_validateROI( cts, *idx ) ) {
  700. free( (*idx) );
  701. m_vROIS.erase( idx );
  702. };
  703. };
  704. };
  705. m_mROIsProtector.unlock();
  706. };
  707. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  708. if( m_sCamData.online ){
  709. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  710. if( tTask ){
  711. memset(tTask, 0, sizeof(CAM_TASK));
  712. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  713. if( tTask->CMD ){
  714. tTask->CMD->CMD = PTZ_ABSOLUTE;
  715. tTask->CMD->PAN = PanPOS;
  716. tTask->CMD->TILT = TiltPOS;
  717. tTask->CMD->ZOOM = ZoomPOS;
  718. tTask->movemet_needed = true;
  719. tTask->source_evt_id = TargetRefID;
  720. setWM( CAM_WS_BUSY );
  721. return pushCamTask( tTask );
  722. } else {
  723. LOG_ERROR("CMD malloc Error!");
  724. free( tTask );
  725. };
  726. } else LOG_ERROR("Task malloc error!");
  727. };
  728. return false;
  729. };
  730. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  731. if( m_sCamData.online ){
  732. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  733. if( tTask ){
  734. memset(tTask, 0, sizeof(CAM_TASK));
  735. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  736. if( tTask->CMD ){
  737. tTask->CMD->CMD = PTZ_ABSOLUTE;
  738. tTask->CMD->PAN = O.pan;
  739. tTask->CMD->TILT = O.tilt;
  740. tTask->CMD->ZOOM = O.zoom;
  741. tTask->movemet_needed = true;
  742. tTask->source_evt_id = TargetRefID;
  743. setWM( CAM_WS_BUSY );
  744. return pushCamTask( tTask );
  745. } else {
  746. LOG_ERROR("CMD malloc Error!");
  747. free( tTask );
  748. };
  749. } else LOG_ERROR("Task malloc error!");
  750. };
  751. return false;
  752. };
  753. void deviceNodeCamera::m_fThreadProc( void ){
  754. DBC_PUSHED_CMD * PUSHED = NULL;
  755. m_bThreadStatus = TH_WORKING;
  756. bool bNewSecondLoop = false;
  757. uint32_t cTimeSeconds = 0;
  758. uint32_t oTimeSeconds = 0;
  759. while( m_bThreadStatus < TH_ENDED ) {
  760. cTimeSeconds = (uint32_t) time( NULL );
  761. if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds;
  762. uint8_t tState = getWM();
  763. char tB[128];
  764. fusion_validate();
  765. switch( tState ) {
  766. case CAM_WS_INIT:{
  767. #ifdef DEBUG
  768. LOG("Init Camera - PowerMode");
  769. #endif
  770. //setBusy( true );
  771. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  772. //setBusy( false );
  773. break;
  774. };
  775. case CAM_WS_WHITE_BALLANCE:{
  776. #ifdef DEBUG
  777. LOG("Set WB");
  778. #endif
  779. //setBusy( true );
  780. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  781. //setBusy( false );
  782. break;
  783. };
  784. case CAM_WS_BACKLIGHT:{
  785. #ifdef DEBUG
  786. LOG("Set BLC");
  787. #endif
  788. //setBusy( true );
  789. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  790. //setBusy( false );
  791. break;
  792. };
  793. case CAM_WS_AUTO_FOCUS:{
  794. #ifdef DEBUG
  795. LOG("Set AF");
  796. #endif
  797. //setBusy( true );
  798. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  799. //setBusy( false );
  800. break;
  801. };
  802. case CAM_WS_INIT_GO_HOME:{
  803. #ifdef DEBUG
  804. LOG("INIT GO -1 0 -1");
  805. #endif
  806. m_nHomingCounter = 0;
  807. if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) {
  808. m_fCP_Pan = -1;
  809. m_fCP_Tilt = 0;
  810. m_fCP_Zoom = -1;
  811. setWM( CAM_WS_READY );
  812. };
  813. break;
  814. };
  815. case CAM_WS_AUTOMATED_MOVE:{
  816. m_nRelativeMoveCounter++;
  817. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  818. //Pan Move
  819. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  820. m_fRM_Pan -= m_fSpeedRise;
  821. } else {
  822. m_fRM_Pan += m_fSpeedRise;
  823. };
  824. };
  825. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  826. //TILT Move
  827. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  828. m_fRM_Tilt -= m_fSpeedRise;
  829. } else {
  830. m_fRM_Tilt += m_fSpeedRise;
  831. }
  832. };
  833. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  834. //ZOOM Move
  835. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  836. //Wide
  837. m_fRM_Zoom -= m_fSpeedRise;
  838. } else {
  839. //Tele
  840. m_fRM_Zoom += m_fSpeedRise;
  841. };
  842. };
  843. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  844. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  845. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  846. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  847. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  848. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  849. m_fCP_Zoom += 0.5f*m_fRM_Zoom;
  850. if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f;
  851. if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f;
  852. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  853. m_nRelativeMoveCounter = 0;
  854. setWM( CAM_WS_READY );
  855. break;
  856. };
  857. if( m_sCamData.ptz_axel ) { //Manual
  858. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) {
  859. m_nRelativeMoveCounter = 0;
  860. setWM( CAM_WS_READY );
  861. };
  862. } else {
  863. // Stepping Axel by Zoom
  864. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  865. if( m_nRelativeMoveCounter > tVal ) {
  866. m_nRelativeMoveCounter = 0;
  867. setWM( CAM_WS_READY );
  868. };
  869. };
  870. break;
  871. };
  872. case CAM_WS_STOP_MOVE:{
  873. m_nRelativeMoveCounter++;
  874. m_fRM_Pan = .0f;
  875. m_fRM_Pan = .0f;
  876. m_fRM_Tilt = .0f;
  877. m_fRM_Tilt = .0f;
  878. m_fRM_Zoom = .0f;
  879. m_fRM_Zoom = .0f;
  880. if( relativeStop() ) {
  881. m_nHomingCounter = 0;
  882. m_nRelativeMoveCounter = 0;
  883. setWM( CAM_WS_READY );
  884. break;
  885. };
  886. if( m_sCamData.ptz_axel ) {
  887. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY);
  888. } else {
  889. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  890. if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY);
  891. };
  892. break;
  893. };
  894. case CAM_WS_GO_ABS_ROI:
  895. case CAM_WS_GO_ABS_POSITION:{
  896. setWM( CAM_WS_BUSY );
  897. if( m_pCurrentROI != NULL ){
  898. m_nHomingCounter = 0;
  899. if( m_pBEH ){
  900. EVENT_CONTAINER *tEVT = createBasicEvent(
  901. m_sNodeData.node_id,
  902. EVT_CLASS_CAMERA_CTRL,
  903. EVT_CAMERA_MOVE_TO,
  904. NULL, NULL, NULL );
  905. if( tEVT ){
  906. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  907. m_pBEH->broadcast( tEVT );
  908. };
  909. };
  910. //update position Info
  911. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  912. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  913. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  914. // Do Movement
  915. if( !m_pProtocol->goAbsolute(
  916. m_pCurrentROI->cam_orientation.pan,
  917. m_pCurrentROI->cam_orientation.tilt,
  918. ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection )
  919. )){
  920. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  921. setWM( CAM_WS_END );
  922. } else {
  923. // Waiting Movement finish
  924. if ( m_bSkeepPositioningWait ) {
  925. // Send Capture event
  926. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  927. utilizeROI( m_pCurrentROI );
  928. setWM( CAM_WS_READY );
  929. if( tData != NULL ) {
  930. if( m_pBEH ){
  931. EVENT_CONTAINER *tEVT = createBasicEvent(
  932. m_sNodeData.node_id,
  933. EVT_CLASS_CAMERA_CTRL,
  934. EVT_CAMERA_ON_POSITION,
  935. NULL, NULL, tData );
  936. if( tEVT ){
  937. #ifdef DEBUG
  938. LOG("INSTANT BCAST CAM ONPOS");
  939. #endif
  940. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  941. m_pBEH->broadcast( tEVT );
  942. } else {
  943. free( tData );
  944. };
  945. } else {
  946. free( tData );
  947. };
  948. };
  949. } else {
  950. setWM(CAM_WS_WAIT_ONPOS);
  951. };
  952. };
  953. } else setWM( CAM_WS_READY );
  954. break;
  955. };
  956. case CAM_WS_WAIT_ONPOS:{
  957. DBC_SNAPSHOT_DATA *tData = NULL;
  958. if( m_pCurrentROI != NULL ){
  959. // Send Capture event
  960. tData = fusion_translate_ROI( m_pCurrentROI );
  961. utilizeROI(m_pCurrentROI);
  962. };
  963. // To Accept any new ROI's ??
  964. setWM( CAM_WS_READY );
  965. #ifdef DEBUG
  966. LOG("Start Waiting Cam On Position!");
  967. #endif
  968. if (!m_pProtocol->waitMoveIsDone()) {
  969. //Some Cameras does not upport that :(
  970. //LOG_ERROR("Cannot Issue getPosition Command!->Restart Camera?");
  971. //setWM( CAM_WS_END );
  972. };
  973. if( tData != NULL ) {
  974. if( m_pBEH ){
  975. EVENT_CONTAINER *tEVT = createBasicEvent(
  976. m_sNodeData.node_id,
  977. EVT_CLASS_CAMERA_CTRL,
  978. EVT_CAMERA_ON_POSITION,
  979. NULL, NULL, tData );
  980. if( tEVT ){
  981. #ifdef DEBUG
  982. LOG("BCAST CAM ONPOS");
  983. #endif
  984. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  985. m_pBEH->broadcast( tEVT );
  986. } else {
  987. free( tData );
  988. };
  989. } else {
  990. free( tData );
  991. };
  992. tData = NULL;
  993. };
  994. break;
  995. };
  996. case CAM_WS_GO_HOMING_PRESET:{
  997. if( m_sCamData.home_token[0] != (char)0 ) {
  998. //LOG( m_sCamData.home_token );
  999. if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1000. LOG_ERROR("Cannot Issue goPreset Command!");
  1001. };
  1002. };
  1003. setWM( CAM_WS_READY );
  1004. break;
  1005. };
  1006. case CAM_WS_BUSY:{
  1007. //Handle Operation TO
  1008. setWM( CAM_WS_READY );
  1009. break;
  1010. };
  1011. case CAM_WS_READY:{
  1012. m_pCurrentROI = fusion_fetchROI();
  1013. if( m_pCurrentROI != NULL ) {
  1014. #ifdef DEBUG_EXTRA__
  1015. LOG("HAVE ROI to see!");
  1016. #endif
  1017. setWM( CAM_WS_GO_ABS_POSITION );
  1018. } else {
  1019. //Idle Work --->
  1020. if( m_sCamData.homing_enable ){
  1021. if ( bNewSecondLoop ) {
  1022. if ( m_nHomingCounter < m_sCamData.homing_to ) {
  1023. m_nHomingCounter++;
  1024. } else {
  1025. //LOG("Go Home Position!");
  1026. m_nHomingCounter = 0;
  1027. setWM( CAM_WS_GO_HOMING_PRESET );
  1028. break;
  1029. };
  1030. };
  1031. };
  1032. CAM_TASK * tTASK = fetchCamTask();
  1033. if ( tTASK != NULL ) {
  1034. if( tTASK->movemet_needed ){
  1035. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  1036. if( tCMD ) {
  1037. switch( tCMD->CMD ) {
  1038. case PTZ_STOP:{
  1039. m_nHomingCounter = 0;
  1040. setWM( CAM_WS_STOP_MOVE );
  1041. break;
  1042. };
  1043. case PTZ_MOVE:{
  1044. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1045. m_bRelativeMoveMatrix = 0;
  1046. m_fRM_Pan = tCMD->PAN;
  1047. m_fRM_Tilt = tCMD->TILT;
  1048. m_fRM_Zoom = tCMD->ZOOM;
  1049. m_nHomingCounter = 0;
  1050. setWM( CAM_WS_AUTOMATED_MOVE );
  1051. break;
  1052. };
  1053. case PTZ_ABSOLUTE:{
  1054. m_pCurrentROI = createCAM_ROI();
  1055. if( m_pCurrentROI != NULL ){
  1056. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1057. if ( tTarget != NULL ) {
  1058. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1059. tTarget->evt_db_ID = tTASK->source_evt_id;
  1060. m_pCurrentROI->targets.push_back( tTarget );
  1061. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1062. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1063. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1064. m_nHomingCounter = 0;
  1065. setWM(CAM_WS_GO_ABS_POSITION);
  1066. } else {
  1067. utilizeROI( m_pCurrentROI );
  1068. };
  1069. };
  1070. break;
  1071. };
  1072. case PTZ_RELOAD_CAMERA:{
  1073. LOG("Exit Command Rcpt");
  1074. m_bThreadStatus = TH_ENDED;
  1075. };
  1076. };
  1077. free( tCMD );
  1078. tTASK->CMD = NULL;
  1079. };
  1080. } else {
  1081. LOG("No Movement -> INSTCAP" );
  1082. // No Movement -> Just Capture
  1083. if( m_pBEH ){
  1084. EVENT_CONTAINER *tEVT = createBasicEvent(
  1085. m_sNodeData.node_id,
  1086. EVT_CLASS_CAMERA_CTRL,
  1087. EVT_CAMERA_ON_POSITION,
  1088. NULL, NULL, NULL );
  1089. if( tEVT ){
  1090. tEVT->m_db_id = tTASK->source_evt_id;
  1091. m_pBEH->broadcast(tEVT);
  1092. };
  1093. };
  1094. };
  1095. free( tTASK );
  1096. } else {
  1097. // Lowest Priority to Web Client Commands
  1098. #ifdef DEBUG_EXTRA__
  1099. LOG("Check Web CMD!");
  1100. #endif
  1101. PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  1102. if( PUSHED != NULL ) {
  1103. #ifdef DEBUG_EXTRA__
  1104. LOG("HAVE Web CMD!");
  1105. #endif
  1106. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  1107. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  1108. switch( PUSHED->CMD ) {
  1109. case 0x80:{
  1110. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  1111. break;
  1112. };
  1113. case 0x81:{
  1114. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  1115. break;
  1116. };
  1117. case 0x82:{
  1118. break;
  1119. };
  1120. }; //?? Check if we don't miss something!!
  1121. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here
  1122. //Direct Movment Mode
  1123. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  1124. m_nHomingCounter = 0;
  1125. setWM( CAM_WS_STOP_MOVE );
  1126. } else {
  1127. char tB[64];
  1128. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1129. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  1130. m_nHomingCounter = 0;
  1131. setWM( CAM_WS_AUTOMATED_MOVE );
  1132. };
  1133. };
  1134. free( PUSHED );
  1135. PUSHED = NULL;
  1136. };
  1137. };
  1138. };
  1139. break;
  1140. };
  1141. case CAM_WS_END:{
  1142. LOG("Exit Command Rcpt");
  1143. m_bThreadStatus = TH_ENDED;
  1144. break;
  1145. };
  1146. };
  1147. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  1148. };
  1149. #ifdef DEBUG
  1150. LOG("CTRL Loop End!");
  1151. #endif
  1152. };
  1153. bool deviceNodeCamera::setNoPositionWait( bool FL ){
  1154. m_bSkeepPositioningWait = FL;
  1155. return true;
  1156. };
  1157. bool deviceNodeCamera::testFOV( C3D_DECART L ){
  1158. //System Coords -> trans to local and go to funct
  1159. systemAnchor* tSystem = systemAnchor::getInstance();
  1160. if ( tSystem == NULL ) return false;
  1161. C3D_SPH_DEGREES SL;
  1162. #ifdef DEBUG
  1163. LOG("Test FOV local Decart coords");
  1164. #endif
  1165. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1166. return testFOV( SL );
  1167. };
  1168. bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){
  1169. //Global Coords
  1170. systemAnchor* tSystem = systemAnchor::getInstance();
  1171. if ( tSystem == NULL ) return false;
  1172. C3D_SPH_DEGREES SL;
  1173. #ifdef DEBUG
  1174. LOG("Test FOV WGS coords");
  1175. #endif
  1176. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1177. return testFOV( SL );
  1178. };
  1179. bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){
  1180. //Local Coords
  1181. C3D_SPH_DEGREES SL;
  1182. #ifdef DEBUG
  1183. LOG("Test FOV local Spherical(rad) coords");
  1184. #endif
  1185. spherical2spherical( L, SL );
  1186. return testFOV( SL );
  1187. };
  1188. bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){
  1189. #ifdef DEBUG
  1190. char tB[128];
  1191. #endif
  1192. #ifdef DEBUG
  1193. LOG("Test FOV local Spherical(deg) coords");
  1194. #endif
  1195. switch ( m_sCamData.triggering_mode ) {
  1196. case CAM_TRIGGER_STRICT:{
  1197. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1198. #ifdef DEBUG
  1199. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1200. LOG( tB );
  1201. #endif
  1202. return false;
  1203. };
  1204. break;
  1205. };
  1206. case CAM_TRIGGER_ALL_NEAR:{
  1207. if( SL.r > m_fMaxRange ){
  1208. #ifdef DEBUG
  1209. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1210. LOG( tB );
  1211. #endif
  1212. return false;
  1213. };
  1214. break;
  1215. };
  1216. case CAM_TRIGGER_ALL_FAR:{
  1217. if( SL.r < m_fMinRange ){
  1218. #ifdef DEBUG
  1219. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1220. LOG( tB );
  1221. #endif
  1222. return false;
  1223. };
  1224. break;
  1225. };
  1226. case CAM_TRIGGER_ALL: return true;
  1227. };
  1228. float HS = AC_imager_width[m_sCamData.imager_type];
  1229. float VS = AC_imager_height[m_sCamData.imager_type];
  1230. float HS2 = HS * HS;
  1231. float VS2 = VS * VS;
  1232. float IR = HS / VS;
  1233. float IR2 = IR * IR;
  1234. if( m_sCamData.pan_angles == 0 ) {
  1235. // Fixed Camera
  1236. float tA = fabsf( SL.fi );
  1237. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1238. if ( tA > tZ ) return false;
  1239. } else {
  1240. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1241. // Partial Pan
  1242. switch( m_sCamData.pan_model ) {
  1243. case 1: {
  1244. // Regular Pan
  1245. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1246. float tRad = deg2rad( SL.fi );
  1247. #ifdef DEBUG
  1248. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1249. LOG( tB );
  1250. #endif
  1251. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1252. break;
  1253. };
  1254. case 2:{
  1255. break;
  1256. };
  1257. case 3:{
  1258. break;
  1259. };
  1260. };
  1261. };
  1262. };
  1263. if( m_sCamData.tilt_angles == 0 ){
  1264. //Fixed Camera
  1265. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1266. } else {
  1267. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1268. // Partial Tilt
  1269. switch( m_sCamData.tilt_model ) {
  1270. case 1: {
  1271. // Regular Tilt
  1272. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1273. float tRad = deg2rad( SL.tetta );
  1274. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1275. break;
  1276. };
  1277. case 2:{
  1278. break;
  1279. };
  1280. };
  1281. };
  1282. };
  1283. return true;
  1284. };
  1285. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1286. systemAnchor* tSystem = systemAnchor::getInstance();
  1287. if( tSystem == NULL ) return false;
  1288. C3D_SPH_DEGREES SL;
  1289. #ifdef DEBUG_EXTRA
  1290. LOG("Look at WGS coords!");
  1291. #endif
  1292. tSystem->convertToLocal(&m_oPosition, L, SL );
  1293. return lookAt(TargetRefID, SL );
  1294. };
  1295. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1296. systemAnchor* tSystem = systemAnchor::getInstance();
  1297. if( tSystem == NULL ) return false;
  1298. C3D_SPH_DEGREES SL;
  1299. #ifdef DEBUG_EXTRA
  1300. LOG("Look at System Decart coords!");
  1301. #endif
  1302. tSystem->convertToLocal( &m_oPosition, L, SL );
  1303. return lookAt(TargetRefID, SL );
  1304. };
  1305. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1306. C3D_SPH_DEGREES SL;
  1307. #ifdef DEBUG_EXTRA
  1308. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1309. #endif
  1310. spherical2spherical( L, SL );
  1311. return lookAt(TargetRefID, SL );
  1312. };
  1313. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1314. //char tB[128];
  1315. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1316. //LOG( tB );
  1317. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1318. //LOG( tB );
  1319. #ifdef DEBUG_EXTRA
  1320. LOG("Look at local spherical deg. coords!(final)");
  1321. #endif
  1322. //if( getWM() == CAM_WS_READY ){
  1323. // C3D_ORIENT_ONVIF LL;
  1324. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1325. // return absoluteMove( TargetRefID, LL );
  1326. //};
  1327. // Camera is Busy
  1328. return fusion_consumeData(TargetRefID, L);
  1329. };
  1330. bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){
  1331. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1332. if( tTask ){
  1333. memset(tTask, 0, sizeof(CAM_TASK));
  1334. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1335. if( tTask->CMD ){
  1336. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1337. tTask->source_evt_id = TargetRefID;
  1338. return pushCamTask( tTask );
  1339. } else {
  1340. free( tTask );
  1341. };
  1342. };
  1343. return false;
  1344. };
  1345. bool deviceNodeCamera::connectToStream( uint32_t id ){
  1346. /*
  1347. if( id ){
  1348. argusKernelModule * tKM = NULL;
  1349. argusKernel * tKRNL = NULL;
  1350. argusModuleFactory * tMF = NULL;
  1351. if( m_pDBC == NULL ) {
  1352. LOG_ERROR("No Database Connector!");
  1353. return false;
  1354. };
  1355. tKRNL = argusKernel::getInstance();
  1356. if( tKRNL ) {
  1357. tMF = tKRNL->getModuleFactory();
  1358. if( tMF ) {
  1359. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1360. } else {
  1361. LOG_ERROR("No ModuleFactory Instance!");
  1362. return false;
  1363. };
  1364. } else {
  1365. LOG_ERROR("No Kernel Instance!");
  1366. return false;
  1367. };
  1368. if( tKM ) {
  1369. // New Module
  1370. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1371. if( tDATA ) {
  1372. m_sMediaSource.mediaStreamId = id;
  1373. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1374. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1375. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1376. } else {
  1377. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1378. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1379. };
  1380. if( m_pSource != NULL ) {
  1381. char tMRL[1024];
  1382. #ifdef DEBUG
  1383. LOG("Source OK");
  1384. #endif
  1385. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1386. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1387. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1388. int pix = -1;
  1389. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1390. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1391. updateOpticalParameters( tW, tH );
  1392. if( strlen( tDATA->c_type ) ){
  1393. };
  1394. //if( tW > 0 ){
  1395. // //m_pSource->setProportionalByWidth( tW );
  1396. // m_pSource->setSize( tW, tH );
  1397. //};
  1398. if( in_16_9 != out_16_9 ){
  1399. if( in_16_9 ){
  1400. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1401. } else {
  1402. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1403. };
  1404. };
  1405. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1406. sprintf( tMRL, tDATA->mrl );
  1407. } else {
  1408. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1409. if( tCAM ) {
  1410. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1411. free( tCAM );
  1412. } else {
  1413. LOG_ERROR("Error Getting CAM Data!");
  1414. delete( m_pSource );
  1415. m_pSource = NULL;
  1416. return false;
  1417. };
  1418. };
  1419. LOG( tMRL );
  1420. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1421. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1422. if ( !m_bSourceConnected ) {
  1423. //delete( m_pSource );
  1424. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1425. delete((videoAVLIBSource*)m_pSource);
  1426. } else {
  1427. delete((videoLIVE555Source*)m_pSource);
  1428. };
  1429. m_pSource = NULL;
  1430. } else {
  1431. m_nActiveStreamID = id;
  1432. #ifdef DEBUG
  1433. LOG("Source is Connected to Output.");
  1434. #endif
  1435. };
  1436. } else {
  1437. LOG_ERROR("Can not Open Video Source!");
  1438. //delete( m_pSource );
  1439. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1440. delete((videoAVLIBSource*)m_pSource);
  1441. } else {
  1442. delete((videoLIVE555Source*)m_pSource);
  1443. };
  1444. m_pSource = NULL;
  1445. };
  1446. } else {
  1447. LOG_ERROR("Video Source Stream Failure!");
  1448. };
  1449. free( tDATA );
  1450. };
  1451. }
  1452. #ifdef DEBUG
  1453. else LOG("No Video Module!")
  1454. #endif
  1455. ;
  1456. };
  1457. LOG("Connect Finished!");
  1458. return m_bSourceConnected;
  1459. */
  1460. return true;
  1461. };
  1462. bool deviceNodeCamera::disconnectFromStream( void ){
  1463. /*if( m_bSourceConnected ) {
  1464. if( m_pSource ){
  1465. delete( m_pSource );
  1466. m_pSource = NULL;
  1467. };
  1468. m_bSourceConnected = false;
  1469. };
  1470. return !m_bSourceConnected;
  1471. */
  1472. return true;
  1473. };
  1474. bool deviceNodeCamera::validateStream( void ){
  1475. /*
  1476. if( m_bSourceConnected ) {
  1477. if( m_pSource ) {
  1478. if( m_pSource->isAttached() ){
  1479. if( m_pSource->getStatus() < MS_STOPED ) return true;
  1480. #ifdef DEBUG
  1481. LOG("Source is Finished!");
  1482. #endif
  1483. };
  1484. #ifdef DEBUG
  1485. LOG("Source is Disconnected!");
  1486. #endif
  1487. } else LOG_ERROR("No Source Object! Set Disconnected!");
  1488. disconnectFromStream();
  1489. };
  1490. return false;
  1491. */
  1492. return true;
  1493. };
  1494. bool deviceNodeCamera::validate(){
  1495. DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id );
  1496. if ( tDATA == NULL ) return false;
  1497. if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) {
  1498. //Settings Changed
  1499. memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  1500. };
  1501. free( tDATA );
  1502. if (!validateStream()) return false;
  1503. return deviceNode::validate();
  1504. };
  1505. #undef LOG
  1506. #undef LOG_ERROR