]> git.sesse.net Git - nageru/blob - main.cpp
Make for somewhat cleaner shutdown.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <dirent.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <sys/types.h>
6
7 #include <atomic>
8 #include <chrono>
9 #include <condition_variable>
10 #include <memory>
11 #include <mutex>
12 #include <string>
13 #include <thread>
14 #include <vector>
15
16 extern "C" {
17 #include <libavformat/avformat.h>
18 }
19
20 #include <QApplication>
21
22 #include <movit/init.h>
23 #include <movit/util.h>
24
25 #include "clip_list.h"
26 #include "context.h"
27 #include "defs.h"
28 #include "mainwindow.h"
29 #include "ffmpeg_raii.h"
30 #include "httpd.h"
31 #include "player.h"
32 #include "post_to_main_thread.h"
33 #include "ref_counted_gl_sync.h"
34 #include "timebase.h"
35 #include "ui_mainwindow.h"
36 #include "vaapi_jpeg_decoder.h"
37
38 using namespace std;
39 using namespace std::chrono;
40
41 mutex RefCountedGLsync::fence_lock;
42 atomic<bool> should_quit{false};
43
44 int64_t start_pts = -1;
45
46 // TODO: Replace by some sort of GUI control, I guess.
47 int64_t current_pts = 0;
48
49 string filename_for_frame(unsigned stream_idx, int64_t pts)
50 {
51         char filename[256];
52         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
53         return filename;
54 }
55
56 mutex frame_mu;
57 vector<int64_t> frames[MAX_STREAMS];
58 HTTPD *global_httpd;
59
60 void load_existing_frames();
61 int record_thread_func();
62
63 int main(int argc, char **argv)
64 {
65         avformat_network_init();
66         global_httpd = new HTTPD;
67         global_httpd->start(DEFAULT_HTTPD_PORT);
68
69         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
70
71         QSurfaceFormat fmt;
72         fmt.setDepthBufferSize(0);
73         fmt.setStencilBufferSize(0);
74         fmt.setProfile(QSurfaceFormat::CoreProfile);
75         fmt.setMajorVersion(4);
76         fmt.setMinorVersion(5);
77
78         // Turn off vsync, since Qt generally gives us at most frame rate
79         // (display frequency) / (number of QGLWidgets active).
80         fmt.setSwapInterval(0);
81
82         QSurfaceFormat::setDefaultFormat(fmt);
83
84         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
85
86         QApplication app(argc, argv);
87         global_share_widget = new QGLWidget();
88         if (!global_share_widget->isValid()) {
89                 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
90                 exit(1);
91         }
92
93         // Initialize Movit.
94         {
95                 QSurface *surface = create_surface();
96                 QOpenGLContext *context = create_context(surface);
97                 make_current(context, surface);
98                 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
99                 delete_context(context);
100                 // TODO: Delete the surface, too.
101         }
102
103         MainWindow mainWindow;
104         mainWindow.show();
105
106         init_jpeg_vaapi();
107
108         load_existing_frames();
109         thread record_thread(record_thread_func);
110
111         int ret = app.exec();
112
113         should_quit = true;
114         record_thread.join();
115         JPEGFrameView::shutdown();
116
117         return ret;
118 }
119
120 void load_existing_frames()
121 {
122         DIR *dir = opendir("frames/");
123         if (dir == nullptr) {
124                 perror("frames/");
125                 start_pts = 0;
126                 return;
127         }
128
129         for ( ;; ) {
130                 errno = 0;
131                 dirent *de = readdir(dir);
132                 if (de == nullptr) {
133                         if (errno != 0) {
134                                 perror("readdir");
135                                 exit(1);
136                         }
137                         break;
138                 }
139
140                 int stream_idx;
141                 int64_t pts;
142                 if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
143                     stream_idx >= 0 && stream_idx < MAX_STREAMS) {
144                         frames[stream_idx].push_back(pts);
145                         start_pts = max(start_pts, pts);
146                 }
147         }
148
149         closedir(dir);
150
151         if (start_pts == -1) {
152                 start_pts = 0;
153         } else {
154                 // Add a gap of one second from the old frames to the new ones.
155                 start_pts += TIMEBASE;
156         }
157
158         for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
159                 sort(frames[stream_idx].begin(), frames[stream_idx].end());
160         }
161 }
162
163 int record_thread_func()
164 {
165         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
166         if (format_ctx == nullptr) {
167                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
168                 return 1;
169         }
170
171         int64_t last_pts = -1;
172         int64_t pts_offset;
173
174         while (!should_quit.load()) {
175                 AVPacket pkt;
176                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
177                         &pkt, av_packet_unref);
178                 av_init_packet(&pkt);
179                 pkt.data = nullptr;
180                 pkt.size = 0;
181
182                 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
183                 // right now, should_quit will be ignored if it's hung on I/O.
184                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
185                         break;
186                 }
187
188                 // Convert pts to our own timebase.
189                 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
190                 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
191
192                 // Translate offset into our stream.
193                 if (last_pts == -1) {
194                         pts_offset = start_pts - pts;
195                 }
196                 pts = std::max(pts + pts_offset, start_pts);
197
198                 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
199                 //      pkt.stream_index, pts, pkt.size);
200                 string filename = filename_for_frame(pkt.stream_index, pts);
201                 FILE *fp = fopen(filename.c_str(), "wb");
202                 if (fp == nullptr) {
203                         perror(filename.c_str());
204                         exit(1);
205                 }
206                 fwrite(pkt.data, pkt.size, 1, fp);
207                 fclose(fp);
208
209                 post_to_main_thread([pkt, pts] {
210                         if (pkt.stream_index == 0) {
211                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
212                         } else if (pkt.stream_index == 1) {
213                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
214                         } else if (pkt.stream_index == 2) {
215                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
216                         } else if (pkt.stream_index == 3) {
217                                 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
218                         }
219                 });
220
221                 assert(pkt.stream_index < MAX_STREAMS);
222                 frames[pkt.stream_index].push_back(pts);
223
224                 // Hack. Remove when we're dealing with live streams.
225                 if (last_pts != -1) {
226                         this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));
227                 }
228                 last_pts = pts;
229                 current_pts = pts;
230         }
231
232         return 0;
233 }