| 1 | // Copyright (C) 2016 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 "qgstreamermetadata_p.h" |
| 5 | #include <QtMultimedia/qmediametadata.h> |
| 6 | #include <QtMultimedia/qtvideo.h> |
| 7 | #include <QtCore/qdebug.h> |
| 8 | #include <QtCore/qdatetime.h> |
| 9 | #include <QtCore/qlocale.h> |
| 10 | #include <QtCore/qtimezone.h> |
| 11 | #include <QtGui/qimage.h> |
| 12 | |
| 13 | #include <gst/gstversion.h> |
| 14 | #include <common/qgst_handle_types_p.h> |
| 15 | #include <common/qgstutils_p.h> |
| 16 | #include <qgstreamerformatinfo_p.h> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | RotationResult parseRotationTag(std::string_view tag) |
| 21 | { |
| 22 | using namespace std::string_view_literals; |
| 23 | Q_ASSERT(!tag.empty()); |
| 24 | |
| 25 | if (tag[0] == 'r') { |
| 26 | if (tag == "rotate-90"sv ) |
| 27 | return { .rotation: QtVideo::Rotation::Clockwise90, .flip: false }; |
| 28 | if (tag == "rotate-180"sv ) |
| 29 | return { .rotation: QtVideo::Rotation::Clockwise180, .flip: false }; |
| 30 | if (tag == "rotate-270"sv ) |
| 31 | return { .rotation: QtVideo::Rotation::Clockwise270, .flip: false }; |
| 32 | if (tag == "rotate-0"sv ) |
| 33 | return { .rotation: QtVideo::Rotation::None, .flip: false }; |
| 34 | } |
| 35 | if (tag[0] == 'f') { |
| 36 | // To flip by horizontal axis is the same as to mirror by vertical axis |
| 37 | // and rotate by 180 degrees. |
| 38 | |
| 39 | if (tag == "flip-rotate-90"sv ) |
| 40 | return { .rotation: QtVideo::Rotation::Clockwise270, .flip: true }; |
| 41 | if (tag == "flip-rotate-180"sv ) |
| 42 | return { .rotation: QtVideo::Rotation::None, .flip: true }; |
| 43 | if (tag == "flip-rotate-270"sv ) |
| 44 | return { .rotation: QtVideo::Rotation::Clockwise90, .flip: true }; |
| 45 | if (tag == "flip-rotate-0"sv ) |
| 46 | return { .rotation: QtVideo::Rotation::Clockwise180, .flip: true }; |
| 47 | } |
| 48 | |
| 49 | qCritical() << "cannot parse orientation: {}" << tag; |
| 50 | return { .rotation: QtVideo::Rotation::None, .flip: false }; |
| 51 | } |
| 52 | |
| 53 | namespace { |
| 54 | |
| 55 | namespace MetadataLookupImpl { |
| 56 | |
| 57 | #ifdef __cpp_lib_constexpr_algorithms |
| 58 | # define constexpr_lookup constexpr |
| 59 | #else |
| 60 | # define constexpr_lookup /*constexpr*/ |
| 61 | #endif |
| 62 | |
| 63 | struct MetadataKeyValuePair |
| 64 | { |
| 65 | const char *tag; |
| 66 | QMediaMetaData::Key key; |
| 67 | }; |
| 68 | |
| 69 | constexpr const char *toTag(const char *t) |
| 70 | { |
| 71 | return t; |
| 72 | } |
| 73 | constexpr const char *toTag(const MetadataKeyValuePair &kv) |
| 74 | { |
| 75 | return kv.tag; |
| 76 | } |
| 77 | |
| 78 | constexpr QMediaMetaData::Key toKey(QMediaMetaData::Key k) |
| 79 | { |
| 80 | return k; |
| 81 | } |
| 82 | constexpr QMediaMetaData::Key toKey(const MetadataKeyValuePair &kv) |
| 83 | { |
| 84 | return kv.key; |
| 85 | } |
| 86 | |
| 87 | constexpr auto compareByKey = [](const auto &lhs, const auto &rhs) { |
| 88 | return toKey(lhs) < toKey(rhs); |
| 89 | }; |
| 90 | |
| 91 | constexpr auto compareByTag = [](const auto &lhs, const auto &rhs) { |
| 92 | return std::strcmp(s1: toTag(lhs), s2: toTag(rhs)) < 0; |
| 93 | }; |
| 94 | |
| 95 | constexpr_lookup auto makeLookupTable() |
| 96 | { |
| 97 | std::array<MetadataKeyValuePair, 22> lookupTable{ ._M_elems: { |
| 98 | { GST_TAG_TITLE, .key: QMediaMetaData::Title }, |
| 99 | { GST_TAG_COMMENT, .key: QMediaMetaData::Comment }, |
| 100 | { GST_TAG_DESCRIPTION, .key: QMediaMetaData::Description }, |
| 101 | { GST_TAG_GENRE, .key: QMediaMetaData::Genre }, |
| 102 | { GST_TAG_DATE_TIME, .key: QMediaMetaData::Date }, |
| 103 | { GST_TAG_DATE, .key: QMediaMetaData::Date }, |
| 104 | |
| 105 | { GST_TAG_LANGUAGE_CODE, .key: QMediaMetaData::Language }, |
| 106 | |
| 107 | { GST_TAG_ORGANIZATION, .key: QMediaMetaData::Publisher }, |
| 108 | { GST_TAG_COPYRIGHT, .key: QMediaMetaData::Copyright }, |
| 109 | |
| 110 | // Media |
| 111 | { GST_TAG_DURATION, .key: QMediaMetaData::Duration }, |
| 112 | |
| 113 | // Audio |
| 114 | { GST_TAG_BITRATE, .key: QMediaMetaData::AudioBitRate }, |
| 115 | { GST_TAG_AUDIO_CODEC, .key: QMediaMetaData::AudioCodec }, |
| 116 | |
| 117 | // Music |
| 118 | { GST_TAG_ALBUM, .key: QMediaMetaData::AlbumTitle }, |
| 119 | { GST_TAG_ALBUM_ARTIST, .key: QMediaMetaData::AlbumArtist }, |
| 120 | { GST_TAG_ARTIST, .key: QMediaMetaData::ContributingArtist }, |
| 121 | { GST_TAG_TRACK_NUMBER, .key: QMediaMetaData::TrackNumber }, |
| 122 | |
| 123 | { GST_TAG_PREVIEW_IMAGE, .key: QMediaMetaData::ThumbnailImage }, |
| 124 | { GST_TAG_IMAGE, .key: QMediaMetaData::CoverArtImage }, |
| 125 | |
| 126 | // Image/Video |
| 127 | { .tag: "resolution" , .key: QMediaMetaData::Resolution }, |
| 128 | { GST_TAG_IMAGE_ORIENTATION, .key: QMediaMetaData::Orientation }, |
| 129 | |
| 130 | // Video |
| 131 | { GST_TAG_VIDEO_CODEC, .key: QMediaMetaData::VideoCodec }, |
| 132 | |
| 133 | // Movie |
| 134 | { GST_TAG_PERFORMER, .key: QMediaMetaData::LeadPerformer }, |
| 135 | } }; |
| 136 | |
| 137 | std::sort(first: lookupTable.begin(), last: lookupTable.end(), |
| 138 | comp: [](const MetadataKeyValuePair &lhs, const MetadataKeyValuePair &rhs) { |
| 139 | return std::string_view(lhs.tag) < std::string_view(rhs.tag); |
| 140 | }); |
| 141 | return lookupTable; |
| 142 | } |
| 143 | |
| 144 | constexpr_lookup auto gstTagToMetaDataKey = makeLookupTable(); |
| 145 | constexpr_lookup auto metaDataKeyToGstTag = [] { |
| 146 | auto array = gstTagToMetaDataKey; |
| 147 | std::sort(first: array.begin(), last: array.end(), comp: compareByKey); |
| 148 | return array; |
| 149 | }(); |
| 150 | |
| 151 | } // namespace MetadataLookupImpl |
| 152 | |
| 153 | QMediaMetaData::Key tagToKey(const char *tag) |
| 154 | { |
| 155 | if (tag == nullptr) |
| 156 | return QMediaMetaData::Key(-1); |
| 157 | |
| 158 | using namespace MetadataLookupImpl; |
| 159 | auto foundIterator = std::lower_bound(first: gstTagToMetaDataKey.begin(), last: gstTagToMetaDataKey.end(), |
| 160 | val: tag, comp: compareByTag); |
| 161 | if (std::strcmp(s1: foundIterator->tag, s2: tag) == 0) |
| 162 | return foundIterator->key; |
| 163 | |
| 164 | return QMediaMetaData::Key(-1); |
| 165 | } |
| 166 | |
| 167 | const char *keyToTag(QMediaMetaData::Key key) |
| 168 | { |
| 169 | using namespace MetadataLookupImpl; |
| 170 | auto foundIterator = std::lower_bound(first: metaDataKeyToGstTag.begin(), last: metaDataKeyToGstTag.end(), |
| 171 | val: key, comp: compareByKey); |
| 172 | if (foundIterator->key == key) |
| 173 | return foundIterator->tag; |
| 174 | |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | #undef constexpr_lookup |
| 179 | |
| 180 | QDateTime parseDate(const GDate *date) |
| 181 | { |
| 182 | if (!g_date_valid(date)) |
| 183 | return {}; |
| 184 | |
| 185 | int year = g_date_get_year(date); |
| 186 | int month = g_date_get_month(date); |
| 187 | int day = g_date_get_day(date); |
| 188 | return QDateTime(QDate(year, month, day), QTime()); |
| 189 | } |
| 190 | |
| 191 | QDateTime parseDate(const GValue &val) |
| 192 | { |
| 193 | Q_ASSERT(G_VALUE_TYPE(&val) == G_TYPE_DATE); |
| 194 | const GDate *date = (const GDate *)g_value_get_boxed(value: &val); |
| 195 | return parseDate(date); |
| 196 | } |
| 197 | |
| 198 | QDateTime parseDateTime(const GstDateTime *dateTime) |
| 199 | { |
| 200 | int year = gst_date_time_has_year(datetime: dateTime) ? gst_date_time_get_year(datetime: dateTime) : 0; |
| 201 | int month = gst_date_time_has_month(datetime: dateTime) ? gst_date_time_get_month(datetime: dateTime) : 0; |
| 202 | int day = gst_date_time_has_day(datetime: dateTime) ? gst_date_time_get_day(datetime: dateTime) : 0; |
| 203 | int hour = 0; |
| 204 | int minute = 0; |
| 205 | int second = 0; |
| 206 | float tz = 0; |
| 207 | if (gst_date_time_has_time(datetime: dateTime)) { |
| 208 | hour = gst_date_time_get_hour(datetime: dateTime); |
| 209 | minute = gst_date_time_get_minute(datetime: dateTime); |
| 210 | second = gst_date_time_get_second(datetime: dateTime); |
| 211 | tz = gst_date_time_get_time_zone_offset(datetime: dateTime); |
| 212 | } |
| 213 | return QDateTime{ |
| 214 | QDate(year, month, day), |
| 215 | QTime(hour, minute, second), |
| 216 | QTimeZone(tz * 60 * 60), |
| 217 | }; |
| 218 | } |
| 219 | |
| 220 | QDateTime parseDateTime(const GValue &val) |
| 221 | { |
| 222 | Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_DATE_TIME); |
| 223 | const GstDateTime *dateTime = (const GstDateTime *)g_value_get_boxed(value: &val); |
| 224 | return parseDateTime(dateTime); |
| 225 | } |
| 226 | |
| 227 | QImage parseImage(const GValue &val) |
| 228 | { |
| 229 | Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_SAMPLE); |
| 230 | |
| 231 | GstSample *sample = (GstSample *)g_value_get_boxed(value: &val); |
| 232 | GstCaps *caps = gst_sample_get_caps(sample); |
| 233 | if (caps && !gst_caps_is_empty(caps)) { |
| 234 | GstStructure *structure = gst_caps_get_structure(caps, index: 0); |
| 235 | const gchar *name = gst_structure_get_name(structure); |
| 236 | if (QByteArray(name).startsWith(bv: "image/" )) { |
| 237 | GstBuffer *buffer = gst_sample_get_buffer(sample); |
| 238 | if (buffer) { |
| 239 | GstMapInfo info; |
| 240 | gst_buffer_map(buffer, info: &info, flags: GST_MAP_READ); |
| 241 | QImage image = QImage::fromData(data: info.data, size: info.size, format: name); |
| 242 | gst_buffer_unmap(buffer, info: &info); |
| 243 | return image; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return {}; |
| 249 | } |
| 250 | |
| 251 | std::optional<double> parseFractionAsDouble(const GValue &val) |
| 252 | { |
| 253 | Q_ASSERT(G_VALUE_TYPE(&val) == GST_TYPE_FRACTION); |
| 254 | |
| 255 | int nom = gst_value_get_fraction_numerator(value: &val); |
| 256 | int denom = gst_value_get_fraction_denominator(value: &val); |
| 257 | if (denom == 0) |
| 258 | return std::nullopt; |
| 259 | return double(nom) / double(denom); |
| 260 | } |
| 261 | |
| 262 | constexpr std::string_view { GST_TAG_EXTENDED_COMMENT }; |
| 263 | |
| 264 | void (const GstTagList *list, const gchar *tag, QMediaMetaData &metadata) |
| 265 | { |
| 266 | using namespace Qt::Literals; |
| 267 | assert(tag == extendedComment); |
| 268 | |
| 269 | int entryCount = gst_tag_list_get_tag_size(list, tag); |
| 270 | for (int i = 0; i != entryCount; ++i) { |
| 271 | const GValue *value = gst_tag_list_get_value_index(list, tag, index: i); |
| 272 | |
| 273 | const QLatin1StringView strValue{ g_value_get_string(value) }; |
| 274 | |
| 275 | auto equalIndex = strValue.indexOf(s: QLatin1StringView("=" )); |
| 276 | if (equalIndex == -1) { |
| 277 | qDebug() << "Cannot parse GST_TAG_EXTENDED_COMMENT entry: " << value; |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | const QLatin1StringView key = strValue.first(n: equalIndex); |
| 282 | const QLatin1StringView valueString = strValue.last(n: strValue.size() - equalIndex - 1); |
| 283 | |
| 284 | if (key == "DURATION"_L1 ) { |
| 285 | QGstDateTimeHandle duration{ |
| 286 | gst_date_time_new_from_iso8601_string(string: valueString.data()), |
| 287 | QGstDateTimeHandle::HasRef, |
| 288 | }; |
| 289 | |
| 290 | if (duration) { |
| 291 | using namespace std::chrono; |
| 292 | |
| 293 | auto chronoDuration = hours(gst_date_time_get_hour(datetime: duration.get())) |
| 294 | + minutes(gst_date_time_get_minute(datetime: duration.get())) |
| 295 | + seconds(gst_date_time_get_second(datetime: duration.get())) |
| 296 | + microseconds(gst_date_time_get_microsecond(datetime: duration.get())); |
| 297 | |
| 298 | metadata.insert(k: QMediaMetaData::Duration, |
| 299 | value: QVariant::fromValue(value: round<milliseconds>(d: chronoDuration).count())); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void addTagToMetaData(const GstTagList *list, const gchar *tag, void *userdata) |
| 306 | { |
| 307 | QMediaMetaData &metadata = *reinterpret_cast<QMediaMetaData *>(userdata); |
| 308 | |
| 309 | QMediaMetaData::Key key = tagToKey(tag); |
| 310 | if (key == QMediaMetaData::Key::Date) |
| 311 | return; // date/datetime are handled on a higher layer |
| 312 | |
| 313 | if (key == QMediaMetaData::Key(-1)) { |
| 314 | if (tag == extendedComment) |
| 315 | addTagsFromExtendedComment(list, tag, metadata); |
| 316 | |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | GValue val{}; |
| 321 | gst_tag_list_copy_value(dest: &val, list, tag); |
| 322 | |
| 323 | GType type = G_VALUE_TYPE(&val); |
| 324 | |
| 325 | if (auto entryCount = gst_tag_list_get_tag_size(list, tag) != 0; entryCount != 1) |
| 326 | qWarning() << "addTagToMetaData: invaled entry count for" << tag << "-" << entryCount; |
| 327 | |
| 328 | if (type == G_TYPE_STRING) { |
| 329 | const gchar *str_value = g_value_get_string(value: &val); |
| 330 | |
| 331 | switch (key) { |
| 332 | case QMediaMetaData::Language: { |
| 333 | metadata.insert(k: key, value: QVariant::fromValue(value: QGstUtils::codeToLanguage(str_value))); |
| 334 | break; |
| 335 | } |
| 336 | case QMediaMetaData::Orientation: { |
| 337 | RotationResult result = parseRotationTag(tag: str_value); |
| 338 | metadata.insert(k: key, value: QVariant::fromValue(value: result.rotation)); |
| 339 | break; |
| 340 | } |
| 341 | default: |
| 342 | metadata.insert(k: key, value: QString::fromUtf8(utf8: str_value)); |
| 343 | break; |
| 344 | }; |
| 345 | } else if (type == G_TYPE_INT) { |
| 346 | metadata.insert(k: key, value: g_value_get_int(value: &val)); |
| 347 | } else if (type == G_TYPE_UINT) { |
| 348 | metadata.insert(k: key, value: g_value_get_uint(value: &val)); |
| 349 | } else if (type == G_TYPE_LONG) { |
| 350 | metadata.insert(k: key, value: qint64(g_value_get_long(value: &val))); |
| 351 | } else if (type == G_TYPE_BOOLEAN) { |
| 352 | metadata.insert(k: key, value: g_value_get_boolean(value: &val)); |
| 353 | } else if (type == G_TYPE_CHAR) { |
| 354 | metadata.insert(k: key, value: g_value_get_schar(value: &val)); |
| 355 | } else if (type == G_TYPE_DOUBLE) { |
| 356 | metadata.insert(k: key, value: g_value_get_double(value: &val)); |
| 357 | } else if (type == G_TYPE_DATE) { |
| 358 | if (!metadata.keys().contains(t: key)) { |
| 359 | QDateTime date = parseDate(val); |
| 360 | if (date.isValid()) |
| 361 | metadata.insert(k: key, value: date); |
| 362 | } |
| 363 | } else if (type == GST_TYPE_DATE_TIME) { |
| 364 | QDateTime date = parseDateTime(val); |
| 365 | if (date.isValid()) |
| 366 | metadata.insert(k: key, value: parseDateTime(val)); |
| 367 | } else if (type == GST_TYPE_SAMPLE) { |
| 368 | QImage image = parseImage(val); |
| 369 | if (!image.isNull()) |
| 370 | metadata.insert(k: key, value: image); |
| 371 | } else if (type == GST_TYPE_FRACTION) { |
| 372 | std::optional<double> fraction = parseFractionAsDouble(val); |
| 373 | |
| 374 | if (fraction) |
| 375 | metadata.insert(k: key, value: *fraction); |
| 376 | } |
| 377 | |
| 378 | g_value_unset(value: &val); |
| 379 | } |
| 380 | |
| 381 | } // namespace |
| 382 | |
| 383 | QMediaMetaData taglistToMetaData(const QGstTagListHandle &handle) |
| 384 | { |
| 385 | QMediaMetaData m; |
| 386 | extendMetaDataFromTagList(m, handle); |
| 387 | return m; |
| 388 | } |
| 389 | |
| 390 | void extendMetaDataFromTagList(QMediaMetaData &metadata, const QGstTagListHandle &handle) |
| 391 | { |
| 392 | if (handle) { |
| 393 | // gstreamer has both GST_TAG_DATE_TIME and GST_TAG_DATE tags. |
| 394 | // if both are present, we use GST_TAG_DATE_TIME, else we fall back to GST_TAG_DATE |
| 395 | |
| 396 | auto readDateTime = [&]() -> std::optional<QDateTime> { |
| 397 | GstDateTime *dateTimeHandle{}; |
| 398 | gst_tag_list_get_date_time(list: handle.get(), GST_TAG_DATE_TIME, value: &dateTimeHandle); |
| 399 | if (dateTimeHandle) { |
| 400 | QDateTime ret = parseDateTime(dateTime: dateTimeHandle); |
| 401 | gst_date_time_unref(datetime: dateTimeHandle); |
| 402 | if (ret.isValid()) |
| 403 | return ret; |
| 404 | } |
| 405 | return std::nullopt; |
| 406 | }; |
| 407 | |
| 408 | auto readDate = [&]() -> std::optional<QDateTime> { |
| 409 | GDate *dateHandle{}; |
| 410 | gst_tag_list_get_date(list: handle.get(), GST_TAG_DATE, value: &dateHandle); |
| 411 | if (dateHandle) { |
| 412 | QDateTime ret = parseDate(date: dateHandle); |
| 413 | g_date_free(date: dateHandle); |
| 414 | if (ret.isValid()) |
| 415 | return ret; |
| 416 | } |
| 417 | return std::nullopt; |
| 418 | }; |
| 419 | |
| 420 | std::optional<QDateTime> date = readDateTime(); |
| 421 | if (!date) |
| 422 | date = readDate(); |
| 423 | |
| 424 | if (date) |
| 425 | metadata.insert(k: QMediaMetaData::Key::Date, value: *date); |
| 426 | |
| 427 | gst_tag_list_foreach(list: handle.get(), func: reinterpret_cast<GstTagForeachFunc>(&addTagToMetaData), |
| 428 | user_data: &metadata); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static void applyMetaDataToTagSetter(const QMediaMetaData &metadata, GstTagSetter *element) |
| 433 | { |
| 434 | gst_tag_setter_reset_tags(setter: element); |
| 435 | |
| 436 | for (QMediaMetaData::Key key : metadata.keys()) { |
| 437 | const char *tagName = keyToTag(key); |
| 438 | if (!tagName) |
| 439 | continue; |
| 440 | const QVariant &tagValue = metadata.value(k: key); |
| 441 | |
| 442 | auto setTag = [&](const auto &value) { |
| 443 | gst_tag_setter_add_tags(element, GST_TAG_MERGE_REPLACE, tagName, value, nullptr); |
| 444 | }; |
| 445 | |
| 446 | switch (tagValue.typeId()) { |
| 447 | case QMetaType::QString: |
| 448 | setTag(tagValue.toString().toUtf8().constData()); |
| 449 | break; |
| 450 | case QMetaType::Int: |
| 451 | case QMetaType::LongLong: |
| 452 | setTag(tagValue.toInt()); |
| 453 | break; |
| 454 | case QMetaType::Double: |
| 455 | setTag(tagValue.toDouble()); |
| 456 | break; |
| 457 | |
| 458 | case QMetaType::QDateTime: { |
| 459 | // tagName does not properly disambiguate between GST_TAG_DATE_TIME and |
| 460 | // GST_TAG_DATE, as both map to QMediaMetaData::Key::Date. so we set it accordingly to |
| 461 | // the QVariant. |
| 462 | |
| 463 | QDateTime date = tagValue.toDateTime(); |
| 464 | |
| 465 | QGstGstDateTimeHandle dateTime{ |
| 466 | gst_date_time_new(tzoffset: date.offsetFromUtc() / 60. / 60., year: date.date().year(), |
| 467 | month: date.date().month(), day: date.date().day(), hour: date.time().hour(), |
| 468 | minute: date.time().minute(), seconds: date.time().second()), |
| 469 | QGstGstDateTimeHandle::HasRef, |
| 470 | }; |
| 471 | |
| 472 | gst_tag_setter_add_tags(setter: element, mode: GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME, |
| 473 | dateTime.get(), nullptr); |
| 474 | break; |
| 475 | } |
| 476 | case QMetaType::QDate: { |
| 477 | QDate date = tagValue.toDate(); |
| 478 | |
| 479 | QUniqueGDateHandle dateHandle{ |
| 480 | g_date_new_dmy(day: date.day(), month: GDateMonth(date.month()), year: date.year()), |
| 481 | }; |
| 482 | |
| 483 | gst_tag_setter_add_tags(setter: element, mode: GST_TAG_MERGE_REPLACE, GST_TAG_DATE, dateHandle.get(), |
| 484 | nullptr); |
| 485 | break; |
| 486 | } |
| 487 | default: { |
| 488 | if (tagValue.typeId() == qMetaTypeId<QLocale::Language>()) { |
| 489 | QByteArray language = QLocale::languageToCode(language: tagValue.value<QLocale::Language>(), |
| 490 | codeTypes: QLocale::ISO639Part2) |
| 491 | .toUtf8(); |
| 492 | setTag(language.constData()); |
| 493 | } |
| 494 | |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstElement &element) |
| 502 | { |
| 503 | GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(arg: element.element()); |
| 504 | if (tagSetter) |
| 505 | applyMetaDataToTagSetter(metadata, element: tagSetter); |
| 506 | else |
| 507 | qWarning() << "applyMetaDataToTagSetter failed: element not a GstTagSetter" |
| 508 | << element.name(); |
| 509 | } |
| 510 | |
| 511 | void applyMetaDataToTagSetter(const QMediaMetaData &metadata, const QGstBin &bin) |
| 512 | { |
| 513 | GstIterator *elements = gst_bin_iterate_all_by_interface(bin: bin.bin(), GST_TYPE_TAG_SETTER); |
| 514 | GValue item = {}; |
| 515 | |
| 516 | while (gst_iterator_next(it: elements, elem: &item) == GST_ITERATOR_OK) { |
| 517 | GstElement *element = static_cast<GstElement *>(g_value_get_object(value: &item)); |
| 518 | if (!element) |
| 519 | continue; |
| 520 | |
| 521 | GstTagSetter *tagSetter = qGstSafeCast<GstTagSetter>(arg: element); |
| 522 | |
| 523 | if (tagSetter) |
| 524 | applyMetaDataToTagSetter(metadata, element: tagSetter); |
| 525 | } |
| 526 | |
| 527 | gst_iterator_free(it: elements); |
| 528 | } |
| 529 | |
| 530 | void extendMetaDataFromCaps(QMediaMetaData &metadata, const QGstCaps &caps) |
| 531 | { |
| 532 | QGstStructureView structure = caps.at(index: 0); |
| 533 | |
| 534 | QMediaFormat::FileFormat fileFormat = QGstreamerFormatInfo::fileFormatForCaps(structure); |
| 535 | if (fileFormat != QMediaFormat::FileFormat::UnspecifiedFormat) { |
| 536 | // Container caps |
| 537 | metadata.insert(k: QMediaMetaData::FileFormat, value: fileFormat); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | QMediaFormat::AudioCodec audioCodec = QGstreamerFormatInfo::audioCodecForCaps(structure); |
| 542 | if (audioCodec != QMediaFormat::AudioCodec::Unspecified) { |
| 543 | // Audio stream caps |
| 544 | metadata.insert(k: QMediaMetaData::AudioCodec, value: QVariant::fromValue(value: audioCodec)); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | QMediaFormat::VideoCodec videoCodec = QGstreamerFormatInfo::videoCodecForCaps(structure); |
| 549 | if (videoCodec != QMediaFormat::VideoCodec::Unspecified) { |
| 550 | // Video stream caps |
| 551 | metadata.insert(k: QMediaMetaData::VideoCodec, value: QVariant::fromValue(value: videoCodec)); |
| 552 | std::optional<float> framerate = structure["framerate" ].getFraction(); |
| 553 | if (framerate) |
| 554 | metadata.insert(k: QMediaMetaData::VideoFrameRate, value: *framerate); |
| 555 | |
| 556 | QSize resolution = structure.resolution(); |
| 557 | if (resolution.isValid()) |
| 558 | metadata.insert(k: QMediaMetaData::Resolution, value: resolution); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | QMediaMetaData capsToMetaData(const QGstCaps &caps) |
| 563 | { |
| 564 | QMediaMetaData metadata; |
| 565 | extendMetaDataFromCaps(metadata, caps); |
| 566 | return metadata; |
| 567 | } |
| 568 | |
| 569 | QT_END_NAMESPACE |
| 570 | |