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.

93 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "mediaNginxStreamingProxyManager.h"
  2. #define LOG( X ) m_pLog->Log( "NGINX_MEDIA", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "NGINX_MEDIA", this, _LOG_ERROR, X )
  4. mediaNginxManager *mediaNginxManager::_instance = NULL;
  5. mediaNginxManager *mediaNginxManager::getInstance(){
  6. // check if the instance has been created yet
  7. if( 0 == _instance ){
  8. // if not, then create it
  9. _instance = new mediaNginxManager;
  10. };
  11. // return the single instance
  12. return _instance;
  13. };
  14. mediaNginxManager::mediaNginxManager(): basicThread(){
  15. m_pLog = basicLogCollection::getInstance();
  16. basicThread::init();
  17. };
  18. static void *streamFeed( void *ptr ){
  19. MEDIA_MANAGER_RECORD * mRec;
  20. mRec = (MEDIA_MANAGER_RECORD*) ptr;
  21. mRec->FID = popen( mRec->CMD, "r" );
  22. if( mRec->FID!= NULL ){
  23. while( !feof( mRec->FID ) ){
  24. };
  25. pclose( mRec->FID );
  26. };
  27. mRec->ThreadStatus = TH_EXIT;
  28. pthread_exit( ptr );
  29. return ptr;
  30. };
  31. bool mediaNginxManager::restartStream( MEDIA_MANAGER_RECORD *tRec ){
  32. if( tRec == NULL ) return false;
  33. tRec->ThreadStatus = TH_ARMED;
  34. int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
  35. if( RetVal == -1 ) {
  36. LOG_ERROR("Thread Creation Failed.");
  37. tRec->ThreadStatus = TH_EXIT;
  38. return false;
  39. };
  40. return false;
  41. };
  42. bool mediaNginxManager::attachStream( CSTR URI, bool TCP_CONNECTION, uint32_t id, CSTR md5 ){
  43. MEDIA_MANAGER_RECORD *tRec;
  44. tRec = (MEDIA_MANAGER_RECORD*) malloc( sizeof(MEDIA_MANAGER_RECORD));
  45. if( tRec == NULL ) return false;
  46. strncpy( tRec->URI, URI, 1024 );
  47. strncpy( tRec->md5, md5, 33 );
  48. tRec->FID = NULL;
  49. tRec->ThreadStatus = TH_WORKING;
  50. sprintf( tRec->CMD, NGINX_MEDIA_MANAGER_CMD, ((mRec->tcp_connection)?"-rtsp_transport tcp":""), URI, NGINX_MEDIA_MANAGER_DEFAULT_FEED, md5 );
  51. int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
  52. if( RetVal == -1 ) {
  53. LOG_ERROR("Thread Creation Failed.");
  54. free( tRec );
  55. return false;
  56. };
  57. m_oProtector.lock();
  58. m_vStreams.push_back( tRec );
  59. m_oProtector.unlock();
  60. return true;
  61. };
  62. /* thread is for checking streams purposes */
  63. void mediaNginxManager::m_fThreadProc( void ){
  64. m_bThreadStatus = TH_WORKING;
  65. while( m_bThreadStatus < TH_ENDED ){
  66. m_oProtector.lock();
  67. if( m_vStreams.size() ){
  68. for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ){
  69. if( (*index)->ThreadStatus > TH_WORKING ){
  70. void * ptr;
  71. int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr );
  72. if( RetVal == 0 ){
  73. pthread_detach( (*index)->m_sThreadHandler );
  74. };
  75. restartStream( *index );
  76. };
  77. };
  78. };
  79. m_oProtector.unlock();
  80. SLEEP( NGINX_MEDIA_MANAGER_INTERVAL );
  81. };
  82. };
  83. #undef LOG
  84. #undef LOG_ERROR