|
|
|
@ -3,6 +3,8 @@ |
|
|
|
#include "kernelHelperMediaMTX.h"
|
|
|
|
#include "SQLite_dataBinder.h"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#define LOG(X) m_pLog->Log("KHMTX", this, X)
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
@ -25,6 +27,7 @@ kernelHelperMediaMTX::kernelHelperMediaMTX(): basicThread() { |
|
|
|
memset( m_sAppLogFile, 0, 512 ); |
|
|
|
memset( m_sAppConfigFile, 0, 512 ); |
|
|
|
m_nLastLogSize = 0; |
|
|
|
m_bRecordMode = false; |
|
|
|
if( loadConfig() ) m_bStatus = KH_STATUS_MTX_INIT; |
|
|
|
} |
|
|
|
|
|
|
|
@ -41,7 +44,7 @@ bool kernelHelperMediaMTX::create(){ |
|
|
|
if( stat( m_sAppLogFile, &tStatbuf ) == -1 ) return false; |
|
|
|
m_nLastLogSize = tStatbuf.st_size; |
|
|
|
char tB[128]; |
|
|
|
sprintf( tB, "Initial Log Size: %lld", m_nLastLogSize ); |
|
|
|
sprintf( tB, "Initial Log Size: %ld", m_nLastLogSize ); |
|
|
|
LOG( tB ); |
|
|
|
return init(); |
|
|
|
} |
|
|
|
@ -59,10 +62,11 @@ bool kernelHelperMediaMTX::updateConfig(){ |
|
|
|
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:") ){ |
|
|
|
CSTR fPP = NULL; |
|
|
|
if( ( fPP = strstr( cline, "paths:") ) ){ |
|
|
|
free( cline ); |
|
|
|
break; |
|
|
|
} else { |
|
|
|
@ -77,11 +81,11 @@ bool kernelHelperMediaMTX::updateConfig(){ |
|
|
|
return false; |
|
|
|
}; |
|
|
|
char tB[128]; |
|
|
|
sprintf( tB,"Update %d Stream Records", m_vStreamRecords.size() ); |
|
|
|
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_%d:\n", 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!!!"); |
|
|
|
}; |
|
|
|
@ -115,9 +119,34 @@ bool kernelHelperMediaMTX::validateConfig(){ |
|
|
|
while (( read = getline( &cline, &len, fd )) != -1) { |
|
|
|
cline = trim( cline ); |
|
|
|
if ( !bStreams ) { |
|
|
|
if ( fPP = strstr(cline, "paths:")) bStreams = true; |
|
|
|
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 ( ( fPP = strstr(cline, "source: ")) ) { |
|
|
|
if ( cNodeID ) { |
|
|
|
fPP += 8; |
|
|
|
for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ){ |
|
|
|
@ -129,7 +158,7 @@ bool kernelHelperMediaMTX::validateConfig(){ |
|
|
|
}; |
|
|
|
} else { |
|
|
|
cNodeID = 0; |
|
|
|
if ( fPP = strstr(cline, "stream_") ){ |
|
|
|
if ( ( fPP = strstr(cline, "stream_") ) ){ |
|
|
|
fPP += 7; |
|
|
|
if( CSTR cPP = strstr(cline, ":") ){ |
|
|
|
cPP[0] = (char)0; |
|
|
|
@ -137,7 +166,7 @@ bool kernelHelperMediaMTX::validateConfig(){ |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
if ( fPP = strstr(cline, "all_others:")) { |
|
|
|
if ( ( fPP = strstr(cline, "all_others:") ) ) { |
|
|
|
bStreams = false; |
|
|
|
}; |
|
|
|
}; |
|
|
|
@ -161,7 +190,7 @@ bool kernelHelperMediaMTX::loadConfig(){ |
|
|
|
while (( read = getline( &cline, &len, fd )) != -1) { |
|
|
|
cline = trim( cline ); |
|
|
|
CSTR fPP = NULL; |
|
|
|
if( fPP = strstr( cline, "LoadState=") ){ |
|
|
|
if( ( fPP = strstr( cline, "LoadState=") ) ){ |
|
|
|
if( strncmp( fPP+10,"not-found", 9 ) == 0 ){ |
|
|
|
LOG_ERROR( "No Service Defined!"); |
|
|
|
break; |
|
|
|
@ -170,7 +199,7 @@ bool kernelHelperMediaMTX::loadConfig(){ |
|
|
|
LOG( "Service is Loadded!"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
if( fPP = strstr( cline, "ActiveState=") ){ |
|
|
|
if( ( fPP = strstr( cline, "ActiveState=") ) ){ |
|
|
|
if( strncmp( fPP+12,"inactive", 8 ) == 0 ){ |
|
|
|
LOG_ERROR( "Service is Inactive!"); |
|
|
|
}; |
|
|
|
@ -178,7 +207,7 @@ bool kernelHelperMediaMTX::loadConfig(){ |
|
|
|
LOG( "Service is Active!"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
if( fPP = strstr( cline, "SubState=") ){ |
|
|
|
if( ( fPP = strstr( cline, "SubState=") ) ){ |
|
|
|
if( strncmp( fPP+9, "dead", 4 ) == 0 ){ |
|
|
|
LOG( "Service is dead!"); |
|
|
|
}; |
|
|
|
@ -186,15 +215,15 @@ bool kernelHelperMediaMTX::loadConfig(){ |
|
|
|
LOG( "Service is Running!"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
if( fPP = strstr( cline, "WorkingDirectory=") ){ |
|
|
|
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[]=") ) { |
|
|
|
if( ( fPP = strstr( cline, "ExecStart=") ) ){ |
|
|
|
if ( ( fPP = strstr( cline, "argv[]=") ) ) { |
|
|
|
fPP = strstr( fPP, " " ); |
|
|
|
CSTR sPP = strstr( fPP, ";"); |
|
|
|
fPP++; |
|
|
|
@ -229,17 +258,132 @@ bool kernelHelperMediaMTX::loadStreams(){ |
|
|
|
}; |
|
|
|
delete( tL ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
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 ){ |
|
|
|
@ -324,20 +468,20 @@ void kernelHelperMediaMTX::m_fThreadProc(void){ |
|
|
|
CSTR cline = NULL; |
|
|
|
while (( read = getline( &cline, &len, fd )) != -1) { |
|
|
|
CSTR fPP = NULL; |
|
|
|
if( fPP = strstr(cline, "ERR" ) ){ |
|
|
|
if( ( fPP = strstr(cline, "ERR" ) ) ){ |
|
|
|
//Error Message!
|
|
|
|
if ( fPP = strstr(cline,"RTSP source") ) { |
|
|
|
if ( ( fPP = strstr(cline,"RTSP source") ) ) { |
|
|
|
#ifdef DEBUG_EXTRA
|
|
|
|
LOG("Error In Source!"); |
|
|
|
#endif
|
|
|
|
if ( fPP = strstr(cline, "[path ")) { |
|
|
|
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, "_") ) { |
|
|
|
if ( ( fPP = strstr( tStream_Name, "_") ) ) { |
|
|
|
uint16_t NodeID = atoi( fPP+1 ); |
|
|
|
if ( NodeID ) { |
|
|
|
for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) |
|
|
|
@ -359,12 +503,12 @@ void kernelHelperMediaMTX::m_fThreadProc(void){ |
|
|
|
};*/ |
|
|
|
}; |
|
|
|
}; |
|
|
|
if( fPP = strstr(cline, "INF" ) ){ |
|
|
|
if ( fPP = strstr(cline,"online") ) { |
|
|
|
if( ( fPP = strstr(cline, "INF" ) ) ){ |
|
|
|
if ( ( fPP = strstr(cline,"online") ) ) { |
|
|
|
#ifdef DEBUG_EXTRA
|
|
|
|
LOG("Source is Online!"); |
|
|
|
#endif
|
|
|
|
if ( fPP = strstr(cline, "[path ")) { |
|
|
|
if ( ( fPP = strstr(cline, "[path ") ) ) { |
|
|
|
CSTR sPP = strstr( fPP, "]"); |
|
|
|
fPP += 6; |
|
|
|
size_t len = sPP - fPP; |
|
|
|
@ -372,7 +516,7 @@ void kernelHelperMediaMTX::m_fThreadProc(void){ |
|
|
|
strncpy( tStream_Name, fPP, len ); |
|
|
|
tStream_Name[len] = (char)0; |
|
|
|
LOG( tStream_Name ); |
|
|
|
if ( fPP = strstr( 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++ ) |
|
|
|
@ -384,24 +528,24 @@ void kernelHelperMediaMTX::m_fThreadProc(void){ |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
if ( fPP = strstr(cline,"RTSP source") ) { |
|
|
|
if ( ( fPP = strstr(cline,"RTSP source") ) ) { |
|
|
|
uint8_t tStatus = KH_MTX_STREAM_LOADDED; |
|
|
|
if ( fPP = strstr(cline,"stopped") ) { |
|
|
|
if ( ( fPP = strstr(cline,"stopped") ) ) { |
|
|
|
LOG("Source is Disconnected!"); |
|
|
|
uint8_t tStatus = KH_MTX_STREAM_OFFLINE; |
|
|
|
}; |
|
|
|
if ( fPP = strstr(cline,"started") ) { |
|
|
|
if ( ( fPP = strstr(cline,"started") ) ) { |
|
|
|
LOG("Source is Connected!"); |
|
|
|
uint8_t tStatus = KH_MTX_STREAM_ONLINE; |
|
|
|
}; |
|
|
|
if ( fPP = strstr(cline, "[path ")) { |
|
|
|
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, "_") ) { |
|
|
|
if ( ( fPP = strstr( tStream_Name, "_") ) ) { |
|
|
|
uint16_t NodeID = atoi( fPP+1 ); |
|
|
|
if ( NodeID ) { |
|
|
|
for ( uint16_t idx = 0; idx < m_vStreamRecords.size(); idx++ ) |
|
|
|
@ -432,7 +576,7 @@ void kernelHelperMediaMTX::m_fThreadProc(void){ |
|
|
|
}; |
|
|
|
m_nLastLogSize = tStatbuf.st_size; |
|
|
|
}; |
|
|
|
SLEEP(1000);//One Second
|
|
|
|
for( uint8_t llo=0; m_bThreadStatus < TH_ENDED, llo < 20; llo++) SLEEP(50);//One Second
|
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
|