From 74b7738a9d2d0570ac422a72732755ef3f76e90e Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 14 Apr 2021 00:16:07 +0200 Subject: [PATCH] Create GLib main event loop on non-glib systems --- src/engine/gststartup.cpp | 31 ++++++++++++++++++++++++++++++- src/engine/gststartup.h | 6 ++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/engine/gststartup.cpp b/src/engine/gststartup.cpp index e52e640b5..1cfd5e2e5 100644 --- a/src/engine/gststartup.cpp +++ b/src/engine/gststartup.cpp @@ -19,10 +19,15 @@ #include "config.h" +#include +#include + #include #include +#include #include +#include #include #include #include @@ -30,6 +35,7 @@ #include #include #include +#include #include "core/logging.h" #include "core/utilities.h" @@ -40,15 +46,38 @@ #include "gststartup.h" +GThread *GstStartup::kGThread = nullptr; + +gpointer GstStartup::GLibMainLoopThreadFunc(gpointer) { + + qLog(Info) << "Creating GLib main event loop."; + + GMainLoop *gloop = g_main_loop_new(nullptr, false); + g_main_loop_run(gloop); + g_main_loop_unref(gloop); + + return nullptr; + +} + GstStartup::GstStartup(QObject *parent) : QObject(parent) { + #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) initializing_ = QtConcurrent::run(&GstStartup::InitializeGStreamer, this); #else initializing_ = QtConcurrent::run(this, &GstStartup::InitializeGStreamer); #endif + + const QMetaObject *mo = QAbstractEventDispatcher::instance(qApp->thread())->metaObject(); + if (mo && strcmp(mo->className(), "QEventDispatcherGlib") != 0 && strcmp(mo->superClass()->className(), "QEventDispatcherGlib") != 0) { + kGThread = g_thread_new(nullptr, GstStartup::GLibMainLoopThreadFunc, nullptr); + } + } -GstStartup::~GstStartup() {} +GstStartup::~GstStartup() { + if (kGThread) g_thread_unref(kGThread); +} void GstStartup::InitializeGStreamer() { diff --git a/src/engine/gststartup.h b/src/engine/gststartup.h index 7875b3a58..dff5e2899 100644 --- a/src/engine/gststartup.h +++ b/src/engine/gststartup.h @@ -22,8 +22,10 @@ #include "config.h" +#include #include +#include #include #include @@ -37,8 +39,12 @@ class GstStartup : public QObject { void EnsureInitialized() { initializing_.waitForFinished(); } private: + static GThread *kGThread; + static gpointer GLibMainLoopThreadFunc(gpointer); + void InitializeGStreamer(); void SetEnvironment(); + QFuture initializing_; };