Files
birdcam/src/templates/index.html

211 lines
5.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>Pi Camera</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: sans-serif;
background: #0f0f0f;
color: #f0f0f0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
gap: 1.5rem;
padding: 2rem;
}
h1 {
font-size: 1.4rem;
letter-spacing: 0.05em;
}
#preview {
width: 100%;
max-width: 720px;
aspect-ratio: 16/9;
background: #1a1a1a;
border: 1px solid #333;
border-radius: 8px;
overflow: hidden;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
#stream-video {
width: 100%;
height: 100%;
object-fit: cover;
display: none;
}
#placeholder {
color: #555;
font-size: 0.9rem;
}
.controls {
display: flex;
gap: 1rem;
align-items: center;
flex-wrap: wrap;
justify-content: center;
}
button {
padding: 0.6rem 2rem;
font-size: 1rem;
border: none;
border-radius: 6px;
cursor: pointer;
transition: opacity 0.2s;
}
button:disabled {
opacity: 0.35;
cursor: not-allowed;
}
#btn-start {
background: #22c55e;
color: #000;
}
#btn-stop {
background: #ef4444;
color: #fff;
}
#status {
font-size: 0.85rem;
color: #888;
min-height: 1.2em;
}
</style>
</head>
<body>
<h1>Pi Camera Stream</h1>
<div id="preview">
<span id="placeholder">Stream not started</span>
<video id="stream-video" autoplay muted playsinline></video>
</div>
<div class="controls">
<button id="btn-start">Start</button>
<button id="btn-stop" disabled>Stop</button>
</div>
<p id="status"></p>
<!-- hls.js from CDN -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@1/dist/hls.min.js"></script>
<script>
const btnStart = document.getElementById("btn-start");
const btnStop = document.getElementById("btn-stop");
const video = document.getElementById("stream-video");
const placeholder = document.getElementById("placeholder");
const status = document.getElementById("status");
let hls = null;
async function postAction(url) {
const res = await fetch(url, { method: "POST" });
return res.json();
}
async function waitForReady(maxAttempts = 40) {
for (let i = 0; i < maxAttempts; i++) {
const res = await fetch("/camera/status");
const data = await res.json();
if (data.ready) return true;
await new Promise(r => setTimeout(r, 500));
}
return false;
}
function startHls() {
const src = "/camera/hls/stream.m3u8";
if (Hls.isSupported()) {
hls = new Hls({
lowLatencyMode: false,
backBufferLength: 10,
});
hls.loadSource(src);
hls.attachMedia(video);
hls.on(Hls.Events.ERROR, (event, data) => {
if (data.fatal) {
console.error("HLS fatal error:", data);
status.textContent = "Stream error — try restarting";
}
});
} else if (video.canPlayType("application/vnd.apple.mpegurl")) {
// Safari native HLS support
video.src = src;
} else {
status.textContent = "HLS not supported in this browser";
return;
}
video.style.display = "block";
placeholder.style.display = "none";
video.play().catch(e => console.warn("Autoplay blocked:", e));
}
function stopHls() {
if (hls) {
hls.destroy();
hls = null;
}
video.pause();
video.src = "";
video.style.display = "none";
placeholder.style.display = "block";
}
btnStart.addEventListener("click", async () => {
btnStart.disabled = true;
status.textContent = "Starting camera…";
await postAction("/camera/start");
status.textContent = "Waiting for first segment…";
const ready = await waitForReady();
if (!ready) {
status.textContent = "Camera timed out — check Pi logs";
btnStart.disabled = false;
return;
}
startHls();
btnStop.disabled = false;
status.textContent = "Streaming (H.264 / HLS)";
});
btnStop.addEventListener("click", async () => {
btnStop.disabled = true;
status.textContent = "Stopping…";
stopHls();
await postAction("/camera/stop");
btnStart.disabled = false;
status.textContent = "Stream stopped";
});
</script>
</body>
</html>