From 321995944e46e187ee515d358f137d94c30f090e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 14 Apr 2017 01:04:32 +0200 Subject: [PATCH] Add a Lua function to rewind the video. --- ffmpeg_capture.cpp | 16 ++++++++++++++++ ffmpeg_capture.h | 13 +++++++++++++ theme.cpp | 9 +++++++++ 3 files changed, 38 insertions(+) diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index 647b7cb..bb41a35 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -186,6 +186,22 @@ bool FFmpegCapture::play_video(const string &pathname) // Main loop. while (!producer_thread_should_quit) { + // Process any queued commands from other threads. + vector commands; + { + lock_guard lock(queue_mu); + swap(commands, command_queue); + } + for (const QueuedCommand &cmd : commands) { + if (cmd.command == QueuedCommand::REWIND) { + 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()); + } + start = steady_clock::now(); + continue; + } + } + // Read packets until we have a frame or there are none left. int frame_finished = 0; AVFrameWithDeleter frame = av_frame_alloc_unique(); diff --git a/ffmpeg_capture.h b/ffmpeg_capture.h index 8c75b4f..daf353e 100644 --- a/ffmpeg_capture.h +++ b/ffmpeg_capture.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,12 @@ public: return card_index; } + void rewind() + { + std::lock_guard lock(queue_mu); + command_queue.push_back(QueuedCommand { QueuedCommand::REWIND }); + } + // CaptureInterface. void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override { @@ -147,6 +154,12 @@ private: std::atomic producer_thread_should_quit{false}; std::thread producer_thread; + + std::mutex queue_mu; + struct QueuedCommand { + enum Command { REWIND } command; + }; + std::vector command_queue; // Protected by . }; #endif // !defined(_FFMPEG_CAPTURE_H) diff --git a/theme.cpp b/theme.cpp index 46803ba..7ece56a 100644 --- a/theme.cpp +++ b/theme.cpp @@ -352,6 +352,14 @@ int VideoInput_new(lua_State* L) return ret; } +int VideoInput_rewind(lua_State* L) +{ + assert(lua_gettop(L) == 1); + FFmpegCapture **video_input = (FFmpegCapture **)luaL_checkudata(L, 1, "VideoInput"); + (*video_input)->rewind(); + return 0; +} + int WhiteBalanceEffect_new(lua_State* L) { assert(lua_gettop(L) == 0); @@ -553,6 +561,7 @@ const luaL_Reg ImageInput_funcs[] = { const luaL_Reg VideoInput_funcs[] = { { "new", VideoInput_new }, + { "rewind", VideoInput_rewind }, { NULL, NULL } }; -- 2.39.2