From 57c055340496b338adc68a78bcf27c894d202157 Mon Sep 17 00:00:00 2001 From: Adam Waldron Date: Sat, 28 Sep 2024 00:30:39 -0700 Subject: [PATCH] chore: add latency estimate to demo --- scripts/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scripts/index.js b/scripts/index.js index 4b66316d6..ac0cf874a 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -296,6 +296,26 @@ return bufferedText; } + var estimateSegmentLatency = function(cue, currentTime) { + var playedMs = (currentTime - cue.value.start) * 1000; + var offsetDateTime = cue.value.programDateTime + playedMs; + + return (Date.now() - offsetDateTime) / 1000; + }; + + var updateSegmentLatency = function(segmentMetadata, cue, currentTime) { + // try and estimate the segment latency if we have program date time tags. + if (cue.value.programDateTime) { + var li = document.createElement('li'); + var latency = document.createElement('latency'); + + latency.textContent = 'Estimated segment latency: ' + estimateSegmentLatency(cue, currentTime); + + li.appendChild(latency); + segmentMetadata.appendChild(li); + } + }; + var setupSegmentMetadata = function(player) { // setup segment metadata var segmentMetadata = document.querySelector('#segment-metadata'); @@ -327,6 +347,7 @@ var li = document.createElement('li'); var pre = document.createElement('pre'); + updateSegmentLatency(segmentMetadata, cues[j], player.currentTime()); pre.classList.add('border', 'rounded', 'p-2'); pre.textContent = text; li.appendChild(pre);