]> git.sesse.net Git - nageru/blob - nageru/main.cpp
cbf66e6fbcccbf029e1a1153c8ce7d54f629e3d5
[nageru] / nageru / main.cpp
1 extern "C" {
2 #include <libavformat/avformat.h>
3 }
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <epoxy/gl.h>  // IWYU pragma: keep
9 #include <QApplication>
10 #include <QCoreApplication>
11 #include <QGL>
12 #include <QSize>
13 #include <QSurfaceFormat>
14 #include <string>
15
16 #ifdef HAVE_CEF
17 #include <cef_app.h>
18 #include <cef_browser.h>
19 #include <cef_client.h>
20 #include <cef_version.h>
21 #endif
22
23 #ifdef HAVE_SRT
24 #include <srt/srt.h>
25 #endif
26
27 #include "basic_stats.h"
28 #ifdef HAVE_CEF
29 #include "nageru_cef_app.h"
30 #endif
31 #include "shared/context.h"
32 #include "flags.h"
33 #include "mainwindow.h"
34 #include "mixer.h"
35 #include "quicksync_encoder.h"
36
37 #ifdef HAVE_CEF
38 CefRefPtr<NageruCefApp> cef_app;
39 #endif
40
41 int main(int argc, char *argv[])
42 {
43 #ifdef HAVE_CEF
44         // Let CEF have first priority on parsing the command line, because we might be
45         // launched as a CEF sub-process.
46         CefMainArgs main_args(argc, argv);
47         cef_app = CefRefPtr<NageruCefApp>(new NageruCefApp());
48         int err = CefExecuteProcess(main_args, cef_app.get(), nullptr);
49         if (err >= 0) {
50                 return err;
51         }
52
53         // CEF wants to use GLib for its main loop, which interferes with Qt's use of it.
54         // The alternative is trying to integrate CEF into Qt's main loop, but that requires
55         // fairly extensive cross-thread communication and that parts of CEF runs on Qt's UI
56         // thread.
57         setenv("QT_NO_GLIB", "1", 0);
58 #endif
59
60         parse_flags(PROGRAM_NAGERU, argc, argv);
61
62         if (global_flags.va_display.empty() && !global_flags.x264_video_to_disk) {
63                 // The user didn't specify a VA-API display, but we need one.
64                 // See if the default works, and if not, let's try to help
65                 // the user by seeing if there's any that would work automatically.
66                 global_flags.va_display = QuickSyncEncoder::get_usable_va_display();
67         }
68
69         // The OpenGL widgets do not work well with the native Wayland integration.
70         setenv("QT_QPA_PLATFORM", "xcb", 0);
71
72         if ((global_flags.va_display.empty() ||
73              global_flags.va_display[0] != '/') && !global_flags.x264_video_to_disk) {
74                 // We normally use EGL for zerocopy, but if we use VA against DRM
75                 // instead of against X11, we turn it off, and then don't need EGL.
76                 setenv("QT_XCB_GL_INTEGRATION", "xcb_egl", 0);
77         }
78         setlinebuf(stdout);
79 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
80         av_register_all();
81 #endif
82
83 #ifdef HAVE_SRT
84         if (global_flags.srt_port >= 0 || !global_flags.srt_destination_host.empty()) {
85                 srt_startup();
86         }
87 #endif
88
89         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
90
91         QSurfaceFormat fmt;
92         fmt.setDepthBufferSize(0);
93         fmt.setStencilBufferSize(0);
94         fmt.setProfile(QSurfaceFormat::CoreProfile);
95         fmt.setMajorVersion(3);
96         fmt.setMinorVersion(1);
97
98         // Turn off vsync, since Qt generally gives us at most frame rate
99         // (display frequency) / (number of QGLWidgets active).
100         fmt.setSwapInterval(0);
101
102         QSurfaceFormat::setDefaultFormat(fmt);
103
104         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
105
106         QApplication app(argc, argv);
107         global_share_widget = new QGLWidget();
108         if (!global_share_widget->isValid()) {
109                 fprintf(stderr, "Failed to initialize OpenGL. Nageru needs at least OpenGL 3.1 to function properly.\n");
110                 abort();
111         }
112
113         MainWindow mainWindow;
114         mainWindow.resize(QSize(1500, 910));
115         mainWindow.show();
116
117         app.installEventFilter(&mainWindow);  // For white balance color picking.
118
119         // Even on an otherwise unloaded system, it would seem writing the recording
120         // to disk (potentially terabytes of data as time goes by) causes Nageru
121         // to be pushed out of RAM. If we have the right privileges, simply lock us
122         // into memory for better realtime behavior.
123         if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
124                 perror("mlockall()");
125                 fprintf(stderr, "Failed to lock Nageru into RAM. You probably want to\n");
126                 fprintf(stderr, "increase \"memlock\" for your user in limits.conf\n");
127                 fprintf(stderr, "for better realtime behavior.\n");
128                 uses_mlock = false;
129         } else {
130                 uses_mlock = true;
131         }
132
133         int rc = app.exec();
134         delete global_mixer;
135 #ifdef HAVE_SRT
136         if (global_flags.srt_port >= 0) {
137                 srt_cleanup();
138         }
139 #endif
140         return rc;
141 }