]> git.sesse.net Git - nageru/blob - main.cpp
Embed shaders into the binary.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <atomic>
3 #include <chrono>
4 #include <condition_variable>
5 #include <dirent.h>
6 #include <getopt.h>
7 #include <memory>
8 #include <mutex>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <string>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <thread>
15 #include <vector>
16
17 extern "C" {
18 #include <libavformat/avformat.h>
19 }
20
21 #include "clip_list.h"
22 #include "context.h"
23 #include "defs.h"
24 #include "disk_space_estimator.h"
25 #include "ffmpeg_raii.h"
26 #include "flags.h"
27 #include "httpd.h"
28 #include "mainwindow.h"
29 #include "player.h"
30 #include "post_to_main_thread.h"
31 #include "ref_counted_gl_sync.h"
32 #include "timebase.h"
33 #include "ui_mainwindow.h"
34 #include "vaapi_jpeg_decoder.h"
35
36 #include <QApplication>
37 #include <movit/init.h>
38 #include <movit/util.h>
39
40 using namespace std;
41 using namespace std::chrono;
42
43 mutex RefCountedGLsync::fence_lock;
44 atomic<bool> should_quit{false};
45
46 int64_t start_pts = -1;
47
48 // TODO: Replace by some sort of GUI control, I guess.
49 int64_t current_pts = 0;
50
51 string filename_for_frame(unsigned stream_idx, int64_t pts)
52 {
53         char filename[256];
54         snprintf(filename, sizeof(filename), "%s/frames/cam%d-pts%09ld.jpeg",
55                 global_flags.working_directory.c_str(), stream_idx, pts);
56         return filename;
57 }
58
59 mutex frame_mu;
60 vector<int64_t> frames[MAX_STREAMS];
61 HTTPD *global_httpd;
62
63 void load_existing_frames();
64 int record_thread_func();
65
66 int main(int argc, char **argv)
67 {
68         parse_flags(argc, argv);
69         if (optind == argc) {
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];
74         } else {
75                 usage();
76                 exit(1);
77         }
78
79         string frame_dir = global_flags.working_directory + "/frames";
80
81         struct stat st;
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());
86                         exit(1);
87                 }
88         }
89
90         avformat_network_init();
91         global_httpd = new HTTPD;
92
93         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
94
95         QSurfaceFormat fmt;
96         fmt.setDepthBufferSize(0);
97         fmt.setStencilBufferSize(0);
98         fmt.setProfile(QSurfaceFormat::CoreProfile);
99         fmt.setMajorVersion(4);
100         fmt.setMinorVersion(5);
101
102         // Turn off vsync, since Qt generally gives us at most frame rate
103         // (display frequency) / (number of QGLWidgets active).
104         fmt.setSwapInterval(0);
105
106         QSurfaceFormat::setDefaultFormat(fmt);
107
108         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
109
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");
114                 exit(1);
115         }
116
117         // Initialize Movit.
118         {
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.
125         }
126
127         MainWindow main_window;
128         main_window.show();
129
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);
132
133         init_jpeg_vaapi();
134
135         load_existing_frames();
136         thread record_thread(record_thread_func);
137
138         int ret = app.exec();
139
140         should_quit = true;
141         record_thread.join();
142         JPEGFrameView::shutdown();
143
144         return ret;
145 }
146
147 void load_existing_frames()
148 {
149         string frame_dir = global_flags.working_directory + "/frames";
150         DIR *dir = opendir(frame_dir.c_str());
151         if (dir == nullptr) {
152                 perror("frames/");
153                 start_pts = 0;
154                 return;
155         }
156
157         for ( ;; ) {
158                 errno = 0;
159                 dirent *de = readdir(dir);
160                 if (de == nullptr) {
161                         if (errno != 0) {
162                                 perror("readdir");
163                                 exit(1);
164                         }
165                         break;
166                 }
167
168                 int stream_idx;
169                 int64_t pts;
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);
174                 }
175         }
176
177         closedir(dir);
178
179         if (start_pts == -1) {
180                 start_pts = 0;
181         } else {
182                 // Add a gap of one second from the old frames to the new ones.
183                 start_pts += TIMEBASE;
184         }
185
186         for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
187                 sort(frames[stream_idx].begin(), frames[stream_idx].end());
188         }
189 }
190
191 int record_thread_func()
192 {
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());
196                 return 1;
197         }
198
199         int64_t last_pts = -1;
200         int64_t pts_offset;
201
202         while (!should_quit.load()) {
203                 AVPacket pkt;
204                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
205                         &pkt, av_packet_unref);
206                 av_init_packet(&pkt);
207                 pkt.data = nullptr;
208                 pkt.size = 0;
209
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) {
213                         break;
214                 }
215
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 });
219
220                 // Translate offset into our stream.
221                 if (last_pts == -1) {
222                         pts_offset = start_pts - pts;
223                 }
224                 pts = std::max(pts + pts_offset, start_pts);
225
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");
230                 if (fp == nullptr) {
231                         perror(filename.c_str());
232                         exit(1);
233                 }
234                 fwrite(pkt.data, pkt.size, 1, fp);
235                 fclose(fp);
236
237                 global_disk_space_estimator->report_write(filename, pts);
238
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);
248                         }
249                 });
250
251                 assert(pkt.stream_index < MAX_STREAMS);
252                 frames[pkt.stream_index].push_back(pts);
253
254                 if (last_pts != -1 && global_flags.slow_down_input) {
255                         this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));
256                 }
257                 last_pts = pts;
258                 current_pts = pts;
259         }
260
261         return 0;
262 }