From fbc0729c83f1bb4e86476e7c3d106ecfe3755169 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 16 Sep 2016 10:14:58 -0400 Subject: [PATCH] code cleanup, more use of av_strerr to give better error logs. --- src/zm_ffmpeg.h | 5 +++++ src/zm_ffmpeg_camera.cpp | 35 ++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/zm_ffmpeg.h b/src/zm_ffmpeg.h index 5b7111fde..372a84907 100644 --- a/src/zm_ffmpeg.h +++ b/src/zm_ffmpeg.h @@ -328,5 +328,10 @@ void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output) #else #define zm_av_unref_packet( packet ) av_free_packet( packet ) #endif +#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) + #define zm_avcodec_decode_video( context, rawFrame, frameComplete, packet ) avcodec_decode_video2( context, rawFrame, frameComplete, packet ) +#else + #define zm_avcodec_decode_video(context, rawFrame, frameComplete, packet ) avcodec_decode_video( context, rawFrame, frameComplete, packet->data, packet->size) +#endif #endif // ZM_FFMPEG_H diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index dd4a487f8..6a0ed04c9 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -532,6 +532,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi return -1; } int ret; + static char errbuf[AV_ERROR_MAX_STRING_SIZE]; // If the reopen thread has a value, but mCanCapture != 0, then we have just reopened the connection to the ffmpeg device, and we can clean up the thread. if (mReopenThread != 0) { @@ -553,26 +554,25 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi int frameComplete = false; while ( !frameComplete ) { - // We are now allocating dynamically because we need to queue these and may go out of scope. - AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket)); - av_init_packet( packet); + // We are now allocating dynamically because we need to queue these and may go out of scope. + AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket)); + av_init_packet( packet); Debug(5, "Before av_read_frame"); - int avResult = av_read_frame( mFormatContext, packet ); -Debug(5, "After av_read_frame (%d)", avResult ); - if ( avResult < 0 ) { - char errbuf[AV_ERROR_MAX_STRING_SIZE]; - av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE); + ret = av_read_frame( mFormatContext, packet ); +Debug(5, "After av_read_frame (%d)", ret ); + if ( ret < 0 ) { + av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE ); if ( // Check if EOF. - (avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || + (ret == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || // Check for Connection failure. - (avResult == -110) + (ret == -110) ) { Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf); ReopenFfmpeg(); } - Error( "Unable to read packet from stream %d: error %d \"%s\".", packet->stream_index, avResult, errbuf ); + Error( "Unable to read packet from stream %d: error %d \"%s\".", packet->stream_index, ret, errbuf ); return( -1 ); } Debug( 5, "Got packet from stream %d", packet->stream_index ); @@ -664,12 +664,13 @@ Debug(5, "After av_read_frame (%d)", avResult ); } // end if if ( packet->stream_index == mVideoStreamId ) { -#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) - if (avcodec_decode_video2(mVideoCodecContext, mRawFrame, &frameComplete, packet) < 0) -#else - if (avcodec_decode_video(mVideoCodecContext, mRawFrame, &frameComplete, packet->data, packet->size) < 0) -#endif - Fatal( "Unable to decode frame at frame %d", frameCount ); + ret = zm_avcodec_decode_video( mVideoCodecContext, mRawFrame, &frameComplete, packet ); + if ( ret < 0 ) { + av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE ); + Error( "Unable to decode frame at frame %d: %s, continuing", frameCount, errbuf ); + zm_av_unref_packet( packet ); + continue; + } Debug( 4, "Decoded video packet at frame %d", frameCount );