Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgstreamervideosink.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
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>
8#include <rhi/qrhi.h>
9
10#include <QtCore/qdebug.h>
11#include <QtCore/qloggingcategory.h>
12
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>
20
21# if QT_CONFIG(gstreamer_gl_x11)
22# include <gst/gl/x11/gstgldisplay_x11.h>
23# endif
24# if QT_CONFIG(gstreamer_gl_egl)
25# include <gst/gl/egl/gstgldisplay_egl.h>
26# include <EGL/egl.h>
27# include <EGL/eglext.h>
28# endif
29# if QT_CONFIG(gstreamer_gl_wayland)
30# include <gst/gl/wayland/gstgldisplay_wayland.h>
31# endif
32#endif // #if QT_CONFIG(gstreamer_gl)
33
35
36Q_STATIC_LOGGING_CATEGORY(qLcGstVideoSink, "qt.multimedia.gstvideosink");
37
38QGstreamerPluggableVideoSink::QGstreamerPluggableVideoSink(QVideoSink *parent)
39 : QPlatformVideoSink{ parent }
40{
41}
42
43void QGstreamerPluggableVideoSink::setRhi(QRhi *rhi)
44{
45 if (m_rhi == rhi)
46 return;
47
48 m_rhi = rhi;
49 emit rhiChanged();
50}
51
53{
54 return m_rhi;
55}
56
58 : QObject{
59 parent,
60 },
62 QGstBin::create("videoSinkBin"),
63 }
64{
65 // This is a hack for some iMX and NVidia platforms. These require the use of a special video
66 // conversion element in the pipeline before the video sink, as they unfortunately
67 // output some proprietary format from the decoder even though it's sometimes marked as
68 // a regular supported video/x-raw format.
69 //
70 // To fix this, simply insert the element into the pipeline if it's available. Otherwise
71 // we simply use an identity element.
72 QGstElementFactoryHandle factory;
73
74 // QT_GSTREAMER_OVERRIDE_VIDEO_CONVERSION_ELEMENT allows users to override the
75 // conversion element. Ideally we construct the element programatically, though.
76 QByteArray preprocessOverride = qgetenv("QT_GSTREAMER_OVERRIDE_VIDEO_CONVERSION_ELEMENT");
77 if (!preprocessOverride.isEmpty()) {
78 qCDebug(qLcGstVideoSink) << "requesting conversion element from environment:"
79 << preprocessOverride;
80
81 m_gstPreprocess = QGstBin::createFromPipelineDescription(preprocessOverride, nullptr,
82 /*ghostUnlinkedPads=*/true);
83 if (!m_gstPreprocess)
84 qCWarning(qLcGstVideoSink) << "Cannot create conversion element:" << preprocessOverride;
85 }
86
87 if (!m_gstPreprocess) {
88 // This is a hack for some iMX and NVidia platforms. These require the use of a special
89 // video conversion element in the pipeline before the video sink, as they unfortunately
90 // output some proprietary format from the decoder even though it's sometimes marked as
91 // a regular supported video/x-raw format.
92 static constexpr auto decodersToTest = {
93 "imxvideoconvert_g2d",
94 "nvvidconv",
95 };
96
97 for (const char *decoder : decodersToTest) {
98 factory = QGstElement::findFactory(decoder);
99 if (factory)
100 break;
101 }
102
103 if (factory) {
104 qCDebug(qLcGstVideoSink)
105 << "instantiating conversion element:"
106 << g_type_name(gst_element_factory_get_element_type(factory.get()));
107
108 m_gstPreprocess = QGstElement::createFromFactory(factory, "preprocess");
109 }
110 }
111
112 bool disablePixelAspectRatio =
113 qEnvironmentVariableIsSet("QT_GSTREAMER_DISABLE_PIXEL_ASPECT_RATIO");
114 if (disablePixelAspectRatio) {
115 // Enabling the pixel aspect ratio may expose a gstreamer bug on cameras that don't expose a
116 // pixel-aspect-ratio via `VIDIOC_CROPCAP`. This can cause the caps negotiation to fail.
117 // Using the QT_GSTREAMER_DISABLE_PIXEL_ASPECT_RATIO environment variable, one can disable
118 // pixel-aspect-ratio handling
119 //
120 // compare: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6242
121 m_gstCapsFilter =
122 QGstElement::createFromFactory("identity", "nullPixelAspectRatioCapsFilter");
123 } else {
124 m_gstCapsFilter =
125 QGstElement::createFromFactory("capsfilter", "pixelAspectRatioCapsFilter");
126 QGstCaps capsFilterCaps{
127 gst_caps_new_simple("video/x-raw", "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL),
128 QGstCaps::HasRef,
129 };
130 g_object_set(m_gstCapsFilter.element(), "caps", capsFilterCaps.caps(), NULL);
131 }
132
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");
137 } else {
138 m_sinkBin.add(m_gstCapsFilter);
139 m_sinkBin.addGhostPad(m_gstCapsFilter, "sink");
140 }
141
142 updateGstContexts();
143}
144
146{
147 emit aboutToBeDestroyed();
148
149 unrefGstContexts();
150}
151
153{
154 if (!m_gstVideoSink) {
155 if (!m_gstQtSink)
156 createQtSink();
157
158 updateSinkElement(m_gstQtSink);
159 }
160
161 return m_sinkBin;
162}
163
165{
166 if (m_isActive == isActive)
167 return;
168 m_isActive = isActive;
169
170 if (m_gstQtSink)
171 m_gstQtSink.setActive(isActive);
172}
173
175{
176 m_sinkIsAsync = isAsync;
177}
178
180{
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);
192
193 // Update pipeline contexts using the rendering rhi
194 renderingRhiChanged(m_pluggableVideoSink->rhi());
195 m_rhiConnection = connect(m_pluggableVideoSink, &QPlatformVideoSink::rhiChanged, this, [this](){
196 renderingRhiChanged(m_pluggableVideoSink->rhi());
197 });
198}
199
201{
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;
211 }
212}
213
214void QGstreamerRelayVideoSink::setVideoFrame(const QVideoFrame &frame)
215{
216 emit videoFrameChanged(frame);
217 m_currentVideoFrame = frame;
218}
219
220void QGstreamerRelayVideoSink::setSubtitleText(const QString &subtitleText)
221{
222 emit subtitleTextChanged(subtitleText);
223 m_currentSubtitleText = subtitleText;
224}
225
227{
228 emit nativeSizeChanged(size);
229 m_currentNativeSize = size;
230}
231
232void QGstreamerRelayVideoSink::createQtSink()
233{
234 Q_ASSERT(!m_gstQtSink);
235
236 m_gstQtSink = QGstVideoRendererSink::createSink(this);
237 if (!m_sinkIsAsync)
238 m_gstQtSink.set("async", false);
239 m_gstQtSink.setActive(m_isActive);
240}
241
242void QGstreamerRelayVideoSink::updateSinkElement(QGstVideoRendererSinkElement newSink)
243{
244 if (newSink == m_gstVideoSink)
245 return;
246
247 m_gstCapsFilter.src().modifyPipelineInIdleProbe([&] {
248 if (m_gstVideoSink)
249 m_sinkBin.stopAndRemoveElements(m_gstVideoSink);
250
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();
257 });
258
259 m_sinkBin.dumpPipelineGraph("updateSinkElement");
260}
261
262void QGstreamerRelayVideoSink::unrefGstContexts()
263{
264 m_gstGlDisplayContext.reset();
265 m_gstGlLocalContext.reset();
266 m_eglDisplay = nullptr;
267 m_eglImageTargetTexture2D = nullptr;
268}
269
270void QGstreamerRelayVideoSink::updateGstContexts()
271{
272 QRhi *currentRhi = rhi();
273
274 using namespace Qt::Literals;
275
276 unrefGstContexts();
277
278#if QT_CONFIG(gstreamer_gl)
279 if (!currentRhi || currentRhi->backend() != QRhi::OpenGLES2)
280 return;
281 auto *nativeHandles = static_cast<const QRhiGles2NativeHandles *>(currentRhi->nativeHandles());
282 auto glContext = nativeHandles->context;
283 Q_ASSERT(glContext);
284
285 const QString platform = QGuiApplication::platformName();
286 QPlatformNativeInterface *pni = QGuiApplication::platformNativeInterface();
287 m_eglDisplay = pni->nativeResourceForIntegration("egldisplay"_ba);
288// qDebug() << "platform is" << platform << m_eglDisplay;
289
290 QGstGLDisplayHandle gstGlDisplay;
291
292 QByteArray contextName = "eglcontext"_ba;
293 GstGLPlatform glPlatform = GST_GL_PLATFORM_EGL;
294 // use the egl display if we have one
295 if (m_eglDisplay) {
296# if QT_CONFIG(gstreamer_gl_egl)
297 gstGlDisplay.reset(
298 GST_GL_DISPLAY_CAST(gst_gl_display_egl_new_with_egl_display(m_eglDisplay)),
299 QGstGLDisplayHandle::HasRef);
300 m_eglImageTargetTexture2D = eglGetProcAddress("glEGLImageTargetTexture2DOES");
301# endif
302 } else {
303 auto display = pni->nativeResourceForIntegration("display"_ba);
304
305 if (display) {
306# if QT_CONFIG(gstreamer_gl_x11)
307 if (platform == QLatin1String("xcb")) {
308 contextName = "glxcontext"_ba;
309 glPlatform = GST_GL_PLATFORM_GLX;
310
311 gstGlDisplay.reset(GST_GL_DISPLAY_CAST(gst_gl_display_x11_new_with_display(
312 reinterpret_cast<Display *>(display))),
313 QGstGLDisplayHandle::HasRef);
314 }
315# endif
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);
322 }
323#endif
324 }
325 }
326
327 if (!gstGlDisplay) {
328 qWarning() << "Could not create GstGLDisplay";
329 return;
330 }
331
332 void *nativeContext = pni->nativeResourceForContext(contextName, glContext);
333 if (!nativeContext)
334 qWarning() << "Could not find resource for" << contextName;
335
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,
340 };
341 if (!appContext)
342 qWarning() << "Could not create wrappped context for platform:" << glPlatform;
343
344 gst_gl_context_activate(appContext.get(), true);
345
346 QUniqueGErrorHandle error;
347 gst_gl_context_fill_info(appContext.get(), &error);
348 if (error) {
349 qWarning() << "Could not fill context info:" << error;
350 error = {};
351 }
352
353 QGstGLContextHandle displayContext;
354 gst_gl_display_create_context(gstGlDisplay.get(), appContext.get(), &displayContext, &error);
355 if (error)
356 qWarning() << "Could not create display context:" << error;
357
358 appContext.reset();
359
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());
363
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();
369
370 // Note: after updating the context, we switch the sink and send gst_event_new_reconfigure()
371 // upstream. this will cause the context to be queried again.
372#endif // #if QT_CONFIG(gstreamer_gl)
373}
374
375void QGstreamerRelayVideoSink::renderingRhiChanged(QRhi *rhi)
376{
377 m_rhi = rhi;
378 updateGstContexts();
379}
380
381QT_END_NAMESPACE
382
383#include "moc_qgstreamervideosink_p.cpp"
QGstreamerRelayVideoSink(QObject *parent=nullptr)
void setSubtitleText(const QString &subtitleText)
void connectPluggableVideoSink(QGstreamerPluggableVideoSink *pluggableSink)
void nativeSizeChanged(QSize size)
Combined button and popup list for selecting options.