]> git.sesse.net Git - nageru/blob - main.cpp
Rename ui_foo.ui to foo.ui; seemingly, it is more standard.
[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 "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                 using_egl = true;
72         }
73         setlinebuf(stdout);
74 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
75         av_register_all();
76 #endif
77
78         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
79
80         QSurfaceFormat fmt;
81         fmt.setDepthBufferSize(0);
82         fmt.setStencilBufferSize(0);
83         fmt.setProfile(QSurfaceFormat::CoreProfile);
84         fmt.setMajorVersion(3);
85         fmt.setMinorVersion(1);
86
87         // Turn off vsync, since Qt generally gives us at most frame rate
88         // (display frequency) / (number of QGLWidgets active).
89         fmt.setSwapInterval(0);
90
91         QSurfaceFormat::setDefaultFormat(fmt);
92
93         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
94
95         QApplication app(argc, argv);
96         global_share_widget = new QGLWidget();
97         if (!global_share_widget->isValid()) {
98                 fprintf(stderr, "Failed to initialize OpenGL. Nageru needs at least OpenGL 3.1 to function properly.\n");
99                 exit(1);
100         }
101
102         MainWindow mainWindow;
103         mainWindow.resize(QSize(1500, 910));
104         mainWindow.show();
105
106         app.installEventFilter(&mainWindow);  // For white balance color picking.
107
108         // Even on an otherwise unloaded system, it would seem writing the recording
109         // to disk (potentially terabytes of data as time goes by) causes Nageru
110         // to be pushed out of RAM. If we have the right privileges, simply lock us
111         // into memory for better realtime behavior.
112         if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
113                 perror("mlockall()");
114                 fprintf(stderr, "Failed to lock Nageru into RAM. You probably want to\n");
115                 fprintf(stderr, "increase \"memlock\" for your user in limits.conf\n");
116                 fprintf(stderr, "for better realtime behavior.\n");
117                 uses_mlock = false;
118         } else {
119                 uses_mlock = true;
120         }
121
122         int rc = app.exec();
123         global_mixer->quit();
124         mainWindow.mixer_shutting_down();
125         delete global_mixer;
126         ImageInput::shutdown_updaters();
127         return rc;
128 }