6 #include <condition_variable>
14 #include <libavformat/avformat.h>
17 #include <QApplication>
19 #include "clip_list.h"
21 #include "mainwindow.h"
22 #include "ffmpeg_raii.h"
25 #include "post_to_main_thread.h"
26 #include "ui_mainwindow.h"
29 using namespace std::chrono;
31 // TODO: Replace by some sort of GUI control, I guess.
32 int64_t current_pts = 0;
34 string filename_for_frame(unsigned stream_idx, int64_t pts)
37 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
42 vector<int64_t> frames[MAX_STREAMS];
43 QGLWidget *global_share_widget;
46 int record_thread_func();
48 int main(int argc, char **argv)
50 avformat_network_init();
51 global_httpd = new HTTPD;
52 global_httpd->start(DEFAULT_HTTPD_PORT);
54 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
57 fmt.setDepthBufferSize(0);
58 fmt.setStencilBufferSize(0);
59 fmt.setProfile(QSurfaceFormat::CoreProfile);
60 fmt.setMajorVersion(3);
61 fmt.setMinorVersion(1);
63 // Turn off vsync, since Qt generally gives us at most frame rate
64 // (display frequency) / (number of QGLWidgets active).
65 fmt.setSwapInterval(0);
67 QSurfaceFormat::setDefaultFormat(fmt);
69 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
71 QApplication app(argc, argv);
72 global_share_widget = new QGLWidget();
73 if (!global_share_widget->isValid()) {
74 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 3.1 to function properly.\n");
77 MainWindow mainWindow;
80 thread(record_thread_func).detach();
85 int record_thread_func()
87 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
88 if (format_ctx == nullptr) {
89 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
93 int64_t last_pts = -1;
97 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
98 &pkt, av_packet_unref);
102 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
105 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
106 // pkt.stream_index, pkt.pts, pkt.size);
107 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
108 FILE *fp = fopen(filename.c_str(), "wb");
110 perror(filename.c_str());
113 fwrite(pkt.data, pkt.size, 1, fp);
116 post_to_main_thread([pkt] {
117 if (pkt.stream_index == 0) {
118 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
119 } else if (pkt.stream_index == 1) {
120 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
121 } else if (pkt.stream_index == 2) {
122 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
123 } else if (pkt.stream_index == 3) {
124 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
128 assert(pkt.stream_index < MAX_STREAMS);
129 frames[pkt.stream_index].push_back(pkt.pts);
131 // Hack. Assumes a given timebase.
132 if (last_pts != -1) {
133 this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
136 current_pts = pkt.pts;