4#include <common/qgst_p.h>
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>
12#include <QtCore/qdebug.h>
13#include <QtMultimedia/private/qvideoframe_p.h>
14#include <QtMultimedia/qcameradevice.h>
16#if QT_CONFIG(gstreamer_gl)
17# include <gst/gl/gstglmemory.h>
24#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
25# include <gst/allocators/gstdmabuf.h>
39 { QVideoFrameFormat::Format_YUV420P, GST_VIDEO_FORMAT_I420 },
40 { QVideoFrameFormat::Format_YUV422P, GST_VIDEO_FORMAT_Y42B },
41 { QVideoFrameFormat::Format_YV12, GST_VIDEO_FORMAT_YV12 },
42 { QVideoFrameFormat::Format_UYVY, GST_VIDEO_FORMAT_UYVY },
43 { QVideoFrameFormat::Format_YUYV, GST_VIDEO_FORMAT_YUY2 },
44 { QVideoFrameFormat::Format_NV12, GST_VIDEO_FORMAT_NV12 },
45 { QVideoFrameFormat::Format_NV21, GST_VIDEO_FORMAT_NV21 },
46 { QVideoFrameFormat::Format_AYUV, GST_VIDEO_FORMAT_AYUV },
47 { QVideoFrameFormat::Format_Y8, GST_VIDEO_FORMAT_GRAY8 },
48 { QVideoFrameFormat::Format_XRGB8888, GST_VIDEO_FORMAT_xRGB },
49 { QVideoFrameFormat::Format_XBGR8888, GST_VIDEO_FORMAT_xBGR },
50 { QVideoFrameFormat::Format_RGBX8888, GST_VIDEO_FORMAT_RGBx },
51 { QVideoFrameFormat::Format_BGRX8888, GST_VIDEO_FORMAT_BGRx },
52 { QVideoFrameFormat::Format_ARGB8888, GST_VIDEO_FORMAT_ARGB },
53 { QVideoFrameFormat::Format_ABGR8888, GST_VIDEO_FORMAT_ABGR },
54 { QVideoFrameFormat::Format_RGBA8888, GST_VIDEO_FORMAT_RGBA },
55 { QVideoFrameFormat::Format_BGRA8888, GST_VIDEO_FORMAT_BGRA },
56#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
57 { QVideoFrameFormat::Format_Y16, GST_VIDEO_FORMAT_GRAY16_LE },
58 { QVideoFrameFormat::Format_P010, GST_VIDEO_FORMAT_P010_10LE },
60 { QVideoFrameFormat::Format_Y16, GST_VIDEO_FORMAT_GRAY16_BE },
61 { QVideoFrameFormat::Format_P010, GST_VIDEO_FORMAT_P010_10BE },
66void appendDmaDrmPixelFormats(QGstCaps &caps,
const QList<QVideoFrameFormat::PixelFormat> &formats)
68 GValue drmFormatList = {};
69 g_value_init(&drmFormatList, GST_TYPE_LIST);
71 for (QVideoFrameFormat::PixelFormat format : formats) {
72 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
73 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
76 const guint32 fourcc =
77 gst_video_dma_drm_fourcc_from_format(gstFormat);
81 GValue drmFormat = {};
82 g_value_init(&drmFormat, G_TYPE_STRING);
83 g_value_take_string(&drmFormat, gst_video_dma_drm_fourcc_to_string(fourcc, 0));
84 gst_value_list_append_value(&drmFormatList, &drmFormat);
85 g_value_unset(&drmFormat);
88 QGstStructure dmaDrmStructure{
"video/x-raw" };
89 dmaDrmStructure.setString(
"format", gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM));
90 dmaDrmStructure.setFractionRange(
"framerate", Fraction{ 0, 1 }, Fraction{ INT_MAX, 1 });
91 dmaDrmStructure.setIntRange(
"width", 1, INT_MAX);
92 dmaDrmStructure.setIntRange(
"height", 1, INT_MAX);
93 dmaDrmStructure.setValue(
"drm-format", &drmFormatList);
95 gst_caps_append_structure(caps.get(), dmaDrmStructure.release());
96 gst_caps_set_features(caps.get(), caps.size() - 1,
97 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
98 g_value_unset(&drmFormatList);
106 for (
const VideoFormat &formatPair : qt_videoFormatLookup)
107 if (formatPair.pixelFormat == format)
108 return formatPair.gstFormat;
110 return GST_VIDEO_FORMAT_UNKNOWN;
115 for (
const VideoFormat &formatPair : qt_videoFormatLookup)
116 if (formatPair.gstFormat == format)
117 return formatPair.pixelFormat;
119 return QVideoFrameFormat::Format_Invalid;
133 if (!G_VALUE_HOLDS_BOOLEAN(value))
135 return g_value_get_boolean(value);
140 if (!G_VALUE_HOLDS_INT(value))
142 return g_value_get_int(value);
147 if (!G_VALUE_HOLDS_INT64(value))
149 return g_value_get_int64(value);
154 return value ? g_value_get_string(value) :
nullptr;
159 if (!GST_VALUE_HOLDS_FRACTION(value))
161 return (
float)gst_value_get_fraction_numerator(value)
162 / (
float)gst_value_get_fraction_denominator(value);
167 if (!GST_VALUE_HOLDS_FRACTION_RANGE(value))
171 return QGRange<
float>{ *min.getFraction(), *max.getFraction() };
176 if (!GST_VALUE_HOLDS_INT_RANGE(value))
178 return QGRange<
int>{ gst_value_get_int_range_min(value), gst_value_get_int_range_max(value) };
183 if (!value || !GST_VALUE_HOLDS_STRUCTURE(value))
184 return QGstStructureView(
nullptr);
190 if (!value || !GST_VALUE_HOLDS_CAPS(value))
192 return QGstCaps(gst_caps_copy(gst_value_get_caps(value)), QGstCaps::HasRef);
197 return value && GST_VALUE_HOLDS_LIST(value);
202 return gst_value_list_get_size(value);
207 return QGValue{ gst_value_list_get_value(value, index) };
221 return QUniqueGstStructureHandle{ gst_structure_copy(structure) };
231 return gst_structure_get_name(structure);
236 return QGValue{ gst_structure_get_value(structure, fieldname) };
250 QGstTagListHandle tagList;
251 gst_structure_get(structure,
"tags", GST_TYPE_TAG_LIST, &tagList,
nullptr);
260 if (structure && gst_structure_get_int(structure,
"width", &w)
261 && gst_structure_get_int(structure,
"height", &h)) {
271 QList<QVideoFrameFormat::PixelFormat> pixelFormats;
276 auto appendFromGstVideoFormat = [&](GstVideoFormat format) {
277 const QVideoFrameFormat::PixelFormat pixelFormat = qPixelFormatFromGstVideoFormat(format);
278 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
281 if (!pixelFormats.contains(pixelFormat))
282 pixelFormats.append(pixelFormat);
285 if (gst_structure_has_name(structure,
"video/x-raw")) {
287 if (
const GValue *drmFormatValue = gst_structure_get_value(structure,
"drm-format")) {
288 auto parseDrmFormat = [&](
const GValue *value) {
289 if (!value || !G_VALUE_HOLDS_STRING(value))
292 const char *drmFormatString = g_value_get_string(value);
293 if (!drmFormatString)
296 guint64 modifier = 0;
297 const guint32 fourcc = gst_video_dma_drm_fourcc_from_string(drmFormatString,
301 qWarning() <<
"Ignoring drm-format modifier when mapping to Qt PixelFormat:"
303 appendFromGstVideoFormat(gst_video_dma_drm_fourcc_to_format(fourcc));
307 if (GST_VALUE_HOLDS_LIST(drmFormatValue)) {
308 const guint listSize = gst_value_list_get_size(drmFormatValue);
309 for (guint i = 0; i < listSize; ++i)
310 parseDrmFormat(gst_value_list_get_value(drmFormatValue, i));
312 parseDrmFormat(drmFormatValue);
315 if (!pixelFormats.isEmpty())
319 if (
const GValue *formatValue = gst_structure_get_value(structure,
"format")) {
320 auto parseFormat = [&](
const GValue *value) {
321 if (!value || !G_VALUE_HOLDS_STRING(value))
323 const char *formatString = g_value_get_string(value);
326 appendFromGstVideoFormat(gst_video_format_from_string(formatString));
329 if (GST_VALUE_HOLDS_LIST(formatValue)) {
330 const guint listSize = gst_value_list_get_size(formatValue);
331 for (guint i = 0; i < listSize; ++i)
332 parseFormat(gst_value_list_get_value(formatValue, i));
334 parseFormat(formatValue);
337 }
else if (gst_structure_has_name(structure,
"image/jpeg")) {
338 pixelFormats.append(QVideoFrameFormat::Format_Jpeg);
349 std::optional<
float> minRate;
350 std::optional<
float> maxRate;
352 auto extractFraction = [](
const GValue *v) ->
float {
353 return (
float)gst_value_get_fraction_numerator(v)
354 / (
float)gst_value_get_fraction_denominator(v);
356 auto extractFrameRate = [&](
const GValue *v) {
357 auto insert = [&](
float min,
float max) {
358 if (!maxRate || max > maxRate)
360 if (!minRate || min < minRate)
364 if (GST_VALUE_HOLDS_FRACTION(v)) {
365 float rate = extractFraction(v);
367 }
else if (GST_VALUE_HOLDS_FRACTION_RANGE(v)) {
368 const GValue *min = gst_value_get_fraction_range_min(v);
369 const GValue *max = gst_value_get_fraction_range_max(v);
370 insert(extractFraction(min), extractFraction(max));
374 const GValue *gstFrameRates = gst_structure_get_value(structure,
"framerate");
376 if (GST_VALUE_HOLDS_LIST(gstFrameRates)) {
377 guint nFrameRates = gst_value_list_get_size(gstFrameRates);
378 for (guint f = 0; f < nFrameRates; ++f) {
379 extractFrameRate(gst_value_list_get_value(gstFrameRates, f));
382 extractFrameRate(gstFrameRates);
385 const GValue *min = gst_structure_get_value(structure,
"min-framerate");
386 const GValue *max = gst_structure_get_value(structure,
"max-framerate");
388 minRate = extractFraction(min);
389 maxRate = extractFraction(max);
393 if (!minRate || !maxRate)
397 minRate.value_or(*maxRate),
398 maxRate.value_or(*minRate),
407 const GValue *width = gst_structure_get_value(structure,
"width");
408 const GValue *height = gst_structure_get_value(structure,
"height");
410 if (!width || !height)
413 for (
const GValue *v : { width, height })
414 if (!GST_VALUE_HOLDS_INT_RANGE(v))
417 int minWidth = gst_value_get_int_range_min(width);
418 int maxWidth = gst_value_get_int_range_max(width);
419 int minHeight = gst_value_get_int_range_min(height);
420 int maxHeight = gst_value_get_int_range_max(height);
422 return QGRange<QSize>{
423 QSize(minWidth, minHeight),
424 QSize(maxWidth, maxHeight),
430 GstMessage *message =
nullptr;
431 gst_structure_get(structure,
"message", GST_TYPE_MESSAGE, &message,
nullptr);
432 return QGstreamerMessage(message, QGstreamerMessage::HasRef);
439 if (gst_structure_get_fraction(structure,
"pixel-aspect-ratio", &numerator, &denominator)) {
451 QSize size = resolution();
452 if (!size.isValid()) {
453 qWarning() << Q_FUNC_INFO <<
"invalid resolution when querying nativeSize";
467 GstVideoInfo vidInfo;
468 std::optional<guint64> dmaDrmModifier;
471 GstVideoInfoDmaDrm videoInfoDmaDrm;
472 if (gst_video_info_dma_drm_from_caps(&videoInfoDmaDrm, get())) {
473 dmaDrmModifier = videoInfoDmaDrm.drm_modifier;
475 if (!gst_video_info_dma_drm_to_video_info(&videoInfoDmaDrm, &vidInfo))
480 if (!gst_video_info_from_caps(&vidInfo, get()))
488 const char *capsFeatures)
490 if (!gst_caps_is_writable(get()))
491 *
this = QGstCaps(gst_caps_make_writable(release()), QGstCaps::RefMode::HasRef);
494 if (qstrcmp(capsFeatures, GST_CAPS_FEATURE_MEMORY_DMABUF) == 0)
495 appendDmaDrmPixelFormats(*
this, formats);
499 g_value_init(&list, GST_TYPE_LIST);
501 for (QVideoFrameFormat::PixelFormat format : formats) {
502 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
503 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
507 g_value_init(&item, G_TYPE_STRING);
508 g_value_set_string(&item, gst_video_format_to_string(gstFormat));
509 gst_value_list_append_value(&list, &item);
510 g_value_unset(&item);
514 structure.setFractionRange(
"framerate", Fraction{ 0, 1 }, Fraction{ INT_MAX, 1 });
517 structure.setValue(
"format", &list);
519 gst_caps_append_structure(get(), structure.release());
520 g_value_unset(&list);
523 gst_caps_set_features(get(),
size() - 1,
524 gst_caps_features_from_string(capsFeatures));
529 Q_ASSERT(resolution.isValid());
531 g_value_init(&width, G_TYPE_INT);
532 g_value_set_int(&width, resolution.width());
534 g_value_init(&height, G_TYPE_INT);
535 g_value_set_int(&height, resolution.height());
537 gst_caps_set_value(caps(),
"width", &width);
538 gst_caps_set_value(caps(),
"height", &height);
543 QSize size = format.resolution();
546 if (format.pixelFormat() == QVideoFrameFormat::Format_Jpeg) {
549 jpegStructure
.setInt("height", size.height()
);
551 gst_caps_append_structure(caps.get(), jpegStructure.release());
555 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format.pixelFormat());
556 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
560 rawStructure
.setString("format", gst_video_format_to_string(gstFormat)
);
564 gst_caps_append_structure(caps.get(), rawStructure.release());
567 if (
const guint32 fourcc = gst_video_dma_drm_fourcc_from_format(gstFormat)) {
568 if (QGString drmFormat{gst_video_dma_drm_fourcc_to_string(fourcc, 0)}) {
569 QGstStructure drmFormatDmabufRawStructure(
"video/x-raw");
570 drmFormatDmabufRawStructure.setString(
571 "format", gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM));
572 drmFormatDmabufRawStructure.setString(
"drm-format", drmFormat.get());
573 drmFormatDmabufRawStructure.setInt(
"width", size.width());
574 drmFormatDmabufRawStructure.setInt(
"height", size.height());
576 gst_caps_append_structure(caps.get(), drmFormatDmabufRawStructure.release());
577 gst_caps_set_features(
578 caps.get(), caps.size() - 1,
579 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
584#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
585 QGstStructure dmabufRawStructure(
"video/x-raw");
586 dmabufRawStructure.setString(
"format", gst_video_format_to_string(gstFormat));
587 dmabufRawStructure.setInt(
"width", size.width());
588 dmabufRawStructure.setInt(
"height", size.height());
590 gst_caps_append_structure(caps.get(), dmabufRawStructure.release());
591 gst_caps_set_features(caps.get(), caps.size() - 1,
592 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
601 gst_caps_copy(caps()),
608 auto *features = gst_caps_get_features(get(), 0);
609 if (gst_caps_features_contains(features,
"memory:GLMemory"))
611 if (gst_caps_features_contains(features,
"memory:DMABuf"))
618 return int(gst_caps_get_size(get()));
624 gst_caps_get_structure(get(), index),
635 return QGstCaps(gst_caps_new_empty(), HasRef);
642 g_object_set(get(), property, str,
nullptr);
647 g_object_set(get(), property, gboolean(b),
nullptr);
652 g_object_set(get(), property, guint(i),
nullptr);
657 g_object_set(get(), property, gint(i),
nullptr);
662 g_object_set(get(), property, gint64(i),
nullptr);
667 g_object_set(get(), property, guint64(i),
nullptr);
672 g_object_set(get(), property, gdouble(d),
nullptr);
677 g_object_set(get(), property, o.object(),
nullptr);
682 g_object_set(get(), property, c.caps(),
nullptr);
685void QGstObject::
set(
const char *property,
void *object, GDestroyNotify destroyFunction)
687 g_object_set_data_full(qGstCheckedCast<GObject>(get()), property, object, destroyFunction);
693 g_object_get(get(), property, &s,
nullptr);
699 GstStructure *s =
nullptr;
700 g_object_get(get(), property, &s,
nullptr);
707 g_object_get(get(), property, &b,
nullptr);
714 g_object_get(get(), property, &i,
nullptr);
721 g_object_get(get(), property, &i,
nullptr);
728 g_object_get(get(), property, &i,
nullptr);
735 g_object_get(get(), property, &i,
nullptr);
742 g_object_get(get(), property, &d,
nullptr);
749 g_object_get(get(), property, &d,
nullptr);
755 GstObject *o =
nullptr;
756 g_object_get(get(), property, &o,
nullptr);
757 return QGstObject(o, HasRef);
762 return g_object_get_data(qGstCheckedCast<GObject>(get()), property);
770 g_signal_connect(get(), name, callback, userData),
776 g_signal_handler_disconnect(get(), handlerId);
781 return G_OBJECT_TYPE(get());
786 return QLatin1StringView{
798 using namespace Qt::StringLiterals;
800 return get() ? QLatin1StringView{ GST_OBJECT_NAME(get()) } :
"(null)"_L1;
815 object.disconnect(handlerId);
817 handlerId = invalidHandlerId;
832 connection.disconnect();
837 connection.disconnect();
860 return QGstCaps(gst_pad_get_current_caps(pad()), QGstCaps::HasRef);
865 return QGstCaps(gst_pad_query_caps(pad(),
nullptr), QGstCaps::HasRef);
870 QGstTagListHandle tagList;
871 g_object_get(object(),
"tags", &tagList,
nullptr);
878 gst_pad_get_stream_id(pad()),
884 using namespace Qt::Literals;
885 using TrackType = QPlatformMediaPlayer::TrackType;
887 QLatin1StringView padName = name();
889 if (padName.startsWith(
"video_"_L1))
890 return TrackType::VideoStream;
891 if (padName.startsWith(
"audio_"_L1))
892 return TrackType::AudioStream;
893 if (padName.startsWith(
"text_"_L1))
894 return TrackType::SubtitleStream;
901 return gst_pad_is_linked(pad());
906 return gst_pad_link(pad(), sink.pad()) == GST_PAD_LINK_OK;
911 return gst_pad_unlink(pad(), sink.pad());
925 return QGstPad(gst_pad_get_peer(pad()), HasRef);
930 return QGstElement(gst_pad_get_parent_element(pad()), HasRef);
935 return qGstCheckedCast<GstPad>(object());
940 return gst_pad_get_sticky_event(pad(), type, 0);
945 return gst_pad_send_event(pad(), event);
950 GstEvent *flushStart = gst_event_new_flush_start();
951 gboolean ret = sendEvent(flushStart);
953 qWarning(
"failed to send flush-start event");
957 GstEvent *flushStop = gst_event_new_flush_stop(resetTime);
958 ret = sendEvent(flushStop);
960 qWarning(
"failed to send flush-stop event");
965 using namespace std::chrono_literals;
967 GstState state =
parent().state(1s);
969 if (state != GST_STATE_PAUSED)
995 return qGstCheckedCast<GstClock>(object());
1000 return gst_clock_get_time(clock());
1015 GstElement *element = gst_element_factory_make(factory, name);
1019 qWarning() <<
"Failed to make element" << name <<
"from factory" << factory;
1020 return QGstElement{};
1033 gst_element_factory_create(factory, name),
1046 return createFromDevice(device.get(), name);
1052 gst_device_create_element(device, name),
1053 QGstElement::NeedsRef,
1059 QUniqueGErrorHandle error;
1061 gst_parse_launch(str, &error),
1062 QGstElement::NeedsRef,
1066 qWarning() <<
"gst_parse_launch error:" << error;
1078 return QGstElementFactoryHandle{
1079 gst_element_factory_find(name),
1080 QGstElementFactoryHandle::HasRef,
1084QGstElementFactoryHandle
QGstElement::findFactory(
const QByteArray &name)
1086 return findFactory(name.constData());
1091 GstElementFactory *factory = gst_element_get_factory(element());
1095 const char *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1096 return name ? QByteArrayView{name} : QByteArrayView{};
1101 return QGstPad(gst_element_get_static_pad(element(), name), HasRef);
1116 return QGstPad(gst_element_request_pad_simple(element(), name), HasRef);
1121 return gst_element_release_request_pad(element(), pad.pad());
1126 using namespace std::chrono_literals;
1129 GstStateChangeReturn change =
1130 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1132 if (Q_UNLIKELY(change == GST_STATE_CHANGE_ASYNC))
1133 qWarning() <<
"QGstElement::state detected an asynchronous state change. Return value not "
1141 return gst_element_set_state(element(), state);
1146 GstStateChangeReturn change = gst_element_set_state(element(), state);
1147 if (change == GST_STATE_CHANGE_ASYNC)
1148 change = gst_element_get_state(element(),
nullptr, &state, timeout.count());
1150 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1151 qWarning() <<
"Could not change state of" << name() <<
"to" << state << change;
1154 return change == GST_STATE_CHANGE_SUCCESS || change == GST_STATE_CHANGE_NO_PREROLL;
1159 Q_ASSERT(element());
1160 return gst_element_sync_state_with_parent(element()) == TRUE;
1165 GstState state, pending;
1166 GstStateChangeReturn change =
1167 gst_element_get_state(element(), &state, &pending, timeout.count());
1169 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1170 qWarning() <<
"Could not finish change state of" << name() << change << state << pending;
1173 return change == GST_STATE_CHANGE_SUCCESS;
1179 GstStateChangeReturn change =
1180 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1181 return change == GST_STATE_CHANGE_ASYNC;
1184bool QGstElement::waitForAsyncStateChangeComplete(
std::chrono::nanoseconds timeout)
const
1186 using namespace std::chrono_literals;
1188 if (!hasAsyncStateChange())
1193 std::this_thread::sleep_for(10ms);
1199 gst_element_set_locked_state(element(), locked);
1204 return gst_element_is_locked_state(element());
1209 gst_element_send_event(element(), event);
1214 sendEvent(gst_event_new_eos());
1220 if (!gst_element_query_duration(element(), GST_FORMAT_TIME, &d)) {
1221 qDebug() <<
"QGstElement: failed to query duration";
1222 return std::nullopt;
1224 return std::chrono::nanoseconds{ d };
1229 using namespace std::chrono;
1230 auto dur = duration();
1232 return round<milliseconds>(*dur);
1233 return std::nullopt;
1238 QGstQueryHandle &query = positionQuery();
1241 if (gst_element_query(element(), query.get())) {
1242 gst_query_parse_position(query.get(),
nullptr, &pos);
1243 return std::chrono::nanoseconds{ pos };
1246 qDebug() <<
"QGstElement: failed to query position";
1247 return std::nullopt;
1252 using namespace std::chrono;
1253 auto pos = position();
1255 return round<milliseconds>(*pos);
1256 return std::nullopt;
1261 QGstQueryHandle query{
1262 gst_query_new_seeking(GST_FORMAT_TIME),
1263 QGstQueryHandle::HasRef,
1265 gboolean canSeek =
false;
1266 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1268 if (gst_element_query(element(), query.get())) {
1269 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1272 return std::nullopt;
1277 return gst_element_get_base_time(element());
1282 gst_element_set_base_time(element(), time);
1287 return GST_ELEMENT_CAST(get());
1293 qGstCheckedCast<GstElement>(gst_element_get_parent(object())),
1294 QGstElement::HasRef,
1301 qGstCheckedCast<GstBin>(gst_element_get_parent(object())),
1302 QGstElement::HasRef,
1311 if (greatAncestor) {
1312 ancestor =
std::move(greatAncestor);
1315 if (GST_IS_BIN(ancestor.element())) {
1317 qGstSafeCast<GstBin>(ancestor.element()),
1329 if (GST_IS_PIPELINE(rootBin.get())) {
1330 return QGstPipeline{
1331 qGstSafeCast<GstPipeline>(rootBin.element()),
1332 QGstPipeline::NeedsRef,
1335 qWarning() <<
"QGstElement::getPipeline failed for element:" << *
this;
1336 return QGstPipeline{};
1342 if (QGstBin parent = getParentBin())
1343 parent.remove(*
this);
1348 static const bool dumpEnabled = qEnvironmentVariableIsSet(
"GST_DEBUG_DUMP_DOT_DIR");
1354QGstQueryHandle &
QGstElement::positionQuery()
const
1356 if (Q_UNLIKELY(!m_positionQuery))
1357 m_positionQuery = QGstQueryHandle{
1358 gst_query_new_position(GST_FORMAT_TIME),
1359 QGstQueryHandle::HasRef,
1362 return m_positionQuery;
1369 return QGstBin(gst_bin_new(name), NeedsRef);
1375 Q_ASSERT(GST_IS_BIN(element.element()));
1377 GST_BIN(element.release()),
1383 const char *name,
bool ghostUnlinkedPads)
1389 bool ghostUnlinkedPads)
1391 QUniqueGErrorHandle error;
1393 GstElement *element =
1394 gst_parse_bin_from_description_full(pipelineDescription, ghostUnlinkedPads,
1395 nullptr, GST_PARSE_FLAG_NONE, &error);
1398 qWarning() <<
"Failed to make element from pipeline description" << pipelineDescription
1404 gst_element_set_name(element, name);
1422 return qGstCheckedCast<GstBin>(object());
1432 gst_element_add_pad(element(), gst_ghost_pad_new(name, pad.pad()));
1437 Q_ASSERT(direction != GstPadDirection::GST_PAD_UNKNOWN);
1441 gst_bin_find_unlinked_pad(bin(), direction),
1448 addGhostPad(unlinkedPad.name().constData(), unlinkedPad);
1454 return gst_bin_sync_children_states(bin());
1462 if (includeTimestamp)
1463 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1465 GST_DEBUG_BIN_TO_DOT_FILE(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1471 gst_bin_get_by_name(bin(), name),
1472 QGstElement::NeedsRef,
1478 gst_bin_recalculate_latency(bin());
1493 gst_base_sink_set_sync(baseSink(), arg ? TRUE : FALSE);
1498 return qGstCheckedCast<GstBaseSink>(element());
1513 return qGstCheckedCast<GstBaseSrc>(element());
1530 qGstCheckedCast<GstAppSink>(created.element()),
1531 QGstAppSink::NeedsRef,
1537 return qGstCheckedCast<GstAppSink>(element());
1540# if GST_CHECK_VERSION(1
, 24
, 0
)
1541void QGstAppSink::setMaxBufferTime(std::chrono::nanoseconds ns)
1543 gst_app_sink_set_max_time(appSink(), qGstClockTimeFromChrono(ns));
1549 gst_app_sink_set_max_buffers(appSink(), n);
1554 gst_app_sink_set_caps(appSink(), caps.caps());
1558 GDestroyNotify notify)
1560 gst_app_sink_set_callbacks(appSink(), &callbacks, user_data, notify);
1565 return QGstSampleHandle{
1566 gst_app_sink_pull_sample(appSink()),
1567 QGstSampleHandle::HasRef,
1585 qGstCheckedCast<GstAppSrc>(created.element()),
1586 QGstAppSrc::NeedsRef,
1592 return qGstCheckedCast<GstAppSrc>(element());
1596 GDestroyNotify notify)
1598 gst_app_src_set_callbacks(appSrc(), &callbacks, user_data, notify);
1603 return gst_app_src_push_buffer(appSrc(), buffer);
1608 return QStringLiteral(
"Could not find the %1 GStreamer element")
1609 .arg(QLatin1StringView(element));
1614 auto &vidInfo = qtVideoInfo.gstVideoInfo;
1615 GstVideoFormat gstFormat = GST_VIDEO_INFO_FORMAT(&vidInfo);
1617 auto pixelFormat = qPixelFormatFromGstVideoFormat(gstFormat);
1618 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
1619 return QVideoFrameFormat();
1621 QVideoFrameFormat format(QSize(vidInfo.width, vidInfo.height), pixelFormat);
1623 if (vidInfo.fps_d > 0)
1624 format.setStreamFrameRate(qreal(vidInfo.fps_n) / vidInfo.fps_d);
1626 QVideoFrameFormat::ColorRange range = QVideoFrameFormat::ColorRange_Unknown;
1627 switch (vidInfo.colorimetry.range) {
1628 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
1630 case GST_VIDEO_COLOR_RANGE_0_255:
1631 range = QVideoFrameFormat::ColorRange_Full;
1633 case GST_VIDEO_COLOR_RANGE_16_235:
1634 range = QVideoFrameFormat::ColorRange_Video;
1637 format.setColorRange(range);
1639 QVideoFrameFormat::ColorSpace colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
1640 switch (vidInfo.colorimetry.matrix) {
1641 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
1642 case GST_VIDEO_COLOR_MATRIX_RGB:
1643 case GST_VIDEO_COLOR_MATRIX_FCC:
1645 case GST_VIDEO_COLOR_MATRIX_BT709:
1646 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
1648 case GST_VIDEO_COLOR_MATRIX_BT601:
1649 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
1651 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
1652 colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
1654 case GST_VIDEO_COLOR_MATRIX_BT2020:
1655 colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
1658 format.setColorSpace(colorSpace);
1660 QVideoFrameFormat::ColorTransfer transfer = QVideoFrameFormat::ColorTransfer_Unknown;
1661 switch (vidInfo.colorimetry.transfer) {
1662 case GST_VIDEO_TRANSFER_UNKNOWN:
1664 case GST_VIDEO_TRANSFER_GAMMA10:
1665 transfer = QVideoFrameFormat::ColorTransfer_Linear;
1667 case GST_VIDEO_TRANSFER_GAMMA22:
1668 case GST_VIDEO_TRANSFER_SMPTE240M:
1669 case GST_VIDEO_TRANSFER_SRGB:
1670 case GST_VIDEO_TRANSFER_ADOBERGB:
1671 transfer = QVideoFrameFormat::ColorTransfer_Gamma22;
1673 case GST_VIDEO_TRANSFER_GAMMA18:
1674 case GST_VIDEO_TRANSFER_GAMMA20:
1676 case GST_VIDEO_TRANSFER_BT709:
1677 case GST_VIDEO_TRANSFER_BT2020_12:
1678 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1680 case GST_VIDEO_TRANSFER_GAMMA28:
1681 transfer = QVideoFrameFormat::ColorTransfer_Gamma28;
1683 case GST_VIDEO_TRANSFER_LOG100:
1684 case GST_VIDEO_TRANSFER_LOG316:
1686 case GST_VIDEO_TRANSFER_SMPTE2084:
1687 transfer = QVideoFrameFormat::ColorTransfer_ST2084;
1689 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
1690 transfer = QVideoFrameFormat::ColorTransfer_STD_B67;
1692 case GST_VIDEO_TRANSFER_BT2020_10:
1693 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1695 case GST_VIDEO_TRANSFER_BT601:
1696 transfer = QVideoFrameFormat::ColorTransfer_BT601;
1699 format.setColorTransfer(transfer);
1710 [[maybe_unused]] GstMemory *mem = gst_buffer_peek_memory(buffer, 0);
1712#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
1713 if (gst_is_dmabuf_memory(mem))
1714 memoryFormat = QGstCaps::DMABuf;
1716#if QT_CONFIG(gstreamer_gl)
1717 if (gst_is_gl_memory(mem))
1718 memoryFormat = QGstCaps::GLTexture;
1720 return memoryFormat;
1725 auto format = qVideoFrameFormatFromGstVideoInfo(videoInfo);
1726 auto videoBuffer =
std::make_unique<QGstVideoBuffer>(buffer, videoInfo, format);
1727 return QVideoFramePrivate::createFrame(std::move(videoBuffer), format);
QGObjectHandlerConnection(QGstObject object, gulong handler)
~QGObjectHandlerScopedConnection()
QGObjectHandlerScopedConnection(QGObjectHandlerConnection connection)
std::optional< int > toInt64() const
std::optional< QGRange< int > > toIntRange() const
std::optional< int > toInt() const
QGstStructureView toStructure() const
std::optional< QGRange< float > > getFractionRange() const
const char * toString() const
std::optional< float > getFraction() const
QGValue at(int index) const
std::optional< bool > toBool() const
QGstSampleHandle pullSample()
void setCallbacks(GstAppSinkCallbacks &callbacks, gpointer user_data, GDestroyNotify notify)
GstAppSink * appSink() const
void setCaps(const QGstCaps &caps)
static QGstAppSink create(const char *name)
QGstAppSink(GstAppSink *, RefMode)
static QGstAppSrc create(const char *name)
GstFlowReturn pushBuffer(GstBuffer *)
void setCallbacks(GstAppSrcCallbacks &callbacks, gpointer user_data, GDestroyNotify notify)
QGstAppSrc(GstAppSrc *, RefMode)
GstAppSrc * appSrc() const
QGstBaseSink(GstBaseSink *, RefMode)
GstBaseSink * baseSink() const
QGstBaseSrc(GstBaseSrc *, RefMode)
GstBaseSrc * baseSrc() const
void addUnlinkedGhostPads(GstPadDirection)
void dumpGraph(const char *fileNamePrefix, bool includeTimestamp=true) const
QGstElement findByName(const char *)
void recalculateLatency()
static QGstBin create(const char *name)
void addGhostPad(const QGstElement &child, const char *name)
static QGstBin createFromPipelineDescription(const char *pipelineDescription, const char *name=nullptr, bool ghostUnlinkedPads=false)
static QGstBin createFromFactory(const char *factory, const char *name)
void addGhostPad(const char *name, const QGstPad &pad)
QGstBin(GstBin *bin, RefMode mode=NeedsRef)
MemoryFormat memoryFormat() const
std::optional< QGstVideoInfo > videoInfo() const
static QGstCaps fromCameraFormat(const QCameraFormat &format)
void setResolution(QSize)
void addPixelFormats(const QList< QVideoFrameFormat::PixelFormat > &formats, const char *capsFeatures=nullptr)
QGstStructureView at(int index) const
QGstClock(GstClock *clock, RefMode mode)
GstClockTime time() const
QGstClock(const QGstObject &o)
void setBaseTime(GstClockTime time) const
GstStateChangeReturn setState(GstState state)
std::optional< std::chrono::nanoseconds > position() const
GstElement * element() const
QGstElement(GstElement *element, RefMode mode)
void lockState(bool locked)
QGstBin getRootBin() const
QGstPad getRequestPad(const char *name) const
std::optional< bool > canSeek() const
std::optional< std::chrono::nanoseconds > duration() const
bool isStateLocked() const
void sendEvent(GstEvent *event) const
void dumpPipelineGraph(const char *filename) const
GstClockTime baseTime() const
QByteArrayView factoryName() const
void releaseRequestPad(const QGstPad &pad) const
bool syncStateWithParent()
QGstPad staticPad(const char *name) const
QGstBin getParentBin() const
std::optional< std::chrono::milliseconds > positionInMs() const
static QGstElement createFromFactory(const char *factory, const char *name=nullptr)
QGstElement getParent() const
QGstPipeline getPipeline() const
static QGstElement createFromPipelineDescription(const char *)
std::optional< std::chrono::milliseconds > durationInMs() const
void set(const char *property, double d)
QGObjectHandlerConnection connect(const char *name, GCallback callback, gpointer userData)
void * getObject(const char *property) const
double getDouble(const char *property) const
int getInt(const char *property) const
void set(const char *property, int32_t i)
QLatin1StringView name() const
void set(const char *property, const QGstObject &o)
QGString getString(const char *property) const
void disconnect(gulong handlerId)
QGstObject getGstObject(const char *property) const
QLatin1StringView typeName() const
void set(const char *property, const char *str)
bool getBool(const char *property) const
QGstObject & operator=(QGstObject &&) noexcept=default
void set(const char *property, void *object, GDestroyNotify destroyFunction)
void set(const char *property, bool b)
quint64 getUInt64(const char *property) const
QGstStructureView getStructure(const char *property) const
qint64 getInt64(const char *property) const
void set(const char *property, const QGstCaps &c)
float getFloat(const char *property) const
uint getUInt(const char *property) const
GstObject * object() const
bool link(const QGstPad &sink) const
GstEvent * stickyEvent(GstEventType type)
QGstTagListHandle tags() const
QGstPad(const QGstObject &o)
QGstCaps queryCaps() const
QGstPad(GstPad *pad, RefMode mode)
void sendFlushStartStop(bool resetTime)
QGString streamId() const
std::optional< QPlatformMediaPlayer::TrackType > inferTrackTypeFromName() const
bool sendEvent(GstEvent *event)
QGstCaps currentCaps() const
QGstElement parent() const
bool unlink(const QGstPad &sink) const
QGstStructureView(const QUniqueGstStructureHandle &)
QGRange< float > frameRateRange() const
QGValue operator[](const char *fieldname) const
QUniqueGstStructureHandle clone() const
QList< QVideoFrameFormat::PixelFormat > pixelFormats() const
std::optional< QGRange< QSize > > resolutionRange() const
QGstreamerMessage getMessage()
std::optional< Fraction > pixelAspectRatio() const
QGstTagListHandle tags() const
QByteArrayView name() const
QGstStructureView(const GstStructure *)
QSize qCalculateFrameSizeGStreamer(QSize resolution, Fraction par)
constexpr std::array< VideoFormat, 19 > qt_videoFormatLookup
QGstCaps::MemoryFormat qMemoryFormatFromGstBuffer(GstBuffer *buffer)
QVideoFrameFormat qVideoFrameFormatFromGstVideoInfo(const QGstVideoInfo &vidInfo)
#define QT_GSTREAMER_SUPPORTS_GST_VIDEO_FORMAT_DMA_DRM
GstVideoFormat qGstVideoFormatFromPixelFormat(QVideoFrameFormat::PixelFormat format)
QString qGstErrorMessageCannotFindElement(std::string_view element)
QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo)
QVideoFrameFormat::PixelFormat qPixelFormatFromGstVideoFormat(GstVideoFormat format)
void setInt(const char *field, int value)
void setIntRange(const char *field, int min, int max)
void setString(const char *field, const char *value)
QGstStructure(const char *name)