Files
hyprland-headless-stream-pl…/src/hhstream_live/pages.py
T
Hermenix 9744d35945 feat(live): one port, a router, and a phrase per session (stage 1)
The session-stream web app: a single HTTP listener (9988) that serves a page
per enabled session, keyed by a spoken phrase. Off by default, so a stream
exists only for the sessions it was enabled on.

  src/hhstream_live/phrase.py     four EFF-large words + a 5-char base-36 tail
                                  (~77.5 bits); the phrase is the password
  src/hhstream_live/publisher.py  one capture thread + one frame slot per
                                  output; one slot/no queue; capture paused at
                                  zero viewers
  src/hhstream_live/server.py     the router: /, /<phrase>/, /stream.mjpg,
                                  /frame.jpg, /status.json; unknown phrase is
                                  an empty 404, rate-limited
  src/hhstream_live/pages.py      the built-in dark pages (no CDN, no build)
  src/hhstream_live/__main__.py   hhstream-live serve|phrase, standalone

The root page is a form, never a list: the phrase is known only by whoever
enabled the stream. Transport is MJPEG (multipart/x-mixed-replace), reusing
the JPEG grim already produces. Not yet wired to the broker -- that is
stage 2. Tests: tests/live (51).
2026-10-06 13:32:00 +00:00

116 lines
4.3 KiB
Python

"""The pages the router serves: the landing form and the per-session viewer.
Everything here is one self-contained HTML string. No CDN, no framework, no
build step -- "webapp" means a router and legible pages, not a toolchain. The
viewer page is the one that matters: it is an ``<img>`` pointed at the MJPEG
route, because a top-level ``multipart/x-mixed-replace`` document is unreliable
on phones while an image tag is not.
The landing page is a **form, never a list**. The phrase is the password to a
stream and is known only by whoever enabled it, so the root page must not
enumerate sessions or leak phrases; it just hands the typed phrase to
``/<phrase>/``.
"""
from __future__ import annotations
import html
from functools import lru_cache
_STYLE = """
:root{color-scheme:dark}
*{box-sizing:border-box}
body{margin:0;background:#0b0d12;color:#e6e8ee;font:15px/1.5 ui-sans-serif,system-ui,sans-serif}
main{max-width:960px;margin:0 auto;padding:24px}
h1{font-size:20px;margin:0 0 4px}
a{color:#7cc4ff}
.muted{color:#8b93a7}
.card{background:#141821;border:1px solid #232838;border-radius:12px;padding:20px}
img{width:100%;height:auto;display:block;border-radius:8px;background:#000}
input{width:100%;padding:12px 14px;margin:10px 0 14px;border-radius:8px;
border:1px solid #2c3346;background:#0e121b;color:inherit;font:inherit}
button{padding:11px 16px;border:0;border-radius:8px;background:#3b82f6;color:#fff;
font:inherit;cursor:pointer}
button:hover{background:#2f6fd6}
.badge{display:inline-flex;align-items:center;gap:6px;font-size:13px;color:#8b93a7}
.dot{width:8px;height:8px;border-radius:50%;background:#3ddc84;box-shadow:0 0 8px #3ddc84}
.row{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:12px}
.tools{display:flex;gap:14px;font-size:13px;margin-top:12px}
"""
def _esc(text: str) -> str:
return html.escape(text, quote=True)
@lru_cache(maxsize=1)
def landing_page(app_name: str = "hhstream live") -> str:
"""The root page: a box to type a phrase into. No session list, ever."""
return f"""<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>{_esc(app_name)}</title><style>{_STYLE}</style>
</head><body><main>
<h1>{_esc(app_name)}</h1>
<p class="muted">A live view is opened with the phrase printed when its session
was enabled.</p>
<div class="card">
<label for="p">Session phrase</label>
<input id="p" autocomplete="off" autocapitalize="off" spellcheck="false"
placeholder="four-words-and-a-tail">
<button id="go">Open stream</button>
</div>
<script>
const input = document.getElementById('p');
function open() {{
const phrase = input.value.trim().toLowerCase();
if (phrase) location.href = '/' + encodeURIComponent(phrase) + '/';
}}
document.getElementById('go').addEventListener('click', open);
input.addEventListener('keydown', e => {{ if (e.key === 'Enter') open(); }});
input.focus();
</script>
</main></body></html>
"""
def viewer_page(phrase: str, label: str, output: str, fps: int) -> str:
"""The page you watch: an `<img>` on the stream, plus a small live readout."""
base = f"/{phrase}"
return f"""<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>{_esc(label)} — live</title><style>{_STYLE}</style>
</head><body><main>
<div class="row">
<div>
<h1>{_esc(label)}</h1>
<div class="muted">{_esc(output)} · {fps} fps</div>
</div>
<span class="badge"><span class="dot"></span><span id="v">live</span></span>
</div>
<div class="card">
<img id="stream" src="{base}/stream.mjpg" alt="live view of {_esc(label)}">
</div>
<div class="tools">
<a href="{base}/frame.jpg">snapshot</a>
<a href="{base}/status.json">status</a>
<a href="/">another phrase</a>
</div>
<script>
const out = document.getElementById('v');
async function tick() {{
try {{
const s = await (await fetch('{base}/status.json', {{cache:'no-store'}})).json();
out.textContent = s.viewers + (s.viewers === 1 ? ' viewer' : ' viewers') +
(s.last_frame_age_s == null ? '' : ' · ' + s.last_frame_age_s.toFixed(1) + 's ago');
}} catch (e) {{ out.textContent = 'offline'; }}
}}
tick(); setInterval(tick, 2000);
</script>
</main></body></html>
"""
__all__ = ["landing_page", "viewer_page"]