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.

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