Replaced completeKeysOnly variable by more versatile needsLookAhead variable

This commit is contained in:
Thulinma 2017-01-10 12:33:13 +01:00
parent 4be57ab043
commit ba2ef09a7e
2 changed files with 26 additions and 39 deletions

View file

@ -13,10 +13,6 @@
#include <mist/timing.h> #include <mist/timing.h>
#include "output.h" #include "output.h"
#ifndef MIN_DELAY
#define MIN_DELAY 2500
#endif
namespace Mist{ namespace Mist{
JSON::Value Output::capa = JSON::Value(); JSON::Value Output::capa = JSON::Value();
@ -43,7 +39,7 @@ namespace Mist{
sought = false; sought = false;
isInitialized = false; isInitialized = false;
isBlocking = false; isBlocking = false;
completeKeysOnly = false; needsLookAhead = 0;
lastStats = 0; lastStats = 0;
maxSkipAhead = 7500; maxSkipAhead = 7500;
minSkipAhead = 5000; minSkipAhead = 5000;
@ -621,7 +617,7 @@ namespace Mist{
/// This function decides where in the stream initial playback starts. /// This function decides where in the stream initial playback starts.
/// The default implementation calls seek(0) for VoD. /// The default implementation calls seek(0) for VoD.
/// For live, it seeks to the last sync'ed keyframe of the main track, no closer than MIN_DELAY ms from the end. /// For live, it seeks to the last sync'ed keyframe of the main track, no closer than needsLookAhead ms from the end.
/// Unless lastms < 5000, then it seeks to the first keyframe of the main track. /// Unless lastms < 5000, then it seeks to the first keyframe of the main track.
/// Aborts if there is no main track or it has no keyframes. /// Aborts if there is no main track or it has no keyframes.
void Output::initialSeek(){ void Output::initialSeek(){
@ -637,7 +633,7 @@ namespace Mist{
bool good = true; bool good = true;
//check if all tracks have data for this point in time //check if all tracks have data for this point in time
for (std::set<unsigned long>::iterator ti = selectedTracks.begin(); ti != selectedTracks.end(); ++ti){ for (std::set<unsigned long>::iterator ti = selectedTracks.begin(); ti != selectedTracks.end(); ++ti){
if (myMeta.tracks[*ti].lastms < seekPos+MIN_DELAY){good = false; break;} if (myMeta.tracks[*ti].lastms < seekPos+needsLookAhead){good = false; break;}
if (mainTrack == *ti){continue;}//skip self if (mainTrack == *ti){continue;}//skip self
if (!myMeta.tracks.count(*ti)){ if (!myMeta.tracks.count(*ti)){
HIGH_MSG("Skipping track %lu, not in tracks", *ti); HIGH_MSG("Skipping track %lu, not in tracks", *ti);
@ -701,41 +697,32 @@ namespace Mist{
} }
} }
//delay the stream until its current keyframe is complete, if only complete keys wanted //delay the stream until metadata has caught up, if needed
if (completeKeysOnly){ if (needsLookAhead){
bool completeKeyReady = false; //we sleep in 250ms increments, or less if the lookahead time itself is less
int timeoutTries = 40;//wait default 250ms*40=10 seconds uint32_t sleepTime = std::min((uint32_t)250, needsLookAhead);
for (std::map<unsigned int, DTSC::Track>::iterator it = myMeta.tracks.begin(); it != myMeta.tracks.end(); it++){ //wait at most double the look ahead time, plus ten seconds
if (it->second.keys.size() >1){ uint32_t timeoutTries = (needsLookAhead / sleepTime) * 2 + (10000/sleepTime);
int thisTimeoutTries = ((it->second.lastms - it->second.firstms) / (it->second.keys.size()-1)) / 125; uint64_t needsTime = thisPacket.getTime() + needsLookAhead;
if (thisTimeoutTries > timeoutTries) timeoutTries = thisTimeoutTries; while(--timeoutTries){
} bool lookReady = true;
} for (std::set<long unsigned int>::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++){
unsigned int mTrack = getMainSelectedTrack(); if (myMeta.tracks[*it].lastms <= needsTime){
while(!completeKeyReady && timeoutTries>0){ if (timeoutTries == 1){
if (!myMeta.tracks[mTrack].keys.size() || myMeta.tracks[mTrack].keys.rbegin()->getTime() <= thisPacket.getTime()){ WARN_MSG("Track %lu: %llu <= %llu", *it, myMeta.tracks[*it].lastms, needsTime);
completeKeyReady = false;
}else{
DTSC::Key & mustHaveKey = myMeta.tracks[mTrack].getKey(getKeyForTime(mTrack, thisPacket.getTime()));
unsigned long long mustHaveTime = mustHaveKey.getTime() + mustHaveKey.getLength();
completeKeyReady = true;
for (std::set<unsigned long>::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++){
if (myMeta.tracks[*it].lastms < mustHaveTime){
completeKeyReady = false;
break;
} }
lookReady = false;
break;
} }
} }
if (!completeKeyReady){ if (lookReady){break;}
timeoutTries--;//we count down Util::wait(sleepTime);
stats(); stats();
Util::wait(250); updateMeta();
updateMeta();
}
} }
if (timeoutTries<=0){ if (!timeoutTries){
WARN_MSG("Waiting for key frame timed out"); WARN_MSG("Waiting for lookahead timed out - resetting lookahead!");
completeKeysOnly = false; needsLookAhead = 0;
} }
} }

View file

@ -99,7 +99,7 @@ namespace Mist {
unsigned int maxSkipAhead;///< Maximum ms that we will go ahead of the intended timestamps. unsigned int maxSkipAhead;///< Maximum ms that we will go ahead of the intended timestamps.
unsigned int minSkipAhead;///< Minimum ms that we will go ahead of the intended timestamps. unsigned int minSkipAhead;///< Minimum ms that we will go ahead of the intended timestamps.
unsigned int realTime;///< Playback speed in ms of data per second. eg: 0 is infinite, 1000 real-time, 5000 is 0.2X speed, 500 = 2X speed. unsigned int realTime;///< Playback speed in ms of data per second. eg: 0 is infinite, 1000 real-time, 5000 is 0.2X speed, 500 = 2X speed.
bool completeKeysOnly;///< Bool if we send whole keys only, so the metadata is complete and the output knows in advance what will be sent. uint32_t needsLookAhead;///< Amount of millis we need to be able to look ahead in the metadata
//Read/write status variables //Read/write status variables
Socket::Connection & myConn;///< Connection to the client. Socket::Connection & myConn;///< Connection to the client.