From 075756d64635101a6889af14054ae152e5f08fcd Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 5 Nov 2018 16:44:24 +0100 Subject: [PATCH] Improved output timing --- src/output/output.cpp | 28 ++++++++++++++++++++++------ src/output/output.h | 2 ++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/output/output.cpp b/src/output/output.cpp index 137e7f01..a182207b 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -64,6 +64,7 @@ namespace Mist{ isInitialized = false; isBlocking = false; needsLookAhead = 0; + extraKeepAway = 0; lastStats = 0; maxSkipAhead = 7500; realTime = 1000; @@ -522,7 +523,7 @@ namespace Mist{ nxtKeyNum[trackId] = 0; } stats(true); - Util::wait(100); + playbackSleep(100); pageNum = pageNumForKey(trackId, keyNum); } @@ -752,7 +753,7 @@ namespace Mist{ continue; }//ignore missing tracks DTSC::Track & thisTrack = myMeta.tracks[*ti]; - if (thisTrack.lastms < seekPos+needsLookAhead+thisTrack.minKeepAway){good = false; break;} + if (thisTrack.lastms < seekPos+needsLookAhead+extraKeepAway+thisTrack.minKeepAway){good = false; break;} if (mainTrack == *ti){continue;}//skip self if (thisTrack.lastms == thisTrack.firstms){ HIGH_MSG("Skipping track %lu, last equals first", *ti); @@ -786,6 +787,16 @@ namespace Mist{ } } } + + /// Waits for the given amount of millis, increasing the realtime playback + /// related times as needed to keep smooth playback intact. + void Output::playbackSleep(uint64_t millis){ + if (realTime){ + firstTime += millis; + extraKeepAway += millis; + } + Util::wait(millis); + } int Output::run(){ DONTEVEN_MSG("MistOut client handler started"); @@ -821,7 +832,8 @@ namespace Mist{ uint32_t sleepTime = std::min((uint32_t)250, needsLookAhead); //wait at most double the look ahead time, plus ten seconds uint32_t timeoutTries = (needsLookAhead / sleepTime) * 2 + (10000/sleepTime); - uint64_t needsTime = thisPacket.getTime() + needsLookAhead; + uint64_t needsTime = thisPacket.getTime() + needsLookAhead; + bool firstTime = true; while(--timeoutTries && keepGoing()){ bool lookReady = true; for (std::set::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++){ @@ -834,7 +846,11 @@ namespace Mist{ } } if (lookReady){break;} - Util::wait(sleepTime); + if (firstTime){ + firstTime = false; + }else{ + playbackSleep(sleepTime); + } stats(); updateMeta(); } @@ -986,7 +1002,7 @@ namespace Mist{ //VoD might be slow, so we check VoD case also, just in case if (currKeyOpen.count(nxt.tid) && (currKeyOpen[nxt.tid] == (unsigned int)nextPage || nextPage == -1)){ if (++emptyCount < 100){ - Util::wait(250); + playbackSleep(250); //we're waiting for new data to show up if (emptyCount % 64 == 0){ reconnect();//reconnect every 16 seconds @@ -1073,7 +1089,7 @@ namespace Mist{ while(myMeta.live && counter < 40 && myMeta.tracks[nxt.tid].getKey(nxtKeyNum[nxt.tid]).getTime() != thisPacket.getTime()){ if (counter++){ //Only sleep 250ms if this is not the first updatemeta try - Util::wait(250); + playbackSleep(250); } updateMeta(); nxtKeyNum[nxt.tid] = getKeyForTime(nxt.tid, thisPacket.getTime()); diff --git a/src/output/output.h b/src/output/output.h index 21391cd7..cd83b61b 100644 --- a/src/output/output.h +++ b/src/output/output.h @@ -76,6 +76,7 @@ namespace Mist { virtual void onFail(); virtual void requestHandler(); static Util::Config * config; + void playbackSleep(uint64_t millis); private://these *should* not be messed with in child classes. std::map currKeyOpen; void loadPageForKey(long unsigned int trackId, long long int keyNum); @@ -90,6 +91,7 @@ namespace Mist { std::string UA; ///< User Agent string, if known. uint16_t uaDelay;///