]> git.sesse.net Git - pkanalytics/commitdiff
Propagate errors on video opening back to the UI.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 20:39:02 +0000 (22:39 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 20:39:02 +0000 (22:39 +0200)
This is kludgy, but it will work for now.

mainwindow.cpp
video_widget.cpp
video_widget.h

index 7eec2cb4d2b8767aa89542edef5941c9827dbea1..5a1b7456cd90e6d701a6e9479d88504fe8353b6c 100644 (file)
@@ -42,7 +42,10 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        ui = new Ui::MainWindow;
        ui->setupUi(this);
 
-       ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv");
+       if (!ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv")) {
+               // TODO: Pop up a dialog box here instead
+               fprintf(stderr, "WARNING: Video opening failed\n");
+       }
        ui->video->play();
 
        ui->event_view->setModel(events);
index 7bafa64e4b0a00f444eecbb40b2ef09d776cf98f..e6708a9257cb8f4c5834fae55d720b94b1d826ae 100644 (file)
@@ -594,30 +594,40 @@ void VideoWidget::fixup_zoom_matrix()
        zoom_matrix[7] = std::max(zoom_matrix[7], 1.0 - zoom_matrix[4]);  // Top side (y=1).
 }
 
-void VideoWidget::open(const string &filename)
+bool VideoWidget::open(const string &filename)
 {
        stop();
        internal_rewind();
        pathname = filename;
        play();
+
+       while (running == STARTING) {
+               // Poor man's condition variable...
+               usleep(10000);
+               sched_yield();
+       }
+       return (running != VIDEO_FILE_ERROR);   
 }
 
 void VideoWidget::play()
 {
-       if (running) {
+       if (running != NOT_RUNNING && running != VIDEO_FILE_ERROR) {
                std::lock_guard<std::mutex> lock(queue_mu);
                command_queue.push_back(QueuedCommand { QueuedCommand::RESUME });
                producer_thread_should_quit.wakeup();
                return;
        }
-       running = true;
+       running = STARTING;
        producer_thread_should_quit.unquit();
+       if (producer_thread.joinable()) {
+               producer_thread.join();
+       }
        producer_thread = std::thread(&VideoWidget::producer_thread_func, this);
 }
 
 void VideoWidget::pause()
 {
-       if (!running) {
+       if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
                return;
        }
        std::lock_guard<std::mutex> lock(queue_mu);
@@ -627,7 +637,7 @@ void VideoWidget::pause()
 
 void VideoWidget::seek(int64_t relative_seek_ms)
 {
-       if (!running) {
+       if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
                return;
        }
        std::lock_guard<std::mutex> lock(queue_mu);
@@ -637,7 +647,7 @@ void VideoWidget::seek(int64_t relative_seek_ms)
 
 void VideoWidget::seek_frames(int64_t relative_seek_frames)
 {
-       if (!running) {
+       if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
                return;
        }
        std::lock_guard<std::mutex> lock(queue_mu);
@@ -647,7 +657,7 @@ void VideoWidget::seek_frames(int64_t relative_seek_frames)
 
 void VideoWidget::seek_absolute(int64_t position_ms)
 {
-       if (!running) {
+       if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
                return;
        }
        std::lock_guard<std::mutex> lock(queue_mu);
@@ -657,10 +667,9 @@ void VideoWidget::seek_absolute(int64_t position_ms)
 
 void VideoWidget::stop()
 {
-       if (!running) {
+       if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
                return;
        }
-       running = false;
        producer_thread_should_quit.quit();
        producer_thread.join();
 }
@@ -669,7 +678,9 @@ void VideoWidget::producer_thread_func()
 {
        if (!producer_thread_should_quit.should_quit()) {
                if (!play_video(pathname)) {
-                       // TODO: Send the error back to the UI somehow.
+                       running = VIDEO_FILE_ERROR;
+               } else {
+                       running = NOT_RUNNING;
                }
        }
 }
@@ -875,6 +886,8 @@ bool VideoWidget::play_video(const string &pathname)
 
        internal_rewind();
 
+       running = RUNNING;
+
        // Main loop.
        int consecutive_errors = 0;
        double rate = 1.0;
index dc7d0ab7b3f834285b1da2feabd3a2e06417a927..c0f28d75ec7ad8ff99e14e245e49e51054385a93 100644 (file)
@@ -25,7 +25,7 @@ public:
        VideoWidget(QWidget *parent);
        ~VideoWidget() { stop(); }
 
-       void open(const std::string &filename);
+       bool open(const std::string &filename);  // False on error.
        void play();
        uint64_t get_position() const { return last_position; }  // In milliseconds.
        void pause();
@@ -77,7 +77,8 @@ private:
        int64_t pts_origin;
        int64_t last_pts;
        std::atomic<uint64_t> last_position{0};  // TODO: sort of redundant wrt. last_pts (but this one can be read from the other thread)
-       std::atomic<bool> running{false};
+       enum RunState { NOT_RUNNING, STARTING, RUNNING, VIDEO_FILE_ERROR };  // NOT_RUNNING and VIDEO_FILE_ERROR both imply that the thread isn't running and can freely be restarted.
+       std::atomic<RunState> running{NOT_RUNNING};
        bool paused = false;
        std::chrono::steady_clock::time_point start, next_frame_start, last_frame;