streamd: copyable m3u8 links + FHD/HD/SD output variants
Some checks failed
release / build-and-upload (push) Failing after 3m33s
Some checks failed
release / build-and-upload (push) Failing after 3m33s
- WebUI Copy button for absolute m3u8 URLs (no player) - Per-stream variants ladder; HD/SD live-transcoded as sibling HLS - variants_json in DB/API; supervisor spawns scaled ffmpeg packagers
This commit is contained in:
parent
ac8701b5a6
commit
3cb259c9b6
12 changed files with 913 additions and 62 deletions
139
apps/streamd/internal/db/variants.go
Normal file
139
apps/streamd/internal/db/variants.go
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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.
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// DefaultVariants returns the FHD + optional HD/SD ladder with only FHD enabled.
|
||||
func DefaultVariants() []VariantSpec {
|
||||
return []VariantSpec{
|
||||
{ID: "fhd", 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"},
|
||||
}
|
||||
}
|
||||
|
||||
// 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 an FHD/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)
|
||||
if v.ID == "" {
|
||||
if v.Height <= 0 {
|
||||
v.ID = "fhd"
|
||||
} 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 = "fhd"
|
||||
if v.Label == "" || strings.EqualFold(v.Label, "fhd") {
|
||||
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: "fhd", 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 == "fhd" {
|
||||
return "/hls/" + streamName + "/index.m3u8"
|
||||
}
|
||||
return "/hls/" + streamName + "-" + variantID + "/index.m3u8"
|
||||
}
|
||||
|
||||
// ScaledVariants returns enabled non-source (transcode) variants.
|
||||
func ScaledVariants(list []VariantSpec) []VariantSpec {
|
||||
var out []VariantSpec
|
||||
for _, v := range list {
|
||||
if v.Enabled && v.Height > 0 {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue