streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links - Embed dashboard screenshots in README; Linux streamd+agent setup guide - C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH - Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
This commit is contained in:
parent
b3955bd81e
commit
066e018749
113 changed files with 19002 additions and 1541 deletions
|
|
@ -14,12 +14,28 @@ import (
|
|||
|
||||
type Store struct {
|
||||
DB *sql.DB
|
||||
// hist buffers event and metric writes; see events.go.
|
||||
hist history
|
||||
}
|
||||
|
||||
// DRM mode values for Stream.DRMMode. "auto" infers from whether the stream
|
||||
// currently holds a key, so a clear channel is not treated as broken for
|
||||
// having none.
|
||||
const (
|
||||
DRMAuto = "auto"
|
||||
DRMYes = "yes"
|
||||
DRMNo = "no"
|
||||
)
|
||||
|
||||
// DefaultIdleTimeoutS is how long an on-demand stream runs with no viewers.
|
||||
const DefaultIdleTimeoutS = 300
|
||||
|
||||
type Stream struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Tags string `json:"tags"`
|
||||
App string `json:"app"`
|
||||
Channel string `json:"channel"`
|
||||
MPD string `json:"mpd"`
|
||||
|
|
@ -31,6 +47,12 @@ type Stream struct {
|
|||
Enabled bool `json:"enabled"`
|
||||
Rewriter string `json:"rewriter"`
|
||||
VariantsJSON string `json:"variants_json"` // FHD/HD/SD ladder config
|
||||
OutputsJSON string `json:"outputs_json"` // HLS/CMAF/DASH/TS output config
|
||||
SettingsJSON string `json:"settings_json"` // per-stream packager knobs
|
||||
DRMMode string `json:"drm_mode"` // auto | yes | no
|
||||
OnDemand bool `json:"on_demand"`
|
||||
IdleTimeoutS int `json:"idle_timeout_s"`
|
||||
Public bool `json:"public"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
|
||||
|
|
@ -45,42 +67,80 @@ type Stream struct {
|
|||
|
||||
// Variants is filled for API responses (play paths + optional live health).
|
||||
Variants []VariantStatus `json:"variants,omitempty"`
|
||||
// Outputs is the resolved output format list for API responses.
|
||||
Outputs []OutputStatus `json:"outputs,omitempty"`
|
||||
// Links is every playable endpoint (variant x output, plus the ABR
|
||||
// master), filled by the API layer which can see the files on disk.
|
||||
Links []PublishLink `json:"links,omitempty"`
|
||||
|
||||
ClaimedBy string `json:"claimed_by,omitempty"`
|
||||
ClaimExp string `json:"claim_expires_at,omitempty"`
|
||||
|
||||
// Derived, never stored. These exist so clients — above all the capture
|
||||
// agent — can reason about credentials without being handed the secrets.
|
||||
HasKey bool `json:"has_key"`
|
||||
HasHeaders bool `json:"has_headers"`
|
||||
HasProxy bool `json:"has_proxy"`
|
||||
KeyValid bool `json:"key_valid"`
|
||||
// DRMRequired is DRMMode resolved: whether this stream needs keys at all.
|
||||
DRMRequired bool `json:"drm_required"`
|
||||
// NeedsCapture is the server's verdict on whether the agent should fetch
|
||||
// fresh credentials. The agent must use this rather than inspecting keys,
|
||||
// which are redacted out of API responses.
|
||||
NeedsCapture bool `json:"needs_capture"`
|
||||
// Viewers is the live client count, filled by the HLS gate.
|
||||
Viewers int `json:"viewers,omitempty"`
|
||||
}
|
||||
|
||||
type CreateStream struct {
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
App string `json:"app"`
|
||||
Channel string `json:"channel"`
|
||||
MPD string `json:"mpd"`
|
||||
Key string `json:"key"`
|
||||
HeadersJSON string `json:"headers_json"`
|
||||
Proxy string `json:"proxy"`
|
||||
VideoSelect string `json:"video_select"`
|
||||
AudioSelect string `json:"audio_select"`
|
||||
Rewriter string `json:"rewriter"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
VariantsJSON string `json:"variants_json"`
|
||||
Variants []VariantSpec `json:"variants"`
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Tags string `json:"tags"`
|
||||
App string `json:"app"`
|
||||
Channel string `json:"channel"`
|
||||
MPD string `json:"mpd"`
|
||||
Key string `json:"key"`
|
||||
HeadersJSON string `json:"headers_json"`
|
||||
Proxy string `json:"proxy"`
|
||||
VideoSelect string `json:"video_select"`
|
||||
AudioSelect string `json:"audio_select"`
|
||||
Rewriter string `json:"rewriter"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
VariantsJSON string `json:"variants_json"`
|
||||
Variants []VariantSpec `json:"variants"`
|
||||
OutputsJSON string `json:"outputs_json"`
|
||||
Outputs []OutputSpec `json:"outputs"`
|
||||
SettingsJSON string `json:"settings_json"`
|
||||
DRMMode string `json:"drm_mode"`
|
||||
OnDemand *bool `json:"on_demand"`
|
||||
IdleTimeoutS *int `json:"idle_timeout_s"`
|
||||
Public *bool `json:"public"`
|
||||
}
|
||||
|
||||
type PatchStream struct {
|
||||
Title *string `json:"title"`
|
||||
App *string `json:"app"`
|
||||
Channel *string `json:"channel"`
|
||||
MPD *string `json:"mpd"`
|
||||
Key *string `json:"key"`
|
||||
HeadersJSON *string `json:"headers_json"`
|
||||
Proxy *string `json:"proxy"`
|
||||
VideoSelect *string `json:"video_select"`
|
||||
AudioSelect *string `json:"audio_select"`
|
||||
Rewriter *string `json:"rewriter"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
VariantsJSON *string `json:"variants_json"`
|
||||
Variants []VariantSpec `json:"variants"`
|
||||
Title *string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
Tags *string `json:"tags"`
|
||||
App *string `json:"app"`
|
||||
Channel *string `json:"channel"`
|
||||
MPD *string `json:"mpd"`
|
||||
Key *string `json:"key"`
|
||||
HeadersJSON *string `json:"headers_json"`
|
||||
Proxy *string `json:"proxy"`
|
||||
VideoSelect *string `json:"video_select"`
|
||||
AudioSelect *string `json:"audio_select"`
|
||||
Rewriter *string `json:"rewriter"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
VariantsJSON *string `json:"variants_json"`
|
||||
Variants []VariantSpec `json:"variants"`
|
||||
OutputsJSON *string `json:"outputs_json"`
|
||||
Outputs []OutputSpec `json:"outputs"`
|
||||
SettingsJSON *string `json:"settings_json"`
|
||||
DRMMode *string `json:"drm_mode"`
|
||||
OnDemand *bool `json:"on_demand"`
|
||||
IdleTimeoutS *int `json:"idle_timeout_s"`
|
||||
Public *bool `json:"public"`
|
||||
}
|
||||
|
||||
type Credentials struct {
|
||||
|
|
@ -156,10 +216,30 @@ CREATE TABLE IF NOT EXISTS agent_claims (
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Older DBs created before proxy / variants existed.
|
||||
_, _ = s.DB.Exec(`ALTER TABLE streams ADD COLUMN proxy TEXT NOT NULL DEFAULT ''`)
|
||||
_, _ = s.DB.Exec(`ALTER TABLE streams ADD COLUMN variants_json TEXT NOT NULL DEFAULT ''`)
|
||||
return nil
|
||||
// Columns added after the original schema. SQLite has no "ADD COLUMN IF
|
||||
// NOT EXISTS", so these are best-effort and fail harmlessly when the
|
||||
// column is already there.
|
||||
for _, stmt := range []string{
|
||||
`ALTER TABLE streams ADD COLUMN proxy TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN variants_json TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN description TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN tags TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN outputs_json TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN settings_json TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE streams ADD COLUMN drm_mode TEXT NOT NULL DEFAULT 'auto'`,
|
||||
`ALTER TABLE streams ADD COLUMN on_demand INTEGER NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE streams ADD COLUMN idle_timeout_s INTEGER NOT NULL DEFAULT 300`,
|
||||
`ALTER TABLE streams ADD COLUMN public INTEGER NOT NULL DEFAULT 1`,
|
||||
} {
|
||||
_, _ = s.DB.Exec(stmt)
|
||||
}
|
||||
if err := s.migrateHistory(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.migrateSettings(); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.migrateSchedules()
|
||||
}
|
||||
|
||||
func now() string {
|
||||
|
|
@ -185,9 +265,13 @@ func slug(name string) string {
|
|||
}
|
||||
|
||||
const streamSelect = `
|
||||
SELECT s.id, s.name, s.title, s.app, s.channel, s.mpd, s.key_kid_key, s.headers_json,
|
||||
SELECT s.id, s.name, s.title, COALESCE(s.description,''), COALESCE(s.tags,''),
|
||||
s.app, s.channel, s.mpd, s.key_kid_key, s.headers_json,
|
||||
COALESCE(s.proxy,''), s.video_select, s.audio_select, s.enabled, s.rewriter,
|
||||
COALESCE(s.variants_json,''), s.created_at, s.updated_at,
|
||||
COALESCE(s.variants_json,''), COALESCE(s.outputs_json,''), COALESCE(s.settings_json,''),
|
||||
COALESCE(s.drm_mode,'auto'), COALESCE(s.on_demand,0),
|
||||
COALESCE(s.idle_timeout_s,300), COALESCE(s.public,1),
|
||||
s.created_at, s.updated_at,
|
||||
COALESCE(r.health,''), COALESCE(r.pid,0), COALESCE(r.play_path,''),
|
||||
COALESCE(r.uptime_s,0), COALESCE(r.bitrate_mbps,0), COALESCE(r.playlist_age_s,0),
|
||||
COALESCE(r.last_error,''), COALESCE(r.updated_at,''),
|
||||
|
|
@ -218,16 +302,25 @@ func (s *Store) GetStream(id int64) (Stream, error) {
|
|||
return scanStream(row)
|
||||
}
|
||||
|
||||
// GetStreamByName resolves a stream by its slug name, used by the HLS gate.
|
||||
func (s *Store) GetStreamByName(name string) (Stream, error) {
|
||||
row := s.DB.QueryRow(streamSelect+` WHERE s.name = ?`, name)
|
||||
return scanStream(row)
|
||||
}
|
||||
|
||||
type rowScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
func scanStream(row rowScanner) (Stream, error) {
|
||||
var st Stream
|
||||
var enabled int
|
||||
var enabled, onDemand, public int
|
||||
err := row.Scan(
|
||||
&st.ID, &st.Name, &st.Title, &st.App, &st.Channel, &st.MPD, &st.Key, &st.HeadersJSON,
|
||||
&st.Proxy, &st.VideoSelect, &st.AudioSelect, &enabled, &st.Rewriter, &st.VariantsJSON,
|
||||
&st.ID, &st.Name, &st.Title, &st.Description, &st.Tags,
|
||||
&st.App, &st.Channel, &st.MPD, &st.Key, &st.HeadersJSON,
|
||||
&st.Proxy, &st.VideoSelect, &st.AudioSelect, &enabled, &st.Rewriter,
|
||||
&st.VariantsJSON, &st.OutputsJSON, &st.SettingsJSON,
|
||||
&st.DRMMode, &onDemand, &st.IdleTimeoutS, &public,
|
||||
&st.CreatedAt, &st.UpdatedAt,
|
||||
&st.Health, &st.PID, &st.PlayPath, &st.UptimeS, &st.BitrateMbps, &st.PlaylistAgeS,
|
||||
&st.LastError, &st.RuntimeAt, &st.ClaimedBy, &st.ClaimExp,
|
||||
|
|
@ -236,6 +329,11 @@ func scanStream(row rowScanner) (Stream, error) {
|
|||
return st, err
|
||||
}
|
||||
st.Enabled = enabled != 0
|
||||
st.OnDemand = onDemand != 0
|
||||
st.Public = public != 0
|
||||
if st.IdleTimeoutS <= 0 {
|
||||
st.IdleTimeoutS = DefaultIdleTimeoutS
|
||||
}
|
||||
if st.Health == "" {
|
||||
st.Health = "stopped"
|
||||
}
|
||||
|
|
@ -243,6 +341,7 @@ func scanStream(row rowScanner) (Stream, error) {
|
|||
st.PlayPath = "/hls/" + st.Name + "/index.m3u8"
|
||||
}
|
||||
st.Variants = AttachVariantStatuses(st.Name, st.VariantsJSON, st.Health, st.PlaylistAgeS)
|
||||
st.Outputs = AttachOutputStatuses(st.Name, st.OutputsJSON)
|
||||
// Hide expired claims in API responses (ExpireClaims cleans them shortly after).
|
||||
if st.ClaimExp != "" {
|
||||
if exp, e := time.Parse(time.RFC3339, st.ClaimExp); e == nil && !exp.After(time.Now().UTC()) {
|
||||
|
|
@ -250,9 +349,127 @@ func scanStream(row rowScanner) (Stream, error) {
|
|||
st.ClaimExp = ""
|
||||
}
|
||||
}
|
||||
st.fillDerived()
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// fillDerived computes the non-secret flags clients use in place of the
|
||||
// credentials themselves.
|
||||
func (st *Stream) fillDerived() {
|
||||
st.HasKey = strings.TrimSpace(st.Key) != ""
|
||||
st.KeyValid = ValidKeyPairs(st.Key)
|
||||
hdr := strings.TrimSpace(st.HeadersJSON)
|
||||
st.HasHeaders = hdr != "" && hdr != "{}" && hdr != "null"
|
||||
st.HasProxy = strings.TrimSpace(st.Proxy) != ""
|
||||
switch strings.ToLower(strings.TrimSpace(st.DRMMode)) {
|
||||
case DRMYes:
|
||||
st.DRMRequired = true
|
||||
case DRMNo:
|
||||
st.DRMRequired = false
|
||||
default:
|
||||
// auto: a stream that holds a key is encrypted; one that never had a
|
||||
// key is assumed clear, matching streamd's support for clear content.
|
||||
st.DRMMode = DRMAuto
|
||||
st.DRMRequired = st.HasKey
|
||||
}
|
||||
st.NeedsCapture = st.computeNeedsCapture()
|
||||
}
|
||||
|
||||
// Redact removes credentials from a Stream destined for an API response.
|
||||
// The derived flags survive, so a caller can still tell that a key exists and
|
||||
// whether it parses, without being handed the key itself.
|
||||
func (st *Stream) Redact() {
|
||||
st.Key = ""
|
||||
st.HeadersJSON = ""
|
||||
st.Proxy = ""
|
||||
}
|
||||
|
||||
// Redacted returns a copy with credentials stripped.
|
||||
func (st Stream) Redacted() Stream {
|
||||
st.Redact()
|
||||
return st
|
||||
}
|
||||
|
||||
// bootstrapGraceS is how long a stream may sit in "starting" before we treat
|
||||
// it as needing fresh credentials. It matches the supervisor's own bootstrap
|
||||
// deadline: capturing sooner would restart a stream that is still coming up,
|
||||
// which is a slow restart loop.
|
||||
const bootstrapGraceS = 180
|
||||
|
||||
// computeNeedsCapture reports whether an enabled stream looks down or is
|
||||
// missing the credentials it needs.
|
||||
func (st Stream) computeNeedsCapture() bool {
|
||||
if !st.Enabled {
|
||||
return false
|
||||
}
|
||||
// No manifest at all: only a capture can supply one.
|
||||
if strings.TrimSpace(st.MPD) == "" {
|
||||
return true
|
||||
}
|
||||
// Encrypted streams need usable keys. Clear streams do not, which is why
|
||||
// this is gated on DRMRequired rather than on the key being empty.
|
||||
if st.DRMRequired && !st.KeyValid {
|
||||
return true
|
||||
}
|
||||
// An on-demand stream that is deliberately idle is not broken. Its
|
||||
// credentials are refreshed when a viewer wakes it.
|
||||
if st.OnDemand && (st.Health == "stopped" || st.Health == "") {
|
||||
return false
|
||||
}
|
||||
switch st.Health {
|
||||
case "down", "stopped", "":
|
||||
return true
|
||||
case "starting":
|
||||
// Give the supervisor its bootstrap window before interfering.
|
||||
return st.UptimeS > bootstrapGraceS
|
||||
}
|
||||
return st.PlaylistAgeS > 30
|
||||
}
|
||||
|
||||
// ValidKeyPairs reports whether raw holds at least one KID:KEY pair and every
|
||||
// pair present is a 32-hex:32-hex pair. Separators may be newlines, commas or
|
||||
// semicolons, matching what the supervisor writes to NRE's key file — a
|
||||
// multi-KID stream is valid, which a single-pair check would wrongly reject.
|
||||
func ValidKeyPairs(raw string) bool {
|
||||
lines := SplitKeyLines(raw)
|
||||
if len(lines) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, line := range lines {
|
||||
kid, key, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if len(hexOnly(kid)) != 32 || len(hexOnly(key)) != 32 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// SplitKeyLines normalises a key field into one KID:KEY entry per line.
|
||||
func SplitKeyLines(raw string) []string {
|
||||
raw = strings.NewReplacer(",", "\n", ";", "\n").Replace(raw)
|
||||
var out []string
|
||||
for _, line := range strings.Split(raw, "\n") {
|
||||
if line = strings.TrimSpace(line); line != "" {
|
||||
out = append(out, line)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func hexOnly(s string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case r >= '0' && r <= '9', r >= 'a' && r <= 'f', r >= 'A' && r <= 'F':
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// AttachVariantStatuses builds the variants array for API responses.
|
||||
// Source (FHD) inherits the stream health; scaled variants stay "configured"
|
||||
// until the supervisor refreshes them via EnrichVariantHealth.
|
||||
|
|
@ -266,7 +483,7 @@ func AttachVariantStatuses(streamName, variantsJSON, streamHealth string, playli
|
|||
}
|
||||
if !v.Enabled {
|
||||
vs.Health = "off"
|
||||
} else if v.Height <= 0 {
|
||||
} else if v.Height <= 0 && !v.AudioOnly {
|
||||
vs.Health = streamHealth
|
||||
vs.PlaylistAgeS = playlistAge
|
||||
} else {
|
||||
|
|
@ -277,6 +494,17 @@ func AttachVariantStatuses(streamName, variantsJSON, streamHealth string, playli
|
|||
return out
|
||||
}
|
||||
|
||||
func normalizeDRMMode(s string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(s)) {
|
||||
case DRMYes:
|
||||
return DRMYes
|
||||
case DRMNo:
|
||||
return DRMNo
|
||||
default:
|
||||
return DRMAuto
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) CreateStream(in CreateStream) (Stream, error) {
|
||||
name := slug(in.Name)
|
||||
if name == "" {
|
||||
|
|
@ -288,38 +516,55 @@ func (s *Store) CreateStream(in CreateStream) (Stream, error) {
|
|||
if !json.Valid([]byte(in.HeadersJSON)) {
|
||||
return Stream{}, fmt.Errorf("headers_json must be valid JSON")
|
||||
}
|
||||
// Installation-wide defaults for on_demand / idle / outputs when the
|
||||
// caller left those fields unset. Explicit body values still win.
|
||||
s.ApplyCreateDefaults(&in)
|
||||
// video_select / audio_select / rewriter are left empty on purpose: the
|
||||
// supervisor resolves them from the stream's app module at start time, so no
|
||||
// provider's preferences are baked into the schema.
|
||||
if in.Title == "" {
|
||||
in.Title = name
|
||||
}
|
||||
variantsJSON := strings.TrimSpace(in.VariantsJSON)
|
||||
if len(in.Variants) > 0 {
|
||||
encoded, err := EncodeVariantsJSON(in.Variants)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
variantsJSON = encoded
|
||||
} else if variantsJSON == "" {
|
||||
variantsJSON, _ = EncodeVariantsJSON(DefaultVariants())
|
||||
} else {
|
||||
encoded, err := EncodeVariantsJSON(ParseVariantsJSON(variantsJSON))
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
variantsJSON = encoded
|
||||
variantsJSON, err := resolveVariantsJSON(in.VariantsJSON, in.Variants, true)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
outputsJSON, err := resolveOutputsJSON(in.OutputsJSON, in.Outputs, true)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
settingsJSON := strings.TrimSpace(in.SettingsJSON)
|
||||
if settingsJSON != "" && !json.Valid([]byte(settingsJSON)) {
|
||||
return Stream{}, fmt.Errorf("settings_json must be valid JSON")
|
||||
}
|
||||
enabled := 0
|
||||
if in.Enabled != nil && *in.Enabled {
|
||||
enabled = 1
|
||||
}
|
||||
onDemand := 0
|
||||
if in.OnDemand != nil && *in.OnDemand {
|
||||
onDemand = 1
|
||||
}
|
||||
// Public by default: that is today's behaviour, and an on-demand stream
|
||||
// that nobody may fetch could never be woken.
|
||||
public := 1
|
||||
if in.Public != nil && !*in.Public {
|
||||
public = 0
|
||||
}
|
||||
idle := DefaultIdleTimeoutS
|
||||
if in.IdleTimeoutS != nil && *in.IdleTimeoutS > 0 {
|
||||
idle = *in.IdleTimeoutS
|
||||
}
|
||||
ts := now()
|
||||
res, err := s.DB.Exec(`
|
||||
INSERT INTO streams(name,title,app,channel,mpd,key_kid_key,headers_json,proxy,video_select,audio_select,enabled,rewriter,variants_json,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
||||
name, in.Title, in.App, in.Channel, in.MPD, in.Key, in.HeadersJSON, strings.TrimSpace(in.Proxy),
|
||||
in.VideoSelect, in.AudioSelect, enabled, in.Rewriter, variantsJSON, ts, ts)
|
||||
INSERT INTO streams(name,title,description,tags,app,channel,mpd,key_kid_key,headers_json,proxy,
|
||||
video_select,audio_select,enabled,rewriter,variants_json,outputs_json,
|
||||
settings_json,drm_mode,on_demand,idle_timeout_s,public,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
||||
name, in.Title, strings.TrimSpace(in.Description), normalizeTags(in.Tags),
|
||||
in.App, in.Channel, in.MPD, in.Key, in.HeadersJSON, strings.TrimSpace(in.Proxy),
|
||||
in.VideoSelect, in.AudioSelect, enabled, in.Rewriter, variantsJSON, outputsJSON,
|
||||
settingsJSON, normalizeDRMMode(in.DRMMode), onDemand, idle, public, ts, ts)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
|
|
@ -329,6 +574,51 @@ VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|||
return s.GetStream(id)
|
||||
}
|
||||
|
||||
// resolveVariantsJSON normalises the ladder from either the typed list or the
|
||||
// raw JSON field. onCreate fills the default ladder when both are empty.
|
||||
func resolveVariantsJSON(raw string, list []VariantSpec, onCreate bool) (string, error) {
|
||||
if len(list) > 0 {
|
||||
return EncodeVariantsJSON(list)
|
||||
}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
if onCreate {
|
||||
return EncodeVariantsJSON(DefaultVariants())
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
return EncodeVariantsJSON(ParseVariantsJSON(raw))
|
||||
}
|
||||
|
||||
func resolveOutputsJSON(raw string, list []OutputSpec, onCreate bool) (string, error) {
|
||||
if len(list) > 0 {
|
||||
return EncodeOutputsJSON(list)
|
||||
}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
if onCreate {
|
||||
return EncodeOutputsJSON(DefaultOutputs())
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
return EncodeOutputsJSON(ParseOutputsJSON(raw))
|
||||
}
|
||||
|
||||
// normalizeTags lowercases and de-duplicates a comma-separated tag list.
|
||||
func normalizeTags(raw string) string {
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, t := range strings.Split(raw, ",") {
|
||||
t = slug(t)
|
||||
if t == "" || seen[t] {
|
||||
continue
|
||||
}
|
||||
seen[t] = true
|
||||
out = append(out, t)
|
||||
}
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
||||
func (s *Store) PatchStream(id int64, in PatchStream) (Stream, error) {
|
||||
cur, err := s.GetStream(id)
|
||||
if err != nil {
|
||||
|
|
@ -337,6 +627,12 @@ func (s *Store) PatchStream(id int64, in PatchStream) (Stream, error) {
|
|||
if in.Title != nil {
|
||||
cur.Title = *in.Title
|
||||
}
|
||||
if in.Description != nil {
|
||||
cur.Description = strings.TrimSpace(*in.Description)
|
||||
}
|
||||
if in.Tags != nil {
|
||||
cur.Tags = normalizeTags(*in.Tags)
|
||||
}
|
||||
if in.App != nil {
|
||||
cur.App = *in.App
|
||||
}
|
||||
|
|
@ -367,18 +663,50 @@ func (s *Store) PatchStream(id int64, in PatchStream) (Stream, error) {
|
|||
if in.Rewriter != nil {
|
||||
cur.Rewriter = *in.Rewriter
|
||||
}
|
||||
if len(in.Variants) > 0 {
|
||||
encoded, err := EncodeVariantsJSON(in.Variants)
|
||||
if in.DRMMode != nil {
|
||||
cur.DRMMode = normalizeDRMMode(*in.DRMMode)
|
||||
}
|
||||
if in.OnDemand != nil {
|
||||
cur.OnDemand = *in.OnDemand
|
||||
}
|
||||
if in.Public != nil {
|
||||
cur.Public = *in.Public
|
||||
}
|
||||
if in.IdleTimeoutS != nil && *in.IdleTimeoutS > 0 {
|
||||
cur.IdleTimeoutS = *in.IdleTimeoutS
|
||||
}
|
||||
if in.SettingsJSON != nil {
|
||||
v := strings.TrimSpace(*in.SettingsJSON)
|
||||
if v != "" && !json.Valid([]byte(v)) {
|
||||
return Stream{}, fmt.Errorf("settings_json must be valid JSON")
|
||||
}
|
||||
cur.SettingsJSON = v
|
||||
}
|
||||
if len(in.Variants) > 0 || in.VariantsJSON != nil {
|
||||
raw := ""
|
||||
if in.VariantsJSON != nil {
|
||||
raw = *in.VariantsJSON
|
||||
}
|
||||
encoded, err := resolveVariantsJSON(raw, in.Variants, false)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
cur.VariantsJSON = encoded
|
||||
} else if in.VariantsJSON != nil {
|
||||
encoded, err := EncodeVariantsJSON(ParseVariantsJSON(*in.VariantsJSON))
|
||||
if encoded != "" {
|
||||
cur.VariantsJSON = encoded
|
||||
}
|
||||
}
|
||||
if len(in.Outputs) > 0 || in.OutputsJSON != nil {
|
||||
raw := ""
|
||||
if in.OutputsJSON != nil {
|
||||
raw = *in.OutputsJSON
|
||||
}
|
||||
encoded, err := resolveOutputsJSON(raw, in.Outputs, false)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
cur.VariantsJSON = encoded
|
||||
if encoded != "" {
|
||||
cur.OutputsJSON = encoded
|
||||
}
|
||||
}
|
||||
enabled := 0
|
||||
if cur.Enabled {
|
||||
|
|
@ -391,12 +719,24 @@ func (s *Store) PatchStream(id int64, in PatchStream) (Stream, error) {
|
|||
enabled = 0
|
||||
}
|
||||
}
|
||||
onDemand := 0
|
||||
if cur.OnDemand {
|
||||
onDemand = 1
|
||||
}
|
||||
public := 0
|
||||
if cur.Public {
|
||||
public = 1
|
||||
}
|
||||
ts := now()
|
||||
_, err = s.DB.Exec(`
|
||||
UPDATE streams SET title=?, app=?, channel=?, mpd=?, key_kid_key=?, headers_json=?, proxy=?,
|
||||
video_select=?, audio_select=?, enabled=?, rewriter=?, variants_json=?, updated_at=? WHERE id=?`,
|
||||
cur.Title, cur.App, cur.Channel, cur.MPD, cur.Key, cur.HeadersJSON, cur.Proxy,
|
||||
cur.VideoSelect, cur.AudioSelect, enabled, cur.Rewriter, cur.VariantsJSON, ts, id)
|
||||
UPDATE streams SET title=?, description=?, tags=?, app=?, channel=?, mpd=?, key_kid_key=?,
|
||||
headers_json=?, proxy=?, video_select=?, audio_select=?, enabled=?, rewriter=?,
|
||||
variants_json=?, outputs_json=?, settings_json=?, drm_mode=?, on_demand=?,
|
||||
idle_timeout_s=?, public=?, updated_at=? WHERE id=?`,
|
||||
cur.Title, cur.Description, cur.Tags, cur.App, cur.Channel, cur.MPD, cur.Key,
|
||||
cur.HeadersJSON, cur.Proxy, cur.VideoSelect, cur.AudioSelect, enabled, cur.Rewriter,
|
||||
cur.VariantsJSON, cur.OutputsJSON, cur.SettingsJSON, cur.DRMMode, onDemand,
|
||||
cur.IdleTimeoutS, public, ts, id)
|
||||
if err != nil {
|
||||
return Stream{}, err
|
||||
}
|
||||
|
|
@ -554,21 +894,3 @@ ON CONFLICT(stream_id) DO UPDATE SET
|
|||
id, p.Health, p.PID, play, p.UptimeS, p.BitrateMbps, p.PlaylistAgeS, p.LastError, now())
|
||||
return err
|
||||
}
|
||||
|
||||
// NeedsCapture reports whether an enabled stream looks down / missing creds.
|
||||
func (st Stream) NeedsCapture() bool {
|
||||
if !st.Enabled {
|
||||
return false
|
||||
}
|
||||
if strings.TrimSpace(st.MPD) == "" || strings.TrimSpace(st.Key) == "" {
|
||||
return true
|
||||
}
|
||||
switch st.Health {
|
||||
case "down", "stopped", "starting", "":
|
||||
return true
|
||||
}
|
||||
if st.PlaylistAgeS > 30 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue