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.

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