Files
xtremflow/web/player_lite.html
T

433 lines
15 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IPTV Player Lite</title>
<!-- HLS.js and mpegts.js for MPEG-TS support -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.4.12/dist/hls.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mpegts.js@1.7.3/dist/mpegts.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
background: #000;
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
#player-container {
width: 100%;
height: 100%;
position: relative;
}
#video {
width: 100%;
height: 100%;
background: #000;
}
#error {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
font-family: Arial, sans-serif;
}
#error-icon {
font-size: 64px;
color: #f44336;
margin-bottom: 16px;
}
#loading {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
font-family: Arial, sans-serif;
}
.spinner {
width: 48px;
height: 48px;
border: 4px solid #fff;
border-top-color: transparent;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 16px;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div id="player-container">
<!-- Native HTML5 controls enabled with 'controls' attribute -->
<video id="video" autoplay playsinline controls></video>
<div id="loading">
<div class="spinner"></div>
<div id="loading-text">Loading stream...</div>
</div>
<div id="error">
<div id="error-icon">⚠</div>
<div id="error-message">Failed to load stream</div>
</div>
</div>
<script>
const video = document.getElementById('video');
const loading = document.getElementById('loading');
const loadingText = document.getElementById('loading-text');
const error = document.getElementById('error');
const errorMessage = document.getElementById('error-message');
// Get parameters from query string
const urlParams = new URLSearchParams(window.location.search);
let streamUrl = urlParams.get('url');
const startTime = parseFloat(urlParams.get('t') || '0');
const streamType = urlParams.get('type') || ''; // 'live' or 'vod'
function log(msg) {
console.log('[LitePlayer] ' + msg);
}
function showLoading(msg) {
loading.style.display = 'block';
loadingText.textContent = msg || 'Loading stream...';
error.style.display = 'none';
}
function hideLoading() {
loading.style.display = 'none';
}
function showError(message) {
hideLoading();
error.style.display = 'block';
errorMessage.textContent = message || 'Failed to load stream';
}
// Seek to startTime when video is ready
video.addEventListener('loadedmetadata', () => {
if (startTime > 0 && startTime < video.duration) {
video.currentTime = startTime;
log('Resuming from ' + Math.floor(startTime / 60) + 'm ' + Math.floor(startTime % 60) + 's');
}
});
// Report position to parent for resume feature
let lastReportedTime = 0;
function reportPosition() {
if (video.currentTime > 0 && !video.paused && video.readyState > 2) {
if (Math.abs(video.currentTime - lastReportedTime) >= 5) {
lastReportedTime = video.currentTime;
window.parent.postMessage({
type: 'playback_position',
currentTime: video.currentTime,
duration: isFinite(video.duration) ? video.duration : 0
}, '*');
}
}
}
setInterval(reportPosition, 5000);
// Report on pause and end
video.addEventListener('pause', () => {
window.parent.postMessage({
type: 'playback_position',
currentTime: video.currentTime,
duration: isFinite(video.duration) ? video.duration : 0
}, '*');
});
video.addEventListener('ended', () => {
window.parent.postMessage({
type: 'playback_ended',
duration: isFinite(video.duration) ? video.duration : 0
}, '*');
});
// Report user activity (mouse movement or touch) to parent for auto-hide controls
let lastActivityTime = 0;
function reportActivity() {
const now = Date.now();
if (now - lastActivityTime > 100) { // Throttle to 100ms
lastActivityTime = now;
window.parent.postMessage({
type: 'user_activity'
}, '*');
}
}
document.addEventListener('mousemove', reportActivity);
document.addEventListener('touchstart', reportActivity);
document.addEventListener('touchmove', reportActivity);
document.addEventListener('click', reportActivity);
document.addEventListener('touchend', reportActivity);
async function playWithHLS(url) {
log('Trying HLS.js with: ' + url);
showLoading('Loading HLS stream...');
if (!Hls.isSupported()) {
if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = url;
video.addEventListener('canplay', () => {
hideLoading();
video.play().catch(e => log('Autoplay prevented: ' + e));
});
return true;
}
return false;
}
const hls = new Hls({
maxBufferLength: 30,
maxMaxBufferLength: 60,
liveSyncDurationCount: 3,
liveMaxLatencyDurationCount: 6,
liveDurationInfinity: true,
enableWorker: true
});
window.hlsInstance = hls;
hls.loadSource(url);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, () => {
log('HLS manifest parsed');
hideLoading();
video.play().catch(e => log('Autoplay prevented: ' + e));
});
hls.on(Hls.Events.ERROR, (event, data) => {
log('HLS Error: ' + data.type + ' - ' + data.details);
if (data.fatal) {
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) {
hls.startLoad();
} else if (data.type === Hls.ErrorTypes.MEDIA_ERROR) {
hls.recoverMediaError();
} else {
showError('HLS Error: ' + data.details);
hls.destroy();
}
}
});
return true;
}
async function playWithMpegTS(url, isLive = true) {
log('Trying mpegts.js with: ' + url + ' (isLive: ' + isLive + ')');
showLoading('Loading MPEG-TS stream...');
if (!mpegts.isSupported()) {
return false;
}
if (mpegts.getLoggingControl) {
mpegts.getLoggingControl().enableAll = false;
mpegts.getLoggingControl().enableError = true;
}
// Configure player differently for VOD vs Live
const playerConfig = {
enableWorker: true,
enableStashBuffer: !isLive, // Enable for VOD, disable for live
stashInitialSize: isLive ? 384 * 1024 : 512 * 1024,
autoCleanupSourceBuffer: true,
fixAudioTimestampGap: isLive, // Only for live streams
liveBufferLatencyChasing: isLive, // Disable for VOD
};
const player = mpegts.createPlayer({
type: 'mpegts',
isLive: isLive, // Use parameter to enable/disable seeking
url: url,
}, playerConfig);
player.attachMediaElement(video);
player.load();
video.addEventListener('canplay', () => {
log('MPEG-TS stream ready');
hideLoading();
video.play().catch(e => log('Autoplay prevented: ' + e));
});
video.addEventListener('error', () => {
showError('Video error: ' + (video.error?.message || 'Unknown error'));
});
player.on(mpegts.Events.ERROR, (errType, errDetail) => {
log('mpegts error: ' + errType + ' - ' + errDetail);
showError('Stream error: ' + errDetail);
});
return true;
}
async function playDirect(url) {
log('Trying direct playback: ' + url);
showLoading('Loading video...');
video.preload = 'auto';
let playStarted = false;
video.addEventListener('waiting', () => {
log('Buffering...');
showLoading('Buffering...');
});
video.addEventListener('playing', () => {
playStarted = true;
hideLoading();
});
const onCanPlay = () => {
playStarted = true;
hideLoading();
// FORCE AUDIO SETTINGS
video.muted = false;
video.volume = 1.0;
video.play().catch(e => {
log('Autoplay prevented: ' + e);
// If autoplay prevents sound/video, we should show a UI to user but native controls are there
// Try playing muted if failed
video.muted = true;
video.play().then(() => {
log('Autoplay started muted');
showError('Click to unmute'); // User can verify context
}).catch(e2 => log('Autoplay failed completely: ' + e2));
});
};
const onError = async () => {
if (!playStarted) {
log('Direct playback failed, trying fallbacks...');
video.removeEventListener('canplay', onCanPlay);
video.removeEventListener('error', onError);
// Fallback 1: HLS (common for streams)
log('Trying HLS fallback...');
if (await playWithHLS(url)) return;
// Fallback 2: MpegTS (for TS streams)
log('Comparing HLS failed, trying MpegTS fallback...');
if (await playWithMpegTS(url)) return;
showError('Format not supported. Try Standard player mode.');
}
};
video.addEventListener('canplay', onCanPlay, { once: true });
video.addEventListener('error', onError, { once: true });
video.src = url;
video.load();
return true;
}
async function initPlayer() {
if (!streamUrl) {
showError('No stream URL provided');
return;
}
log('Stream URL: ' + streamUrl);
log('Stream type: ' + streamType);
const lowerUrl = streamUrl.toLowerCase();
// SPECIAL: /api/stream/ endpoint handling - differentiate Live vs VOD
if (streamUrl.includes('/api/stream/')) {
if (streamType === 'live') {
// LIVE TV: Use MPEG-TS direct streaming
log('Live TV via /api/stream/ - using MPEG-TS direct');
showLoading('Connecting to live stream...');
await playWithMpegTS(streamUrl, true); // isLive=true
return;
} else {
// VOD: Fetch JSON to get HLS playlist URL (transcoding mode)
log('VOD via /api/stream/ - fetching HLS playlist...');
showLoading('Initializing stream (transcoding audio)...');
try {
const response = await fetch(streamUrl);
const data = await response.json();
if (data.playlist) {
const playlistUrl = window.location.origin + data.playlist;
log('Got HLS playlist: ' + playlistUrl);
showLoading('Loading HLS stream...');
await playWithHLS(playlistUrl);
return;
} else {
showError('Invalid stream response');
return;
}
} catch (err) {
log('Error fetching playlist: ' + err);
showError('Failed to initialize stream');
return;
}
}
}
// Use type parameter if available, otherwise detect from URL
if (streamType === 'vod') {
// VOD content - use direct HTML5 playback
log('VOD mode: Using direct playback');
await playDirect(streamUrl);
} else if (streamType === 'live') {
// Live TV - check for HLS first, then MPEG-TS
if (lowerUrl.includes('.m3u8') || lowerUrl.includes('/hls/')) {
log('Live HLS detected');
await playWithHLS(streamUrl);
} else {
log('Live MPEG-TS mode');
await playWithMpegTS(streamUrl);
}
} else {
// Fallback: detect from URL pattern
if (lowerUrl.includes('.m3u8') || lowerUrl.includes('/hls/')) {
await playWithHLS(streamUrl);
} else if (lowerUrl.includes('.mp4') || lowerUrl.includes('.mkv') ||
lowerUrl.includes('/movie/') || lowerUrl.includes('/series/')) {
await playDirect(streamUrl);
} else if (lowerUrl.includes('.ts') || lowerUrl.includes('/live/')) {
await playWithMpegTS(streamUrl);
} else {
await playDirect(streamUrl);
}
}
}
// Start player
initPlayer();
</script>
</body>
</html>