From 1632b737345630f54b7ea5473bbaf592a8109b41 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 15 Apr 2023 19:03:35 +0200 Subject: [PATCH] Be more resilient to video decoding errors. In particular, hardware decoding of SRT streams that don't start on a keyframe can throw errors the first few frames, but then recover afterwards. --- nageru/ffmpeg_capture.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nageru/ffmpeg_capture.cpp b/nageru/ffmpeg_capture.cpp index 504eff2..d1b7e74 100644 --- a/nageru/ffmpeg_capture.cpp +++ b/nageru/ffmpeg_capture.cpp @@ -591,6 +591,7 @@ bool FFmpegCapture::play_video(const string &pathname) // Main loop. bool first_frame = true; + int consecutive_errors = 0; while (!producer_thread_should_quit.should_quit()) { if (process_queued_commands(format_ctx.get(), pathname, last_modified, /*rewound=*/nullptr)) { return true; @@ -607,7 +608,14 @@ bool FFmpegCapture::play_video(const string &pathname) AVFrameWithDeleter frame = decode_frame(format_ctx.get(), video_codec_ctx.get(), audio_codec_ctx.get(), pathname, video_stream_index, audio_stream_index, subtitle_stream_index, audio_frame.get(), &audio_format, &audio_pts, &error); if (error) { - return false; + if (++consecutive_errors >= 100) { + fprintf(stderr, "More than 100 consecutive video frames, aborting playback.\n"); + return false; + } else { + continue; + } + } else { + consecutive_errors = 0; } if (frame == nullptr) { // EOF. Loop back to the start if we can. -- 2.39.2