Files
dropshell-templates/squashkiwi-streaming/config/web/webrtc.html
Your Name a2c82d66d0
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 22s
Show 'Loading...' text when clicking play button on WebRTC player
2025-09-02 12:35:06 +12:00

299 lines
9.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SquashKiwi Court Stream - WebRTC</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
color: white;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
padding: 30px 0;
}
h1 {
font-size: 2.5rem;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.video-section {
background: rgba(255, 255, 255, 0.1);
border-radius: 12px;
overflow: hidden;
backdrop-filter: blur(10px);
margin-bottom: 20px;
}
.video-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
background: black;
}
.play-overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px 40px;
border-radius: 10px;
cursor: pointer;
font-size: 1.2rem;
z-index: 10;
display: none;
}
.play-overlay:hover {
background: rgba(0, 0, 0, 0.9);
}
video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.video-controls {
padding: 15px;
background: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.status-badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
background: rgba(255, 255, 255, 0.2);
border-radius: 20px;
font-size: 0.9rem;
font-weight: 500;
}
.status-badge.live {
background: rgba(76, 175, 80, 0.3);
}
.status-badge.offline {
background: rgba(244, 67, 54, 0.3);
}
.btn {
padding: 8px 16px;
background: rgba(255, 255, 255, 0.2);
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
font-size: 0.85rem;
}
.btn:hover {
background: rgba(255, 255, 255, 0.3);
}
.info-section {
background: rgba(255, 255, 255, 0.1);
border-radius: 12px;
padding: 20px;
backdrop-filter: blur(10px);
margin-bottom: 20px;
}
.switch-link {
text-align: center;
padding: 10px;
}
.switch-link a {
color: white;
text-decoration: none;
background: rgba(255, 255, 255, 0.2);
padding: 8px 16px;
border-radius: 4px;
display: inline-block;
}
.switch-link a:hover {
background: rgba(255, 255, 255, 0.3);
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>🎾 SquashKiwi Court Stream (WebRTC)</h1>
</header>
<div class="video-section">
<div class="video-container">
<video id="video" controls autoplay muted playsinline></video>
<div id="playOverlay" class="play-overlay">▶️ Click to Play</div>
</div>
<div class="video-controls">
<div class="status-badge offline" id="status">
<span>Connecting...</span>
</div>
<div>
<button class="btn" onclick="toggleFullscreen()">📺 Fullscreen</button>
<button class="btn" onclick="refreshStream()">🔄 Refresh</button>
</div>
</div>
</div>
<div class="switch-link">
<a href="/">Switch to HLS Player</a>
</div>
<div class="info-section">
<h2>WebRTC Stream</h2>
<p>This player uses WebRTC for lower latency streaming. It should work with H265/HEVC streams.</p>
</div>
</div>
<script>
const config = {
courtId: new URLSearchParams(window.location.search).get('court') || 'court_h264',
webrtcUrl: '/webrtc/'
};
let pc = null;
async function initPlayer() {
const video = document.getElementById('video');
pc = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
});
// Add transceiver for receiving video
pc.addTransceiver('video', {direction: 'recvonly'});
pc.addTransceiver('audio', {direction: 'recvonly'});
pc.ontrack = (event) => {
console.log('Got track:', event.track.kind);
video.srcObject = event.streams[0];
const playOverlay = document.getElementById('playOverlay');
// Ensure autoplay works
video.play().then(() => {
console.log('Video playback started automatically');
playOverlay.style.display = 'none';
}).catch(e => {
console.log('Autoplay was blocked, user interaction may be required:', e);
// Show play overlay
playOverlay.style.display = 'block';
// Add click handlers to both overlay and video
const startPlayback = () => {
playOverlay.innerHTML = 'Loading...';
video.play().then(() => {
playOverlay.style.display = 'none';
}).catch(e => {
playOverlay.innerHTML = '▶️ Click to Play';
console.error('Failed to start playback:', e);
});
video.onclick = null;
playOverlay.onclick = null;
};
video.onclick = startPlayback;
playOverlay.onclick = startPlayback;
});
updateStatus(true);
};
pc.oniceconnectionstatechange = () => {
console.log('ICE connection state:', pc.iceConnectionState);
if (pc.iceConnectionState === 'disconnected' || pc.iceConnectionState === 'failed') {
updateStatus(false);
setTimeout(refreshStream, 5000);
}
};
// Create offer with ICE candidates
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// Wait for ICE gathering to complete
await new Promise((resolve) => {
if (pc.iceGatheringState === 'complete') {
resolve();
} else {
pc.onicegatheringstatechange = () => {
if (pc.iceGatheringState === 'complete') {
resolve();
}
};
// Timeout after 2 seconds
setTimeout(resolve, 2000);
}
});
// Send complete offer with ICE candidates to MediaMTX
try {
const response = await fetch(`${config.webrtcUrl}${config.courtId}/whep`, {
method: 'POST',
headers: {
'Content-Type': 'application/sdp'
},
body: pc.localDescription.sdp
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const answer = await response.text();
await pc.setRemoteDescription({
type: 'answer',
sdp: answer
});
console.log('WebRTC connection established');
} catch (error) {
console.error('WebRTC error:', error);
updateStatus(false);
setTimeout(refreshStream, 5000);
}
}
function updateStatus(live) {
const statusEl = document.getElementById('status');
if (live) {
statusEl.className = 'status-badge live';
statusEl.innerHTML = '<span>🔴 LIVE</span>';
} else {
statusEl.className = 'status-badge offline';
statusEl.innerHTML = '<span>⚫ OFFLINE</span>';
}
}
function toggleFullscreen() {
const video = document.getElementById('video');
if (!document.fullscreenElement) {
video.requestFullscreen().catch(err => {
console.error('Fullscreen error:', err);
});
} else {
document.exitFullscreen();
}
}
function refreshStream() {
if (pc) {
pc.close();
}
initPlayer();
}
// Initialize player on load
initPlayer();
</script>
</body>
</html>