]> git.sesse.net Git - nageru/blob - main.cpp
Import a bunch of http/mux code from Nageru.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdint.h>
4
5 #include <chrono>
6 #include <condition_variable>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <thread>
11 #include <vector>
12
13 extern "C" {
14 #include <libavformat/avformat.h>
15 }
16
17 #include <QApplication>
18
19 #include "clip_list.h"
20 #include "defs.h"
21 #include "mainwindow.h"
22 #include "ffmpeg_raii.h"
23 #include "httpd.h"
24 #include "player.h"
25 #include "post_to_main_thread.h"
26 #include "ui_mainwindow.h"
27
28 using namespace std;
29 using namespace std::chrono;
30
31 // TODO: Replace by some sort of GUI control, I guess.
32 int64_t current_pts = 0;
33
34 string filename_for_frame(unsigned stream_idx, int64_t pts)
35 {
36         char filename[256];
37         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
38         return filename;
39 }
40
41 mutex frame_mu;
42 vector<int64_t> frames[MAX_STREAMS];
43 QGLWidget *global_share_widget;
44 HTTPD *global_httpd;
45
46 int record_thread_func();
47
48 int main(int argc, char **argv)
49 {
50         avformat_network_init();
51         global_httpd = new HTTPD;
52         global_httpd->start(DEFAULT_HTTPD_PORT);
53
54         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
55
56         QSurfaceFormat fmt;
57         fmt.setDepthBufferSize(0);
58         fmt.setStencilBufferSize(0);
59         fmt.setProfile(QSurfaceFormat::CoreProfile);
60         fmt.setMajorVersion(3);
61         fmt.setMinorVersion(1);
62
63         // Turn off vsync, since Qt generally gives us at most frame rate
64         // (display frequency) / (number of QGLWidgets active).
65         fmt.setSwapInterval(0);
66
67         QSurfaceFormat::setDefaultFormat(fmt);
68
69         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
70
71         QApplication app(argc, argv);
72         global_share_widget = new QGLWidget();
73         if (!global_share_widget->isValid()) {
74                 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 3.1 to function properly.\n");
75                 exit(1);
76         }
77         MainWindow mainWindow;
78         mainWindow.show();
79
80         thread(record_thread_func).detach();
81
82         return app.exec();
83 }
84
85 int record_thread_func()
86 {
87         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
88         if (format_ctx == nullptr) {
89                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
90                 return 1;
91         }
92
93         int64_t last_pts = -1;
94
95         for ( ;; ) {
96                 AVPacket pkt;
97                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
98                         &pkt, av_packet_unref);
99                 av_init_packet(&pkt);
100                 pkt.data = nullptr;
101                 pkt.size = 0;
102                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
103                         break;
104                 }
105                 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
106                 //      pkt.stream_index, pkt.pts, pkt.size);
107                 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
108                 FILE *fp = fopen(filename.c_str(), "wb");
109                 if (fp == nullptr) {
110                         perror(filename.c_str());
111                         exit(1);
112                 }
113                 fwrite(pkt.data, pkt.size, 1, fp);
114                 fclose(fp);
115
116                 post_to_main_thread([pkt] {
117                         if (pkt.stream_index == 0) {
118                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
119                         } else if (pkt.stream_index == 1) {
120                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
121                         } else if (pkt.stream_index == 2) {
122                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
123                         } else if (pkt.stream_index == 3) {
124                                 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
125                         }
126                 });
127
128                 assert(pkt.stream_index < MAX_STREAMS);
129                 frames[pkt.stream_index].push_back(pkt.pts);
130
131                 // Hack. Assumes a given timebase.
132                 if (last_pts != -1) {
133                         this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
134                 }
135                 last_pts = pkt.pts;
136                 current_pts = pkt.pts;
137         }
138
139         return 0;
140 }