Debian Codegraph @

src/main.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2022, 2023, 2024, 2025, 2026
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cstdio>
22
23 #include <QApplication>
24 #include <QSettings>
25 #include <QTranslator>
26 #include <QCommandLineParser>
27 #include <QFileInfo>
28 #include <QUrl>
29 #include <QMessageBox>
30 #include <QMediaDevices>
31 #include <QAudioDevice>
32 #include <QCameraDevice>
33 #include <QAudioOutput>
34 #include <QAudioInput>
35 #include <QCamera>
36 #include <QSurfaceFormat>
37 #include <QOpenGLContext>
38 #include <QWindowCapture>
39 #include <QtSystemDetection>
40 #include <QtProcessorDetection>
41 #include <QtVersion>
42
43 #ifdef WITH_QVR
44 # include <qvr/manager.hpp>
45 #endif
46
47 #include "version.hpp"
48 #include "log.hpp"
49 #include "videosink.hpp"
50 #include "playlist.hpp"
51 #include "metadata.hpp"
52 #include "screen.hpp"
53 #include "qvrapp.hpp"
54 #include "gui.hpp"
55 #include "commandinterpreter.hpp"
56 #include "modes.hpp"
57 #include "tools.hpp"
58 #include "bino.hpp"
59
60
61 void logQtMsg(QtMsgType type, const QMessageLogContext&, const QString& msg)
62 {
63 switch (type) {
64 case QtDebugMsg:
65 LOG_DEBUG("Qt debug: %s", qPrintable(msg));
66 break;
67 case QtInfoMsg:
68 LOG_INFO("Qt info: %s", qPrintable(msg));
69 break;
70 case QtWarningMsg:
71 LOG_WARNING("Qt warning: %s", qPrintable(msg));
72 break;
73 case QtCriticalMsg:
74 LOG_FATAL("Qt critical: %s", qPrintable(msg));
75 break;
76 case QtFatalMsg:
77 LOG_FATAL("Qt fatal: %s", qPrintable(msg));
78 break;
79 }
80 }
81
82 int main(int argc, char* argv[])
83 {
84 // Early check before command line options are consumed: is this process a QVR child process?
85 bool vrChildProcess = false;
86 #ifdef WITH_QVR
87 for (int i = 1; i < argc; i++) {
88 if (QString(argv[i]).startsWith("--qvr-process")) {
89 vrChildProcess = true;
90 break;
91 }
92 }
93 #endif
94
95 // Initialize Qt
96 qInstallMessageHandler(logQtMsg);
97 QApplication app(argc, argv);
98 QApplication::setApplicationName("Bino");
99 QApplication::setApplicationVersion(BINO_VERSION);
100 #ifdef WITH_QVR
101 QVRManager manager(argc, argv);
102 #endif
103 QTranslator qtTranslator;
104 if (qtTranslator.load(QLocale(), "qt", "_", QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
105 app.installTranslator(&qtTranslator);
106 }
107 QTranslator binoTranslator;
108 if (binoTranslator.load(QLocale(), "bino", "_", ":/i18n")) {
109 app.installTranslator(&binoTranslator);
110 }
111 QCoreApplication::setOrganizationName("Bino3D");
112 QCoreApplication::setOrganizationDomain("bino3d.org");
113 QCoreApplication::setApplicationName("Bino");
114 QSettings settings;
115
116 // Process command line
117 QCommandLineParser parser;
118 parser.setApplicationDescription(QCommandLineParser::tr("3D video player -- see https://bino3d.org"));
119 parser.addPositionalArgument("[URL...]", QCommandLineParser::tr("Media to play."));
120 parser.addHelpOption();
121 parser.addVersionOption();
122 parser.addOption({ "log-level",
123 QCommandLineParser::tr("Set log level (%1).").arg("fatal, warning, info, debug, firehose"),
124 "level" });
125 parser.addOption({ "log-file",
126 QCommandLineParser::tr("Set log file."),
127 "file" });
128 parser.addOption({ "read-commands",
129 QCommandLineParser::tr("Read commands from a script file."), "script" });
130 parser.addOption({ "disable-stereo",
131 QCommandLineParser::tr("Disable OpenGL quad-buffered stereo support.") });
132 parser.addOption({ "opengles",
133 QCommandLineParser::tr("Use OpenGL ES instead of Desktop OpenGL.") });
134 parser.addOption({ "vr",
135 QCommandLineParser::tr("Start in VR mode instead of GUI mode.")});
136 parser.addOption({ "vr-screen",
137 QCommandLineParser::tr("Set VR screen geometry, either as the special values 'united' or 'intersected', or "
138 "as a comma-separated list of nine values representing three 3D coordinates that define a planar screen (bottom left, bottom right, top left), "
139 "or as a an aspect ratio followed by the name of an OBJ file that contains the screen geometry with texture coordinates "
140 "(example: '16:9,myscreen.obj')."),
141 "screen" });
142 parser.addOption({ "vr-show-devices",
143 QCommandLineParser::tr("Set show-devices mode (%1).").arg("off, on"),
144 "mode" });
145 parser.addOption({ "capture",
146 QCommandLineParser::tr("Capture audio/video input from microphone and camera/screen/window.") });
147 parser.addOption({ "list-audio-outputs",
148 QCommandLineParser::tr("List audio outputs.") });
149 parser.addOption({ "list-audio-inputs",
150 QCommandLineParser::tr("List audio inputs.") });
151 parser.addOption({ "list-video-inputs",
152 QCommandLineParser::tr("List video inputs.") });
153 parser.addOption({ "list-screen-inputs",
154 QCommandLineParser::tr("List screen inputs.") });
155 parser.addOption({ "list-window-inputs",
156 QCommandLineParser::tr("List window inputs.") });
157 parser.addOption({ "audio-output",
158 QCommandLineParser::tr("Choose audio output via its index."),
159 "ao" });
160 parser.addOption({ "audio-input",
161 QCommandLineParser::tr("Choose audio input via its index. Can be empty."),
162 "ai" });
163 parser.addOption({ "video-input",
164 QCommandLineParser::tr("Choose video input via its index."),
165 "vi" });
166 parser.addOption({ "screen-input",
167 QCommandLineParser::tr("Choose screen input via its index."),
168 "si" });
169 parser.addOption({ "window-input",
170 QCommandLineParser::tr("Choose window input via its index."),
171 "si" });
172 parser.addOption({ "list-tracks",
173 QCommandLineParser::tr("List all video, audio and subtitle tracks in the media.") });
174 parser.addOption({ "preferred-audio",
175 QCommandLineParser::tr("Set preferred audio track language (en, de, fr, ...)."),
176 "lang" });
177 parser.addOption({ "preferred-subtitle",
178 QCommandLineParser::tr("Set preferred subtitle track language (en, de, fr, ...). Can be empty."),
179 "lang" });
180 parser.addOption({ "video-track",
181 QCommandLineParser::tr("Choose video track via its index."), "track" });
182 parser.addOption({ "audio-track",
183 QCommandLineParser::tr("Choose audio track via its index."), "track" });
184 parser.addOption({ "subtitle-track",
185 QCommandLineParser::tr("Choose subtitle track via its index. Can be empty."),
186 "track" });
187 parser.addOption({ { "p", "playlist" },
188 QCommandLineParser::tr("Load playlist."),
189 "file" });
190 parser.addOption({ { "l", "loop" },
191 QCommandLineParser::tr("Set loop mode (%1).").arg("off, one, all"),
192 "mode" });
193 parser.addOption({ { "w", "wait" },
194 QCommandLineParser::tr("Set wait mode (%1).").arg("off, on"),
195 "mode" });
196 parser.addOption({ { "i", "input" },
197 QCommandLineParser::tr("Set input mode (%1).").arg("mono, "
198 "top-bottom, top-bottom-half, bottom-top, bottom-top-half, "
199 "left-right, left-right-half, right-left, right-left-half, "
200 "alternating-left-right, alternating-right-left"),
201 "mode" });
202 parser.addOption({ { "o", "output" },
203 QCommandLineParser::tr("Set output mode (%1).").arg("left, right, stereo, alternating, "
204 "hdmi-frame-pack, "
205 "left-right, left-right-half, right-left, right-left-half, "
206 "top-bottom, top-bottom-half, bottom-top, bottom-top-half, "
207 "even-odd-rows, even-odd-columns, checkerboard, "
208 "red-cyan-dubois, red-cyan-full-color, red-cyan-half-color, red-cyan-monochrome, "
209 "green-magenta-dubois, green-magenta-full-color, green-magenta-half-color, green-magenta-monochrome, "
210 "amber-blue-dubois, amber-blue-full-color, amber-blue-half-color, amber-blue-monochrome, "
211 "red-green-monochrome, red-blue-monochrome"),
212 "mode" });
213 parser.addOption({ "surround",
214 QCommandLineParser::tr("Set surround mode (%1).").arg("360, 180, off"),
215 "mode" });
216 parser.addOption({ "surround-vfov",
217 QCommandLineParser::tr("Set surround vertical field of view (default 50, range 5-115)."),
218 "degrees" });
219 parser.addOption({ { "S", "swap-eyes" },
220 QCommandLineParser::tr("Swap left/right eye.") });
221 parser.addOption({ { "f", "fullscreen" },
222 QCommandLineParser::tr("Start in fullscreen mode.") });
223 parser.process(app);
224
225 // Initialize logging
226 SetLogLevel(Log_Level_Warning);
227 if (parser.isSet("log-file")) {
228 SetLogFile(qPrintable(parser.value("log-file")), true);
229 }
230 if (parser.isSet("log-level")) {
231 if (parser.value("log-level") == "fatal")
232 SetLogLevel(Log_Level_Fatal);
233 else if (parser.value("log-level") == "warning")
234 SetLogLevel(Log_Level_Warning);
235 else if (parser.value("log-level") == "info")
236 SetLogLevel(Log_Level_Info);
237 else if (parser.value("log-level") == "debug")
238 SetLogLevel(Log_Level_Debug);
239 else if (parser.value("log-level") == "firehose")
240 SetLogLevel(Log_Level_Firehose);
241 else {
242 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--log-level")));
243 return 1;
244 }
245 }
246 LOG_DEBUG("Bino version " BINO_VERSION);
247 LOG_DEBUG("Built against Qt version " QT_VERSION_STR);
248 LOG_DEBUG("Running with Qt version %s", qVersion());
249 LOG_DEBUG("Running on Qt platform %s", qPrintable(QGuiApplication::platformName()));
250
251 // Check if VR mode is available if requested
252 #ifndef WITH_QVR
253 if (parser.isSet("vr")) {
254 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("VR mode unavailable - recompile Bino with QVR support!")));
255 return 1;
256 }
257 #endif
258
259 // Set modes
260 SurroundMode surroundMode = Surround_Unknown;
261 if (parser.isSet("surround")) {
262 bool ok;
263 surroundMode = surroundModeFromString(parser.value("surround"), &ok);
264 if (!ok) {
265 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--surround")));
266 return 1;
267 }
268 }
269 float surroundVerticalFOV = 50.0f;
270 if (parser.isSet("surround-vfov")) {
271 bool ok;
272 surroundVerticalFOV = parser.value("surround-vfov").toFloat(&ok);
273 if (!ok || surroundVerticalFOV < 5.0f || surroundVerticalFOV > 115.0f) {
274 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--surround-vfov")));
275 return 1;
276 }
277 }
278 InputMode inputMode = Input_Unknown;
279 if (parser.isSet("input")) {
280 bool ok;
281 inputMode = inputModeFromString(parser.value("input"), &ok);
282 if (!ok) {
283 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--input")));
284 return 1;
285 }
286 }
287 OutputMode outputMode = Output_Red_Cyan_Dubois;
288 if (parser.isSet("output")) {
289 bool ok;
290 outputMode = outputModeFromString(parser.value("output"), &ok);
291 if (!ok) {
292 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--output")));
293 return 1;
294 }
295 }
296
297 // Lists of available devices. Initialize these lists only when necessary because
298 // this can take some time!
299 QList<QAudioDevice> audioOutputDevices;
300 QList<QAudioDevice> audioInputDevices;
301 QList<QCameraDevice> videoInputDevices;
302 QList<QScreen*> screenInputDevices;
303 QList<QCapturableWindow> windowInputDevices;
304
305 // List devices
306 bool deviceListRequested = false;
307 if (parser.isSet("list-audio-outputs")) {
308 audioOutputDevices = QMediaDevices::audioOutputs();
309 if (audioOutputDevices.size() == 0) {
310 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("No audio outputs available.")));
311 } else {
312 for (qsizetype i = 0; i < audioOutputDevices.size(); i++)
313 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("Audio output %1: %2").arg(i).arg(audioOutputDevices[i].description())));
314 }
315 deviceListRequested = true;
316 }
317 if (parser.isSet("list-audio-inputs")) {
318 audioInputDevices = QMediaDevices::audioInputs();
319 if (audioInputDevices.size() == 0) {
320 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("No audio inputs available.")));
321 } else {
322 for (qsizetype i = 0; i < audioInputDevices.size(); i++)
323 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("Audio input %1: %2").arg(i).arg(audioInputDevices[i].description())));
324 }
325 deviceListRequested = true;
326 }
327 if (parser.isSet("list-video-inputs")) {
328 videoInputDevices = QMediaDevices::videoInputs();
329 if (videoInputDevices.size() == 0) {
330 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("No video inputs available.")));
331 } else {
332 for (qsizetype i = 0; i < videoInputDevices.size(); i++)
333 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("Video input %1: %2").arg(i).arg(videoInputDevices[i].description())));
334 }
335 deviceListRequested = true;
336 }
337 if (parser.isSet("list-screen-inputs")) {
338 screenInputDevices = QGuiApplication::screens();
339 if (screenInputDevices.size() == 0) {
340 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("No screen inputs available.")));
341 } else {
342 for (qsizetype i = 0; i < screenInputDevices.size(); i++)
343 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("Screen input %1: %2").arg(i).arg(screenInputDevices[i]->name())));
344 }
345 deviceListRequested = true;
346 }
347 if (parser.isSet("list-window-inputs")) {
348 windowInputDevices = QWindowCapture::capturableWindows();
349 if (windowInputDevices.size() == 0) {
350 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("No window inputs available.")));
351 } else {
352 for (qsizetype i = 0; i < windowInputDevices.size(); i++)
353 LOG_REQUESTED("%s", qPrintable(QCommandLineParser::tr("Window input %1: %2").arg(i).arg(windowInputDevices[i].description())));
354 }
355 deviceListRequested = true;
356 }
357 if (deviceListRequested) {
358 return 0;
359 }
360
361 // Get requested device indices; -1 means default
362 int audioOutputDeviceIndex = -1;
363 int audioInputDeviceIndex = -1;
364 int videoInputDeviceIndex = -1;
365 int screenInputDeviceIndex = -1;
366 int windowInputDeviceIndex = -1;
367 if (parser.isSet("audio-output")) {
368 audioOutputDevices = QMediaDevices::audioOutputs();
369 bool ok;
370 int ao = parser.value("audio-output").toInt(&ok);
371 if (ok && ao >= 0 && ao < audioOutputDevices.size()) {
372 audioOutputDeviceIndex = ao;
373 } else {
374 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--audio-output")));
375 return 1;
376 }
377 }
378 if (parser.isSet("capture")) {
379 bool ok;
380 if (parser.isSet("audio-input")) {
381 if (parser.value("audio-input").length() == 0) {
382 audioInputDeviceIndex = -2; // this means no audio input
383 } else {
384 if (audioInputDevices.size() == 0)
385 audioInputDevices = QMediaDevices::audioInputs();
386 int ai = parser.value("audio-input").toInt(&ok);
387 if (ok && ai >= 0 && ai < audioInputDevices.size()) {
388 audioInputDeviceIndex = ai;
389 } else {
390 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--audio-input")));
391 return 1;
392 }
393 }
394 }
395 if (parser.isSet("video-input")) {
396 if (videoInputDevices.size() == 0)
397 videoInputDevices = QMediaDevices::videoInputs();
398 int vi = parser.value("video-input").toInt(&ok);
399 if (ok && vi >= 0 && vi < videoInputDevices.size()) {
400 videoInputDeviceIndex = vi;
401 } else {
402 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--video-input")));
403 return 1;
404 }
405 }
406 if (parser.isSet("screen-input")) {
407 if (screenInputDevices.size() == 0)
408 screenInputDevices = QGuiApplication::screens();
409 int vi = parser.value("screen-input").toInt(&ok);
410 if (ok && vi >= 0 && vi < screenInputDevices.size()) {
411 screenInputDeviceIndex = vi;
412 } else {
413 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--screen-input")));
414 return 1;
415 }
416 }
417 if (parser.isSet("window-input")) {
418 if (windowInputDevices.size() == 0)
419 windowInputDevices = QWindowCapture::capturableWindows();
420 int vi = parser.value("window-input").toInt(&ok);
421 if (ok && vi >= 0 && vi < windowInputDevices.size()) {
422 windowInputDeviceIndex = vi;
423 } else {
424 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--window-input")));
425 return 1;
426 }
427 }
428 }
429
430 // Get playlist.
431 Playlist playlist;
432 if (parser.isSet("preferred-audio")) {
433 QLocale::Language lang = QLocale::codeToLanguage(parser.value("preferred-audio"));
434 if (lang == QLocale::AnyLanguage) {
435 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--preferred-audio")));
436 return 1;
437 } else {
438 playlist.setPreferredAudio(lang);
439 }
440 }
441 if (parser.isSet("preferred-subtitle")) {
442 if (parser.value("preferred-subtitle").length() == 0) {
443 playlist.setWantSubtitle(false);
444 } else {
445 QLocale::Language lang = QLocale::codeToLanguage(parser.value("preferred-subtitle"));
446 if (lang == QLocale::AnyLanguage) {
447 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--preferred-subtitle")));
448 return 1;
449 } else {
450 playlist.setWantSubtitle(true);
451 playlist.setPreferredSubtitle(lang);
452 }
453 }
454 }
455 if (parser.isSet("loop")) {
456 bool ok;
457 LoopMode loopMode = loopModeFromString(parser.value("loop"), &ok);
458 if (!ok) {
459 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--loop")));
460 return 1;
461 }
462 playlist.setLoopMode(loopMode);
463 }
464 if (parser.isSet("wait")) {
465 bool ok;
466 WaitMode waitMode = waitModeFromString(parser.value("wait"), &ok);
467 if (!ok) {
468 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--wait")));
469 return 1;
470 }
471 playlist.setWaitMode(waitMode);
472 }
473 int videoTrack = PlaylistEntry::DefaultTrack;
474 int audioTrack = PlaylistEntry::DefaultTrack;
475 int subtitleTrack = PlaylistEntry::DefaultTrack;
476 if (parser.isSet("video-track")) {
477 bool ok;
478 int vt = parser.value("video-track").toInt(&ok);
479 if (ok && vt >= 0) {
480 videoTrack = vt;
481 } else {
482 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--video-track")));
483 return 1;
484 }
485 }
486 if (parser.isSet("audio-track")) {
487 bool ok;
488 int at = parser.value("audio-track").toInt(&ok);
489 if (ok && at >= 0) {
490 audioTrack = at;
491 } else {
492 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--audio-track")));
493 return 1;
494 }
495 }
496 if (parser.isSet("subtitle-track")) {
497 if (parser.value("subtitle-track").length() == 0) {
498 subtitleTrack = PlaylistEntry::NoTrack;
499 } else {
500 bool ok;
501 int st = parser.value("subtitle-track").toInt(&ok);
502 if (ok && st >= 0) {
503 subtitleTrack = st;
504 } else {
505 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--subtitle-track")));
506 return 1;
507 }
508 }
509 }
510 for (qsizetype i = 0; i < parser.positionalArguments().length(); i++) {
511 QUrl url = parser.positionalArguments()[i];
512 if (url.isRelative()) {
513 QFileInfo fileInfo(parser.positionalArguments()[i]);
514 if (fileInfo.exists()) {
515 url = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
516 } else {
517 LOG_WARNING("%s", qPrintable(QCommandLineParser::tr("File does not exist: %1").arg(parser.positionalArguments()[i])));
518 continue;
519 }
520 }
521 playlist.append(PlaylistEntry(url, inputMode, surroundMode,
522 videoTrack, audioTrack, subtitleTrack));
523 }
524 if (parser.positionalArguments().length() > 0 && playlist.length() == 0) {
525 return 1;
526 }
527 if (parser.isSet("playlist")) {
528 QString errStr;
529 if (!playlist.load(parser.value("playlist"), errStr)) {
530 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("%1: %2")
531 .arg(parser.value("playlist"))
532 .arg(errStr.isEmpty() ? QString("invalid playlist file") : errStr)));
533 return 1;
534 }
535 }
536 if (!parser.isSet("wait")) {
537 playlist.setWaitModeAuto();
538 }
539 if (parser.isSet("capture") && playlist.length() > 0) {
540 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Cannot capture and play URL at the same time.")));
541 return 1;
542 }
543
544 // List tracks
545 if (parser.isSet("list-tracks")) {
546 MetaData metaData;
547 for (qsizetype i = 0; i < playlist.length(); i++) {
548 if (!metaData.detectCached(playlist.entries()[i].url))
549 return 1;
550 LOG_REQUESTED("%s", qPrintable(metaData.url.toString()));
551 for (qsizetype l = 0; l < metaData.global.keys().size(); l++) {
552 LOG_REQUESTED(" %s: %s",
553 qPrintable(QMediaMetaData::metaDataKeyToString(metaData.global.keys()[l])),
554 qPrintable(metaData.global.stringValue(metaData.global.keys()[l])));
555 }
556 for (int k = 0; k < 3; k++) {
557 QString trackType =
558 (k == 0 ? QCommandLineParser::tr("video")
559 : k == 1 ? QCommandLineParser::tr("audio")
560 : QCommandLineParser::tr("subtitle"));
561 const QList<QMediaMetaData>& metaDataList =
562 (k == 0 ? metaData.videoTracks : k == 1 ? metaData.audioTracks : metaData.subtitleTracks);
563 if (metaDataList.size() == 0) {
564 LOG_REQUESTED(" %s", qPrintable(QCommandLineParser::tr("no %1 tracks").arg(trackType)));
565 } else {
566 for (qsizetype l = 0; l < metaDataList.size(); l++) {
567 LOG_REQUESTED(" %s", qPrintable(QCommandLineParser::tr("%1 track %2").arg(qPrintable(trackType)).arg(l)));
568 QMediaMetaData lmd = metaDataList[l];
569 for (qsizetype m = 0; m < lmd.keys().size(); m++) {
570 QMediaMetaData::Key key = lmd.keys()[m];
571 if (metaData.global.stringValue(key) == lmd.stringValue(key))
572 continue;
573 LOG_REQUESTED(" %s: %s",
574 qPrintable(QMediaMetaData::metaDataKeyToString(key)),
575 qPrintable(lmd.stringValue(key)));
576 }
577 }
578 }
579 }
580 }
581 return 0;
582 }
583
584 // Handle the VR parameters
585 Bino::ScreenType screenType = Bino::ScreenGeometry;
586 Screen screen; // default screen for non-VR mode
587 bool vrShowDevices = true;
588 if (parser.isSet("vr")) {
589 float screenCenterHeight = 1.76f - 0.15f; // does not matter, never used
590 #ifdef WITH_QVR
591 screenCenterHeight = QVRObserverConfig::defaultEyeHeight;
592 #endif
593 screen = Screen( // default screen for VR mode
594 QVector3D(-16.0f / 9.0f, -1.0f + screenCenterHeight, -8.0f),
595 QVector3D(+16.0f / 9.0f, -1.0f + screenCenterHeight, -8.0f),
596 QVector3D(-16.0f / 9.0f, +1.0f + screenCenterHeight, -8.0f));
597 if (parser.isSet("vr-screen")) {
598 QStringList paramList = parser.value("vr-screen").split(',');
599 float values[9];
600 if (parser.value("vr-screen") == "united") {
601 screenType = Bino::ScreenUnited;
602 } else if (parser.value("vr-screen") == "intersected") {
603 screenType = Bino::ScreenIntersected;
604 } else if (paramList.length() == 9
605 && 9 == std::sscanf(qPrintable(parser.value("vr-screen")),
606 "%f,%f,%f,%f,%f,%f,%f,%f,%f",
607 values + 0, values + 1, values + 2,
608 values + 3, values + 4, values + 5,
609 values + 6, values + 7, values + 8)) {
610 screenType = Bino::ScreenGeometry;
611 screen = Screen(
612 QVector3D(values[0], values[1], values[2]),
613 QVector3D(values[3], values[4], values[5]),
614 QVector3D(values[6], values[7], values[8]));
615 } else if (paramList.length() == 2) {
616 screenType = Bino::ScreenGeometry;
617 float ar;
618 float ar2[2];
619 if (2 == std::sscanf(qPrintable(paramList[0]), "%f:%f", ar2 + 0, ar2 + 1)) {
620 ar = ar2[0] / ar2[1];
621 } else if (1 != std::sscanf(qPrintable(paramList[0]), "%f", &ar)) {
622 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--vr-screen")));
623 return 1;
624 }
625 screen = Screen(paramList[1], "", ar);
626 if (screen.indices.size() == 0)
627 return 1;
628 } else {
629 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--vr-screen")));
630 return 1;
631 }
632 } else {
633 LOG_DEBUG("using default VR screen");
634 }
635 if (parser.isSet("vr-show-devices")) {
636 if (parser.value("vr-show-devices") == "on") {
637 vrShowDevices = true;
638 } else if (parser.value("vr-show-devices") == "off") {
639 vrShowDevices = false;
640 } else {
641 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Invalid argument for option %1").arg("--vr-show-devices")));
642 return 1;
643 }
644 }
645 }
646
647 // Initialize the command interpreter (but don't start it yet)
648 CommandInterpreter cmdInterpreter;
649 if (parser.isSet("read-commands") && !cmdInterpreter.init(parser.value("read-commands"))) {
650 return 1;
651 }
652
653 // Determine VR or GUI mode
654 bool vrMainProcess = parser.isSet("vr");
655 bool vrMode = (vrMainProcess || vrChildProcess);
656 bool guiMode = !vrMode;
657
658 // Set the OpenGL context parameters
659 QSurfaceFormat format;
660 format.setRedBufferSize(10);
661 format.setGreenBufferSize(10);
662 format.setBlueBufferSize(10);
663 format.setAlphaBufferSize(0);
664 format.setStencilBufferSize(0);
665 bool wantOpenGLES = parser.isSet("opengles");
666 #if defined Q_OS_LINUX
667 # if defined Q_PROCESSOR_ARM
668 // Use OpenGL ES by default on Linux/ARM, for Raspberry Pi 5 which has crappy desktop GL.
669 // But don't do this on other systems. For example, MacOS on ARM needs desktop GL.
670 wantOpenGLES = true;
671 # endif
672 #endif
673 if (wantOpenGLES)
674 format.setRenderableType(QSurfaceFormat::OpenGLES);
675 initializeOpenGLType(format);
676 if (OpenGLType != OpenGL_Type_WebGL) {
677 if (OpenGLType == OpenGL_Type_OpenGLES) {
678 format.setVersion(3, 1);
679 } else {
680 format.setProfile(QSurfaceFormat::CoreProfile);
681 format.setVersion(3, 3);
682 }
683 if (guiMode && !parser.isSet("disable-stereo")) {
684 // Try to get quad-buffered stereo; it's ok if this fails
685 format.setOption(QSurfaceFormat::StereoBuffers);
686 }
687 QSurfaceFormat::setDefaultFormat(format);
688 }
689
690 // Initialize Bino (in VR mode: only from the main process!)
691 Bino bino(screenType, screen, parser.isSet("swap-eyes"));
692 if (guiMode || !vrChildProcess) {
693 bino.initializeOutput(audioOutputDeviceIndex >= 0
694 ? audioOutputDevices[audioOutputDeviceIndex]
695 : QMediaDevices::defaultAudioOutput());
696 if (parser.isSet("capture")) {
697 if (screenInputDeviceIndex < 0 && windowInputDeviceIndex < 0) {
698 bino.startCaptureModeCamera(audioInputDeviceIndex >= -1,
699 audioInputDeviceIndex >= 0
700 ? audioInputDevices[audioInputDeviceIndex]
701 : QMediaDevices::defaultAudioInput(),
702 videoInputDeviceIndex >= 0
703 ? videoInputDevices[videoInputDeviceIndex]
704 : QMediaDevices::defaultVideoInput(),
705 inputMode);
706 } else if (screenInputDeviceIndex >= 0) {
707 bino.startCaptureModeScreen(audioInputDeviceIndex >= -1,
708 audioInputDeviceIndex >= 0
709 ? audioInputDevices[audioInputDeviceIndex]
710 : QMediaDevices::defaultAudioInput(),
711 screenInputDevices[screenInputDeviceIndex],
712 inputMode);
713 } else {
714 bino.startCaptureModeWindow(audioInputDeviceIndex >= -1,
715 audioInputDeviceIndex >= 0
716 ? audioInputDevices[audioInputDeviceIndex]
717 : QMediaDevices::defaultAudioInput(),
718 windowInputDevices[windowInputDeviceIndex],
719 inputMode);
720 }
721 } else {
722 bino.startPlaylistMode();
723 }
724 }
725
726 // Start VR or GUI mode
727 if (vrMode) {
728 #ifdef WITH_QVR
729 BinoQVRApp qvrapp(vrShowDevices);
730 if (!manager.init(&qvrapp)) {
731 LOG_FATAL("%s", qPrintable(QCommandLineParser::tr("Cannot initialize QVR manager")));
732 return 1;
733 }
734 playlist.start();
735 cmdInterpreter.start();
736 return app.exec();
737 #else
738 return 1;
739 #endif
740 } else {
741 // Restore GUI settings unless they were overwritten on the command line
742 if (!parser.isSet("output")) {
743 bool ok = false;
744 OutputMode om = outputModeFromString(qPrintable(settings.value("OutputMode").toString()), &ok);
745 if (ok)
746 outputMode = om;
747 }
748 // Start the GUI
749 Gui gui(outputMode, surroundVerticalFOV, parser.isSet("fullscreen"));
750 gui.show();
751 // wait for several seconds to process all events before starting
752 // the playlist, because otherwise playing might be finished before
753 // the first frame rendering, e.g. if you just want to "play" an image
754 QGuiApplication::processEvents(QEventLoop::AllEvents, 3000);
755 playlist.start();
756 cmdInterpreter.start();
757 return app.exec();
758 }
759 }