From 773eb62dd0667905a167252b0da0e45ae54e19d1 Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Mon, 25 Nov 2024 17:08:21 +0100 Subject: [PATCH] [HLSTree] Add support to read subtitle codec --- src/parser/HLSTree.cpp | 38 +++++++++++++++++++++++++++++++++++--- src/parser/HLSTree.h | 9 +++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/parser/HLSTree.cpp b/src/parser/HLSTree.cpp index 808fc2060..12fe178fd 100644 --- a/src/parser/HLSTree.cpp +++ b/src/parser/HLSTree.cpp @@ -137,6 +137,18 @@ std::string GetVideoCodec(std::string_view codecs) } return ""; } + +// \brief Get the first subtitle codec string from CODECS attribute list +std::string GetSubtitleCodec(std::string_view codecs) +{ + const std::vector list = STRING::SplitToVec(codecs, ','); + for (const std::string& codecStr : list) + { + if (CODEC::IsSubtitleFourCC(codecStr)) + return codecStr; + } + return ""; +} } // unnamed namespace adaptive::CHLSTree::CHLSTree() : AdaptiveTree() @@ -1645,9 +1657,17 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data) if (!ParseRenditon(r, newAdpSet, newRepr)) continue; - // Use WebVTT as default subtitle codec - newRepr->AddCodecs(CODEC::FOURCC_WVTT); - newAdpSet->AddCodecs(CODEC::FOURCC_WVTT); + + // Find the codec string from a variant that references it + const Variant* varFound = FindVariantBySubtitleGroupId(r.m_groupId, pl.m_variants); + std::string codecStr; + if (varFound) + codecStr = GetSubtitleCodec(varFound->m_codecs); + if (codecStr.empty()) + codecStr = CODEC::FOURCC_WVTT; // WebVTT as default subtitle codec + + newRepr->AddCodecs(codecStr); + newAdpSet->AddCodecs(codecStr); newAdpSet->AddRepresentation(newRepr); period->AddAdaptationSet(newAdpSet); @@ -1872,6 +1892,18 @@ const adaptive::CHLSTree::Variant* adaptive::CHLSTree::FindVariantByAudioGroupId return nullptr; } +const adaptive::CHLSTree::Variant* adaptive::CHLSTree::FindVariantBySubtitleGroupId( + std::string groupId, std::vector& variants) const +{ + auto itVar = + std::find_if(variants.cbegin(), variants.cend(), + [&groupId](const Variant& item) { return item.m_groupIdSubtitles == groupId; }); + if (itVar != variants.cend()) + return &(*itVar); + + return nullptr; +} + const adaptive::CHLSTree::Rendition* adaptive::CHLSTree::FindRenditionByGroupId( std::string groupId, std::vector& renditions) const { diff --git a/src/parser/HLSTree.h b/src/parser/HLSTree.h index 71754ddeb..c7f6911e0 100644 --- a/src/parser/HLSTree.h +++ b/src/parser/HLSTree.h @@ -209,6 +209,15 @@ class ATTR_DLL_LOCAL CHLSTree : public AdaptiveTree const Variant* FindVariantByAudioGroupId(std::string groupId, std::vector& variants) const; + /*! + * \brief Find the first variant with the specified audio group id. + * \param groupId The group id + * \param variants The variants where search for + * \return The variant if found, otherwise nullptr + */ + const adaptive::CHLSTree::Variant* FindVariantBySubtitleGroupId( + std::string groupId, std::vector& variants) const; + /*! * \brief Find the first rendition with the specified group id. * \param groupId The group id