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).
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
"""Shared doubles for the session-stream web app suite.
|
|
|
|
Kept out of `conftest.py` on purpose: the rest of the suite imports its doubles
|
|
from a plain module (`tests/unit/helpers.py`), and `import conftest` by name is
|
|
fragile across directories.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pathlib
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
_HERE = pathlib.Path(__file__).resolve().parent
|
|
_SRC = _HERE.parents[1] / "src"
|
|
for _path in (str(_HERE), str(_SRC)):
|
|
if _path not in sys.path:
|
|
sys.path.insert(0, _path)
|
|
|
|
from fake_jpeg import jpeg_bytes # noqa: E402
|
|
|
|
#: The one phrase the suite enables. Shape matters, not the words.
|
|
PHRASE = "amber-otter-lantern-harbor-7k2qp"
|
|
|
|
|
|
class StubCapture:
|
|
"""A `Capture` under the test's control: counts grabs, can be told to fail."""
|
|
|
|
def __init__(self, frames=None, fail_first: int = 0, delay: float = 0.0):
|
|
self.frames = list(frames) if frames else [jpeg_bytes(i) for i in range(1, 50)]
|
|
self.fail_first = fail_first
|
|
self.delay = delay
|
|
self.calls = 0
|
|
self._lock = threading.Lock()
|
|
|
|
def grab(self) -> bytes:
|
|
with self._lock:
|
|
self.calls += 1
|
|
call = self.calls
|
|
if self.delay:
|
|
time.sleep(self.delay)
|
|
if call <= self.fail_first:
|
|
raise RuntimeError(f"stub capture failing on purpose (call {call})")
|
|
return self.frames[(call - 1 - self.fail_first) % len(self.frames)]
|
|
|
|
|
|
def read_parts(resp, count: int, timeout: float = 10.0) -> list[bytes]:
|
|
"""Read up to `count` JPEG parts out of a multipart MJPEG response."""
|
|
from hhstream_live.server import BOUNDARY
|
|
|
|
boundary = ("--" + BOUNDARY).encode("ascii")
|
|
parts: list[bytes] = []
|
|
buf = b""
|
|
deadline = time.monotonic() + timeout
|
|
while len(parts) < count and time.monotonic() < deadline:
|
|
chunk = resp.read(4096)
|
|
if not chunk:
|
|
break
|
|
buf += chunk
|
|
while True:
|
|
start = buf.find(boundary)
|
|
if start < 0:
|
|
break
|
|
header_end = buf.find(b"\r\n\r\n", start)
|
|
if header_end < 0:
|
|
break
|
|
header = buf[start:header_end].decode("latin-1")
|
|
length = None
|
|
for line in header.splitlines():
|
|
if line.lower().startswith("content-length:"):
|
|
length = int(line.split(":", 1)[1].strip())
|
|
if length is None:
|
|
break
|
|
body_start = header_end + 4
|
|
if len(buf) < body_start + length:
|
|
break
|
|
parts.append(buf[body_start:body_start + length])
|
|
buf = buf[body_start + length + 2:]
|
|
if len(parts) >= count:
|
|
break
|
|
return parts[:count]
|