]> git.sesse.net Git - nageru/blob - futatabi/main.cpp
If the Futatabi source URL is empty or /dev/null, don't bother trying to open it.
[nageru] / futatabi / main.cpp
1 #include <assert.h>
2 #include <arpa/inet.h>
3 #include <atomic>
4 #include <chrono>
5 #include <condition_variable>
6 #include <dirent.h>
7 #include <getopt.h>
8 #include <memory>
9 #include <mutex>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <string>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <thread>
16 #include <unistd.h>
17 #include <vector>
18
19 extern "C" {
20 #include <libavformat/avformat.h>
21 }
22
23 #include "clip_list.h"
24 #include "shared/context.h"
25 #include "defs.h"
26 #include "shared/disk_space_estimator.h"
27 #include "shared/ffmpeg_raii.h"
28 #include "flags.h"
29 #include "frame_on_disk.h"
30 #include "frame.pb.h"
31 #include "shared/httpd.h"
32 #include "mainwindow.h"
33 #include "player.h"
34 #include "shared/post_to_main_thread.h"
35 #include "shared/ref_counted_gl_sync.h"
36 #include "shared/timebase.h"
37 #include "ui_mainwindow.h"
38 #include "vaapi_jpeg_decoder.h"
39
40 #include <QApplication>
41 #include <QGLFormat>
42 #include <QSurfaceFormat>
43 #include <QProgressDialog>
44 #include <movit/init.h>
45 #include <movit/util.h>
46
47 using namespace std;
48 using namespace std::chrono;
49
50 constexpr char frame_magic[] = "Ftbifrm0";
51 constexpr size_t frame_magic_len = 8;
52
53 mutex RefCountedGLsync::fence_lock;
54 atomic<bool> should_quit{false};
55
56 int64_t start_pts = -1;
57
58 // TODO: Replace by some sort of GUI control, I guess.
59 int64_t current_pts = 0;
60
61 struct FrameFile {
62         FILE *fp = nullptr;
63         unsigned filename_idx;
64         size_t frames_written_so_far = 0;
65 };
66 std::map<int, FrameFile> open_frame_files;
67
68 mutex frame_mu;
69 vector<FrameOnDisk> frames[MAX_STREAMS];  // Under frame_mu.
70 vector<string> frame_filenames;  // Under frame_mu.
71
72 namespace {
73
74 FrameOnDisk write_frame(int stream_idx, int64_t pts, const uint8_t *data, size_t size, DB *db)
75 {
76         if (open_frame_files.count(stream_idx) == 0) {
77                 char filename[256];
78                 snprintf(filename, sizeof(filename), "%s/frames/cam%d-pts%09ld.frames",
79                         global_flags.working_directory.c_str(), stream_idx, pts);
80                 FILE *fp = fopen(filename, "wb");
81                 if (fp == nullptr) {
82                         perror(filename);
83                         exit(1);
84                 }
85
86                 lock_guard<mutex> lock(frame_mu);
87                 unsigned filename_idx = frame_filenames.size();
88                 frame_filenames.push_back(filename);
89                 open_frame_files[stream_idx] = FrameFile{ fp, filename_idx, 0 };
90         }
91
92         FrameFile &file = open_frame_files[stream_idx];
93         unsigned filename_idx = file.filename_idx;
94         string filename;
95         {
96                 lock_guard<mutex> lock(frame_mu);
97                 filename = frame_filenames[filename_idx];
98         }
99
100         FrameHeaderProto hdr;
101         hdr.set_stream_idx(stream_idx);
102         hdr.set_pts(pts);
103         hdr.set_file_size(size);
104
105         string serialized;
106         if (!hdr.SerializeToString(&serialized)) {
107                 fprintf(stderr, "Frame header serialization failed.\n");
108                 exit(1);
109         }
110         uint32_t len = htonl(serialized.size());
111
112         if (fwrite(frame_magic, frame_magic_len, 1, file.fp) != 1) {
113                 perror("fwrite");
114                 exit(1);
115         }
116         if (fwrite(&len, sizeof(len), 1, file.fp) != 1) {
117                 perror("fwrite");
118                 exit(1);
119         }
120         if (fwrite(serialized.data(), serialized.size(), 1, file.fp) != 1) {
121                 perror("fwrite");
122                 exit(1);
123         }
124         off_t offset = ftell(file.fp);
125         if (fwrite(data, size, 1, file.fp) != 1) {
126                 perror("fwrite");
127                 exit(1);
128         }
129         fflush(file.fp);  // No fsync(), though. We can accept losing a few frames.
130         global_disk_space_estimator->report_write(filename, 8 + sizeof(len) + serialized.size() + size, pts);
131
132         FrameOnDisk frame;
133         frame.pts = pts;
134         frame.filename_idx = filename_idx;
135         frame.offset = offset;
136         frame.size = size;
137
138         {
139                 lock_guard<mutex> lock(frame_mu);
140                 assert(stream_idx < MAX_STREAMS);
141                 frames[stream_idx].push_back(frame);
142         }
143
144         if (++file.frames_written_so_far >= 1000) {
145                 size_t size = ftell(file.fp);
146
147                 // Start a new file next time.
148                 if (fclose(file.fp) != 0) {
149                         perror("fclose");
150                         exit(1);
151                 }
152                 open_frame_files.erase(stream_idx);
153
154                 // Write information about all frames in the finished file to SQLite.
155                 // (If we crash before getting to do this, we'll be scanning through
156                 // the file on next startup, and adding it to the database then.)
157                 // NOTE: Since we don't fsync(), we could in theory get broken data
158                 // but with the right size, but it would seem unlikely.
159                 vector<DB::FrameOnDiskAndStreamIdx> frames_this_file;
160                 {
161                         lock_guard<mutex> lock(frame_mu);
162                         for (size_t stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
163                                 for (const FrameOnDisk &frame : frames[stream_idx]) {
164                                         if (frame.filename_idx == filename_idx) {
165                                                 frames_this_file.emplace_back(DB::FrameOnDiskAndStreamIdx{ frame, unsigned(stream_idx) });
166                                         }
167                                 }
168                         }
169                 }
170
171                 const char *basename = filename.c_str();
172                 while (strchr(basename, '/') != nullptr) {
173                         basename = strchr(basename, '/') + 1;
174                 }
175                 db->store_frame_file(basename, size, frames_this_file);
176         }
177
178         return frame;
179 }
180
181 } // namespace
182
183 HTTPD *global_httpd;
184
185 void load_existing_frames();
186 void record_thread_func();
187
188 int main(int argc, char **argv)
189 {
190         parse_flags(argc, argv);
191         if (optind == argc) {
192                 global_flags.stream_source = "multiangle.mp4";
193                 global_flags.slow_down_input = true;
194         } else if (optind + 1 == argc) {
195                 global_flags.stream_source = argv[optind];
196         } else {
197                 usage();
198                 exit(1);
199         }
200
201         string frame_dir = global_flags.working_directory + "/frames";
202
203         if (mkdir(frame_dir.c_str(), 0777) == 0) {
204                 fprintf(stderr, "%s does not exist, creating it.\n", frame_dir.c_str());
205         } else if (errno != EEXIST) {
206                 perror(global_flags.working_directory.c_str());
207                 exit(1);
208         }
209
210         avformat_network_init();
211         global_httpd = new HTTPD;
212
213         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
214
215         QSurfaceFormat fmt;
216         fmt.setDepthBufferSize(0);
217         fmt.setStencilBufferSize(0);
218         fmt.setProfile(QSurfaceFormat::CoreProfile);
219         fmt.setMajorVersion(4);
220         fmt.setMinorVersion(5);
221
222         // Turn off vsync, since Qt generally gives us at most frame rate
223         // (display frequency) / (number of QGLWidgets active).
224         fmt.setSwapInterval(0);
225
226         QSurfaceFormat::setDefaultFormat(fmt);
227
228         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
229
230         QApplication app(argc, argv);
231         global_share_widget = new QGLWidget();
232         if (!global_share_widget->isValid()) {
233                 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
234                 exit(1);
235         }
236
237         // Initialize Movit.
238         {
239                 QSurface *surface = create_surface();
240                 QOpenGLContext *context = create_context(surface);
241                 if (!make_current(context, surface)) {
242                         printf("oops\n");
243                         exit(1);
244                 }
245                 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
246                 delete_context(context);
247                 // TODO: Delete the surface, too.
248         }
249
250         load_existing_frames();
251
252         MainWindow main_window;
253         main_window.show();
254
255         global_httpd->add_endpoint("/queue_status", bind(&MainWindow::get_queue_status, &main_window), HTTPD::NO_CORS_POLICY);
256         global_httpd->start(global_flags.http_port);
257
258         init_jpeg_vaapi();
259
260         thread record_thread(record_thread_func);
261
262         int ret = app.exec();
263
264         should_quit = true;
265         record_thread.join();
266         JPEGFrameView::shutdown();
267
268         return ret;
269 }
270
271 void load_frame_file(const char *filename, const string &basename, unsigned filename_idx, DB *db)
272 {
273         struct stat st;
274         if (stat(filename, &st) == -1) {
275                 perror(filename);
276                 exit(1);
277         }
278
279         vector<DB::FrameOnDiskAndStreamIdx> all_frames = db->load_frame_file(basename, st.st_size, filename_idx);
280         if (!all_frames.empty()) {
281                 // We already had this cached in the database, so no need to look in the file.
282                 for (const DB::FrameOnDiskAndStreamIdx &frame : all_frames) {
283                         if (frame.stream_idx < MAX_STREAMS) {
284                                 frames[frame.stream_idx].push_back(frame.frame);
285                                 start_pts = max(start_pts, frame.frame.pts);
286                         }
287                 }
288                 return;
289         }
290
291         FILE *fp = fopen(filename, "rb");
292         if (fp == nullptr) {
293                 perror(filename);
294                 exit(1);
295         }
296
297         size_t magic_offset = 0;
298         size_t skipped_bytes = 0;
299         while (!feof(fp) && !ferror(fp)) {
300                 int ch = getc(fp);
301                 if (ch == -1) {
302                         break;
303                 }
304                 if (ch != frame_magic[magic_offset++]) {
305                         skipped_bytes += magic_offset;
306                         magic_offset = 0;
307                         continue;
308                 }
309                 if (magic_offset < frame_magic_len) {
310                         // Still reading the magic (hopefully).
311                         continue;
312                 }
313
314                 // OK, found the magic. Try to parse the frame header.
315                 magic_offset = 0;
316
317                 if (skipped_bytes > 0)  {
318                         fprintf(stderr, "WARNING: %s: Skipped %zu garbage bytes in the middle.\n",
319                                 filename, skipped_bytes);
320                         skipped_bytes = 0;
321                 }
322
323                 uint32_t len;
324                 if (fread(&len, sizeof(len), 1, fp) != 1) {
325                         fprintf(stderr, "WARNING: %s: Short read when getting length.\n", filename);
326                         break;
327                 }
328
329                 string serialized;
330                 serialized.resize(ntohl(len));
331                 if (fread(&serialized[0], serialized.size(), 1, fp) != 1) {
332                         fprintf(stderr, "WARNING: %s: Short read when reading frame header (%zu bytes).\n", filename, serialized.size());
333                         break;
334                 }
335
336                 FrameHeaderProto hdr;
337                 if (!hdr.ParseFromString(serialized)) {
338                         fprintf(stderr, "WARNING: %s: Corrupted frame header.\n", filename);
339                         continue;
340                 }
341
342                 FrameOnDisk frame;
343                 frame.pts = hdr.pts();
344                 frame.offset = ftell(fp);
345                 if (frame.offset == -1) {
346                         fprintf(stderr, "WARNING: %s: ftell() failed (%s).\n", filename, strerror(errno));
347                         break;
348                 }
349                 frame.filename_idx = filename_idx;
350                 frame.size = hdr.file_size();
351
352                 if (fseek(fp, frame.offset + frame.size, SEEK_SET) == -1) {
353                         fprintf(stderr, "WARNING: %s: Could not seek past frame (probably truncated).\n", filename);
354                         continue;
355                 }
356
357                 if (hdr.stream_idx() >= 0 && hdr.stream_idx() < MAX_STREAMS) {
358                         frames[hdr.stream_idx()].push_back(frame);
359                         start_pts = max(start_pts, hdr.pts());
360                 }
361                 all_frames.emplace_back(DB::FrameOnDiskAndStreamIdx{ frame, unsigned(hdr.stream_idx()) });
362         }
363
364         if (skipped_bytes > 0) {
365                 fprintf(stderr, "WARNING: %s: Skipped %zu garbage bytes at the end.\n",
366                         filename, skipped_bytes);
367         }
368
369         off_t size = ftell(fp);
370         fclose(fp);
371
372         if (size == -1) {
373                 fprintf(stderr, "WARNING: %s: ftell() failed (%s).\n", filename, strerror(errno));
374                 return;
375         }
376
377         db->store_frame_file(basename, size, all_frames);
378 }
379
380 void load_existing_frames()
381 {
382         QProgressDialog progress("Scanning frame directory...", "Abort", 0, 1);
383         progress.setWindowTitle("Futatabi");
384         progress.setWindowModality(Qt::WindowModal);
385         progress.setMinimumDuration(1000);
386         progress.setMaximum(1);
387         progress.setValue(0);
388
389         string frame_dir = global_flags.working_directory + "/frames";
390         DIR *dir = opendir(frame_dir.c_str());
391         if (dir == nullptr) {
392                 perror("frames/");
393                 start_pts = 0;
394                 return;
395         }
396
397         vector<string> frame_basenames;
398         for ( ;; ) {
399                 errno = 0;
400                 dirent *de = readdir(dir);
401                 if (de == nullptr) {
402                         if (errno != 0) {
403                                 perror("readdir");
404                                 exit(1);
405                         }
406                         break;
407                 }
408
409                 if (de->d_type == DT_REG || de->d_type == DT_LNK) {
410                         string filename = frame_dir + "/" + de->d_name;
411                         frame_filenames.push_back(filename);
412                         frame_basenames.push_back(de->d_name);
413                 }
414
415                 if (progress.wasCanceled()) {
416                         exit(1);
417                 }
418         }
419         closedir(dir);
420
421         progress.setMaximum(frame_filenames.size() + 2);
422         progress.setValue(1);
423
424         progress.setLabelText("Opening database...");
425         DB db(global_flags.working_directory + "/futatabi.db");
426
427         progress.setLabelText("Reading frame files...");
428         progress.setValue(2);
429
430         for (size_t i = 0; i < frame_filenames.size(); ++i) {
431                 load_frame_file(frame_filenames[i].c_str(), frame_basenames[i], i, &db);
432                 progress.setValue(i + 3);
433                 if (progress.wasCanceled()) {
434                         exit(1);
435                 }
436         }
437
438         if (start_pts == -1) {
439                 start_pts = 0;
440         } else {
441                 // Add a gap of one second from the old frames to the new ones.
442                 start_pts += TIMEBASE;
443         }
444         current_pts = start_pts;
445
446         for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
447                 sort(frames[stream_idx].begin(), frames[stream_idx].end(),
448                         [](const auto &a, const auto &b) { return a.pts < b.pts; });
449         }
450
451         db.clean_unused_frame_files(frame_basenames);
452 }
453
454 void record_thread_func()
455 {
456         if (global_flags.stream_source.empty() || global_flags.stream_source == "/dev/null") {
457                 // Save the user from some repetitive messages.
458                 return;
459         }
460
461         pthread_setname_np(pthread_self(), "ReceiveFrames");
462
463         int64_t pts_offset;
464         DB db(global_flags.working_directory + "/futatabi.db");
465
466         while (!should_quit.load()) {
467                 auto format_ctx = avformat_open_input_unique(global_flags.stream_source.c_str(), nullptr, nullptr);
468                 if (format_ctx == nullptr) {
469                         fprintf(stderr, "%s: Error opening file. Waiting one second and trying again...\n", global_flags.stream_source.c_str());
470                         sleep(1);
471                         continue;
472                 }
473
474                 int64_t last_pts = -1;
475
476                 while (!should_quit.load()) {
477                         AVPacket pkt;
478                         unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
479                                 &pkt, av_packet_unref);
480                         av_init_packet(&pkt);
481                         pkt.data = nullptr;
482                         pkt.size = 0;
483
484                         // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
485                         // right now, should_quit will be ignored if it's hung on I/O.
486                         if (av_read_frame(format_ctx.get(), &pkt) != 0) {
487                                 break;
488                         }
489
490                         // Convert pts to our own timebase.
491                         AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
492                         int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
493
494                         // Translate offset into our stream.
495                         if (last_pts == -1) {
496                                 pts_offset = start_pts - pts;
497                         }
498                         pts = std::max(pts + pts_offset, start_pts);
499
500                         //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
501                         //      pkt.stream_index, pts, pkt.size);
502                         FrameOnDisk frame = write_frame(pkt.stream_index, pts, pkt.data, pkt.size, &db);
503
504                         post_to_main_thread([pkt, frame] {
505                                 global_mainwindow->display_frame(pkt.stream_index, frame);
506                         });
507
508                         if (last_pts != -1 && global_flags.slow_down_input) {
509                                 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));
510                         }
511                         last_pts = pts;
512                         current_pts = pts;
513                 }
514
515                 fprintf(stderr, "%s: Hit EOF. Waiting one second and trying again...\n", global_flags.stream_source.c_str());
516                 sleep(1);
517
518                 start_pts = last_pts + TIMEBASE;
519         }
520 }