- 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
179 lines
4.7 KiB
Go
179 lines
4.7 KiB
Go
package db
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Setting keys. These are the installation-wide defaults that sit at the
|
|
// bottom of the resolution chain: stream row -> app module -> these -> the
|
|
// compiled-in defaults.
|
|
const (
|
|
SettingHLSTime = "hls_time"
|
|
SettingHLSListSize = "hls_list_size"
|
|
SettingLiveWait = "live_wait"
|
|
SettingTSReadyBytes = "ts_ready_bytes"
|
|
SettingIdleTimeoutS = "idle_timeout_s"
|
|
SettingDefaultOutputs = "default_outputs"
|
|
SettingOnDemand = "default_on_demand"
|
|
)
|
|
|
|
func (s *Store) migrateSettings() error {
|
|
_, err := s.DB.Exec(`
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL DEFAULT '',
|
|
updated_at TEXT NOT NULL DEFAULT ''
|
|
);`)
|
|
return err
|
|
}
|
|
|
|
// GetSettings returns every stored setting.
|
|
func (s *Store) GetSettings() (map[string]string, error) {
|
|
rows, err := s.DB.Query(`SELECT key, value FROM settings`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := map[string]string{}
|
|
for rows.Next() {
|
|
var k, v string
|
|
if err := rows.Scan(&k, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
out[k] = v
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// SetSettings upserts the given keys in one transaction. A key with an empty
|
|
// value is deleted, which returns it to the built-in default.
|
|
func (s *Store) SetSettings(in map[string]string) error {
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for k, v := range in {
|
|
k = strings.TrimSpace(k)
|
|
if k == "" {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(v) == "" {
|
|
if _, err := tx.Exec(`DELETE FROM settings WHERE key=?`, k); err != nil {
|
|
_ = tx.Rollback()
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO settings(key,value,updated_at) VALUES(?,?,?)
|
|
ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at`,
|
|
k, v, now()); err != nil {
|
|
_ = tx.Rollback()
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// PackagerSettings are the knobs that shape the ffmpeg command line. A zero
|
|
// field means "not set at this level", so the next level down applies.
|
|
type PackagerSettings struct {
|
|
HLSTime int `json:"hls_time,omitempty"`
|
|
HLSListSize int `json:"hls_list_size,omitempty"`
|
|
LiveWait int `json:"live_wait,omitempty"`
|
|
TSReadyBytes int `json:"ts_ready_bytes,omitempty"`
|
|
}
|
|
|
|
// ParsePackagerSettings reads a stream's settings_json. Unparseable JSON
|
|
// yields the zero value, which simply defers to the levels below.
|
|
func ParsePackagerSettings(raw string) PackagerSettings {
|
|
var p PackagerSettings
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" || raw == "null" {
|
|
return p
|
|
}
|
|
_ = json.Unmarshal([]byte(raw), &p)
|
|
return p.sane()
|
|
}
|
|
|
|
// sane drops nonsense values rather than passing them to ffmpeg, where a
|
|
// zero segment duration would produce an unplayable stream.
|
|
func (p PackagerSettings) sane() PackagerSettings {
|
|
if p.HLSTime < 1 || p.HLSTime > 30 {
|
|
p.HLSTime = 0
|
|
}
|
|
if p.HLSListSize < 1 || p.HLSListSize > 500 {
|
|
p.HLSListSize = 0
|
|
}
|
|
if p.LiveWait < 0 || p.LiveWait > 120 {
|
|
p.LiveWait = 0
|
|
}
|
|
if p.TSReadyBytes < 0 || p.TSReadyBytes > (256<<20) {
|
|
p.TSReadyBytes = 0
|
|
}
|
|
return p
|
|
}
|
|
|
|
// GlobalPackagerSettings reads the installation-wide packager defaults.
|
|
func (s *Store) GlobalPackagerSettings() PackagerSettings {
|
|
vals, err := s.GetSettings()
|
|
if err != nil {
|
|
return PackagerSettings{}
|
|
}
|
|
atoi := func(k string) int {
|
|
n, err := strconv.Atoi(strings.TrimSpace(vals[k]))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
return PackagerSettings{
|
|
HLSTime: atoi(SettingHLSTime),
|
|
HLSListSize: atoi(SettingHLSListSize),
|
|
LiveWait: atoi(SettingLiveWait),
|
|
TSReadyBytes: atoi(SettingTSReadyBytes),
|
|
}.sane()
|
|
}
|
|
|
|
// settingTruthy accepts the spellings operators realistically type into the
|
|
// settings form for boolean defaults.
|
|
func settingTruthy(v string) bool {
|
|
switch strings.ToLower(strings.TrimSpace(v)) {
|
|
case "1", "true", "yes", "on":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ApplyCreateDefaults fills CreateStream fields that the caller left unset
|
|
// from the installation-wide settings table. Explicit request fields win.
|
|
// Packager knobs (hls_time etc.) are resolved at start time, not at create.
|
|
func (s *Store) ApplyCreateDefaults(in *CreateStream) {
|
|
if in == nil {
|
|
return
|
|
}
|
|
vals, err := s.GetSettings()
|
|
if err != nil || len(vals) == 0 {
|
|
return
|
|
}
|
|
if in.OnDemand == nil {
|
|
if v, ok := vals[SettingOnDemand]; ok && strings.TrimSpace(v) != "" {
|
|
b := settingTruthy(v)
|
|
in.OnDemand = &b
|
|
}
|
|
}
|
|
if in.IdleTimeoutS == nil {
|
|
if v := strings.TrimSpace(vals[SettingIdleTimeoutS]); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
in.IdleTimeoutS = &n
|
|
}
|
|
}
|
|
}
|
|
if len(in.Outputs) == 0 && strings.TrimSpace(in.OutputsJSON) == "" {
|
|
if v := strings.TrimSpace(vals[SettingDefaultOutputs]); v != "" {
|
|
in.OutputsJSON = v
|
|
}
|
|
}
|
|
}
|