#ifndef LRF_PROXY_ONLY #include "kernelHelperMediaMTX.h" #include "SQLite_dataBinder.h" #include #define LOG(X) m_pLog->Log("KHMTX", this, X) #ifdef DEBUG #define LOG_ERROR(X) \ { \ char _tBfrErrMSG2w34[1024]; \ sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \ m_pLog->Log("KHMTX", this, _LOG_ERROR, _tBfrErrMSG2w34); \ } #else #define LOG_ERROR(X) m_pLog->Log("KHMTX", this, _LOG_ERROR, X); #endif kernelHelperMediaMTX::kernelHelperMediaMTX(): basicThread() { m_bStatus = KH_STATUS_MTX_NA; m_pDBC = sqliteDBC::getInstance(); m_pLog = basicLogCollection::getInstance(); memset( m_sWorkDir, 0, 512 ); memset( m_sAppLogFile, 0, 512 ); memset( m_sAppConfigFile, 0, 512 ); m_nLastLogSize = 0; m_bRecordMode = false; if( loadConfig() ) m_bStatus = KH_STATUS_MTX_INIT; } kernelHelperMediaMTX::~kernelHelperMediaMTX(){ if( m_bStatus ){ }; } bool kernelHelperMediaMTX::create(){ LOG("Create!"); if ( m_bStatus == KH_STATUS_MTX_NA ) return false; struct stat tStatbuf; if( stat( m_sAppLogFile, &tStatbuf ) == -1 ) return false; m_nLastLogSize = tStatbuf.st_size; char tB[128]; sprintf( tB, "Initial Log Size: %ld", m_nLastLogSize ); LOG( tB ); return init(); } bool kernelHelperMediaMTX::restart(){ int RetVal = system( "systemctl restart mediamtx.service" ); if ( RetVal == -1 ) return false; return true; } bool kernelHelperMediaMTX::updateConfig(){ if ( m_bStatus == KH_STATUS_MTX_NA ) return false; FILE *fd = fopen( m_sAppConfigFile, "r+" ); if ( fd ) { CSTR cline = NULL; size_t len = 0; ssize_t read; m_bRecordMode = false; while (( read = getline( &cline, &len, fd )) != -1) { cline = trim( cline ); CSTR fPP = NULL; if( ( fPP = strstr( cline, "paths:") ) ){ free( cline ); break; } else { free( cline ); cline = NULL; len = 0; }; }; if ( fseek( fd , 0, SEEK_CUR ) != 0) { LOG_ERROR("fseek failed"); fclose( fd ); return false; }; char tB[128]; sprintf( tB,"Update %ld Stream Records", m_vStreamRecords.size() ); LOG( tB ); for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){ if ( m_vStreamRecords[idx]->node_id ) { fprintf(fd, " stream_%ld:\n", m_vStreamRecords[idx]->node_id); fprintf( fd, " source: %s\n", m_vStreamRecords[idx]->source_uri ); } else LOG_ERROR("Some Junk in Stream List!!!"); }; fprintf( fd, " all_others:\n\n" ); fflush( fd ); // Get the descriptor and truncate the rest of the file int fdi = fileno( fd ); long final_pos = ftell( fd ); ftruncate( fdi, final_pos ); fclose( fd ); }; return true; } bool kernelHelperMediaMTX::validateConfig(){ if ( m_bStatus == KH_STATUS_MTX_NA ) return false; FILE *fd = fopen( m_sAppConfigFile, "r" ); bool retVal = true; if ( fd ) { CSTR cline = NULL; CSTR fPP = NULL; size_t len = 0; ssize_t read; bool bStreams = false; uint16_t cNodeID = 0; //Reset Statuses for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) m_vStreamRecords[idx]->valid = false; while (( read = getline( &cline, &len, fd )) != -1) { cline = trim( cline ); if ( !bStreams ) { if( ( fPP = strstr( cline, "record:") ) ){ CSTR tPP = strstr( cline, "#"); if ( tPP == NULL || fPP < tPP ) { fPP = strstr( cline, "true"); if ( fPP ) { if (tPP == NULL) { m_bRecordMode = true; } else { if ( fPP < tPP ) { m_bRecordMode = true; }; }; }; }; }; if ( m_bRecordMode ) { if ( (fPP = strstr(cline, "recordPath:")) ) { CSTR tPP = strstr( cline,"%path" ); if ( tPP ) { CSTR cP = trim( fPP + 11 ); strncpy( m_sRecordsBase, cP, tPP - cP - 2 ); m_sRecordsBase[tPP - cP-2] = (char) 0; }; }; }; if ( ( fPP = strstr(cline, "paths:")) ) bStreams = true; } else { if ( ( fPP = strstr(cline, "source: ")) ) { if ( cNodeID ) { fPP += 8; for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){ if( m_vStreamRecords[idx]->node_id == cNodeID ) { if( strcmp( m_vStreamRecords[idx]->source_uri, fPP ) == 0 ) m_vStreamRecords[idx]->valid = true; break; }; }; }; } else { cNodeID = 0; if ( ( fPP = strstr(cline, "stream_") ) ){ fPP += 7; if( CSTR cPP = strstr(cline, ":") ){ cPP[0] = (char)0; cNodeID = atoi( fPP ); }; }; }; if ( ( fPP = strstr(cline, "all_others:") ) ) { bStreams = false; }; }; free( cline ); cline = NULL; len = 0; }; fclose( fd ); }; for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( !m_vStreamRecords[idx]->valid ) retVal = false; return retVal; } bool kernelHelperMediaMTX::loadConfig(){ FILE *fd = popen( "systemctl show mediamtx.service", "r" ); if( fd ){ CSTR cline = NULL; size_t len = 0; ssize_t read; // Dynamically manages buffer size while (( read = getline( &cline, &len, fd )) != -1) { cline = trim( cline ); CSTR fPP = NULL; if( ( fPP = strstr( cline, "LoadState=") ) ){ if( strncmp( fPP+10,"not-found", 9 ) == 0 ){ LOG_ERROR( "No Service Defined!"); break; }; if( strncmp( fPP+10,"loaded", 6 ) == 0 ){ LOG( "Service is Loadded!"); }; }; if( ( fPP = strstr( cline, "ActiveState=") ) ){ if( strncmp( fPP+12,"inactive", 8 ) == 0 ){ LOG_ERROR( "Service is Inactive!"); }; if( strncmp( fPP+12,"active", 6 ) == 0 ){ LOG( "Service is Active!"); }; }; if( ( fPP = strstr( cline, "SubState=") ) ){ if( strncmp( fPP+9, "dead", 4 ) == 0 ){ LOG( "Service is dead!"); }; if( strncmp( fPP+9, "running", 7 ) == 0 ){ LOG( "Service is Running!"); }; }; if( ( fPP = strstr( cline, "WorkingDirectory=") ) ){ strncpy( m_sWorkDir, fPP+17, 512 ); //m_sWorkDir = trim( m_sWorkDir ); sprintf( m_sAppLogFile, "%s/mediamtx.log", m_sWorkDir ); LOG( m_sWorkDir ); LOG( m_sAppLogFile ); }; if( ( fPP = strstr( cline, "ExecStart=") ) ){ if ( ( fPP = strstr( cline, "argv[]=") ) ) { fPP = strstr( fPP, " " ); CSTR sPP = strstr( fPP, ";"); fPP++; size_t len = sPP - fPP - 1; strncpy( m_sAppConfigFile, fPP, len ); LOG( m_sAppConfigFile ); }; }; free( cline ); cline = NULL; len = 0; }; pclose( fd ); }; if ( m_sAppLogFile[0] != (char) 0 ) { m_bStatus = KH_STATUS_MTX_INIT; return true; }; return false; } bool kernelHelperMediaMTX::loadStreams(){ DBC_INT_LIST * tL = m_pDBC->listCameras(); if ( tL ) { for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){ DBC_CAM_DATA *tData = m_pDBC->getCamData( *idx ); if ( tData ) { if( tData->online ) assignStream( tData->stream_id ); free( tData ); }; }; delete( tL ); }; return true; } #include bool kernelHelperMediaMTX::validateService(){ loadStreams(); if( !validateConfig() ) { updateConfig(); return restart(); } else { if( m_bRecordMode ) { uint32_t cSeconds = time( NULL ); if( m_vStreamRecords.size() ) { for( V_StreamDescRecords::iterator idx = m_vStreamRecords.begin(); idx != m_vStreamRecords.end(); idx++ ) { char cStreamName[32]; char cFolder[MAX_PATH_LEN]; struct dirent *entry; sprintf( cStreamName, "stream_%" PRIu64, (*idx)->node_id ); sprintf( cFolder, "%s/%s", m_sRecordsBase, cStreamName ); if( DIR *dir = opendir( cFolder ) ){ M_StreamMap *tMap = NULL; try { tMap = m_mVideoSegments.at( (*idx)->node_id ); } catch (const std::out_of_range & oor) { tMap = new M_StreamMap(); if( tMap ) { m_mVideoSegments[(*idx)->node_id] = tMap; } else { LOG_ERROR("Can not create map[stream->map...]!"); }; }; while( ( entry=readdir(dir) ) != NULL ){ if ( entry->d_type == DT_REG ) { char tFileName[MAX_PATH_LEN]; sprintf( tFileName, "%s/%s", cFolder, entry->d_name ); struct statx stx; if( statx(AT_FDCWD, tFileName, AT_SYMLINK_NOFOLLOW, STATX_BTIME, &stx ) == 0 ){ if( stx.stx_mask & STATX_BTIME ){ uint32_t bSeconds = stx.stx_btime.tv_sec; StreamSegmentRecord *tSI = NULL; if( tMap ) { try{ tSI = tMap->at( stx.stx_ino ); } catch( const std::out_of_range & oor ) { tSI = createSSR( (*idx)->stream_id, stx.stx_btime.tv_sec, stx.stx_btime.tv_nsec / 1000, tFileName ); if ( tSI ) { (*tMap)[stx.stx_ino] = tSI; }; }; }; //More then 1 Minute if( ( cSeconds - bSeconds ) > 61 ) { // File Segment done - can be transfered to Report/History/RemoteServer // In case of bonded events if( tSI ){ if ( tSI->keep ) { //Create Event }; }; }; // More then 3 minutes if ( ( cSeconds - bSeconds ) > 180 ) { tMap->erase( stx.stx_ino ); deleteSSR( tSI ); unlink( tFileName ); }; }; }; }; }; closedir( dir ); } else { LOG_ERROR("Unable to open directory!"); }; }; }; }; }; return true; } StreamSegmentRecord *kernelHelperMediaMTX::createSSR( uint32_t stream_id, uint32_t streamTSS, uint32_t streamTMS, CSTR path ){ StreamSegmentRecord *retVal = new StreamSegmentRecord(); if ( retVal ) { retVal->keep = false; retVal->SI.stream_id = stream_id; retVal->SI.streamTSS = streamTSS; retVal->SI.streamTMS = streamTMS; strncpy( retVal->SI.scratchPath, path, PATH_MAX ); CSTR tMD5 = getUniqID_MD5(); if( tMD5 ) { strncpy( retVal->SI.recordMD5, tMD5, 32 ); free( tMD5 ); }; strncpy( retVal->SI.recordEXT, "mp4", 6); retVal->SI.streamWidth = 1280; retVal->SI.streamHeight = 720; retVal->SI.streamFPS = 15; }; return retVal; }; void kernelHelperMediaMTX::deleteSSR( StreamSegmentRecord *ptr ){ if ( ptr == NULL ) return; ptr->SI.eventIDS.clear(); ptr->SI.eventFrames.clear(); ptr->SI.eventTSS.clear(); ptr->SI.eventTMS.clear(); delete( ptr ); }; // validateSSR - Creates Frame Records based on times void kernelHelperMediaMTX::validateSSR( StreamSegmentRecord *ptr ){ if ( ptr == NULL ) return; if ( ptr->SI.eventIDS.size() ) { uint64_t vStartMS = ptr->SI.streamTSS; vStartMS = vStartMS * 1000 + ptr->SI.streamTMS; float k = 1000.0f / ptr->SI.streamFPS; for ( uint32_t i=0; i < ptr->SI.eventIDS.size(); i++ ) { uint64_t cTS = ptr->SI.eventTSS[i]; cTS = cTS * 1000 + ptr->SI.eventTMS[i]; ptr->SI.eventFrames.push_back( cTS / k ); }; }; }; bool kernelHelperMediaMTX::assignStream( uint32_t stream_id ){ if( m_pDBC ){ if( stream_id ){ DBC_MEDIA_STREAM_DATA *tData = m_pDBC->getMediaStreamData( stream_id ); if( tData ){ StreamDescRecord *tRecord = NULL; for ( int i = 0; i < m_vStreamRecords.size(); i++ ) { if ( m_vStreamRecords[i]->cam_id == tData->source_id ) { tRecord = m_vStreamRecords[i]; break; }; }; DBC_CAM_DATA * tCamData = m_pDBC->getCamData( tData->source_id ); if( tCamData ){ if ( tRecord ) { sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s", tData->mrl_proto, tCamData->username, tCamData->password, tCamData->ip, tData->mrl_port, tData->mrl ); tRecord->stream_id = stream_id; #ifdef DEBUG_EXTRA char tB[256]; sprintf( tB, "Stream[%d] Updated. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id ); LOG( tB ); #endif } else { tRecord = ( StreamDescRecord *) malloc( sizeof( StreamDescRecord ) ); if( tRecord ){ memset( tRecord, 0, sizeof( StreamDescRecord )); sprintf( tRecord->source_uri, "%s://%s:%s@%s:%d%s", tData->mrl_proto, tCamData->username, tCamData->password, tCamData->ip, tData->mrl_port, tData->mrl ); tRecord->stream_id = stream_id; tRecord->cam_id = tData->source_id; tRecord->node_id = m_pDBC->getNodeIdByTypeAndAux( tCamData->type, tData->source_id ); m_vStreamRecords.push_back( tRecord ); #ifdef DEBUG_EXTRA char tB[256]; sprintf( tB, "Stream[%d] Created. id: %d cam %d node %d", stream_id, tRecord->stream_id, tRecord->cam_id, tRecord->node_id ); LOG( tB ); #endif }; }; free( tCamData ); }; free( tData ); }; }; }; return false; } void kernelHelperMediaMTX::m_fThreadProc(void){ if (m_bStatus == KH_STATUS_MTX_INIT ) { pthread_setname_np( pthread_self(), "MediaMTX_HLP" ); m_bThreadStatus = TH_WORKING; while (m_bThreadStatus < TH_ENDED){ struct stat tStatbuf; if( stat( m_sAppLogFile, &tStatbuf ) == -1 ){ LOG_ERROR("Cannot Stat Log File!"); //m_bThreadStatus = TH_ENDED; } else if ( m_nLastLogSize != tStatbuf.st_size ) { if ( m_nLastLogSize > tStatbuf.st_size ) { // Reading at begin m_nLastLogSize = 0; }; size_t RLen = tStatbuf.st_size - m_nLastLogSize; FILE *fd = fopen( m_sAppLogFile, "r" ); if ( fd ) { fseek( fd, m_nLastLogSize, SEEK_SET ); size_t len = 0; ssize_t read; CSTR cline = NULL; while (( read = getline( &cline, &len, fd )) != -1) { CSTR fPP = NULL; if( ( fPP = strstr(cline, "ERR" ) ) ){ //Error Message! if ( ( fPP = strstr(cline,"RTSP source") ) ) { #ifdef DEBUG_EXTRA LOG("Error In Source!"); #endif if ( ( fPP = strstr(cline, "[path ") ) ) { CSTR sPP = strstr( fPP, "]"); fPP += 6; size_t len = sPP - fPP; char tStream_Name[128]; strncpy( tStream_Name, fPP, len ); tStream_Name[len] = (char)0; if ( ( fPP = strstr( tStream_Name, "_") ) ) { uint16_t NodeID = atoi( fPP+1 ); if ( NodeID ) { for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( m_vStreamRecords[idx]->node_id == NodeID ) { m_vStreamRecords[idx]->status = KH_MTX_STREAM_ERROR; break; }; }; }; }; /*if ( fPP = strstr(cline, "dial tcp ")) { CSTR sPP = strstr( fPP, ":"); fPP += 9; size_t len = sPP - fPP - 1; char tIP[128]; strncpy( tIP, fPP, len ); tIP[len] = (char)0; LOG( tIP ); };*/ }; }; if( ( fPP = strstr(cline, "INF" ) ) ){ if ( ( fPP = strstr(cline,"online") ) ) { #ifdef DEBUG_EXTRA LOG("Source is Online!"); #endif if ( ( fPP = strstr(cline, "[path ") ) ) { CSTR sPP = strstr( fPP, "]"); fPP += 6; size_t len = sPP - fPP; char tStream_Name[128]; strncpy( tStream_Name, fPP, len ); tStream_Name[len] = (char)0; LOG( tStream_Name ); if ( ( fPP = strstr( tStream_Name, "_") ) ) { uint16_t NodeID = atoi( fPP+1 ); if ( NodeID ) { for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( m_vStreamRecords[idx]->node_id == NodeID ) { m_vStreamRecords[idx]->status = KH_MTX_STREAM_ONLINE; break; }; }; }; }; }; if ( ( fPP = strstr(cline,"RTSP source") ) ) { uint8_t tStatus = KH_MTX_STREAM_LOADDED; if ( ( fPP = strstr(cline,"stopped") ) ) { LOG("Source is Disconnected!"); uint8_t tStatus = KH_MTX_STREAM_OFFLINE; }; if ( ( fPP = strstr(cline,"started") ) ) { LOG("Source is Connected!"); uint8_t tStatus = KH_MTX_STREAM_ONLINE; }; if ( ( fPP = strstr(cline, "[path ") ) ) { CSTR sPP = strstr( fPP, "]"); fPP += 6; size_t len = sPP - fPP; char tStream_Name[128]; strncpy( tStream_Name, fPP, len ); tStream_Name[len] = (char)0; if ( ( fPP = strstr( tStream_Name, "_") ) ) { uint16_t NodeID = atoi( fPP+1 ); if ( NodeID ) { for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) if( m_vStreamRecords[idx]->node_id == NodeID ) { m_vStreamRecords[idx]->status = tStatus; break; }; }; }; }; }; }; free( cline ); cline = NULL; len = 0; }; fclose(fd); if ( m_nLastLogSize > 40960 ) { // Clean Log > 40 KB LOG("Cleanup Log!"); if (truncate( m_sAppLogFile, 0) == -1) { char tB[256]; sprintf( tB, "Truncate failed: %s\n", strerror(errno)); LOG_ERROR( tB ); }; }; } else { LOG_ERROR("Cannot Open Log File!"); }; m_nLastLogSize = tStatbuf.st_size; }; for( uint8_t llo=0; m_bThreadStatus < TH_ENDED, llo < 20; llo++) SLEEP(50);//One Second }; }; } #undef LOG_ERROR #undef LOG #endif // !LRF_PROXY_ONLY