From 4a0187ffb4075b4d217b8d9e9c96cac548b199d8 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 18 Apr 2018 23:13:45 +0200 Subject: [PATCH] Reset audio resampler when FFmpeg inputs restart due to errors. Not relevant for Kaeru, but will be relevant when Nageru gets FFmpeg audio inputs soon. --- ffmpeg_capture.cpp | 22 ++++++++++++++++++++++ ffmpeg_capture.h | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index 8fdb6a5..9ae3504 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -211,6 +211,8 @@ FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned he // Not really used for anything. description = "Video: " + filename; + last_frame = steady_clock::now(); + avformat_network_init(); // In case someone wants this. } @@ -338,6 +340,7 @@ void FFmpegCapture::send_disconnected_frame() frame_callback(-1, AVRational{1, TIMEBASE}, -1, AVRational{1, TIMEBASE}, timecode++, video_frame, /*video_offset=*/0, video_format, FrameAllocator::Frame(), /*audio_offset=*/0, AudioFormat()); + last_frame_was_connected = false; } } @@ -423,6 +426,7 @@ bool FFmpegCapture::play_video(const string &pathname) internal_rewind(); // Main loop. + bool first_frame = true; while (!producer_thread_should_quit.should_quit()) { if (process_queued_commands(format_ctx.get(), pathname, last_modified, /*rewound=*/nullptr)) { return true; @@ -471,6 +475,14 @@ bool FFmpegCapture::play_video(const string &pathname) pts_origin = frame->pts; } next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate); + if (first_frame && last_frame_was_connected) { + // If reconnect took more than one second, this is probably a live feed, + // and we should reset the resampler. (Or the rate is really, really low, + // in which case a reset on the first frame is fine anyway.) + if (duration(next_frame_start - last_frame).count() >= 1.0) { + last_frame_was_connected = false; + } + } video_frame->received_timestamp = next_frame_start; audio_frame->received_timestamp = next_frame_start; bool finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start); @@ -478,9 +490,19 @@ bool FFmpegCapture::play_video(const string &pathname) if (audio_frame->len > 0) { assert(audio_pts != -1); } + if (!last_frame_was_connected) { + // We're recovering from an error (or really slow load, see above). + // Make sure to get the audio resampler reset. (This is a hack; + // ideally, the frame callback should just accept a way to signal + // audio discontinuity.) + timecode += MAX_FPS * 2 + 1; + } frame_callback(frame->pts, video_timebase, audio_pts, audio_timebase, timecode++, video_frame.get_and_release(), 0, video_format, audio_frame.get_and_release(), 0, audio_format); + first_frame = false; + last_frame = steady_clock::now(); + last_frame_was_connected = true; break; } else { if (producer_thread_should_quit.should_quit()) break; diff --git a/ffmpeg_capture.h b/ffmpeg_capture.h index 705d25f..0fbc3e8 100644 --- a/ffmpeg_capture.h +++ b/ffmpeg_capture.h @@ -225,6 +225,7 @@ private: int card_index = -1; double rate = 1.0; std::atomic should_interrupt{false}; + bool last_frame_was_connected = true; bool has_dequeue_callbacks = false; std::function dequeue_init_callback = nullptr; @@ -246,7 +247,7 @@ private: std::thread producer_thread; int64_t pts_origin, last_pts; - std::chrono::steady_clock::time_point start, next_frame_start; + std::chrono::steady_clock::time_point start, next_frame_start, last_frame; std::mutex queue_mu; struct QueuedCommand { -- 2.39.2