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#if QT_CONFIG(linux_dmabuf)
13# include <common/qgstdmabufvideobuffer_p.h>
16#include <QtCore/qdebug.h>
17#include <QtMultimedia/private/qvideoframe_p.h>
18#include <QtMultimedia/qcameradevice.h>
20#if QT_CONFIG(gstreamer_gl)
21# include <gst/gl/gstglmemory.h>
28#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
29# include <gst/allocators/gstdmabuf.h>
34#if QT_CONFIG(gpu_vivante)
35# include <gst/allocators/gstphysmemory.h>
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 },
70 { QVideoFrameFormat::Format_Y16, GST_VIDEO_FORMAT_GRAY16_BE },
71 { QVideoFrameFormat::Format_P010, GST_VIDEO_FORMAT_P010_10BE },
76void appendDmaDrmPixelFormats(QGstCaps &caps,
const QList<QVideoFrameFormat::PixelFormat> &formats)
78 GValue drmFormatList = {};
79 g_value_init(&drmFormatList, GST_TYPE_LIST);
81 for (QVideoFrameFormat::PixelFormat format : formats) {
82 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
83 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
86 const guint32 fourcc =
87 gst_video_dma_drm_fourcc_from_format(gstFormat);
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);
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);
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);
116 for (
const VideoFormat &formatPair : qt_videoFormatLookup)
117 if (formatPair.pixelFormat == format)
118 return formatPair.gstFormat;
120 return GST_VIDEO_FORMAT_UNKNOWN;
125 for (
const VideoFormat &formatPair : qt_videoFormatLookup)
126 if (formatPair.gstFormat == format)
127 return formatPair.pixelFormat;
129 return QVideoFrameFormat::Format_Invalid;
143 if (!G_VALUE_HOLDS_BOOLEAN(value))
145 return g_value_get_boolean(value);
150 if (!G_VALUE_HOLDS_INT(value))
152 return g_value_get_int(value);
157 if (!G_VALUE_HOLDS_INT64(value))
159 return g_value_get_int64(value);
164 return value ? g_value_get_string(value) :
nullptr;
169 if (!GST_VALUE_HOLDS_FRACTION(value))
171 return (
float)gst_value_get_fraction_numerator(value)
172 / (
float)gst_value_get_fraction_denominator(value);
177 if (!GST_VALUE_HOLDS_FRACTION_RANGE(value))
181 return QGRange<
float>{ *min.getFraction(), *max.getFraction() };
186 if (!GST_VALUE_HOLDS_INT_RANGE(value))
188 return QGRange<
int>{ gst_value_get_int_range_min(value), gst_value_get_int_range_max(value) };
193 if (!value || !GST_VALUE_HOLDS_STRUCTURE(value))
194 return QGstStructureView(
nullptr);
200 if (!value || !GST_VALUE_HOLDS_CAPS(value))
202 return QGstCaps(gst_caps_copy(gst_value_get_caps(value)), QGstCaps::HasRef);
207 return value && GST_VALUE_HOLDS_LIST(value);
212 return gst_value_list_get_size(value);
217 return QGValue{ gst_value_list_get_value(value, index) };
231 return QUniqueGstStructureHandle{ gst_structure_copy(structure) };
241 return gst_structure_get_name(structure);
246 return QGValue{ gst_structure_get_value(structure, fieldname) };
260 QGstTagListHandle tagList;
261 gst_structure_get(structure,
"tags", GST_TYPE_TAG_LIST, &tagList,
nullptr);
270 if (structure && gst_structure_get_int(structure,
"width", &w)
271 && gst_structure_get_int(structure,
"height", &h)) {
281 QList<QVideoFrameFormat::PixelFormat> pixelFormats;
286 auto appendFromGstVideoFormat = [&](GstVideoFormat format) {
287 const QVideoFrameFormat::PixelFormat pixelFormat = qPixelFormatFromGstVideoFormat(format);
288 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
291 if (!pixelFormats.contains(pixelFormat))
292 pixelFormats.append(pixelFormat);
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))
302 const char *drmFormatString = g_value_get_string(value);
303 if (!drmFormatString)
306 guint64 modifier = 0;
307 const guint32 fourcc = gst_video_dma_drm_fourcc_from_string(drmFormatString,
311 qWarning() <<
"Ignoring drm-format modifier when mapping to Qt PixelFormat:"
313 appendFromGstVideoFormat(gst_video_dma_drm_fourcc_to_format(fourcc));
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));
322 parseDrmFormat(drmFormatValue);
325 if (!pixelFormats.isEmpty())
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))
333 const char *formatString = g_value_get_string(value);
336 appendFromGstVideoFormat(gst_video_format_from_string(formatString));
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));
344 parseFormat(formatValue);
347 }
else if (gst_structure_has_name(structure,
"image/jpeg")) {
348 pixelFormats.append(QVideoFrameFormat::Format_Jpeg);
359 std::optional<
float> minRate;
360 std::optional<
float> maxRate;
362 auto extractFraction = [](
const GValue *v) ->
float {
363 return (
float)gst_value_get_fraction_numerator(v)
364 / (
float)gst_value_get_fraction_denominator(v);
366 auto extractFrameRate = [&](
const GValue *v) {
367 auto insert = [&](
float min,
float max) {
368 if (!maxRate || max > maxRate)
370 if (!minRate || min < minRate)
374 if (GST_VALUE_HOLDS_FRACTION(v)) {
375 float rate = extractFraction(v);
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));
384 const GValue *gstFrameRates = gst_structure_get_value(structure,
"framerate");
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));
392 extractFrameRate(gstFrameRates);
395 const GValue *min = gst_structure_get_value(structure,
"min-framerate");
396 const GValue *max = gst_structure_get_value(structure,
"max-framerate");
398 minRate = extractFraction(min);
399 maxRate = extractFraction(max);
403 if (!minRate || !maxRate)
407 minRate.value_or(*maxRate),
408 maxRate.value_or(*minRate),
417 const GValue *width = gst_structure_get_value(structure,
"width");
418 const GValue *height = gst_structure_get_value(structure,
"height");
420 if (!width || !height)
423 for (
const GValue *v : { width, height })
424 if (!GST_VALUE_HOLDS_INT_RANGE(v))
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);
432 return QGRange<QSize>{
433 QSize(minWidth, minHeight),
434 QSize(maxWidth, maxHeight),
440 GstMessage *message =
nullptr;
441 gst_structure_get(structure,
"message", GST_TYPE_MESSAGE, &message,
nullptr);
442 return QGstreamerMessage(message, QGstreamerMessage::HasRef);
449 if (gst_structure_get_fraction(structure,
"pixel-aspect-ratio", &numerator, &denominator)) {
461 QSize size = resolution();
462 if (!size.isValid()) {
463 qWarning() << Q_FUNC_INFO <<
"invalid resolution when querying nativeSize";
477 GstVideoInfo vidInfo;
478 std::optional<guint64> dmaDrmModifier;
481 GstVideoInfoDmaDrm videoInfoDmaDrm;
482 if (gst_video_info_dma_drm_from_caps(&videoInfoDmaDrm, get())) {
483 dmaDrmModifier = videoInfoDmaDrm.drm_modifier;
485 if (!gst_video_info_dma_drm_to_video_info(&videoInfoDmaDrm, &vidInfo))
490 if (!gst_video_info_from_caps(&vidInfo, get()))
498 const char *capsFeatures)
500 if (!gst_caps_is_writable(get()))
501 *
this = QGstCaps(gst_caps_make_writable(release()), QGstCaps::RefMode::HasRef);
504 if (qstrcmp(capsFeatures, GST_CAPS_FEATURE_MEMORY_DMABUF) == 0)
505 appendDmaDrmPixelFormats(*
this, formats);
509 g_value_init(&list, GST_TYPE_LIST);
511 for (QVideoFrameFormat::PixelFormat format : formats) {
512 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format);
513 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
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);
524 structure.setFractionRange(
"framerate", Fraction{ 0, 1 }, Fraction{ INT_MAX, 1 });
527 structure.setValue(
"format", &list);
529 gst_caps_append_structure(get(), structure.release());
530 g_value_unset(&list);
533 gst_caps_set_features(get(),
size() - 1,
534 gst_caps_features_from_string(capsFeatures));
539 Q_ASSERT(resolution.isValid());
541 g_value_init(&width, G_TYPE_INT);
542 g_value_set_int(&width, resolution.width());
544 g_value_init(&height, G_TYPE_INT);
545 g_value_set_int(&height, resolution.height());
547 gst_caps_set_value(caps(),
"width", &width);
548 gst_caps_set_value(caps(),
"height", &height);
553 QSize size = format.resolution();
556 if (format.pixelFormat() == QVideoFrameFormat::Format_Jpeg) {
559 jpegStructure
.setInt("height", size.height()
);
561 gst_caps_append_structure(caps.get(), jpegStructure.release());
565 const GstVideoFormat gstFormat = qGstVideoFormatFromPixelFormat(format.pixelFormat());
566 if (gstFormat == GST_VIDEO_FORMAT_UNKNOWN)
570 rawStructure
.setString("format", gst_video_format_to_string(gstFormat)
);
574 gst_caps_append_structure(caps.get(), rawStructure.release());
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());
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));
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());
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));
611 gst_caps_copy(caps()),
618 auto *features = gst_caps_get_features(get(), 0);
619 if (gst_caps_features_contains(features,
"memory:GLMemory"))
621 if (gst_caps_features_contains(features,
"memory:DMABuf"))
628 return int(gst_caps_get_size(get()));
634 gst_caps_get_structure(get(), index),
645 return QGstCaps(gst_caps_new_empty(), HasRef);
652 g_object_set(get(), property, str,
nullptr);
657 g_object_set(get(), property, gboolean(b),
nullptr);
662 g_object_set(get(), property, guint(i),
nullptr);
667 g_object_set(get(), property, gint(i),
nullptr);
672 g_object_set(get(), property, gint64(i),
nullptr);
677 g_object_set(get(), property, guint64(i),
nullptr);
682 g_object_set(get(), property, gdouble(d),
nullptr);
687 g_object_set(get(), property, o.object(),
nullptr);
692 g_object_set(get(), property, c.caps(),
nullptr);
695void QGstObject::
set(
const char *property,
void *object, GDestroyNotify destroyFunction)
697 g_object_set_data_full(qGstCheckedCast<GObject>(get()), property, object, destroyFunction);
703 g_object_get(get(), property, &s,
nullptr);
709 GstStructure *s =
nullptr;
710 g_object_get(get(), property, &s,
nullptr);
717 g_object_get(get(), property, &b,
nullptr);
724 g_object_get(get(), property, &i,
nullptr);
731 g_object_get(get(), property, &i,
nullptr);
738 g_object_get(get(), property, &i,
nullptr);
745 g_object_get(get(), property, &i,
nullptr);
752 g_object_get(get(), property, &d,
nullptr);
759 g_object_get(get(), property, &d,
nullptr);
765 GstObject *o =
nullptr;
766 g_object_get(get(), property, &o,
nullptr);
767 return QGstObject(o, HasRef);
772 return g_object_get_data(qGstCheckedCast<GObject>(get()), property);
780 g_signal_connect(get(), name, callback, userData),
786 g_signal_handler_disconnect(get(), handlerId);
791 return G_OBJECT_TYPE(get());
796 return QLatin1StringView{
808 using namespace Qt::StringLiterals;
810 return get() ? QLatin1StringView{ GST_OBJECT_NAME(get()) } :
"(null)"_L1;
825 object.disconnect(handlerId);
827 handlerId = invalidHandlerId;
842 connection.disconnect();
847 connection.disconnect();
870 return QGstCaps(gst_pad_get_current_caps(pad()), QGstCaps::HasRef);
875 return QGstCaps(gst_pad_query_caps(pad(),
nullptr), QGstCaps::HasRef);
880 QGstTagListHandle tagList;
881 g_object_get(object(),
"tags", &tagList,
nullptr);
888 gst_pad_get_stream_id(pad()),
894 using namespace Qt::Literals;
895 using TrackType = QPlatformMediaPlayer::TrackType;
897 QLatin1StringView padName = name();
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;
911 return gst_pad_is_linked(pad());
916 return gst_pad_link(pad(), sink.pad()) == GST_PAD_LINK_OK;
921 return gst_pad_unlink(pad(), sink.pad());
935 return QGstPad(gst_pad_get_peer(pad()), HasRef);
940 return QGstElement(gst_pad_get_parent_element(pad()), HasRef);
945 return qGstCheckedCast<GstPad>(object());
950 return gst_pad_get_sticky_event(pad(), type, 0);
955 return gst_pad_send_event(pad(), event);
960 GstEvent *flushStart = gst_event_new_flush_start();
961 gboolean ret = sendEvent(flushStart);
963 qWarning(
"failed to send flush-start event");
967 GstEvent *flushStop = gst_event_new_flush_stop(resetTime);
968 ret = sendEvent(flushStop);
970 qWarning(
"failed to send flush-stop event");
975 using namespace std::chrono_literals;
977 GstState state =
parent().state(1s);
979 if (state != GST_STATE_PAUSED)
1005 return qGstCheckedCast<GstClock>(object());
1010 return gst_clock_get_time(clock());
1025 GstElement *element = gst_element_factory_make(factory, name);
1029 qWarning() <<
"Failed to make element" << name <<
"from factory" << factory;
1030 return QGstElement{};
1043 gst_element_factory_create(factory, name),
1056 return createFromDevice(device.get(), name);
1062 gst_device_create_element(device, name),
1063 QGstElement::NeedsRef,
1069 QUniqueGErrorHandle error;
1071 gst_parse_launch(str, &error),
1072 QGstElement::NeedsRef,
1076 qWarning() <<
"gst_parse_launch error:" << error;
1088 return QGstElementFactoryHandle{
1089 gst_element_factory_find(name),
1090 QGstElementFactoryHandle::HasRef,
1094QGstElementFactoryHandle
QGstElement::findFactory(
const QByteArray &name)
1096 return findFactory(name.constData());
1101 GstElementFactory *factory = gst_element_get_factory(element());
1105 const char *name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1106 return name ? QByteArrayView{name} : QByteArrayView{};
1111 return QGstPad(gst_element_get_static_pad(element(), name), HasRef);
1126 return QGstPad(gst_element_request_pad_simple(element(), name), HasRef);
1131 return gst_element_release_request_pad(element(), pad.pad());
1136 using namespace std::chrono_literals;
1139 GstStateChangeReturn change =
1140 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1142 if (Q_UNLIKELY(change == GST_STATE_CHANGE_ASYNC))
1143 qWarning() <<
"QGstElement::state detected an asynchronous state change. Return value not "
1151 return gst_element_set_state(element(), state);
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());
1160 if (change != GST_STATE_CHANGE_SUCCESS && change != GST_STATE_CHANGE_NO_PREROLL) {
1161 qWarning() <<
"Could not change state of" << name() <<
"to" << state << change;
1164 return change == GST_STATE_CHANGE_SUCCESS || change == GST_STATE_CHANGE_NO_PREROLL;
1169 Q_ASSERT(element());
1170 return gst_element_sync_state_with_parent(element()) == TRUE;
1175 GstState state, pending;
1176 GstStateChangeReturn change =
1177 gst_element_get_state(element(), &state, &pending, timeout.count());
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;
1183 return change == GST_STATE_CHANGE_SUCCESS;
1189 GstStateChangeReturn change =
1190 gst_element_get_state(element(), &state,
nullptr, timeout.count());
1191 return change == GST_STATE_CHANGE_ASYNC;
1194bool QGstElement::waitForAsyncStateChangeComplete(
std::chrono::nanoseconds timeout)
const
1196 using namespace std::chrono_literals;
1198 if (!hasAsyncStateChange())
1203 std::this_thread::sleep_for(10ms);
1209 gst_element_set_locked_state(element(), locked);
1214 return gst_element_is_locked_state(element());
1219 gst_element_send_event(element(), event);
1224 sendEvent(gst_event_new_eos());
1230 if (!gst_element_query_duration(element(), GST_FORMAT_TIME, &d)) {
1231 qDebug() <<
"QGstElement: failed to query duration";
1232 return std::nullopt;
1234 return std::chrono::nanoseconds{ d };
1239 using namespace std::chrono;
1240 auto dur = duration();
1242 return round<milliseconds>(*dur);
1243 return std::nullopt;
1248 QGstQueryHandle &query = positionQuery();
1251 if (gst_element_query(element(), query.get())) {
1252 gst_query_parse_position(query.get(),
nullptr, &pos);
1253 return std::chrono::nanoseconds{ pos };
1256 qDebug() <<
"QGstElement: failed to query position";
1257 return std::nullopt;
1262 using namespace std::chrono;
1263 auto pos = position();
1265 return round<milliseconds>(*pos);
1266 return std::nullopt;
1271 QGstQueryHandle query{
1272 gst_query_new_seeking(GST_FORMAT_TIME),
1273 QGstQueryHandle::HasRef,
1275 gboolean canSeek =
false;
1276 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1278 if (gst_element_query(element(), query.get())) {
1279 gst_query_parse_seeking(query.get(),
nullptr, &canSeek,
nullptr,
nullptr);
1282 return std::nullopt;
1287 return gst_element_get_base_time(element());
1292 gst_element_set_base_time(element(), time);
1297 return GST_ELEMENT_CAST(get());
1303 qGstCheckedCast<GstElement>(gst_element_get_parent(object())),
1304 QGstElement::HasRef,
1311 qGstCheckedCast<GstBin>(gst_element_get_parent(object())),
1312 QGstElement::HasRef,
1321 if (greatAncestor) {
1322 ancestor =
std::move(greatAncestor);
1325 if (GST_IS_BIN(ancestor.element())) {
1327 qGstSafeCast<GstBin>(ancestor.element()),
1339 if (GST_IS_PIPELINE(rootBin.get())) {
1340 return QGstPipeline{
1341 qGstSafeCast<GstPipeline>(rootBin.element()),
1342 QGstPipeline::NeedsRef,
1345 qWarning() <<
"QGstElement::getPipeline failed for element:" << *
this;
1346 return QGstPipeline{};
1352 if (QGstBin parent = getParentBin())
1353 parent.remove(*
this);
1358 static const bool dumpEnabled = qEnvironmentVariableIsSet(
"GST_DEBUG_DUMP_DOT_DIR");
1364QGstQueryHandle &
QGstElement::positionQuery()
const
1366 if (Q_UNLIKELY(!m_positionQuery))
1367 m_positionQuery = QGstQueryHandle{
1368 gst_query_new_position(GST_FORMAT_TIME),
1369 QGstQueryHandle::HasRef,
1372 return m_positionQuery;
1379 return QGstBin(gst_bin_new(name), NeedsRef);
1385 Q_ASSERT(GST_IS_BIN(element.element()));
1387 GST_BIN(element.release()),
1393 const char *name,
bool ghostUnlinkedPads)
1399 bool ghostUnlinkedPads)
1401 QUniqueGErrorHandle error;
1403 GstElement *element =
1404 gst_parse_bin_from_description_full(pipelineDescription, ghostUnlinkedPads,
1405 nullptr, GST_PARSE_FLAG_NONE, &error);
1408 qWarning() <<
"Failed to make element from pipeline description" << pipelineDescription
1414 gst_element_set_name(element, name);
1432 return qGstCheckedCast<GstBin>(object());
1442 gst_element_add_pad(element(), gst_ghost_pad_new(name, pad.pad()));
1447 Q_ASSERT(direction != GstPadDirection::GST_PAD_UNKNOWN);
1451 gst_bin_find_unlinked_pad(bin(), direction),
1458 addGhostPad(unlinkedPad.name().constData(), unlinkedPad);
1464 return gst_bin_sync_children_states(bin());
1472 if (includeTimestamp)
1473 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1475 GST_DEBUG_BIN_TO_DOT_FILE(bin(), GST_DEBUG_GRAPH_SHOW_VERBOSE, fileNamePrefix);
1481 gst_bin_get_by_name(bin(), name),
1482 QGstElement::NeedsRef,
1488 gst_bin_recalculate_latency(bin());
1503 gst_base_sink_set_sync(baseSink(), arg ? TRUE : FALSE);
1508 return qGstCheckedCast<GstBaseSink>(element());
1523 return qGstCheckedCast<GstBaseSrc>(element());
1540 qGstCheckedCast<GstAppSink>(created.element()),
1541 QGstAppSink::NeedsRef,
1547 return qGstCheckedCast<GstAppSink>(element());
1550# if GST_CHECK_VERSION(1
, 24
, 0
)
1551void QGstAppSink::setMaxBufferTime(std::chrono::nanoseconds ns)
1553 gst_app_sink_set_max_time(appSink(), qGstClockTimeFromChrono(ns));
1559 gst_app_sink_set_max_buffers(appSink(), n);
1564 gst_app_sink_set_caps(appSink(), caps.caps());
1568 GDestroyNotify notify)
1570 gst_app_sink_set_callbacks(appSink(), &callbacks, user_data, notify);
1575 return QGstSampleHandle{
1576 gst_app_sink_pull_sample(appSink()),
1577 QGstSampleHandle::HasRef,
1595 qGstCheckedCast<GstAppSrc>(created.element()),
1596 QGstAppSrc::NeedsRef,
1602 return qGstCheckedCast<GstAppSrc>(element());
1606 GDestroyNotify notify)
1608 gst_app_src_set_callbacks(appSrc(), &callbacks, user_data, notify);
1613 return gst_app_src_push_buffer(appSrc(), buffer);
1618 return QStringLiteral(
"Could not find the %1 GStreamer element")
1619 .arg(QLatin1StringView(element));
1624 auto &vidInfo = qtVideoInfo.gstVideoInfo;
1625 GstVideoFormat gstFormat = GST_VIDEO_INFO_FORMAT(&vidInfo);
1627 auto pixelFormat = qPixelFormatFromGstVideoFormat(gstFormat);
1628 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
1629 return QVideoFrameFormat();
1631 QVideoFrameFormat format(QSize(vidInfo.width, vidInfo.height), pixelFormat);
1633 if (vidInfo.fps_d > 0)
1634 format.setStreamFrameRate(qreal(vidInfo.fps_n) / vidInfo.fps_d);
1636 QVideoFrameFormat::ColorRange range = QVideoFrameFormat::ColorRange_Unknown;
1637 switch (vidInfo.colorimetry.range) {
1638 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
1640 case GST_VIDEO_COLOR_RANGE_0_255:
1641 range = QVideoFrameFormat::ColorRange_Full;
1643 case GST_VIDEO_COLOR_RANGE_16_235:
1644 range = QVideoFrameFormat::ColorRange_Video;
1647 format.setColorRange(range);
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:
1655 case GST_VIDEO_COLOR_MATRIX_BT709:
1656 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
1658 case GST_VIDEO_COLOR_MATRIX_BT601:
1659 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
1661 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
1662 colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
1664 case GST_VIDEO_COLOR_MATRIX_BT2020:
1665 colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
1668 format.setColorSpace(colorSpace);
1670 QVideoFrameFormat::ColorTransfer transfer = QVideoFrameFormat::ColorTransfer_Unknown;
1671 switch (vidInfo.colorimetry.transfer) {
1672 case GST_VIDEO_TRANSFER_UNKNOWN:
1674 case GST_VIDEO_TRANSFER_GAMMA10:
1675 transfer = QVideoFrameFormat::ColorTransfer_Linear;
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;
1683 case GST_VIDEO_TRANSFER_GAMMA18:
1684 case GST_VIDEO_TRANSFER_GAMMA20:
1686 case GST_VIDEO_TRANSFER_BT709:
1687 case GST_VIDEO_TRANSFER_BT2020_12:
1688 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1690 case GST_VIDEO_TRANSFER_GAMMA28:
1691 transfer = QVideoFrameFormat::ColorTransfer_Gamma28;
1693 case GST_VIDEO_TRANSFER_LOG100:
1694 case GST_VIDEO_TRANSFER_LOG316:
1696 case GST_VIDEO_TRANSFER_SMPTE2084:
1697 transfer = QVideoFrameFormat::ColorTransfer_ST2084;
1699 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
1700 transfer = QVideoFrameFormat::ColorTransfer_STD_B67;
1702 case GST_VIDEO_TRANSFER_BT2020_10:
1703 transfer = QVideoFrameFormat::ColorTransfer_BT709;
1705 case GST_VIDEO_TRANSFER_BT601:
1706 transfer = QVideoFrameFormat::ColorTransfer_BT601;
1709 format.setColorTransfer(transfer);
1718 [[maybe_unused]] GstMemory *mem = gst_buffer_peek_memory(buffer, 0);
1720#if QT_CONFIG(gstreamer_gl_egl) && QT_CONFIG(linux_dmabuf)
1721 if (gst_is_dmabuf_memory(mem))
1722 return QGstCaps::DMABuf;
1724#if QT_CONFIG(gstreamer_gl)
1725 if (gst_is_gl_memory(mem))
1726 return QGstCaps::GLTexture;
1730#if QT_CONFIG(gpu_vivante)
1731 if (gst_is_phys_memory(mem))
1732 return QGstCaps::VivantePhysMemory;
1738 const QVideoFrameFormat &format)
1740#if QT_CONFIG(linux_dmabuf)
1741 if (
auto dmaBufBuffer = QGstDmaBufVideoBuffer::create(buffer, videoInfo, format))
1742 return QVideoFramePrivate::createFrame(std::move(dmaBufBuffer), format);
1744 auto videoBuffer =
std::make_unique<QGstVideoBuffer>(
std::move(buffer), videoInfo, format);
1745 return QVideoFramePrivate::createFrame(std::move(videoBuffer), format);
1750 auto format = qVideoFrameFormatFromGstVideoInfo(videoInfo);
1751 return qCreateFrameFromGstBuffer(std::move(buffer), videoInfo, 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)
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)
QVideoFrame qCreateFrameFromGstBuffer(QGstBufferHandle buffer, const QGstVideoInfo &videoInfo, const QVideoFrameFormat &format)
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)
typedefQT_BEGIN_NAMESPACE struct _GstBuffer GstBuffer
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)