streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links - Embed dashboard screenshots in README; Linux streamd+agent setup guide - C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH - Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
This commit is contained in:
parent
b3955bd81e
commit
066e018749
113 changed files with 19002 additions and 1541 deletions
331
apps/streamd/internal/ui/static/js/app.js
Normal file
331
apps/streamd/internal/ui/static/js/app.js
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
// Application shell: sign-in gate, hash router, chrome.
|
||||
|
||||
import { api, setCSRF, onAuthLost } from "./api.js";
|
||||
import { state, subscribe, connect, disconnect, refresh, setMe, isAdmin } from "./store.js";
|
||||
import { $, $$, el, clear, setText, toast, fmtSeconds } from "./util.js";
|
||||
import { dashboard } from "./views/dashboard.js";
|
||||
import { streams } from "./views/streams.js";
|
||||
import { streamView } from "./views/stream.js";
|
||||
import { scheduleView, settingsView, peopleView, auditView } from "./views/admin.js";
|
||||
|
||||
const app = $("#app");
|
||||
const gate = $("#gate");
|
||||
const viewRoot = $("#view");
|
||||
|
||||
let teardown = null;
|
||||
|
||||
// ---------- theme ----------
|
||||
|
||||
function showingDark() {
|
||||
const cur = document.documentElement.dataset.theme;
|
||||
if (cur) return cur === "dark";
|
||||
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
}
|
||||
|
||||
function paintThemeLabel() {
|
||||
const label = $("#theme-btn-label");
|
||||
if (!label) return;
|
||||
// Button names the mode you will switch *to*.
|
||||
setText(label, showingDark() ? "Light" : "Dark");
|
||||
}
|
||||
|
||||
function initTheme() {
|
||||
const saved = localStorage.getItem("streamd.theme");
|
||||
if (saved === "light" || saved === "dark") {
|
||||
document.documentElement.dataset.theme = saved;
|
||||
}
|
||||
paintThemeLabel();
|
||||
$("#theme-btn").addEventListener("click", () => {
|
||||
const next = showingDark() ? "light" : "dark";
|
||||
document.documentElement.dataset.theme = next;
|
||||
paintThemeLabel();
|
||||
try {
|
||||
localStorage.setItem("streamd.theme", next);
|
||||
} catch {
|
||||
// Private browsing; the choice simply will not persist.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- routes ----------
|
||||
|
||||
const ROUTES = [
|
||||
{ path: /^\/?$/, title: "Dashboard", nav: "/", mount: (root) => dashboard(root) },
|
||||
{ path: /^\/streams$/, title: "Streams", nav: "/streams", mount: (root) => streams(root) },
|
||||
{
|
||||
path: /^\/streams\/new$/, title: "Add stream", nav: "/streams",
|
||||
mount: (root) => import("./views/stream-form.js").then((m) => m.streamCreate(root)),
|
||||
},
|
||||
{
|
||||
path: /^\/stream\/(\d+)(?:\/(live|config|debug))?$/, title: "Stream", nav: "/streams",
|
||||
mount: (root, m) => streamView(root, Number(m[1]), m[2] || "live"),
|
||||
},
|
||||
{ path: /^\/schedule$/, title: "Schedule", nav: "/schedule", mount: (root) => scheduleView(root) },
|
||||
{ path: /^\/settings$/, title: "Settings", nav: "/settings", admin: true, mount: (root) => settingsView(root) },
|
||||
{ path: /^\/people$/, title: "People & keys", nav: "/people", admin: true, mount: (root) => peopleView(root) },
|
||||
{ path: /^\/audit$/, title: "Audit log", nav: "/audit", admin: true, mount: (root) => auditView(root) },
|
||||
];
|
||||
|
||||
function currentPath() {
|
||||
const h = location.hash.replace(/^#/, "");
|
||||
return h || "/";
|
||||
}
|
||||
|
||||
function route() {
|
||||
const path = currentPath();
|
||||
if (teardown) {
|
||||
try {
|
||||
teardown();
|
||||
} catch (err) {
|
||||
console.error("view teardown failed", err);
|
||||
}
|
||||
teardown = null;
|
||||
}
|
||||
clear(viewRoot);
|
||||
|
||||
for (const r of ROUTES) {
|
||||
const m = path.match(r.path);
|
||||
if (!m) continue;
|
||||
if (r.admin && !isAdmin()) {
|
||||
viewRoot.append(el("div.card",
|
||||
el("h2", { text: "Not available" }),
|
||||
el("p.hint", { text: "This page is administrator-only." }),
|
||||
));
|
||||
setText($("#page-title"), "Not available");
|
||||
return;
|
||||
}
|
||||
setText($("#page-title"), r.title);
|
||||
document.title = `${r.title} · streamd`;
|
||||
markNav(r.nav);
|
||||
viewRoot.classList.remove("view-enter");
|
||||
void viewRoot.offsetWidth;
|
||||
viewRoot.classList.add("view-enter");
|
||||
try {
|
||||
const mounted = r.mount(viewRoot, m);
|
||||
// Mount may return a teardown, or a Promise thereof (lazy create page).
|
||||
if (mounted && typeof mounted.then === "function") {
|
||||
mounted.then((fn) => { teardown = fn || null; }).catch((err) => {
|
||||
console.error(err);
|
||||
viewRoot.append(el("div.card", el("p.msg.err", { text: `This page failed to render: ${err.message}` })));
|
||||
});
|
||||
} else {
|
||||
teardown = mounted || null;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
viewRoot.append(el("div.card", el("p.msg.err", { text: `This page failed to render: ${err.message}` })));
|
||||
}
|
||||
app.classList.remove("nav-open");
|
||||
viewRoot.focus({ preventScroll: true });
|
||||
return;
|
||||
}
|
||||
|
||||
setText($("#page-title"), "Not found");
|
||||
markNav("");
|
||||
viewRoot.append(el("div.card",
|
||||
el("h2", { text: "Not found" }),
|
||||
el("p", el("a", { href: "#/", text: "Back to the dashboard" })),
|
||||
));
|
||||
}
|
||||
|
||||
function markNav(nav) {
|
||||
for (const a of $$("#nav a")) {
|
||||
a.classList.toggle("active", a.dataset.nav === nav);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- chrome ----------
|
||||
|
||||
function paintChrome() {
|
||||
const me = state.me || {};
|
||||
setText($("#who-name"), me.name || "");
|
||||
const role = $("#who-role");
|
||||
setText(role, me.role || "");
|
||||
role.className = `pill role`;
|
||||
|
||||
for (const a of $$("#nav a[data-admin]")) a.hidden = !isAdmin();
|
||||
|
||||
const conn = $("#conn");
|
||||
const live = !!state.connected;
|
||||
setText(conn, live ? "Live" : "Reconnecting");
|
||||
conn.classList.toggle("off", !live);
|
||||
conn.classList.toggle("warn", !live);
|
||||
conn.title = live ? "WebSocket connected" : "WebSocket reconnecting — data may be stale";
|
||||
|
||||
setText($("#uptime"), state.uptimeS ? `up ${fmtSeconds(state.uptimeS)}` : "");
|
||||
|
||||
const agents = state.agents || [];
|
||||
const agentVal = $("#agent-chip .chip-val") || $("#agent-chip");
|
||||
const agentIds = agents.map((a) => a.agent_id);
|
||||
setText(agentVal, agents.length ? agentIds.join(", ") : "none");
|
||||
$("#agent-chip").title = agents.length
|
||||
? agentIds.join("\n")
|
||||
: "No capture agents connected";
|
||||
|
||||
const d = state.deps;
|
||||
if (d) {
|
||||
const mark = (ok) => (ok ? "✓" : "✗");
|
||||
// /api/health gives paths to a signed-in caller and booleans otherwise.
|
||||
const has = (v) => (typeof v === "boolean" ? v : !!v);
|
||||
const chip = $("#deps-chip");
|
||||
const val = chip.querySelector(".chip-val") || chip;
|
||||
setText(val, `ffmpeg${mark(has(d.ffmpeg))} nre${mark(has(d.nre))} mp4decrypt${mark(has(d.mp4decrypt))}`);
|
||||
chip.title = [
|
||||
`ffmpeg: ${d.ffmpeg || "missing"}`,
|
||||
`N_m3u8DL-RE: ${d.nre || "missing"}`,
|
||||
`mp4decrypt: ${d.mp4decrypt || "missing"}`,
|
||||
d.bin_dir ? `bin: ${d.bin_dir}` : "",
|
||||
].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
if (state.lastError) toastOnce(state.lastError);
|
||||
}
|
||||
|
||||
let lastToast = "";
|
||||
function toastOnce(msg) {
|
||||
if (msg === lastToast) return;
|
||||
lastToast = msg;
|
||||
toast(msg, true);
|
||||
setTimeout(() => (lastToast = ""), 8000);
|
||||
}
|
||||
|
||||
// ---------- sign-in ----------
|
||||
|
||||
function showGate(opts = {}) {
|
||||
disconnect();
|
||||
app.hidden = true;
|
||||
gate.hidden = false;
|
||||
const form = $("#login-form");
|
||||
const setupRow = $("#setup-row");
|
||||
const needsSetup = !!opts.needsSetup;
|
||||
setupRow.hidden = !needsSetup;
|
||||
setupRow.querySelector("input").required = needsSetup;
|
||||
setText($("#gate-title"), needsSetup ? "Create the first administrator" : "Sign in to streamd");
|
||||
setText($("#gate-note"), needsSetup
|
||||
? "No accounts exist yet. Paste the one-time setup token from the streamd startup log."
|
||||
: "");
|
||||
setText($("#gate-submit"), needsSetup ? "Create account" : "Sign in");
|
||||
setText($("#gate-msg"), opts.message || "");
|
||||
$("#gate-msg").classList.toggle("err", !!opts.message);
|
||||
form.dataset.mode = needsSetup ? "setup" : "login";
|
||||
(needsSetup ? setupRow.querySelector("input") : form.username).focus();
|
||||
}
|
||||
|
||||
function showApp() {
|
||||
gate.hidden = true;
|
||||
app.hidden = false;
|
||||
connect();
|
||||
route();
|
||||
}
|
||||
|
||||
async function submitGate(ev) {
|
||||
ev.preventDefault();
|
||||
const form = ev.target;
|
||||
const msg = $("#gate-msg");
|
||||
setText(msg, "");
|
||||
msg.classList.remove("err");
|
||||
const btn = $("#gate-submit");
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const username = form.username.value.trim();
|
||||
const password = form.password.value;
|
||||
let res;
|
||||
if (form.dataset.mode === "setup") {
|
||||
res = await api.setup(form.setup_token.value.trim(), username, password);
|
||||
} else {
|
||||
res = await api.login(username, password);
|
||||
}
|
||||
if (!res || res.error) {
|
||||
throw new Error((res && res.error) || "sign-in failed");
|
||||
}
|
||||
setCSRF(res.csrf_token || "");
|
||||
// Prefer /api/me (includes csrf). If the Set-Cookie round-trip is slow or
|
||||
// blocked, fall back to the login/setup payload so the UI still advances.
|
||||
let me = await api.me();
|
||||
if (!me || !me.role) {
|
||||
if (res.user) {
|
||||
me = {
|
||||
name: res.user.username || username,
|
||||
role: res.user.role || "admin",
|
||||
actor_kind: "user",
|
||||
user_id: res.user.id,
|
||||
csrf_token: res.csrf_token,
|
||||
};
|
||||
} else if (res.role || res.name) {
|
||||
me = res;
|
||||
}
|
||||
}
|
||||
if (!me || !me.role) {
|
||||
throw new Error(
|
||||
"Account was created/accepted, but the browser session cookie was not established. " +
|
||||
"Allow cookies for this origin, then hard-refresh and sign in.",
|
||||
);
|
||||
}
|
||||
setMe(me);
|
||||
form.reset();
|
||||
showApp();
|
||||
await refresh("signed-in");
|
||||
} catch (err) {
|
||||
setText(msg, err.message || String(err));
|
||||
msg.classList.add("err");
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
try {
|
||||
await api.logout();
|
||||
} catch {
|
||||
// Signing out locally matters more than the request succeeding.
|
||||
}
|
||||
setCSRF("");
|
||||
setMe(null);
|
||||
showGate({ message: "Signed out." });
|
||||
}
|
||||
|
||||
// ---------- boot ----------
|
||||
|
||||
async function boot() {
|
||||
initTheme();
|
||||
subscribe(paintChrome);
|
||||
window.addEventListener("hashchange", route);
|
||||
$("#login-form").addEventListener("submit", submitGate);
|
||||
$("#logout-btn").addEventListener("click", logout);
|
||||
$("#nav-open").addEventListener("click", () => app.classList.add("nav-open"));
|
||||
$("#nav-close").addEventListener("click", () => app.classList.remove("nav-open"));
|
||||
app.addEventListener("click", (ev) => {
|
||||
// Tapping the scrim closes the mobile drawer.
|
||||
if (app.classList.contains("nav-open") && !ev.target.closest("#sidebar") && !ev.target.closest("#nav-open")) {
|
||||
app.classList.remove("nav-open");
|
||||
}
|
||||
});
|
||||
document.addEventListener("keydown", (ev) => {
|
||||
if (ev.key === "Escape") app.classList.remove("nav-open");
|
||||
});
|
||||
|
||||
// A session that expires while the page is open sends the viewer back to
|
||||
// sign-in rather than leaving a page full of failing requests.
|
||||
onAuthLost(() => {
|
||||
setMe(null);
|
||||
showGate({ message: "Your session expired. Sign in again." });
|
||||
});
|
||||
|
||||
try {
|
||||
const me = await api.me();
|
||||
if (me && me.role) {
|
||||
setCSRF(me.csrf_token || "");
|
||||
setMe(me);
|
||||
await refresh("boot");
|
||||
showApp();
|
||||
return;
|
||||
}
|
||||
// allow401 returns the 401 JSON body instead of throwing.
|
||||
showGate({ needsSetup: !!(me && me.needs_setup) });
|
||||
} catch (err) {
|
||||
const needsSetup = !!(err.body && err.body.needs_setup);
|
||||
showGate({ needsSetup });
|
||||
}
|
||||
}
|
||||
|
||||
boot();
|
||||
Loading…
Add table
Add a link
Reference in a new issue