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
qgstreamervideosource.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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
6
7#include <QtMultimedia/private/qplatformcamera_p.h>
8#include <QtMultimedia/private/qplatformmediaintegration_p.h>
9
10#include <QtCore/qdebug.h>
11
13
14/*!
15 \class QGStreamerVideoSource
16 \since 6.12
17
18 \brief The QGStreamerVideoSource class provides a video source backed by
19 a custom GStreamer element.
20
21 \inmodule QtMultimedia
22 \ingroup multimedia
23 \ingroup multimedia_video
24
25 \warning The GStreamerVideoSource class offers limited compatibility guarantees.
26 There are no source or binary compatibility guarantees for these classes,
27 meaning the API is only guaranteed to work with the Qt version the application
28 has been developed against. However, incompatible changes are aimed to be kept
29 at a minimum and will only be made in minor releases.
30 To use the class in an application, link to \c Qt::MultimediaPrivate (if using CMake),
31 and include the header #include <spi/qgstreamervideosource.h>.
32
33 Construct QGStreamerVideoSource from a GStreamer bin description
34 or from an existing \c GstElement and attach it to
35 QMediaCaptureSession by \l QMediaCaptureSession::setNativeVideoSource().
36
37 For any standard case, we recommend using QCamera instead of this class,
38 unless you need GStreamer-specific configuration to handle any custom
39 video source.
40
41 By default, the class is available in meta-qt6 builds. In custom builds
42 with GStreamer support, the \c gstreamer_qt_api feature can enable it.
43
44 QtMultimedia must be run with the \c gstreamer media backend for this class to work.
45
46 \code
47 QMediaCaptureSession session;
48 QGStreamerVideoSource videoSource(QStringLiteral("videotestsrc name=testsrc"));
49
50 session.setNativeVideoSource(&videoSource);
51 videoSource.start();
52 \endcode
53
54 \sa QMediaCaptureSession, QCamera
55*/
56
57/*!
58 \qmltype GStreamerVideoSource
59 \nativetype QGStreamerVideoSource
60 \inqmlmodule QtMultimedia
61 \ingroup multimedia_qml
62 \ingroup multimedia_video_qml
63
64 \brief A video source backed by a custom GStreamer bin.
65
66 Construct the video source from a GStreamer bin description
67 and attach it to CaptureSession using its
68 \l{CaptureSession::}{nativeVideoSource} property.
69
70 For any standard case, we recommend using \l Camera instead of this type,
71 unless you need GStreamer-specific configuration to handle any custom
72 video source.
73
74 By default, the type is available only in meta-qt6 builds. In custom builds
75 with GStreamer support, the \c gstreamer_qt_api feature can enable it.
76
77 QtMultimedia must be run with the \c gstreamer media backend for this class to work.
78
79\qml
80 Item {
81 CaptureSession {
82 nativeVideoSource: GStreamerVideoSource {
83 gstElementDescription: "videotestsrc name=testsrc"
84 active: true
85 }
86 videoOutput: preview
87 }
88
89 VideoOutput {
90 id: preview
91 }
92 }
93\endqml
94
95 \sa CaptureSession, Camera
96*/
97
98
99void QGStreamerVideoSourcePrivate::createPlatformCamera(QGStreamerVideoSource *source,
100 GstElementOrDescription elementOrDesc,
101 bool active)
102{
103 Q_ASSERT(!platformCamera);
104
105 auto maybePlatformCamera = QPlatformMediaIntegration::instance()->createGStreamerVideoSource(
106 source, elementOrDesc);
107 if (!maybePlatformCamera) {
108 qWarning() << "Failed to initialize QGStreamerVideoSource" << maybePlatformCamera.error();
109 return;
110 }
111
112 if (auto gstBinDesc = std::get_if<QString>(&elementOrDesc))
113 gstBinDescription = std::move(*gstBinDesc);
114
115 platformCamera = *maybePlatformCamera;
116
117 if (active)
118 platformCamera->setActive(true);
119
120 QObject::connect(platformCamera, &QPlatformVideoSource::activeChanged, source,
121 &QGStreamerVideoSource::activeChanged);
122}
123
124/*!
125 Constructs a QGStreamerVideoSource from the GStreamer bin
126 description \a gstBinDescription and a \a parent.
127
128 The GStreamer backend interprets the description and creates
129 a bin that can act as a video source for a capture session.
130 Use method \l gstElement() to access the created bin.
131
132 See \l{https://gstreamer.freedesktop.org/documentation/tools/gst-launch.html?gi-language=c#pipeline-description}{GStreamer documentation}
133 for the syntax of the description string.
134
135 If it's unable to parse the specified description, preventing the \c GstBin
136 from being created, the constructed QGStreamerVideoSource is non-functional,
137 \l gstElement() returns \c nullptr, and \l gstBinDescription() returns
138 an empty string.
139
140 \note QGStreamerVideoSource doesn't perform any additional validation
141 of the provided bin description, so if the created \c GstBin doesn't represent
142 a video input for \c GstPipeline, QGStreamerVideoSource and
143 the associated QMediaCaptureSession may misbehave.
144
145 For better control of the underlying GStreamer element, you can
146 construct QGStreamerVideoSource from an existing \c GstElement instead.
147
148 \sa gstElement(), gstBinDescription()
149*/
150QGStreamerVideoSource::QGStreamerVideoSource(const QString &gstBinDescription, QObject *parent)
151 : QGStreamerVideoSource(parent)
152{
153 Q_D(QGStreamerVideoSource);
154 d->createPlatformCamera(this, gstBinDescription);
155}
156
157/*!
158 Constructs a QGStreamerVideoSource from an existing \a gstElement and a
159 \a parent.
160
161 The element must provide a video source that a capture session can use.
162 The \c GstElement type is forward-declared in the \c QGStreamerVideoSource header,
163 so the corresponding type from the GStreamer library must be used.
164
165 If the specified element is null, the constructed video source is
166 non-functional.
167
168 \note The class doesn't perform any additional validation
169 of the provided element, so if the specified element doesn't represent
170 a video input for \c GstPipeline, QGStreamerVideoSource and
171 the associated QMediaCaptureSession may misbehave.
172
173 \note QGStreamerVideoSource shares ownership of the provided \c GstElement
174 with the caller.
175*/
176QGStreamerVideoSource::QGStreamerVideoSource(GstElement *gstElement, QObject *parent)
177 : QGStreamerVideoSource(parent)
178{
179 Q_D(QGStreamerVideoSource);
180 d->createPlatformCamera(this, gstElement);
181}
182
183QGStreamerVideoSource::QGStreamerVideoSource(QObject *parent)
184 : QObject(*new QGStreamerVideoSourcePrivate, parent)
185{
186}
187
188QGStreamerVideoSource::~QGStreamerVideoSource() = default;
189
190
191/*! \qmlproperty bool QtMultimedia::GStreamerVideoSource::active
192
193 Describes whether the video source is currently active.
194
195 If the video source has been connected to a \l CaptureSession,
196 this property indicates whether the underlying \c GstElement
197 is attached to the \c GstPipeline within the media capture session.
198*/
199
200/*! \property QGStreamerVideoSource::active
201
202 Describes whether the video source is currently active.
203
204 If the video source has been connected to a \l QMediaCaptureSession,
205 this property indicates whether the underlying \c GstElement
206 is attached to the \c GstPipeline within the media capture session.
207*/
208
209bool QGStreamerVideoSource::isActive() const
210{
211 Q_D(const QGStreamerVideoSource);
212 return d->platformCamera && d->platformCamera->isActive();
213}
214
215/*! \qmlmethod void GStreamerVideoSource::start()
216
217 Starts the video source.
218
219 Same as setting the \l active property to \c true.
220*/
221
222/*! \fn void QGStreamerVideoSource::start()
223
224 Starts the video source.
225
226 Same as setting the \l active property to \c true.
227*/
228
229/*! \qmlmethod void GStreamerVideoSource::stop()
230
231 Stops the video source.
232
233 Same as setting the \l active property to \c false.
234*/
235
236/*! \fn void QGStreamerVideoSource::stop()
237
238 Stops the video source.
239
240 Same as setting the \l active property to \c false.
241*/
242
243/*! \qmlproperty string QtMultimedia::GStreamerVideoSource::gstBinDescription
244
245 Describes the GStreamer bin description associated with the video source.
246
247 In QML, this property is required.
248
249 The GStreamer backend interprets the description and creates a bin,
250 that can act as a video source for a capture session.
251
252 See \l{https://gstreamer.freedesktop.org/documentation/tools/gst-launch.html?gi-language=c#pipeline-description}{GStreamer documentation}
253 for the syntax of the description string.
254
255 If the specified description is invalid, preventing a bin from
256 being created, the constructed \c GStreamerVideoSource is non-functional,
257 and the property returns an empty string.
258
259 \note GStreamerVideoSource doesn't perform any additional validation
260 of the provided bin description, so if the created \c GstBin doesn't represent
261 a video input for \c GstPipeline, GStreamerVideoSource and
262 the associated CaptureSession may misbehave.
263*/
264
265/*! \property QGStreamerVideoSource::gstBinDescription
266
267 Describes the GStreamer bin description associated with the video source.
268
269 If the video source is non-functional or has been constructed
270 from an existing \c GstElement, the property string is empty.
271*/
272QString QGStreamerVideoSource::gstBinDescription() const
273{
274 Q_D(const QGStreamerVideoSource);
275 return d->gstBinDescription;
276}
277
278/*!
279 Returns the underlying \c GstElement used by the video source,
280 or \c nullptr if the video source is non-functional.
281
282 If the video source has been constructed from a GStreamer bin description,
283 the returned \c GstElement is convertable to the \c GstBin subclass.
284
285 \note The method does not transfer ownership of the \c GstElement.
286*/
287GstElement *QGStreamerVideoSource::gstElement() const
288{
289 Q_D(const QGStreamerVideoSource);
290 return d->platformCamera ? d->platformCamera->rawGstElement() : nullptr;
291}
292
293void QGStreamerVideoSource::setActive(bool active)
294{
295 Q_D(const QGStreamerVideoSource);
296 if (d->platformCamera)
297 d->platformCamera->setActive(active);
298}
299
300QPlatformCamera *QGStreamerVideoSource::platformVideoSource() const
301{
302 Q_D(const QGStreamerVideoSource);
303 return d->platformCamera;
304}
305
306QMediaCaptureSession *QGStreamerVideoSource::captureSession() const
307{
308 Q_D(const QGStreamerVideoSource);
309 return d->captureSession;
310}
311
312void QGStreamerVideoSource::setCaptureSession(QMediaCaptureSession *session)
313{
314 Q_D(QGStreamerVideoSource);
315 d->captureSession = session;
316}
317
318QT_END_NAMESPACE
319
320#include "moc_qgstreamervideosource.cpp"
Combined button and popup list for selecting options.