4 #include <condition_variable>
11 #include <sys/types.h>
16 #include <libavformat/avformat.h>
19 #include "clip_list.h"
22 #include "disk_space_estimator.h"
23 #include "ffmpeg_raii.h"
25 #include "mainwindow.h"
27 #include "post_to_main_thread.h"
28 #include "ref_counted_gl_sync.h"
30 #include "ui_mainwindow.h"
31 #include "vaapi_jpeg_decoder.h"
33 #include <QApplication>
34 #include <movit/init.h>
35 #include <movit/util.h>
38 using namespace std::chrono;
40 mutex RefCountedGLsync::fence_lock;
41 atomic<bool> should_quit{false};
43 int64_t start_pts = -1;
45 // TODO: Replace by some sort of GUI control, I guess.
46 int64_t current_pts = 0;
48 string filename_for_frame(unsigned stream_idx, int64_t pts)
51 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
56 vector<int64_t> frames[MAX_STREAMS];
59 void load_existing_frames();
60 int record_thread_func();
62 int main(int argc, char **argv)
64 avformat_network_init();
65 global_httpd = new HTTPD;
66 global_httpd->start(DEFAULT_HTTPD_PORT);
68 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
71 fmt.setDepthBufferSize(0);
72 fmt.setStencilBufferSize(0);
73 fmt.setProfile(QSurfaceFormat::CoreProfile);
74 fmt.setMajorVersion(4);
75 fmt.setMinorVersion(5);
77 // Turn off vsync, since Qt generally gives us at most frame rate
78 // (display frequency) / (number of QGLWidgets active).
79 fmt.setSwapInterval(0);
81 QSurfaceFormat::setDefaultFormat(fmt);
83 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
85 QApplication app(argc, argv);
86 global_share_widget = new QGLWidget();
87 if (!global_share_widget->isValid()) {
88 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
94 QSurface *surface = create_surface();
95 QOpenGLContext *context = create_context(surface);
96 make_current(context, surface);
97 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
98 delete_context(context);
99 // TODO: Delete the surface, too.
102 MainWindow mainWindow;
107 load_existing_frames();
108 thread record_thread(record_thread_func);
110 int ret = app.exec();
113 record_thread.join();
114 JPEGFrameView::shutdown();
119 void load_existing_frames()
121 DIR *dir = opendir("frames/");
122 if (dir == nullptr) {
130 dirent *de = readdir(dir);
141 if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
142 stream_idx >= 0 && stream_idx < MAX_STREAMS) {
143 frames[stream_idx].push_back(pts);
144 start_pts = max(start_pts, pts);
150 if (start_pts == -1) {
153 // Add a gap of one second from the old frames to the new ones.
154 start_pts += TIMEBASE;
157 for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
158 sort(frames[stream_idx].begin(), frames[stream_idx].end());
162 int record_thread_func()
164 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
165 if (format_ctx == nullptr) {
166 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
170 int64_t last_pts = -1;
173 while (!should_quit.load()) {
175 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
176 &pkt, av_packet_unref);
177 av_init_packet(&pkt);
181 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
182 // right now, should_quit will be ignored if it's hung on I/O.
183 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
187 // Convert pts to our own timebase.
188 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
189 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
191 // Translate offset into our stream.
192 if (last_pts == -1) {
193 pts_offset = start_pts - pts;
195 pts = std::max(pts + pts_offset, start_pts);
197 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
198 // pkt.stream_index, pts, pkt.size);
199 string filename = filename_for_frame(pkt.stream_index, pts);
200 FILE *fp = fopen(filename.c_str(), "wb");
202 perror(filename.c_str());
205 fwrite(pkt.data, pkt.size, 1, fp);
208 global_disk_space_estimator->report_write(filename, pts);
210 post_to_main_thread([pkt, pts] {
211 if (pkt.stream_index == 0) {
212 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
213 } else if (pkt.stream_index == 1) {
214 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
215 } else if (pkt.stream_index == 2) {
216 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
217 } else if (pkt.stream_index == 3) {
218 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
222 assert(pkt.stream_index < MAX_STREAMS);
223 frames[pkt.stream_index].push_back(pts);
225 // Hack. Remove when we're dealing with live streams.
226 if (last_pts != -1) {
227 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));