8#include <QtCore/QByteArrayList>
9#include <QtCore/QCoreApplication>
10#include <QtCore/QList>
11#include <QtCore/QPointer>
12#include <QtCore/QStringList>
13#include <QtCore/QVarLengthArray>
14#include <QtMultimedia/QVideoSink>
31#ifdef QGC_GST_STATIC_BUILD
34extern void gst_init_static_plugins(
void);
44void _registerPlugins()
48 static std::once_flag s_pluginsRegistered;
49 std::call_once(s_pluginsRegistered, [] {
50#ifdef QGC_GST_STATIC_BUILD
53 gst_init_static_plugins();
55 GST_PLUGIN_STATIC_REGISTER(qgc);
63 const char* factories;
64 const char* hint =
nullptr;
69constexpr PluginFeature kPluginFeatures[] = {
72 {
"QGC video sink",
true,
"qgcqvideosink qgcvideosinkbin",
73 "the qgc plugin registered without exposing its elements; link-time symbol stripping (iOS LTO / Android R8) "
74 "likely removed the GST_ELEMENT_REGISTER side effect: add gstqgc.cc to a -force_load / keep rule"},
75 {
"Video display",
true,
"decodebin3 tee queue valve capsfilter videoconvertscale|videoconvert"},
76 {
"RTP/UDP video",
true,
"udpsrc rtpjitterbuffer parsebin rtph264depay h264parse rtph265depay h265parse"},
77 {
"RTSP video",
false,
"rtspsrc"},
78 {
"MPEG-TS video",
false,
"tsdemux"},
79 {
"TCP video",
false,
"tcpclientsrc"},
80 {
"MKV recording",
false,
"matroskamux"},
81 {
"MP4/MOV recording",
false,
"mp4mux qtmux"},
84bool _verifyPluginFeatures()
86 GstRegistry* registry = gst_registry_get();
87 if (GStreamerLog().isDebugEnabled()) {
88 qCDebug(GStreamerLog) <<
"Installed GStreamer plugins:";
90 qCDebug(GStreamerLog) <<
" " << gst_plugin_get_name(plugin) << gst_plugin_get_version(plugin);
95 if (missing.isEmpty()) {
99 bool displayAvailable =
true;
101 const QString message =
102 QStringLiteral(
"%1 unavailable, GStreamer elements not found: %2%3")
103 .arg(feature.feature, feature.factories.join(QStringLiteral(
", ")),
104 feature.hint.isEmpty() ? QString() : QStringLiteral(
" (%1)").arg(feature.hint));
105 if (feature.required) {
106 qCCritical(GStreamerLog).noquote() << message;
107 displayAvailable =
false;
109 qCWarning(GStreamerLog).noquote() << message;
116 if (!GST_OBJECT_FLAG_IS_SET(p, GST_PLUGIN_FLAG_BLACKLISTED)) {
119 const gchar* filename = gst_plugin_get_filename(p);
120 qCWarning(GStreamerLog) <<
"Blacklisted plugin:" << gst_plugin_get_name(p)
121 <<
"file:" << (filename ? filename :
"(null)");
124 if (!displayAvailable) {
129 return displayAvailable;
136 QList<MissingPluginFeature> missing;
137 for (
const PluginFeature& feature : kPluginFeatures) {
139 for (
const QByteArray& entry : QByteArray(feature.factories).split(
' ')) {
140 const QList<QByteArray> alternatives = entry.split(
'|');
141 if (std::ranges::none_of(alternatives,
142 [&hasFactory](
const QByteArray& name) {
return hasFactory(name.constData()); })) {
143 absent.append(QString::fromLatin1(entry));
146 if (!absent.isEmpty()) {
147 missing.append({.feature = QString::fromLatin1(feature.name),
149 .required = feature.required,
150 .hint = QString::fromLatin1(feature.hint)});
161 qCCritical(GStreamerLog) <<
"Invalid GStreamer environment configuration:" << env.
error;
167 QByteArrayList argStorage;
168 argStorage.reserve(args.size());
169 for (
const QString& arg : args) {
170 argStorage.append(arg.toUtf8());
173 QVarLengthArray<char*, 16> argv;
174 for (QByteArray& arg : argStorage) {
175 argv.append(arg.data());
178 int argc = argv.size();
179 char** argvPtr = argv.data();
180 GError* error =
nullptr;
182 if (!gst_init_check(&argc, &argvPtr, &error)) {
183 qCCritical(GStreamerLog) <<
"Failed to initialize GStreamer:" << (error ? error->message :
"unknown error");
184 g_clear_error(&error);
192void _deinitGStreamer()
202 if (!gst_is_initialized()) {
203 qCCritical(GStreamerLog) <<
"completeInit called but gst_init() has not been called";
209 guint major, minor, micro, nano;
210 gst_version(&major, &minor, µ, &nano);
211 qCDebug(GStreamerLog) <<
"GStreamer initialized:" << major <<
"." << minor <<
"." << micro;
214 if (std::make_pair(major, minor) < std::make_pair(guint{GST_VERSION_MAJOR}, guint{GST_VERSION_MINOR})) {
215 qCWarning(GStreamerLog) <<
"GStreamer runtime" << major <<
"." << minor <<
"." << micro <<
"is older than the"
216 << GST_VERSION_MAJOR <<
"." << GST_VERSION_MINOR <<
"QGC was built against";
221 if (!_verifyPluginFeatures()) {
247 if (qEnvironmentVariableIntValue(
"QGC_GST_DEINIT") > 0) {
248 qAddPostRoutine(_deinitGStreamer);
253 return completeInit(
static_cast<int>(gst_debug_get_default_threshold()));
259 [[maybe_unused]]
bool gpuZeroCopy)
261 const gboolean disablePar = disablePixelAspectRatio ? TRUE : FALSE;
262 const char*
const conversion = conversionElement.isEmpty() ? nullptr : conversionElement.constData();
267 qCCritical(GStreamerLog) <<
"qgcvideosinkbin factory not found";
271#if defined(QGC_HAS_ANY_GPU_PATH)
273 gst_element_factory_create_full(factory.get(),
"gpu-zerocopy", gpuZeroCopy ? TRUE : FALSE,
"conversion-element",
274 conversion,
"disable-par", disablePar, NULL);
276 GstElement* videoSinkBin = gst_element_factory_create_full(factory.get(),
"conversion-element", conversion,
277 "disable-par", disablePar, NULL);
280 qCCritical(GStreamerLog) <<
"qgcvideosinkbin element creation failed";
284 return GST_ELEMENT(gst_object_ref_sink(videoSinkBin));
289 if (!sinkBin || !videoSink || !controllerParent) {
291 qCWarning(GStreamerLog) <<
"null sinkBin, videoSink, or controllerParent";
303 qCCritical(GStreamerLog) <<
"bin has no qgcqvideosink child";
306 GstElement*
const elementRaw = GST_ELEMENT(element.get());
312 controller->setVideoSink(QPointer<QVideoSink>(videoSink));
322 if (QCoreApplication*
const app = QCoreApplication::instance()) {
323 (void) QMetaObject::invokeMethod(
329 qCInfo(GStreamerLog) <<
"Resolved RHI backend:" <<
GstQgc::toString(backend)
G_BEGIN_DECLS GST_PLUGIN_STATIC_DECLARE(qgc)
struct _GstElement GstElement
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void reapply()
apply() for the option applied last, when the platform's RankTraits changed since: Qt's device became...
static DecoderRankApplier & instance()
GstElement * gst_qgc_video_sink_bin_get_qvideosink(GstQgcVideoSinkBin *self)
Returns the internal qgcqvideosink element, transfer-full (caller unrefs); NULL if not yet constructe...
GstElement * createVideoSink(const QByteArray &conversionElement, bool disablePixelAspectRatio, bool gpuZeroCopy)
std::unique_ptr< GstObject, GstObjectDeleter > GstObjectPtr
void onMainWindowReady(QQuickWindow *window)
void qtGstLog(GstDebugCategory *category, GstDebugLevel level, const gchar *file, const gchar *function, gint line, GObject *object, GstDebugMessage *message, gpointer data)
std::unique_ptr< GstElementFactory, GstObjectDeleter > GstFactoryPtr
void forEachPlugin(GstRegistry *registry, const std::function< void(GstPlugin *)> &visitor)
GstFactoryPtr adoptFactory(GstElementFactory *factory) noexcept
void resetExternalPluginLoaderFailed()
void serializeUdpSrcUnlock()
void redirectGLibLogging()
void configureDebugLogging(std::optional< int > level)
QList< MissingPluginFeature > missingPluginFeatures(const std::function< bool(const char *factoryName)> &hasFactory)
QGCQVideoSinkController * setupQVideoSinkElement(GstElement *sinkBin, QVideoSink *videoSink, QObject *controllerParent)
bool hasElementFactory(const char *name)
bool completeInit(std::optional< int > debugLevel)
debugLevel: the debug threshold to start with (none when unset), unless GST_DEBUG is set.
bool initialize(const VideoBackend::InitParams ¶ms)
RenderTarget currentRenderTarget()
const char * toString(RenderBackend backend) noexcept
RenderBackend
Graphics API Qt Quick renders the video with, which decides the GPU memory qgcqvideosink can import.
const char * zeroCopyPathName(RenderBackend backend) noexcept
void setGraphicsDevicePublishedHandler(void(*handler)()) noexcept
void connectMainWindow(QQuickWindow *window)
A QGC video feature whose GStreamer elements are not all registered.
std::optional< int > debugLevel
The persisted debug threshold to start with (persistedDebugLevel()).
QStringList arguments
QCoreApplication::arguments(), snapshotted on the GUI thread.
EnvPrepResult environment
What prepareEnvironment() did.