streamd: preview quality picker, dashboard polish, C4 wrap keys
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s

- 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:
404errordeveloper 2026-10-09 20:57:43 +02:00
parent b3955bd81e
commit 066e018749
113 changed files with 19002 additions and 1541 deletions

View file

@ -6,9 +6,14 @@ import (
"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"`
@ -16,6 +21,10 @@ type VariantSpec struct {
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.
@ -26,12 +35,19 @@ type VariantStatus struct {
PlaylistAgeS float64 `json:"playlist_age_s,omitempty"`
}
// DefaultVariants returns the FHD + optional HD/SD ladder with only FHD enabled.
// 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: "fhd", Label: "FHD", Enabled: true, Height: 0},
{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"},
}
}
@ -48,16 +64,37 @@ func ParseVariantsJSON(raw string) []VariantSpec {
return NormalizeVariants(list)
}
// NormalizeVariants fills ids/labels/bitrates and ensures an FHD/source entry exists.
// 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 = "fhd"
v.ID = SourceVariantID
} else {
v.ID = fmt.Sprintf("%dp", v.Height)
}
@ -74,8 +111,8 @@ func NormalizeVariants(in []VariantSpec) []VariantSpec {
}
if v.Height == 0 {
hasSource = true
v.ID = "fhd"
if v.Label == "" || strings.EqualFold(v.Label, "fhd") {
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
@ -103,7 +140,7 @@ func NormalizeVariants(in []VariantSpec) []VariantSpec {
out = append(out, v)
}
if !hasSource {
out = append([]VariantSpec{{ID: "fhd", Label: "FHD", Enabled: true, Height: 0}}, out...)
out = append([]VariantSpec{{ID: SourceVariantID, Label: "FHD", Enabled: true, Height: 0}}, out...)
}
return out
}
@ -121,19 +158,71 @@ func EncodeVariantsJSON(list []VariantSpec) (string, error) {
// 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 == "fhd" {
if height <= 0 || variantID == "" || variantID == SourceVariantID {
return "/hls/" + streamName + "/index.m3u8"
}
return "/hls/" + streamName + "-" + variantID + "/index.m3u8"
}
// ScaledVariants returns enabled non-source (transcode) variants.
// 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.Height > 0 {
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
}