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.
 
 
 
 
 
 

440 lines
18 KiB

#ifndef LRF_PROXY_ONLY
#include "kernelHelperMediaMTX.h"
#include "SQLite_dataBinder.h"
#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;
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: %lld", 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;
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 %d 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, " 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, "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 );
};
};
bool kernelHelperMediaMTX::validateService(){
loadStreams();
if( !validateConfig() ) {
updateConfig();
return restart();
};
return true;
}
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") ) {
//Source Error
LOG("Error In Source!");
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") ) {
LOG("Source is 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;
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;
};
SLEEP(1000);//One Second
};
};
}
#undef LOG_ERROR
#undef LOG
#endif // !LRF_PROXY_ONLY