
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiview</title>
    <style>
        /* Estilo para mostrar los videos en una cuadrícula que se adapta al tamaño del contenedor */
        .video-container {
            display: grid;
            grid-template-columns: repeat(4, 1fr); /* 5 columnas por defecto en pantallas grandes */
            gap: 2px;
            max-width: 100%;
            margin: 0px;
            width: 100%;
            height: 80vh; /* La altura de la ventana */
            padding: 0px;
            box-sizing: border-box;
        }

        /* Contenedor individual para cada video */
        .video-item {
            position: relative;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: normal;
            text-align: center;
            background-color: rgba(0, 0, 0, 0.5); /* Fondo semi-transparente */
            border-radius: 8px;
            overflow: hidden;
            height: 100%;
        }

        /* Estilo para centrar el texto dentro del cuadro */
        .video-item p {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: white;
            font-size: 18px;
            font-weight: bold;
            z-index: 10;
            transition: opacity 0.3s ease;
        }

        /* Hacer que los videos ocupen todo el espacio de su celda de la cuadrícula */
        video {
            width: 100%; /* Se adapta al ancho de la celda */
            height: auto; /* Mantiene la proporción */
            object-fit: cover; /* Asegura que el video cubra el área sin distorsionarse */
        }

        /* Botón para activar audio */
        .audio-control {
            position: absolute;
            bottom: 0px;
            background-color: rgba(0, 0, 0, 0.6);
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            z-index: 20;
            font-size: 14px;
        }

        /* Media queries para adaptar el número de columnas en diferentes tamaños de pantalla */
        @media (max-width: 1024px) {
            .video-container {
                grid-template-columns: repeat(3, 1fr); /* 3 columnas */
            }
        }

        @media (max-width: 600px) {
            .video-container {
                grid-template-columns: 1fr; /* 1 columna */
            }
        }
    </style>
</head>
<body style="background-color: #424242;font-family: sans-serif;">
    <div class="video-container" id="video-container"></div>

    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    <script>
        // Lista de URLs M3U8 (puedes agregar más si lo deseas)
        let m3u8Urls = [
             'http://host1:8080/live/truJillo573gtFRG75dfd.m3u8',
            'http://host1:8080/live/chiclayo548rtshe7555stujjUY.m3u8',
            'http://host1:8080/live/iCa5ertshe7rw5ssujsUgX.m3u8',
            'http://host1:8080/live/piuRaherfhedsdfgssjvsfaX.m3u8',
            'http://host1:8080/live/tacna97Ybg&3CdfRGkikJ.m3u8',
            'http://host1:8080/live/huAcho897G54JsmkUytdA.m3u8',
            'http://host1:8080/live/areqiPahsgsHgsddgGGkdhsg.m3u8',
            'http://host1:8080/live/huanCayo765GydhjhGFHJK876.m3u8',
            'http://host1:8080/live/iquiTosUYHBF56654FGhbvcxfg756.m3u8',
            'http://host1:8080/live/tarPoto866Fhs8754skjghqaS.m3u8',
            'http://host1:8080/live/chimboteU4Hve56dvghjGhb74356.m3u8',
            'http://host1:8080/live/CAjArca562009shgsYTjdqaS.m3u8'
        ];
         // Nombres personalizados para cada stream
        let streamNames = [
            'TRUJILLO',
            'CHICLAYO',
            'ICA',
            'PIURA',
            'TACNA',
            'HUACHO',
            'AREQUIPA',
            'HUANCAYO',
            'IQUITOS',
            'TARAPOTO',
            'CHIMBOTE',
            'CAJAMARCA'
        ];

        // Contenedor donde se agregarán los videos
        const container = document.getElementById("video-container");

        // Función para agregar los videos dinámicamente
        function loadVideos() {
            // Limpiar el contenedor de videos
            container.innerHTML = '';

            m3u8Urls.forEach((url, index) => {
                const videoItem = document.createElement("div");
                videoItem.classList.add("video-item");

                const videoElement = document.createElement("video");
                videoElement.id = `video${index + 1}`;
                videoElement.autoplay = true;
                videoElement.muted = true; // El video comienza sin audio
                videoElement.controls = false;

                const nameElement = document.createElement("p");
                nameElement.innerText =  streamNames[index];

                // Botón para activar el audio
                const audioButton = document.createElement("button");
                audioButton.innerText = "AUDIO";
                audioButton.classList.add("audio-control");
                audioButton.addEventListener("click", function() {
                    if (videoElement.muted) {
                        videoElement.muted = false; // Desactivar el mute cuando se haga clic
                        audioButton.innerText = "MUTE"; // Cambiar el texto del botón
                    } else {
                        videoElement.muted = true; // Activar el mute cuando se haga clic nuevamente
                        audioButton.innerText = "AUDIO"; // Cambiar el texto del botón
                    }
                });

                // Añadir el nombre, el video y el botón al contenedor
                videoItem.appendChild(nameElement);
                videoItem.appendChild(videoElement);
                videoItem.appendChild(audioButton);
                container.appendChild(videoItem);

                // Mostrar los nombres inicialmente
                nameElement.style.opacity = '1';

                // Ocultar nombre cuando el video comience a cargar o reproducirse
                videoElement.addEventListener('loadeddata', function() {
                    nameElement.style.opacity = '0'; // Desaparece el nombre cuando el video empieza a cargar
                });

                videoElement.addEventListener('play', function() {
                    nameElement.style.opacity = '0'; // Desaparece el nombre cuando el video comienza a reproducirse
                });

                // Si HLS.js es soportado por el navegador, usarlo
                if (Hls.isSupported()) {
                    const hls = new Hls();
                    hls.loadSource(url);
                    hls.attachMedia(videoElement);
                    hls.on(Hls.Events.MANIFEST_PARSED, function () {
                        videoElement.play();
                    });
                } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) {
                    videoElement.src = url; // Para navegadores como Safari que soportan HLS nativamente
                    videoElement.play();
                }
            });
        }

        // Cargar videos al inicio
        loadVideos();

        // Actualización automática de los streams cada 60 segundos (ajustable)
        setInterval(() => {
            // Aquí podrías actualizar m3u8Urls si tienes una fuente externa de URLs
            console.log('Recargando videos...');
            loadVideos();
        }, 60000); // 60000ms = 1 minuto
    </script>
</body>
</html>
