#include "rgooseModuleZLMedia.h" #include "rgooseKernel.h" #include "rgooseHardwareResourceNetworkProvider.h" #include "../alg/generic/md5.h" #include #include #include #include #include #include #include #define LOG( X ) m_pLog->Log( "MOD_ZLMM", this, X ) #define LOG_S( X ) basicLogCollection::getInstance()->Log( "MOD_ZLMM", NULL, X ) #define LOG_ERROR( X ) m_pLog->Log( "MOD_ZLMM", this, _LOG_ERROR, X ) argusModuleZLMedia::argusModuleZLMedia( uint32_t id ): argusKernelModule( id, ARGUS_MODULE_ZLMEDIA ){ m_pLog = basicLogCollection::getInstance(); m_pProtocol = NULL; //m_pDefinedStreams = NULL; m_bCStage = ZLMC_STAGE_INIT; m_tTimeSeconds = 0; char tCHAR[1024]; if(getcwd( tCHAR, 1024 )) { sprintf( m_pBasePath, "%s/data/videos", tCHAR ); } else { sprintf( m_pBasePath, "./data/videos" ); }; memset( &m_sSettings,0,sizeof( DBC_MEDIA_SERVER_DATA )); if( m_pDBC!= NULL ) { DBC_MEDIA_SERVER_DATA * tDATA = NULL; tDATA = m_pDBC->getMediaServerData( m_nDBID ); if( tDATA != NULL ) { memcpy( &m_sSettings, tDATA,sizeof( DBC_MEDIA_SERVER_DATA ) ); free( tDATA ); LOG( m_sSettings.name ); m_pProtocol = new protocolZLMEDIA( m_sSettings.key, m_sSettings.host, m_sSettings.ip, m_sSettings.port ); if( m_pProtocol!=NULL) { //Add IP Video Socket to Server in DB char tBuff[256]; sprintf( tBuff,"%s_pxd", m_sSettings.key ); //m_pDBC->addNodeSocket( m_pKernel->getServerNodeId(), m_nDBID, ARGUS_NODE_SOCK_TYPE_NETWORK_DVR, ARGUS_NODE_SOCK_MODE_IN, true, tBuff, "ZLMEDIA Digital" ); m_pDBC->addResource( m_pKernel->getServerNodeId(), ARGUS_HARDWARE_IP_VIDEO_IN, tBuff ); sprintf( tBuff,"%s_pxa", m_sSettings.key ); //m_pDBC->addNodeSocket( m_pKernel->getServerNodeId(), m_nDBID, ARGUS_NODE_SOCK_TYPE_AD_CONVERTER, ARGUS_NODE_SOCK_MODE_IN, true, tBuff, "ZLMEDIA Analog" ); m_pDBC->addResource( m_pKernel->getServerNodeId(), ARGUS_HARDWARE_ANALOG_VIDEO_IN, tBuff ); }; }; updateMRLs(); }; }; argusModuleZLMedia::~argusModuleZLMedia(){ if(m_pProtocol!= NULL) delete(m_pProtocol); //if(m_pDefinedStreams!=NULL) { // m_pDefinedStreams->clear(); // delete(m_pDefinedStreams); //}; if( m_pVHostIP!= NULL ) free( m_pVHostIP ); }; void argusModuleZLMedia::updateMRLs( void ){ if( strstr( m_sSettings.ip, "127.0.0.1" ) != NULL ){ //Must Update MRL only for Local Media Server LOG("Updating Local MRLs"); if ( m_pKernel ) { argusHardwareManager *tHWM = m_pKernel->getHardwareManager(); if ( tHWM ) { argusHardwareResourceNetwork *tRES = (argusHardwareResourceNetwork *) tHWM->requestResource( ARGUS_HARDWARE_PROVIDER_NETWORK, m_pKernel->getServerNodeId() ); //m_pPort = static_cast(static_cast(tRES)); if( tRES ) m_pVHostIP = tRES->getAddress( NETWORK_IPV4 ); if( m_pVHostIP!= NULL ){ }; } } }; }; bool argusModuleZLMedia::validate(){ time_t tTimeSeconds; // CLeanUp Records if( m_vRecorderSlots.size() > 0 ) { // validate old and search for( VECTOR_ZL_RECORDER_SLOTS::iterator idx=m_vRecorderSlots.begin(); idx < m_vRecorderSlots.end(); idx++ ) { if( (*idx)->m_bFinished ) { LOG( "Reaping Record Tread!ZZ" ); pthread_join( (*idx)->m_hThreadHandle, NULL ); delete( (*idx)->m_pMutex ); (*idx)->m_uStreamID = 0; free((*idx)); m_vRecorderSlots.erase( idx ); }; }; }; if( m_pProtocol != NULL ) { if( m_pProtocol->isOnline() ) { //tTimeSeconds = time( NULL ); //if( m_tTimeSeconds > tTimeSeconds ) return true; //m_tTimeSeconds = tTimeSeconds + ZLM_SECONDS_VALIDATE; #ifdef DEBUG //char tBuff[256]; //sprintf( tBuff, "Stage: %d", m_bCStage ); //LOG( tBuff ); #endif switch(m_bCStage) { // Request Current Media case ZLMC_STAGE_INIT: if( !m_pProtocol->delAllStreamProxys() ) return false; break; // Validate Media List case ZLMC_STAGE_START: { // Stay on same stage till request is finished if( !m_pProtocol->requestIsFinished() ) return true; DBC_INT_LIST *tStreams; tStreams = m_pDBC->listMediaServerStreams( m_pKernel->getSiteId(), m_sSettings.id, LIST_VALID_RECORDS ); if( tStreams != NULL ){ for( int index = 0; index < tStreams->size(); index++ ) { DBC_MEDIA_SERVER_STREAM_DATA * tData; tData = m_pDBC->getMediaServerStreamData( tStreams->at(index) ); if( tData!= NULL ) { if( ! tData->online ) { char *tKey = NULL; // Put Stream Offline at first //m_pDBC->updateMediaServerStream( tData->id ); LOG("Try to add MRL -> Server"); LOG( tData->mrl ); tKey = m_pProtocol->addStreamProxy( tData->id, m_pKernel->getServerIdentity(), tData->mrl ); LOG( tKey ); if( tKey!= NULL ) { m_pDBC->updateMediaServerStream( tData->id, tData->mrl, tKey ); }; }; free( tData ); }; }; }; break; }; case ZLM_STAGE_COLLECT_DATA:{ // Validate Invalid Streams DBC_INT_LIST *tStreams; tStreams = m_pDBC->listMediaServerStreams( m_pKernel->getSiteId(), m_sSettings.id, LIST_INVALID_RECORDS ); if( tStreams!= NULL ) { DBC_MEDIA_SERVER_STREAM_DATA *tData; for( int index = 0; index < tStreams->size(); index++ ) { tData = m_pDBC->getMediaServerStreamData( tStreams->at(index) ); if( tData!= NULL ){ //update MRL DBC_MEDIA_STREAM_DATA *tStream; tStream = m_pDBC->getMediaStreamData( tData->media_stream_id ); if( tStream != NULL ) { if( tStream->valid ){ if( tStream->source_type[0] == 'D' ) { DBC_CAM_DATA *tCam; tCam = m_pDBC->getCamData( tStream->source_id ); if( tCam!= NULL ) { if( tCam->online ) { sprintf( tData->mrl, "%s://%s:%s@%s:%d%s", tStream->mrl_proto, tCam->username, tCam->password, tCam->ip, tStream->mrl_port, tStream->mrl ); LOG( tData->mrl ); m_pDBC->updateMediaServerStream( tStreams->at(index), tData->mrl ); }; free( tCam ); } else LOG("Error Fetching Cam Data From DB"); } else { // Grabber Stream Handling }; } else { //LOG("Stream Not Valid"); }; free( tStream ); } else LOG("Error Fetching Media Stream Data From DB"); free( tData ); } else LOG("Error Fetching Media Server Stream Data From DB"); }; tStreams->clear(); delete( tStreams ); }; break; }; }; m_bCStage ++; if( m_bCStage >= ZLMC_STAGE_END ) m_bCStage = ZLMC_STAGE_START; }; }; return false; }; char *findLatestDir( char *base ){ DIR * DFD; struct dirent * DREC; struct stat STAT; struct timespec CT = { 0, 0 }; char * RetVal = NULL; CT.tv_sec = 0; DFD = opendir( base ); if( DFD ){ while (( DREC = readdir( DFD )) != NULL) { if( DREC->d_type == DT_DIR && ( DREC->d_name[0] != '.' )){ char tPath[1024]; sprintf( tPath,"%s/%s", base, DREC->d_name ); if( stat( tPath, &STAT ) == 0 ){ if( CT.tv_sec < STAT.st_mtim.tv_sec ) { CT.tv_sec = STAT.st_mtim.tv_sec; if( RetVal == NULL ) { RetVal = (char *) malloc( 1024 ); }; if( RetVal!= NULL ) { sprintf( RetVal, "%s", tPath ); } else { break; }; }; }; }; }; closedir( DFD ); }; return RetVal; }; char *findLatestFile( char *base ){ DIR * DFD; struct dirent * DREC; struct stat STAT; struct timespec CT = { 0, 0 }; char * RetVal = NULL; CT.tv_sec = 0; DFD = opendir( base ); if( DFD ){ while (( DREC = readdir( DFD )) != NULL) { if( DREC->d_type == DT_REG ){ char tPath[1024]; sprintf( tPath,"%s/%s", base, DREC->d_name ); if( stat( tPath, &STAT ) == 0 ){ if( CT.tv_sec < STAT.st_mtim.tv_sec ) { CT = STAT.st_mtim; if( RetVal == NULL ) { RetVal = (char *) malloc( 128 ); }; if( RetVal!= NULL ) { sprintf( RetVal, "%s", DREC->d_name ); } else { break; }; }; }; }; }; closedir( DFD ); }; return RetVal; }; int appendFile( const char* source, const char* destination ){ char tCMD[4096]; sprintf( tCMD, "cat \"%s\" > \"%s\"", source, destination ); return system( tCMD ); }; void argusModuleZLMedia::dispatchEvent( EVENT_CONTAINER *E ){ if( E == NULL ) return; switch( E->m_eClass ) { case EVT_CLASS_VIDEO:{ switch( E->m_eType ) { case EVT_CAPTURE_START:{ if( E->m_ref_id ) { DBC_MEDIA_SERVER_STREAM_DATA *tStream; #ifdef DEBUG char tBuff[256]; sprintf( tBuff, "RefID: %d SrcID: %d", E->m_ref_id, E->m_nDestinationNodeID ); LOG( tBuff ); #endif tStream = m_pDBC->getMediaServerStreamDataBySource( E->m_nDestinationNodeID ); if( tStream != NULL ) { if( tStream->online ){ char tDirName[MAX_PATH_LEN]; sprintf( tDirName, "%s%s", m_sSettings.docRoot, tStream->key+strlen( m_sSettings.host ) ); #ifdef DEBUG LOG("Media Dir For Record"); #endif if( !startRecord( E->m_ref_id, tStream->id, E->m_ref_UUID, tDirName ) ) LOG_ERROR("Error Starting Record!"); }; free( tStream ); } else LOG_ERROR("Error Fitching Media Stream Data!!"); } else { LOG_ERROR("No Reference ID is SET!"); }; break; }; }; break; }; }; }; bool argusModuleZLMedia::startRecord( uint32_t evt_id, uint32_t chan, CSTR evtID, CSTR base ){ ZL_RECORDER_SLOT *tRecord = NULL; if( m_vRecorderSlots.size() > 0 ) { // validate old and search for( VECTOR_ZL_RECORDER_SLOTS::iterator idx=m_vRecorderSlots.begin(); idx < m_vRecorderSlots.end(); idx++ ) { if( (*idx)->m_bFinished ){ LOG( "Finished Record Tread!" ); pthread_join( (*idx)->m_hThreadHandle, NULL ); delete( (*idx)->m_pMutex ); (*idx)->m_uStreamID = 0; free((*idx)); m_vRecorderSlots.erase( idx ); } else { if( (*idx)->m_uStreamID == chan ) { tRecord = (*idx); }; }; }; }; if( tRecord != NULL ) { tRecord->m_pMutex->lock(); //m_pDBC->addVideoEvent( evt_id, tRecord->m_uVideoID ); tRecord->m_uTimer += ZLM_10TH_SEC_RECORD_LENGTH; tRecord->m_pMutex->unlock(); return true; } else { char tCHAR[128]; CSTR tMD5; #ifdef DEBUG sprintf( tCHAR,"New Video Record Started: %d_%d", evt_id, chan ); LOG(tCHAR); #endif sprintf( tCHAR,"%d_%d", evt_id, chan ); tMD5 = MD5String( tCHAR ); if( tMD5 ) { CSTR tPath = createNewRecordFolder( tMD5 ); if( tPath != NULL ){ tRecord = (ZL_RECORDER_SLOT *)malloc( sizeof(ZL_RECORDER_SLOT)); if( tRecord != NULL ) { memset(tRecord, 0, sizeof(ZL_RECORDER_SLOT)); strncpy( tRecord->m_sSrcBase, base, 1024 ); strncpy( tRecord->m_sDstBase, tPath, 1024 ); strncpy( tRecord->m_sDstName, tMD5, 33 ); tRecord->m_uStreamID = chan; tRecord->m_bFinished = false; tRecord->m_uTimer = ZLM_10TH_SEC_RECORD_LENGTH; tRecord->m_uSkeep = ZLM_10TH_SEC_RECORD_SKEEP; tRecord->m_pMutex = new basicMutex(); //tRecord->m_uVideoID = m_pDBC->addVideoRecord( tRecord->m_uStreamID, tRecord->m_sDstName, tRecord->m_uPartNumber ); //m_pDBC->addVideoEvent( evt_id, tRecord->m_uVideoID ); if(tRecord->m_pMutex!=NULL) { int RetVal = pthread_create( &tRecord->m_hThreadHandle, NULL, m_fRecorder, ( void * ) tRecord ); if( RetVal == -1 ) { delete( tRecord->m_pMutex ); free( tRecord ); LOG_ERROR("Thread Creation Failed."); } else { m_vRecorderSlots.push_back( tRecord ); free( tPath ); return true; }; } else { LOG_ERROR("Mutex Creation Failed."); free( tRecord ); }; }; free( tPath ); }; free( tMD5 ); }; }; return false; }; float getDurationOfTS( CSTR fname ){ char tCMD[2048]; char tBuff[256]; float RetVal = .0f; FILE * FID = NULL; //sprintf( tCMD, "ffprobe %s 2>&1 | grep \"Duration\"", fname ); sprintf( tCMD, "ffprobe -hide_banner -v quiet -show_streams \"%s\" | grep \"duration=\"", fname ); FID = popen( tCMD, "r" ); if( FID ) { char * tT; fread( tBuff,1,256,FID); tT = trim( tBuff ); //RetVal = atof( tT+16 ); RetVal = atof( tT+9 ); pclose( FID ); }; return RetVal; }; void *argusModuleZLMedia::m_fRecorder( void *ptr ){ ZL_RECORDER_SLOT * MS = (ZL_RECORDER_SLOT *) ptr; argusDBC * m_pDBC = argusDBC::getInstance(); if( m_pDBC != NULL && MS != NULL ) { char SFN[1024]; char DFN[1024]; char tB[4096]; char LAST[1024]; char *cFN; while( MS->m_uSkeep && !MS->m_bFinished ) { MS->m_uSkeep--; SLEEP( 100 ); }; if( !MS->m_bFinished ){ char *tDD = findLatestDir( MS->m_sSrcBase ); if( tDD ) { sprintf( SFN,"%s", tDD ); free( tDD ); tDD = findLatestDir( SFN ); if( tDD ) { sprintf( LAST, "%s", tDD ); free( tDD ); cFN = findLatestFile( LAST ); }; }; if( cFN ) { while( MS->m_uTimer && !MS->m_bFinished ) { char *tFN; tDD = findLatestDir( MS->m_sSrcBase ); if( tDD ) { sprintf( SFN,"%s", tDD ); free( tDD ); tDD = findLatestDir( SFN ); if( tDD ) { sprintf( LAST,"%s", tDD ); free( tDD ); tFN = findLatestFile( LAST ); if( tFN ) { if( strstr( cFN, tFN )== NULL ) { sprintf( SFN, "%s/%s", LAST, cFN ); sprintf( DFN, "%s/%d.ts", MS->m_sDstBase, MS->m_uPartNumber ); appendFile( SFN, DFN ); free( cFN ); cFN = tFN; MS->m_pMutex->lock(); MS->m_uPartNumber++; //MS->m_uVideoID = m_pDBC->addVideoRecord( MS->m_uStreamID, MS->m_sDstName, MS->m_uPartNumber ); MS->m_pMutex->unlock(); } else free( tFN ); SLEEP( 100 ); MS->m_pMutex->lock(); MS->m_uTimer--; MS->m_pMutex->unlock(); } else MS->m_bFinished = true; }; }; }; // Must Be Provided adequate delay as skeep delay at beginning // https://en.wikipedia.org/wiki/M3U sprintf( SFN, "%s/%s", LAST, cFN ); sprintf( DFN, "%s/%d.ts", MS->m_sDstBase, MS->m_uPartNumber ); appendFile( SFN, DFN ); //MS->m_pMutex->lock(); //MS->m_uVideoID = m_pDBC->addVideoRecord( MS->m_uStreamID, MS->m_sDstName, MS->m_uPartNumber ); //MS->m_bFinished = true; //MS->m_pMutex->unlock(); free( cFN ); } else { LOG_S("No File Find1!"); }; MS->m_bFinished = true; }; }; pthread_exit( NULL ); return NULL; }; CSTR argusModuleZLMedia::createNewRecordFolder( CSTR seed ){ char Buffer[1024]; char tCWD[1024]; char tB[64]; int RetVal; if( fileExist( m_pBasePath ) == 0 ) { sprintf( Buffer, "No BasePath[%s] Detected", m_pBasePath ); LOG(Buffer); return NULL; }; strncpy( tB, seed, 2 ); tB[2] = (char)0; sprintf( tCWD, "%s/%s", m_pBasePath, tB ); #ifdef DEBUG sprintf( Buffer,"Level[0]: %s", tCWD ); LOG(Buffer); #endif if( fileExist( tCWD ) == 0 ){ #ifdef __linux__ RetVal = mkdir( tCWD, STORAGE_DIR_MODE ); #else RetVal = _mkdir( tCWD ); #endif if( RetVal!= 0 ) { return NULL; }; }; strncpy( tB, seed+2, 2 ); tB[2] = (char)0; sprintf( Buffer, "%s/%s", tCWD, tB ); strncpy( tCWD, Buffer, 1024 ); #ifdef DEBUG sprintf( Buffer,"Level[1]: %s", tCWD ); LOG(Buffer); #endif if( fileExist( tCWD ) == 0 ){ #ifdef __linux__ RetVal = mkdir( tCWD, STORAGE_DIR_MODE ); #else RetVal = _mkdir( tCWD ); #endif if( RetVal!= 0 ) { return NULL; }; }; /* strncpy( tB, seed+4, 28 ); tB[28] = (char)0; sprintf( Buffer, "%s/%s", tCWD, tB ); strncpy( tCWD, Buffer, 1024 ); #ifdef DEBUG sprintf( Buffer,"Level[2]: %s", tCWD ); LOG(Buffer); #endif if( fileExist( tCWD ) == 0 ){ #ifdef __linux__ RetVal = mkdir( tCWD, STORAGE_DIR_MODE ); #else RetVal = _mkdir( tCWD ); #endif if( RetVal!= 0 ) { return NULL; }; }; strncpy( tB, seed+19, 4 ); tB[4] = (char)0; sprintf( Buffer, "%s/%s", tCWD, tB ); strncpy( tCWD, Buffer, 1024 ); #ifdef DEBUG sprintf( Buffer,"Level[3]: %s", tCWD ); LOG(Buffer); #endif if( fileExist( tCWD ) == 0 ){ #ifdef __linux__ RetVal = mkdir( tCWD, STORAGE_DIR_MODE ); #else RetVal = _mkdir( tCWD ); #endif if( RetVal!= 0 ) { return NULL; }; }; strncpy( tB, seed+24, 12 ); tB[12] = (char)0; sprintf( Buffer, "%s/%s", tCWD, tB ); strncpy( tCWD, Buffer, 1024 ); #ifdef DEBUG sprintf( Buffer,"Level[4]: %s", tCWD ); LOG(Buffer); #endif if( fileExist( tCWD ) == 0 ){ #ifdef __linux__ RetVal = mkdir( tCWD, STORAGE_DIR_MODE ); #else RetVal = _mkdir( tCWD ); #endif if( RetVal!= 0 ) { return NULL; }; }; */ return strdup( tCWD ); }; #undef LOG #undef LOG_S #undef LOG_ERROR