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.

619 lines
26 KiB

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