package api import ( "os" "path/filepath" "strings" "time" "streamd/apps/streamd/internal/db" ) // stalePlaylistS is how old a playlist may get before the output counts as // down. It matches the supervisor's own staleness threshold. const stalePlaylistS = 45 // 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.IsSource() { v.Health = st.Health v.PlaylistAgeS = st.PlaylistAgeS continue } dirName := db.VariantDir(st.Name, v.ID, v.Height, v.AudioOnly) health, age := h.playlistHealth(filepath.Join(www, dirName, "index.m3u8"), st.Health) v.Health = health v.PlaylistAgeS = age } } // enrichLinks resolves every variant x output endpoint and stats each one, so // the dashboard can show that (say) HLS is fine while DASH has died. func (h *Handler) enrichLinks(st *db.Stream) { if st == nil { return } links := db.BuildPublishLinks(st.Name, st.VariantsJSON, st.OutputsJSON) www := strings.TrimSpace(h.WWWRoot) // Variant health computed above, to fall back on for outputs with no file // of their own (progressive TS is a live pipe, not a playlist). variantHealth := map[string]string{} for _, v := range st.Variants { variantHealth[v.ID] = v.Health } for i := range links { l := &links[i] if www == "" { continue } if l.Format == db.FormatTS { // No file to stat: the pipe is live exactly when its variant is. if hv, ok := variantHealth[l.VariantID]; ok { l.Health = hv } else { l.Health = st.Health } continue } rel := relFromPlayPath(l.PlayPath) if rel == "" { continue } l.Health, l.PlaylistAgeS = h.playlistHealth(filepath.Join(www, rel), st.Health) } st.Links = links } // playlistHealth reports a playlist's health from its mtime. A missing file // while the stream is coming up is "starting", not "down". func (h *Handler) playlistHealth(path, streamHealth string) (string, float64) { fi, err := os.Stat(path) if err != nil { if streamHealth == "ok" || streamHealth == "starting" { return "starting", 0 } return streamHealth, 0 } age := time.Since(fi.ModTime()).Seconds() if age > stalePlaylistS { return "down", age } return "ok", age } // relFromPlayPath turns a /hls/... URL path back into a path relative to the // www root. It returns "" for anything that escapes that root. func relFromPlayPath(p string) string { const prefix = "/hls/" if !strings.HasPrefix(p, prefix) { return "" } rel := strings.TrimPrefix(p, prefix) // These paths are built by us, but treat them as untrusted anyway: a // stream name is user input and must never climb out of www. clean := filepath.Clean(filepath.FromSlash(rel)) if clean == "." || strings.HasPrefix(clean, "..") || filepath.IsAbs(clean) { return "" } for _, seg := range strings.Split(filepath.ToSlash(clean), "/") { if seg == ".." { return "" } } return clean }