Last active
July 10, 2023 19:24
-
-
Save Yogu/176fbfe5d5304c3899b011a0c9ebe5fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prints how long in the past the current "end" of the stream is | |
// this is a lower bound on latency that's viewer-independent | |
// put in the m3u of a stream | |
// e.g. just open network tab, refresh, then filter for m3u | |
// don't use the meta one (the first) but the one that's called repeatedly | |
const playlistUrl = ""; | |
async function test() { | |
const minNow = new Date().getTime(); | |
const res = await fetch(playlistUrl, { | |
headers: { | |
"accept": "application/x-mpegURL, application/vnd.apple.mpegurl, application/json, text/plain" | |
} | |
}); | |
const text = await res.text(); | |
const dateLine = text.split('\n').filter(l => l.includes('EXT-X-PROGRAM-DATE-TIME:')).at(-1); | |
const lengthLine = text.split('\n').filter(l => l.includes('EXTINF:')).at(-1) | |
const maxNow = new Date().getTime(); | |
const date = new Date(dateLine.substring(dateLine.indexOf(':') + 1)); | |
const pos = lengthLine.indexOf(':'); | |
const length = Number(lengthLine.substring(pos + 1, lengthLine.indexOf(','))) * 1000; | |
const minEndAge = minNow - date.getTime() - length; | |
const maxEndAge = maxNow - date.getTime() - length; | |
const timestamp = new Date().toISOString(); | |
return `${timestamp}: stream end is between ${(minEndAge/1000).toFixed(3)} and ${(maxEndAge/1000).toFixed(3)} s in the past`; | |
} | |
// run this a few times: | |
for (let i = 0; i < 200; i++) { setTimeout(() => { test().then(l => console.log(l))}, i * 20)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment