Skip to content

Commit

Permalink
[HLSTree] Check for already add uri variant/rendition
Browse files Browse the repository at this point in the history
Some manifests can have multiple audio rendition GROUPS with differents audio
codecs
means that multiple Variants with same uri but different AUDIO group may exists and we should not add them more times

Similar thing seem to happens for Renditions despite seem less common thing,
more Renditions with identical properties (and so same uri) but different GROUP-ID may exists, and we should not add them more times
  • Loading branch information
CastagnaIT committed Aug 26, 2023
1 parent e69a203 commit 1ef93cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/parser/HLSTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "../utils/log.h"
#include "kodi/tools/StringUtils.h"

#include <algorithm>
#include <optional>
#include <sstream>

Expand Down Expand Up @@ -974,6 +975,19 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data)
rend.m_isForced = attribs["FORCED"] == "YES";
rend.m_characteristics = attribs["CHARACTERISTICS"];
rend.m_uri = attribs["URI"];
std::string uri = attribs["URI"];

if (!uri.empty())
{
// Check if this uri has been already added
if (std::any_of(pl.m_audioRenditions.cbegin(), pl.m_audioRenditions.cend(),
[&uri](const Rendition& v) { return v.m_uri == uri; }) ||
std::any_of(pl.m_subtitleRenditions.cbegin(), pl.m_subtitleRenditions.cend(),
[&uri](const Rendition& v) { return v.m_uri == uri; }))
{
rend.m_isUriDuplicate = true;
}
}

if (streamType == StreamType::AUDIO)
pl.m_audioRenditions.emplace_back(rend);
Expand Down Expand Up @@ -1017,6 +1031,13 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data)
var.m_groupIdSubtitles = attribs["SUBTITLES"];
var.m_uri = uri;

// Check if this uri has been already added
if (std::any_of(pl.m_variants.cbegin(), pl.m_variants.cend(),
[&uri](const Variant& v) { return v.m_uri == uri; }))
{
var.m_isUriDuplicate = true;
}

pl.m_variants.emplace_back(var);
}
else if (tagName == "#EXT-X-SESSION-KEY")
Expand Down Expand Up @@ -1050,6 +1071,10 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data)
// Add audio renditions (do not take in account variants references)
for (const Rendition& r : pl.m_audioRenditions)
{
// There may be multiple renditions with the same uri but different GROUP-ID
if (r.m_isUriDuplicate)
continue;

auto newAdpSet = CAdaptationSet::MakeUniquePtr(period.get());
auto newRepr = CRepresentation::MakeUniquePtr(newAdpSet.get());

Expand Down Expand Up @@ -1096,6 +1121,10 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data)
// Add subtitles renditions (do not take in account variants references)
for (const Rendition& r : pl.m_subtitleRenditions)
{
// There may be multiple renditions with the same uri but different GROUP-ID
if (r.m_isUriDuplicate)
continue;

auto newAdpSet = CAdaptationSet::MakeUniquePtr(period.get());
auto newRepr = CRepresentation::MakeUniquePtr(newAdpSet.get());

Expand All @@ -1112,6 +1141,10 @@ bool adaptive::CHLSTree::ParseMultivariantPlaylist(const std::string& data)
// Add variants
for (const Variant& var : pl.m_variants)
{
// There may be multiple variants with the same uri but different AUDIO group
if (var.m_isUriDuplicate)
continue;

if (var.m_bandwidth == 0)
LOG::LogF(LOGWARNING, "Variant with malformed bandwidth attribute");

Expand Down
2 changes: 2 additions & 0 deletions src/parser/HLSTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ATTR_DLL_LOCAL CHLSTree : public AdaptiveTree
std::string m_characteristics;
std::string m_uri;
int m_features{REND_FEATURE_NONE};
bool m_isUriDuplicate{false}; // Another rendition have same uri
};

// \brief Usually refer to an EXT-X-STREAM-INF tag
Expand All @@ -91,6 +92,7 @@ class ATTR_DLL_LOCAL CHLSTree : public AdaptiveTree
std::string m_groupIdAudio;
std::string m_groupIdSubtitles;
std::string m_uri;
bool m_isUriDuplicate{false}; // Another variant have same uri
};

struct MultivariantPlaylist
Expand Down

0 comments on commit 1ef93cb

Please sign in to comment.