From bf6f4393ef3282685392858aaef8151f63e8b3c2 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 14 Apr 2017 18:12:27 +0200 Subject: [PATCH] Factor out rewinding code in its own member function. --- ffmpeg_capture.cpp | 16 +++++++++------- ffmpeg_capture.h | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index 5474c52..e03382c 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -189,9 +189,7 @@ bool FFmpegCapture::play_video(const string &pathname) unique_ptr codec_ctx_cleanup( codec_ctx.get(), avcodec_close); - int64_t pts_origin = 0, last_pts = 0; - steady_clock::time_point start = steady_clock::now(); - steady_clock::time_point next_frame_start = start; + internal_rewind(); double rate = 1.0; unique_ptr sws_ctx(nullptr, sws_freeContext); @@ -211,8 +209,7 @@ bool FFmpegCapture::play_video(const string &pathname) if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) { fprintf(stderr, "%s: Rewind failed, stopping play.\n", pathname.c_str()); } - pts_origin = last_pts = 0; - start = next_frame_start = steady_clock::now(); + internal_rewind(); break; case QueuedCommand::CHANGE_RATE: @@ -263,8 +260,7 @@ bool FFmpegCapture::play_video(const string &pathname) fprintf(stderr, "%s: Rewind failed, not looping.\n", pathname.c_str()); return true; } - pts_origin = last_pts = 0; - start = steady_clock::now(); + internal_rewind(); continue; } @@ -314,3 +310,9 @@ bool FFmpegCapture::play_video(const string &pathname) } return true; } + +void FFmpegCapture::internal_rewind() +{ + pts_origin = last_pts = 0; + start = next_frame_start = steady_clock::now(); +} diff --git a/ffmpeg_capture.h b/ffmpeg_capture.h index 24e6b5b..0950782 100644 --- a/ffmpeg_capture.h +++ b/ffmpeg_capture.h @@ -141,6 +141,7 @@ public: private: void producer_thread_func(); bool play_video(const std::string &pathname); + void internal_rewind(); std::string description, filename; uint16_t timecode = 0; @@ -161,6 +162,9 @@ private: QuittableSleeper producer_thread_should_quit; std::thread producer_thread; + int64_t pts_origin, last_pts; + std::chrono::steady_clock::time_point start, next_frame_start; + std::mutex queue_mu; struct QueuedCommand { enum Command { REWIND, CHANGE_RATE } command; -- 2.39.2