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