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.

189 lines
6.0 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. mediaNginxManager::~mediaNginxManager(){
  19. LOG("Destruct!");
  20. waitThreadEnd( true );
  21. LOG("Reaper Ended!");
  22. if( m_vStreams.size() > 0 ) {
  23. LOG("Have Channels!");
  24. for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ) {
  25. if( (*index)->ThreadStatus < TH_ENDED ) {
  26. void * ptr;
  27. char buffer[256];
  28. sprintf( buffer, "Channel[%p] is Live!", (*index) );
  29. LOG( buffer );
  30. (*index)->ThreadStatus = TH_ENDED;
  31. LOG("Channel set to End!");
  32. int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr );
  33. if( RetVal == 0 ){
  34. pthread_detach( (*index)->m_sThreadHandler );
  35. };
  36. LOG("Stream is Detached!");
  37. };
  38. };
  39. };
  40. };
  41. #define READ 0
  42. #define WRITE 1
  43. pid_t popen2( const char *command, int *infp, int *outfp ){
  44. int p_stdin[2], p_stdout[2];
  45. pid_t pid;
  46. if( pipe(p_stdin) != 0 || pipe(p_stdout) != 0 ) return -1;
  47. pid = fork();
  48. if( pid < 0 ){
  49. return pid;
  50. } else {
  51. if( pid == 0 ){
  52. close(p_stdin[WRITE]);
  53. dup2(p_stdin[READ], READ);
  54. close(p_stdout[READ]);
  55. dup2(p_stdout[WRITE], WRITE);
  56. execl("/bin/sh", "sh", "-c", command, NULL);
  57. perror("execl");
  58. exit(1);
  59. };
  60. };
  61. if( infp == NULL ){
  62. close(p_stdin[WRITE]);
  63. } else {
  64. *infp = p_stdin[WRITE];
  65. };
  66. if( outfp == NULL ){
  67. close(p_stdout[READ]);
  68. } else {
  69. *outfp = p_stdout[READ];
  70. };
  71. return pid;
  72. };
  73. #undef READ
  74. #undef WRITE
  75. static void *streamFeed( void *ptr ){
  76. MEDIA_MANAGER_RECORD * mRec;
  77. char * dummy[1024];
  78. mRec = (MEDIA_MANAGER_RECORD*) ptr;
  79. mRec->FID = popen( mRec->CMD, "wr" );
  80. if( mRec->FID != NULL ){
  81. char buffer[1024];
  82. size_t len = 0;
  83. int d = fileno( mRec->FID );
  84. fcntl( d, F_SETFL, O_NONBLOCK );
  85. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Started OK" );
  86. while( ( !feof( mRec->FID ) ) && ( mRec->ThreadStatus < TH_ENDED ) ){
  87. //len = read( d, buffer, 1024 );
  88. sprintf( buffer, "%d ? %d", mRec->ThreadStatus, TH_ENDED );
  89. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, buffer );
  90. if( len == -1 && errno == EAGAIN ){
  91. SLEEP( 100 );
  92. } else {
  93. if( len > -1 ){
  94. SLEEP( 100 );
  95. } else break;
  96. };
  97. };
  98. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Finished" );
  99. if(!(mRec->ThreadStatus < TH_ENDED)) {
  100. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "by Flag!" );
  101. fwrite( "q", 1, 1, mRec->FID );
  102. };
  103. pclose( mRec->FID );
  104. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "pclose" );
  105. } else {
  106. basicLogCollection::getInstance()->Log( "FFMPEG", ptr, "Popen Failed!" );
  107. }
  108. mRec->ThreadStatus = TH_EXIT;
  109. pthread_exit( ptr );
  110. return ptr;
  111. };
  112. bool mediaNginxManager::restartStream( MEDIA_MANAGER_RECORD *tRec ){
  113. if( tRec == NULL ) return false;
  114. tRec->ThreadStatus = TH_ARMED;
  115. int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
  116. if( RetVal == -1 ) {
  117. LOG_ERROR("Thread Creation Failed.");
  118. tRec->ThreadStatus = TH_EXIT;
  119. return false;
  120. };
  121. return false;
  122. };
  123. bool mediaNginxManager::attachStream( CSTR URI, bool TCP_CONNECTION, uint32_t id, CSTR md5 ){
  124. MEDIA_MANAGER_RECORD *tRec;
  125. LOG("Attachibg New Stream");
  126. tRec = (MEDIA_MANAGER_RECORD*) malloc( sizeof(MEDIA_MANAGER_RECORD));
  127. if( tRec == NULL ) return false;
  128. strncpy( tRec->URI, URI, 1024 );
  129. strncpy( tRec->md5, md5, 33 );
  130. tRec->FID = NULL;
  131. tRec->ThreadStatus = TH_WORKING;
  132. sprintf( tRec->CMD, NGINX_MEDIA_MANAGER_CMD, ((tRec->tcp_connection)?"-rtsp_transport tcp":""), URI, NGINX_MEDIA_MANAGER_DEFAULT_FEED, md5 );
  133. LOG( tRec->CMD );
  134. int RetVal = pthread_create( &tRec->m_sThreadHandler, NULL, streamFeed, ( void * ) tRec );
  135. if( RetVal == -1 ) {
  136. LOG_ERROR("Thread Creation Failed.");
  137. free( tRec );
  138. return false;
  139. };
  140. m_oProtector.lock();
  141. m_vStreams.push_back( tRec );
  142. m_oProtector.unlock();
  143. return true;
  144. };
  145. /* thread is for checking streams purposes */
  146. void mediaNginxManager::m_fThreadProc( void ){
  147. m_bThreadStatus = TH_WORKING;
  148. LOG("Start Media Manager Riper");
  149. while( m_bThreadStatus < TH_ENDED ){
  150. m_oProtector.lock();
  151. if( m_vStreams.size() ){
  152. for( vMEDIARECORD::iterator index = m_vStreams.begin(); index < m_vStreams.end(); index++ ){
  153. if( (*index)->ThreadStatus > TH_WORKING ){
  154. LOG("Stream Channel is Dead!");
  155. void * ptr;
  156. int RetVal = pthread_join( (*index)->m_sThreadHandler, &ptr );
  157. if( RetVal == 0 ){
  158. pthread_detach( (*index)->m_sThreadHandler );
  159. };
  160. LOG("Restarting Stream Channel");
  161. restartStream( *index );
  162. };
  163. };
  164. };
  165. m_oProtector.unlock();
  166. SLEEP( NGINX_MEDIA_MANAGER_INTERVAL );
  167. };
  168. };
  169. #undef LOG
  170. #undef LOG_ERROR