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