package supervisor import ( "strings" "streamd/app" "streamd/apps/streamd/internal/db" "streamd/modhost" "streamd/repo" ) // streamSettings are the downloader and packager knobs for one stream. They // come from the stream row first, then the stream's app module, then the // installation-wide settings, then neutral defaults — so the supervisor never // has to guess from an app name or a stream name. type streamSettings struct { VideoSelect string AudioSelect string Rewriter string LiveWait int TSReadyBytes int64 // HLSTime and HLSListSize shape every output's segmenting. HLSTime int HLSListSize int // IsHLS drives packaging choices that depend on the manifest format. IsHLS bool } const ( defaultLiveWait = 2 defaultTSReadyBytes = 256 * 1024 // hlsTSReadyBytes buffers more before probing: an HLS pipe-mux often starts // with incomplete audio config. hlsTSReadyBytes = 2 * 1024 * 1024 hlsLiveWait = 6 ) // settingsFor resolves a stream's settings. globals is the installation-wide // layer, read from the settings table; pass the zero value when there is none. func settingsFor(st db.Stream, globals db.PackagerSettings) streamSettings { // Per-stream overrides win over everything. own := db.ParsePackagerSettings(st.SettingsJSON) s := streamSettings{ VideoSelect: strings.TrimSpace(st.VideoSelect), AudioSelect: strings.TrimSpace(st.AudioSelect), Rewriter: strings.TrimSpace(st.Rewriter), LiveWait: own.LiveWait, TSReadyBytes: int64(own.TSReadyBytes), HLSTime: own.HLSTime, HLSListSize: own.HLSListSize, IsHLS: isHLS(st), } if sd := streamDefaultsFor(st.App); sd != nil { if s.VideoSelect == "" { s.VideoSelect = sd.VideoSelect() } if s.AudioSelect == "" { s.AudioSelect = sd.AudioSelect() } if s.Rewriter == "" { s.Rewriter = sd.RewriterName() } if s.LiveWait == 0 { if n := sd.LiveWaitSeconds(); n > 0 { s.LiveWait = n } } if s.TSReadyBytes == 0 { if n := sd.TSReadyBytes(); n > 0 { s.TSReadyBytes = n } } } // Installation-wide settings. if s.LiveWait == 0 { s.LiveWait = globals.LiveWait } if s.TSReadyBytes == 0 { s.TSReadyBytes = int64(globals.TSReadyBytes) } if s.HLSTime == 0 { s.HLSTime = globals.HLSTime } if s.HLSListSize == 0 { s.HLSListSize = globals.HLSListSize } // An HLS source needs a longer wait and a bigger buffer before probing, // but only when nothing more specific asked otherwise. if s.IsHLS { if s.LiveWait == 0 { s.LiveWait = hlsLiveWait } if s.TSReadyBytes == 0 { s.TSReadyBytes = hlsTSReadyBytes } } // Neutral defaults. if s.LiveWait == 0 { s.LiveWait = defaultLiveWait } if s.TSReadyBytes == 0 { s.TSReadyBytes = defaultTSReadyBytes } if s.HLSTime == 0 { s.HLSTime = defaultHLSTime } if s.HLSListSize == 0 { s.HLSListSize = defaultHLSListSize } if s.VideoSelect == "" { s.VideoSelect = "for=best" } if s.AudioSelect == "" { s.AudioSelect = "for=best" } if s.Rewriter == "" { s.Rewriter = "none" } return s } // streamDefaultsFor prefers an external modules// provider; falls back to // any in-process app.Register (used by unit tests). func streamDefaultsFor(name string) app.StreamDefaults { if name == "" { return nil } if a, err := modhost.Open(repo.Root(), name); err == nil { return a } if a, err := app.Get(name); err == nil { if sd, ok := a.(app.StreamDefaults); ok { return sd } } return nil } // isHLS reports whether a stream's manifest is an HLS playlist, by URL shape. // No provider host is consulted: a module declares its own needs through // app.StreamDefaults. func isHLS(st db.Stream) bool { return strings.Contains(strings.ToLower(strings.TrimSpace(st.MPD)), ".m3u8") }