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.
 
 
 
 
 
 

94 lines
2.8 KiB

#include "mediaNginxStreamingProxyManager.h"
#define LOG( X ) m_pLog->Log( "NGINX_MEDIA", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "NGINX_MEDIA", this, _LOG_ERROR, X )
mediaNginxManager *mediaNginxManager::_instance = NULL;
mediaNginxManager *mediaNginxManager::getInstance(){
// check if the instance has been created yet
if( 0 == _instance ){
// if not, then create it
_instance = new mediaNginxManager;
};
// return the single instance
return _instance;
};
mediaNginxManager::mediaNginxManager(): basicThread(){
m_pLog = basicLogCollection::getInstance();
basicThread::init();
};
static void *streamFeed( void *ptr ){
MEDIA_MANAGER_RECORD * mRec;
mRec = (MEDIA_MANAGER_RECORD*) ptr;
mRec->FID = popen( mRec->CMD, "r" );
if( mRec->FID!= NULL ){
while( !feof( mRec->FID ) ){
};
pclose( mRec->FID );
};
mRec->ThreadStatus = TH_EXIT;
pthread_exit( ptr );
return ptr;
};
bool mediaNginxManager::restartStream( MEDIA_MANAGER_RECORD *tRec ){
if( tRec == NULL ) return false;
tRec->ThreadStatus = TH_ARMED;
int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
if( RetVal == -1 ) {
LOG_ERROR("Thread Creation Failed.");
tRec->ThreadStatus = TH_EXIT;
return false;
};
return false;
};
bool mediaNginxManager::attachStream( CSTR URI, bool TCP_CONNECTION, uint32_t id, CSTR md5 ){
MEDIA_MANAGER_RECORD *tRec;
tRec = (MEDIA_MANAGER_RECORD*) malloc( sizeof(MEDIA_MANAGER_RECORD));
if( tRec == NULL ) return false;
strncpy( tRec->URI, URI, 1024 );
strncpy( tRec->md5, md5, 33 );
tRec->FID = NULL;
tRec->ThreadStatus = TH_WORKING;
sprintf( tRec->CMD, NGINX_MEDIA_MANAGER_CMD, ((mRec->tcp_connection)?"-rtsp_transport tcp":""), URI, NGINX_MEDIA_MANAGER_DEFAULT_FEED, md5 );
int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
if( RetVal == -1 ) {
LOG_ERROR("Thread Creation Failed.");
free( tRec );
return false;
};
m_oProtector.lock();
m_vStreams.push_back( tRec );
m_oProtector.unlock();
return true;
};
/* thread is for checking streams purposes */
void mediaNginxManager::m_fThreadProc( void ){
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
m_oProtector.lock();
if( m_vStreams.size() ){
for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ){
if( (*index)->ThreadStatus > TH_WORKING ){
void * ptr;
int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr );
if( RetVal == 0 ){
pthread_detach( (*index)->m_sThreadHandler );
};
restartStream( *index );
};
};
};
m_oProtector.unlock();
SLEEP( NGINX_MEDIA_MANAGER_INTERVAL );
};
};
#undef LOG
#undef LOG_ERROR