streamd: copyable m3u8 links + FHD/HD/SD output variants
Some checks failed
release / build-and-upload (push) Failing after 3m33s

- WebUI Copy button for absolute m3u8 URLs (no player)
- Per-stream variants ladder; HD/SD live-transcoded as sibling HLS
- variants_json in DB/API; supervisor spawns scaled ffmpeg packagers
This commit is contained in:
404errordeveloper 2026-10-09 01:12:32 +02:00
parent ac8701b5a6
commit 3cb259c9b6
12 changed files with 913 additions and 62 deletions

View file

@ -25,6 +25,7 @@ type Handler struct {
Supervisor Supervisor
StartedAt time.Time
Deps deps.Status
WWWRoot string // data/www — used to report variant playlist health
}
func (h *Handler) Mount(mux *http.ServeMux) {
@ -83,6 +84,9 @@ func (h *Handler) streams(w http.ResponseWriter, r *http.Request) {
if list == nil {
list = []db.Stream{}
}
for i := range list {
h.enrichVariantHealth(&list[i])
}
writeJSON(w, http.StatusOK, map[string]any{"streams": list})
case http.MethodPost:
if !h.auth(w, r) {
@ -98,6 +102,7 @@ func (h *Handler) streams(w http.ResponseWriter, r *http.Request) {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
h.enrichVariantHealth(&st)
writeJSON(w, http.StatusCreated, st)
default:
writeErr(w, http.StatusMethodNotAllowed, "method not allowed")
@ -133,6 +138,7 @@ func (h *Handler) streamAction(w http.ResponseWriter, r *http.Request) {
writeErr(w, http.StatusNotFound, "not found")
return
}
h.enrichVariantHealth(&st)
writeJSON(w, http.StatusOK, st)
case action == "" && (r.Method == http.MethodPatch || r.Method == http.MethodPut):
@ -150,8 +156,8 @@ func (h *Handler) streamAction(w http.ResponseWriter, r *http.Request) {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
// Editing mpd/key/headers must tear down the live media worker and
// bring up a fresh instance with the new credentials.
// Editing mpd/key/headers/variants must tear down the live media worker
// and bring up a fresh instance with the new credentials.
if h.Supervisor != nil && credentialsChanged(before, st, in) {
if st.Enabled {
_ = h.Supervisor.Restart(id)
@ -160,6 +166,7 @@ func (h *Handler) streamAction(w http.ResponseWriter, r *http.Request) {
}
st, _ = h.Store.GetStream(id)
}
h.enrichVariantHealth(&st)
writeJSON(w, http.StatusOK, st)
case action == "" && r.Method == http.MethodDelete:
@ -315,8 +322,8 @@ func writeErr(w http.ResponseWriter, code int, msg string) {
writeJSON(w, code, map[string]any{"error": msg})
}
// credentialsChanged is true when the patch touched mpd, key, headers, or proxy
// that the live NRE/ffmpeg worker is already using.
// credentialsChanged is true when the patch touched mpd, key, headers, proxy,
// or variants that the live NRE/ffmpeg worker is already using.
func credentialsChanged(before, after db.Stream, in db.PatchStream) bool {
if in.MPD != nil && strings.TrimSpace(before.MPD) != strings.TrimSpace(after.MPD) {
return true
@ -330,5 +337,10 @@ func credentialsChanged(before, after db.Stream, in db.PatchStream) bool {
if in.Proxy != nil && strings.TrimSpace(before.Proxy) != strings.TrimSpace(after.Proxy) {
return true
}
if in.VariantsJSON != nil || len(in.Variants) > 0 {
if strings.TrimSpace(before.VariantsJSON) != strings.TrimSpace(after.VariantsJSON) {
return true
}
}
return false
}

View file

@ -0,0 +1,54 @@
package api
import (
"os"
"path/filepath"
"strings"
"time"
"drmdecryption/apps/streamd/internal/db"
)
// enrichVariantHealth updates each variant's health from on-disk HLS playlists.
func (h *Handler) enrichVariantHealth(st *db.Stream) {
if st == nil {
return
}
if len(st.Variants) == 0 {
st.Variants = db.AttachVariantStatuses(st.Name, st.VariantsJSON, st.Health, st.PlaylistAgeS)
}
www := strings.TrimSpace(h.WWWRoot)
if www == "" {
return
}
for i := range st.Variants {
v := &st.Variants[i]
if !v.Enabled {
v.Health = "off"
continue
}
if v.Height <= 0 {
v.Health = st.Health
v.PlaylistAgeS = st.PlaylistAgeS
continue
}
dirName := st.Name + "-" + v.ID
index := filepath.Join(www, dirName, "index.m3u8")
fi, err := os.Stat(index)
if err != nil {
if st.Health == "ok" || st.Health == "starting" {
v.Health = "starting"
} else {
v.Health = st.Health
}
continue
}
age := time.Since(fi.ModTime()).Seconds()
v.PlaylistAgeS = age
if age > 45 {
v.Health = "down"
} else {
v.Health = "ok"
}
}
}