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.

443 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef LRF_PROXY_ONLY
  2. #include "kernelHelperMediaMTX.h"
  3. #include "SQLite_dataBinder.h"
  4. #define LOG(X) m_pLog->Log("KHMTX", this, X)
  5. #ifdef DEBUG
  6. #define LOG_ERROR(X) \
  7. { \
  8. char _tBfrErrMSG2w34[1024]; \
  9. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  10. m_pLog->Log("KHMTX", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  11. }
  12. #else
  13. #define LOG_ERROR(X) m_pLog->Log("KHMTX", this, _LOG_ERROR, X);
  14. #endif
  15. kernelHelperMediaMTX::kernelHelperMediaMTX(): basicThread() {
  16. m_bStatus = KH_STATUS_MTX_NA;
  17. m_pDBC = sqliteDBC::getInstance();
  18. m_pLog = basicLogCollection::getInstance();
  19. memset( m_sWorkDir, 0, 512 );
  20. memset( m_sAppLogFile, 0, 512 );
  21. memset( m_sAppConfigFile, 0, 512 );
  22. m_nLastLogSize = 0;
  23. if( loadConfig() ) m_bStatus = KH_STATUS_MTX_INIT;
  24. }
  25. kernelHelperMediaMTX::~kernelHelperMediaMTX(){
  26. if( m_bStatus ){
  27. };
  28. }
  29. bool kernelHelperMediaMTX::create(){
  30. LOG("Create!");
  31. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  32. struct stat tStatbuf;
  33. if( stat( m_sAppLogFile, &tStatbuf ) == -1 ) return false;
  34. m_nLastLogSize = tStatbuf.st_size;
  35. char tB[128];
  36. sprintf( tB, "Initial Log Size: %lld", m_nLastLogSize );
  37. LOG( tB );
  38. return init();
  39. }
  40. bool kernelHelperMediaMTX::restart(){
  41. int RetVal = system( "systemctl restart mediamtx.service" );
  42. if ( RetVal == -1 ) return false;
  43. return true;
  44. }
  45. bool kernelHelperMediaMTX::updateConfig(){
  46. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  47. FILE *fd = fopen( m_sAppConfigFile, "r+" );
  48. if ( fd ) {
  49. CSTR cline = NULL;
  50. size_t len = 0;
  51. ssize_t read;
  52. while (( read = getline( &cline, &len, fd )) != -1) {
  53. cline = trim( cline );
  54. CSTR fPP = NULL;
  55. if( fPP = strstr( cline, "paths:") ){
  56. free( cline );
  57. break;
  58. } else {
  59. free( cline );
  60. cline = NULL;
  61. len = 0;
  62. };
  63. };
  64. if ( fseek( fd , 0, SEEK_CUR ) != 0) {
  65. LOG_ERROR("fseek failed");
  66. fclose( fd );
  67. return false;
  68. };
  69. char tB[128];
  70. sprintf( tB,"Update %d Stream Records", m_vStreamRecords.size() );
  71. LOG( tB );
  72. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){
  73. if ( m_vStreamRecords[idx]->node_id ) {
  74. fprintf(fd, " stream_%d:\n", m_vStreamRecords[idx]->node_id);
  75. fprintf( fd, " source: %s\n", m_vStreamRecords[idx]->source_uri );
  76. } else LOG_ERROR("Some Junk in Stream List!!!");
  77. };
  78. fprintf( fd, " all_others:\n\n" );
  79. fflush( fd );
  80. // Get the descriptor and truncate the rest of the file
  81. int fdi = fileno( fd );
  82. long final_pos = ftell( fd );
  83. ftruncate( fdi, final_pos );
  84. fclose( fd );
  85. };
  86. return true;
  87. }
  88. bool kernelHelperMediaMTX::validateConfig(){
  89. if ( m_bStatus == KH_STATUS_MTX_NA ) return false;
  90. FILE *fd = fopen( m_sAppConfigFile, "r" );
  91. bool retVal = true;
  92. if ( fd ) {
  93. CSTR cline = NULL;
  94. CSTR fPP = NULL;
  95. size_t len = 0;
  96. ssize_t read;
  97. bool bStreams = false;
  98. uint16_t cNodeID = 0;
  99. //Reset Statuses
  100. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) m_vStreamRecords[idx]->valid = false;
  101. while (( read = getline( &cline, &len, fd )) != -1) {
  102. cline = trim( cline );
  103. if ( !bStreams ) {
  104. if ( fPP = strstr(cline, "paths:")) bStreams = true;
  105. } else {
  106. if ( fPP = strstr(cline, "source: ")) {
  107. if ( cNodeID ) {
  108. fPP += 8;
  109. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){
  110. if( m_vStreamRecords[idx]->node_id == cNodeID ) {
  111. if( strcmp( m_vStreamRecords[idx]->source_uri, fPP ) == 0 ) m_vStreamRecords[idx]->valid = true;
  112. break;
  113. };
  114. };
  115. };
  116. } else {
  117. cNodeID = 0;
  118. if ( fPP = strstr(cline, "stream_") ){
  119. fPP += 7;
  120. if( CSTR cPP = strstr(cline, ":") ){
  121. cPP[0] = (char)0;
  122. cNodeID = atoi( fPP );
  123. };
  124. };
  125. };
  126. if ( fPP = strstr(cline, "all_others:")) {
  127. bStreams = false;
  128. };
  129. };
  130. free( cline );
  131. cline = NULL;
  132. len = 0;
  133. };
  134. fclose( fd );
  135. };
  136. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( !m_vStreamRecords[idx]->valid ) retVal = false;
  137. return retVal;
  138. }
  139. bool kernelHelperMediaMTX::loadConfig(){
  140. FILE *fd = popen( "systemctl show mediamtx.service", "r" );
  141. if( fd ){
  142. CSTR cline = NULL;
  143. size_t len = 0;
  144. ssize_t read;
  145. // Dynamically manages buffer size
  146. while (( read = getline( &cline, &len, fd )) != -1) {
  147. cline = trim( cline );
  148. CSTR fPP = NULL;
  149. if( fPP = strstr( cline, "LoadState=") ){
  150. if( strncmp( fPP+10,"not-found", 9 ) == 0 ){
  151. LOG_ERROR( "No Service Defined!");
  152. break;
  153. };
  154. if( strncmp( fPP+10,"loaded", 6 ) == 0 ){
  155. LOG( "Service is Loadded!");
  156. };
  157. };
  158. if( fPP = strstr( cline, "ActiveState=") ){
  159. if( strncmp( fPP+12,"inactive", 8 ) == 0 ){
  160. LOG_ERROR( "Service is Inactive!");
  161. };
  162. if( strncmp( fPP+12,"active", 6 ) == 0 ){
  163. LOG( "Service is Active!");
  164. };
  165. };
  166. if( fPP = strstr( cline, "SubState=") ){
  167. if( strncmp( fPP+9, "dead", 4 ) == 0 ){
  168. LOG( "Service is dead!");
  169. };
  170. if( strncmp( fPP+9, "running", 7 ) == 0 ){
  171. LOG( "Service is Running!");
  172. };
  173. };
  174. if( fPP = strstr( cline, "WorkingDirectory=") ){
  175. strncpy( m_sWorkDir, fPP+17, 512 );
  176. //m_sWorkDir = trim( m_sWorkDir );
  177. sprintf( m_sAppLogFile, "%s/mediamtx.log", m_sWorkDir );
  178. LOG( m_sWorkDir );
  179. LOG( m_sAppLogFile );
  180. };
  181. if( fPP = strstr( cline, "ExecStart=") ){
  182. if ( fPP = strstr( cline, "argv[]=") ) {
  183. fPP = strstr( fPP, " " );
  184. CSTR sPP = strstr( fPP, ";");
  185. fPP++;
  186. size_t len = sPP - fPP - 1;
  187. strncpy( m_sAppConfigFile, fPP, len );
  188. LOG( m_sAppConfigFile );
  189. };
  190. };
  191. free( cline );
  192. cline = NULL;
  193. len = 0;
  194. };
  195. pclose( fd );
  196. };
  197. if ( m_sAppLogFile[0] != (char) 0 ) {
  198. m_bStatus = KH_STATUS_MTX_INIT;
  199. return true;
  200. };
  201. return false;
  202. }
  203. bool kernelHelperMediaMTX::loadStreams(){
  204. DBC_INT_LIST * tL = m_pDBC->listCameras();
  205. if ( tL ) {
  206. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  207. DBC_CAM_DATA *tData = m_pDBC->getCamData( *idx );
  208. if ( tData ) {
  209. if( tData->online ) assignStream( tData->stream_id );
  210. free( tData );
  211. };
  212. };
  213. delete( tL );
  214. };
  215. };
  216. bool kernelHelperMediaMTX::validateService(){
  217. loadStreams();
  218. if( !validateConfig() ) {
  219. updateConfig();
  220. return restart();
  221. };
  222. return true;
  223. }
  224. bool kernelHelperMediaMTX::assignStream( uint32_t stream_id ){
  225. if( m_pDBC ){
  226. if( stream_id ){
  227. DBC_MEDIA_STREAM_DATA *tData = m_pDBC->getMediaStreamData( stream_id );
  228. if( tData ){
  229. StreamDescRecord *tRecord = NULL;
  230. for ( int i = 0; i < m_vStreamRecords.size(); i++ ) {
  231. if ( m_vStreamRecords[i]->cam_id == tData->source_id ) {
  232. tRecord = m_vStreamRecords[i];
  233. break;
  234. };
  235. };
  236. DBC_CAM_DATA * tCamData = m_pDBC->getCamData( tData->source_id );
  237. if( tCamData ){
  238. if ( tRecord ) {
  239. sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s",
  240. tData->mrl_proto,
  241. tCamData->username,
  242. tCamData->password,
  243. tCamData->ip,
  244. tData->mrl_port,
  245. tData->mrl
  246. );
  247. tRecord->stream_id = stream_id;
  248. #ifdef DEBUG_EXTRA
  249. char tB[256];
  250. sprintf( tB, "Stream[%d] Updated. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id );
  251. LOG( tB );
  252. #endif
  253. } else {
  254. tRecord = ( StreamDescRecord *) malloc( sizeof( StreamDescRecord ) );
  255. if( tRecord ){
  256. memset( tRecord, 0, sizeof( StreamDescRecord ));
  257. sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s",
  258. tData->mrl_proto,
  259. tCamData->username,
  260. tCamData->password,
  261. tCamData->ip,
  262. tData->mrl_port,
  263. tData->mrl
  264. );
  265. tRecord->stream_id = stream_id;
  266. tRecord->cam_id = tData->source_id;
  267. tRecord->node_id = m_pDBC->getNodeIdByTypeAndAux( tCamData->type, tData->source_id );
  268. m_vStreamRecords.push_back( tRecord );
  269. #ifdef DEBUG_EXTRA
  270. char tB[256];
  271. sprintf( tB, "Stream[%d] Created. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id );
  272. LOG( tB );
  273. #endif
  274. };
  275. };
  276. free( tCamData );
  277. };
  278. free( tData );
  279. };
  280. };
  281. };
  282. return false;
  283. }
  284. void kernelHelperMediaMTX::m_fThreadProc(void){
  285. if (m_bStatus == KH_STATUS_MTX_INIT ) {
  286. pthread_setname_np( pthread_self(), "MediaMTX_HLP" );
  287. m_bThreadStatus = TH_WORKING;
  288. while (m_bThreadStatus < TH_ENDED){
  289. struct stat tStatbuf;
  290. if( stat( m_sAppLogFile, &tStatbuf ) == -1 ){
  291. LOG_ERROR("Cannot Stat Log File!");
  292. //m_bThreadStatus = TH_ENDED;
  293. } else if ( m_nLastLogSize != tStatbuf.st_size ) {
  294. if ( m_nLastLogSize > tStatbuf.st_size ) {
  295. // Reading at begin
  296. m_nLastLogSize = 0;
  297. };
  298. size_t RLen = tStatbuf.st_size - m_nLastLogSize;
  299. FILE *fd = fopen( m_sAppLogFile, "r" );
  300. if ( fd ) {
  301. fseek( fd, m_nLastLogSize, SEEK_SET );
  302. size_t len = 0;
  303. ssize_t read;
  304. CSTR cline = NULL;
  305. while (( read = getline( &cline, &len, fd )) != -1) {
  306. CSTR fPP = NULL;
  307. if( fPP = strstr(cline, "ERR" ) ){
  308. //Error Message!
  309. if ( fPP = strstr(cline,"RTSP source") ) {
  310. #ifdef DEBUG_EXTRA
  311. LOG("Error In Source!");
  312. #endif
  313. if ( fPP = strstr(cline, "[path ")) {
  314. CSTR sPP = strstr( fPP, "]");
  315. fPP += 6;
  316. size_t len = sPP - fPP;
  317. char tStream_Name[128];
  318. strncpy( tStream_Name, fPP, len );
  319. tStream_Name[len] = (char)0;
  320. if ( fPP = strstr( tStream_Name, "_") ) {
  321. uint16_t NodeID = atoi( fPP+1 );
  322. if ( NodeID ) {
  323. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  324. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  325. m_vStreamRecords[idx]->status = KH_MTX_STREAM_ERROR;
  326. break;
  327. };
  328. };
  329. };
  330. };
  331. /*if ( fPP = strstr(cline, "dial tcp ")) {
  332. CSTR sPP = strstr( fPP, ":");
  333. fPP += 9;
  334. size_t len = sPP - fPP - 1;
  335. char tIP[128];
  336. strncpy( tIP, fPP, len );
  337. tIP[len] = (char)0;
  338. LOG( tIP );
  339. };*/
  340. };
  341. };
  342. if( fPP = strstr(cline, "INF" ) ){
  343. if ( fPP = strstr(cline,"online") ) {
  344. #ifdef DEBUG_EXTRA
  345. LOG("Source is Online!");
  346. #endif
  347. if ( fPP = strstr(cline, "[path ")) {
  348. CSTR sPP = strstr( fPP, "]");
  349. fPP += 6;
  350. size_t len = sPP - fPP;
  351. char tStream_Name[128];
  352. strncpy( tStream_Name, fPP, len );
  353. tStream_Name[len] = (char)0;
  354. LOG( tStream_Name );
  355. if ( fPP = strstr( tStream_Name, "_") ) {
  356. uint16_t NodeID = atoi( fPP+1 );
  357. if ( NodeID ) {
  358. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  359. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  360. m_vStreamRecords[idx]->status = KH_MTX_STREAM_ONLINE;
  361. break;
  362. };
  363. };
  364. };
  365. };
  366. };
  367. if ( fPP = strstr(cline,"RTSP source") ) {
  368. uint8_t tStatus = KH_MTX_STREAM_LOADDED;
  369. if ( fPP = strstr(cline,"stopped") ) {
  370. LOG("Source is Disconnected!");
  371. uint8_t tStatus = KH_MTX_STREAM_OFFLINE;
  372. };
  373. if ( fPP = strstr(cline,"started") ) {
  374. LOG("Source is Connected!");
  375. uint8_t tStatus = KH_MTX_STREAM_ONLINE;
  376. };
  377. if ( fPP = strstr(cline, "[path ")) {
  378. CSTR sPP = strstr( fPP, "]");
  379. fPP += 6;
  380. size_t len = sPP - fPP;
  381. char tStream_Name[128];
  382. strncpy( tStream_Name, fPP, len );
  383. tStream_Name[len] = (char)0;
  384. if ( fPP = strstr( tStream_Name, "_") ) {
  385. uint16_t NodeID = atoi( fPP+1 );
  386. if ( NodeID ) {
  387. for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ )
  388. if( m_vStreamRecords[idx]->node_id == NodeID ) {
  389. m_vStreamRecords[idx]->status = tStatus;
  390. break;
  391. };
  392. };
  393. };
  394. };
  395. };
  396. };
  397. free( cline );
  398. cline = NULL;
  399. len = 0;
  400. };
  401. fclose(fd);
  402. if ( m_nLastLogSize > 40960 ) { // Clean Log > 40 KB
  403. LOG("Cleanup Log!");
  404. if (truncate( m_sAppLogFile, 0) == -1) {
  405. char tB[256];
  406. sprintf( tB, "Truncate failed: %s\n", strerror(errno));
  407. LOG_ERROR( tB );
  408. };
  409. };
  410. } else {
  411. LOG_ERROR("Cannot Open Log File!");
  412. };
  413. m_nLastLogSize = tStatbuf.st_size;
  414. };
  415. SLEEP(1000);//One Second
  416. };
  417. };
  418. }
  419. #undef LOG_ERROR
  420. #undef LOG
  421. #endif // !LRF_PROXY_ONLY