From fea4d302cddbb91e23864a4dbd786d53b72f67ea Mon Sep 17 00:00:00 2001
From: VaL Doroshchuk <valentyn.doroshchuk@qt.io>
Date: Thu, 7 Feb 2019 13:27:36 +0100
Subject: [PATCH] Gstreamer: Set surface from renderer before creating pipeline

When multiple pipelines are created, and one of these gets recreated,
it picks up the last set surface.

This is incorrect, as it needs a surface belonging to the current renderer.

This patch sets the surface before creating pipelines, retrieving it from the current renderer.

Task-number: QTBUG-73557
Change-Id: I0811f7262a0eca57e01361a55515351127520064
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
---
 src/gsttools/qgstreamerplayersession.cpp | 8 +++++++-
 src/gsttools/qgstreamervideorenderer.cpp | 1 -
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/gsttools/qgstreamerplayersession.cpp b/src/gsttools/qgstreamerplayersession.cpp
index bd402fb3c..f21c94c5c 100644
--- a/src/gsttools/qgstreamerplayersession.cpp
+++ b/src/gsttools/qgstreamerplayersession.cpp
@@ -47,7 +47,7 @@
 #include <private/gstvideoconnector_p.h>
 #endif
 #include <private/qgstutils_p.h>
-#include <private/qgstutils_p.h>
+#include <private/qvideosurfacegstsink_p.h>
 
 #include <gst/gstvalue.h>
 #include <gst/base/gstbasesrc.h>
@@ -60,6 +60,7 @@
 #include <QtCore/qdebug.h>
 #include <QtCore/qdir.h>
 #include <QtCore/qstandardpaths.h>
+#include <qvideorenderercontrol.h>
 
 //#define DEBUG_PLAYBIN
 //#define DEBUG_VO_BIN_DUMP
@@ -338,6 +339,11 @@ void QGstreamerPlayerSession::loadFromUri(const QNetworkRequest &request)
 #endif
 
     if (m_request.url().scheme() == QLatin1String("gst-pipeline")) {
+        // Set current surface to video sink before creating a pipeline.
+        auto renderer = qobject_cast<QVideoRendererControl*>(m_videoOutput);
+        if (renderer)
+            QVideoSurfaceGstSink::setSurface(renderer->surface());
+
         QString url = m_request.url().toString(QUrl::RemoveScheme);
         QString pipeline = QUrl::fromPercentEncoding(url.toLatin1().constData());
         GError *err = nullptr;
diff --git a/src/gsttools/qgstreamervideorenderer.cpp b/src/gsttools/qgstreamervideorenderer.cpp
index 25fc33cb3..c2226d658 100644
--- a/src/gsttools/qgstreamervideorenderer.cpp
+++ b/src/gsttools/qgstreamervideorenderer.cpp
@@ -113,7 +113,6 @@ void QGstreamerVideoRenderer::setSurface(QAbstractVideoSurface *surface)
         if (m_surface) {
             connect(m_surface.data(), SIGNAL(supportedFormatsChanged()),
                     this, SLOT(handleFormatChange()));
-            QVideoSurfaceGstSink::setSurface(m_surface);
         }
 
         if (wasReady != isReady())
-- 
GitLab