#include "videoAVLIBSource.h" #include "libavutil/error.h" #define LOG( X ) m_pLog->Log( "LibAV_SRC", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "LibAV_SRC", this, _LOG_ERROR, X ) videoAVLIBSource::videoAVLIBSource(): basicMediaSource() { m_bSizeIsAutomatic = true; m_nWidth = 640; m_nHeight = 360; if (!initLibAV()) return; m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState)); if( m_pVideoState == NULL ){ LOG_ERROR("VideoState Memory Allocation Error"); return; }; m_pVideoState->m_bStatus = MS_UNDEFINED; m_pVideoState->m_pFormatCtx = avformat_alloc_context(); if( m_pVideoState->m_pFormatCtx == NULL ){ LOG_ERROR("VideoFormat Context Memory Allocation Error"); return; }; m_pFrame = NULL; m_pFrameBGR = NULL; img_convert_ctx = NULL; m_pVideoState->m_iVideoStreamID = -1; #ifndef _NOAUDIO_ m_pVideoState->m_iAudioStreamID = -1; #endif m_pVideoState->m_bStatus = MS_INIT; }; videoAVLIBSource::videoAVLIBSource(char *Source) : basicMediaSource(){ m_bStreamingSource = true; m_bSizeIsAutomatic = true; m_nWidth = 640; m_nHeight = 360; if( !initLibAV() ) return; m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState)); if (m_pVideoState == NULL){ LOG_ERROR("VideoState Memory Allocation Error"); return; }; m_pVideoState->m_bStatus = MS_UNDEFINED; m_pVideoState->m_pFormatCtx = avformat_alloc_context(); if (m_pVideoState->m_pFormatCtx == NULL){ LOG_ERROR("VideoCodec Context Memory Allocation Error"); return; }; m_pVideoState->m_pFormatCtx->video_codec_id = AV_CODEC_ID_MJPEG; m_pVideoState->m_iVideoStreamID = -1; #ifndef _NOAUDIO_ m_pVideoState->m_iAudioStreamID = -1; #endif m_pVideoState->m_bStatus = MS_INIT; _open( Source ); }; videoAVLIBSource::~videoAVLIBSource(){ if (m_pVideoState != NULL){ if( m_pVideoState->m_bStatus > MS_INIT ){ m_bThreadStatus = TH_ENDED; while (m_pVideoState->m_bStatus == MS_PRESENT) #ifdef __linux__ usleep(1000); #else Sleep(1); #endif #ifndef _NOAUDIO_ if( m_pVideoState->m_pAudioCtx!=NULL ){ avcodec_close( m_pVideoState->m_pAudioCtx ); //avcodec_free_context(&m_pVideoState->m_pAudioCtx); }; #endif if( m_pVideoState->m_pCodecCtx != NULL ){ avcodec_close(m_pVideoState->m_pCodecCtx); //avcodec_free_context(&m_pVideoState->m_pCodecCtx); }; if( m_pVideoState->m_pFormatCtx != NULL ){ avformat_close_input(&m_pVideoState->m_pFormatCtx); //avformat_free_context(m_pVideoState->m_pFormatCtx); }; av_free(m_pVideoState); if( m_pFrame!= NULL ) av_frame_free(&m_pFrame); #ifdef PROCESS_YUV420P av_frame_free( &m_pFrameYUV ); #endif #ifdef PROCESS_BGR24 if (m_pFrameBGR != NULL) av_frame_free(&m_pFrameBGR); #endif //av_free( m_pOut_buffer ); if( img_convert_ctx != NULL ) sws_freeContext(img_convert_ctx); }; }; }; bool videoAVLIBSource::setSize(int Width, int Height){ m_bSizeIsAutomatic = false; return true; }; bool videoAVLIBSource::setProportionalByWidth(int Width){ m_bSizeIsAutomatic = false; return true; }; bool videoAVLIBSource::setProportionalByHeight(int Height){ m_bSizeIsAutomatic = false; return true; }; bool videoAVLIBSource::setFrameAspectRatio(FrameAspectRatio AspectRatio){ return true; }; bool videoAVLIBSource::_close(void){ if (m_pVideoState == NULL) return true; if (m_pVideoState->m_bStatus < MS_OPENED) return true; m_pVideoState->m_bStatus = MS_CLOSING; while( m_pVideoState->m_bStatus != MS_CLOSED ) SLEEP(2); if (m_pVideoState->m_bStatus > MS_ALOCATED){ av_frame_free(&m_pFrame); #ifdef PROCESS_YUV420P av_frame_free(&m_pFrameYUV); #endif #ifdef PROCESS_BGR24 av_frame_free(&m_pFrameBGR); #endif sws_freeContext(img_convert_ctx); #ifndef _NOAUDIO_ if (m_pVideoState->m_pAudioCtx != NULL){ avcodec_close(m_pVideoState->m_pAudioCtx); //avcodec_free_context(&m_pVideoState->m_pAudioCtx); }; #endif if (m_pVideoState->m_pCodecCtx != NULL){ avcodec_close(m_pVideoState->m_pCodecCtx); //avcodec_free_context(&m_pVideoState->m_pCodecCtx); avformat_close_input(&m_pVideoState->m_pFormatCtx); //av_free(m_pVideoState->m_pVideoCodec); m_pVideoState->m_iVideoStreamID = -1; }; }; m_pVideoState->m_bStatus = MS_INIT; return true; }; bool videoAVLIBSource::_stop(void){ if ( _close() ) basicThread::waitThreadEnd(); else return false; return true; }; //https://www.youtube.com/watch?v=Lu3LZmhC_DY bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, bool scaled, int s_width, int s_height ){ if (m_pVideoState == NULL) return false; if (m_pVideoState->m_bStatus == MS_INIT){ char Buffer[256] = {0}; char tChar[128] = {0}; sprintf(m_pCookie, "%s", MD5String(URI)); m_pVideoState->m_bStatus = MS_OPENING; if( strncmp( URI, "video=", 6 ) == 0 ){ // MS-Video Device AVDictionary *options = NULL; if( width != -1 && height != -1 ){ m_bSizeIsAutomatic = false; char tChar[10]; sprintf(tChar, "%dx%d", width, height); av_dict_set(&options, "video_size", tChar, 0); } else { m_bSizeIsAutomatic = true; }; //av_dict_set(&options, "pixel_format", "yuyv422", 0 ); av_dict_set(&options, "video_codec", "mjpeg", 0 ); if( inputPin != -1 ){ sprintf(tChar, "%d", inputPin); av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0); }; if( framerate != -1 ){ sprintf(tChar, "%f", framerate); av_dict_set(&options, "framerate", tChar, 0); }; av_dict_set(&options, "rtbufsize", "500000k", 0); if( ShowDeviceMenu ){ av_dict_set(&options, "show_video_device_dialog", "true", 0); av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0); //sprintf(Buffer, "data/%s.settings", m_pCookie); //av_dict_set(&options, "video_device_save", Buffer, 0); #ifndef _NOAUDIO_ av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0); sprintf(Buffer, "%s-a.settings", URI); av_dict_set(&options, "audio_device_save", Buffer, 0); #endif } else { sprintf(Buffer, "data/%s.settings", m_pCookie ); //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0); }; //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0); //av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0); //av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0); AVInputFormat *iformat = av_find_input_format("dshow"); //av_dict_set(&options, "pixel_format", "bgr24", 0); av_dict_set(&options, "timeout", "5000", 0); //AVDeviceInfoList *device_list; //m_pVideoState->m_pFormatCtx->iformat = iformat; //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list); //avdevice_free_list_devices(&device_list); if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){ av_dict_free(&options); av_free( iformat ); sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI); LOG_ERROR(Buffer); m_pVideoState->m_bStatus = MS_INIT; return false; }; /*AVDictionaryEntry *e; if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) { sprintf(Buffer, "Option %s not recognized by the demuxer", e->key); LOG_ERROR(Buffer); };*/ //if (iformat!=NULL) //if (iformat->priv_class != NULL){ // const AVOption *cOption = NULL; // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL ) // serializeOption(cOption); //}; av_dict_free(&options); AVOption *mpOption = NULL; } else { // Linux Video Device Or IP Stream Source AVDictionary *options = NULL; if( width != -1 && height != -1 ){ m_bSizeIsAutomatic = false; sprintf(tChar, "%dx%d", width, height); av_dict_set(&options, "video_size", tChar, 0); } else { m_bSizeIsAutomatic = true; }; //av_dict_set(&options, "rtsp_transport", "tcp", 0); av_dict_set(&options, "max_delay", "300000", 0); //av_dict_set(&options, "stimeout", "1000000", 0); av_dict_set(&options, "fflags", "nobuffer", 0); //av_dict_set(&options, "analyzeduration", "1000000", 0); if( framerate != -1 ){ sprintf(tChar, "%0.2f", framerate); //sprintf(tChar, "120" ); av_dict_set(&options, "framerate", tChar, 0); //av_dict_set(&options, "frame_rate", tChar, 0); }; //av_dict_set(&options, "pixel_format", "mjpeg", 0); //av_dict_set(&options, "timeout", "8000", 0); int RetVal = 0; if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, NULL, &options) ) != 0 ){ av_dict_free(&options); char tBuffer[1024]; av_strerror( RetVal, tBuffer, 1024 ); sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, URI); LOG_ERROR(Buffer); return false; }; AVDictionaryEntry *e; if( e = av_dict_get( options, "", NULL, AV_DICT_IGNORE_SUFFIX )){ sprintf(Buffer, "Option %s not recognized by the demuxer", e->key); LOG_ERROR(Buffer); }; av_dict_free(&options); }; //DataStream Opened ! m_pVideoState->m_bStatus = MS_OPENED; if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){ sprintf(Buffer, "Couldn't find stream information: %s", URI); LOG_ERROR(Buffer); return false; }; #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ) //LOG("YABABA-1!"); for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){ //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){ if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){ LOG( "VIDEO stream detected." ); if( m_pVideoState->m_iVideoStreamID <0 ){ LOG( "VIDEO stream assigned." ); m_pVideoState->m_iVideoStreamID=i; }; }; # ifndef _NOAUDIO_ if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) { LOG( "AUDIO stream detected." ); if( m_pVideoState->m_iAudioStreamID < 0 ) { LOG( "AUDIO stream assigned." ); m_pVideoState->m_iAudioStreamID=i; } }; # endif }; if( m_pVideoState->m_iVideoStreamID == -1 ){ LOG_ERROR( "Didn't find a video stream."); return false; }; # ifndef _NOAUDIO_ if( m_pVideoState->m_iAudioStreamID == -1 ){ LOG_ERROR("Didn't find a audio stream."); return false; }; # endif // Stream found m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec; //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id ); #else //LOG("YABABA-2!"); /* find the video stream information */ m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0); if (m_pVideoState->m_iVideoStreamID == -1){ LOG_ERROR("Didn't find a video stream."); return false; }; # ifndef _NOAUDIO_ if (m_pVideoState->m_iAudioStreamID == -1){ LOG_ERROR("Didn't find a audio stream."); return false; }; # endif if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){ LOG_ERROR("Can not create decoder context!"); return false; }; m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]; if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){ LOG_ERROR("Can not set Context parameters!"); return false; }; #endif if( m_pVideoState->m_pVideoCodec == NULL ){ //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI); sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI); LOG_ERROR( Buffer ); return false; }; if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){ sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI ); LOG_ERROR( Buffer ); return false; }; #ifndef _NOAUDIO_ m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec; m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id ); if( !m_pVideoState->m_pAudioCodec ){ LOG_ERROR( "Unsupported Audio codec!" ); return false; }; if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){ //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id ); LOG_ERROR( "Could not open Audio codec." ); return false; }; #endif m_pFrame = av_frame_alloc(); #ifdef PROCESS_YUV420P m_pFrameYUV = av_frame_alloc(); #endif #ifdef PROCESS_BGR24 m_pFrameBGR = av_frame_alloc(); #endif if( scaled ){ m_nWidth = s_width; m_nHeight = s_height; } else { if (m_bSizeIsAutomatic){ m_nWidth = m_pVideoState->m_pCodecCtx->width; m_nHeight = m_pVideoState->m_pCodecCtx->height; } else { m_nWidth = width; m_nHeight = height; }; }; #ifdef PROCESS_YUV420P m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_YUV420P, m_nWidth , m_nHeight , BUFFER_ALIGN )); avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight ); #endif #ifdef PROCESS_BGR24 m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN )); avpicture_fill( (AVPicture *) m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight ); //av_image_fill_arrays(m_pFrameBGR->data, m_pFrameBGR->linesize, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, 1); //m_pMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) ); //m_pOMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) ); #endif m_pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) ); // Printout FF //av_dump_format(m_pFormatCtx,0,filepath,0); #ifdef PROCESS_YUV420P img_convert_ctx = sws_getContext( m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL ); #endif #ifdef PROCESS_BGR24 img_convert_ctx = sws_getContext( m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL ); //img_convert_ctx = sws_getContext(m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); #endif m_pVideoState->m_bStatus = MS_ALOCATED; // All seems to be good -> allocating mem //LOG("YABABA!!!!"); if( m_bThreadStatus < TH_WORKING ) init(); //LOG("YABABA232!"); return true; }; return false; }; #if !(( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )) // The flush packet is a non-NULL packet with size 0 and data NULL int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt){ int ret; *got_frame = 0; if (pkt){ ret = avcodec_send_packet(avctx, pkt); // In particular, we don't expect AVERROR(EAGAIN), because we read all // decoded frames with avcodec_receive_frame() until done. if (ret < 0) return ret == AVERROR_EOF ? 0 : ret; }; ret = avcodec_receive_frame(avctx, frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret; if (ret >= 0) *got_frame = 1; return 0; }; #endif void videoAVLIBSource::m_fThreadProc( void ){ int RetVal; int bGotPicture = 0; m_bThreadStatus = TH_WORKING; LOG("Reader Thread Started."); //cv::setBreakOnError( true ); m_pVideoState->m_bStatus = MS_STOPED; while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){ SLEEP(20); }; LOG("Presentation Started."); //m_pVideoState->m_bStatus = MS_PRESENT; while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){ if ( m_pMediaDestination != NULL ){ if( av_read_frame( m_pVideoState->m_pFormatCtx, m_pPacket ) >= 0 ){ if( m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){ # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ) RetVal = avcodec_decode_video2(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket); # else RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket); #endif if( RetVal < 0 ){ LOG_ERROR( "MediaSource::DecoderProc - Decode Error." ); m_bThreadStatus = TH_ENDED; m_pVideoState->m_bStatus = MS_STOPED; continue; }; if( m_bThreadStatus == TH_ENDED ){ m_pVideoState->m_bStatus = MS_STOPED; continue; }; if (bGotPicture){ #ifdef PROCESS_YUV420P sws_scale( img_convert_ctx, (const uint8_t* const*) m_pFrame->data, m_pFrame->linesize, 0, m_pCodecCtx->height, m_pFrameYUV->data, m_pFrameYUV->linesize ); SDL_UpdateYUVTexture( m_pTexture, &m_oRect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2] ); #endif #ifdef PROCESS_BGR24 sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize); try{ # ifdef USE_QVideoFrame QVideoFrame *tFrame; tFrame = new QVideoFrame(m_pFrameBGR->linesize[0] * m_nHeight, QSize(m_nHeight, m_nWidth), m_pFrameBGR->linesize[0], QVideoFrame::Format_BGR24); memcpy(tFrame->bits(), m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight); if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(tFrame, me ); # else cv::Mat *mRGB; mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3); memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight); if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(mRGB, this); delete(mRGB); # endif // !USE_QVideoFrame } catch (cv::Exception & e) { LOG_ERROR(e.msg.c_str()); // output exception message }; #endif //if(!m_bStreamingSource) SLEEP( m_pFrame->pkt_duration / 1000 ); }; }; # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ) av_free_packet( m_pPacket ); # else av_packet_unref(m_pPacket); # endif } else { // Read Tale of data while(1){ # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ) RetVal = avcodec_decode_video2( m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket ); # else RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket); # endif if( RetVal < 0 ) break; if( !bGotPicture ) break; if( m_bThreadStatus == TH_ENDED ){ m_pVideoState->m_bStatus = MS_STOPED; break; }; #ifdef PROCESS_YUV420P sws_scale(me->img_convert_ctx, (const uint8_t* const*)me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize); SDL_UpdateYUVTexture( m_pTexture, &m_oRect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2] ); #endif #ifdef PROCESS_BGR24 sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize); try{ cv::Mat *mRGB; mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3); memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight); if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame( mRGB, this ); delete(mRGB); } catch (cv::Exception & e) { LOG_ERROR(e.msg.c_str()); // output exception message }; #endif }; LOG("Video Stream Ended"); # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ) av_free_packet(m_pPacket); # else av_packet_unref(m_pPacket); # endif m_pVideoState->m_bStatus = MS_CLOSED; m_bThreadStatus = TH_ENDED; }; }else{ m_pVideoState->m_bStatus = MS_CLOSED; #ifdef __linux__ usleep( 20000 ); #else Sleep(20); #endif }; }; m_pVideoState->m_bStatus = MS_CLOSED; m_bThreadStatus = TH_ENDED; LOG("Thread Ended."); }; #undef LOG #undef LOG_ERROR