]> git.sesse.net Git - nageru/blobdiff - main.cpp
Assorted clang-format fixes (not complete).
[nageru] / main.cpp
index 42b1867dfb82e061d98ab1d12f62c0ba035e510a..da2340bdfd81c34862bd075648395adaf1e8554b 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,14 +1,14 @@
 #include <assert.h>
-#include <dirent.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <sys/types.h>
-
+#include <atomic>
 #include <chrono>
 #include <condition_variable>
+#include <dirent.h>
 #include <memory>
 #include <mutex>
+#include <stdint.h>
+#include <stdio.h>
 #include <string>
+#include <sys/types.h>
 #include <thread>
 #include <vector>
 
@@ -16,17 +16,13 @@ extern "C" {
 #include <libavformat/avformat.h>
 }
 
-#include <QApplication>
-
-#include <movit/init.h>
-#include <movit/util.h>
-
 #include "clip_list.h"
 #include "context.h"
 #include "defs.h"
-#include "mainwindow.h"
+#include "disk_space_estimator.h"
 #include "ffmpeg_raii.h"
 #include "httpd.h"
+#include "mainwindow.h"
 #include "player.h"
 #include "post_to_main_thread.h"
 #include "ref_counted_gl_sync.h"
@@ -34,10 +30,15 @@ extern "C" {
 #include "ui_mainwindow.h"
 #include "vaapi_jpeg_decoder.h"
 
+#include <QApplication>
+#include <movit/init.h>
+#include <movit/util.h>
+
 using namespace std;
 using namespace std::chrono;
 
-std::mutex RefCountedGLsync::fence_lock;
+mutex RefCountedGLsync::fence_lock;
+atomic<bool> should_quit{false};
 
 int64_t start_pts = -1;
 
@@ -101,12 +102,18 @@ int main(int argc, char **argv)
        MainWindow mainWindow;
        mainWindow.show();
 
+       init_jpeg_vaapi();
+
        load_existing_frames();
-       thread(record_thread_func).detach();
+       thread record_thread(record_thread_func);
 
-       init_jpeg_vaapi();
+       int ret = app.exec();
+
+       should_quit = true;
+       record_thread.join();
+       JPEGFrameView::shutdown();
 
-       return app.exec();
+       return ret;
 }
 
 void load_existing_frames()
@@ -163,13 +170,16 @@ int record_thread_func()
        int64_t last_pts = -1;
        int64_t pts_offset;
 
-       for ( ;; ) {
+       while (!should_quit.load()) {
                AVPacket pkt;
                unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
                        &pkt, av_packet_unref);
                av_init_packet(&pkt);
                pkt.data = nullptr;
                pkt.size = 0;
+
+               // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
+               // right now, should_quit will be ignored if it's hung on I/O.
                if (av_read_frame(format_ctx.get(), &pkt) != 0) {
                        break;
                }
@@ -195,6 +205,8 @@ int record_thread_func()
                fwrite(pkt.data, pkt.size, 1, fp);
                fclose(fp);
 
+               global_disk_space_estimator->report_write(filename, pts);
+
                post_to_main_thread([pkt, pts] {
                        if (pkt.stream_index == 0) {
                                global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);