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