// Shared pieces used by more than one view. import { el, pill, copyText, absURL, fmtAge, fmtDateTime, clear, append } from "./util.js"; /** A list of copyable publish endpoints, one row per variant x format. */ export function publishLinks(stream, opts = {}) { const links = Array.isArray(stream.links) ? stream.links : []; const box = el(`div.links${opts.compact ? ".compact" : ""}`); if (!links.length) { // A stream with no resolved links still has its canonical HLS path. const path = stream.play_path || `/hls/${stream.name}/index.m3u8`; box.append(linkRow({ output_label: "HLS", variant_label: "", play_path: path, health: stream.health }, opts)); return box; } const shown = opts.limit ? links.slice(0, opts.limit) : links; for (const l of shown) box.append(linkRow(l, opts)); if (opts.limit && links.length > opts.limit) { box.append(el("div.hint", { text: `+${links.length - opts.limit} more` })); } return box; } function linkRow(l, opts = {}) { const url = absURL(l.play_path); const label = l.master ? "MASTER" : [l.variant_label, l.output_label].filter(Boolean).join(" · "); const copyBtn = el("button.tiny.ghost", { text: "Copy", title: `Copy ${url}`, onclick: (ev) => copyText(url, label ? `${label} link` : "link", ev.currentTarget), }); return el(`div.link-row${l.master ? ".master" : ""}`, el("span.lab", { text: label || "HLS" }), el("span.path", { text: l.play_path, title: url }), !opts.compact && l.health ? pill(l.health) : null, copyBtn, ); } /** * Editor for the output ladder: any number of rungs at any height and * bitrate, which replaces the three hardcoded checkboxes. */ export function variantEditor(variants) { const rows = el("div"); const wrap = el("fieldset", el("legend", { text: "Quality ladder" }), el("p.hint", { text: "The source rung is always published as a straight copy. Each extra rung is " + "re-encoded, so every one you enable costs CPU.", }), el("div.col-head", el("span", { text: "On" }), el("span", { text: "Name" }), el("span", { text: "Height" }), el("span", { text: "Video" }), el("span", { text: "Audio" }), el("span", { text: "" }), ), rows, el("div.row", { style: { marginTop: "10px" } }, el("button.ghost.sm", { type: "button", text: "+ Add rung", onclick: () => rows.append(variantRow({ id: "", label: "", enabled: true, height: 480, video_bitrate: "1200k", audio_bitrate: "96k", })), }), el("button.ghost.sm", { type: "button", text: "+ Add audio-only", onclick: () => rows.append(variantRow({ id: "audio", label: "Audio", enabled: true, height: 0, audio_only: true, audio_bitrate: "96k", })), }), ), ); const list = (variants && variants.length ? variants : [{ id: "fhd", label: "FHD", enabled: true, height: 0 }]); for (const v of list) rows.append(variantRow(v)); wrap.readValue = () => Array.from(rows.children) .map((r) => r.readValue()) .filter((v) => v && (v.audio_only || v.height > 0 || v.id === "fhd")); return wrap; } function variantRow(v) { const isSource = !v.audio_only && (!v.height || v.height <= 0) && (v.id === "fhd" || !v.id); const on = el("input", { type: "checkbox", checked: isSource || v.enabled !== false, disabled: isSource }); const name = el("input", { type: "text", value: v.label || (v.id || "").toUpperCase(), placeholder: "HD", disabled: isSource, maxlength: "24", }); const height = el("input", { type: "number", value: v.height || 0, min: "0", max: "4320", step: "2", disabled: isSource || v.audio_only, }); const vbr = el("input", { type: "text", value: v.video_bitrate || "", placeholder: "2800k", disabled: isSource || v.audio_only, }); const abr = el("input", { type: "text", value: v.audio_bitrate || "", placeholder: "128k", disabled: isSource }); const row = el("div.vrow", on, isSource ? el("div", el("div.vname", { text: v.label || "FHD" }), el("div.locked-note", { text: "source · copy" })) : name, isSource ? el("span.locked-note", { text: "as source" }) : height, isSource ? el("span.locked-note", { text: "copy" }) : (v.audio_only ? el("span.locked-note", { text: "none" }) : vbr), abr, isSource ? el("span") : el("button.tiny.danger.del", { type: "button", text: "Remove", onclick: () => row.remove() }), ); row.readValue = () => { if (isSource) return { id: "fhd", label: "FHD", enabled: true, height: 0 }; const h = v.audio_only ? 0 : Math.max(0, parseInt(height.value, 10) || 0); const label = name.value.trim() || (v.audio_only ? "Audio" : `${h}p`); return { id: (v.id || label).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""), label, enabled: on.checked, height: h, audio_only: !!v.audio_only, video_bitrate: v.audio_only ? "" : vbr.value.trim(), audio_bitrate: abr.value.trim(), }; }; return row; } /** Editor for which formats a stream publishes. */ export function outputEditor(outputs) { const rows = el("div"); const wrap = el("fieldset", el("legend", { text: "Publish formats" }), el("p.hint", { text: "Every enabled rung is published in each enabled format. The formats share " + "one encode per rung, so adding a format is far cheaper than adding a rung.", }), el("div.col-head.out", el("span", { text: "On" }), el("span", { text: "Format" }), el("span", { text: "Segment s" }), el("span", { text: "Playlist len" }), ), rows, ); const list = outputs && outputs.length ? outputs : [ { id: "hls", label: "HLS", format: "hls", enabled: true }, { id: "cmaf", label: "CMAF", format: "hls_fmp4", enabled: false }, { id: "dash", label: "DASH", format: "dash", enabled: false }, { id: "ts", label: "TS", format: "mpegts", enabled: false }, ]; for (const o of list) rows.append(outputRow(o)); wrap.readValue = () => Array.from(rows.children).map((r) => r.readValue()); return wrap; } const FORMAT_BLURB = { hls: "index.m3u8 — MPEG-TS HLS, the broadest player support", hls_fmp4: "cmaf/index.m3u8 — fMP4 segments, better on Safari", dash: "dash/index.mpd — MPEG-DASH", mpegts: "/live/.ts — one endless URL, opens in VLC", }; function outputRow(o) { const on = el("input", { type: "checkbox", checked: !!o.enabled }); const seg = el("input", { type: "number", min: "0", max: "30", value: o.segment_duration || "", placeholder: "inherit" }); const len = el("input", { type: "number", min: "0", max: "500", value: o.list_size || "", placeholder: "inherit" }); const row = el("div.orow", on, el("div", el("div", { text: o.label || o.id }), el("div.fmt", { text: FORMAT_BLURB[o.format] || o.format }), ), seg, len, ); row.readValue = () => ({ id: o.id, label: o.label, format: o.format, enabled: on.checked, segment_duration: parseInt(seg.value, 10) || 0, list_size: parseInt(len.value, 10) || 0, }); return row; } /** * An HLS preview player using the vendored hls.js, with native fallback. * * @param {string|object} playPathOrOpts * string path, or { path, choices: [{ label, play_path }] } so the operator * can pick FHD / HD / SD (or ABR master) before hitting Play preview. */ export function player(playPathOrOpts) { const opts = typeof playPathOrOpts === "string" ? { path: playPathOrOpts, choices: [] } : (playPathOrOpts || {}); let playPath = opts.path || ""; const choices = Array.isArray(opts.choices) ? opts.choices.filter((c) => c && c.play_path) : []; if (!playPath && choices.length) playPath = choices[0].play_path; const video = el("video", { controls: true, playsinline: true, muted: true, preload: "none" }); const note = el("p.hint"); const pathHint = el("p.hint.mono"); setPathHint(playPath); let quality = null; if (choices.length > 1) { quality = el("select.sm", { title: "Preview quality", onchange: () => { playPath = quality.value; setPathHint(playPath); // If already playing, switch source immediately. if (hls || video.src) start(); }, }); for (const c of choices) { quality.append(el("option", { value: c.play_path, text: c.label || c.play_path, selected: c.play_path === playPath, })); } } const wrap = el("div.preview-player", el("div.player-wrap", video), el("div.row.preview-controls", { style: { marginTop: "8px" } }, quality ? el("label.row", { style: { gap: "6px", alignItems: "center" } }, el("span.muted", { text: "Quality" }), quality, ) : null, el("button.sm", { type: "button", text: "Play preview", onclick: start }), el("button.ghost.sm", { type: "button", text: "Stop", onclick: stop }), note, ), pathHint, ); let hls = null; function setPathHint(path) { pathHint.textContent = path ? `Playing ${path}` : ""; } async function start() { stop(); if (quality) playPath = quality.value; if (!playPath) { note.textContent = "no playlist to play"; return; } setPathHint(playPath); const url = absURL(playPath); // Safari plays HLS natively; everywhere else needs hls.js, which is // served from this host so the page CSP can stay default-src 'self'. if (video.canPlayType("application/vnd.apple.mpegurl")) { video.src = url; note.textContent = "native HLS"; video.play().catch((e) => (note.textContent = e.message)); return; } note.textContent = "loading player…"; try { const mod = await import("/static/vendor/hls.min.js"); const Hls = mod.default || window.Hls; if (!Hls || !Hls.isSupported()) { note.textContent = "this browser cannot play HLS — use the copied link in VLC"; return; } hls = new Hls({ lowLatencyMode: false, enableWorker: true }); hls.on(Hls.Events.ERROR, (_e, data) => { if (data && data.fatal) note.textContent = `player error: ${data.details || data.type}`; }); hls.loadSource(url); hls.attachMedia(video); note.textContent = "hls.js"; video.play().catch(() => { note.textContent = "ready — press play"; }); } catch (err) { note.textContent = `could not load the player: ${err.message}`; } } function stop() { if (hls) { hls.destroy(); hls = null; } video.removeAttribute("src"); video.load(); note.textContent = ""; } wrap.stop = stop; return wrap; } /** Build preview quality choices from a stream's publish links (HLS/CMAF). */ export function previewChoices(stream) { const links = Array.isArray(stream && stream.links) ? stream.links : []; const out = []; const seen = new Set(); for (const l of links) { if (!l || !l.play_path) continue; const fmt = String(l.format || "").toLowerCase(); // Prefer MPEG-TS HLS and fMP4 HLS; skip DASH/TS fanout for in-browser preview. if (fmt && fmt !== "hls" && fmt !== "hls_fmp4" && fmt !== "cmaf") continue; if (seen.has(l.play_path)) continue; seen.add(l.play_path); const label = l.master ? "ABR master" : [l.variant_label || l.variant_id, l.output_label].filter(Boolean).join(" · ") || l.play_path; out.push({ label, play_path: l.play_path, master: !!l.master }); } if (!out.length) { const path = (stream && stream.play_path) || (stream && stream.name ? `/hls/${stream.name}/index.m3u8` : ""); if (path) out.push({ label: "Source", play_path: path }); } return out; } /** A stream's event timeline. */ export function timeline(events) { const box = el("div.timeline"); if (!events || !events.length) { box.append(el("div.empty", { text: "Nothing recorded yet." })); return box; } for (const e of events) { const sev = e.severity === "error" ? "error" : e.severity === "warn" ? "warn" : "info"; box.append(el(`div.ev.${sev}`, el("span.at", { text: fmtDateTime(e.at), title: e.at }), pill(e.kind), el("span.msg", el("span", { text: e.message || "" }), e.actor && e.actor !== "system" ? el("span.who", { text: ` — ${e.actor}` }) : null, ), )); } return box; } /** A tag strip. */ export function tagList(tags) { const row = el("span.row", { style: { gap: "4px" } }); for (const t of String(tags || "").split(",").filter(Boolean)) { row.append(pill(t, "tag")); } return row; } /** Health summary for a stream row, including the staleness age. */ export function healthCell(s) { return el("div", pill(s.health || "stopped"), s.last_error ? el("div.sub", { text: s.last_error, title: s.last_error }) : null, s.playlist_age_s ? el("div.sub", { text: `playlist ${fmtAge(s.playlist_age_s)} old` }) : null, ); } /** Replace a container's children with new nodes. */ export function replace(node, ...children) { clear(node); append(node, children); return node; }