42using namespace std::chrono_literals;
44constexpr std::chrono::milliseconds kBudget = 15s;
45constexpr std::chrono::milliseconds kPipelineTimeout = 4s;
48constexpr std::chrono::milliseconds kScannerStartTimeout = 5s;
50constexpr const char* kProxyLookupUri =
"rtsp://127.0.0.1";
52#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
54constexpr bool kStaticRuntime =
true;
56constexpr bool kStaticRuntime =
false;
60constexpr Qt::CaseSensitivity kPathCase = Qt::CaseInsensitive;
62constexpr Qt::CaseSensitivity kPathCase = Qt::CaseSensitive;
65QString normalizedPath(
const QString& path)
67 return QDir::cleanPath(QDir::fromNativeSeparators(path));
70QString pathKey(
const QString& path)
72 const QString normalized = normalizedPath(path);
73 return (kPathCase == Qt::CaseInsensitive) ? normalized.toCaseFolded() : normalized;
77bool isPluginFile(
const QString& fileName)
80 return fileName.endsWith(QLatin1String(
".dll"), Qt::CaseInsensitive);
81#elif defined(Q_OS_MACOS)
82 return fileName.endsWith(QLatin1String(
".dylib")) || fileName.endsWith(QLatin1String(
".so"));
84 return fileName.endsWith(QLatin1String(
".so"));
88QStringList pluginFilesIn(
const QStringList& dirs)
91 for (
const QString& dir : dirs) {
92 QDirIterator it(dir, QDir::Files, QDirIterator::Subdirectories);
93 while (it.hasNext()) {
94 const QString file = it.next();
95 if (isPluginFile(file)) {
103bool hasGioModule(
const QString& dir,
const QString& module)
105 return !dir.isEmpty() && !QDir(dir).entryList({QStringLiteral(
"*%1.*").arg(module)}, QDir::Files).isEmpty();
115void onProxyLookedUp(GObject* resolver, GAsyncResult* result, gpointer data)
117 auto*
const lookup =
static_cast<ProxyLookup*
>(data);
118 GError* error =
nullptr;
119 gchar**
const proxies = g_proxy_resolver_lookup_finish(G_PROXY_RESOLVER(resolver), result, &error);
120 if (proxies && proxies[0]) {
121 lookup->proxy = QString::fromUtf8(proxies[0]);
123 lookup->failure = error ? QString::fromUtf8(error->message) : QStringLiteral(
"no proxy returned");
126 g_clear_error(&error);
130gboolean markTimedOut(gpointer flag)
132 *
static_cast<bool*
>(flag) =
true;
133 return G_SOURCE_REMOVE;
137std::pair<QString, QString> lookUpProxy(GProxyResolver* resolver,
const char* uri, std::chrono::milliseconds timeout)
140 GMainContext*
const context = g_main_context_new();
141 g_main_context_push_thread_default(context);
143 bool timedOut =
false;
144 GSource*
const timer = g_timeout_source_new(
static_cast<guint
>(std::max(timeout, 0ms).count()));
145 g_source_set_callback(timer, markTimedOut, &timedOut,
nullptr);
146 (void) g_source_attach(timer, context);
148 auto lookup = std::make_unique<ProxyLookup>();
149 g_proxy_resolver_lookup_async(resolver, uri,
nullptr, onProxyLookedUp, lookup.get());
150 while (!lookup->done && !timedOut) {
151 (void) g_main_context_iteration(context, TRUE);
154 g_source_destroy(timer);
155 g_source_unref(timer);
156 g_main_context_pop_thread_default(context);
157 g_main_context_unref(context);
161 (void) lookup.release();
162 return {QStringLiteral(
"no answer within %1 ms").arg(timeout.count()), QString()};
164 return {lookup->failure, lookup->proxy};
177 std::chrono::milliseconds _timeout()
const;
178 void _fail(
const QString& message);
179 QByteArray _readSample(
const Sample& sample);
180 void _checkBundledPlugins();
181 void _checkPluginScanner();
182 void _checkFeatures();
185 void _checkDecoding(
const Sample& sample,
const QByteArray& data);
186 void _checkRecordings(
const QByteArray& h264);
189 const QDeadlineTimer _budget{kBudget};
196 QElapsedTimer elapsed;
199 const bool bundled = !_runtime.
pluginDirs.isEmpty();
201 _checkBundledPlugins();
202 _checkPluginScanner();
205 if (bundled || kStaticRuntime) {
209 if (hasGioModule(_runtime.gioModuleDir, QStringLiteral(
"giolibproxy"))) {
214 if (!h264.isEmpty()) {
216 _checkRecordings(h264);
221 if (!h265.isEmpty()) {
225 _notes.append(QStringLiteral(
"no H.265 decoder to test"));
228 if (_failures == 0) {
229 qCInfo(GStreamerSelfTestLog).noquote() << QStringLiteral(
"GStreamer self-test passed in %1 ms: %2")
230 .arg(elapsed.elapsed())
231 .arg(_notes.join(QStringLiteral(
", ")));
234 qCCritical(GStreamerSelfTestLog).noquote()
235 << QStringLiteral(
"GStreamer self-test failed %1 check(s) in %2 ms").arg(_failures).arg(elapsed.elapsed());
239std::chrono::milliseconds Checker::_timeout()
const
241 return std::min(kPipelineTimeout,
242 std::chrono::duration_cast<std::chrono::milliseconds>(_budget.remainingTimeAsDuration()));
245void Checker::_fail(
const QString& message)
247 qCCritical(GStreamerSelfTestLog).noquote() << message;
251QByteArray Checker::_readSample(
const Sample& sample)
253 QFile file(QString::fromLatin1(sample.
resource));
254 if (!file.open(QIODevice::ReadOnly) || (file.size() == 0)) {
255 _fail(QStringLiteral(
"The %1 sample %2 is missing from the app's resources")
256 .arg(QLatin1String(sample.
codec), QLatin1String(sample.
resource)));
259 return file.readAll();
262void Checker::_checkBundledPlugins()
264 QList<RegisteredPlugin> registered;
265 forEachPlugin(gst_registry_get(), [®istered](GstPlugin* plugin) {
266 const gchar*
const filename = gst_plugin_get_filename(plugin);
267 registered.append({.name = QString::fromUtf8(gst_plugin_get_name(plugin)),
268 .filename = filename ? QString::fromUtf8(filename) : QString(),
269 .blacklisted = GST_OBJECT_FLAG_IS_SET(plugin, GST_PLUGIN_FLAG_BLACKLISTED) != 0});
272 const QStringList files = pluginFilesIn(_runtime.pluginDirs);
274 for (
const QString& issue : issues) {
277 if (!issues.isEmpty()) {
280 _notes.append(QStringLiteral(
"%1 bundled plugins").arg(files.size()));
283void Checker::_checkPluginScanner()
285 const QString scanner = qEnvironmentVariable(
"GST_PLUGIN_SCANNER_1_0");
290 if (failure.isEmpty()) {
291 failure = scanner.isEmpty() ? QStringLiteral(
"is not in the bundle") : pluginScannerStartFailure(scanner);
294 if (failure.isEmpty()) {
297 _fail(QStringLiteral(
"GStreamer's plugin scanner %1 %2, so GStreamer loads the plugins into QGC's process: check "
298 "that the bundled gst-plugin-scanner is executable and signed and finds the GStreamer "
300 .arg(scanner.isEmpty() ? QStringLiteral(
"(GST_PLUGIN_SCANNER unset)") : scanner, failure));
303void Checker::_checkFeatures()
310 _fail(QStringLiteral(
"%1 unavailable: GStreamer elements %2 not found; %3")
311 .arg(feature.feature, feature.factories.join(QStringLiteral(
", ")),
312 feature.hint.isEmpty() ? QStringLiteral(
"their plugin is missing or failed to load")
317void Checker::_checkTls()
319 GTlsBackend*
const backend = g_tls_backend_get_default();
320 if (!backend || !g_tls_backend_supports_tls(backend)) {
321 const QString where =
322 _runtime.gioModuleDir.isEmpty() ? QString() : QStringLiteral(
" from ") + _runtime.gioModuleDir;
323 _fail(QStringLiteral(
"GIO has no TLS support: no TLS GIO module (gioopenssl, giognutls) loaded%1, so RTSP over "
324 "TLS and HTTPS sources fail")
329 GTlsDatabase*
const database = g_tls_backend_get_default_database(backend);
331 _fail(QStringLiteral(
"GIO's TLS backend %1 has no certificate database, so no server certificate verifies")
332 .arg(QLatin1String(G_OBJECT_TYPE_NAME(backend))));
335 g_object_unref(database);
336 _notes.append(QStringLiteral(
"TLS by %1").arg(QLatin1String(G_OBJECT_TYPE_NAME(backend))));
339void Checker::_checkProxy()
342 (void) g_proxy_resolver_get_default();
343 GIOExtensionPoint*
const point = g_io_extension_point_lookup(G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
344 GIOExtension*
const extension = point ? g_io_extension_point_get_extension_by_name(point,
"libproxy") :
nullptr;
347 QStringLiteral(
"The libproxy GIO module in %1 did not load: libproxy or a library it links is missing, so "
348 "proxy settings are ignored")
349 .arg(_runtime.gioModuleDir));
353 auto*
const resolver =
static_cast<GProxyResolver*
>(g_object_new(g_io_extension_get_type(extension),
nullptr));
354 const auto unref = qScopeGuard([resolver] { g_object_unref(resolver); });
355 if (!g_proxy_resolver_is_supported(resolver)) {
356 _fail(QStringLiteral(
"The libproxy GIO module in %1 loaded, but libproxy failed to start")
357 .arg(_runtime.gioModuleDir));
361 const auto [failure, proxy] = lookUpProxy(resolver, kProxyLookupUri, std::min(_timeout(), 3000ms));
362 if (!failure.isEmpty()) {
363 _fail(QStringLiteral(
"libproxy failed to look up the proxy for %1: %2; its backend (pxbackend), which it loads "
364 "by name, may be missing from the bundle")
365 .arg(QLatin1String(kProxyLookupUri), failure));
368 _notes.append(QStringLiteral(
"libproxy: %1").arg(proxy));
371void Checker::_checkDecoding(
const Sample& sample,
const QByteArray& data)
379 if (software.
failure.isEmpty()) {
380 qCWarning(GStreamerSelfTestLog).noquote()
381 << QStringLiteral(
"%1 hardware decoding with %2 failed (%3); software decoding with %4 works")
383 software.
decoders.join(QLatin1Char(
'+')));
386 result.
failure += QStringLiteral(
"; with software decoders forced: %1").arg(software.
failure);
391 if (result.
failure.isEmpty()) {
393 const QString decoder = result.
decoders.isEmpty() ? QStringLiteral(
"?") : result.
decoders.takeLast();
394 QString note = QStringLiteral(
"%1 decoded by %2").arg(QLatin1String(sample.
codec), decoder);
396 note += QStringLiteral(
" (%1 failed to start)").arg(result.
decoders.join(QLatin1Char(
'+')));
401 _fail(QStringLiteral(
"%1 decoding failed with decoders [%2]: %3")
405void Checker::_checkRecordings(
const QByteArray& h264)
407 const QTemporaryDir dir;
408 if (!dir.isValid()) {
409 _fail(QStringLiteral(
"Cannot create a directory to record into: %1").arg(dir.errorString()));
413 QStringList recorded;
415 const QString file = dir.filePath(QStringLiteral(
"selftest.%1").arg(QLatin1String(format.extension)));
416 QString failure =
recordSample(file, format.format, h264, _timeout());
417 if (failure.isEmpty() && (QFileInfo(file).size() <= 0)) {
418 failure = QStringLiteral(
"the file is empty");
420 if (failure.isEmpty()) {
421 failure =
readBack(file, format.demuxer, _timeout());
423 if (failure.isEmpty()) {
424 recorded.append(QLatin1String(format.name));
426 _fail(QStringLiteral(
"%1 recording to %2 failed: %3").arg(QLatin1String(format.name), file, failure));
429 if (!recorded.isEmpty()) {
430 _notes.append(QStringLiteral(
"recorded %1").arg(recorded.join(QLatin1Char(
'/'))));
437 const QStringList& pluginFiles)
440 for (
const QString& dir : pluginDirs) {
441 dirs.append(normalizedPath(dir) + QLatin1Char(
'/'));
443 const auto inBundle = [&dirs](
const QString& file) {
444 return std::ranges::any_of(dirs, [&file](
const QString& dir) {
return file.startsWith(dir, kPathCase); });
447 QSet<QString> shipped;
448 for (
const QString& file : pluginFiles) {
449 shipped.insert(pathKey(file));
453 QSet<QString> registeredFiles;
455 if (plugin.filename.isEmpty()) {
458 const QString file = normalizedPath(plugin.filename);
459 registeredFiles.insert(pathKey(file));
460 if (!inBundle(file)) {
461 issues.append(QStringLiteral(
"Plugin %1 was loaded from %2, outside the bundled plugin directory %3: a "
462 "GStreamer installed outside the app leaks into it")
463 .arg(plugin.name, file, pluginDirs.join(QStringLiteral(
", "))));
464 }
else if (!shipped.contains(pathKey(file))) {
465 issues.append(QStringLiteral(
"Plugin %1 is registered from %2, which the bundle does not ship: the plugin "
467 .arg(plugin.name, file));
468 }
else if (plugin.blacklisted) {
470 QStringLiteral(
"Plugin %1 (%2) is blacklisted: it failed to load, typically because a library "
471 "it links is missing from the bundle")
472 .arg(plugin.name, file));
476 for (
const QString& file : pluginFiles) {
477 if (!registeredFiles.contains(pathKey(file))) {
478 issues.append(QStringLiteral(
"Plugin file %1 is not in GStreamer's registry: GStreamer failed to load it")
479 .arg(normalizedPath(file)));
486QString pluginScannerStartFailure(
const QString& scanner)
488 const int timeoutMs =
static_cast<int>(kScannerStartTimeout.count());
490 process.setProgram(scanner);
492 if (!process.waitForStarted(timeoutMs)) {
493 return QStringLiteral(
"could not be started (%1)").arg(process.errorString());
495 if (!process.waitForFinished(timeoutMs)) {
497 (void) process.waitForFinished(timeoutMs);
498 return QStringLiteral(
"did not exit within %1 ms").arg(timeoutMs);
500 if ((process.exitStatus() != QProcess::NormalExit) || (process.exitCode() != 1)) {
501 return QStringLiteral(
"exited with code 0x%1 where a working scanner exits with 1")
502 .arg(
static_cast<quint32
>(process.exitCode()), 0, 16);
510 return Checker(runtime).run();