Skip to content

Commit

Permalink
Merge pull request #1178 from tmm1/nexus-better-period-matching
Browse files Browse the repository at this point in the history
[backport] [DASHTree] handle new Period better
  • Loading branch information
glennguy committed Mar 16, 2023
2 parents 8964800 + 8192c32 commit 28c1b11
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,10 @@ bool CSession::SeekChapter(int ch)
if (ch >= 0 && ch < static_cast<int>(m_adaptiveTree->periods_.size()) &&
m_adaptiveTree->periods_[ch] != m_adaptiveTree->current_period_)
{
m_adaptiveTree->next_period_ = m_adaptiveTree->periods_[ch];
auto newPd = m_adaptiveTree->periods_[ch];
m_adaptiveTree->next_period_ = newPd;
LOG::LogF(LOGDEBUG, "Switching to new Period (id=%s, start=%ld, seq=%d)", newPd->id_.c_str(),
newPd->start_, newPd->sequence_);
for (auto& stream : m_streams)
{
ISampleReader* sr{stream->GetReader()};
Expand Down
2 changes: 1 addition & 1 deletion src/common/AdaptiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ bool AdaptiveStream::seek_time(double seek_seconds, bool preceeding, bool& needR

bool AdaptiveStream::waitingForSegment(bool checkTime) const
{
if (tree_.HasUpdateThread())
if (tree_.HasUpdateThread() && state_ == RUNNING)
{
std::lock_guard<std::mutex> lckTree(tree_.GetTreeMutex());
if (current_rep_ && (current_rep_->flags_ & AdaptiveTree::Representation::WAITFORSEGMENT) != 0)
Expand Down
37 changes: 34 additions & 3 deletions src/parser/DASHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ static void XMLCALL start(void* data, const char* el, const char** attr)
dash->periods_.push_back(dash->current_period_);
dash->period_timelined_ = false;
dash->current_period_->start_ = 0;
dash->current_period_->sequence_ = dash->current_sequence_++;

for (; *attr;)
{
Expand Down Expand Up @@ -1778,19 +1779,49 @@ void DASHTree::RefreshLiveSegments()

for (size_t index{0}; index < updateTree->periods_.size(); index++)
{
if (index == periods_.size()) // Exceeded periods
break;
auto updPeriod = updateTree->periods_[index];
if (!updPeriod)
continue;

Period* period{nullptr};
// find matching period based on ID
auto itPd = std::find_if(periods_.begin(), periods_.end(),
[&updPeriod](const Period* item)
{ return !item->id_.empty() && item->id_ == updPeriod->id_; });
if (itPd == periods_.end())
{
// not found, try matching period based on start
itPd = std::find_if(periods_.begin(), periods_.end(),
[&updPeriod](const Period* item)
{ return item->start_ && item->start_ == updPeriod->start_; });
}
// found!
if (itPd != periods_.end())
period = *itPd;
if (!period && updPeriod->id_.empty() && updPeriod->start_ == 0)
{
// not found, fallback match based on position
if (index < periods_.size())
period = periods_[index];
}
// new period, insert it
if (!period)
{
LOG::LogF(LOGDEBUG, "Inserting new Period (id=%s, start=%ld)", updPeriod->id_.c_str(),
updPeriod->start_);
updateTree->periods_[index] = nullptr; // remove to prevent delete; we take ownership
updPeriod->sequence_ = current_sequence_++;
periods_.push_back(updPeriod);
continue;
}

for (auto updAdaptationSet : updPeriod->adaptationSets_)
{
// Locate adaptationset
if (!updAdaptationSet)
continue;

for (auto adaptationSet : periods_[index]->adaptationSets_)
for (auto adaptationSet : period->adaptationSets_)
{
if (!(adaptationSet->id_ == updAdaptationSet->id_ &&
adaptationSet->group_ == updAdaptationSet->group_ &&
Expand Down
1 change: 1 addition & 0 deletions src/parser/DASHTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ATTR_DLL_LOCAL DASHTree : public AdaptiveTree

uint64_t pts_helper_, timeline_time_;
uint64_t firstStartNumber_;
uint32_t current_sequence_;
std::string current_playready_wrmheader_;
std::string mpd_url_;

Expand Down

0 comments on commit 28c1b11

Please sign in to comment.