| |
| |
| let hls; |
| const video = document.getElementById("videoPlayer"); |
| const spinner = document.getElementById("spinner"); |
| const channelsContainer = document.getElementById("channelsContainer"); |
| const fileInput = document.getElementById("m3uFile"); |
| const tsInput = document.getElementById("tsFile"); |
| const localM3u8Input = document.getElementById("localM3u8File"); |
| |
| let channels = []; |
| let currentSelectedIndex = -1; |
|
|
| |
| document.addEventListener("DOMContentLoaded", () => { |
| const demoChannel = document.createElement("div"); |
| demoChannel.className = "channel"; |
| demoChannel.textContent = "Local Demo (TempVideo)"; |
| demoChannel.addEventListener("click", function() { |
| playChannel("/TempVideo/playlist.m3u8"); |
| currentSelectedIndex = channels.indexOf(demoChannel); |
| updateSelection(); |
| }); |
| channelsContainer.appendChild(demoChannel); |
| channels.push(demoChannel); |
| }); |
| |
| function showSpinner(show = true) { |
| spinner.style.display = show ? "flex" : "none"; |
| } |
| |
| function playChannel(streamUrl) { |
| console.log("Caricamento stream: " + streamUrl); |
| showSpinner(true); |
| |
| if (hls) { |
| hls.destroy(); |
| hls = null; |
| } |
| |
| if (Hls.isSupported()) { |
| hls = new Hls({ enableWorker: true }); |
| hls.loadSource(streamUrl); |
| hls.attachMedia(video); |
| hls.once(Hls.Events.MANIFEST_PARSED, () => { |
| video.play().then(() => { |
| showSpinner(false); |
| }).catch(err => { |
| console.error("Errore nel play:", err); |
| showSpinner(false); |
| }); |
| }); |
| hls.on(Hls.Events.ERROR, (event, data) => { |
| console.error("Errore HLS:", data); |
| showSpinner(false); |
| }); |
| } else if (video.canPlayType("application/vnd.apple.mpegurl")) { |
| video.src = streamUrl; |
| video.play().then(() => { |
| showSpinner(false); |
| }).catch(err => { |
| console.error("Errore nel play (nativo):", err); |
| showSpinner(false); |
| }); |
| } else { |
| alert("Il tuo browser non supporta lo streaming HLS."); |
| showSpinner(false); |
| } |
| } |
| |
| function parseChannelList(content) { |
| const lines = content.split("\n"); |
| channelsContainer.innerHTML = ""; |
| channels = []; |
| currentSelectedIndex = -1; |
| let currentTitle = ""; |
| lines.forEach(line => { |
| line = line.trim(); |
| if (!line) return; |
| if (line.startsWith("#EXTINF")) { |
| |
| const match = line.match(/,(.*)$/); |
| currentTitle = match ? match[1].trim() : "Canale IPTV"; |
| } else if (line.startsWith("http")) { |
| const streamUrl = line; |
| const channelDiv = document.createElement("div"); |
| channelDiv.className = "channel"; |
| channelDiv.textContent = currentTitle; |
| channelDiv.addEventListener("click", function() { |
| playChannel(streamUrl); |
| currentSelectedIndex = channels.indexOf(channelDiv); |
| updateSelection(); |
| }); |
| channelsContainer.appendChild(channelDiv); |
| channels.push(channelDiv); |
| } |
| }); |
| } |
| |
| |
| fileInput.addEventListener("change", function(event) { |
| const file = event.target.files[0]; |
| if (!file) return; |
| const reader = new FileReader(); |
| reader.onload = function(e) { |
| const content = e.target.result; |
| parseChannelList(content); |
| }; |
| reader.readAsText(file); |
| }); |
|
|
| |
| tsInput.addEventListener("change", function(event) { |
| const file = event.target.files[0]; |
| if (!file) return; |
|
|
| |
| if (hls) { |
| hls.destroy(); |
| hls = null; |
| } |
| |
| |
| const transmuxer = new muxjs.mp4.Transmuxer(); |
| const reader = new FileReader(); |
|
|
| reader.onload = function(e) { |
| const data = new Uint8Array(e.target.result); |
| |
| |
| const mime = 'video/mp4; codecs="avc1.42E01E,mp4a.40.2"'; |
| const mediaSource = new MediaSource(); |
| video.src = URL.createObjectURL(mediaSource); |
|
|
| mediaSource.addEventListener('sourceopen', function() { |
| const sourceBuffer = mediaSource.addSourceBuffer(mime); |
| |
| |
| transmuxer.on('data', (segment) => { |
| const data = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength); |
| data.set(segment.initSegment, 0); |
| data.set(segment.data, segment.initSegment.byteLength); |
| sourceBuffer.appendBuffer(data); |
| }); |
|
|
| |
| transmuxer.push(data); |
| transmuxer.flush(); |
| }); |
| |
| video.play().catch(console.error); |
| }; |
| |
| reader.readAsArrayBuffer(file); |
| }); |
|
|
| |
| localM3u8Input.addEventListener("change", function(event) { |
| const files = Array.from(event.target.files); |
| const m3u8File = files.find(f => f.name.endsWith('.m3u8')); |
| |
| if (!m3u8File) { |
| alert("No .m3u8 file found in the selected folder."); |
| return; |
| } |
|
|
| |
| if (hls) { |
| hls.destroy(); |
| hls = null; |
| } |
|
|
| const reader = new FileReader(); |
| reader.onload = function(e) { |
| const content = e.target.result; |
| const lines = content.split('\n'); |
| const tsFilenames = lines |
| .map(line => line.trim()) |
| .filter(line => line && !line.startsWith('#')); |
| |
| if (tsFilenames.length === 0) { |
| alert("No TS segments found in m3u8 file."); |
| return; |
| } |
|
|
| playLocalSegments(files, tsFilenames); |
| }; |
| reader.readAsText(m3u8File); |
| }); |
|
|
| async function playLocalSegments(allFiles, tsFilenames) { |
| const transmuxer = new muxjs.mp4.Transmuxer(); |
| const mime = 'video/mp4; codecs="avc1.42E01E,mp4a.40.2"'; |
| const mediaSource = new MediaSource(); |
| video.src = URL.createObjectURL(mediaSource); |
| |
| mediaSource.addEventListener('sourceopen', async function() { |
| const sourceBuffer = mediaSource.addSourceBuffer(mime); |
| |
| transmuxer.on('data', (segment) => { |
| const data = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength); |
| data.set(segment.initSegment, 0); |
| data.set(segment.data, segment.initSegment.byteLength); |
| |
| |
| if (!sourceBuffer.updating) { |
| try { |
| sourceBuffer.appendBuffer(data); |
| } catch (e) { |
| console.error("Buffer append error:", e); |
| } |
| } else { |
| |
| console.warn("Buffer updating, dropping frame for simplicity in demo"); |
| } |
| }); |
|
|
| |
| for (const filename of tsFilenames) { |
| const tsFile = allFiles.find(f => f.name === filename); |
| if (tsFile) { |
| const arrayBuffer = await tsFile.arrayBuffer(); |
| const data = new Uint8Array(arrayBuffer); |
| transmuxer.push(data); |
| transmuxer.flush(); |
| |
| await new Promise(r => setTimeout(r, 100)); |
| } else { |
| console.warn(`File ${filename} not found in selected folder.`); |
| } |
| } |
| |
| |
| if (mediaSource.readyState === 'open') { |
| mediaSource.endOfStream(); |
| } |
| }); |
|
|
| video.play().catch(console.error); |
| } |
| |
| function updateSelection() { |
| channels.forEach((channel, index) => { |
| if (index === currentSelectedIndex) { |
| channel.classList.add("selected"); |
| channel.scrollIntoView({ behavior: "smooth", block: "nearest" }); |
| } else { |
| channel.classList.remove("selected"); |
| } |
| }); |
| } |
| |
| |
| document.addEventListener("keydown", function(e) { |
| |
| if (e.key.toLowerCase() === "l") { |
| e.preventDefault(); |
| fileInput.click(); |
| return; |
| } |
| |
| |
| if (channels.length > 0) { |
| if (e.key === "ArrowDown") { |
| e.preventDefault(); |
| currentSelectedIndex = (currentSelectedIndex + 1) % channels.length; |
| updateSelection(); |
| return; |
| } |
| if (e.key === "ArrowUp") { |
| e.preventDefault(); |
| currentSelectedIndex = (currentSelectedIndex - 1 + channels.length) % channels.length; |
| updateSelection(); |
| return; |
| } |
| if (e.key === "Enter") { |
| e.preventDefault(); |
| if (currentSelectedIndex >= 0 && currentSelectedIndex < channels.length) { |
| channels[currentSelectedIndex].click(); |
| } |
| return; |
| } |
| } |
| |
| |
| if (e.key === " ") { |
| e.preventDefault(); |
| video.paused ? video.play() : video.pause(); |
| } else if (e.key === "+" || e.key === "=") { |
| e.preventDefault(); |
| video.volume = Math.min(video.volume + 0.1, 1); |
| } else if (e.key === "-") { |
| e.preventDefault(); |
| video.volume = Math.max(video.volume - 0.1, 0); |
| } else if (e.key.toLowerCase() === "m") { |
| e.preventDefault(); |
| video.muted = !video.muted; |
| } else if (e.key.toLowerCase() === "f") { |
| e.preventDefault(); |
| if (!document.fullscreenElement) { |
| video.requestFullscreen ? video.requestFullscreen() : (video.webkitRequestFullscreen && video.webkitRequestFullscreen()); |
| } else { |
| document.exitFullscreen ? document.exitFullscreen() : (document.webkitExitFullscreen && document.webkitExitFullscreen()); |
| } |
| } else if (e.key.toLowerCase() === "p") { |
| e.preventDefault(); |
| if (document.pictureInPictureElement) { |
| document.exitPictureInPicture().catch(err => console.error(err)); |
| } else { |
| video.requestPictureInPicture ? video.requestPictureInPicture().catch(err => console.error(err)) : null; |
| } |
| } |
| }); |
| |
| |
| const debounceDelay = 250; |
| |
| const debounceTimes = { |
| ArrowUp: 0, |
| ArrowDown: 0, |
| Enter: 0, |
| " ": 0, |
| m: 0, |
| f: 0, |
| p: 0, |
| l: 0, |
| |
| volUp: 0, |
| volDown: 0 |
| }; |
| |
| function simulateKeyEvent(key) { |
| const event = new KeyboardEvent("keydown", { key: key, bubbles: true }); |
| document.dispatchEvent(event); |
| } |
| |
| function pollGamepad() { |
| const gamepads = navigator.getGamepads ? navigator.getGamepads() : []; |
| if (gamepads[0]) { |
| const gp = gamepads[0]; |
| let now = Date.now(); |
| |
| if (gp.buttons[12] && gp.buttons[12].pressed) { |
| if (now - debounceTimes["ArrowUp"] > debounceDelay) { |
| simulateKeyEvent("ArrowUp"); |
| debounceTimes["ArrowUp"] = now; |
| } |
| } |
| |
| if (gp.buttons[13] && gp.buttons[13].pressed) { |
| if (now - debounceTimes["ArrowDown"] > debounceDelay) { |
| simulateKeyEvent("ArrowDown"); |
| debounceTimes["ArrowDown"] = now; |
| } |
| } |
| |
| if (gp.buttons[0] && gp.buttons[0].pressed) { |
| if (now - debounceTimes["Enter"] > debounceDelay) { |
| simulateKeyEvent("Enter"); |
| debounceTimes["Enter"] = now; |
| } |
| } |
| |
| if (gp.buttons[1] && gp.buttons[1].pressed) { |
| if (now - debounceTimes[" "] > debounceDelay) { |
| simulateKeyEvent(" "); |
| debounceTimes[" "] = now; |
| } |
| } |
| |
| if (gp.buttons[6] && gp.buttons[6].pressed) { |
| if (now - debounceTimes["volDown"] > debounceDelay) { |
| simulateKeyEvent("-"); |
| debounceTimes["volDown"] = now; |
| } |
| } |
| |
| if (gp.buttons[7] && gp.buttons[7].pressed) { |
| if (now - debounceTimes["volUp"] > debounceDelay) { |
| simulateKeyEvent("+"); |
| debounceTimes["volUp"] = now; |
| } |
| } |
| |
| if (gp.buttons[2] && gp.buttons[2].pressed) { |
| if (now - debounceTimes["m"] > debounceDelay) { |
| simulateKeyEvent("m"); |
| debounceTimes["m"] = now; |
| } |
| } |
| |
| if (gp.buttons[3] && gp.buttons[3].pressed) { |
| if (now - debounceTimes["f"] > debounceDelay) { |
| simulateKeyEvent("f"); |
| debounceTimes["f"] = now; |
| } |
| } |
| |
| if (gp.buttons[4] && gp.buttons[4].pressed) { |
| if (now - debounceTimes["l"] > debounceDelay) { |
| simulateKeyEvent("l"); |
| debounceTimes["l"] = now; |
| } |
| } |
| |
| if (gp.buttons[8] && gp.buttons[8].pressed) { |
| if (now - debounceTimes["p"] > debounceDelay) { |
| simulateKeyEvent("p"); |
| debounceTimes["p"] = now; |
| } |
| } |
| } |
| requestAnimationFrame(pollGamepad); |
| } |
| |
| window.addEventListener("gamepadconnected", function(e) { |
| console.log("Gamepad collegato:", e.gamepad); |
| }); |
| if (navigator.getGamepads) { |
| requestAnimationFrame(pollGamepad); |
| } |
| |
| video.addEventListener("playing", () => showSpinner(false)); |
| video.addEventListener("waiting", () => showSpinner(true)); |
|
|