45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
(function(){
|
||
const name = document.documentElement.dataset.stream;
|
||
const v = document.getElementById('v');
|
||
const liveEl = document.getElementById('live');
|
||
const viewersEl = document.getElementById('viewers');
|
||
const srcEl = document.getElementById('hlssrc');
|
||
|
||
function updateLive(live){
|
||
liveEl.className = 'pill ' + (live ? 'live' : 'off');
|
||
liveEl.textContent = live ? 'LIVE' : 'Offline';
|
||
}
|
||
|
||
async function refresh(){
|
||
let data;
|
||
try{
|
||
const r = await fetch('/api/streams', { cache: 'no-store' });
|
||
if (!r.ok) throw new Error('api '+r.status);
|
||
data = await r.json();
|
||
}catch(e){
|
||
console.warn('streams api error:', e);
|
||
setLive(false);
|
||
viewersEl.textContent = 'Zuschauer: –';
|
||
return;
|
||
}
|
||
const r = await fetch('/api/streams');
|
||
const d = await r.json();
|
||
const it = d.items.find(x=>x.name===name);
|
||
const live = !!(it && it.live);
|
||
updateLive(live);
|
||
viewersEl.textContent = 'Zuschauer: ' + (it ? it.viewers : 0);
|
||
if(live){
|
||
const src = '/hls/'+encodeURIComponent(name);
|
||
srcEl.textContent = src;
|
||
if (window.Hls && Hls.isSupported()){
|
||
if(!window._hls){ window._hls = new Hls(); window._hls.attachMedia(v); }
|
||
window._hls.loadSource(src);
|
||
} else if (v.canPlayType('application/vnd.apple.mpegurl')) {
|
||
v.src = src;
|
||
}
|
||
}
|
||
}
|
||
|
||
refresh(); setInterval(refresh, 2500);
|
||
})();
|