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