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.

174 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servletChannelWS.h"
  2. #include "../../components/base/basicFunctionality.h"
  3. #define LOG( X ) m_pLog->Log( "SWCHAN", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "SWCHAN", this, _LOG_ERROR, X )
  5. static bool _FFD_REGISTER_ALL_FLAG;
  6. bool servletChannelWS::initLibAV(void){
  7. if (!_FFD_REGISTER_ALL_FLAG){
  8. char Buffer[256];
  9. _FFD_REGISTER_ALL_FLAG = true;
  10. LOG("Init LIBAV");
  11. sprintf(Buffer, "LibAV Codec: %d.%d.%d", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO);
  12. LOG(Buffer);
  13. sprintf(Buffer, "LibAV Device: %d.%d.%d", LIBAVDEVICE_VERSION_MAJOR, LIBAVDEVICE_VERSION_MINOR, LIBAVDEVICE_VERSION_MICRO);
  14. LOG(Buffer);
  15. sprintf(Buffer, "LibAV Format: %d.%d.%d", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO);
  16. LOG(Buffer);
  17. sprintf(Buffer, "LibAV SWScale: %d.%d.%d", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO);
  18. LOG(Buffer);
  19. av_register_all();
  20. avcodec_register_all();
  21. avdevice_register_all();
  22. avformat_network_init();
  23. };
  24. return _FFD_REGISTER_ALL_FLAG;
  25. };
  26. servletChannelWS::servletChannelWS():basicThread(){
  27. m_pLog = basicLogCollection::getInstance();
  28. pthread_mutex_init( &new_pkt_mutex, NULL);
  29. pthread_cond_init( &new_pkt_cond, NULL);
  30. pthread_mutex_init( &fill_buff_mutex, NULL);
  31. pthread_cond_init( &fill_buff_cond, NULL);
  32. };
  33. servletChannelWS::~servletChannelWS(){
  34. basicThread::waitThreadEnd(true);
  35. };
  36. //rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
  37. char white_list_proto[] = "file,udp,tcp,rtp,rtsp";
  38. bool servletChannelWS::create( CSTR MediaURI ){
  39. LOG( MediaURI );
  40. if(!initLibAV()) {
  41. LOG_ERROR("LibAV INIT Error!");
  42. return false;
  43. }
  44. m_pFormatCtx = avformat_alloc_context();
  45. if( m_pFormatCtx == NULL ) return false;
  46. LOG("Format Context Allocated");
  47. m_pFormatCtx->protocol_whitelist = white_list_proto;
  48. LOG("White List is Set");
  49. int ret = avformat_open_input( &m_pFormatCtx, MediaURI , NULL, NULL);
  50. if ( ret != 0){
  51. //char tBuf[CTEXT];
  52. //av_strerror( ret, tBuf, CTEXT );
  53. LOG_ERROR( "Couldn't open file" );
  54. return false;
  55. };
  56. LOG("Media Opened");
  57. // Retrieve stream information
  58. if( avformat_find_stream_info(m_pFormatCtx,NULL) < 0 ){
  59. LOG_ERROR( "Couldn't find stream information" );
  60. return false;
  61. };
  62. LOG("Stream Info Found");
  63. // Find the first video stream
  64. m_iVideoStreamID = -1;
  65. for( unsigned int i = 0; i < m_pFormatCtx->nb_streams; i++ ){
  66. AVCodecContext *cc = m_pFormatCtx->streams[i]->codec;
  67. if( cc->codec_type==AVMEDIA_TYPE_VIDEO ){
  68. // don't care FF_DEBUG_VIS_MV_B_BACK
  69. cc->debug_mv = 0;//FF_DEBUG_VIS_MV_P_FOR | FF_DEBUG_VIS_MV_B_FOR;
  70. m_iVideoStreamID = i;
  71. break;
  72. };
  73. };
  74. if (m_iVideoStreamID == -1){
  75. LOG_ERROR("Didn't find a video stream");
  76. return false;
  77. }
  78. LOG("Video Stream Found");
  79. // Get a pointer to the codec context for the video stream
  80. m_pCodecCtx = m_pFormatCtx->streams[m_iVideoStreamID]->codec;
  81. m_pCodecCtx->refcounted_frames = 1;
  82. m_pCodecCtx->thread_count = 0;//NB_DEC_THREADS;
  83. //pCodecCtx->thread_type=2;
  84. // Find the decoder for the video stream
  85. m_pCodec = avcodec_find_decoder( m_pCodecCtx->codec_id );
  86. if (m_pCodec == NULL){
  87. LOG_ERROR("Codec not found");
  88. return false;
  89. };
  90. LOG("Codec Found");
  91. // Inform the codec that we can handle truncated bitstreams -- i.e.,
  92. // bitstreams where frame boundaries can fall in the middle of packets
  93. if (m_pCodec->capabilities & AV_CODEC_CAP_TRUNCATED) m_pCodecCtx->flags |= AV_CODEC_FLAG_TRUNCATED;
  94. // Open codec
  95. if (avcodec_open2(m_pCodecCtx, m_pCodec, NULL)<0)
  96. return -1; // Could not open codec
  97. LOG("Codec Opened");
  98. m_fFrameRate = 1*m_pCodecCtx->time_base.den/(m_pCodecCtx->time_base.num*m_pCodecCtx->ticks_per_frame);
  99. m_nWidth = m_pCodecCtx->width;
  100. m_nHeight = m_pCodecCtx->height;
  101. LOG("Starting Decoder Thread");
  102. return basicThread::init();
  103. };
  104. void servletChannelWS::m_fThreadProc( void ){
  105. AVPacket *dummy_packet;
  106. pid_t pid = gettid();
  107. char pidBuf[16];
  108. sprintf(pidBuf,"pid:%d", pid);
  109. LOG("Decoder Thread Started");
  110. LOG(pidBuf);
  111. m_bThreadStatus = TH_WORKING;
  112. while( m_bThreadStatus < TH_ENDED ){
  113. AVPacket *packet=(AVPacket*)av_malloc(sizeof(AVPacket));
  114. if( packet != NULL ) {
  115. packet->stream_index = -1;
  116. do{
  117. av_init_packet(packet);
  118. //av_free_packet(&packet);
  119. packet->data = NULL;
  120. int err = av_read_frame( m_pFormatCtx, packet );
  121. if( err < 0 ){
  122. //pthread_exit(NULL);
  123. //return 0 ;
  124. if( b_streaming == false ){
  125. av_seek_frame( m_pFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD );
  126. packet->stream_index = -1;
  127. };
  128. //fprintf(stderr, "#################### =======> av_seek_frame");
  129. continue;
  130. };
  131. } while( packet->stream_index != m_iVideoStreamID );
  132. pthread_mutex_lock(&fill_buff_mutex);
  133. //if(Pkt_buf.size()<PKT_BUF_LEN)
  134. b_fill_buf = m_qMediaBus.size() < PKT_BUF_LEN;
  135. if( b_streaming == false ){
  136. while( b_fill_buf == 0 ) pthread_cond_wait(&fill_buff_cond, &fill_buff_mutex);
  137. } else {
  138. if( b_fill_buf == 0 ){// if streaming mode is true, the receiving thread is never blocked and some packets can be dropped
  139. dummy_packet = m_qMediaBus.front();
  140. m_qMediaBus.pop();
  141. av_packet_unref( dummy_packet );
  142. };
  143. };
  144. //AVPacket *packet = av_malloc(sizeof(AVPacket));
  145. //av_frame_ref (pFrame_ref,decode_th_data->pFrame);
  146. m_qMediaBus.push( packet );
  147. pthread_mutex_unlock(&fill_buff_mutex);
  148. pthread_mutex_lock(&new_pkt_mutex);
  149. b_new_pkt=true;
  150. pthread_cond_signal( &new_pkt_cond );
  151. pthread_mutex_unlock(&new_pkt_mutex);
  152. };
  153. };
  154. };
  155. #undef LOG
  156. #undef LOG_ERROR