bool SegmentTracker::segmentsListReady() const { BaseRepresentation *rep = curRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep && rep->getPlaylist()->isLive()) return rep->getMinAheadTime(curNumber) > 0; return true; }
mtime_t SegmentTracker::getMinAheadTime() const { BaseRepresentation *rep = curRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep) return rep->getMinAheadTime(curNumber); return 0; }
StreamFormat SegmentTracker::initialFormat() const { BaseRepresentation *rep = curRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep) return rep->getStreamFormat(); return StreamFormat(); }
Chunk * SegmentTracker::getNextChunk(StreamType type, bool switch_allowed) { BaseRepresentation *rep; ISegment *segment; if(!currentPeriod) return NULL; if( !switch_allowed || (prevRepresentation && prevRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) ) rep = prevRepresentation; else rep = logic->getCurrentRepresentation(type, currentPeriod); if ( rep == NULL ) return NULL; if(rep != prevRepresentation) { prevRepresentation = rep; initializing = true; } if(initializing) { initializing = false; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT); if(segment) return segment->toChunk(count, rep); } if(!indexed) { indexed = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX); if(segment) return segment->toChunk(count, rep); } segment = rep->getSegment(BaseRepresentation::INFOTYPE_MEDIA, count); if(!segment) { currentPeriod = playlist->getNextPeriod(currentPeriod); resetCounter(); return getNextChunk(type, switch_allowed); } Chunk *chunk = segment->toChunk(count, rep); if(chunk) count++; return chunk; }
StreamFormat SegmentTracker::getCurrentFormat() const { BaseRepresentation *rep = curRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep) { /* Ensure ephemere content is updated/loaded */ if(rep->needsUpdate()) (void) rep->runLocalUpdates(0, curNumber, false); return rep->getStreamFormat(); } return StreamFormat(); }
bool SegmentTracker::setPositionByTime(mtime_t time, bool restarted, bool tryonly) { uint64_t segnumber; BaseRepresentation *rep = prevRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep && rep->getSegmentNumberByTime(time, &segnumber)) { if(!tryonly) setPositionByNumber(segnumber, restarted); return true; } return false; }
BaseRepresentation * RepresentationSelector::select(BaseAdaptationSet *adaptSet, uint64_t bitrate) const { if (adaptSet == NULL) return NULL; BaseRepresentation *best = NULL; std::vector<BaseRepresentation *> reps = adaptSet->getRepresentations(); BaseRepresentation *candidate = select(reps, (best)?best->getBandwidth():0, bitrate); if (candidate) { if (candidate->getBandwidth() > bitrate) /* none matched, returned lowest */ return candidate; best = candidate; } return best; }
BaseRepresentation * NearOptimalAdaptationLogic::getNextQualityIndex( BaseAdaptationSet *adaptSet, RepresentationSelector &selector, float gammaP, mtime_t VD, mtime_t Q ) { BaseRepresentation *ret = NULL; BaseRepresentation *prev = NULL; float argmax; for(BaseRepresentation *rep = selector.lowest(adaptSet); rep && rep != prev; rep = selector.higher(adaptSet, rep)) { float arg = ( VD * (getUtility(rep) + gammaP) - Q ) / rep->getBandwidth(); if(ret == NULL || argmax <= arg) { ret = rep; argmax = arg; } prev = rep; } return ret; }
BaseRepresentation *NearOptimalAdaptationLogic::getNextRepresentation(BaseAdaptationSet *adaptSet, BaseRepresentation *prevRep) { RepresentationSelector selector(maxwidth, maxheight); const float umin = getUtility(selector.lowest(adaptSet)); const float umax = getUtility(selector.highest(adaptSet)); vlc_mutex_lock(&lock); std::map<ID, NearOptimalContext>::iterator it = streams.find(adaptSet->getID()); if(it == streams.end()) { vlc_mutex_unlock(&lock); return selector.lowest(adaptSet); } NearOptimalContext ctxcopy = (*it).second; const unsigned bps = getAvailableBw(currentBps, prevRep); vlc_mutex_unlock(&lock); const float gammaP = 1.0 + (umax - umin) / ((float)ctxcopy.buffering_target / ctxcopy.buffering_min - 1.0); const float Vd = ((float)ctxcopy.buffering_min / CLOCK_FREQ - 1.0) / (umin + gammaP); BaseRepresentation *m; if(prevRep == NULL) /* Starting */ { m = selector.select(adaptSet, bps); } else { /* noted m* */ m = getNextQualityIndex(adaptSet, selector, gammaP - umin /* umin == Sm, utility = std::log(S/Sm) */, Vd, (float)ctxcopy.buffering_level / CLOCK_FREQ); if(m->getBandwidth() < prevRep->getBandwidth()) /* m*[n] < m*[n-1] */ { BaseRepresentation *mp = selector.select(adaptSet, bps); /* m' */ if(mp->getBandwidth() <= m->getBandwidth()) { mp = m; } else if(mp->getBandwidth() > prevRep->getBandwidth()) { mp = prevRep; } else { mp = selector.lower(adaptSet, mp); } m = mp; } } BwDebug( msg_Info(p_obj, "buffering level %.2f% rep %ld kBps %zu kBps", (float) 100 * ctxcopy.buffering_level / ctxcopy.buffering_target, m->getBandwidth()/8000, bps / 8000); );
BaseRepresentation * RepresentationSelector::select(BasePeriod *period, adaptative::StreamType type, uint64_t bitrate) const { if (period == NULL) return NULL; std::vector<BaseAdaptationSet *> adaptSets = period->getAdaptationSets(type); BaseRepresentation *best = NULL; std::vector<BaseAdaptationSet *>::const_iterator adaptIt; for(adaptIt=adaptSets.begin(); adaptIt!=adaptSets.end(); ++adaptIt) { std::vector<BaseRepresentation *> reps = (*adaptIt)->getRepresentations(); BaseRepresentation *candidate = select(reps, (best)?best->getBandwidth():0, bitrate); if (candidate) { if (candidate->getBandwidth() > bitrate) /* none matched, returned lowest */ return candidate; best = candidate; } } return best; }
mtime_t SegmentTracker::getPlaybackTime() const { mtime_t time, duration; BaseRepresentation *rep = curRepresentation; if(!rep) rep = logic->getNextRepresentation(adaptationSet, NULL); if(rep && rep->getPlaybackTimeDurationBySegmentNumber(next, &time, &duration)) { /* LVP added, TFE */ // apparently not used //msg_Info(NULL, "TFE SegmentTracker getPlaybackTime, %" PRId64 ", %" PRId64 ", %" PRId64, // mdate(), time, duration); //std::cerr << "TFE SegmentTracker::getPlaybackTime time and duration, " // << mdate() << ", " << time << ", " << duration << std::endl; return time; } return 0; }
SegmentChunk * SegmentTracker::getNextChunk(bool switch_allowed, HTTPConnectionManager *connManager) { BaseRepresentation *rep = NULL; ISegment *segment; if(!adaptationSet) return NULL; /* Ensure we don't keep chaining init/index without data */ if( initializing ) { if( prevRepresentation ) switch_allowed = false; else switch_allowed = true; } if( !switch_allowed || (prevRepresentation && prevRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) ) rep = prevRepresentation; else rep = logic->getNextRepresentation(adaptationSet, prevRepresentation); if ( rep == NULL ) return NULL; if(rep != prevRepresentation) { notify(SegmentTrackerEvent(prevRepresentation, rep)); prevRepresentation = rep; init_sent = false; index_sent = false; initializing = true; } /* Ensure ephemere content is updated/loaded */ if(rep->needsUpdate()) updateSelected(); if(!init_sent) { init_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT); if(segment) return segment->toChunk(count, rep, connManager); } if(!index_sent) { index_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX); if(segment) return segment->toChunk(count, rep, connManager); } bool b_gap = false; segment = rep->getNextSegment(BaseRepresentation::INFOTYPE_MEDIA, count, &count, &b_gap); if(!segment) { reset(); return NULL; } /* stop initializing after 1st chunk */ initializing = false; SegmentChunk *chunk = segment->toChunk(count, rep, connManager); if(chunk) count++; /* We need to check segment/chunk format changes, as we can't rely on representation's (HLS)*/ if(format != chunk->getStreamFormat()) { format = chunk->getStreamFormat(); notify(SegmentTrackerEvent(&format)); } /* Handle both implicit and explicit discontinuities */ if( (b_gap && count) || (chunk && chunk->discontinuity) ) { notify(SegmentTrackerEvent(chunk)); } return chunk; }
BaseRepresentation *PredictiveAdaptationLogic::getNextRepresentation(BaseAdaptationSet *adaptSet, BaseRepresentation *prevRep) { RepresentationSelector selector(maxwidth, maxheight); BaseRepresentation *rep; vlc_mutex_lock(&lock); std::map<ID, PredictiveStats>::iterator it = streams.find(adaptSet->getID()); if(it == streams.end()) { rep = selector.highest(adaptSet); } else { PredictiveStats &stats = (*it).second; double f_buffering_level = (double)stats.buffering_level / stats.buffering_target; double f_min_buffering_level = f_buffering_level; unsigned i_max_bitrate = 0; if(streams.size() > 1) { std::map<ID, PredictiveStats>::const_iterator it2 = streams.begin(); for(; it2 != streams.end(); ++it2) { if(it2 == it) continue; const PredictiveStats &other = (*it2).second; f_min_buffering_level = std::min((double)other.buffering_level / other.buffering_target, f_min_buffering_level); i_max_bitrate = std::max(i_max_bitrate, other.last_download_rate); } } if(stats.starting()) { rep = selector.highest(adaptSet); } else { const unsigned i_available_bw = getAvailableBw(i_max_bitrate, prevRep); if(!prevRep) { rep = selector.select(adaptSet, i_available_bw); } else if(f_buffering_level > 0.8) { rep = selector.select(adaptSet, std::max((uint64_t) i_available_bw, (uint64_t) prevRep->getBandwidth())); } else if(f_buffering_level > 0.5) { rep = prevRep; } else { if(f_buffering_level > 2 * stats.last_duration) { rep = selector.lower(adaptSet, prevRep); } else { rep = selector.select(adaptSet, i_available_bw * f_buffering_level); } } } BwDebug( for(it=streams.begin(); it != streams.end(); ++it) { const PredictiveStats &s = (*it).second; msg_Info(p_obj, "Stream %s buffering level %.2f%", (*it).first.str().c_str(), (double) s.buffering_level / s.buffering_target); } ); BwDebug( if( rep != prevRep ) msg_Info(p_obj, "Stream %s new bandwidth usage %zu KiB/s", adaptSet->getID().str().c_str(), rep->getBandwidth() / 8000); );
SegmentChunk * SegmentTracker::getNextChunk(bool switch_allowed, HTTPConnectionManager *connManager) { BaseRepresentation *rep = NULL, *prevRep = NULL; ISegment *segment; if(!adaptationSet) return NULL; /* Ensure we don't keep chaining init/index without data */ if( initializing ) { if( curRepresentation ) switch_allowed = false; else switch_allowed = true; } if( !switch_allowed || (curRepresentation && curRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) ) rep = curRepresentation; else rep = logic->getNextRepresentation(adaptationSet, curRepresentation); if ( rep == NULL ) return NULL; if(rep != curRepresentation) { notify(SegmentTrackerEvent(curRepresentation, rep)); prevRep = curRepresentation; curRepresentation = rep; init_sent = false; index_sent = false; initializing = true; } bool b_updated = false; /* Ensure ephemere content is updated/loaded */ if(rep->needsUpdate()) b_updated = rep->runLocalUpdates(getPlaybackTime(), curNumber, false); if(prevRep && !rep->consistentSegmentNumber()) { /* Convert our segment number */ next = rep->translateSegmentNumber(next, prevRep); } else if(first && rep->getPlaylist()->isLive()) { next = rep->getLiveStartSegmentNumber(next); first = false; } if(b_updated) { if(!rep->consistentSegmentNumber()) curRepresentation->pruneBySegmentNumber(curNumber); curRepresentation->scheduleNextUpdate(next); } if(!init_sent) { init_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT); if(segment) return segment->toChunk(next, rep, connManager); } if(!index_sent) { index_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX); if(segment) return segment->toChunk(next, rep, connManager); } bool b_gap = false; segment = rep->getNextSegment(BaseRepresentation::INFOTYPE_MEDIA, next, &next, &b_gap); if(!segment) { reset(); return NULL; } if(initializing) { b_gap = false; /* stop initializing after 1st chunk */ initializing = false; } SegmentChunk *chunk = segment->toChunk(next, rep, connManager); /* We need to check segment/chunk format changes, as we can't rely on representation's (HLS)*/ if(chunk && format != chunk->getStreamFormat()) { format = chunk->getStreamFormat(); notify(SegmentTrackerEvent(&format)); } /* Handle both implicit and explicit discontinuities */ if( (b_gap && next) || (chunk && chunk->discontinuity) ) { notify(SegmentTrackerEvent(chunk)); } if(chunk) { curNumber = next; next++; } return chunk; }
SegmentChunk * SegmentTracker::getNextChunk(bool switch_allowed, AbstractConnectionManager *connManager) { /* LVP added, TFE DEBUG */ //msg_Info(NULL, "TFE DEBUG SegmentTracker getNextChunk, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker getNextChunk, " << mdate() << std::endl; BaseRepresentation *rep = NULL, *prevRep = NULL; ISegment *segment; if(!adaptationSet) { /* LVP added, TFE DEBUG */ //msg_Info(NULL, "TFE DEBUG SegmentTracker getNextChunk no adaptation set, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker getNextChunk no adaptation set, " << mdate() << std::endl; return NULL; } /* Ensure we don't keep chaining init/index without data */ if( initializing ) { if( curRepresentation ) switch_allowed = false; else switch_allowed = true; } if( !switch_allowed || (curRepresentation && curRepresentation->getSwitchPolicy() == SegmentInformation::SWITCH_UNAVAILABLE) ) rep = curRepresentation; else rep = logic->getNextRepresentation(adaptationSet, curRepresentation); if ( rep == NULL ) { /* LVP added, TFE DEBUG */ //msg_Info(NULL, "TFE DEBUG SegmentTracker no rep 1, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker no rep 1, " << mdate() << std::endl; return NULL; } if(rep != curRepresentation) { notify(SegmentTrackerEvent(curRepresentation, rep)); prevRep = curRepresentation; curRepresentation = rep; init_sent = false; index_sent = false; initializing = true; } bool b_updated = false; /* Ensure ephemere content is updated/loaded */ if(rep->needsUpdate()) b_updated = rep->runLocalUpdates(getPlaybackTime(), curNumber, false); if(prevRep && !rep->consistentSegmentNumber()) { /* Convert our segment number */ next = rep->translateSegmentNumber(next, prevRep); } else if(first && rep->getPlaylist()->isLive()) { next = rep->getLiveStartSegmentNumber(next); first = false; } if(b_updated) { if(!rep->consistentSegmentNumber()) curRepresentation->pruneBySegmentNumber(curNumber); curRepresentation->scheduleNextUpdate(next); } if(rep->getStreamFormat() != format) { /* Initial format ? */ if(format == StreamFormat(StreamFormat::UNSUPPORTED)) { format = rep->getStreamFormat(); } else { format = rep->getStreamFormat(); notify(SegmentTrackerEvent(&format)); /* Notify new demux format */ return NULL; /* Force current demux to end */ } } if(format == StreamFormat(StreamFormat::UNSUPPORTED)) { return NULL; /* Can't return chunk because no demux will be created */ } if(!init_sent) { init_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INIT); if(segment) { /* LVP added, TFE DEBUG */ std::cerr << "TFE DEBUG SegmentTracker getNextChunk no init sent but segment, " << mdate() << std::endl; //msg_Info(NULL, "TFE DEBUG SegmentTracker getNextChunk no init sent but segment, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker getNextChunk no init sent but segment, " << mdate() << std::endl; return segment->toChunk(next, rep, connManager); } } if(!index_sent) { index_sent = true; segment = rep->getSegment(BaseRepresentation::INFOTYPE_INDEX); if(segment) { /* LVP added, TFE DEBUG */ // happens after no init sent //msg_Info(NULL, "TFE DEBUG SegmentTracker getNextChunk no index sent but segment, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker getNextChunk no index sent but segment, " << mdate() << std::endl; return segment->toChunk(next, rep, connManager); } } bool b_gap = false; segment = rep->getNextSegment(BaseRepresentation::INFOTYPE_MEDIA, next, &next, &b_gap); if(!segment) { reset(); /* LVP added, TFE DEBUG */ // once at the end //msg_Info(NULL, "TFE DEBUG SegmentTracker getNextChunk no segment and reset, %" PRId64, mdate()); //std::cerr << "TFE DEBUG SegmentTracker getNextChunk no segment and reset, " << mdate() << std::endl; return NULL; } if(initializing) { b_gap = false; /* stop initializing after 1st chunk */ initializing = false; } SegmentChunk *chunk = segment->toChunk(next, rep, connManager); /* Notify new segment length for stats / logic */ if(chunk) { const Timescale timescale = rep->inheritTimescale(); notify(SegmentTrackerEvent(rep->getAdaptationSet()->getID(), timescale.ToTime(segment->duration.Get()))); } /* We need to check segment/chunk format changes, as we can't rely on representation's (HLS)*/ if(chunk && format != chunk->getStreamFormat()) { format = chunk->getStreamFormat(); notify(SegmentTrackerEvent(&format)); } /* Handle both implicit and explicit discontinuities */ if( (b_gap && next) || (chunk && chunk->discontinuity) ) { notify(SegmentTrackerEvent(chunk)); } if(chunk) { curNumber = next; next++; } /* LVP added, TFE */ //msg_Info(NULL, "TFE SegmentTracker getNextChunk done, %" PRId64 ", %" PRId64, mdate(), rep->getBandwidth()); //std::cerr << "TFE SegmentTracker getNextChunk done, " << mdate() << ", " << rep->getBandwidth() << std::endl; return chunk; }