#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(); }; mediaNginxManager::~mediaNginxManager(){ LOG("Destruct!"); waitThreadEnd( true ); LOG("Reaper Ended!"); if( m_vStreams.size() > 0 ) { LOG("Have Channels!"); for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ) { if( (*index)->ThreadStatus < TH_ENDED ) { void * ptr; char buffer[256]; sprintf( buffer, "Channel[%p] is Live!", (*index) ); LOG( buffer ); (*index)->ThreadStatus = TH_ENDED; LOG("Channel set to End!"); int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr ); if( RetVal == 0 ){ pthread_detach( (*index)->m_sThreadHandler ); }; LOG("Stream is Detached!"); }; }; }; }; #define READ 0 #define WRITE 1 pid_t popen2( const char *command, int *infp, int *outfp ){ int p_stdin[2], p_stdout[2]; pid_t pid; if( pipe(p_stdin) != 0 || pipe(p_stdout) != 0 ) return -1; pid = fork(); if( pid < 0 ){ return pid; } else { if( pid == 0 ){ close(p_stdin[WRITE]); dup2(p_stdin[READ], READ); close(p_stdout[READ]); dup2(p_stdout[WRITE], WRITE); execl("/bin/sh", "sh", "-c", command, NULL); perror("execl"); exit(1); }; }; if( infp == NULL ){ close(p_stdin[WRITE]); } else { *infp = p_stdin[WRITE]; }; if( outfp == NULL ){ close(p_stdout[READ]); } else { *outfp = p_stdout[READ]; }; return pid; }; #undef READ #undef WRITE static void *streamFeed( void *ptr ){ MEDIA_MANAGER_RECORD * mRec; char * dummy[1024]; mRec = (MEDIA_MANAGER_RECORD*) ptr; mRec->FID = popen( mRec->CMD, "wr" ); if( mRec->FID != NULL ){ char buffer[1024]; size_t len = 0; int d = fileno( mRec->FID ); fcntl( d, F_SETFL, O_NONBLOCK ); basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Started OK" ); while( ( !feof( mRec->FID ) ) && ( mRec->ThreadStatus < TH_ENDED ) ){ //len = read( d, buffer, 1024 ); sprintf( buffer, "%d ? %d", mRec->ThreadStatus, TH_ENDED ); basicLogCollection::getInstance()->Log( "FFMPEG", ptr, buffer ); if( len == -1 && errno == EAGAIN ){ SLEEP( 100 ); } else { if( len > -1 ){ SLEEP( 100 ); } else break; }; }; basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Finished" ); if(!(mRec->ThreadStatus < TH_ENDED)) { basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "by Flag!" ); fwrite( "q", 1, 1, mRec->FID ); }; pclose( mRec->FID ); basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "pclose" ); } else { basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Popen Failed!" ); } 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; LOG("Attachibg New Stream"); 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, ((tRec->tcp_connection)?"-rtsp_transport tcp":""), URI, NGINX_MEDIA_MANAGER_DEFAULT_FEED, md5 ); LOG( tRec->CMD ); 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; LOG("Start Media Manager Riper"); 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 ){ LOG("Stream Channel is Dead!"); void * ptr; int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr ); if( RetVal == 0 ){ pthread_detach( (*index)->m_sThreadHandler ); }; LOG("Restarting Stream Channel"); restartStream( *index ); }; }; }; m_oProtector.unlock(); SLEEP( NGINX_MEDIA_MANAGER_INTERVAL ); }; }; #undef LOG #undef LOG_ERROR