#include "servletChannelWebRTC.h" #include "../../components/base/basicFunctionality.h" #define LOG( X ) m_pLog->Log( "SWCHAN", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "SWCHAN", this, _LOG_ERROR, X ) static bool _FFD_REGISTER_ALL_FLAG; bool servletChannelWebRTC::initLibAV(void){ if (!_FFD_REGISTER_ALL_FLAG){ char Buffer[256]; _FFD_REGISTER_ALL_FLAG = true; LOG("Init LIBAV"); sprintf(Buffer, "LibAV Codec: %d.%d.%d", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO); LOG(Buffer); sprintf(Buffer, "LibAV Device: %d.%d.%d", LIBAVDEVICE_VERSION_MAJOR, LIBAVDEVICE_VERSION_MINOR, LIBAVDEVICE_VERSION_MICRO); LOG(Buffer); sprintf(Buffer, "LibAV Format: %d.%d.%d", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO); LOG(Buffer); sprintf(Buffer, "LibAV SWScale: %d.%d.%d", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO); LOG(Buffer); av_register_all(); avcodec_register_all(); avdevice_register_all(); avformat_network_init(); }; return _FFD_REGISTER_ALL_FLAG; }; servletChannelWebRTC::servletChannelWebRTC():basicThread(){ m_pLog = basicLogCollection::getInstance(); pthread_mutex_init( &new_pkt_mutex, NULL); pthread_cond_init( &new_pkt_cond, NULL); pthread_mutex_init( &fill_buff_mutex, NULL); pthread_cond_init( &fill_buff_cond, NULL); }; servletChannelWebRTC::~servletChannelWebRTC(){ basicThread::waitThreadEnd(true); }; //rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1 char white_list_proto[] = "file,udp,tcp,rtp,rtsp"; bool servletChannelWebRTC::create( CSTR MediaURI ){ LOG( MediaURI ); if(!initLibAV()) { LOG_ERROR("LibAV INIT Error!"); return false; } m_pFormatCtx = avformat_alloc_context(); if( m_pFormatCtx == NULL ) return false; LOG("Format Context Allocated"); m_pFormatCtx->protocol_whitelist = white_list_proto; LOG("White List is Set"); int ret = avformat_open_input( &m_pFormatCtx, MediaURI , NULL, NULL); if ( ret != 0){ //char tBuf[CTEXT]; //av_strerror( ret, tBuf, CTEXT ); LOG_ERROR( "Couldn't open file" ); return false; }; LOG("Media Opened"); // Retrieve stream information if( avformat_find_stream_info(m_pFormatCtx,NULL) < 0 ){ LOG_ERROR( "Couldn't find stream information" ); return false; }; LOG("Stream Info Found"); // Find the first video stream m_iVideoStreamID = -1; for( unsigned int i = 0; i < m_pFormatCtx->nb_streams; i++ ){ AVCodecContext *cc = m_pFormatCtx->streams[i]->codec; if( cc->codec_type==AVMEDIA_TYPE_VIDEO ){ // don't care FF_DEBUG_VIS_MV_B_BACK cc->debug_mv = 0;//FF_DEBUG_VIS_MV_P_FOR | FF_DEBUG_VIS_MV_B_FOR; m_iVideoStreamID = i; break; }; }; if (m_iVideoStreamID == -1){ LOG_ERROR("Didn't find a video stream"); return false; } LOG("Video Stream Found"); // Get a pointer to the codec context for the video stream m_pCodecCtx = m_pFormatCtx->streams[m_iVideoStreamID]->codec; m_pCodecCtx->refcounted_frames = 1; m_pCodecCtx->thread_count = 0;//NB_DEC_THREADS; //pCodecCtx->thread_type=2; // Find the decoder for the video stream m_pCodec = avcodec_find_decoder( m_pCodecCtx->codec_id ); if (m_pCodec == NULL){ LOG_ERROR("Codec not found"); return false; }; LOG("Codec Found"); // Inform the codec that we can handle truncated bitstreams -- i.e., // bitstreams where frame boundaries can fall in the middle of packets if (m_pCodec->capabilities & CODEC_CAP_TRUNCATED) m_pCodecCtx->flags |= CODEC_FLAG_TRUNCATED; // Open codec if (avcodec_open2(m_pCodecCtx, m_pCodec, NULL)<0) return -1; // Could not open codec LOG("Codec Opened"); m_fFrameRate = 1*m_pCodecCtx->time_base.den/(m_pCodecCtx->time_base.num*m_pCodecCtx->ticks_per_frame); m_nWidth = m_pCodecCtx->width; m_nHeight = m_pCodecCtx->height; LOG("Starting Decoder Thread"); return basicThread::init(); }; void servletChannelWebRTC::m_fThreadProc( void ){ AVPacket *dummy_packet; pid_t pid = gettid(); char pidBuf[16]; sprintf(pidBuf,"pid:%d", pid); LOG("Decoder Thread Started"); LOG(pidBuf); m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ AVPacket *packet=(AVPacket*)av_malloc(sizeof(AVPacket)); if( packet != NULL ) { packet->stream_index = -1; do{ av_init_packet(packet); //av_free_packet(&packet); packet->data = NULL; int err = av_read_frame( m_pFormatCtx, packet ); if( err < 0 ){ //pthread_exit(NULL); //return 0 ; if( b_streaming == false ){ av_seek_frame( m_pFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD ); packet->stream_index = -1; }; //fprintf(stderr, "#################### =======> av_seek_frame"); continue; }; } while( packet->stream_index != m_iVideoStreamID ); pthread_mutex_lock(&fill_buff_mutex); //if(Pkt_buf.size()pFrame); m_qMediaBus.push( packet ); pthread_mutex_unlock(&fill_buff_mutex); pthread_mutex_lock(&new_pkt_mutex); b_new_pkt=true; pthread_cond_signal( &new_pkt_cond ); pthread_mutex_unlock(&new_pkt_mutex); }; }; }; #undef LOG #undef LOG_ERROR