Skip to content

Commit

Permalink
[DASHTree] handle new Period better
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Feb 28, 2023
1 parent ab25c26 commit 7ba0047
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/common/AdaptiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,53 @@ bool AdaptiveStream::ensureSegment()
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return false;
}
else if (tree_.HasUpdateThread() && current_period_ != tree_.periods_.back())
{
auto itPer = std::find(tree_.periods_.begin(), tree_.periods_.end(), current_period_);
if (itPer == tree_.periods_.end())
{
LOG::LogF(LOGWARNING, "Current Period missing from tree!");
state_ = STOPPED;
return false;
}

auto pos = std::distance(tree_.periods_.begin(), itPer);
current_period_ = tree_.periods_[pos+1];
LOG::LogF(LOGDEBUG, "Switching to new Period (id=%s, start=%ld)", current_period_->id_.c_str(), current_period_->start_);
auto newAdp = current_adp_;
bool adpFound{false};
for (auto updAdp : current_period_->adaptationSets_)
{
if (!(current_adp_->id_ == updAdp->id_ &&
current_adp_->group_ == updAdp->group_ &&
current_adp_->type_ == updAdp->type_ &&
current_adp_->mimeType_ == updAdp->mimeType_ &&
current_adp_->language_ == updAdp->language_))
continue;
for (auto udpRep : updAdp->representations_)
{
if (udpRep->codecs_ != current_rep_->codecs_)
continue;
newAdp = updAdp;
adpFound = true;
break;
}
if (adpFound)
break;
}
if (!adpFound)
{
LOG::LogF(LOGWARNING, "Failed to find matching AdaptationSet in new Period!");
return false;
}
current_adp_ = newAdp;
LOG::LogF(LOGDEBUG, "Switching to new AdaptationSet (startNumber=%d)", newAdp->startNumber_);
current_rep_ = tree_.GetRepChooser()->GetRepresentation(current_adp_);
return true;
}
else
{
LOG::LogF(LOGDEBUG, "Transition to STOPPED");
state_ = STOPPED;
return false;
}
Expand Down
41 changes: 38 additions & 3 deletions src/parser/DASHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,19 +1775,54 @@ 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_);
std::vector<Period*> tmp = periods_;
updateTree->periods_[index] = nullptr;
tmp.push_back(updPeriod);
periods_ = tmp;
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

0 comments on commit 7ba0047

Please sign in to comment.