9 #include <condition_variable>
17 #include <libavformat/avformat.h>
20 #include <QApplication>
22 #include <movit/init.h>
23 #include <movit/util.h>
25 #include "clip_list.h"
28 #include "mainwindow.h"
29 #include "ffmpeg_raii.h"
32 #include "post_to_main_thread.h"
33 #include "ref_counted_gl_sync.h"
35 #include "ui_mainwindow.h"
36 #include "vaapi_jpeg_decoder.h"
39 using namespace std::chrono;
41 mutex RefCountedGLsync::fence_lock;
42 atomic<bool> should_quit{false};
44 int64_t start_pts = -1;
46 // TODO: Replace by some sort of GUI control, I guess.
47 int64_t current_pts = 0;
49 string filename_for_frame(unsigned stream_idx, int64_t pts)
52 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
57 vector<int64_t> frames[MAX_STREAMS];
60 void load_existing_frames();
61 int record_thread_func();
63 int main(int argc, char **argv)
65 avformat_network_init();
66 global_httpd = new HTTPD;
67 global_httpd->start(DEFAULT_HTTPD_PORT);
69 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
72 fmt.setDepthBufferSize(0);
73 fmt.setStencilBufferSize(0);
74 fmt.setProfile(QSurfaceFormat::CoreProfile);
75 fmt.setMajorVersion(4);
76 fmt.setMinorVersion(5);
78 // Turn off vsync, since Qt generally gives us at most frame rate
79 // (display frequency) / (number of QGLWidgets active).
80 fmt.setSwapInterval(0);
82 QSurfaceFormat::setDefaultFormat(fmt);
84 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
86 QApplication app(argc, argv);
87 global_share_widget = new QGLWidget();
88 if (!global_share_widget->isValid()) {
89 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
95 QSurface *surface = create_surface();
96 QOpenGLContext *context = create_context(surface);
97 make_current(context, surface);
98 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
99 delete_context(context);
100 // TODO: Delete the surface, too.
103 MainWindow mainWindow;
108 load_existing_frames();
109 thread record_thread(record_thread_func);
111 int ret = app.exec();
114 record_thread.join();
115 JPEGFrameView::shutdown();
120 void load_existing_frames()
122 DIR *dir = opendir("frames/");
123 if (dir == nullptr) {
131 dirent *de = readdir(dir);
142 if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
143 stream_idx >= 0 && stream_idx < MAX_STREAMS) {
144 frames[stream_idx].push_back(pts);
145 start_pts = max(start_pts, pts);
151 if (start_pts == -1) {
154 // Add a gap of one second from the old frames to the new ones.
155 start_pts += TIMEBASE;
158 for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
159 sort(frames[stream_idx].begin(), frames[stream_idx].end());
163 int record_thread_func()
165 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
166 if (format_ctx == nullptr) {
167 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
171 int64_t last_pts = -1;
174 while (!should_quit.load()) {
176 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
177 &pkt, av_packet_unref);
178 av_init_packet(&pkt);
182 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
183 // right now, should_quit will be ignored if it's hung on I/O.
184 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
188 // Convert pts to our own timebase.
189 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
190 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
192 // Translate offset into our stream.
193 if (last_pts == -1) {
194 pts_offset = start_pts - pts;
196 pts = std::max(pts + pts_offset, start_pts);
198 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
199 // pkt.stream_index, pts, pkt.size);
200 string filename = filename_for_frame(pkt.stream_index, pts);
201 FILE *fp = fopen(filename.c_str(), "wb");
203 perror(filename.c_str());
206 fwrite(pkt.data, pkt.size, 1, fp);
209 post_to_main_thread([pkt, pts] {
210 if (pkt.stream_index == 0) {
211 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
212 } else if (pkt.stream_index == 1) {
213 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
214 } else if (pkt.stream_index == 2) {
215 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
216 } else if (pkt.stream_index == 3) {
217 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
221 assert(pkt.stream_index < MAX_STREAMS);
222 frames[pkt.stream_index].push_back(pts);
224 // Hack. Remove when we're dealing with live streams.
225 if (last_pts != -1) {
226 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));