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.

587 lines
25 KiB

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