Spaces:
Running
Running
Rig live HLS relay (RAM only)
Browse files- Dockerfile +7 -0
- README.md +16 -4
- app.py +88 -0
Dockerfile
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
RUN pip install --no-cache-dir fastapi==0.115.6 uvicorn==0.32.1
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
WORKDIR /home/user/app
|
| 6 |
+
COPY --chown=user app.py .
|
| 7 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--no-access-log"]
|
README.md
CHANGED
|
@@ -1,10 +1,22 @@
|
|
| 1 |
---
|
| 2 |
-
title: Rig
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Rig live relay
|
| 3 |
+
emoji: 📡
|
| 4 |
+
colorFrom: gray
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Rig live relay
|
| 12 |
+
|
| 13 |
+
Relays the field rig's live camera (HLS) to the public ZIO site. The rig pushes
|
| 14 |
+
each 2 s segment and the playlist here; viewers pull them. The last ~48 s are
|
| 15 |
+
held in RAM and nothing is stored.
|
| 16 |
+
|
| 17 |
+
- `GET /stream.m3u8` — the live playlist (404 while the rig is not streaming)
|
| 18 |
+
- `GET /seg/<name>.ts` — a segment from the current window
|
| 19 |
+
- `PUT` on both — the rig only (`RELAY_TOKEN` Space secret)
|
| 20 |
+
|
| 21 |
+
Source: `ipad/FieldGlass/jetson/hf_live_relay/` in the AERIE repo. Deploy:
|
| 22 |
+
upload this folder to the Space and set the `RELAY_TOKEN` secret.
|
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Live HLS relay for the field rig — runs as a free Hugging Face Space.
|
| 2 |
+
|
| 3 |
+
The rig's hls_uploader.py PUTs each new 2 s segment and then the playlist; the
|
| 4 |
+
public site's player GETs them from here. Everything lives in RAM: the last
|
| 5 |
+
KEEP segments (about 48 s) and the current playlist. Nothing is written to disk
|
| 6 |
+
or to any storage service, so the stream costs nothing to host and leaves
|
| 7 |
+
nothing behind.
|
| 8 |
+
|
| 9 |
+
Env (Space secret): RELAY_TOKEN — the shared secret the uploader sends.
|
| 10 |
+
|
| 11 |
+
Clean-room: original code.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
import re
|
| 16 |
+
import time
|
| 17 |
+
from collections import OrderedDict
|
| 18 |
+
|
| 19 |
+
from fastapi import FastAPI, HTTPException, Request, Response
|
| 20 |
+
|
| 21 |
+
TOKEN = os.environ["RELAY_TOKEN"]
|
| 22 |
+
KEEP = 24 # segments held: the 6-segment window plus ~36 s for late players
|
| 23 |
+
STALE_S = 15.0 # no playlist for this long → the stream is down (404)
|
| 24 |
+
MAX_SEGMENT_BYTES = 16 * 1024 * 1024
|
| 25 |
+
MAX_PLAYLIST_BYTES = 64 * 1024
|
| 26 |
+
SEGMENT_NAME = re.compile(r"^[A-Za-z0-9_-]{1,80}\.ts$")
|
| 27 |
+
CORS = {"Access-Control-Allow-Origin": "*"}
|
| 28 |
+
|
| 29 |
+
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
|
| 30 |
+
segments: "OrderedDict[str, bytes]" = OrderedDict()
|
| 31 |
+
playlist: dict[str, object] = {"body": None, "beat": 0.0}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _check_token(req: Request) -> None:
|
| 35 |
+
if req.headers.get("authorization") != f"Bearer {TOKEN}":
|
| 36 |
+
raise HTTPException(status_code=401)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _live() -> bool:
|
| 40 |
+
return playlist["body"] is not None and time.monotonic() - float(playlist["beat"]) < STALE_S
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@app.get("/")
|
| 44 |
+
def status() -> dict:
|
| 45 |
+
return {"service": "rig live HLS relay", "live": _live(), "segments": len(segments)}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.put("/seg/{name}")
|
| 49 |
+
async def put_segment(name: str, req: Request) -> dict:
|
| 50 |
+
_check_token(req)
|
| 51 |
+
if not SEGMENT_NAME.match(name):
|
| 52 |
+
raise HTTPException(status_code=400, detail="bad segment name")
|
| 53 |
+
body = await req.body()
|
| 54 |
+
if not body or len(body) > MAX_SEGMENT_BYTES:
|
| 55 |
+
raise HTTPException(status_code=413)
|
| 56 |
+
segments[name] = body
|
| 57 |
+
segments.move_to_end(name)
|
| 58 |
+
while len(segments) > KEEP:
|
| 59 |
+
segments.popitem(last=False)
|
| 60 |
+
return {"ok": True}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@app.get("/seg/{name}")
|
| 64 |
+
def get_segment(name: str) -> Response:
|
| 65 |
+
body = segments.get(name)
|
| 66 |
+
if body is None:
|
| 67 |
+
return Response(status_code=404, headers=CORS)
|
| 68 |
+
# Names are unique per run, so a segment never changes once written.
|
| 69 |
+
return Response(body, media_type="video/mp2t", headers={**CORS, "Cache-Control": "public, max-age=60"})
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@app.put("/stream.m3u8")
|
| 73 |
+
async def put_playlist(req: Request) -> dict:
|
| 74 |
+
_check_token(req)
|
| 75 |
+
body = await req.body()
|
| 76 |
+
if len(body) > MAX_PLAYLIST_BYTES or not body.startswith(b"#EXTM3U"):
|
| 77 |
+
raise HTTPException(status_code=400, detail="not an HLS playlist")
|
| 78 |
+
playlist["body"] = body
|
| 79 |
+
playlist["beat"] = time.monotonic()
|
| 80 |
+
return {"ok": True}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@app.get("/stream.m3u8")
|
| 84 |
+
def get_playlist() -> Response:
|
| 85 |
+
headers = {**CORS, "Cache-Control": "no-store"}
|
| 86 |
+
if not _live():
|
| 87 |
+
return Response(status_code=404, headers=headers)
|
| 88 |
+
return Response(playlist["body"], media_type="application/vnd.apple.mpegurl", headers=headers)
|