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");
147 emit aboutToBeDestroyed();
154 if (!m_gstVideoSink) {
158 updateSinkElement(m_gstQtSink);
166 if (m_isActive == isActive)
168 m_isActive = isActive;
171 m_gstQtSink.setActive(isActive);
176 m_sinkIsAsync = isAsync;
181 Q_ASSERT(pluggableSink);
182 m_pluggableVideoSink = pluggableSink;
183 m_pluggableVideoSink->setVideoFrame(m_currentVideoFrame);
184 m_pluggableVideoSink->setSubtitleText(m_currentSubtitleText);
185 m_pluggableVideoSink->setNativeSize(m_currentNativeSize);
186 connect(
this, &QGstreamerRelayVideoSink::videoFrameChanged,
187 m_pluggableVideoSink, &QPlatformVideoSink::setVideoFrame);
188 connect(
this, &QGstreamerRelayVideoSink::subtitleTextChanged,
189 m_pluggableVideoSink, &QPlatformVideoSink::setSubtitleText);
191 m_pluggableVideoSink, &QPlatformVideoSink::setNativeSize);
194 renderingRhiChanged(m_pluggableVideoSink->rhi());
195 m_rhiConnection = connect(m_pluggableVideoSink, &QPlatformVideoSink::rhiChanged,
this, [
this](){
196 renderingRhiChanged(m_pluggableVideoSink->rhi());
202 if (m_pluggableVideoSink) {
203 disconnect(
this, &QGstreamerRelayVideoSink::videoFrameChanged,
204 m_pluggableVideoSink, &QPlatformVideoSink::setVideoFrame);
205 disconnect(
this, &QGstreamerRelayVideoSink::subtitleTextChanged,
206 m_pluggableVideoSink, &QPlatformVideoSink::setSubtitleText);
208 m_pluggableVideoSink, &QPlatformVideoSink::setNativeSize);
209 disconnect(m_rhiConnection);
210 m_pluggableVideoSink =
nullptr;
216 emit videoFrameChanged(frame);
217 m_currentVideoFrame = frame;
222 emit subtitleTextChanged(subtitleText);
223 m_currentSubtitleText = subtitleText;
228 emit nativeSizeChanged(size);
229 m_currentNativeSize = size;
234 Q_ASSERT(!m_gstQtSink);
236 m_gstQtSink = QGstVideoRendererSink::createSink(
this);
238 m_gstQtSink.set(
"async",
false);
239 m_gstQtSink.setActive(m_isActive);
244 if (newSink == m_gstVideoSink)
247 m_gstCapsFilter.src().modifyPipelineInIdleProbe([&] {
249 m_sinkBin.stopAndRemoveElements(m_gstVideoSink);
251 m_gstVideoSink = std::move(newSink);
252 m_sinkBin.add(m_gstVideoSink);
253 qLinkGstElements(m_gstCapsFilter, m_gstVideoSink);
254 GstEvent *event = gst_event_new_reconfigure();
255 gst_element_send_event(m_gstVideoSink.element(), event);
256 m_gstVideoSink.syncStateWithParent();
259 m_sinkBin.dumpPipelineGraph(
"updateSinkElement");
264 m_gstGlDisplayContext.reset();
265 m_gstGlLocalContext.reset();
266 m_eglDisplay =
nullptr;
267 m_eglImageTargetTexture2D =
nullptr;
272 QRhi *currentRhi = rhi();
274 using namespace Qt::Literals;
278#if QT_CONFIG(gstreamer_gl)
279 if (!currentRhi || currentRhi->backend() != QRhi::OpenGLES2)
281 auto *nativeHandles =
static_cast<
const QRhiGles2NativeHandles *>(currentRhi->nativeHandles());
282 auto glContext = nativeHandles->context;
285 const QString platform = QGuiApplication::platformName();
286 QPlatformNativeInterface *pni = QGuiApplication::platformNativeInterface();
287 m_eglDisplay = pni->nativeResourceForIntegration(
"egldisplay"_ba);
290 QGstGLDisplayHandle gstGlDisplay;
292 QByteArray contextName =
"eglcontext"_ba;
293 GstGLPlatform glPlatform = GST_GL_PLATFORM_EGL;
296# if QT_CONFIG(gstreamer_gl_egl)
298 GST_GL_DISPLAY_CAST(gst_gl_display_egl_new_with_egl_display(m_eglDisplay)),
299 QGstGLDisplayHandle::HasRef);
300 m_eglImageTargetTexture2D = eglGetProcAddress(
"glEGLImageTargetTexture2DOES");
303 auto display = pni->nativeResourceForIntegration(
"display"_ba);
306# if QT_CONFIG(gstreamer_gl_x11)
307 if (platform == QLatin1String(
"xcb")) {
308 contextName =
"glxcontext"_ba;
309 glPlatform = GST_GL_PLATFORM_GLX;
311 gstGlDisplay.reset(GST_GL_DISPLAY_CAST(gst_gl_display_x11_new_with_display(
312 reinterpret_cast<Display *>(display))),
313 QGstGLDisplayHandle::HasRef);
316# if QT_CONFIG(gstreamer_gl_wayland)
317 if (platform.startsWith(QLatin1String(
"wayland"))) {
318 Q_ASSERT(!gstGlDisplay);
319 gstGlDisplay.reset(GST_GL_DISPLAY_CAST(gst_gl_display_wayland_new_with_display(
320 reinterpret_cast<
struct wl_display *>(display))),
321 QGstGLDisplayHandle::HasRef);
328 qWarning() <<
"Could not create GstGLDisplay";
332 void *nativeContext = pni->nativeResourceForContext(contextName, glContext);
334 qWarning() <<
"Could not find resource for" << contextName;
336 GstGLAPI glApi = QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL ? GST_GL_API_OPENGL : GST_GL_API_GLES2;
337 QGstGLContextHandle appContext{
338 gst_gl_context_new_wrapped(gstGlDisplay.get(), guintptr(nativeContext), glPlatform, glApi),
339 QGstGLContextHandle::HasRef,
342 qWarning() <<
"Could not create wrappped context for platform:" << glPlatform;
344 gst_gl_context_activate(appContext.get(),
true);
346 QUniqueGErrorHandle error;
347 gst_gl_context_fill_info(appContext.get(), &error);
349 qWarning() <<
"Could not fill context info:" << error;
353 QGstGLContextHandle displayContext;
354 gst_gl_display_create_context(gstGlDisplay.get(), appContext.get(), &displayContext, &error);
356 qWarning() <<
"Could not create display context:" << error;
360 m_gstGlDisplayContext.reset(gst_context_new(GST_GL_DISPLAY_CONTEXT_TYPE,
false),
361 QGstContextHandle::HasRef);
362 gst_context_set_gl_display(m_gstGlDisplayContext.get(), gstGlDisplay.get());
364 m_gstGlLocalContext.reset(gst_context_new(
"gst.gl.local_context",
false),
365 QGstContextHandle::HasRef);
366 GstStructure *structure = gst_context_writable_structure(m_gstGlLocalContext.get());
367 gst_structure_set(structure,
"context", GST_TYPE_GL_CONTEXT, displayContext.get(),
nullptr);
368 displayContext.reset();
383#include "moc_qgstreamervideosink_p.cpp"
void setNativeSize(QSize size)
QGstreamerRelayVideoSink(QObject *parent=nullptr)
~QGstreamerRelayVideoSink()
void setSubtitleText(const QString &subtitleText)
void connectPluggableVideoSink(QGstreamerPluggableVideoSink *pluggableSink)
void nativeSizeChanged(QSize size)
void disconnectPluggableVideoSink()
Combined button and popup list for selecting options.