4 #include <condition_variable>
13 #include <sys/types.h>
18 #include <libavformat/avformat.h>
21 #include "clip_list.h"
24 #include "disk_space_estimator.h"
25 #include "ffmpeg_raii.h"
28 #include "mainwindow.h"
30 #include "post_to_main_thread.h"
31 #include "ref_counted_gl_sync.h"
33 #include "ui_mainwindow.h"
34 #include "vaapi_jpeg_decoder.h"
36 #include <QApplication>
37 #include <movit/init.h>
38 #include <movit/util.h>
41 using namespace std::chrono;
43 mutex RefCountedGLsync::fence_lock;
44 atomic<bool> should_quit{false};
46 int64_t start_pts = -1;
48 // TODO: Replace by some sort of GUI control, I guess.
49 int64_t current_pts = 0;
51 string filename_for_frame(unsigned stream_idx, int64_t pts)
54 snprintf(filename, sizeof(filename), "%s/frames/cam%d-pts%09ld.jpeg",
55 global_flags.working_directory.c_str(), stream_idx, pts);
60 vector<int64_t> frames[MAX_STREAMS];
63 void load_existing_frames();
64 int record_thread_func();
66 int main(int argc, char **argv)
68 parse_flags(argc, argv);
70 global_flags.stream_source = "multiangle.mp4";
71 global_flags.slow_down_input = true;
72 } else if (optind + 1 == argc) {
73 global_flags.stream_source = argv[optind];
79 string frame_dir = global_flags.working_directory + "/frames";
82 if (stat(frame_dir.c_str(), &st) == -1) {
83 fprintf(stderr, "%s does not exist, creating it.\n", frame_dir.c_str());
84 if (mkdir(frame_dir.c_str(), 0777) == -1) {
85 perror(global_flags.working_directory.c_str());
90 avformat_network_init();
91 global_httpd = new HTTPD;
93 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
96 fmt.setDepthBufferSize(0);
97 fmt.setStencilBufferSize(0);
98 fmt.setProfile(QSurfaceFormat::CoreProfile);
99 fmt.setMajorVersion(4);
100 fmt.setMinorVersion(5);
102 // Turn off vsync, since Qt generally gives us at most frame rate
103 // (display frequency) / (number of QGLWidgets active).
104 fmt.setSwapInterval(0);
106 QSurfaceFormat::setDefaultFormat(fmt);
108 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
110 QApplication app(argc, argv);
111 global_share_widget = new QGLWidget();
112 if (!global_share_widget->isValid()) {
113 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
119 QSurface *surface = create_surface();
120 QOpenGLContext *context = create_context(surface);
121 make_current(context, surface);
122 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
123 delete_context(context);
124 // TODO: Delete the surface, too.
127 MainWindow main_window;
130 global_httpd->add_endpoint("/queue_status", bind(&MainWindow::get_queue_status, &main_window), HTTPD::NO_CORS_POLICY);
131 global_httpd->start(global_flags.http_port);
135 load_existing_frames();
136 thread record_thread(record_thread_func);
138 int ret = app.exec();
141 record_thread.join();
142 JPEGFrameView::shutdown();
147 void load_existing_frames()
149 string frame_dir = global_flags.working_directory + "/frames";
150 DIR *dir = opendir(frame_dir.c_str());
151 if (dir == nullptr) {
159 dirent *de = readdir(dir);
170 if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
171 stream_idx >= 0 && stream_idx < MAX_STREAMS) {
172 frames[stream_idx].push_back(pts);
173 start_pts = max(start_pts, pts);
179 if (start_pts == -1) {
182 // Add a gap of one second from the old frames to the new ones.
183 start_pts += TIMEBASE;
186 for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
187 sort(frames[stream_idx].begin(), frames[stream_idx].end());
191 int record_thread_func()
193 auto format_ctx = avformat_open_input_unique(global_flags.stream_source.c_str(), nullptr, nullptr);
194 if (format_ctx == nullptr) {
195 fprintf(stderr, "%s: Error opening file\n", global_flags.stream_source.c_str());
199 int64_t last_pts = -1;
202 while (!should_quit.load()) {
204 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
205 &pkt, av_packet_unref);
206 av_init_packet(&pkt);
210 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
211 // right now, should_quit will be ignored if it's hung on I/O.
212 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
216 // Convert pts to our own timebase.
217 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
218 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
220 // Translate offset into our stream.
221 if (last_pts == -1) {
222 pts_offset = start_pts - pts;
224 pts = std::max(pts + pts_offset, start_pts);
226 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
227 // pkt.stream_index, pts, pkt.size);
228 string filename = filename_for_frame(pkt.stream_index, pts);
229 FILE *fp = fopen(filename.c_str(), "wb");
231 perror(filename.c_str());
234 fwrite(pkt.data, pkt.size, 1, fp);
237 global_disk_space_estimator->report_write(filename, pts);
239 post_to_main_thread([pkt, pts] {
240 if (pkt.stream_index == 0) {
241 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts);
242 } else if (pkt.stream_index == 1) {
243 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pts);
244 } else if (pkt.stream_index == 2) {
245 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pts);
246 } else if (pkt.stream_index == 3) {
247 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pts);
251 assert(pkt.stream_index < MAX_STREAMS);
252 frames[pkt.stream_index].push_back(pts);
254 if (last_pts != -1 && global_flags.slow_down_input) {
255 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));