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.

600 lines
26 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
9 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
9 hours ago
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
9 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef LRF_PROXY_ONLY
  2. #include "kernelHelperMediaMTX.h"
  3. #include "SQLite_dataBinder.h"
  4. #include <stdexcept>
  5. #define LOG(X) m_pLog->Log("KHMTX", this, X)
  6. #ifdef DEBUG
  7. #define LOG_ERROR(X) \
  8. { \
  9. char _tBfrErrMSG2w34[1024]; \
  10. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  11. m_pLog->Log("KHMTX", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  12. }
  13. #else
  14. #define LOG_ERROR(X) m_pLog->Log("KHMTX", this, _LOG_ERROR, X);
  15. #endif
  16. kernelHelperMediaMTX::kernelHelperMediaMTX(): basicThread() {
  17. m_bStatus = KH_STATUS_MTX_NA;
  18. m_pDBC = sqliteDBC::getInstance();
  19. m_pLog = basicLogCollection::getInstance();
  20. memset( m_sWorkDir, 0, 512 );
  21. memset( m_sAppLogFile, 0, 512 );
  22. memset( m_sAppConfigFile, 0, 512 );
  23. m_nLastLogSize = 0;
  24. m_bRecordMode = false;
  25. m_tLastRestart = 0;
  26. if( loadConfig() ) m_bStatus = KH_STATUS_MTX_INIT;
  27. }
  28. kernelHelperMediaMTX::~kernelHelperMediaMTX(){
  29. if( m_bStatus ){
  30. };
  31. }
  32. bool kernelHelperMediaMTX::create(){
  33. LOG("Create!");
  34. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  35. struct stat tStatbuf;
  36. if( stat( m_sAppLogFile, &tStatbuf ) == -1 ) return false;
  37. m_nLastLogSize = tStatbuf.st_size;
  38. char tB[128];
  39. sprintf( tB, "Initial Log Size: %ld", m_nLastLogSize );
  40. LOG( tB );
  41. return init();
  42. }
  43. /*
  44. Called from the kernel loop (validateService, every second when the config does not validate).
  45. "systemctl restart" waited for the service to stop and start (many seconds) - the kernel loop stalled
  46. and the watchdog killed la_mojo. --no-block only queues the restart job; at most one restart per
  47. KH_MTX_RESTART_MIN_S (a config that keeps failing validation restarted MediaMTX every second).
  48. */
  49. bool kernelHelperMediaMTX::restart(){
  50. time_t tNow = time( NULL );
  51. if ( m_tLastRestart != 0 && ( tNow - m_tLastRestart ) < KH_MTX_RESTART_MIN_S ) {
  52. return true; // restarted a moment ago: the config is re-checked after the period
  53. };
  54. m_tLastRestart = tNow;
  55. LOG( "Restart MediaMTX (queued)" );
  56. int RetVal = system( "systemctl --no-block restart mediamtx.service" );
  57. if ( RetVal == -1 ) return false;
  58. return true;
  59. }
  60. bool kernelHelperMediaMTX::updateConfig(){
  61. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  62. FILE *fd = fopen( m_sAppConfigFile, "r+" );
  63. if ( fd ) {
  64. CSTR cline = NULL;
  65. size_t len = 0;
  66. ssize_t read;
  67. m_bRecordMode = false;
  68. while (( read = getline( &cline, &len, fd )) != -1) {
  69. cline = trim( cline );
  70. CSTR fPP = NULL;
  71. if( ( fPP = strstr( cline, "paths:") ) ){
  72. free( cline );
  73. break;
  74. } else {
  75. free( cline );
  76. cline = NULL;
  77. len = 0;
  78. };
  79. };
  80. if ( fseek( fd , 0, SEEK_CUR ) != 0) {
  81. LOG_ERROR("fseek failed");
  82. fclose( fd );
  83. return false;
  84. };
  85. char tB[128];
  86. sprintf( tB,"Update %ld Stream Records", m_vStreamRecords.size() );
  87. LOG( tB );
  88. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){
  89. if ( m_vStreamRecords[idx]->node_id ) {
  90. fprintf(fd, " stream_%ld:\n", m_vStreamRecords[idx]->node_id);
  91. fprintf( fd, " source: %s\n", m_vStreamRecords[idx]->source_uri );
  92. } else LOG_ERROR("Some Junk in Stream List!!!");
  93. };
  94. fprintf( fd, " all_others:\n\n" );
  95. fflush( fd );
  96. // Get the descriptor and truncate the rest of the file
  97. int fdi = fileno( fd );
  98. long final_pos = ftell( fd );
  99. ftruncate( fdi, final_pos );
  100. fclose( fd );
  101. };
  102. return true;
  103. }
  104. bool kernelHelperMediaMTX::validateConfig(){
  105. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  106. FILE *fd = fopen( m_sAppConfigFile, "r" );
  107. bool retVal = true;
  108. if ( fd ) {
  109. CSTR cline = NULL;
  110. CSTR fPP = NULL;
  111. size_t len = 0;
  112. ssize_t read;
  113. bool bStreams = false;
  114. uint16_t cNodeID = 0;
  115. //Reset Statuses
  116. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) m_vStreamRecords[idx]->valid = false;
  117. while (( read = getline( &cline, &len, fd )) != -1) {
  118. cline = trim( cline );
  119. if ( !bStreams ) {
  120. if( ( fPP = strstr( cline, "record:") ) ){
  121. CSTR tPP = strstr( cline, "#");
  122. if ( tPP == NULL || fPP < tPP ) {
  123. fPP = strstr( cline, "true");
  124. if ( fPP ) {
  125. if (tPP == NULL) {
  126. m_bRecordMode = true;
  127. } else {
  128. if ( fPP < tPP ) {
  129. m_bRecordMode = true;
  130. };
  131. };
  132. };
  133. };
  134. };
  135. if ( m_bRecordMode ) {
  136. if ( (fPP = strstr(cline, "recordPath:")) ) {
  137. CSTR tPP = strstr( cline,"%path" );
  138. if ( tPP ) {
  139. CSTR cP = trim( fPP + 11 );
  140. strncpy( m_sRecordsBase, cP, tPP - cP - 2 );
  141. m_sRecordsBase[tPP - cP-2] = (char) 0;
  142. };
  143. };
  144. };
  145. if ( ( fPP = strstr(cline, "paths:")) ) bStreams = true;
  146. } else {
  147. if ( ( fPP = strstr(cline, "source: ")) ) {
  148. if ( cNodeID ) {
  149. fPP += 8;
  150. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){
  151. if( m_vStreamRecords[idx]->node_id == cNodeID ) {
  152. if( strcmp( m_vStreamRecords[idx]->source_uri, fPP ) == 0 ) m_vStreamRecords[idx]->valid = true;
  153. break;
  154. };
  155. };
  156. };
  157. } else {
  158. cNodeID = 0;
  159. if ( ( fPP = strstr(cline, "stream_") ) ){
  160. fPP += 7;
  161. if( CSTR cPP = strstr(cline, ":") ){
  162. cPP[0] = (char)0;
  163. cNodeID = atoi( fPP );
  164. };
  165. };
  166. };
  167. if ( ( fPP = strstr(cline, "all_others:") ) ) {
  168. bStreams = false;
  169. };
  170. };
  171. free( cline );
  172. cline = NULL;
  173. len = 0;
  174. };
  175. fclose( fd );
  176. };
  177. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( !m_vStreamRecords[idx]->valid ) retVal = false;
  178. return retVal;
  179. }
  180. bool kernelHelperMediaMTX::loadConfig(){
  181. FILE *fd = popen( "systemctl show mediamtx.service", "r" );
  182. if( fd ){
  183. CSTR cline = NULL;
  184. size_t len = 0;
  185. ssize_t read;
  186. // Dynamically manages buffer size
  187. while (( read = getline( &cline, &len, fd )) != -1) {
  188. cline = trim( cline );
  189. CSTR fPP = NULL;
  190. if( ( fPP = strstr( cline, "LoadState=") ) ){
  191. if( strncmp( fPP+10,"not-found", 9 ) == 0 ){
  192. LOG_ERROR( "No Service Defined!");
  193. break;
  194. };
  195. if( strncmp( fPP+10,"loaded", 6 ) == 0 ){
  196. LOG( "Service is Loadded!");
  197. };
  198. };
  199. if( ( fPP = strstr( cline, "ActiveState=") ) ){
  200. if( strncmp( fPP+12,"inactive", 8 ) == 0 ){
  201. LOG_ERROR( "Service is Inactive!");
  202. };
  203. if( strncmp( fPP+12,"active", 6 ) == 0 ){
  204. LOG( "Service is Active!");
  205. };
  206. };
  207. if( ( fPP = strstr( cline, "SubState=") ) ){
  208. if( strncmp( fPP+9, "dead", 4 ) == 0 ){
  209. LOG( "Service is dead!");
  210. };
  211. if( strncmp( fPP+9, "running", 7 ) == 0 ){
  212. LOG( "Service is Running!");
  213. };
  214. };
  215. if( ( fPP = strstr( cline, "WorkingDirectory=") ) ){
  216. strncpy( m_sWorkDir, fPP+17, 512 );
  217. //m_sWorkDir = trim( m_sWorkDir );
  218. sprintf( m_sAppLogFile, "%s/mediamtx.log", m_sWorkDir );
  219. LOG( m_sWorkDir );
  220. LOG( m_sAppLogFile );
  221. };
  222. if( ( fPP = strstr( cline, "ExecStart=") ) ){
  223. if ( ( fPP = strstr( cline, "argv[]=") ) ) {
  224. fPP = strstr( fPP, " " );
  225. CSTR sPP = strstr( fPP, ";");
  226. fPP++;
  227. size_t len = sPP - fPP - 1;
  228. strncpy( m_sAppConfigFile, fPP, len );
  229. LOG( m_sAppConfigFile );
  230. };
  231. };
  232. free( cline );
  233. cline = NULL;
  234. len = 0;
  235. };
  236. pclose( fd );
  237. };
  238. if ( m_sAppLogFile[0] != (char) 0 ) {
  239. m_bStatus = KH_STATUS_MTX_INIT;
  240. return true;
  241. };
  242. return false;
  243. }
  244. bool kernelHelperMediaMTX::loadStreams(){
  245. DBC_INT_LIST * tL = m_pDBC->listCameras();
  246. if ( tL ) {
  247. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  248. DBC_CAM_DATA *tData = m_pDBC->getCamData( *idx );
  249. if ( tData ) {
  250. if( tData->online ) assignStream( tData->stream_id );
  251. free( tData );
  252. };
  253. };
  254. delete( tL );
  255. };
  256. return true;
  257. }
  258. #include <dirent.h>
  259. bool kernelHelperMediaMTX::validateService(){
  260. loadStreams();
  261. if( !validateConfig() ) {
  262. updateConfig();
  263. return restart();
  264. } else {
  265. if( m_bRecordMode ) {
  266. uint32_t cSeconds = time( NULL );
  267. if( m_vStreamRecords.size() ) {
  268. for( V_StreamDescRecords::iterator idx = m_vStreamRecords.begin(); idx != m_vStreamRecords.end(); idx++ ) {
  269. char cStreamName[32];
  270. char cFolder[MAX_PATH_LEN];
  271. struct dirent *entry;
  272. sprintf( cStreamName, "stream_%" PRIu64, (*idx)->node_id );
  273. sprintf( cFolder, "%s/%s", m_sRecordsBase, cStreamName );
  274. if( DIR *dir = opendir( cFolder ) ){
  275. M_StreamMap *tMap = NULL;
  276. try {
  277. tMap = m_mVideoSegments.at( (*idx)->node_id );
  278. } catch (const std::out_of_range & oor) {
  279. tMap = new M_StreamMap();
  280. if( tMap ) {
  281. m_mVideoSegments[(*idx)->node_id] = tMap;
  282. } else {
  283. LOG_ERROR("Can not create map[stream->map...]!");
  284. };
  285. };
  286. while( ( entry=readdir(dir) ) != NULL ){
  287. if ( entry->d_type == DT_REG ) {
  288. char tFileName[MAX_PATH_LEN];
  289. sprintf( tFileName, "%s/%s", cFolder, entry->d_name );
  290. struct statx stx;
  291. if( statx(AT_FDCWD, tFileName, AT_SYMLINK_NOFOLLOW, STATX_BTIME, &stx ) == 0 ){
  292. if( stx.stx_mask & STATX_BTIME ){
  293. uint32_t bSeconds = stx.stx_btime.tv_sec;
  294. StreamSegmentRecord *tSI = NULL;
  295. if( tMap ) {
  296. try{
  297. tSI = tMap->at( stx.stx_ino );
  298. } catch( const std::out_of_range & oor ) {
  299. tSI = createSSR( (*idx)->stream_id, stx.stx_btime.tv_sec, stx.stx_btime.tv_nsec / 1000, tFileName );
  300. if ( tSI ) {
  301. (*tMap)[stx.stx_ino] = tSI;
  302. };
  303. };
  304. };
  305. //More then 1 Minute
  306. if( ( cSeconds - bSeconds ) > 61 ) {
  307. // File Segment done - can be transfered to Report/History/RemoteServer
  308. // In case of bonded events
  309. if( tSI ){
  310. if ( tSI->keep ) {
  311. //Create Event
  312. };
  313. };
  314. };
  315. // More then 3 minutes
  316. if ( ( cSeconds - bSeconds ) > 180 ) {
  317. tMap->erase( stx.stx_ino );
  318. deleteSSR( tSI );
  319. unlink( tFileName );
  320. };
  321. };
  322. };
  323. };
  324. };
  325. closedir( dir );
  326. } else {
  327. LOG_ERROR("Unable to open directory!");
  328. };
  329. };
  330. };
  331. };
  332. };
  333. return true;
  334. }
  335. StreamSegmentRecord *kernelHelperMediaMTX::createSSR( uint32_t stream_id, uint32_t streamTSS, uint32_t streamTMS, CSTR path ){
  336. StreamSegmentRecord *retVal = new StreamSegmentRecord();
  337. if ( retVal ) {
  338. retVal->keep = false;
  339. retVal->SI.stream_id = stream_id;
  340. retVal->SI.streamTSS = streamTSS;
  341. retVal->SI.streamTMS = streamTMS;
  342. strncpy( retVal->SI.scratchPath, path, PATH_MAX );
  343. CSTR tMD5 = getUniqID_MD5();
  344. if( tMD5 ) {
  345. strncpy( retVal->SI.recordMD5, tMD5, 32 );
  346. free( tMD5 );
  347. };
  348. strncpy( retVal->SI.recordEXT, "mp4", 6);
  349. retVal->SI.streamWidth = 1280;
  350. retVal->SI.streamHeight = 720;
  351. retVal->SI.streamFPS = 15;
  352. };
  353. return retVal;
  354. };
  355. void kernelHelperMediaMTX::deleteSSR( StreamSegmentRecord *ptr ){
  356. if ( ptr == NULL ) return;
  357. ptr->SI.eventIDS.clear();
  358. ptr->SI.eventFrames.clear();
  359. ptr->SI.eventTSS.clear();
  360. ptr->SI.eventTMS.clear();
  361. delete( ptr );
  362. };
  363. // validateSSR - Creates Frame Records based on times
  364. void kernelHelperMediaMTX::validateSSR( StreamSegmentRecord *ptr ){
  365. if ( ptr == NULL ) return;
  366. if ( ptr->SI.eventIDS.size() ) {
  367. uint64_t vStartMS = ptr->SI.streamTSS;
  368. vStartMS = vStartMS * 1000 + ptr->SI.streamTMS;
  369. float k = 1000.0f / ptr->SI.streamFPS;
  370. for ( uint32_t i=0; i < ptr->SI.eventIDS.size(); i++ ) {
  371. uint64_t cTS = ptr->SI.eventTSS[i];
  372. cTS = cTS * 1000 + ptr->SI.eventTMS[i];
  373. ptr->SI.eventFrames.push_back( cTS / k );
  374. };
  375. };
  376. };
  377. bool kernelHelperMediaMTX::assignStream( uint32_t stream_id ){
  378. if( m_pDBC ){
  379. if( stream_id ){
  380. DBC_MEDIA_STREAM_DATA *tData = m_pDBC->getMediaStreamData( stream_id );
  381. if( tData ){
  382. StreamDescRecord *tRecord = NULL;
  383. for ( int i = 0; i < m_vStreamRecords.size(); i++ ) {
  384. if ( m_vStreamRecords[i]->cam_id == tData->source_id ) {
  385. tRecord = m_vStreamRecords[i];
  386. break;
  387. };
  388. };
  389. DBC_CAM_DATA * tCamData = m_pDBC->getCamData( tData->source_id );
  390. if( tCamData ){
  391. if ( tRecord ) {
  392. sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s",
  393. tData->mrl_proto,
  394. tCamData->username,
  395. tCamData->password,
  396. tCamData->ip,
  397. tData->mrl_port,
  398. tData->mrl
  399. );
  400. tRecord->stream_id = stream_id;
  401. #ifdef DEBUG_EXTRA
  402. char tB[256];
  403. sprintf( tB, "Stream[%d] Updated. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id );
  404. LOG( tB );
  405. #endif
  406. } else {
  407. tRecord = ( StreamDescRecord *) malloc( sizeof( StreamDescRecord ) );
  408. if( tRecord ){
  409. memset( tRecord, 0, sizeof( StreamDescRecord ));
  410. sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s",
  411. tData->mrl_proto,
  412. tCamData->username,
  413. tCamData->password,
  414. tCamData->ip,
  415. tData->mrl_port,
  416. tData->mrl
  417. );
  418. tRecord->stream_id = stream_id;
  419. tRecord->cam_id = tData->source_id;
  420. tRecord->node_id = m_pDBC->getNodeIdByTypeAndAux( tCamData->type, tData->source_id );
  421. m_vStreamRecords.push_back( tRecord );
  422. #ifdef DEBUG_EXTRA
  423. char tB[256];
  424. sprintf( tB, "Stream[%d] Created. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id );
  425. LOG( tB );
  426. #endif
  427. };
  428. };
  429. free( tCamData );
  430. };
  431. free( tData );
  432. };
  433. };
  434. };
  435. return false;
  436. }
  437. void kernelHelperMediaMTX::m_fThreadProc(void){
  438. if (m_bStatus == KH_STATUS_MTX_INIT ) {
  439. pthread_setname_np( pthread_self(), "MediaMTX_HLP" );
  440. m_bThreadStatus = TH_WORKING;
  441. while (m_bThreadStatus < TH_ENDED){
  442. struct stat tStatbuf;
  443. if( stat( m_sAppLogFile, &tStatbuf ) == -1 ){
  444. LOG_ERROR("Cannot Stat Log File!");
  445. //m_bThreadStatus = TH_ENDED;
  446. } else if ( m_nLastLogSize != tStatbuf.st_size ) {
  447. if ( m_nLastLogSize > tStatbuf.st_size ) {
  448. // Reading at begin
  449. m_nLastLogSize = 0;
  450. };
  451. size_t RLen = tStatbuf.st_size - m_nLastLogSize;
  452. FILE *fd = fopen( m_sAppLogFile, "r" );
  453. if ( fd ) {
  454. fseek( fd, m_nLastLogSize, SEEK_SET );
  455. size_t len = 0;
  456. ssize_t read;
  457. CSTR cline = NULL;
  458. while (( read = getline( &cline, &len, fd )) != -1) {
  459. CSTR fPP = NULL;
  460. if( ( fPP = strstr(cline, "ERR" ) ) ){
  461. //Error Message!
  462. if ( ( fPP = strstr(cline,"RTSP source") ) ) {
  463. #ifdef DEBUG_EXTRA
  464. LOG("Error In Source!");
  465. #endif
  466. if ( ( fPP = strstr(cline, "[path ") ) ) {
  467. CSTR sPP = strstr( fPP, "]");
  468. fPP += 6;
  469. size_t len = sPP - fPP;
  470. char tStream_Name[128];
  471. strncpy( tStream_Name, fPP, len );
  472. tStream_Name[len] = (char)0;
  473. if ( ( fPP = strstr( tStream_Name, "_") ) ) {
  474. uint16_t NodeID = atoi( fPP+1 );
  475. if ( NodeID ) {
  476. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  477. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  478. m_vStreamRecords[idx]->status = KH_MTX_STREAM_ERROR;
  479. break;
  480. };
  481. };
  482. };
  483. };
  484. /*if ( fPP = strstr(cline, "dial tcp ")) {
  485. CSTR sPP = strstr( fPP, ":");
  486. fPP += 9;
  487. size_t len = sPP - fPP - 1;
  488. char tIP[128];
  489. strncpy( tIP, fPP, len );
  490. tIP[len] = (char)0;
  491. LOG( tIP );
  492. };*/
  493. };
  494. };
  495. if( ( fPP = strstr(cline, "INF" ) ) ){
  496. if ( ( fPP = strstr(cline,"online") ) ) {
  497. #ifdef DEBUG_EXTRA
  498. LOG("Source is Online!");
  499. #endif
  500. if ( ( fPP = strstr(cline, "[path ") ) ) {
  501. CSTR sPP = strstr( fPP, "]");
  502. fPP += 6;
  503. size_t len = sPP - fPP;
  504. char tStream_Name[128];
  505. strncpy( tStream_Name, fPP, len );
  506. tStream_Name[len] = (char)0;
  507. LOG( tStream_Name );
  508. if ( ( fPP = strstr( tStream_Name, "_") ) ) {
  509. uint16_t NodeID = atoi( fPP+1 );
  510. if ( NodeID ) {
  511. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  512. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  513. m_vStreamRecords[idx]->status = KH_MTX_STREAM_ONLINE;
  514. break;
  515. };
  516. };
  517. };
  518. };
  519. };
  520. if ( ( fPP = strstr(cline,"RTSP source") ) ) {
  521. uint8_t tStatus = KH_MTX_STREAM_LOADDED;
  522. if ( ( fPP = strstr(cline,"stopped") ) ) {
  523. LOG("Source is Disconnected!");
  524. uint8_t tStatus = KH_MTX_STREAM_OFFLINE;
  525. };
  526. if ( ( fPP = strstr(cline,"started") ) ) {
  527. LOG("Source is Connected!");
  528. uint8_t tStatus = KH_MTX_STREAM_ONLINE;
  529. };
  530. if ( ( fPP = strstr(cline, "[path ") ) ) {
  531. CSTR sPP = strstr( fPP, "]");
  532. fPP += 6;
  533. size_t len = sPP - fPP;
  534. char tStream_Name[128];
  535. strncpy( tStream_Name, fPP, len );
  536. tStream_Name[len] = (char)0;
  537. if ( ( fPP = strstr( tStream_Name, "_") ) ) {
  538. uint16_t NodeID = atoi( fPP+1 );
  539. if ( NodeID ) {
  540. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  541. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  542. m_vStreamRecords[idx]->status = tStatus;
  543. break;
  544. };
  545. };
  546. };
  547. };
  548. };
  549. };
  550. free( cline );
  551. cline = NULL;
  552. len = 0;
  553. };
  554. fclose(fd);
  555. if ( m_nLastLogSize > 40960 ) { // Clean Log > 40 KB
  556. LOG("Cleanup Log!");
  557. if (truncate( m_sAppLogFile, 0) == -1) {
  558. char tB[256];
  559. sprintf( tB, "Truncate failed: %s\n", strerror(errno));
  560. LOG_ERROR( tB );
  561. };
  562. };
  563. } else {
  564. LOG_ERROR("Cannot Open Log File!");
  565. };
  566. m_nLastLogSize = tStatbuf.st_size;
  567. };
  568. for( uint8_t llo=0; m_bThreadStatus < TH_ENDED, llo < 20; llo++) SLEEP(50);//One Second
  569. };
  570. };
  571. }
  572. #undef LOG_ERROR
  573. #undef LOG
  574. #endif // !LRF_PROXY_ONLY