Null-DRM-Official/apps/streamd/internal/db/variants.go
404errordeveloper 066e018749
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s
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
2026-10-09 20:57:43 +02:00

228 lines
6.2 KiB
Go

package db
import (
"encoding/json"
"fmt"
"strings"
)
// SourceVariantID is the id of the pass-through rung: the source copy
// published at /hls/<name>/index.m3u8.
const SourceVariantID = "fhd"
// VariantSpec is one configured output ladder rung for a stream.
// Height 0 (or id "fhd") is the source copy published at /hls/<name>/index.m3u8.
// Other heights are ffmpeg-scaled sibling playlists at /hls/<name>-<id>/index.m3u8.
// An AudioOnly rung is also height 0 but is a separate, re-encoded rung.
type VariantSpec struct {
ID string `json:"id"`
Label string `json:"label"`
Enabled bool `json:"enabled"`
Height int `json:"height"`
VideoBitrate string `json:"video_bitrate,omitempty"`
AudioBitrate string `json:"audio_bitrate,omitempty"`
// AudioOnly drops video entirely. It is checked before the height-zero
// source-rung rule, because an audio rung is height zero too and would
// otherwise be rewritten into the source copy.
AudioOnly bool `json:"audio_only,omitempty"`
}
// VariantStatus is VariantSpec plus live play path / health for the API/UI.
type VariantStatus struct {
VariantSpec
PlayPath string `json:"play_path"`
Health string `json:"health,omitempty"`
PlaylistAgeS float64 `json:"playlist_age_s,omitempty"`
}
// IsSource reports whether this rung is the pass-through copy of the input.
func (v VariantSpec) IsSource() bool {
return !v.AudioOnly && v.Height <= 0
}
// DefaultVariants returns the FHD + optional HD/SD/audio ladder with only FHD
// enabled.
func DefaultVariants() []VariantSpec {
return []VariantSpec{
{ID: SourceVariantID, Label: "FHD", Enabled: true, Height: 0},
{ID: "hd", Label: "HD", Enabled: false, Height: 720, VideoBitrate: "2800k", AudioBitrate: "128k"},
{ID: "sd", Label: "SD", Enabled: false, Height: 480, VideoBitrate: "1200k", AudioBitrate: "96k"},
{ID: "audio", Label: "Audio", Enabled: false, AudioOnly: true, AudioBitrate: "96k"},
}
}
// ParseVariantsJSON reads variants_json from the DB. Empty → defaults (FHD only on).
func ParseVariantsJSON(raw string) []VariantSpec {
raw = strings.TrimSpace(raw)
if raw == "" || raw == "[]" || raw == "null" {
return DefaultVariants()
}
var list []VariantSpec
if err := json.Unmarshal([]byte(raw), &list); err != nil || len(list) == 0 {
return DefaultVariants()
}
return NormalizeVariants(list)
}
// NormalizeVariants fills ids/labels/bitrates and ensures a source entry exists.
func NormalizeVariants(in []VariantSpec) []VariantSpec {
out := make([]VariantSpec, 0, len(in))
seen := map[string]bool{}
hasSource := false
for _, v := range in {
v.ID = slug(v.ID)
// An audio rung is resolved first: it is height zero like the source
// copy, and the source rule below would otherwise swallow it.
if v.AudioOnly {
v.Height = 0
if v.ID == "" || v.ID == SourceVariantID {
v.ID = "audio"
}
if v.Label == "" {
v.Label = "Audio"
}
if v.AudioBitrate == "" {
v.AudioBitrate = "96k"
}
v.VideoBitrate = ""
if seen[v.ID] {
continue
}
seen[v.ID] = true
out = append(out, v)
continue
}
if v.ID == "" {
if v.Height <= 0 {
v.ID = SourceVariantID
} else {
v.ID = fmt.Sprintf("%dp", v.Height)
}
}
if seen[v.ID] {
continue
}
seen[v.ID] = true
if v.Label == "" {
v.Label = strings.ToUpper(v.ID)
}
if v.Height < 0 {
v.Height = 0
}
if v.Height == 0 {
hasSource = true
v.ID = SourceVariantID
if v.Label == "" || strings.EqualFold(v.Label, SourceVariantID) {
v.Label = "FHD"
}
v.Enabled = true // source copy always published when the stream runs
} else {
if v.VideoBitrate == "" {
switch {
case v.Height >= 1080:
v.VideoBitrate = "4500k"
case v.Height >= 720:
v.VideoBitrate = "2800k"
case v.Height >= 480:
v.VideoBitrate = "1200k"
default:
v.VideoBitrate = "800k"
}
}
if v.AudioBitrate == "" {
if v.Height >= 720 {
v.AudioBitrate = "128k"
} else {
v.AudioBitrate = "96k"
}
}
}
out = append(out, v)
}
if !hasSource {
out = append([]VariantSpec{{ID: SourceVariantID, Label: "FHD", Enabled: true, Height: 0}}, out...)
}
return out
}
// EncodeVariantsJSON stores a normalized ladder.
func EncodeVariantsJSON(list []VariantSpec) (string, error) {
list = NormalizeVariants(list)
b, err := json.Marshal(list)
if err != nil {
return "[]", err
}
return string(b), nil
}
// VariantPlayPath is the HLS URL path for one variant of stream name.
func VariantPlayPath(streamName, variantID string, height int) string {
variantID = slug(variantID)
if height <= 0 || variantID == "" || variantID == SourceVariantID {
return "/hls/" + streamName + "/index.m3u8"
}
return "/hls/" + streamName + "-" + variantID + "/index.m3u8"
}
// ScaledVariants returns enabled rungs that need their own encoder: every
// non-source rung, including audio-only.
func ScaledVariants(list []VariantSpec) []VariantSpec {
var out []VariantSpec
for _, v := range list {
if v.Enabled && !v.IsSource() {
out = append(out, v)
}
}
return out
}
// EnabledVariants returns every enabled rung, source included.
func EnabledVariants(list []VariantSpec) []VariantSpec {
var out []VariantSpec
for _, v := range list {
if v.Enabled {
out = append(out, v)
}
}
return out
}
// VariantWidth estimates a rung's pixel width for the ABR master playlist.
// The packager scales with scale=-2:<height>, so width follows the source
// aspect; 16:9 is the right assumption for broadcast content and the only
// thing available before the stream has been probed.
func VariantWidth(height int) int {
if height <= 0 {
return 0
}
w := height * 16 / 9
// H.264 needs even dimensions, which is what -2 in the scale filter gives.
if w%2 == 1 {
w++
}
return w
}
// BitrateBps parses an ffmpeg bitrate string such as "2800k" into bits per
// second, for the BANDWIDTH attribute in the master playlist.
func BitrateBps(s string) int {
s = strings.TrimSpace(strings.ToLower(s))
if s == "" {
return 0
}
mult := 1
switch {
case strings.HasSuffix(s, "k"):
mult, s = 1000, strings.TrimSuffix(s, "k")
case strings.HasSuffix(s, "m"):
mult, s = 1000000, strings.TrimSuffix(s, "m")
}
n := 0
for _, r := range s {
if r < '0' || r > '9' {
return 0
}
n = n*10 + int(r-'0')
}
return n * mult
}