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 auto *dmaDrmStructure =
89 gst_structure_new(
"video/x-raw",
"format", G_TYPE_STRING,
90 gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM),
91 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, INT_MAX, 1,
92 "width", GST_TYPE_INT_RANGE, 1, INT_MAX,
93 "height", GST_TYPE_INT_RANGE, 1, INT_MAX,
nullptr);
94 gst_structure_set_value(dmaDrmStructure,
"drm-format", &drmFormatList);
95 gst_caps_append_structure(caps.get(), dmaDrmStructure);
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);
513 auto *structure = gst_structure_new(
"video/x-raw",
"framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
514 INT_MAX, 1,
"width", GST_TYPE_INT_RANGE, 1, INT_MAX,
515 "height", GST_TYPE_INT_RANGE, 1, INT_MAX,
nullptr);
516 gst_structure_set_value(structure,
"format", &list);
517 gst_caps_append_structure(get(), structure);
518 g_value_unset(&list);
521 gst_caps_set_features(get(),
size() - 1,
522 gst_caps_features_from_string(capsFeatures));
527 Q_ASSERT(resolution.isValid());
529 g_value_init(&width, G_TYPE_INT);
530 g_value_set_int(&width, resolution.width());
532 g_value_init(&height, G_TYPE_INT);
533 g_value_set_int(&height, resolution.height());
535 gst_caps_set_value(caps(),
"width", &width);
536 gst_caps_set_value(caps(),
"height", &height);
541 QSize size = format.resolution();
544 if (format.pixelFormat() == QVideoFrameFormat::Format_Jpeg) {
545 auto *jpegStructure = gst_structure_new(
"image/jpeg",
546 "width", G_TYPE_INT, size.width(),
547 "height", G_TYPE_INT, size.height(),
nullptr);
548 gst_caps_append_structure(caps.get(), jpegStructure);
552 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format.pixelFormat());
553 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
557 gst_structure_new(
"video/x-raw",
558 "format", G_TYPE_STRING, gst_video_format_to_string(gstFormat),
559 "width", G_TYPE_INT, size.width(),
560 "height", G_TYPE_INT, size.height(),
nullptr);
561 gst_caps_append_structure(caps.get(), rawStructure);
564 if (
const guint32 fourcc = gst_video_dma_drm_fourcc_from_format(gstFormat)) {
565 if (QGString drmFormat{gst_video_dma_drm_fourcc_to_string(fourcc, 0)}) {
566 auto *drmFormatDmabufRawStructure =
567 gst_structure_new(
"video/x-raw",
"format", G_TYPE_STRING,
568 gst_video_format_to_string(GST_VIDEO_FORMAT_DMA_DRM),
569 "drm-format", G_TYPE_STRING, drmFormat.get(),
570 "width", G_TYPE_INT, size.width(),
571 "height", G_TYPE_INT, size.height(),
nullptr);
572 gst_caps_append_structure(caps.get(), drmFormatDmabufRawStructure);
573 gst_caps_set_features(
574 caps.get(), caps.size() - 1,
575 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
580#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
581 auto *dmabufRawStructure =
582 gst_structure_new(
"video/x-raw",
583 "format", G_TYPE_STRING, gst_video_format_to_string(gstFormat),
584 "width", G_TYPE_INT, size.width(),
585 "height", G_TYPE_INT, size.height(),
nullptr);
586 gst_caps_append_structure(caps.get(), dmabufRawStructure);
587 gst_caps_set_features(caps.get(), caps.size() - 1,
588 gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
597 gst_caps_copy(caps()),
604 auto *features = gst_caps_get_features(get(), 0);
605 if (gst_caps_features_contains(features,
"memory:GLMemory"))
607 if (gst_caps_features_contains(features,
"memory:DMABuf"))
614 return int(gst_caps_get_size(get()));
620 gst_caps_get_structure(get(), index),
631 return QGstCaps(gst_caps_new_empty(), HasRef);
638 g_object_set(get(), property, str,
nullptr);
643 g_object_set(get(), property, gboolean(b),
nullptr);
648 g_object_set(get(), property, guint(i),
nullptr);
653 g_object_set(get(), property, gint(i),
nullptr);
658 g_object_set(get(), property, gint64(i),
nullptr);
663 g_object_set(get(), property, guint64(i),
nullptr);
668 g_object_set(get(), property, gdouble(d),
nullptr);
673 g_object_set(get(), property, o.object(),
nullptr);
678 g_object_set(get(), property, c.caps(),
nullptr);
681void QGstObject::
set(
const char *property,
void *object, GDestroyNotify destroyFunction)
683 g_object_set_data_full(qGstCheckedCast<GObject>(get()), property, object, destroyFunction);
689 g_object_get(get(), property, &s,
nullptr);
695 GstStructure *s =
nullptr;
696 g_object_get(get(), property, &s,
nullptr);
703 g_object_get(get(), property, &b,
nullptr);
710 g_object_get(get(), property, &i,
nullptr);
717 g_object_get(get(), property, &i,
nullptr);
724 g_object_get(get(), property, &i,
nullptr);
731 g_object_get(get(), property, &i,
nullptr);
738 g_object_get(get(), property, &d,
nullptr);
745 g_object_get(get(), property, &d,
nullptr);
751 GstObject *o =
nullptr;
752 g_object_get(get(), property, &o,
nullptr);
753 return QGstObject(o, HasRef);
758 return g_object_get_data(qGstCheckedCast<GObject>(get()), property);
766 g_signal_connect(get(), name, callback, userData),
772 g_signal_handler_disconnect(get(), handlerId);
777 return G_OBJECT_TYPE(get());
782 return QLatin1StringView{
794 using namespace Qt::StringLiterals;
796 return get() ? QLatin1StringView{ GST_OBJECT_NAME(get()) } :
"(null)"_L1;
811 object.disconnect(handlerId);
813 handlerId = invalidHandlerId;
828 connection.disconnect();
833 connection.disconnect();
856 return QGstCaps(gst_pad_get_current_caps(pad()), QGstCaps::HasRef);
861 return QGstCaps(gst_pad_query_caps(pad(),
nullptr), QGstCaps::HasRef);
866 QGstTagListHandle tagList;
867 g_object_get(object(),
"tags", &tagList,
nullptr);
874 gst_pad_get_stream_id(pad()),
880 using namespace Qt::Literals;
881 QLatin1StringView padName = name();
883 if (padName.startsWith(
"video_"_L1))
884 return QPlatformMediaPlayer::TrackType::VideoStream;
885 if (padName.startsWith(
"audio_"_L1))
886 return QPlatformMediaPlayer::TrackType::AudioStream;
887 if (padName.startsWith(
"text_"_L1))
888 return QPlatformMediaPlayer::TrackType::SubtitleStream;
895 return gst_pad_is_linked(pad());
900 return gst_pad_link(pad(), sink.pad()) == GST_PAD_LINK_OK;
905 return gst_pad_unlink(pad(), sink.pad());
919 return QGstPad(gst_pad_get_peer(pad()), HasRef);
924 return QGstElement(gst_pad_get_parent_element(pad()), HasRef);
929 return qGstCheckedCast<GstPad>(object());
934 return gst_pad_get_sticky_event(pad(), type, 0);
939 return gst_pad_send_event(pad(), event);
944 GstEvent *flushStart = gst_event_new_flush_start();
945 gboolean ret = sendEvent(flushStart);
947 qWarning(
"failed to send flush-start event");
951 GstEvent *flushStop = gst_event_new_flush_stop(resetTime);
952 ret = sendEvent(flushStop);
954 qWarning(
"failed to send flush-stop event");
959 using namespace std::chrono_literals;
961 GstState state =
parent().state(1s);
963 if (state != GST_STATE_PAUSED)
989 return qGstCheckedCast<GstClock>(object());
994 return gst_clock_get_time(clock());
1009 GstElement *element = gst_element_factory_make(factory, name);
1013 qWarning() <<
"Failed to make element" << name <<
"from factory" << factory;
1014 return QGstElement{};
1027 gst_element_factory_create(factory, name),
1040 return createFromDevice(device.get(), name);
1046 gst_device_create_element(device, name),
1047 QGstElement::NeedsRef,
1053 QUniqueGErrorHandle error;
1055 gst_parse_launch(str, &error),
1056 QGstElement::NeedsRef,
1060 qWarning() <<
"gst_parse_launch error:" << error;
1072 return QGstElementFactoryHandle{
1073 gst_element_factory_find(name),
1074 QGstElementFactoryHandle::HasRef,
1078QGstElementFactoryHandle
QGstElement::findFactory(
const QByteArray &name)
1080 return findFactory(name.constData());
1085 GstElementFactory *factory = gst_element_get_factory(element());
1089 const char *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1090 return name ? QByteArrayView{name} : QByteArrayView{};
1095 return QGstPad(gst_element_get_static_pad(element(), name), HasRef);
1110 return QGstPad(gst_element_request_pad_simple(element(), name), HasRef);
1115 return gst_element_release_request_pad(element(), pad.pad());
1120 using namespace std::chrono_literals;
1123 GstStateChangeReturn change =
1124 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1126 if (Q_UNLIKELY(change == GST_STATE_CHANGE_ASYNC))
1127 qWarning() <<
"QGstElement::state detected an asynchronous state change. Return value not "
1135 return gst_element_set_state(element(), state);
1140 GstStateChangeReturn change = gst_element_set_state(element(), state);
1141 if (change == GST_STATE_CHANGE_ASYNC)
1142 change = gst_element_get_state(element(),
nullptr, &state, timeout.count());
1144 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1145 qWarning() <<
"Could not change state of" << name() <<
"to" << state << change;
1148 return change == GST_STATE_CHANGE_SUCCESS || change == GST_STATE_CHANGE_NO_PREROLL;
1153 Q_ASSERT(element());
1154 return gst_element_sync_state_with_parent(element()) == TRUE;
1159 GstState state, pending;
1160 GstStateChangeReturn change =
1161 gst_element_get_state(element(), &state, &pending, timeout.count());
1163 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1164 qWarning() <<
"Could not finish change state of" << name() << change << state << pending;
1167 return change == GST_STATE_CHANGE_SUCCESS;
1173 GstStateChangeReturn change =
1174 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1175 return change == GST_STATE_CHANGE_ASYNC;
1178bool QGstElement::waitForAsyncStateChangeComplete(
std::chrono::nanoseconds timeout)
const
1180 using namespace std::chrono_literals;
1182 if (!hasAsyncStateChange())
1187 std::this_thread::sleep_for(10ms);
1193 gst_element_set_locked_state(element(), locked);
1198 return gst_element_is_locked_state(element());
1203 gst_element_send_event(element(), event);
1208 sendEvent(gst_event_new_eos());
1214 if (!gst_element_query_duration(element(), GST_FORMAT_TIME, &d)) {
1215 qDebug() <<
"QGstElement: failed to query duration";
1216 return std::nullopt;
1218 return std::chrono::nanoseconds{ d };
1223 using namespace std::chrono;
1224 auto dur = duration();
1226 return round<milliseconds>(*dur);
1227 return std::nullopt;
1232 QGstQueryHandle &query = positionQuery();
1235 if (gst_element_query(element(), query.get())) {
1236 gst_query_parse_position(query.get(),
nullptr, &pos);
1237 return std::chrono::nanoseconds{ pos };
1240 qDebug() <<
"QGstElement: failed to query position";
1241 return std::nullopt;
1246 using namespace std::chrono;
1247 auto pos = position();
1249 return round<milliseconds>(*pos);
1250 return std::nullopt;
1255 QGstQueryHandle query{
1256 gst_query_new_seeking(GST_FORMAT_TIME),
1257 QGstQueryHandle::HasRef,
1259 gboolean canSeek =
false;
1260 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1262 if (gst_element_query(element(), query.get())) {
1263 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1266 return std::nullopt;
1271 return gst_element_get_base_time(element());
1276 gst_element_set_base_time(element(), time);
1281 return GST_ELEMENT_CAST(get());
1287 qGstCheckedCast<GstElement>(gst_element_get_parent(object())),
1288 QGstElement::HasRef,
1295 qGstCheckedCast<GstBin>(gst_element_get_parent(object())),
1296 QGstElement::HasRef,
1305 if (greatAncestor) {
1306 ancestor =
std::move(greatAncestor);
1309 if (GST_IS_BIN(ancestor.element())) {
1311 qGstSafeCast<GstBin>(ancestor.element()),
1323 if (GST_IS_PIPELINE(rootBin.get())) {
1324 return QGstPipeline{
1325 qGstSafeCast<GstPipeline>(rootBin.element()),
1326 QGstPipeline::NeedsRef,
1329 qWarning() <<
"QGstElement::getPipeline failed for element:" << *
this;
1330 return QGstPipeline{};
1336 if (QGstBin parent = getParentBin())
1337 parent.remove(*
this);
1342 static const bool dumpEnabled = qEnvironmentVariableIsSet(
"GST_DEBUG_DUMP_DOT_DIR");
1348QGstQueryHandle &
QGstElement::positionQuery()
const
1350 if (Q_UNLIKELY(!m_positionQuery))
1351 m_positionQuery = QGstQueryHandle{
1352 gst_query_new_position(GST_FORMAT_TIME),
1353 QGstQueryHandle::HasRef,
1356 return m_positionQuery;
1363 return QGstBin(gst_bin_new(name), NeedsRef);
1369 Q_ASSERT(GST_IS_BIN(element.element()));
1371 GST_BIN(element.release()),
1377 const char *name,
bool ghostUnlinkedPads)
1383 bool ghostUnlinkedPads)
1385 QUniqueGErrorHandle error;
1387 GstElement *element =
1388 gst_parse_bin_from_description_full(pipelineDescription, ghostUnlinkedPads,
1389 nullptr, GST_PARSE_FLAG_NONE, &error);
1392 qWarning() <<
"Failed to make element from pipeline description" << pipelineDescription
1398 gst_element_set_name(element, name);
1416 return qGstCheckedCast<GstBin>(object());
1426 gst_element_add_pad(element(), gst_ghost_pad_new(name, pad.pad()));
1431 Q_ASSERT(direction != GstPadDirection::GST_PAD_UNKNOWN);
1435 gst_bin_find_unlinked_pad(bin(), direction),
1442 addGhostPad(unlinkedPad.name().constData(), unlinkedPad);
1448 return gst_bin_sync_children_states(bin());
1456 if (includeTimestamp)
1457 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1459 GST_DEBUG_BIN_TO_DOT_FILE(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1465 gst_bin_get_by_name(bin(), name),
1466 QGstElement::NeedsRef,
1472 gst_bin_recalculate_latency(bin());
1487 gst_base_sink_set_sync(baseSink(), arg ? TRUE : FALSE);
1492 return qGstCheckedCast<GstBaseSink>(element());
1507 return qGstCheckedCast<GstBaseSrc>(element());
1524 qGstCheckedCast<GstAppSink>(created.element()),
1525 QGstAppSink::NeedsRef,
1531 return qGstCheckedCast<GstAppSink>(element());
1534# if GST_CHECK_VERSION(1
, 24
, 0
)
1535void QGstAppSink::setMaxBufferTime(std::chrono::nanoseconds ns)
1537 gst_app_sink_set_max_time(appSink(), qGstClockTimeFromChrono(ns));
1543 gst_app_sink_set_max_buffers(appSink(), n);
1548 gst_app_sink_set_caps(appSink(), caps.caps());
1552 GDestroyNotify notify)
1554 gst_app_sink_set_callbacks(appSink(), &callbacks, user_data, notify);
1559 return QGstSampleHandle{
1560 gst_app_sink_pull_sample(appSink()),
1561 QGstSampleHandle::HasRef,
1579 qGstCheckedCast<GstAppSrc>(created.element()),
1580 QGstAppSrc::NeedsRef,
1586 return qGstCheckedCast<GstAppSrc>(element());
1590 GDestroyNotify notify)
1592 gst_app_src_set_callbacks(appSrc(), &callbacks, user_data, notify);
1597 return gst_app_src_push_buffer(appSrc(), buffer);
1602 return QStringLiteral(
"Could not find the %1 GStreamer element")
1603 .arg(QLatin1StringView(element));
1608 auto &vidInfo = qtVideoInfo.gstVideoInfo;
1609 GstVideoFormat gstFormat = GST_VIDEO_INFO_FORMAT(&vidInfo);
1611 auto pixelFormat = qPixelFormatFromGstVideoFormat(gstFormat);
1612 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
1613 return QVideoFrameFormat();
1615 QVideoFrameFormat format(QSize(vidInfo.width, vidInfo.height), pixelFormat);
1617 if (vidInfo.fps_d > 0)
1618 format.setStreamFrameRate(qreal(vidInfo.fps_n) / vidInfo.fps_d);
1620 QVideoFrameFormat::ColorRange range = QVideoFrameFormat::ColorRange_Unknown;
1621 switch (vidInfo.colorimetry.range) {
1622 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
1624 case GST_VIDEO_COLOR_RANGE_0_255:
1625 range = QVideoFrameFormat::ColorRange_Full;
1627 case GST_VIDEO_COLOR_RANGE_16_235:
1628 range = QVideoFrameFormat::ColorRange_Video;
1631 format.setColorRange(range);
1633 QVideoFrameFormat::ColorSpace colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
1634 switch (vidInfo.colorimetry.matrix) {
1635 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
1636 case GST_VIDEO_COLOR_MATRIX_RGB:
1637 case GST_VIDEO_COLOR_MATRIX_FCC:
1639 case GST_VIDEO_COLOR_MATRIX_BT709:
1640 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
1642 case GST_VIDEO_COLOR_MATRIX_BT601:
1643 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
1645 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
1646 colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
1648 case GST_VIDEO_COLOR_MATRIX_BT2020:
1649 colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
1652 format.setColorSpace(colorSpace);
1654 QVideoFrameFormat::ColorTransfer transfer = QVideoFrameFormat::ColorTransfer_Unknown;
1655 switch (vidInfo.colorimetry.transfer) {
1656 case GST_VIDEO_TRANSFER_UNKNOWN:
1658 case GST_VIDEO_TRANSFER_GAMMA10:
1659 transfer = QVideoFrameFormat::ColorTransfer_Linear;
1661 case GST_VIDEO_TRANSFER_GAMMA22:
1662 case GST_VIDEO_TRANSFER_SMPTE240M:
1663 case GST_VIDEO_TRANSFER_SRGB:
1664 case GST_VIDEO_TRANSFER_ADOBERGB:
1665 transfer = QVideoFrameFormat::ColorTransfer_Gamma22;
1667 case GST_VIDEO_TRANSFER_GAMMA18:
1668 case GST_VIDEO_TRANSFER_GAMMA20:
1670 case GST_VIDEO_TRANSFER_BT709:
1671 case GST_VIDEO_TRANSFER_BT2020_12:
1672 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1674 case GST_VIDEO_TRANSFER_GAMMA28:
1675 transfer = QVideoFrameFormat::ColorTransfer_Gamma28;
1677 case GST_VIDEO_TRANSFER_LOG100:
1678 case GST_VIDEO_TRANSFER_LOG316:
1680 case GST_VIDEO_TRANSFER_SMPTE2084:
1681 transfer = QVideoFrameFormat::ColorTransfer_ST2084;
1683 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
1684 transfer = QVideoFrameFormat::ColorTransfer_STD_B67;
1686 case GST_VIDEO_TRANSFER_BT2020_10:
1687 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1689 case GST_VIDEO_TRANSFER_BT601:
1690 transfer = QVideoFrameFormat::ColorTransfer_BT601;
1693 format.setColorTransfer(transfer);
1704 [[maybe_unused]] GstMemory *mem = gst_buffer_peek_memory(buffer, 0);
1706#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
1707 if (gst_is_dmabuf_memory(mem))
1708 memoryFormat = QGstCaps::DMABuf;
1710#if QT_CONFIG(gstreamer_gl)
1711 if (gst_is_gl_memory(mem))
1712 memoryFormat = QGstCaps::GLTexture;
1714 return memoryFormat;
1719 auto format = qVideoFrameFormatFromGstVideoInfo(videoInfo);
1720 auto videoBuffer =
std::make_unique<QGstVideoBuffer>(buffer, videoInfo, format);
1721 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)