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
qgst.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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/qgst_p.h>
5
6#include <common/qgst_debug_p.h>
7#include <common/qgstpipeline_p.h>
8#include <common/qgstreamermessage_p.h>
9#include <common/qgstutils_p.h>
10#include <common/qgstvideobuffer_p.h>
11
12#if QT_CONFIG(linux_dmabuf)
13# include <common/qgstdmabufvideobuffer_p.h>
14#endif
15
16#include <QtCore/qdebug.h>
17#include <QtMultimedia/private/qvideoframe_p.h>
18#include <QtMultimedia/qcameradevice.h>
19
20#if QT_CONFIG(gstreamer_gl)
21# include <gst/gl/gstglmemory.h>
22#endif
23
24#include <array>
25#include <thread>
26
27// DMA support
28#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
29# include <gst/allocators/gstdmabuf.h>
30#endif
31
32// gstphysmemory.h ships with NXP/Freescale's imx-gst1.0-plugin allocator
33// package, not upstream gst-plugins-base, hence the guard.
34#if QT_CONFIG(gpu_vivante)
35# include <gst/allocators/gstphysmemory.h>
36#endif
37
38QT_BEGIN_NAMESPACE
39
40namespace {
41
47
48constexpr std::array<VideoFormat, 19> qt_videoFormatLookup{ {
49 { QVideoFrameFormat::Format_YUV420P, GST_VIDEO_FORMAT_I420 },
50 { QVideoFrameFormat::Format_YUV422P, GST_VIDEO_FORMAT_Y42B },
51 { QVideoFrameFormat::Format_YV12, GST_VIDEO_FORMAT_YV12 },
52 { QVideoFrameFormat::Format_UYVY, GST_VIDEO_FORMAT_UYVY },
53 { QVideoFrameFormat::Format_YUYV, GST_VIDEO_FORMAT_YUY2 },
54 { QVideoFrameFormat::Format_NV12, GST_VIDEO_FORMAT_NV12 },
55 { QVideoFrameFormat::Format_NV21, GST_VIDEO_FORMAT_NV21 },
56 { QVideoFrameFormat::Format_AYUV, GST_VIDEO_FORMAT_AYUV },
57 { QVideoFrameFormat::Format_Y8, GST_VIDEO_FORMAT_GRAY8 },
58 { QVideoFrameFormat::Format_XRGB8888, GST_VIDEO_FORMAT_xRGB },
59 { QVideoFrameFormat::Format_XBGR8888, GST_VIDEO_FORMAT_xBGR },
60 { QVideoFrameFormat::Format_RGBX8888, GST_VIDEO_FORMAT_RGBx },
61 { QVideoFrameFormat::Format_BGRX8888, GST_VIDEO_FORMAT_BGRx },
62 { QVideoFrameFormat::Format_ARGB8888, GST_VIDEO_FORMAT_ARGB },
63 { QVideoFrameFormat::Format_ABGR8888, GST_VIDEO_FORMAT_ABGR },
64 { QVideoFrameFormat::Format_RGBA8888, GST_VIDEO_FORMAT_RGBA },
65 { QVideoFrameFormat::Format_BGRA8888, GST_VIDEO_FORMAT_BGRA },
66#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
67 { QVideoFrameFormat::Format_Y16, GST_VIDEO_FORMAT_GRAY16_LE },
68 { QVideoFrameFormat::Format_P010, GST_VIDEO_FORMAT_P010_10LE },
69#else
70 { QVideoFrameFormat::Format_Y16, GST_VIDEO_FORMAT_GRAY16_BE },
71 { QVideoFrameFormat::Format_P010, GST_VIDEO_FORMAT_P010_10BE },
72#endif
73} };
74
76void appendDmaDrmPixelFormats(QGstCaps &caps, const QList<QVideoFrameFormat::PixelFormat> &formats)
77{
78 GValue drmFormatList = {};
79 g_value_init(&drmFormatList, GST_TYPE_LIST);
80
81 for (QVideoFrameFormat::PixelFormat format : formats) {
82 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
83 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
84 continue;
85
86 const guint32 fourcc =
87 gst_video_dma_drm_fourcc_from_format(gstFormat);
88 if (!fourcc)
89 continue;
90
91 GValue drmFormat = {};
92 g_value_init(&drmFormat, G_TYPE_STRING);
93 g_value_take_string(&drmFormat, gst_video_dma_drm_fourcc_to_string(fourcc, 0));
94 gst_value_list_append_value(&drmFormatList, &drmFormat);
95 g_value_unset(&drmFormat);
96 }
97
98 QGstStructure dmaDrmStructure{ "video/x-raw" };
99 dmaDrmStructure.setString("format", gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM));
100 dmaDrmStructure.setFractionRange("framerate", Fraction{ 0, 1 }, Fraction{ INT_MAX, 1 });
101 dmaDrmStructure.setIntRange("width", 1, INT_MAX);
102 dmaDrmStructure.setIntRange("height", 1, INT_MAX);
103 dmaDrmStructure.setValue("drm-format", &drmFormatList);
104
105 gst_caps_append_structure(caps.get(), dmaDrmStructure.release());
106 gst_caps_set_features(caps.get(), caps.size() - 1,
107 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
108 g_value_unset(&drmFormatList);
109}
110#endif
111
112} // namespace
113
114GstVideoFormat qGstVideoFormatFromPixelFormat(QVideoFrameFormat::PixelFormat format)
115{
116 for (const VideoFormat &formatPair : qt_videoFormatLookup)
117 if (formatPair.pixelFormat == format)
118 return formatPair.gstFormat;
119
120 return GST_VIDEO_FORMAT_UNKNOWN;
121}
122
124{
125 for (const VideoFormat &formatPair : qt_videoFormatLookup)
126 if (formatPair.gstFormat == format)
127 return formatPair.pixelFormat;
128
129 return QVideoFrameFormat::Format_Invalid;
130}
131
132// QGValue
133
134QGValue::QGValue(const GValue *v) : value(v) { }
135
136bool QGValue::isNull() const
137{
138 return !value;
139}
140
141std::optional<bool> QGValue::toBool() const
142{
143 if (!G_VALUE_HOLDS_BOOLEAN(value))
144 return std::nullopt;
145 return g_value_get_boolean(value);
146}
147
148std::optional<int> QGValue::toInt() const
149{
150 if (!G_VALUE_HOLDS_INT(value))
151 return std::nullopt;
152 return g_value_get_int(value);
153}
154
155std::optional<int> QGValue::toInt64() const
156{
157 if (!G_VALUE_HOLDS_INT64(value))
158 return std::nullopt;
159 return g_value_get_int64(value);
160}
161
162const char *QGValue::toString() const
163{
164 return value ? g_value_get_string(value) : nullptr;
165}
166
167std::optional<float> QGValue::getFraction() const
168{
169 if (!GST_VALUE_HOLDS_FRACTION(value))
170 return std::nullopt;
171 return (float)gst_value_get_fraction_numerator(value)
172 / (float)gst_value_get_fraction_denominator(value);
173}
174
175std::optional<QGRange<float>> QGValue::getFractionRange() const
176{
177 if (!GST_VALUE_HOLDS_FRACTION_RANGE(value))
178 return std::nullopt;
179 QGValue min = QGValue{ gst_value_get_fraction_range_min(value) };
180 QGValue max = QGValue{ gst_value_get_fraction_range_max(value) };
181 return QGRange<float>{ *min.getFraction(), *max.getFraction() };
182}
183
184std::optional<QGRange<int>> QGValue::toIntRange() const
185{
186 if (!GST_VALUE_HOLDS_INT_RANGE(value))
187 return std::nullopt;
188 return QGRange<int>{ gst_value_get_int_range_min(value), gst_value_get_int_range_max(value) };
189}
190
192{
193 if (!value || !GST_VALUE_HOLDS_STRUCTURE(value))
194 return QGstStructureView(nullptr);
195 return QGstStructureView(gst_value_get_structure(value));
196}
197
199{
200 if (!value || !GST_VALUE_HOLDS_CAPS(value))
201 return {};
202 return QGstCaps(gst_caps_copy(gst_value_get_caps(value)), QGstCaps::HasRef);
203}
204
205bool QGValue::isList() const
206{
207 return value && GST_VALUE_HOLDS_LIST(value);
208}
209
210int QGValue::listSize() const
211{
212 return gst_value_list_get_size(value);
213}
214
215QGValue QGValue::at(int index) const
216{
217 return QGValue{ gst_value_list_get_value(value, index) };
218}
219
220// QGstStructureView
221
222QGstStructureView::QGstStructureView(const GstStructure *s) : structure(s) { }
223
224QGstStructureView::QGstStructureView(const QUniqueGstStructureHandle &handle)
225 : QGstStructureView{ handle.get() }
226{
227}
228
230{
231 return QUniqueGstStructureHandle{ gst_structure_copy(structure) };
232}
233
235{
236 return !structure;
237}
238
240{
241 return gst_structure_get_name(structure);
242}
243
244QGValue QGstStructureView::operator[](const char *fieldname) const
245{
246 return QGValue{ gst_structure_get_value(structure, fieldname) };
247}
248
250{
251 return operator[]("caps").toCaps();
252}
253
255{
256 QGValue tags = operator[]("tags");
257 if (tags.isNull())
258 return {};
259
260 QGstTagListHandle tagList;
261 gst_structure_get(structure, "tags", GST_TYPE_TAG_LIST, &tagList, nullptr);
262 return tagList;
263}
264
266{
267 QSize size;
268
269 int w, h;
270 if (structure && gst_structure_get_int(structure, "width", &w)
271 && gst_structure_get_int(structure, "height", &h)) {
272 size.rwidth() = w;
273 size.rheight() = h;
274 }
275
276 return size;
277}
278
280{
281 QList<QVideoFrameFormat::PixelFormat> pixelFormats;
282
283 if (!structure)
284 return pixelFormats;
285
286 auto appendFromGstVideoFormat = [&](GstVideoFormat format) {
287 const QVideoFrameFormat::PixelFormat pixelFormat = qPixelFormatFromGstVideoFormat(format);
288 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
289 return;
290
291 if (!pixelFormats.contains(pixelFormat))
292 pixelFormats.append(pixelFormat);
293 };
294
295 if (gst_structure_has_name(structure, "video/x-raw")) {
297 if (const GValue *drmFormatValue = gst_structure_get_value(structure, "drm-format")) {
298 auto parseDrmFormat = [&](const GValue *value) {
299 if (!value || !G_VALUE_HOLDS_STRING(value))
300 return;
301
302 const char *drmFormatString = g_value_get_string(value);
303 if (!drmFormatString)
304 return;
305
306 guint64 modifier = 0;
307 const guint32 fourcc = gst_video_dma_drm_fourcc_from_string(drmFormatString,
308 &modifier);
309 if (fourcc) {
310 if (modifier != 0)
311 qWarning() << "Ignoring drm-format modifier when mapping to Qt PixelFormat:"
312 << drmFormatString;
313 appendFromGstVideoFormat(gst_video_dma_drm_fourcc_to_format(fourcc));
314 }
315 };
316
317 if (GST_VALUE_HOLDS_LIST(drmFormatValue)) {
318 const guint listSize = gst_value_list_get_size(drmFormatValue);
319 for (guint i = 0; i < listSize; ++i)
320 parseDrmFormat(gst_value_list_get_value(drmFormatValue, i));
321 } else {
322 parseDrmFormat(drmFormatValue);
323 }
324
325 if (!pixelFormats.isEmpty())
326 return pixelFormats; // Skip checking "format" field when "drm-format" succeeds
327 }
328#endif
329 if (const GValue *formatValue = gst_structure_get_value(structure, "format")) {
330 auto parseFormat = [&](const GValue *value) {
331 if (!value || !G_VALUE_HOLDS_STRING(value))
332 return;
333 const char *formatString = g_value_get_string(value);
334 if (!formatString)
335 return;
336 appendFromGstVideoFormat(gst_video_format_from_string(formatString));
337 };
338
339 if (GST_VALUE_HOLDS_LIST(formatValue)) {
340 const guint listSize = gst_value_list_get_size(formatValue);
341 for (guint i = 0; i < listSize; ++i)
342 parseFormat(gst_value_list_get_value(formatValue, i));
343 } else {
344 parseFormat(formatValue);
345 }
346 }
347 } else if (gst_structure_has_name(structure, "image/jpeg")) {
348 pixelFormats.append(QVideoFrameFormat::Format_Jpeg);
349 }
350
351 return pixelFormats;
352}
353
355{
356 if (!structure)
357 return { 0.f, 0.f };
358
359 std::optional<float> minRate;
360 std::optional<float> maxRate;
361
362 auto extractFraction = [](const GValue *v) -> float {
363 return (float)gst_value_get_fraction_numerator(v)
364 / (float)gst_value_get_fraction_denominator(v);
365 };
366 auto extractFrameRate = [&](const GValue *v) {
367 auto insert = [&](float min, float max) {
368 if (!maxRate || max > maxRate)
369 maxRate = max;
370 if (!minRate || min < minRate)
371 minRate = min;
372 };
373
374 if (GST_VALUE_HOLDS_FRACTION(v)) {
375 float rate = extractFraction(v);
376 insert(rate, rate);
377 } else if (GST_VALUE_HOLDS_FRACTION_RANGE(v)) {
378 const GValue *min = gst_value_get_fraction_range_min(v);
379 const GValue *max = gst_value_get_fraction_range_max(v);
380 insert(extractFraction(min), extractFraction(max));
381 }
382 };
383
384 const GValue *gstFrameRates = gst_structure_get_value(structure, "framerate");
385 if (gstFrameRates) {
386 if (GST_VALUE_HOLDS_LIST(gstFrameRates)) {
387 guint nFrameRates = gst_value_list_get_size(gstFrameRates);
388 for (guint f = 0; f < nFrameRates; ++f) {
389 extractFrameRate(gst_value_list_get_value(gstFrameRates, f));
390 }
391 } else {
392 extractFrameRate(gstFrameRates);
393 }
394 } else {
395 const GValue *min = gst_structure_get_value(structure, "min-framerate");
396 const GValue *max = gst_structure_get_value(structure, "max-framerate");
397 if (min && max) {
398 minRate = extractFraction(min);
399 maxRate = extractFraction(max);
400 }
401 }
402
403 if (!minRate || !maxRate)
404 return { 0.f, 0.f };
405
406 return {
407 minRate.value_or(*maxRate),
408 maxRate.value_or(*minRate),
409 };
410}
411
413{
414 if (!structure)
415 return std::nullopt;
416
417 const GValue *width = gst_structure_get_value(structure, "width");
418 const GValue *height = gst_structure_get_value(structure, "height");
419
420 if (!width || !height)
421 return std::nullopt;
422
423 for (const GValue *v : { width, height })
424 if (!GST_VALUE_HOLDS_INT_RANGE(v))
425 return std::nullopt;
426
427 int minWidth = gst_value_get_int_range_min(width);
428 int maxWidth = gst_value_get_int_range_max(width);
429 int minHeight = gst_value_get_int_range_min(height);
430 int maxHeight = gst_value_get_int_range_max(height);
431
432 return QGRange<QSize>{
433 QSize(minWidth, minHeight),
434 QSize(maxWidth, maxHeight),
435 };
436}
437
439{
440 GstMessage *message = nullptr;
441 gst_structure_get(structure, "message", GST_TYPE_MESSAGE, &message, nullptr);
442 return QGstreamerMessage(message, QGstreamerMessage::HasRef);
443}
444
446{
447 gint numerator;
448 gint denominator;
449 if (gst_structure_get_fraction(structure, "pixel-aspect-ratio", &numerator, &denominator)) {
450 return Fraction{
451 numerator,
452 denominator,
453 };
454 }
455
456 return std::nullopt;
457}
458
460{
461 QSize size = resolution();
462 if (!size.isValid()) {
463 qWarning() << Q_FUNC_INFO << "invalid resolution when querying nativeSize";
464 return size;
465 }
466
467 std::optional<Fraction> par = pixelAspectRatio();
468 if (par)
470 return size;
471}
472
473// QGstCaps
474
476{
477 GstVideoInfo vidInfo;
478 std::optional<guint64> dmaDrmModifier;
479
481 GstVideoInfoDmaDrm videoInfoDmaDrm;
482 if (gst_video_info_dma_drm_from_caps(&videoInfoDmaDrm, get())) {
483 dmaDrmModifier = videoInfoDmaDrm.drm_modifier;
484 // Sets vidInfo‘s format based on drm_fourcc from videoInfoDmaDrm:
485 if (!gst_video_info_dma_drm_to_video_info(&videoInfoDmaDrm, &vidInfo))
486 return std::nullopt;
487 } else
488#endif
489 {
490 if (!gst_video_info_from_caps(&vidInfo, get()))
491 return std::nullopt;
492 }
493
494 return QGstVideoInfo{vidInfo, dmaDrmModifier};
495}
496
497void QGstCaps::addPixelFormats(const QList<QVideoFrameFormat::PixelFormat> &formats,
498 const char *capsFeatures)
499{
500 if (!gst_caps_is_writable(get()))
501 *this = QGstCaps(gst_caps_make_writable(release()), QGstCaps::RefMode::HasRef);
502
504 if (qstrcmp(capsFeatures, GST_CAPS_FEATURE_MEMORY_DMABUF) == 0)
505 appendDmaDrmPixelFormats(*this, formats);
506#endif
507
508 GValue list = {};
509 g_value_init(&list, GST_TYPE_LIST);
510
511 for (QVideoFrameFormat::PixelFormat format : formats) {
512 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
513 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
514 continue;
515 GValue item = {};
516
517 g_value_init(&item, G_TYPE_STRING);
518 g_value_set_string(&item, gst_video_format_to_string(gstFormat));
519 gst_value_list_append_value(&list, &item);
520 g_value_unset(&item);
521 }
522
523 QGstStructure structure{ "video/x-raw" };
524 structure.setFractionRange("framerate", Fraction{ 0, 1 }, Fraction{ INT_MAX, 1 });
525 structure.setIntRange("width", 1, INT_MAX);
526 structure.setIntRange("height", 1, INT_MAX);
527 structure.setValue("format", &list);
528
529 gst_caps_append_structure(get(), structure.release());
530 g_value_unset(&list);
531
532 if (capsFeatures)
533 gst_caps_set_features(get(), size() - 1,
534 gst_caps_features_from_string(capsFeatures));
535}
536
537void QGstCaps::setResolution(QSize resolution)
538{
539 Q_ASSERT(resolution.isValid());
540 GValue width{};
541 g_value_init(&width, G_TYPE_INT);
542 g_value_set_int(&width, resolution.width());
543 GValue height{};
544 g_value_init(&height, G_TYPE_INT);
545 g_value_set_int(&height, resolution.height());
546
547 gst_caps_set_value(caps(), "width", &width);
548 gst_caps_set_value(caps(), "height", &height);
549}
550
551QGstCaps QGstCaps::fromCameraFormat(const QCameraFormat &format)
552{
553 QSize size = format.resolution();
554 auto caps = QGstCaps::create();
555
556 if (format.pixelFormat() == QVideoFrameFormat::Format_Jpeg) {
557 QGstStructure jpegStructure("image/jpeg");
558 jpegStructure.setInt("width", size.width());
559 jpegStructure.setInt("height", size.height());
560
561 gst_caps_append_structure(caps.get(), jpegStructure.release());
562 return caps;
563 }
564
565 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format.pixelFormat());
566 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
567 return {};
568
569 QGstStructure rawStructure("video/x-raw");
570 rawStructure.setString("format", gst_video_format_to_string(gstFormat));
571 rawStructure.setInt("width", size.width());
572 rawStructure.setInt("height", size.height());
573
574 gst_caps_append_structure(caps.get(), rawStructure.release());
575
577 if (const guint32 fourcc = gst_video_dma_drm_fourcc_from_format(gstFormat)) {
578 if (QGString drmFormat{gst_video_dma_drm_fourcc_to_string(fourcc, 0)}) {
579 QGstStructure drmFormatDmabufRawStructure("video/x-raw");
580 drmFormatDmabufRawStructure.setString(
581 "format", gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM));
582 drmFormatDmabufRawStructure.setString("drm-format", drmFormat.get());
583 drmFormatDmabufRawStructure.setInt("width", size.width());
584 drmFormatDmabufRawStructure.setInt("height", size.height());
585
586 gst_caps_append_structure(caps.get(), drmFormatDmabufRawStructure.release());
587 gst_caps_set_features(
588 caps.get(), caps.size() - 1,
589 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
590 }
591 }
592#endif
593
594#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
595 QGstStructure dmabufRawStructure("video/x-raw");
596 dmabufRawStructure.setString("format", gst_video_format_to_string(gstFormat));
597 dmabufRawStructure.setInt("width", size.width());
598 dmabufRawStructure.setInt("height", size.height());
599
600 gst_caps_append_structure(caps.get(), dmabufRawStructure.release());
601 gst_caps_set_features(caps.get(), caps.size() - 1,
602 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
603#endif
604
605 return caps;
606}
607
609{
610 return QGstCaps{
611 gst_caps_copy(caps()),
612 QGstCaps::HasRef,
613 };
614}
615
618 auto *features = gst_caps_get_features(get(), 0);
619 if (gst_caps_features_contains(features, "memory:GLMemory"))
620 return GLTexture;
621 if (gst_caps_features_contains(features, "memory:DMABuf"))
622 return DMABuf;
623 return CpuMemory;
624}
625
626int QGstCaps::size() const
627{
628 return int(gst_caps_get_size(get()));
629}
630
632{
633 return QGstStructureView{
634 gst_caps_get_structure(get(), index),
635 };
636}
637
639{
640 return get();
641}
642
644{
645 return QGstCaps(gst_caps_new_empty(), HasRef);
646}
647
648// QGstObject
649
650void QGstObject::set(const char *property, const char *str)
651{
652 g_object_set(get(), property, str, nullptr);
653}
654
655void QGstObject::set(const char *property, bool b)
656{
657 g_object_set(get(), property, gboolean(b), nullptr);
658}
659
660void QGstObject::set(const char *property, uint32_t i)
661{
662 g_object_set(get(), property, guint(i), nullptr);
663}
664
665void QGstObject::set(const char *property, int32_t i)
666{
667 g_object_set(get(), property, gint(i), nullptr);
668}
669
670void QGstObject::set(const char *property, int64_t i)
671{
672 g_object_set(get(), property, gint64(i), nullptr);
673}
674
675void QGstObject::set(const char *property, uint64_t i)
676{
677 g_object_set(get(), property, guint64(i), nullptr);
678}
679
680void QGstObject::set(const char *property, double d)
681{
682 g_object_set(get(), property, gdouble(d), nullptr);
683}
684
685void QGstObject::set(const char *property, const QGstObject &o)
686{
687 g_object_set(get(), property, o.object(), nullptr);
688}
689
690void QGstObject::set(const char *property, const QGstCaps &c)
691{
692 g_object_set(get(), property, c.caps(), nullptr);
693}
694
695void QGstObject::set(const char *property, void *object, GDestroyNotify destroyFunction)
696{
697 g_object_set_data_full(qGstCheckedCast<GObject>(get()), property, object, destroyFunction);
698}
699
700QGString QGstObject::getString(const char *property) const
701{
702 char *s = nullptr;
703 g_object_get(get(), property, &s, nullptr);
704 return QGString(s);
705}
706
707QGstStructureView QGstObject::getStructure(const char *property) const
708{
709 GstStructure *s = nullptr;
710 g_object_get(get(), property, &s, nullptr);
711 return QGstStructureView(s);
712}
713
714bool QGstObject::getBool(const char *property) const
715{
716 gboolean b = false;
717 g_object_get(get(), property, &b, nullptr);
718 return b;
719}
720
721uint QGstObject::getUInt(const char *property) const
722{
723 guint i = 0;
724 g_object_get(get(), property, &i, nullptr);
725 return i;
726}
727
728int QGstObject::getInt(const char *property) const
729{
730 gint i = 0;
731 g_object_get(get(), property, &i, nullptr);
732 return i;
733}
734
735quint64 QGstObject::getUInt64(const char *property) const
736{
737 guint64 i = 0;
738 g_object_get(get(), property, &i, nullptr);
739 return i;
740}
741
742qint64 QGstObject::getInt64(const char *property) const
743{
744 gint64 i = 0;
745 g_object_get(get(), property, &i, nullptr);
746 return i;
747}
748
749float QGstObject::getFloat(const char *property) const
750{
751 gfloat d = 0;
752 g_object_get(get(), property, &d, nullptr);
753 return d;
754}
755
756double QGstObject::getDouble(const char *property) const
757{
758 gdouble d = 0;
759 g_object_get(get(), property, &d, nullptr);
760 return d;
761}
762
763QGstObject QGstObject::getGstObject(const char *property) const
764{
765 GstObject *o = nullptr;
766 g_object_get(get(), property, &o, nullptr);
767 return QGstObject(o, HasRef);
768}
769
770void *QGstObject::getObject(const char *property) const
771{
772 return g_object_get_data(qGstCheckedCast<GObject>(get()), property);
773}
774
775QGObjectHandlerConnection QGstObject::connect(const char *name, GCallback callback,
776 gpointer userData)
777{
779 *this,
780 g_signal_connect(get(), name, callback, userData),
781 };
782}
783
784void QGstObject::disconnect(gulong handlerId)
785{
786 g_signal_handler_disconnect(get(), handlerId);
787}
788
790{
791 return G_OBJECT_TYPE(get());
792}
793
795{
796 return QLatin1StringView{
797 g_type_name(type()),
798 };
799}
800
802{
803 return get();
804}
805
807{
808 using namespace Qt::StringLiterals;
809
810 return get() ? QLatin1StringView{ GST_OBJECT_NAME(get()) } : "(null)"_L1;
811}
812
813// QGObjectHandlerConnection
814
816 : object{ std::move(object) }, handlerId{ handlerId }
817{
818}
819
821{
822 if (!object)
823 return;
824
825 object.disconnect(handlerId);
826 object = {};
827 handlerId = invalidHandlerId;
828}
829
830// QGObjectHandlerScopedConnection
831
839
841{
842 connection.disconnect();
843}
844
846{
847 connection.disconnect();
848}
849
850// QGstPad
851
853 : QGstPad{
856 }
857{
858}
859
860QGstPad::QGstPad(GstPad *pad, RefMode mode)
861 : QGstObject{
863 mode,
864 }
865{
866}
867
869{
870 return QGstCaps(gst_pad_get_current_caps(pad()), QGstCaps::HasRef);
871}
872
874{
875 return QGstCaps(gst_pad_query_caps(pad(), nullptr), QGstCaps::HasRef);
876}
877
879{
880 QGstTagListHandle tagList;
881 g_object_get(object(), "tags", &tagList, nullptr);
882 return tagList;
883}
884
886{
887 return QGString{
888 gst_pad_get_stream_id(pad()),
889 };
890}
891
893{
894 using namespace Qt::Literals;
895 using TrackType = QPlatformMediaPlayer::TrackType;
896
897 QLatin1StringView padName = name();
898
899 if (padName.startsWith("video_"_L1))
900 return TrackType::VideoStream;
901 if (padName.startsWith("audio_"_L1))
902 return TrackType::AudioStream;
903 if (padName.startsWith("text_"_L1))
904 return TrackType::SubtitleStream;
905
906 return std::nullopt;
907}
908
909bool QGstPad::isLinked() const
910{
911 return gst_pad_is_linked(pad());
912}
913
914bool QGstPad::link(const QGstPad &sink) const
915{
916 return gst_pad_link(pad(), sink.pad()) == GST_PAD_LINK_OK;
917}
918
919bool QGstPad::unlink(const QGstPad &sink) const
921 return gst_pad_unlink(pad(), sink.pad());
922}
923
924bool QGstPad::unlinkPeer() const
925{
926 QGstPad peerPad = peer();
927 if (peerPad)
928 return GST_PAD_IS_SRC(pad()) ? unlink(peerPad) : peerPad.unlink(*this);
929
930 return true;
931}
932
934{
935 return QGstPad(gst_pad_get_peer(pad()), HasRef);
936}
939{
940 return QGstElement(gst_pad_get_parent_element(pad()), HasRef);
941}
942
944{
945 return qGstCheckedCast<GstPad>(object());
946}
947
948GstEvent *QGstPad::stickyEvent(GstEventType type)
949{
950 return gst_pad_get_sticky_event(pad(), type, 0);
951}
952
953bool QGstPad::sendEvent(GstEvent *event)
954{
955 return gst_pad_send_event(pad(), event);
956}
957
958void QGstPad::sendFlushStartStop(bool resetTime)
959{
960 GstEvent *flushStart = gst_event_new_flush_start();
961 gboolean ret = sendEvent(flushStart);
962 if (!ret) {
963 qWarning("failed to send flush-start event");
964 return;
965 }
966
967 GstEvent *flushStop = gst_event_new_flush_stop(resetTime);
968 ret = sendEvent(flushStop);
969 if (!ret)
970 qWarning("failed to send flush-stop event");
971}
972
974{
975 using namespace std::chrono_literals;
976
977 GstState state = parent().state(1s);
978
979 if (state != GST_STATE_PAUSED)
980 return;
981
982 sendFlushStartStop(/*resetTime=*/true);
983}
984
985// QGstClock
986
988 : QGstClock{
991 }
992{
993}
994
995QGstClock::QGstClock(GstClock *clock, RefMode mode)
996 : QGstObject{
998 mode,
999 }
1000{
1001}
1002
1004{
1005 return qGstCheckedCast<GstClock>(object());
1006}
1007
1009{
1010 return gst_clock_get_time(clock());
1011}
1012
1013// QGstElement
1014
1015QGstElement::QGstElement(GstElement *element, RefMode mode)
1016 : QGstObject{
1018 mode,
1019 }
1020{
1021}
1022
1023QGstElement QGstElement::createFromFactory(const char *factory, const char *name)
1024{
1025 GstElement *element = gst_element_factory_make(factory, name);
1026
1027#ifndef QT_NO_DEBUG
1028 if (!element) {
1029 qWarning() << "Failed to make element" << name << "from factory" << factory;
1030 return QGstElement{};
1031 }
1032#endif
1033
1034 return QGstElement{
1035 element,
1036 NeedsRef,
1037 };
1038}
1039
1040QGstElement QGstElement::createFromFactory(GstElementFactory *factory, const char *name)
1041{
1042 return QGstElement{
1043 gst_element_factory_create(factory, name),
1044 NeedsRef,
1045 };
1046}
1047
1048QGstElement QGstElement::createFromFactory(const QGstElementFactoryHandle &factory,
1049 const char *name)
1050{
1051 return createFromFactory(factory.get(), name);
1052}
1053
1054QGstElement QGstElement::createFromDevice(const QGstDeviceHandle &device, const char *name)
1055{
1056 return createFromDevice(device.get(), name);
1057}
1058
1059QGstElement QGstElement::createFromDevice(GstDevice *device, const char *name)
1060{
1061 return QGstElement{
1062 gst_device_create_element(device, name),
1063 QGstElement::NeedsRef,
1064 };
1065}
1066
1068{
1069 QUniqueGErrorHandle error;
1070 QGstElement element{
1071 gst_parse_launch(str, &error),
1072 QGstElement::NeedsRef,
1073 };
1074
1075 if (error) // error does not mean that the element could not be constructed
1076 qWarning() << "gst_parse_launch error:" << error;
1077
1078 return element;
1079}
1080
1081QGstElement QGstElement::createFromPipelineDescription(const QByteArray &str)
1082{
1083 return createFromPipelineDescription(str.constData());
1084}
1085
1086QGstElementFactoryHandle QGstElement::findFactory(const char *name)
1087{
1088 return QGstElementFactoryHandle{
1089 gst_element_factory_find(name),
1090 QGstElementFactoryHandle::HasRef,
1091 };
1092}
1093
1094QGstElementFactoryHandle QGstElement::findFactory(const QByteArray &name)
1095{
1096 return findFactory(name.constData());
1097}
1098
1100{
1101 GstElementFactory *factory = gst_element_get_factory(element());
1102 if (!factory)
1103 return {};
1104
1105 const char *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1106 return name ? QByteArrayView{name} : QByteArrayView{};
1107}
1108
1109QGstPad QGstElement::staticPad(const char *name) const
1110{
1111 return QGstPad(gst_element_get_static_pad(element(), name), HasRef);
1112}
1113
1115{
1116 return staticPad("src");
1117}
1118
1120{
1121 return staticPad("sink");
1122}
1123
1124QGstPad QGstElement::getRequestPad(const char *name) const
1125{
1126 return QGstPad(gst_element_request_pad_simple(element(), name), HasRef);
1127}
1128
1130{
1131 return gst_element_release_request_pad(element(), pad.pad());
1132}
1133
1134GstState QGstElement::state(std::chrono::nanoseconds timeout) const
1135{
1136 using namespace std::chrono_literals;
1137
1138 GstState state;
1139 GstStateChangeReturn change =
1140 gst_element_get_state(element(), &state, nullptr, timeout.count());
1141
1142 if (Q_UNLIKELY(change == GST_STATE_CHANGE_ASYNC))
1143 qWarning() << "QGstElement::state detected an asynchronous state change. Return value not "
1144 "reliable";
1145
1146 return state;
1147}
1148
1150{
1151 return gst_element_set_state(element(), state);
1152}
1153
1154bool QGstElement::setStateSync(GstState state, std::chrono::nanoseconds timeout)
1155{
1156 GstStateChangeReturn change = gst_element_set_state(element(), state);
1157 if (change == GST_STATE_CHANGE_ASYNC)
1158 change = gst_element_get_state(element(), nullptr, &state, timeout.count());
1159
1160 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1161 qWarning() << "Could not change state of" << name() << "to" << state << change;
1162 dumpPipelineGraph("setStateSyncFailure");
1163 }
1164 return change == GST_STATE_CHANGE_SUCCESS || change == GST_STATE_CHANGE_NO_PREROLL;
1165}
1166
1168{
1169 Q_ASSERT(element());
1170 return gst_element_sync_state_with_parent(element()) == TRUE;
1171}
1172
1173bool QGstElement::finishStateChange(std::chrono::nanoseconds timeout)
1174{
1175 GstState state, pending;
1176 GstStateChangeReturn change =
1177 gst_element_get_state(element(), &state, &pending, timeout.count());
1178
1179 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1180 qWarning() << "Could not finish change state of" << name() << change << state << pending;
1181 dumpPipelineGraph("finishStateChangeFailure");
1182 }
1183 return change == GST_STATE_CHANGE_SUCCESS;
1184}
1185
1186bool QGstElement::hasAsyncStateChange(std::chrono::nanoseconds timeout) const
1187{
1188 GstState state;
1189 GstStateChangeReturn change =
1190 gst_element_get_state(element(), &state, nullptr, timeout.count());
1191 return change == GST_STATE_CHANGE_ASYNC;
1192}
1193
1194bool QGstElement::waitForAsyncStateChangeComplete(std::chrono::nanoseconds timeout) const
1195{
1196 using namespace std::chrono_literals;
1197 for (;;) {
1198 if (!hasAsyncStateChange())
1199 return true;
1200 timeout -= 10ms;
1201 if (timeout < 0ms)
1202 return false;
1203 std::this_thread::sleep_for(10ms);
1204 }
1205}
1206
1207void QGstElement::lockState(bool locked)
1208{
1209 gst_element_set_locked_state(element(), locked);
1210}
1211
1213{
1214 return gst_element_is_locked_state(element());
1215}
1216
1217void QGstElement::sendEvent(GstEvent *event) const
1218{
1219 gst_element_send_event(element(), event);
1220}
1221
1223{
1224 sendEvent(gst_event_new_eos());
1225}
1226
1227std::optional<std::chrono::nanoseconds> QGstElement::duration() const
1228{
1229 gint64 d;
1230 if (!gst_element_query_duration(element(), GST_FORMAT_TIME, &d)) {
1231 qDebug() << "QGstElement: failed to query duration";
1232 return std::nullopt;
1233 }
1234 return std::chrono::nanoseconds{ d };
1235}
1236
1237std::optional<std::chrono::milliseconds> QGstElement::durationInMs() const
1238{
1239 using namespace std::chrono;
1240 auto dur = duration();
1241 if (dur)
1242 return round<milliseconds>(*dur);
1243 return std::nullopt;
1244}
1245
1246std::optional<std::chrono::nanoseconds> QGstElement::position() const
1247{
1248 QGstQueryHandle &query = positionQuery();
1249
1250 gint64 pos;
1251 if (gst_element_query(element(), query.get())) {
1252 gst_query_parse_position(query.get(), nullptr, &pos);
1253 return std::chrono::nanoseconds{ pos };
1254 }
1255
1256 qDebug() << "QGstElement: failed to query position";
1257 return std::nullopt;
1258}
1259
1260std::optional<std::chrono::milliseconds> QGstElement::positionInMs() const
1261{
1262 using namespace std::chrono;
1263 auto pos = position();
1264 if (pos)
1265 return round<milliseconds>(*pos);
1266 return std::nullopt;
1267}
1268
1269std::optional<bool> QGstElement::canSeek() const
1270{
1271 QGstQueryHandle query{
1272 gst_query_new_seeking(GST_FORMAT_TIME),
1273 QGstQueryHandle::HasRef,
1274 };
1275 gboolean canSeek = false;
1276 gst_query_parse_seeking(query.get(), nullptr, &canSeek, nullptr, nullptr);
1277
1278 if (gst_element_query(element(), query.get())) {
1279 gst_query_parse_seeking(query.get(), nullptr, &canSeek, nullptr, nullptr);
1280 return canSeek;
1281 }
1282 return std::nullopt;
1283}
1284
1286{
1287 return gst_element_get_base_time(element());
1288}
1289
1290void QGstElement::setBaseTime(GstClockTime time) const
1291{
1292 gst_element_set_base_time(element(), time);
1293}
1294
1296{
1297 return GST_ELEMENT_CAST(get());
1298}
1299
1301{
1302 return QGstElement{
1303 qGstCheckedCast<GstElement>(gst_element_get_parent(object())),
1304 QGstElement::HasRef,
1305 };
1306}
1307
1309{
1310 return QGstBin{
1311 qGstCheckedCast<GstBin>(gst_element_get_parent(object())),
1312 QGstElement::HasRef,
1313 };
1314}
1315
1317{
1318 QGstElement ancestor = *this;
1319 for (;;) {
1320 QGstElement greatAncestor = ancestor.getParent();
1321 if (greatAncestor) {
1322 ancestor = std::move(greatAncestor);
1323 continue;
1324 }
1325 if (GST_IS_BIN(ancestor.element())) {
1326 return QGstBin{
1327 qGstSafeCast<GstBin>(ancestor.element()),
1328 QGstBin::NeedsRef,
1329 };
1330 } else {
1331 return QGstBin{};
1332 }
1333 }
1334}
1335
1337{
1338 QGstBin rootBin = getRootBin();
1339 if (GST_IS_PIPELINE(rootBin.get())) {
1340 return QGstPipeline{
1341 qGstSafeCast<GstPipeline>(rootBin.element()),
1342 QGstPipeline::NeedsRef,
1343 };
1344 } else {
1345 qWarning() << "QGstElement::getPipeline failed for element:" << *this;
1346 return QGstPipeline{};
1347 }
1348}
1349
1351{
1352 if (QGstBin parent = getParentBin())
1353 parent.remove(*this);
1354}
1355
1356void QGstElement::dumpPipelineGraph(const char *filename) const
1357{
1358 static const bool dumpEnabled = qEnvironmentVariableIsSet("GST_DEBUG_DUMP_DOT_DIR");
1359 if (dumpEnabled) {
1361 }
1362}
1363
1364QGstQueryHandle &QGstElement::positionQuery() const
1365{
1366 if (Q_UNLIKELY(!m_positionQuery))
1367 m_positionQuery = QGstQueryHandle{
1368 gst_query_new_position(GST_FORMAT_TIME),
1369 QGstQueryHandle::HasRef,
1370 };
1371
1372 return m_positionQuery;
1373}
1374
1375// QGstBin
1376
1377QGstBin QGstBin::create(const char *name)
1378{
1379 return QGstBin(gst_bin_new(name), NeedsRef);
1380}
1381
1382QGstBin QGstBin::createFromFactory(const char *factory, const char *name)
1383{
1384 QGstElement element = QGstElement::createFromFactory(factory, name);
1385 Q_ASSERT(GST_IS_BIN(element.element()));
1386 return QGstBin{
1387 GST_BIN(element.release()),
1388 RefMode::HasRef,
1389 };
1390}
1391
1392QGstBin QGstBin::createFromPipelineDescription(const QByteArray &pipelineDescription,
1393 const char *name, bool ghostUnlinkedPads)
1394{
1395 return createFromPipelineDescription(pipelineDescription.constData(), name, ghostUnlinkedPads);
1396}
1397
1398QGstBin QGstBin::createFromPipelineDescription(const char *pipelineDescription, const char *name,
1399 bool ghostUnlinkedPads)
1400{
1401 QUniqueGErrorHandle error;
1402
1403 GstElement *element =
1404 gst_parse_bin_from_description_full(pipelineDescription, ghostUnlinkedPads,
1405 /*context=*/nullptr, GST_PARSE_FLAG_NONE, &error);
1406
1407 if (!element) {
1408 qWarning() << "Failed to make element from pipeline description" << pipelineDescription
1409 << error;
1410 return QGstBin{};
1411 }
1412
1413 if (name)
1414 gst_element_set_name(element, name);
1415
1416 return QGstBin{
1417 element,
1418 NeedsRef,
1419 };
1420}
1421
1422QGstBin::QGstBin(GstBin *bin, RefMode mode)
1423 : QGstElement{
1425 mode,
1426 }
1427{
1428}
1429
1431{
1432 return qGstCheckedCast<GstBin>(object());
1433}
1434
1435void QGstBin::addGhostPad(const QGstElement &child, const char *name)
1436{
1437 addGhostPad(name, child.staticPad(name));
1438}
1439
1440void QGstBin::addGhostPad(const char *name, const QGstPad &pad)
1441{
1442 gst_element_add_pad(element(), gst_ghost_pad_new(name, pad.pad()));
1443}
1444
1445void QGstBin::addUnlinkedGhostPads(GstPadDirection direction)
1446{
1447 Q_ASSERT(direction != GstPadDirection::GST_PAD_UNKNOWN);
1448
1449 for (;;) {
1450 QGstPad unlinkedPad{
1451 gst_bin_find_unlinked_pad(bin(), direction),
1452 QGstPad::HasRef,
1453 };
1454
1455 if (!unlinkedPad)
1456 return;
1457
1458 addGhostPad(unlinkedPad.name().constData(), unlinkedPad);
1459 }
1460}
1461
1463{
1464 return gst_bin_sync_children_states(bin());
1465}
1466
1467void QGstBin::dumpGraph(const char *fileNamePrefix, bool includeTimestamp) const
1468{
1469 if (!get())
1470 return;
1471
1472 if (includeTimestamp)
1473 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1474 else
1475 GST_DEBUG_BIN_TO_DOT_FILE(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1476}
1477
1479{
1480 return QGstElement{
1481 gst_bin_get_by_name(bin(), name),
1482 QGstElement::NeedsRef,
1483 };
1484}
1485
1487{
1488 gst_bin_recalculate_latency(bin());
1489}
1490
1491// QGstBaseSink
1492
1493QGstBaseSink::QGstBaseSink(GstBaseSink *element, RefMode mode)
1494 : QGstElement{
1496 mode,
1497 }
1498{
1499}
1500
1501void QGstBaseSink::setSync(bool arg)
1502{
1503 gst_base_sink_set_sync(baseSink(), arg ? TRUE : FALSE);
1504}
1505
1507{
1508 return qGstCheckedCast<GstBaseSink>(element());
1509}
1510
1511// QGstBaseSrc
1512
1513QGstBaseSrc::QGstBaseSrc(GstBaseSrc *element, RefMode mode)
1514 : QGstElement{
1516 mode,
1517 }
1518{
1519}
1520
1522{
1523 return qGstCheckedCast<GstBaseSrc>(element());
1524}
1525
1526// QGstAppSink
1527
1528QGstAppSink::QGstAppSink(GstAppSink *element, RefMode mode)
1529 : QGstBaseSink{
1531 mode,
1532 }
1533{
1534}
1535
1537{
1538 QGstElement created = QGstElement::createFromFactory("appsink", name);
1539 return QGstAppSink{
1540 qGstCheckedCast<GstAppSink>(created.element()),
1541 QGstAppSink::NeedsRef,
1542 };
1543}
1544
1546{
1547 return qGstCheckedCast<GstAppSink>(element());
1548}
1549
1550# if GST_CHECK_VERSION(1, 24, 0)
1551void QGstAppSink::setMaxBufferTime(std::chrono::nanoseconds ns)
1552{
1553 gst_app_sink_set_max_time(appSink(), qGstClockTimeFromChrono(ns));
1554}
1555# endif
1556
1558{
1559 gst_app_sink_set_max_buffers(appSink(), n);
1560}
1561
1562void QGstAppSink::setCaps(const QGstCaps &caps)
1563{
1564 gst_app_sink_set_caps(appSink(), caps.caps());
1565}
1566
1567void QGstAppSink::setCallbacks(GstAppSinkCallbacks &callbacks, gpointer user_data,
1568 GDestroyNotify notify)
1569{
1570 gst_app_sink_set_callbacks(appSink(), &callbacks, user_data, notify);
1571}
1572
1574{
1575 return QGstSampleHandle{
1576 gst_app_sink_pull_sample(appSink()),
1577 QGstSampleHandle::HasRef,
1578 };
1579}
1580
1581// QGstAppSrc
1582
1583QGstAppSrc::QGstAppSrc(GstAppSrc *element, RefMode mode)
1584 : QGstBaseSrc{
1586 mode,
1587 }
1588{
1589}
1590
1591QGstAppSrc QGstAppSrc::create(const char *name)
1592{
1593 QGstElement created = QGstElement::createFromFactory("appsrc", name);
1594 return QGstAppSrc{
1595 qGstCheckedCast<GstAppSrc>(created.element()),
1596 QGstAppSrc::NeedsRef,
1597 };
1598}
1599
1601{
1602 return qGstCheckedCast<GstAppSrc>(element());
1603}
1604
1605void QGstAppSrc::setCallbacks(GstAppSrcCallbacks &callbacks, gpointer user_data,
1606 GDestroyNotify notify)
1607{
1608 gst_app_src_set_callbacks(appSrc(), &callbacks, user_data, notify);
1609}
1610
1611GstFlowReturn QGstAppSrc::pushBuffer(GstBuffer *buffer)
1612{
1613 return gst_app_src_push_buffer(appSrc(), buffer);
1614}
1615
1617{
1618 return QStringLiteral("Could not find the %1 GStreamer element")
1619 .arg(QLatin1StringView(element));
1620}
1621
1622QVideoFrameFormat qVideoFrameFormatFromGstVideoInfo(const QGstVideoInfo &qtVideoInfo)
1623{
1624 auto &vidInfo = qtVideoInfo.gstVideoInfo;
1625 GstVideoFormat gstFormat = GST_VIDEO_INFO_FORMAT(&vidInfo);
1626
1627 auto pixelFormat = qPixelFormatFromGstVideoFormat(gstFormat);
1628 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
1629 return QVideoFrameFormat();
1630
1631 QVideoFrameFormat format(QSize(vidInfo.width, vidInfo.height), pixelFormat);
1632
1633 if (vidInfo.fps_d > 0)
1634 format.setStreamFrameRate(qreal(vidInfo.fps_n) / vidInfo.fps_d);
1635
1636 QVideoFrameFormat::ColorRange range = QVideoFrameFormat::ColorRange_Unknown;
1637 switch (vidInfo.colorimetry.range) {
1638 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
1639 break;
1640 case GST_VIDEO_COLOR_RANGE_0_255:
1641 range = QVideoFrameFormat::ColorRange_Full;
1642 break;
1643 case GST_VIDEO_COLOR_RANGE_16_235:
1644 range = QVideoFrameFormat::ColorRange_Video;
1645 break;
1646 }
1647 format.setColorRange(range);
1648
1649 QVideoFrameFormat::ColorSpace colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
1650 switch (vidInfo.colorimetry.matrix) {
1651 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
1652 case GST_VIDEO_COLOR_MATRIX_RGB:
1653 case GST_VIDEO_COLOR_MATRIX_FCC:
1654 break;
1655 case GST_VIDEO_COLOR_MATRIX_BT709:
1656 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
1657 break;
1658 case GST_VIDEO_COLOR_MATRIX_BT601:
1659 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
1660 break;
1661 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
1662 colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
1663 break;
1664 case GST_VIDEO_COLOR_MATRIX_BT2020:
1665 colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
1666 break;
1667 }
1668 format.setColorSpace(colorSpace);
1669
1670 QVideoFrameFormat::ColorTransfer transfer = QVideoFrameFormat::ColorTransfer_Unknown;
1671 switch (vidInfo.colorimetry.transfer) {
1672 case GST_VIDEO_TRANSFER_UNKNOWN:
1673 break;
1674 case GST_VIDEO_TRANSFER_GAMMA10:
1675 transfer = QVideoFrameFormat::ColorTransfer_Linear;
1676 break;
1677 case GST_VIDEO_TRANSFER_GAMMA22:
1678 case GST_VIDEO_TRANSFER_SMPTE240M:
1679 case GST_VIDEO_TRANSFER_SRGB:
1680 case GST_VIDEO_TRANSFER_ADOBERGB:
1681 transfer = QVideoFrameFormat::ColorTransfer_Gamma22;
1682 break;
1683 case GST_VIDEO_TRANSFER_GAMMA18:
1684 case GST_VIDEO_TRANSFER_GAMMA20:
1685 // not quite, but best fit
1686 case GST_VIDEO_TRANSFER_BT709:
1687 case GST_VIDEO_TRANSFER_BT2020_12:
1688 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1689 break;
1690 case GST_VIDEO_TRANSFER_GAMMA28:
1691 transfer = QVideoFrameFormat::ColorTransfer_Gamma28;
1692 break;
1693 case GST_VIDEO_TRANSFER_LOG100:
1694 case GST_VIDEO_TRANSFER_LOG316:
1695 break;
1696 case GST_VIDEO_TRANSFER_SMPTE2084:
1697 transfer = QVideoFrameFormat::ColorTransfer_ST2084;
1698 break;
1699 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
1700 transfer = QVideoFrameFormat::ColorTransfer_STD_B67;
1701 break;
1702 case GST_VIDEO_TRANSFER_BT2020_10:
1703 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1704 break;
1705 case GST_VIDEO_TRANSFER_BT601:
1706 transfer = QVideoFrameFormat::ColorTransfer_BT601;
1707 break;
1708 }
1709 format.setColorTransfer(transfer);
1710
1711 return format;
1712}
1713
1715{
1716 Q_ASSERT(buffer);
1717
1718 [[maybe_unused]] GstMemory *mem = gst_buffer_peek_memory(buffer, 0);
1719
1720#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
1721 if (gst_is_dmabuf_memory(mem))
1722 return QGstCaps::DMABuf;
1723#endif
1724#if QT_CONFIG(gstreamer_gl)
1725 if (gst_is_gl_memory(mem))
1726 return QGstCaps::GLTexture;
1727#endif
1728 // Checked last: some phys-mem allocators (e.g. the i.MX8 VPU one) are also
1729 // dmabuf backed, and those should take the dmabuf path.
1730#if QT_CONFIG(gpu_vivante)
1731 if (gst_is_phys_memory(mem))
1732 return QGstCaps::VivantePhysMemory;
1733#endif
1734 return QGstCaps::CpuMemory;
1735}
1736
1737QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo,
1738 const QVideoFrameFormat &format)
1739{
1740#if QT_CONFIG(linux_dmabuf)
1741 if (auto dmaBufBuffer = QGstDmaBufVideoBuffer::create(buffer, videoInfo, format))
1742 return QVideoFramePrivate::createFrame(std::move(dmaBufBuffer), format);
1743#endif
1744 auto videoBuffer = std::make_unique<QGstVideoBuffer>(std::move(buffer), videoInfo, format);
1745 return QVideoFramePrivate::createFrame(std::move(videoBuffer), format);
1746}
1747
1748QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo)
1749{
1750 auto format = qVideoFrameFormatFromGstVideoInfo(videoInfo);
1751 return qCreateFrameFromGstBuffer(std::move(buffer), videoInfo, format);
1752}
1753
1754QT_END_NAMESPACE
QGObjectHandlerConnection(QGstObject object, gulong handler)
Definition qgst.cpp:815
QGObjectHandlerScopedConnection(QGObjectHandlerConnection connection)
Definition qgst.cpp:832
std::optional< int > toInt64() const
Definition qgst.cpp:155
std::optional< QGRange< int > > toIntRange() const
Definition qgst.cpp:184
std::optional< int > toInt() const
Definition qgst.cpp:148
int listSize() const
Definition qgst.cpp:210
bool isList() const
Definition qgst.cpp:205
QGstStructureView toStructure() const
Definition qgst.cpp:191
std::optional< QGRange< float > > getFractionRange() const
Definition qgst.cpp:175
const char * toString() const
Definition qgst.cpp:162
std::optional< float > getFraction() const
Definition qgst.cpp:167
QGValue at(int index) const
Definition qgst.cpp:215
bool isNull() const
Definition qgst.cpp:136
QGstCaps toCaps() const
Definition qgst.cpp:198
std::optional< bool > toBool() const
Definition qgst.cpp:141
QGValue(const GValue *v)
Definition qgst.cpp:134
QGstSampleHandle pullSample()
Definition qgst.cpp:1573
void setCallbacks(GstAppSinkCallbacks &callbacks, gpointer user_data, GDestroyNotify notify)
Definition qgst.cpp:1567
GstAppSink * appSink() const
Definition qgst.cpp:1545
void setCaps(const QGstCaps &caps)
Definition qgst.cpp:1562
void setMaxBuffers(int)
Definition qgst.cpp:1557
static QGstAppSink create(const char *name)
Definition qgst.cpp:1536
QGstAppSink(GstAppSink *, RefMode)
Definition qgst.cpp:1528
static QGstAppSrc create(const char *name)
Definition qgst.cpp:1591
void setCallbacks(GstAppSrcCallbacks &callbacks, gpointer user_data, GDestroyNotify notify)
Definition qgst.cpp:1605
QGstAppSrc(GstAppSrc *, RefMode)
Definition qgst.cpp:1583
GstAppSrc * appSrc() const
Definition qgst.cpp:1600
void setSync(bool)
Definition qgst.cpp:1501
QGstBaseSink(GstBaseSink *, RefMode)
Definition qgst.cpp:1493
GstBaseSink * baseSink() const
Definition qgst.cpp:1506
QGstBaseSrc(GstBaseSrc *, RefMode)
Definition qgst.cpp:1513
GstBaseSrc * baseSrc() const
Definition qgst.cpp:1521
void addUnlinkedGhostPads(GstPadDirection)
Definition qgst.cpp:1445
void dumpGraph(const char *fileNamePrefix, bool includeTimestamp=true) const
Definition qgst.cpp:1467
QGstElement findByName(const char *)
Definition qgst.cpp:1478
void recalculateLatency()
Definition qgst.cpp:1486
GstBin * bin() const
Definition qgst.cpp:1430
static QGstBin create(const char *name)
Definition qgst.cpp:1377
void addGhostPad(const QGstElement &child, const char *name)
Definition qgst.cpp:1435
static QGstBin createFromPipelineDescription(const char *pipelineDescription, const char *name=nullptr, bool ghostUnlinkedPads=false)
Definition qgst.cpp:1398
static QGstBin createFromFactory(const char *factory, const char *name)
Definition qgst.cpp:1382
void addGhostPad(const char *name, const QGstPad &pad)
Definition qgst.cpp:1440
bool syncChildrenState()
Definition qgst.cpp:1462
QGstBin(GstBin *bin, RefMode mode=NeedsRef)
Definition qgst.cpp:1422
MemoryFormat memoryFormat() const
Definition qgst.cpp:616
std::optional< QGstVideoInfo > videoInfo() const
Definition qgst.cpp:475
int size() const
Definition qgst.cpp:626
static QGstCaps fromCameraFormat(const QCameraFormat &format)
Definition qgst.cpp:551
static QGstCaps create()
Definition qgst.cpp:643
GstCaps * caps() const
Definition qgst.cpp:638
void setResolution(QSize)
Definition qgst.cpp:537
MemoryFormat
Definition qgst_p.h:394
@ DMABuf
Definition qgst_p.h:394
@ CpuMemory
Definition qgst_p.h:394
@ GLTexture
Definition qgst_p.h:394
QGstCaps copy() const
Definition qgst.cpp:608
void addPixelFormats(const QList< QVideoFrameFormat::PixelFormat > &formats, const char *capsFeatures=nullptr)
Definition qgst.cpp:497
QGstStructureView at(int index) const
Definition qgst.cpp:631
QGstClock(GstClock *clock, RefMode mode)
Definition qgst.cpp:995
GstClockTime time() const
Definition qgst.cpp:1008
GstClock * clock() const
Definition qgst.cpp:1003
QGstClock(const QGstObject &o)
Definition qgst.cpp:987
void setBaseTime(GstClockTime time) const
Definition qgst.cpp:1290
GstStateChangeReturn setState(GstState state)
Definition qgst.cpp:1149
std::optional< std::chrono::nanoseconds > position() const
Definition qgst.cpp:1246
GstElement * element() const
Definition qgst.cpp:1295
void removeFromParent()
Definition qgst.cpp:1350
QGstElement(GstElement *element, RefMode mode)
Definition qgst.cpp:1015
void lockState(bool locked)
Definition qgst.cpp:1207
QGstBin getRootBin() const
Definition qgst.cpp:1316
QGstPad getRequestPad(const char *name) const
Definition qgst.cpp:1124
std::optional< bool > canSeek() const
Definition qgst.cpp:1269
std::optional< std::chrono::nanoseconds > duration() const
Definition qgst.cpp:1227
bool isStateLocked() const
Definition qgst.cpp:1212
void sendEvent(GstEvent *event) const
Definition qgst.cpp:1217
void dumpPipelineGraph(const char *filename) const
Definition qgst.cpp:1356
GstClockTime baseTime() const
Definition qgst.cpp:1285
QByteArrayView factoryName() const
Definition qgst.cpp:1099
void releaseRequestPad(const QGstPad &pad) const
Definition qgst.cpp:1129
bool syncStateWithParent()
Definition qgst.cpp:1167
QGstPad sink() const
Definition qgst.cpp:1119
QGstPad staticPad(const char *name) const
Definition qgst.cpp:1109
void sendEos() const
Definition qgst.cpp:1222
QGstBin getParentBin() const
Definition qgst.cpp:1308
std::optional< std::chrono::milliseconds > positionInMs() const
Definition qgst.cpp:1260
static QGstElement createFromFactory(const char *factory, const char *name=nullptr)
Definition qgst.cpp:1023
QGstElement getParent() const
Definition qgst.cpp:1300
QGstPipeline getPipeline() const
Definition qgst.cpp:1336
static QGstElement createFromPipelineDescription(const char *)
Definition qgst.cpp:1067
QGstPad src() const
Definition qgst.cpp:1114
std::optional< std::chrono::milliseconds > durationInMs() const
Definition qgst.cpp:1237
void set(const char *property, double d)
Definition qgst.cpp:680
QGObjectHandlerConnection connect(const char *name, GCallback callback, gpointer userData)
Definition qgst.cpp:775
void * getObject(const char *property) const
Definition qgst.cpp:770
double getDouble(const char *property) const
Definition qgst.cpp:756
int getInt(const char *property) const
Definition qgst.cpp:728
void set(const char *property, int32_t i)
Definition qgst.cpp:665
QLatin1StringView name() const
Definition qgst.cpp:806
void set(const char *property, const QGstObject &o)
Definition qgst.cpp:685
QGString getString(const char *property) const
Definition qgst.cpp:700
void disconnect(gulong handlerId)
Definition qgst.cpp:784
QGstObject getGstObject(const char *property) const
Definition qgst.cpp:763
QLatin1StringView typeName() const
Definition qgst.cpp:794
void set(const char *property, const char *str)
Definition qgst.cpp:650
bool getBool(const char *property) const
Definition qgst.cpp:714
GType type() const
Definition qgst.cpp:789
QGstObject & operator=(QGstObject &&) noexcept=default
void set(const char *property, void *object, GDestroyNotify destroyFunction)
Definition qgst.cpp:695
void set(const char *property, bool b)
Definition qgst.cpp:655
quint64 getUInt64(const char *property) const
Definition qgst.cpp:735
QGstStructureView getStructure(const char *property) const
Definition qgst.cpp:707
qint64 getInt64(const char *property) const
Definition qgst.cpp:742
void set(const char *property, const QGstCaps &c)
Definition qgst.cpp:690
float getFloat(const char *property) const
Definition qgst.cpp:749
uint getUInt(const char *property) const
Definition qgst.cpp:721
GstObject * object() const
Definition qgst.cpp:801
bool link(const QGstPad &sink) const
Definition qgst.cpp:914
GstEvent * stickyEvent(GstEventType type)
Definition qgst.cpp:948
QGstTagListHandle tags() const
Definition qgst.cpp:878
QGstPad(const QGstObject &o)
Definition qgst.cpp:852
bool isLinked() const
Definition qgst.cpp:909
bool unlinkPeer() const
Definition qgst.cpp:924
QGstCaps queryCaps() const
Definition qgst.cpp:873
GstPad * pad() const
Definition qgst.cpp:943
QGstPad(GstPad *pad, RefMode mode)
Definition qgst.cpp:860
void sendFlushIfPaused()
Definition qgst.cpp:973
void sendFlushStartStop(bool resetTime)
Definition qgst.cpp:958
QGString streamId() const
Definition qgst.cpp:885
std::optional< QPlatformMediaPlayer::TrackType > inferTrackTypeFromName() const
Definition qgst.cpp:892
bool sendEvent(GstEvent *event)
Definition qgst.cpp:953
QGstCaps currentCaps() const
Definition qgst.cpp:868
QGstElement parent() const
Definition qgst.cpp:938
bool unlink(const QGstPad &sink) const
Definition qgst.cpp:919
QGstPad peer() const
Definition qgst.cpp:933
QGstStructureView(const QUniqueGstStructureHandle &)
Definition qgst.cpp:224
bool isNull() const
Definition qgst.cpp:234
QGRange< float > frameRateRange() const
Definition qgst.cpp:354
QSize nativeSize() const
Definition qgst.cpp:459
QGValue operator[](const char *fieldname) const
Definition qgst.cpp:244
QUniqueGstStructureHandle clone() const
Definition qgst.cpp:229
QList< QVideoFrameFormat::PixelFormat > pixelFormats() const
Definition qgst.cpp:279
std::optional< QGRange< QSize > > resolutionRange() const
Definition qgst.cpp:412
QGstreamerMessage getMessage()
Definition qgst.cpp:438
QGstCaps caps() const
Definition qgst.cpp:249
std::optional< Fraction > pixelAspectRatio() const
Definition qgst.cpp:445
QGstTagListHandle tags() const
Definition qgst.cpp:254
QSize resolution() const
Definition qgst.cpp:265
QByteArrayView name() const
Definition qgst.cpp:239
QGstStructureView(const GstStructure *)
Definition qgst.cpp:222
QSize qCalculateFrameSizeGStreamer(QSize resolution, Fraction par)
constexpr std::array< VideoFormat, 19 > qt_videoFormatLookup
Definition qgst.cpp:48
QGstCaps::MemoryFormat qMemoryFormatFromGstBuffer(GstBuffer *buffer)
Definition qgst.cpp:1714
QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo, const QVideoFrameFormat &format)
Definition qgst.cpp:1737
QVideoFrameFormat qVideoFrameFormatFromGstVideoInfo(const QGstVideoInfo &vidInfo)
Definition qgst.cpp:1622
#define QT_GSTREAMER_SUPPORTS_GST_VIDEO_FORMAT_DMA_DRM
Definition qgst_p.h:38
GstVideoFormat qGstVideoFormatFromPixelFormat(QVideoFrameFormat::PixelFormat format)
Definition qgst.cpp:114
QString qGstErrorMessageCannotFindElement(std::string_view element)
Definition qgst.cpp:1616
QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo)
Definition qgst.cpp:1748
QVideoFrameFormat::PixelFormat qPixelFormatFromGstVideoFormat(GstVideoFormat format)
Definition qgst.cpp:123
typedefQT_BEGIN_NAMESPACE struct _GstBuffer GstBuffer
void setInt(const char *field, int value)
Definition qgst_p.h:203
void setIntRange(const char *field, int min, int max)
Definition qgst_p.h:213
void setString(const char *field, const char *value)
Definition qgst_p.h:208
QGstStructure(const char *name)
Definition qgst_p.h:198
QVideoFrameFormat::PixelFormat pixelFormat
Definition qgst.cpp:44