Commit fc5f2e6c authored by François Cartegnie's avatar François Cartegnie 🤞

demux: adaptive: use absolute number reference for live templates

instead of using it for offsetting relative sequence number in url.
(needed for picking startnumber by time offset)
parent 798169a1
......@@ -164,39 +164,75 @@ std::size_t SegmentInformation::getAllSegments(std::vector<ISegment *> &retSegme
uint64_t SegmentInformation::getLiveStartSegmentNumber(uint64_t def) const
{
const mtime_t i_max_buffering = getPlaylist()->getMaxBuffering();
if( mediaSegmentTemplate )
{
uint64_t start = 0;
uint64_t end = 0;
const Timescale timescale = mediaSegmentTemplate->inheritTimescale();
SegmentTimeline *timeline = mediaSegmentTemplate->segmentTimeline.Get();
if(timeline)
if( timeline )
{
const uint64_t start = timeline->minElementNumber();
const uint64_t end = timeline->maxElementNumber();
if(end > 2 && (end - start >= 2))
return end - 2;
else
start = timeline->minElementNumber();
end = timeline->maxElementNumber();
stime_t endtime, duration;
timeline->getScaledPlaybackTimeDurationBySegmentNumber( end, &endtime, &duration );
if( endtime + duration <= timescale.ToScaled( i_max_buffering ) )
return start;
uint64_t number = timeline->getElementNumberByScaledPlaybackTime(
endtime + duration - timescale.ToScaled( i_max_buffering ) );
if( number < start )
number = start;
return number;
}
/* Else compute, current time and timeshiftdepth based */
else if( mediaSegmentTemplate->duration.Get() )
{
const mtime_t i_buffer_depth = getPlaylist()->timeShiftBufferDepth.Get();
const uint64_t startnumber = mediaSegmentTemplate->startNumber.Get();
end = mediaSegmentTemplate->getCurrentLiveTemplateNumber();
const uint64_t count = timescale.ToScaled( i_buffer_depth ) / mediaSegmentTemplate->duration.Get();
if( startnumber + count >= end )
start = startnumber;
else
start = end - count;
const uint64_t bufcount = ( 1 + timescale.ToScaled(i_max_buffering) /
mediaSegmentTemplate->duration.Get() );
return ( end - start > bufcount ) ? end - bufcount : start;
}
return mediaSegmentTemplate->startNumber.Get();
}
else if ( segmentList && !segmentList->getSegments().empty() )
{
const Timescale timescale = segmentList->inheritTimescale();
const std::vector<ISegment *> list = segmentList->getSegments();
if(list.size() > 3)
return list.at(list.size() - 3)->getSequenceNumber();
else if(!list.empty())
const ISegment *back = list.back();
const stime_t bufferingstart = back->startTime.Get() + back->duration.Get() - timescale.ToScaled( i_max_buffering );
uint64_t number;
if( !segmentList->getSegmentNumberByScaledTime( bufferingstart, &number ) )
return list.front()->getSequenceNumber();
else
return segmentList->getStartIndex();
return number;
}
else if( segmentBase )
{
const std::vector<ISegment *> list = segmentBase->subSegments();
if(list.size() > 3)
return list.at(list.size() - 3)->getSequenceNumber();
else if(!list.empty())
return list.front()->getSequenceNumber();
else
if(!list.empty())
return segmentBase->getSequenceNumber();
const Timescale timescale = inheritTimescale();
const ISegment *back = list.back();
const stime_t bufferingstart = back->startTime.Get() + back->duration.Get() - timescale.ToScaled( i_max_buffering );
uint64_t number;
if( !SegmentInfoCommon::getSegmentNumberByScaledTime( list, bufferingstart, &number ) )
return list.front()->getSequenceNumber();
return number;
}
if(parent)
......@@ -300,6 +336,7 @@ bool SegmentInformation::getSegmentNumberByTime(mtime_t time, uint64_t *ret) con
return true;
}
/* Note: incorrect for live */
const stime_t duration = mediaSegmentTemplate->duration.Get();
*ret = mediaSegmentTemplate->startNumber.Get();
if(duration)
......
......@@ -27,6 +27,7 @@
#include "SegmentTemplate.h"
#include "SegmentTimeline.h"
#include "SegmentInformation.hpp"
#include "AbstractPlaylist.hpp"
using namespace adaptive::playlist;
......@@ -44,6 +45,7 @@ MediaSegmentTemplate::MediaSegmentTemplate( SegmentInformation *parent ) :
startNumber.Set( 1 );
initialisationSegment.Set( NULL );
templated = true;
parentSegmentInformation = parent;
}
void MediaSegmentTemplate::mergeWith(MediaSegmentTemplate *updated, mtime_t prunebarrier)
......@@ -75,6 +77,25 @@ size_t MediaSegmentTemplate::pruneBySequenceNumber(uint64_t number)
return 0;
}
uint64_t MediaSegmentTemplate::getCurrentLiveTemplateNumber() const
{
uint64_t number = startNumber.Get();
/* live streams / templated */
const stime_t dur = duration.Get();
if(dur)
{
/* compute, based on current time */
const time_t playbacktime = time(NULL);
const Timescale timescale = inheritTimescale();
time_t streamstart = parentSegmentInformation->getPlaylist()->availabilityStartTime.Get();
streamstart += parentSegmentInformation->getPeriodStart();
stime_t elapsed = timescale.ToScaled(CLOCK_FREQ * (playbacktime - streamstart));
number += elapsed / dur - 2;
}
return number;
}
stime_t MediaSegmentTemplate::getMinAheadScaledTime(uint64_t number) const
{
if( segmentTimeline.Get() )
......
......@@ -52,11 +52,15 @@ namespace adaptive
virtual void setSourceUrl( const std::string &url ); /* reimpl */
void mergeWith( MediaSegmentTemplate *, mtime_t );
virtual uint64_t getSequenceNumber() const; /* reimpl */
uint64_t getCurrentLiveTemplateNumber() const;
stime_t getMinAheadScaledTime(uint64_t) const;
void pruneByPlaybackTime(mtime_t);
size_t pruneBySequenceNumber(uint64_t);
virtual void debug(vlc_object_t *, int = 0) const; /* reimpl */
Property<size_t> startNumber;
protected:
SegmentInformation *parentSegmentInformation;
};
class InitSegmentTemplate : public BaseSegmentTemplate
......
......@@ -94,7 +94,10 @@ uint64_t SegmentTimeline::getElementNumberByScaledPlaybackTime(stime_t scaled) c
{
const Element *el = *it;
if(it == elements.begin())
{
scaled -= el->t;
prevnumber = el->number;
}
for(uint64_t repeat = 1 + el->r; repeat; repeat--)
{
......
......@@ -112,7 +112,7 @@ std::string Representation::contextualize(size_t number, const std::string &comp
{
std::stringstream ss;
ss.imbue(std::locale("C"));
ss << getLiveTemplateNumberOffset(number, templ);
ss << number;
ret.replace(pos, std::string("$Number$").length(), ss.str());
}
else
......@@ -137,7 +137,7 @@ std::string Representation::contextualize(size_t number, const std::string &comp
oss.imbue(std::locale("C"));
oss.width(width); /* set format string length */
oss.fill('0');
oss << getLiveTemplateNumberOffset(number, templ);
oss << number;
ret.replace(pos, fmtend - pos + 1, oss.str());
} catch(int) {}
}
......@@ -175,25 +175,3 @@ mtime_t Representation::getScaledTimeBySegmentNumber(uint64_t index, const Media
return time;
}
uint64_t Representation::getLiveTemplateNumberOffset(uint64_t index, const MediaSegmentTemplate *templ) const
{
/* live streams / templated */
if(getPlaylist()->isLive())
{
if(templ->segmentTimeline.Get())
{
// do nothing ?
}
else if(templ->duration.Get())
{
const time_t playbackstart = getPlaylist()->playbackStart.Get();
time_t streamstart = getPlaylist()->availabilityStartTime.Get();
streamstart += getPeriodStart();
const stime_t duration = templ->duration.Get();
const Timescale timescale = templ->inheritTimescale();
if(duration)
index += timescale.ToScaled(CLOCK_FREQ * (playbackstart - streamstart)) / duration;
}
}
return index;
}
......@@ -72,7 +72,6 @@ namespace dash
/* for contextualize() */
mtime_t getScaledTimeBySegmentNumber(uint64_t, const MediaSegmentTemplate *) const;
uint64_t getLiveTemplateNumberOffset(uint64_t, const MediaSegmentTemplate *) const;
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment