]> git.sesse.net Git - pkanalytics/commitdiff
std::shared_ptr is surprisingly not thread-safe, so we need a mutex.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 20 Jul 2023 13:44:51 +0000 (15:44 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 20 Jul 2023 13:44:51 +0000 (15:44 +0200)
video_widget.cpp
video_widget.h

index 126e874aa53b1591e6b53d984a26cfd63a25ad1e..b50ea1c404a0494b8297180e8fc0f7c671c4bad6 100644 (file)
@@ -194,7 +194,10 @@ bool VideoWidget::process_queued_commands(AVFormatContext *format_ctx, AVCodecCo
                                                queue.pop_front();
                                                queued_frames = std::move(queue);
                                        }
-                                       current_frame.reset(new Frame(make_video_frame(frame.get())));
+                                       {
+                                               lock_guard lock(current_frame_mu);
+                                               current_frame.reset(new Frame(make_video_frame(frame.get())));
+                                       }
                                        update();
                                        store_pts(frame->pts);
                                        break;
@@ -225,7 +228,10 @@ bool VideoWidget::process_queued_commands(AVFormatContext *format_ctx, AVCodecCo
                if (frame == nullptr || error) {
                        return true;
                }
-               current_frame.reset(new Frame(make_video_frame(frame.get())));
+               {
+                       lock_guard lock(current_frame_mu);
+                       current_frame.reset(new Frame(make_video_frame(frame.get())));
+               }
                update();
                store_pts(frame->pts);
        }
@@ -375,7 +381,11 @@ int num_levels(GLuint width, GLuint height)
 
 void VideoWidget::paintGL()
 {
-       std::shared_ptr<Frame> frame = current_frame;
+       std::shared_ptr<Frame> frame;
+       {
+               lock_guard lock(current_frame_mu);
+               frame = current_frame;
+       }
        if (frame == nullptr) {
                glClear(GL_COLOR_BUFFER_BIT);
                return;
@@ -913,7 +923,10 @@ bool VideoWidget::play_video(const string &pathname)
                        bool finished_wakeup;
                        finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start);
                        if (finished_wakeup) {
-                               current_frame.reset(new Frame(make_video_frame(frame.get())));
+                               {
+                                       lock_guard lock(current_frame_mu);
+                                       current_frame.reset(new Frame(make_video_frame(frame.get())));
+                               }
                                last_frame = steady_clock::now();
                                update();
                                break;
@@ -927,7 +940,10 @@ bool VideoWidget::play_video(const string &pathname)
 
                                if (paused) {
                                        // Just paused, so present the frame immediately and then go into deep sleep.
-                                       current_frame.reset(new Frame(make_video_frame(frame.get())));
+                                       {
+                                               lock_guard lock(current_frame_mu);
+                                               current_frame.reset(new Frame(make_video_frame(frame.get())));
+                                       }
                                        last_frame = steady_clock::now();
                                        update();
                                        break;
index 1f1ffe2ffb6b8f752fff942a031108dce94f6477..dc7d0ab7b3f834285b1da2feabd3a2e06417a927 100644 (file)
@@ -56,7 +56,8 @@ private:
                unsigned chroma_width, chroma_height;
                std::unique_ptr<uint8_t[]> data;  // Y, followed by Cb, followed by Cr.
        };
-       std::shared_ptr<Frame> current_frame;
+       std::mutex current_frame_mu;
+       std::shared_ptr<Frame> current_frame;  // Protected by current_frame_mu.
        std::deque<AVFrameWithDeleter> queued_frames;  // Frames decoded but not displayed. Only used when frame-stepping backwards.
 
        GLuint ycbcr_vertex_shader, ycbcr_fragment_shader, ycbcr_program;