]> git.sesse.net Git - pkanalytics/blobdiff - video_widget.cpp
Fix a contention issue.
[pkanalytics] / video_widget.cpp
index a5b45136537552282d1ec8c118f5cd998de51adb..7bafa64e4b0a00f444eecbb40b2ef09d776cf98f 100644 (file)
@@ -30,6 +30,8 @@ extern "C" {
 #include <vector>
 #include <unordered_set>
 
+#include "post_to_main_thread.h"
+
 #include <QOpenGLFunctions>
 #include <QWheelEvent>
 #include <QMouseEvent>
@@ -192,7 +194,11 @@ 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())));
+                                       Frame *new_frame = new Frame(make_video_frame(frame.get()));
+                                       {
+                                               lock_guard lock(current_frame_mu);
+                                               current_frame.reset(new_frame);
+                                       }
                                        update();
                                        store_pts(frame->pts);
                                        break;
@@ -223,7 +229,11 @@ 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())));
+               Frame *new_frame = new Frame(make_video_frame(frame.get()));
+               {
+                       lock_guard lock(current_frame_mu);
+                       current_frame.reset(new_frame);
+               }
                update();
                store_pts(frame->pts);
        }
@@ -373,7 +383,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;
@@ -911,7 +925,11 @@ 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())));
+                               Frame *new_frame = new Frame(make_video_frame(frame.get()));
+                               {
+                                       lock_guard lock(current_frame_mu);
+                                       current_frame.reset(new_frame);
+                               }
                                last_frame = steady_clock::now();
                                update();
                                break;
@@ -925,7 +943,11 @@ 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())));
+                                       Frame *new_frame = new Frame(make_video_frame(frame.get()));
+                                       {
+                                               lock_guard lock(current_frame_mu);
+                                               current_frame.reset(new_frame);
+                                       }
                                        last_frame = steady_clock::now();
                                        update();
                                        break;
@@ -946,7 +968,9 @@ void VideoWidget::store_pts(int64_t pts)
 {
        last_pts = pts;
        last_position = lrint(pts * double(video_timebase.num) / double(video_timebase.den) * 1000);
-       emit position_changed(last_position);
+       post_to_main_thread([this, last_position{last_position.load()}] {
+               emit position_changed(last_position);
+       });
 }
 
 // Taken from Movit (see the comment there for explanation)