6 #include <condition_variable>
14 #include <libavformat/avformat.h>
17 #include <QApplication>
19 #include <movit/init.h>
20 #include <movit/util.h>
22 #include "clip_list.h"
25 #include "mainwindow.h"
26 #include "ffmpeg_raii.h"
29 #include "post_to_main_thread.h"
30 #include "ref_counted_gl_sync.h"
31 #include "ui_mainwindow.h"
34 using namespace std::chrono;
36 std::mutex RefCountedGLsync::fence_lock;
38 // TODO: Replace by some sort of GUI control, I guess.
39 int64_t current_pts = 0;
41 string filename_for_frame(unsigned stream_idx, int64_t pts)
44 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
49 vector<int64_t> frames[MAX_STREAMS];
52 int record_thread_func();
54 int main(int argc, char **argv)
56 avformat_network_init();
57 global_httpd = new HTTPD;
58 global_httpd->start(DEFAULT_HTTPD_PORT);
60 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
63 fmt.setDepthBufferSize(0);
64 fmt.setStencilBufferSize(0);
65 fmt.setProfile(QSurfaceFormat::CoreProfile);
66 fmt.setMajorVersion(4);
67 fmt.setMinorVersion(5);
69 // Turn off vsync, since Qt generally gives us at most frame rate
70 // (display frequency) / (number of QGLWidgets active).
71 fmt.setSwapInterval(0);
73 QSurfaceFormat::setDefaultFormat(fmt);
75 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
77 QApplication app(argc, argv);
78 global_share_widget = new QGLWidget();
79 if (!global_share_widget->isValid()) {
80 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
86 QSurface *surface = create_surface();
87 QOpenGLContext *context = create_context(surface);
88 make_current(context, surface);
89 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
90 delete_context(context);
91 // TODO: Delete the surface, too.
94 MainWindow mainWindow;
97 thread(record_thread_func).detach();
102 int record_thread_func()
104 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
105 if (format_ctx == nullptr) {
106 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
110 int64_t last_pts = -1;
114 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
115 &pkt, av_packet_unref);
116 av_init_packet(&pkt);
119 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
122 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
123 // pkt.stream_index, pkt.pts, pkt.size);
124 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
125 FILE *fp = fopen(filename.c_str(), "wb");
127 perror(filename.c_str());
130 fwrite(pkt.data, pkt.size, 1, fp);
133 post_to_main_thread([pkt] {
134 if (pkt.stream_index == 0) {
135 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
136 } else if (pkt.stream_index == 1) {
137 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
138 } else if (pkt.stream_index == 2) {
139 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
140 } else if (pkt.stream_index == 3) {
141 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
145 assert(pkt.stream_index < MAX_STREAMS);
146 frames[pkt.stream_index].push_back(pkt.pts);
148 // Hack. Assumes a given timebase.
149 if (last_pts != -1) {
150 this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
153 current_pts = pkt.pts;