/* Afspelen. HLS (.m3u8) eerst via de ingebouwde speler van webOS, met hls.js als terugval. MPEG-TS via mpegts.js. Bij een fout wordt automatisch de andere methode geprobeerd. */ (function (App) { 'use strict'; var video, hls = null, ts = null, current = null, attempt = 0, stallTimer = null; function isHls(url) { return /\.m3u8(\?|$)/i.test(url); } function isTs(url) { return /\.ts(\?|$)/i.test(url) || /\/live\/[^/]+\/[^/]+\/\d+$/.test(url); } function teardown() { clearTimeout(stallTimer); if (hls) { try { hls.destroy(); } catch (e) { /* niets */ } hls = null; } if (ts) { try { ts.pause(); ts.unload(); ts.detachMediaElement(); ts.destroy(); } catch (e) { /* niets */ } ts = null; } video.removeAttribute('src'); try { video.load(); } catch (e) { /* niets */ } } /* Volgorde van methodes per soort stream */ function methods(url) { var nativeHls = !!video.canPlayType('application/vnd.apple.mpegurl'); var hlsJs = window.Hls && Hls.isSupported(); var tsJs = window.mpegts && mpegts.getFeatureList().mseLivePlayback; var list = []; if (isHls(url)) { if (nativeHls) list.push('native'); if (hlsJs) list.push('hlsjs'); } else if (isTs(url)) { if (tsJs) list.push('mpegts'); list.push('native'); } else { list.push('native'); if (hlsJs) list.push('hlsjs'); if (tsJs) list.push('mpegts'); } return list; } function start(method, url) { App.Player.method = method; if (method === 'native') { video.src = url; video.play().catch(function () { /* autoplay-weigering negeren; error-event volgt bij echte fouten */ }); } else if (method === 'hlsjs') { hls = new Hls({ lowLatencyMode: false, liveSyncDurationCount: 3, enableWorker: true }); hls.on(Hls.Events.ERROR, function (ev, data) { if (data.fatal) failed('hls.js: ' + data.details); }); hls.loadSource(url); hls.attachMedia(video); hls.on(Hls.Events.MANIFEST_PARSED, function () { video.play().catch(function () {}); }); } else if (method === 'mpegts') { ts = mpegts.createPlayer({ type: 'mpegts', isLive: true, url: url }, { enableWorker: false, lazyLoad: false, liveBufferLatencyChasing: true, liveBufferLatencyMaxLatency: 6 }); ts.on(mpegts.Events.ERROR, function (type, detail) { failed('mpegts: ' + type + ' ' + detail); }); ts.attachMediaElement(video); ts.load(); var p = ts.play(); if (p && p.catch) p.catch(function () {}); } // na 15 s nog geen beeld: volgende methode proberen clearTimeout(stallTimer); stallTimer = setTimeout(function () { if (video.readyState < 2) failed('geen beeld na 15 s'); }, 15000); } function failed(reason) { if (!current) return; console.warn('afspelen mislukt (' + App.Player.method + '): ' + reason); var list = methods(current.url); attempt++; teardown(); if (attempt < list.length) start(list[attempt], current.url); else { App.Player.method = null; App.showMessage('Deze zender speelt niet af.\n' + reason, 6000); } } App.Player = { method: null, init: function () { video = App.$('video'); video.addEventListener('error', function () { // alleen fouten van de huidige stream tellen (niet van een vorige die net is gestopt) if (App.Player.method === 'native' && current && video.getAttribute('src') === current.url) { failed('mediafout ' + (video.error ? video.error.code : '?')); } }); video.addEventListener('playing', function () { clearTimeout(stallTimer); App.hideMessage(); }); }, play: function (ch) { teardown(); current = ch; attempt = 0; var list = methods(ch.url); if (!list.length) { App.showMessage('Dit streamformaat wordt niet ondersteund.', 5000); return; } start(list[0], ch.url); }, stop: function () { current = null; teardown(); }, current: function () { return current; } }; })(window.App);