- 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
389 lines
13 KiB
Go
389 lines
13 KiB
Go
package supervisor
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"drmdecryption/apps/streamd/internal/db"
|
|
)
|
|
|
|
// quote and backslash as values, so the test source stays readable where it
|
|
// asserts on ffmpeg's escaping.
|
|
const (
|
|
q = "'"
|
|
bs = `\`
|
|
)
|
|
|
|
func testCfg() streamSettings {
|
|
return streamSettings{HLSTime: defaultHLSTime, HLSListSize: defaultHLSListSize}
|
|
}
|
|
|
|
func only(format string) []db.OutputSpec {
|
|
return []db.OutputSpec{{ID: "x", Format: format, Enabled: true}}
|
|
}
|
|
|
|
func sourceSpecForTest() db.VariantSpec {
|
|
return db.VariantSpec{ID: db.SourceVariantID, Label: "FHD", Enabled: true, Height: 0}
|
|
}
|
|
|
|
func normalize(s string) string { return strings.ReplaceAll(s, `\`, "/") }
|
|
|
|
// findFormat returns the muxer named by -f, without assuming a position: a
|
|
// stream-copied rung publishing fMP4 carries -tag options first.
|
|
func findFormat(args []string) string {
|
|
for i := 0; i < len(args)-1; i++ {
|
|
if args[i] == "-f" {
|
|
return args[i+1]
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// teeSpec returns the single tee specification argument.
|
|
func teeSpec(t *testing.T, args []string) string {
|
|
t.Helper()
|
|
for i := 0; i < len(args)-1; i++ {
|
|
if args[i] == "-f" && args[i+1] == "tee" {
|
|
if i+2 >= len(args) {
|
|
t.Fatalf("-f tee with no spec: %v", args)
|
|
}
|
|
return args[i+2]
|
|
}
|
|
}
|
|
t.Fatalf("no tee muxer in %v", args)
|
|
return ""
|
|
}
|
|
|
|
// With a single mpegts HLS output the command must stay the plain form
|
|
// streamd has always run. media.go is the least-exercised code in the project,
|
|
// so the common path deliberately does not change shape.
|
|
func TestSingleHLSOutputMatchesTheOriginalCommand(t *testing.T) {
|
|
plan := buildOutputPlan(only(db.FormatHLS), "/data/www/news", testCfg(), sourceSpecForTest())
|
|
want := []string{
|
|
"-f", "hls",
|
|
"-hls_time", "4",
|
|
"-hls_list_size", "15",
|
|
"-hls_flags", "delete_segments+append_list+omit_endlist+independent_segments",
|
|
"-hls_segment_type", "mpegts",
|
|
"-hls_segment_filename", "/data/www/news/seg_%05d.ts",
|
|
"/data/www/news/index.m3u8",
|
|
}
|
|
if len(plan.Args) != len(want) {
|
|
t.Fatalf("arg count %d, want %d\ngot: %v\nwant: %v", len(plan.Args), len(want), plan.Args, want)
|
|
}
|
|
for i := range want {
|
|
// Separators are normalised, since filepath.Join is platform-specific.
|
|
if normalize(plan.Args[i]) != normalize(want[i]) {
|
|
t.Errorf("arg %d = %q, want %q", i, plan.Args[i], want[i])
|
|
}
|
|
}
|
|
if plan.WantsTS {
|
|
t.Error("an HLS-only plan must not claim stdout")
|
|
}
|
|
for _, a := range plan.Args {
|
|
if a == "tee" {
|
|
t.Error("a single output must not use the tee muxer")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSingleOutputPerFormat(t *testing.T) {
|
|
cases := []struct {
|
|
format string
|
|
wantMux string
|
|
wantPath string
|
|
}{
|
|
{db.FormatHLS, "hls", "/data/www/news/index.m3u8"},
|
|
{db.FormatCMAF, "hls", "/data/www/news/cmaf/index.m3u8"},
|
|
{db.FormatDASH, "dash", "/data/www/news/dash/index.mpd"},
|
|
{db.FormatTS, "mpegts", "pipe:1"},
|
|
}
|
|
for _, c := range cases {
|
|
plan := buildOutputPlan(only(c.format), "/data/www/news", testCfg(), sourceSpecForTest())
|
|
if mux := findFormat(plan.Args); mux != c.wantMux {
|
|
t.Errorf("%s: muxer = %q, want %s (args %v)", c.format, mux, c.wantMux, plan.Args)
|
|
}
|
|
if last := normalize(plan.Args[len(plan.Args)-1]); last != c.wantPath {
|
|
t.Errorf("%s: output = %q, want %q", c.format, last, c.wantPath)
|
|
}
|
|
if (c.format == db.FormatTS) != plan.WantsTS {
|
|
t.Errorf("%s: WantsTS = %v", c.format, plan.WantsTS)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The headline requirement: several outputs must share ONE encode via tee. If
|
|
// this regresses into several plain outputs, ffmpeg runs the encoder once per
|
|
// format and CPU triples for a transcoded rung.
|
|
func TestMultipleOutputsUseOneTee(t *testing.T) {
|
|
outputs := []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "cmaf", Format: db.FormatCMAF, Enabled: true},
|
|
{ID: "dash", Format: db.FormatDASH, Enabled: true},
|
|
{ID: "ts", Format: db.FormatTS, Enabled: true},
|
|
}
|
|
plan := buildOutputPlan(outputs, "/data/www/news", testCfg(), sourceSpecForTest())
|
|
spec := teeSpec(t, plan.Args)
|
|
|
|
fCount := 0
|
|
for _, a := range plan.Args {
|
|
if a == "-f" {
|
|
fCount++
|
|
}
|
|
}
|
|
if fCount != 1 {
|
|
t.Errorf("want a single -f (the tee), got %d: %v", fCount, plan.Args)
|
|
}
|
|
|
|
legs := strings.Split(spec, "|")
|
|
if len(legs) != 4 {
|
|
t.Fatalf("want 4 tee legs, got %d: %s", len(legs), spec)
|
|
}
|
|
// Every leg must carry onfail=ignore: without it one dead output (a
|
|
// disconnected TS reader, a full disk) kills the whole process and takes
|
|
// the healthy outputs with it.
|
|
for i, leg := range legs {
|
|
if !strings.Contains(leg, "onfail=ignore") {
|
|
t.Errorf("leg %d has no onfail=ignore: %s", i, leg)
|
|
}
|
|
if !strings.HasPrefix(leg, "[") || !strings.Contains(leg, "]") {
|
|
t.Errorf("leg %d is not a bracketed tee spec: %s", i, leg)
|
|
}
|
|
}
|
|
if n := strings.Count(spec, "f=hls"); n != 2 {
|
|
t.Errorf("want two hls legs (mpegts + fmp4), got %d", n)
|
|
}
|
|
if !strings.Contains(spec, "f=dash") || !strings.Contains(spec, "f=mpegts") {
|
|
t.Errorf("missing a format in the tee spec: %s", spec)
|
|
}
|
|
if !plan.WantsTS {
|
|
t.Error("a plan including the TS output must claim stdout")
|
|
}
|
|
// CMAF and DASH write into subdirectories ffmpeg will not create.
|
|
if len(plan.Dirs) != 2 {
|
|
t.Errorf("want 2 directories to pre-create, got %v", plan.Dirs)
|
|
}
|
|
}
|
|
|
|
// Windows paths contain a colon, which is also the tee option separator.
|
|
// Unescaped, hls_segment_filename=C:/... is split at the drive letter, that
|
|
// slave is rejected, and under onfail=ignore the output silently never
|
|
// appears.
|
|
func TestTeeEscapesColonsInPaths(t *testing.T) {
|
|
outputs := []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "dash", Format: db.FormatDASH, Enabled: true},
|
|
}
|
|
plan := buildOutputPlan(outputs, `C:\data\www\city news`, testCfg(), sourceSpecForTest())
|
|
spec := teeSpec(t, plan.Args)
|
|
|
|
if strings.Contains(spec, "hls_segment_filename=C:") {
|
|
t.Errorf("drive-letter colon is unescaped in the option value: %s", spec)
|
|
}
|
|
// Real ffmpeg needs BOTH: the value quoted and its colon escaped.
|
|
if !strings.Contains(spec, "hls_segment_filename="+q+"C"+bs+":") {
|
|
t.Errorf("want the value quoted with its colon escaped, got: %s", spec)
|
|
}
|
|
// A space is fine: this is one argv element, not a shell word.
|
|
if !strings.Contains(spec, "city news") {
|
|
t.Errorf("path with a space was mangled: %s", spec)
|
|
}
|
|
// The URL after ] keeps its colon, which is not special there.
|
|
if !strings.Contains(spec, "]C:/data/www/city news/index.m3u8") {
|
|
t.Errorf("output URL should keep its drive colon: %s", spec)
|
|
}
|
|
}
|
|
|
|
func TestTeeEscapeKey(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"plain": "plain",
|
|
"a:b": "a" + bs + ":b",
|
|
"a|b": "a" + bs + "|b",
|
|
"a[b]": "a" + bs + "[b" + bs + "]",
|
|
"k=v": "k" + bs + "=v",
|
|
"bsfs/a": "bsfs/a",
|
|
} {
|
|
if got := teeEscapeKey(in); got != want {
|
|
t.Errorf("teeEscapeKey(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTeeEscapeValue(t *testing.T) {
|
|
// Both layers, in this order: quote the whole value, escape its colons.
|
|
// Established against the real ffmpeg; neither alone is accepted.
|
|
if got := teeEscapeValue("C:/data/seg_%05d.ts"); got != q+"C"+bs+":/data/seg_%05d.ts"+q {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
// Backslashes become forward slashes rather than being doubled, because a
|
|
// backslash is itself an escape character in a tee spec.
|
|
if got := teeEscapeValue(`C:\data\x.ts`); got != q+"C"+bs+":/data/x.ts"+q {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
// A space needs nothing special once the value is quoted.
|
|
if got := teeEscapeValue("/srv/city news/seg.ts"); got != q+"/srv/city news/seg.ts"+q {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
// A plain value is quoted too, which is harmless and keeps one rule.
|
|
if got := teeEscapeValue("mpegts"); got != q+"mpegts"+q {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
// A literal quote must not end the quoted run.
|
|
if got := teeEscapeValue("/srv/o" + q + "brien/seg.ts"); !strings.Contains(got, q+bs+q) {
|
|
t.Errorf("apostrophe was not escaped: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestTeeEscapeURL(t *testing.T) {
|
|
// Backslashes become forward slashes, and only the separator characters
|
|
// are escaped — a colon must survive untouched in a URL.
|
|
if got := teeEscapeURL(`C:\data\x.m3u8`); got != "C:/data/x.m3u8" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := teeEscapeURL("a|b"); got != "a"+bs+"|b" {
|
|
t.Errorf("pipe must be escaped in a URL, got %q", got)
|
|
}
|
|
if got := teeEscapeURL("pipe:1"); got != "pipe:1" {
|
|
t.Errorf("pipe:1 must pass through unchanged, got %q", got)
|
|
}
|
|
}
|
|
|
|
// A copied source rung into an fMP4 container needs the ADTS-to-ASC filter on
|
|
// that leg only, plus an MP4 codec tag. A re-encoded rung needs neither,
|
|
// because ffmpeg then produces the right bitstream for each muxer itself.
|
|
func TestFMP4CopyFixesOnlyWhenCopying(t *testing.T) {
|
|
outputs := []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "cmaf", Format: db.FormatCMAF, Enabled: true},
|
|
}
|
|
src := buildOutputPlan(outputs, "/w", testCfg(), sourceSpecForTest())
|
|
if !strings.Contains(strings.Join(src.Args, " "), "-tag:v avc1") {
|
|
t.Error("a copied rung publishing fMP4 needs an MP4 codec tag")
|
|
}
|
|
for _, leg := range strings.Split(teeSpec(t, src.Args), "|") {
|
|
isFMP4 := strings.Contains(leg, "fmp4")
|
|
hasBSF := strings.Contains(leg, "aac_adtstoasc")
|
|
if isFMP4 != hasBSF {
|
|
t.Errorf("aac_adtstoasc belongs on the fMP4 leg and only there: %s", leg)
|
|
}
|
|
}
|
|
|
|
hd := db.VariantSpec{ID: "hd", Enabled: true, Height: 720, VideoBitrate: "2800k", AudioBitrate: "128k"}
|
|
enc := strings.Join(buildOutputPlan(outputs, "/w", testCfg(), hd).Args, " ")
|
|
if strings.Contains(enc, "-tag:v avc1") {
|
|
t.Error("a re-encoded rung should not need a codec tag override")
|
|
}
|
|
if strings.Contains(enc, "aac_adtstoasc") {
|
|
t.Error("a re-encoded rung should not need the ADTS filter")
|
|
}
|
|
}
|
|
|
|
// A single fMP4 output has no tee, so the bitstream filter is set the
|
|
// ordinary way.
|
|
func TestSingleFMP4OutputUsesPlainBSF(t *testing.T) {
|
|
plan := buildOutputPlan(only(db.FormatCMAF), "/w", testCfg(), sourceSpecForTest())
|
|
joined := strings.Join(plan.Args, " ")
|
|
if !strings.Contains(joined, "-bsf:a aac_adtstoasc") {
|
|
t.Errorf("want a plain -bsf:a, got %s", joined)
|
|
}
|
|
if strings.Contains(joined, "bsfs/a") {
|
|
t.Errorf("tee syntax leaked into a single-output command: %s", joined)
|
|
}
|
|
}
|
|
|
|
// Publishing nothing would leave the stream unreachable.
|
|
func TestNoEnabledOutputsFallsBackToHLS(t *testing.T) {
|
|
plan := buildOutputPlan([]db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: false},
|
|
}, "/data/www/news", testCfg(), sourceSpecForTest())
|
|
if findFormat(plan.Args) != "hls" {
|
|
t.Errorf("want an HLS fallback, got %v", plan.Args)
|
|
}
|
|
}
|
|
|
|
func TestPerOutputSegmentOverrides(t *testing.T) {
|
|
outputs := []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true, SegmentDuration: 2, ListSize: 6},
|
|
}
|
|
plan := buildOutputPlan(outputs, "/data/www/news", testCfg(), sourceSpecForTest())
|
|
joined := strings.Join(plan.Args, " ")
|
|
if !strings.Contains(joined, "-hls_time 2") || !strings.Contains(joined, "-hls_list_size 6") {
|
|
t.Errorf("per-output overrides were ignored: %s", joined)
|
|
}
|
|
// An output that does not override inherits the stream's settings.
|
|
cfg := testCfg()
|
|
cfg.HLSTime, cfg.HLSListSize = 8, 40
|
|
plan = buildOutputPlan(only(db.FormatHLS), "/data/www/news", cfg, sourceSpecForTest())
|
|
joined = strings.Join(plan.Args, " ")
|
|
if !strings.Contains(joined, "-hls_time 8") || !strings.Contains(joined, "-hls_list_size 40") {
|
|
t.Errorf("stream settings were not inherited: %s", joined)
|
|
}
|
|
}
|
|
|
|
func TestVariantCodecArgs(t *testing.T) {
|
|
// The source rung is a pass-through copy: no encoder at all.
|
|
src := strings.Join(variantCodecArgs(sourceSpecForTest()), " ")
|
|
if !strings.Contains(src, "-c copy") {
|
|
t.Errorf("source rung must stream-copy, got %q", src)
|
|
}
|
|
if strings.Contains(src, "libx264") {
|
|
t.Error("source rung must not re-encode")
|
|
}
|
|
|
|
// A scaled rung encodes video and audio.
|
|
hd := strings.Join(variantCodecArgs(db.VariantSpec{
|
|
ID: "hd", Height: 720, VideoBitrate: "2800k", AudioBitrate: "128k",
|
|
}), " ")
|
|
for _, want := range []string{"scale=-2:720", "libx264", "-b:v 2800k", "-b:a 128k", "-bufsize 5600k"} {
|
|
if !strings.Contains(hd, want) {
|
|
t.Errorf("scaled rung missing %q in %q", want, hd)
|
|
}
|
|
}
|
|
|
|
// An audio-only rung drops video entirely.
|
|
au := strings.Join(variantCodecArgs(db.VariantSpec{
|
|
ID: "audio", AudioOnly: true, AudioBitrate: "96k",
|
|
}), " ")
|
|
if !strings.Contains(au, "-vn") {
|
|
t.Errorf("audio rung must pass -vn, got %q", au)
|
|
}
|
|
if strings.Contains(au, "libx264") || strings.Contains(au, "scale=") {
|
|
t.Errorf("audio rung must not touch video, got %q", au)
|
|
}
|
|
if !strings.Contains(au, "-b:a 96k") {
|
|
t.Errorf("audio rung missing its bitrate, got %q", au)
|
|
}
|
|
}
|
|
|
|
func TestDoubleBitrate(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"2800k": "5600k",
|
|
"1200k": "2400k",
|
|
"5M": "10000k",
|
|
"": "5600k",
|
|
"junk": "5600k",
|
|
} {
|
|
if got := doubleBitrate(in); got != want {
|
|
t.Errorf("doubleBitrate(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlaylistsRecorded(t *testing.T) {
|
|
outputs := []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "cmaf", Format: db.FormatCMAF, Enabled: true},
|
|
{ID: "dash", Format: db.FormatDASH, Enabled: true},
|
|
{ID: "ts", Format: db.FormatTS, Enabled: true},
|
|
}
|
|
plan := buildOutputPlan(outputs, "/w", testCfg(), sourceSpecForTest())
|
|
for _, f := range []string{db.FormatHLS, db.FormatCMAF, db.FormatDASH} {
|
|
if plan.Playlists[f] == "" {
|
|
t.Errorf("no playlist recorded for %s", f)
|
|
}
|
|
}
|
|
// The progressive TS has no file on disk, so no playlist entry.
|
|
if plan.Playlists[db.FormatTS] != "" {
|
|
t.Error("the TS output should not record a playlist file")
|
|
}
|
|
}
|