5 #include <condition_variable>
14 #include <sys/types.h>
20 #include <libavformat/avformat.h>
23 #include "clip_list.h"
24 #include "shared/context.h"
26 #include "shared/disk_space_estimator.h"
27 #include "shared/ffmpeg_raii.h"
29 #include "frame_on_disk.h"
31 #include "shared/httpd.h"
32 #include "mainwindow.h"
34 #include "shared/post_to_main_thread.h"
35 #include "shared/ref_counted_gl_sync.h"
36 #include "shared/timebase.h"
37 #include "shared/metrics.h"
38 #include "ui_mainwindow.h"
39 #include "vaapi_jpeg_decoder.h"
41 #include <QApplication>
43 #include <QSurfaceFormat>
44 #include <QProgressDialog>
45 #include <movit/init.h>
46 #include <movit/util.h>
49 using namespace std::chrono;
51 constexpr char frame_magic[] = "Ftbifrm0";
52 constexpr size_t frame_magic_len = 8;
54 mutex RefCountedGLsync::fence_lock;
55 atomic<bool> should_quit{false};
57 int64_t start_pts = -1;
59 // TODO: Replace by some sort of GUI control, I guess.
60 int64_t current_pts = 0;
64 unsigned filename_idx;
65 size_t frames_written_so_far = 0;
67 std::map<int, FrameFile> open_frame_files;
70 vector<FrameOnDisk> frames[MAX_STREAMS]; // Under frame_mu.
71 vector<string> frame_filenames; // Under frame_mu.
73 atomic<int64_t> metric_received_frames[MAX_STREAMS]{{0}};
77 FrameOnDisk write_frame(int stream_idx, int64_t pts, const uint8_t *data, size_t size, DB *db)
79 if (open_frame_files.count(stream_idx) == 0) {
81 snprintf(filename, sizeof(filename), "%s/frames/cam%d-pts%09ld.frames",
82 global_flags.working_directory.c_str(), stream_idx, pts);
83 FILE *fp = fopen(filename, "wb");
89 lock_guard<mutex> lock(frame_mu);
90 unsigned filename_idx = frame_filenames.size();
91 frame_filenames.push_back(filename);
92 open_frame_files[stream_idx] = FrameFile{ fp, filename_idx, 0 };
95 FrameFile &file = open_frame_files[stream_idx];
96 unsigned filename_idx = file.filename_idx;
99 lock_guard<mutex> lock(frame_mu);
100 filename = frame_filenames[filename_idx];
103 FrameHeaderProto hdr;
104 hdr.set_stream_idx(stream_idx);
106 hdr.set_file_size(size);
109 if (!hdr.SerializeToString(&serialized)) {
110 fprintf(stderr, "Frame header serialization failed.\n");
113 uint32_t len = htonl(serialized.size());
115 if (fwrite(frame_magic, frame_magic_len, 1, file.fp) != 1) {
119 if (fwrite(&len, sizeof(len), 1, file.fp) != 1) {
123 if (fwrite(serialized.data(), serialized.size(), 1, file.fp) != 1) {
127 off_t offset = ftell(file.fp);
128 if (fwrite(data, size, 1, file.fp) != 1) {
132 fflush(file.fp); // No fsync(), though. We can accept losing a few frames.
133 global_disk_space_estimator->report_write(filename, 8 + sizeof(len) + serialized.size() + size, pts);
137 frame.filename_idx = filename_idx;
138 frame.offset = offset;
142 lock_guard<mutex> lock(frame_mu);
143 assert(stream_idx < MAX_STREAMS);
144 frames[stream_idx].push_back(frame);
147 if (++file.frames_written_so_far >= 1000) {
148 size_t size = ftell(file.fp);
150 // Start a new file next time.
151 if (fclose(file.fp) != 0) {
155 open_frame_files.erase(stream_idx);
157 // Write information about all frames in the finished file to SQLite.
158 // (If we crash before getting to do this, we'll be scanning through
159 // the file on next startup, and adding it to the database then.)
160 // NOTE: Since we don't fsync(), we could in theory get broken data
161 // but with the right size, but it would seem unlikely.
162 vector<DB::FrameOnDiskAndStreamIdx> frames_this_file;
164 lock_guard<mutex> lock(frame_mu);
165 for (size_t stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
166 for (const FrameOnDisk &frame : frames[stream_idx]) {
167 if (frame.filename_idx == filename_idx) {
168 frames_this_file.emplace_back(DB::FrameOnDiskAndStreamIdx{ frame, unsigned(stream_idx) });
174 const char *basename = filename.c_str();
175 while (strchr(basename, '/') != nullptr) {
176 basename = strchr(basename, '/') + 1;
178 db->store_frame_file(basename, size, frames_this_file);
188 void load_existing_frames();
189 void record_thread_func();
191 int main(int argc, char **argv)
193 parse_flags(argc, argv);
194 if (optind == argc) {
195 global_flags.stream_source = "multiangle.mp4";
196 global_flags.slow_down_input = true;
197 } else if (optind + 1 == argc) {
198 global_flags.stream_source = argv[optind];
204 string frame_dir = global_flags.working_directory + "/frames";
206 if (mkdir(frame_dir.c_str(), 0777) == 0) {
207 fprintf(stderr, "%s does not exist, creating it.\n", frame_dir.c_str());
208 } else if (errno != EEXIST) {
209 perror(global_flags.working_directory.c_str());
213 avformat_network_init();
214 global_metrics.set_prefix("futatabi");
215 global_httpd = new HTTPD;
217 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
220 fmt.setDepthBufferSize(0);
221 fmt.setStencilBufferSize(0);
222 fmt.setProfile(QSurfaceFormat::CoreProfile);
223 fmt.setMajorVersion(4);
224 fmt.setMinorVersion(5);
226 // Turn off vsync, since Qt generally gives us at most frame rate
227 // (display frequency) / (number of QGLWidgets active).
228 fmt.setSwapInterval(0);
230 QSurfaceFormat::setDefaultFormat(fmt);
232 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
234 QApplication app(argc, argv);
235 global_share_widget = new QGLWidget();
236 if (!global_share_widget->isValid()) {
237 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
243 QSurface *surface = create_surface();
244 QOpenGLContext *context = create_context(surface);
245 if (!make_current(context, surface)) {
249 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
250 delete_context(context);
251 // TODO: Delete the surface, too.
254 load_existing_frames();
256 MainWindow main_window;
259 global_httpd->add_endpoint("/queue_status", bind(&MainWindow::get_queue_status, &main_window), HTTPD::NO_CORS_POLICY);
260 global_httpd->start(global_flags.http_port);
264 thread record_thread(record_thread_func);
266 int ret = app.exec();
269 record_thread.join();
270 JPEGFrameView::shutdown();
275 void load_frame_file(const char *filename, const string &basename, unsigned filename_idx, DB *db)
278 if (stat(filename, &st) == -1) {
283 vector<DB::FrameOnDiskAndStreamIdx> all_frames = db->load_frame_file(basename, st.st_size, filename_idx);
284 if (!all_frames.empty()) {
285 // We already had this cached in the database, so no need to look in the file.
286 for (const DB::FrameOnDiskAndStreamIdx &frame : all_frames) {
287 if (frame.stream_idx < MAX_STREAMS) {
288 frames[frame.stream_idx].push_back(frame.frame);
289 start_pts = max(start_pts, frame.frame.pts);
295 FILE *fp = fopen(filename, "rb");
301 size_t magic_offset = 0;
302 size_t skipped_bytes = 0;
303 while (!feof(fp) && !ferror(fp)) {
308 if (ch != frame_magic[magic_offset++]) {
309 skipped_bytes += magic_offset;
313 if (magic_offset < frame_magic_len) {
314 // Still reading the magic (hopefully).
318 // OK, found the magic. Try to parse the frame header.
321 if (skipped_bytes > 0) {
322 fprintf(stderr, "WARNING: %s: Skipped %zu garbage bytes in the middle.\n",
323 filename, skipped_bytes);
328 if (fread(&len, sizeof(len), 1, fp) != 1) {
329 fprintf(stderr, "WARNING: %s: Short read when getting length.\n", filename);
334 serialized.resize(ntohl(len));
335 if (fread(&serialized[0], serialized.size(), 1, fp) != 1) {
336 fprintf(stderr, "WARNING: %s: Short read when reading frame header (%zu bytes).\n", filename, serialized.size());
340 FrameHeaderProto hdr;
341 if (!hdr.ParseFromString(serialized)) {
342 fprintf(stderr, "WARNING: %s: Corrupted frame header.\n", filename);
347 frame.pts = hdr.pts();
348 frame.offset = ftell(fp);
349 if (frame.offset == -1) {
350 fprintf(stderr, "WARNING: %s: ftell() failed (%s).\n", filename, strerror(errno));
353 frame.filename_idx = filename_idx;
354 frame.size = hdr.file_size();
356 if (fseek(fp, frame.offset + frame.size, SEEK_SET) == -1) {
357 fprintf(stderr, "WARNING: %s: Could not seek past frame (probably truncated).\n", filename);
361 if (hdr.stream_idx() >= 0 && hdr.stream_idx() < MAX_STREAMS) {
362 frames[hdr.stream_idx()].push_back(frame);
363 start_pts = max(start_pts, hdr.pts());
365 all_frames.emplace_back(DB::FrameOnDiskAndStreamIdx{ frame, unsigned(hdr.stream_idx()) });
368 if (skipped_bytes > 0) {
369 fprintf(stderr, "WARNING: %s: Skipped %zu garbage bytes at the end.\n",
370 filename, skipped_bytes);
373 off_t size = ftell(fp);
377 fprintf(stderr, "WARNING: %s: ftell() failed (%s).\n", filename, strerror(errno));
381 db->store_frame_file(basename, size, all_frames);
384 void load_existing_frames()
386 QProgressDialog progress("Scanning frame directory...", "Abort", 0, 1);
387 progress.setWindowTitle("Futatabi");
388 progress.setWindowModality(Qt::WindowModal);
389 progress.setMinimumDuration(1000);
390 progress.setMaximum(1);
391 progress.setValue(0);
393 string frame_dir = global_flags.working_directory + "/frames";
394 DIR *dir = opendir(frame_dir.c_str());
395 if (dir == nullptr) {
401 vector<string> frame_basenames;
404 dirent *de = readdir(dir);
413 if (de->d_type == DT_REG || de->d_type == DT_LNK) {
414 string filename = frame_dir + "/" + de->d_name;
415 frame_filenames.push_back(filename);
416 frame_basenames.push_back(de->d_name);
419 if (progress.wasCanceled()) {
425 progress.setMaximum(frame_filenames.size() + 2);
426 progress.setValue(1);
428 progress.setLabelText("Opening database...");
429 DB db(global_flags.working_directory + "/futatabi.db");
431 progress.setLabelText("Reading frame files...");
432 progress.setValue(2);
434 for (size_t i = 0; i < frame_filenames.size(); ++i) {
435 load_frame_file(frame_filenames[i].c_str(), frame_basenames[i], i, &db);
436 progress.setValue(i + 3);
437 if (progress.wasCanceled()) {
442 if (start_pts == -1) {
445 // Add a gap of one second from the old frames to the new ones.
446 start_pts += TIMEBASE;
448 current_pts = start_pts;
450 for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
451 sort(frames[stream_idx].begin(), frames[stream_idx].end(),
452 [](const auto &a, const auto &b) { return a.pts < b.pts; });
455 db.clean_unused_frame_files(frame_basenames);
458 void record_thread_func()
460 for (unsigned i = 0; i < MAX_STREAMS; ++i) {
461 global_metrics.add("received_frames", {{ "stream", to_string(i) }}, &metric_received_frames[i]);
464 if (global_flags.stream_source.empty() || global_flags.stream_source == "/dev/null") {
465 // Save the user from some repetitive messages.
469 pthread_setname_np(pthread_self(), "ReceiveFrames");
471 int64_t pts_offset = 0; // Needs to be initialized due to a spurious GCC warning.
472 DB db(global_flags.working_directory + "/futatabi.db");
474 while (!should_quit.load()) {
475 auto format_ctx = avformat_open_input_unique(global_flags.stream_source.c_str(), nullptr, nullptr);
476 if (format_ctx == nullptr) {
477 fprintf(stderr, "%s: Error opening file. Waiting one second and trying again...\n", global_flags.stream_source.c_str());
482 int64_t last_pts = -1;
484 while (!should_quit.load()) {
486 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
487 &pkt, av_packet_unref);
488 av_init_packet(&pkt);
492 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
493 // right now, should_quit will be ignored if it's hung on I/O.
494 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
497 if (pkt.stream_index >= MAX_STREAMS) {
501 ++metric_received_frames[pkt.stream_index];
503 // Convert pts to our own timebase.
504 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
505 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
507 // Translate offset into our stream.
508 if (last_pts == -1) {
509 pts_offset = start_pts - pts;
511 pts = std::max(pts + pts_offset, start_pts);
513 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
514 // pkt.stream_index, pts, pkt.size);
515 FrameOnDisk frame = write_frame(pkt.stream_index, pts, pkt.data, pkt.size, &db);
517 post_to_main_thread([pkt, frame] {
518 global_mainwindow->display_frame(pkt.stream_index, frame);
521 if (last_pts != -1 && global_flags.slow_down_input) {
522 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));
528 fprintf(stderr, "%s: Hit EOF. Waiting one second and trying again...\n", global_flags.stream_source.c_str());
531 start_pts = last_pts + TIMEBASE;