4#include <common/qgstreamervideosink_p.h>
5#include <common/qgstvideorenderersink_p.h>
6#include <common/qgst_debug_p.h>
7#include <common/qgstutils_p.h>
10#include <QtCore/qdebug.h>
11#include <QtCore/qloggingcategory.h>
13#if QT_CONFIG(gstreamer_gl)
14# include <QtGui/qguiapplication.h>
15# include <QtGui/qopenglcontext.h>
16# include <QtGui/qwindow.h>
17# include <QtGui/qpa/qplatformnativeinterface.h>
18# include <gst/gl/gstglconfig.h>
19# include <gst/gl/gstgldisplay.h>
21# if QT_CONFIG(gstreamer_gl_x11)
22# include <gst/gl/x11/gstgldisplay_x11.h>
24# if QT_CONFIG(gstreamer_gl_egl)
25# include <gst/gl/egl/gstgldisplay_egl.h>
27# include <EGL/eglext.h>
29# if QT_CONFIG(gstreamer_gl_wayland)
30# include <gst/gl/wayland/gstgldisplay_wayland.h>
36Q_STATIC_LOGGING_CATEGORY(qLcGstVideoSink,
"qt.multimedia.gstvideosink");
39 : QPlatformVideoSink{ parent }
72 QGstElementFactoryHandle factory;
76 QByteArray preprocessOverride = qgetenv(
"QT_GSTREAMER_OVERRIDE_VIDEO_CONVERSION_ELEMENT");
77 if (!preprocessOverride.isEmpty()) {
78 qCDebug(qLcGstVideoSink) <<
"requesting conversion element from environment:"
79 << preprocessOverride;
81 m_gstPreprocess = QGstBin::createFromPipelineDescription(preprocessOverride,
nullptr,
84 qCWarning(qLcGstVideoSink) <<
"Cannot create conversion element:" << preprocessOverride;
87 if (!m_gstPreprocess) {
92 static constexpr auto decodersToTest = {
93 "imxvideoconvert_g2d",
97 for (
const char *decoder : decodersToTest) {
98 factory = QGstElement::findFactory(decoder);
104 qCDebug(qLcGstVideoSink)
105 <<
"instantiating conversion element:"
106 << g_type_name(gst_element_factory_get_element_type(factory.get()));
108 m_gstPreprocess = QGstElement::createFromFactory(factory,
"preprocess");
112 bool disablePixelAspectRatio =
113 qEnvironmentVariableIsSet(
"QT_GSTREAMER_DISABLE_PIXEL_ASPECT_RATIO");
114 if (disablePixelAspectRatio) {
122 QGstElement::createFromFactory(
"identity",
"nullPixelAspectRatioCapsFilter");
125 QGstElement::createFromFactory(
"capsfilter",
"pixelAspectRatioCapsFilter");
127 gst_caps_new_simple(
"video/x-raw",
"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL),
130 g_object_set(m_gstCapsFilter.element(),
"caps", capsFilterCaps.caps(), NULL);
133 if (m_gstPreprocess) {
134 m_sinkBin.add(m_gstPreprocess, m_gstCapsFilter);
135 qLinkGstElements(m_gstPreprocess, m_gstCapsFilter);
136 m_sinkBin.addGhostPad(m_gstPreprocess,
"sink");
138 m_sinkBin.add(m_gstCapsFilter);
139 m_sinkBin.addGhostPad(m_gstCapsFilter,
"sink");
145 emit aboutToBeDestroyed();
152 if (!m_gstVideoSink) {
156 updateSinkElement(m_gstQtSink);
164 if (m_isActive == isActive)
166 m_isActive = isActive;
169 m_gstQtSink.setActive(isActive);
174 m_sinkIsAsync = isAsync;
179 if (rhi && rhi->backend() != QRhi::OpenGLES2)
185 updateGstContexts(m_rhi);
191 updateSinkElement(m_gstQtSink);
197 Q_ASSERT(pluggableSink);
198 m_pluggableVideoSink = pluggableSink;
199 m_pluggableVideoSink->setVideoFrame(m_currentVideoFrame);
200 m_pluggableVideoSink->setSubtitleText(m_currentSubtitleText);
201 m_pluggableVideoSink->setNativeSize(m_currentNativeSize);
202 connect(
this, &QGstreamerRelayVideoSink::videoFrameChanged,
203 m_pluggableVideoSink, &QPlatformVideoSink::setVideoFrame);
204 connect(
this, &QGstreamerRelayVideoSink::subtitleTextChanged,
205 m_pluggableVideoSink, &QPlatformVideoSink::setSubtitleText);
206 connect(
this, &QGstreamerRelayVideoSink::nativeSizeChanged,
207 m_pluggableVideoSink, &QPlatformVideoSink::setNativeSize);
210 updateGstContexts(m_pluggableVideoSink->rhi());
215 if (m_pluggableVideoSink) {
216 disconnect(
this, &QGstreamerRelayVideoSink::videoFrameChanged,
217 m_pluggableVideoSink, &QPlatformVideoSink::setVideoFrame);
218 disconnect(
this, &QGstreamerRelayVideoSink::subtitleTextChanged,
219 m_pluggableVideoSink, &QPlatformVideoSink::setSubtitleText);
220 disconnect(
this, &QGstreamerRelayVideoSink::nativeSizeChanged,
221 m_pluggableVideoSink, &QPlatformVideoSink::setNativeSize);
222 m_pluggableVideoSink =
nullptr;
228 emit videoFrameChanged(frame);
229 m_currentVideoFrame = frame;
234 emit subtitleTextChanged(subtitleText);
235 m_currentSubtitleText = subtitleText;
240 emit nativeSizeChanged(size);
241 m_currentNativeSize = size;
246 Q_ASSERT(!m_gstQtSink);
248 m_gstQtSink = QGstVideoRendererSink::createSink(
this);
250 m_gstQtSink.set(
"async",
false);
251 m_gstQtSink.setActive(m_isActive);
256 if (newSink == m_gstVideoSink)
259 m_gstCapsFilter.src().modifyPipelineInIdleProbe([&] {
261 m_sinkBin.stopAndRemoveElements(m_gstVideoSink);
263 m_gstVideoSink = std::move(newSink);
264 m_sinkBin.add(m_gstVideoSink);
265 qLinkGstElements(m_gstCapsFilter, m_gstVideoSink);
266 GstEvent *event = gst_event_new_reconfigure();
267 gst_element_send_event(m_gstVideoSink.element(), event);
268 m_gstVideoSink.syncStateWithParent();
271 m_sinkBin.dumpPipelineGraph(
"updateSinkElement");
276 m_gstGlDisplayContext.reset();
277 m_gstGlLocalContext.reset();
278 m_eglDisplay =
nullptr;
279 m_eglImageTargetTexture2D =
nullptr;
284 using namespace Qt::Literals;
288#if QT_CONFIG(gstreamer_gl)
289 if (!rhi || rhi->backend() != QRhi::OpenGLES2)
292 auto *nativeHandles =
static_cast<
const QRhiGles2NativeHandles *>(rhi->nativeHandles());
293 auto glContext = nativeHandles->context;
296 const QString platform = QGuiApplication::platformName();
297 QPlatformNativeInterface *pni = QGuiApplication::platformNativeInterface();
298 m_eglDisplay = pni->nativeResourceForIntegration(
"egldisplay"_ba);
301 QGstGLDisplayHandle gstGlDisplay;
303 QByteArray contextName =
"eglcontext"_ba;
304 GstGLPlatform glPlatform = GST_GL_PLATFORM_EGL;
307# if QT_CONFIG(gstreamer_gl_egl)
309 GST_GL_DISPLAY_CAST(gst_gl_display_egl_new_with_egl_display(m_eglDisplay)),
310 QGstGLDisplayHandle::HasRef);
311 m_eglImageTargetTexture2D = eglGetProcAddress(
"glEGLImageTargetTexture2DOES");
314 auto display = pni->nativeResourceForIntegration(
"display"_ba);
317# if QT_CONFIG(gstreamer_gl_x11)
318 if (platform == QLatin1String(
"xcb")) {
319 contextName =
"glxcontext"_ba;
320 glPlatform = GST_GL_PLATFORM_GLX;
322 gstGlDisplay.reset(GST_GL_DISPLAY_CAST(gst_gl_display_x11_new_with_display(
323 reinterpret_cast<Display *>(display))),
324 QGstGLDisplayHandle::HasRef);
327# if QT_CONFIG(gstreamer_gl_wayland)
328 if (platform.startsWith(QLatin1String(
"wayland"))) {
329 Q_ASSERT(!gstGlDisplay);
330 gstGlDisplay.reset(GST_GL_DISPLAY_CAST(gst_gl_display_wayland_new_with_display(
331 reinterpret_cast<
struct wl_display *>(display))),
332 QGstGLDisplayHandle::HasRef);
339 qWarning() <<
"Could not create GstGLDisplay";
343 void *nativeContext = pni->nativeResourceForContext(contextName, glContext);
345 qWarning() <<
"Could not find resource for" << contextName;
347 GstGLAPI glApi = QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL ? GST_GL_API_OPENGL : GST_GL_API_GLES2;
348 QGstGLContextHandle appContext{
349 gst_gl_context_new_wrapped(gstGlDisplay.get(), guintptr(nativeContext), glPlatform, glApi),
350 QGstGLContextHandle::HasRef,
353 qWarning() <<
"Could not create wrappped context for platform:" << glPlatform;
355 gst_gl_context_activate(appContext.get(),
true);
357 QUniqueGErrorHandle error;
358 gst_gl_context_fill_info(appContext.get(), &error);
360 qWarning() <<
"Could not fill context info:" << error;
364 QGstGLContextHandle displayContext;
365 gst_gl_display_create_context(gstGlDisplay.get(), appContext.get(), &displayContext, &error);
367 qWarning() <<
"Could not create display context:" << error;
371 m_gstGlDisplayContext.reset(gst_context_new(GST_GL_DISPLAY_CONTEXT_TYPE,
false),
372 QGstContextHandle::HasRef);
373 gst_context_set_gl_display(m_gstGlDisplayContext.get(), gstGlDisplay.get());
375 m_gstGlLocalContext.reset(gst_context_new(
"gst.gl.local_context",
false),
376 QGstContextHandle::HasRef);
377 GstStructure *structure = gst_context_writable_structure(m_gstGlLocalContext.get());
378 gst_structure_set(structure,
"context", GST_TYPE_GL_CONTEXT, displayContext.get(),
nullptr);
379 displayContext.reset();
388#include "moc_qgstreamervideosink_p.cpp"
void setNativeSize(QSize size)
QGstreamerRelayVideoSink(QObject *parent=nullptr)
~QGstreamerRelayVideoSink()
void setSubtitleText(const QString &subtitleText)
void connectPluggableVideoSink(QGstreamerPluggableVideoSink *pluggableSink)
void disconnectPluggableVideoSink()
Combined button and popup list for selecting options.