]> git.sesse.net Git - nageru/commitdiff
Add a Lua function to rewind the video.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 13 Apr 2017 23:04:32 +0000 (01:04 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 13 Apr 2017 23:04:32 +0000 (01:04 +0200)
ffmpeg_capture.cpp
ffmpeg_capture.h
theme.cpp

index 647b7cbea4b4a96e25b1496b66a0b2b5d476d335..bb41a35a4dc01b9909cb374967c65a0153216179 100644 (file)
@@ -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<QueuedCommand> commands;
+               {
+                       lock_guard<mutex> 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();
index 8c75b4f705c25691bb2a967fc68c11b6d6ebeac0..daf353eec6bdad339dc5e1ddbf87a4d36709726c 100644 (file)
@@ -26,6 +26,7 @@
 #include <functional>
 #include <map>
 #include <memory>
+#include <mutex>
 #include <set>
 #include <string>
 #include <thread>
@@ -48,6 +49,12 @@ public:
                return card_index;
        }
 
+       void rewind()
+       {
+               std::lock_guard<std::mutex> 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<bool> producer_thread_should_quit{false};
        std::thread producer_thread;
+
+       std::mutex queue_mu;
+       struct QueuedCommand {
+               enum Command { REWIND } command;
+       };
+       std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
 };
 
 #endif  // !defined(_FFMPEG_CAPTURE_H)
index 46803ba92394321412a8617bef8d34acfc082ecf..7ece56a928330bc1baad55ac357e06ebf231b963 100644 (file)
--- 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 }
 };