Skip to content

Commit

Permalink
Merge pull request #1733 from CastagnaIT/hls_sub_codec
Browse files Browse the repository at this point in the history
[HLSTree] Add support to read subtitle codec
  • Loading branch information
CastagnaIT authored Nov 26, 2024
2 parents 9bf03c5 + 773eb62 commit 63e433d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/parser/HLSTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> list = STRING::SplitToVec(codecs, ',');
for (const std::string& codecStr : list)
{
if (CODEC::IsSubtitleFourCC(codecStr))
return codecStr;
}
return "";
}
} // unnamed namespace

adaptive::CHLSTree::CHLSTree() : AdaptiveTree()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Variant>& 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<adaptive::CHLSTree::Rendition>& renditions) const
{
Expand Down
9 changes: 9 additions & 0 deletions src/parser/HLSTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ class ATTR_DLL_LOCAL CHLSTree : public AdaptiveTree
const Variant* FindVariantByAudioGroupId(std::string groupId,
std::vector<Variant>& 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<Variant>& variants) const;

/*!
* \brief Find the first rendition with the specified group id.
* \param groupId The group id
Expand Down

0 comments on commit 63e433d

Please sign in to comment.