diff --git a/src/main.cpp b/src/main.cpp index 59efed3..9a46f6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,13 +5,13 @@ #include #include #include "backend.h" +#include "mediautils.h" #include "theme.h" #include "translator.h" #include "thumbprovider.h" int main(int argc,char *argv[]) { - // Custom timestamped frame/buffer inputs require Qt's FFmpeg backend. - qputenv("QT_MEDIA_BACKEND","ffmpeg"); + media::useFfmpeg(); QGuiApplication app(argc,argv); app.setApplicationName("monologue"); app.setApplicationVersion("0.3.1"); diff --git a/src/mediautils.cpp b/src/mediautils.cpp index f575ac5..a8c11c1 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -72,6 +72,14 @@ QString media::chooseSource(const QStringList &connected, const QString &preferr if (connected.isEmpty() || connected.contains(remembered)) return remembered; return connected.contains(preferred) ? preferred : connected.first(); } +void media::useFfmpeg() { + qputenv("QT_MEDIA_BACKEND", "ffmpeg"); + // Loading the backend tries every hardware API before returning, about two + // seconds before the window can show, Vulkan alone most of one. Recording + // only needs the APIs behind NVENC (CUDA) and Intel and AMD's encoders (VA-API). + for (const char *variable : {"QT_FFMPEG_ENCODING_HW_DEVICE_TYPES", "QT_FFMPEG_DECODING_HW_DEVICE_TYPES"}) + if (!qEnvironmentVariableIsSet(variable)) qputenv(variable, "cuda,vaapi"); +} double media::targetFps(double minimum, double maximum) { return std::clamp(30.0, minimum, maximum); } int media::bestFormat(const QList &formats) { int best = -1; diff --git a/src/mediautils.h b/src/mediautils.h index be12399..e305ad9 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -18,6 +18,8 @@ QVideoFrameFormat::ColorSpace colorSpace(int v4l2Colorspace, int v4l2Encoding); QVideoFrameFormat::ColorSpace cameraColorSpace(const QByteArray &device); // Shares the source's image planes and capture times under a new format; later metadata changes stay independent. QVideoFrame withFormat(const QVideoFrame &source, const QVideoFrameFormat &format); +// Chooses Qt's FFmpeg backend, which takes custom timestamped frames, before anything loads it. +void useFfmpeg(); double targetFps(double minimum, double maximum); // The source to use: the remembered one while it's connected, else the default, else the first connected. QString chooseSource(const QStringList &connected, const QString &preferred, const QString &remembered); diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index b9ffcd9..9814139 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -564,7 +564,7 @@ private slots: } }; int main(int argc,char**argv) { - qputenv("QT_MEDIA_BACKEND","ffmpeg"); + media::useFfmpeg(); // Offscreen however it's started, so a test never opens a window on the desktop. qputenv("QT_QPA_PLATFORM","offscreen"); // The desktop's platform theme plugin has no place in a test run. diff --git a/tests/camera_check.cpp b/tests/camera_check.cpp index 994f949..fc88cc6 100644 --- a/tests/camera_check.cpp +++ b/tests/camera_check.cpp @@ -6,8 +6,9 @@ #include #include #include "backend.h" +#include "mediautils.h" int main(int argc,char**argv) { - qputenv("QT_MEDIA_BACKEND","ffmpeg"); + media::useFfmpeg(); QGuiApplication app(argc,argv); app.setOrganizationName("omacom"); app.setApplicationName("monologue-camera-check"); Backend backend; diff --git a/tests/ui_tests.cpp b/tests/ui_tests.cpp index f458005..1293ec6 100644 --- a/tests/ui_tests.cpp +++ b/tests/ui_tests.cpp @@ -10,6 +10,7 @@ #include #include #include "theme.h" +#include "mediautils.h" class UiTests : public QObject { Q_OBJECT @@ -302,7 +303,7 @@ QtObject { } }; int main(int argc,char**argv) { - qputenv("QT_MEDIA_BACKEND","ffmpeg"); + media::useFfmpeg(); // Offscreen however it's started, so a test never opens a window on the desktop. qputenv("QT_QPA_PLATFORM","offscreen"); // Rendered the same with or without a GPU, and without the desktop's platform theme plugin.