diff --git a/Projects/la_mojo/build b/Projects/la_mojo/build index 0d3905e9..59699e61 100755 --- a/Projects/la_mojo/build +++ b/Projects/la_mojo/build @@ -1,11 +1,11 @@ #! /bin/sh -make clean -e "CFG=Debug" -make -e "CFG=Debug" -j6 +#make clean -e "CFG=Debug" +#make -e "CFG=Debug" -j6 -#make clean -e "CFG=Debug86" -#make -e "CFG=Debug86" -j4 +make clean -e "CFG=Debug86" +make -e "CFG=Debug86" -j4 #make clean -e "CFG=Release86" #make -e "CFG=Release86" -j4 diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 15ff6403..588d515f 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -3420,12 +3420,17 @@ bool streamHandler::triggerCamMove( void ){ bool streamHandler::validate( void ){ if ( m_pSource ) { - if( m_pSource->getStatus() < MS_CLOSING ) + if( m_pSource->getStatus() < MS_CLOSING ){ #ifdef EVE_ENABLE_VIDEO - if( m_bThreadStatus < TH_ENDED ) + if( m_nGlobalFrameCounter ) { //Frames Received from a source + if( m_nGlobalFrameCounter == m_nValidatorFrameCounter ) { + // Video Stalled! + return false;// Try use System to recuperate + } else m_nValidatorFrameCounter = m_nGlobalFrameCounter; + }; + if( m_bThreadStatus < TH_ENDED ) return true; #endif - return true; - + }; }; return false; }; diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index 100769c3..6d3d5371 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -276,7 +276,8 @@ class streamHandler: - uint32_t m_nGlobalFrameCounter; + uint64_t m_nGlobalFrameCounter; + uint64_t m_nValidatorFrameCounter; //uint32_t m_nVideoRegisteredDBID; uint32_t m_nSegAFrameCounter; uint32_t m_nSegBFrameCounter; diff --git a/lib/components/base/basicMediaSource.h b/lib/components/base/basicMediaSource.h index 7c2a84ea..c751f2d4 100644 --- a/lib/components/base/basicMediaSource.h +++ b/lib/components/base/basicMediaSource.h @@ -25,11 +25,12 @@ class basicMediaSource { return false; }; bool isAttached( void ) { return ( m_pMediaDestination != NULL ); }; - void setStreaming(bool T){ m_bStreamingSource = T; }; + void setStreaming( bool T ){ m_bStreamingSource = T; }; virtual bool _open( CSTR URI, bool ShowDeviceMenu = false, float framerate=-1.0f, int inputPin = -1, int width = -1, int height = -1 , int s_width=-1, int s_height=-1 ){ return false; }; virtual bool _close( void ){ return true; }; - virtual bool _stop(void){ return true; }; - MediaStateStatus getStatus( void ){ return m_eMediaStatus;}; + virtual bool _stop( void ){ return true; }; + virtual bool _restart( void ){ return true; }; + MediaStateStatus getStatus( void ){ return m_eMediaStatus; }; }; inline basicMediaSource::basicMediaSource(){ diff --git a/lib/components/base/basicThread.cpp b/lib/components/base/basicThread.cpp index 1931714b..5f235551 100644 --- a/lib/components/base/basicThread.cpp +++ b/lib/components/base/basicThread.cpp @@ -76,15 +76,19 @@ void basicThread::waitThreadEnd(bool forced){ }; bool basicThread::uninit( void ){ if( m_bThreadStatus == TH_PRE_INIT ) return true; - while( m_bThreadStatus < TH_WORKING ) SLEEP( DEFAULT_THREAD_SLEEP_TIME ); - m_bThreadStatus = TH_ENDED; - while( m_bThreadStatus < TH_EXIT ) SLEEP( DEFAULT_THREAD_SLEEP_TIME ); + //If Thread about to start -> Wait till it starts + if( m_bThreadStatus == TH_ARMED ) while( m_bThreadStatus < TH_WORKING ) SLEEP( DEFAULT_THREAD_SLEEP_TIME ); + + if( m_bThreadStatus == TH_WORKING ) m_bThreadStatus = TH_ENDED; + + if( m_bThreadStatus > TH_WORKING ){ #if defined( __linux__ ) || defined(WIN_PTHREAD) - pthread_join(m_sThreadHandler, NULL); + pthread_join(m_sThreadHandler, NULL); #else - WaitForSingleObject(m_hThreadHandle, DEFAULT_THREAD_SLEEP_TIME); - CloseHandle(m_hThreadHandle); + WaitForSingleObject(m_hThreadHandle, DEFAULT_THREAD_SLEEP_TIME); + CloseHandle(m_hThreadHandle); #endif + }; m_bThreadStatus = TH_PRE_INIT; return true; }; diff --git a/lib/data/media/video/videoAVLIBSource.cpp b/lib/data/media/video/videoAVLIBSource.cpp index 6ce5a358..d2531ee6 100644 --- a/lib/data/media/video/videoAVLIBSource.cpp +++ b/lib/data/media/video/videoAVLIBSource.cpp @@ -199,69 +199,235 @@ bool videoAVLIBSource::_stop(void){ }; //https://www.youtube.com/watch?v=Lu3LZmhC_DY +bool videoAVLIBSource::_restart( void ){ + if (m_pVideoState == NULL) return false; + if (m_pVideoState->m_bStatus < MS_OPENED) return false; + // Stop Thread + basicThread::uninit(); + int RetVal; + switch( m_bSrcType ){ + case AVLIB_SOURCE_DDSHOW:{ + AVInputFormat *iformat = av_find_input_format("dshow"); + if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) ) != 0 ){ + char tB[1024]; + //av_free( iformat ); + char tBuffer[1024]; + av_strerror( RetVal, tBuffer, 1024 ); + sprintf(tB, "VideoSource Couldn't open input device[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName ); + LOG_ERROR(tB); + m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT; + return false; + }; + break; + }; + case AVLIB_SOURCE_V4L2: + case AVLIB_SOURCE_IPSTREAM:{ + if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){ + //av_dict_free(&options); + char tB[1024]; + char tBuffer[1024]; + av_strerror( RetVal, tBuffer, 1024 ); + sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName ); + LOG_ERROR( tB ); + m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT; + return false; + }; + break; + }; + }; +#ifdef DEBUG + LOG("DataStream Opened"); +#endif + m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED; + + if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){ + char tB[1024]; + sprintf( tB, "Couldn't find stream information: %s", m_pVideoState->m_pFileName ); + LOG_ERROR( tB ); + return false; + }; + +#if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 ) + 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 +# ifdef DEBUG_EXTRA + LOG("YABABA-2!"); +# endif + /* 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 ){ + char tB[1024]; + //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName); + sprintf( tB, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName ); + LOG_ERROR( tB ); + return false; + }; + /* + End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2()) + and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE). + Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool. + */ + m_pVideoState->m_pCodecCtx->thread_count = 4; + //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME; + //if (is_live) + m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE; + //else + //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME; +#ifdef DEBUG_EXTRA + LOG("Before Open2"); +#endif + if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){ + char tB[1024]; + sprintf( tB, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName ); + LOG_ERROR( tB ); + return false; + }; + + /* we do not send complete frames */ + //if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED; + +#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 + return basicThread::init(); +}; + bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height ){ - if (m_pVideoState == NULL) return false; + if (URI == NULL) return false; + if (m_pVideoState == NULL) return false; if (m_bThreadStatus > TH_PRE_INIT ) return false; if (m_pVideoState->m_bStatus == MS_INIT) { char Buffer[1024] = {0}; char tChar[128] = {0}; - sprintf( m_pCookie, "%s", MD5String(URI) ); + strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN ); + sprintf( m_pCookie, "%s", MD5String(m_pVideoState->m_pFileName) ); m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING; - if( strncmp( URI, "video=", 6 ) == 0 ){ + if( strncmp( m_pVideoState->m_pFileName, "video=", 6 ) == 0 ){ m_bSrcType = AVLIB_SOURCE_DDSHOW; #ifdef DEBUG LOG("MS-Video Device"); #endif - AVDictionary *options = NULL; + //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); + av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0); } else { //m_bSizeIsAutomatic = true; }; - //av_dict_set(&options, "pixel_format", "yuyv422", 0 ); - av_dict_set(&options, "video_codec", "mjpeg", 0 ); + //av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuyv422", 0 ); + av_dict_set(&m_pVideoState->m_pFormatOpts, "video_codec", "mjpeg", 0 ); if( inputPin != -1 ){ sprintf(tChar, "%d", inputPin); - av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0); + av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", tChar, 0); }; if( framerate != -1 ){ sprintf(tChar, "%f", framerate); - av_dict_set(&options, "framerate", tChar, 0); + av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0); }; - av_dict_set(&options, "rtbufsize", "500000k", 0); + av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_device_dialog", "true", 0); + av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0); sprintf(Buffer, "%s-a.settings", URI); - av_dict_set(&options, "audio_device_save", Buffer, 0); + av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + //if (!fileExist(Buffer)) av_dict_set(&m_pVideoState->m_pFormatOpts, "video_device_load", Buffer, 0); }; - //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0); + //av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + //av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0); + //av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + av_dict_set(&m_pVideoState->m_pFormatOpts, "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); + if( avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) != 0 ){ + av_dict_free(&m_pVideoState->m_pFormatOpts); //av_free( iformat ); sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI); LOG_ERROR(Buffer); @@ -280,8 +446,8 @@ bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, i // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL ) // serializeOption(cOption); //}; - av_dict_free(&options); - AVOption *mpOption = NULL; + //av_dict_free(&options); + //AVOption *mpOption = NULL; } else { if( strncmp( URI, "/dev/video", 10 ) == 0 ) { @@ -340,8 +506,7 @@ bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, i }; //av_dict_set(&options, "timeout", "8000", 0); - int RetVal = 0; - strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN ); + int RetVal = 0; //LOG("Before Open1"); if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){ //av_dict_free(&options); diff --git a/lib/data/media/video/videoAVLIBSource.h b/lib/data/media/video/videoAVLIBSource.h index be4d4dee..e288d6f0 100644 --- a/lib/data/media/video/videoAVLIBSource.h +++ b/lib/data/media/video/videoAVLIBSource.h @@ -38,6 +38,7 @@ class videoAVLIBSource : public basicMediaSource, public basicThread { virtual bool _open( CSTR URI, bool ShowDeviceMenu = false, float framerate=-1.0f, int inputPin = -1, int width = -1, int height = -1 , int s_width=-1, int s_height=-1 ); virtual bool _close( void ); virtual bool _stop( void ); + virtual bool _restart( void ); bool isFinished( void ); MediaStateStatus getStatus( void ); videoAVLIBSource( void *opaque=NULL );