From c69f0f95a9d847bafb36a94ca289e763a46bd544 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Thu, 9 Nov 2023 23:55:42 +0100
Subject: [PATCH] Fix Nheko freezing after stopping a call on Wayland

---
 CMakeLists.txt             |  2 +-
 src/voip/WebRTCSession.cpp | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ec601e6b..af983590c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -606,7 +606,7 @@ endif()
 
 if(VOIP)
     include(FindPkgConfig)
-    pkg_check_modules(GSTREAMER REQUIRED IMPORTED_TARGET gstreamer-sdp-1.0>=1.18 gstreamer-webrtc-1.0>=1.18)
+    pkg_check_modules(GSTREAMER REQUIRED IMPORTED_TARGET gstreamer-sdp-1.0>=1.18 gstreamer-webrtc-1.0>=1.18 gstreamer-gl-1.0)
 endif()
 
 if(X11 AND NOT WIN32 AND NOT APPLE AND NOT HAIKU)
diff --git a/src/voip/WebRTCSession.cpp b/src/voip/WebRTCSession.cpp
index d3e88c32e..332a27fd1 100644
--- a/src/voip/WebRTCSession.cpp
+++ b/src/voip/WebRTCSession.cpp
@@ -25,6 +25,7 @@
 #ifdef GSTREAMER_AVAILABLE
 extern "C"
 {
+#include "gst/gl/gstgldisplay.h"
 #include "gst/gst.h"
 #include "gst/sdp/sdp.h"
 
@@ -346,6 +347,21 @@ newVideoSinkChain(GstElement *pipe)
     gst_element_sync_state_with_parent(glupload);
     gst_element_sync_state_with_parent(glcolorconvert);
     gst_element_sync_state_with_parent(glsinkbin);
+
+    // to propagate context (hopefully)
+    gst_element_set_state(qmlglsink, GST_STATE_READY);
+
+    // Workaround: On wayland, when egl is used, gstreamer might terminate the display connection.
+    // Prevent that by "leaking" a reference to the display. See
+    // https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3743
+    if (QGuiApplication::platformName() == QStringLiteral("wayland")) {
+        auto context = gst_element_get_context(qmlglsink, "gst.gl.GLDisplay");
+        if (context) {
+            GstGLDisplay *display;
+            gst_context_get_gl_display(context, &display);
+        }
+    }
+
     return queue;
 }
 
-- 
GitLab