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