- 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
218 lines
7.3 KiB
Go
218 lines
7.3 KiB
Go
package supervisor
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"drmdecryption/apps/streamd/internal/db"
|
|
)
|
|
|
|
// These tests run the real ffmpeg against the real generated command line.
|
|
// The unit tests prove the arguments look right; only ffmpeg can prove it
|
|
// accepts them — tee's escaping rules in particular are easy to get subtly
|
|
// wrong in a way no amount of string assertion would catch.
|
|
//
|
|
// Skipped when ffmpeg is not installed, and under -short.
|
|
|
|
func ffmpegPath(t *testing.T) string {
|
|
t.Helper()
|
|
if testing.Short() {
|
|
t.Skip("skipping ffmpeg integration test in short mode")
|
|
}
|
|
for _, cand := range []string{os.Getenv("FFMPEG_PATH"), "ffmpeg"} {
|
|
if cand == "" {
|
|
continue
|
|
}
|
|
if p, err := exec.LookPath(cand); err == nil {
|
|
return p
|
|
}
|
|
}
|
|
t.Skip("ffmpeg not found on PATH; skipping integration test")
|
|
return ""
|
|
}
|
|
|
|
// makeTestTS renders a few seconds of synthetic audio and video to an MPEG-TS
|
|
// file, standing in for what the downloader produces.
|
|
func makeTestTS(t *testing.T, ff, dir string, seconds int) string {
|
|
t.Helper()
|
|
out := filepath.Join(dir, "source.ts")
|
|
cmd := exec.Command(ff,
|
|
"-hide_banner", "-loglevel", "error", "-y",
|
|
"-f", "lavfi", "-i", "testsrc2=size=640x360:rate=25",
|
|
"-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000",
|
|
"-t", strconv.Itoa(seconds),
|
|
"-c:v", "libx264", "-preset", "ultrafast", "-g", "25", "-pix_fmt", "yuv420p",
|
|
"-c:a", "aac", "-b:a", "96k",
|
|
"-f", "mpegts", out,
|
|
)
|
|
if b, err := cmd.CombinedOutput(); err != nil {
|
|
t.Skipf("could not render a test source (%v): %s", err, b)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// runPlan runs one variant's full generated command against src and returns
|
|
// the process's CPU time.
|
|
func runPlan(t *testing.T, ff, src, wwwDir string, spec db.VariantSpec, outputs []db.OutputSpec) (time.Duration, string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(wwwDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg := streamSettings{HLSTime: 2, HLSListSize: 5}
|
|
plan := buildOutputPlan(outputs, wwwDir, cfg, spec)
|
|
for _, d := range plan.Dirs {
|
|
if err := os.MkdirAll(d, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The real supervisor passes "-follow 1" to track a growing file; here the
|
|
// source is complete, so read it once and exit.
|
|
args := []string{"-hide_banner", "-loglevel", "error", "-y", "-i", src}
|
|
args = append(args, variantCodecArgs(spec)...)
|
|
args = append(args, plan.Args...)
|
|
|
|
cmd := exec.Command(ff, args...)
|
|
// Capture stderr explicitly rather than with CombinedOutput: when the TS
|
|
// output is on, stdout carries media and cannot also collect diagnostics.
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr
|
|
if plan.WantsTS {
|
|
sink, err := os.Create(filepath.Join(wwwDir, "pipe.ts"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer sink.Close()
|
|
cmd.Stdout = sink
|
|
}
|
|
err := cmd.Run()
|
|
out := stderr.String()
|
|
if err != nil {
|
|
t.Fatalf("ffmpeg rejected the generated command: %v\nargs: %q\nstderr: %s", err, args, out)
|
|
}
|
|
var cpu time.Duration
|
|
if cmd.ProcessState != nil {
|
|
cpu = cmd.ProcessState.UserTime() + cmd.ProcessState.SystemTime()
|
|
}
|
|
return cpu, out
|
|
}
|
|
|
|
// A single HLS output must work: this is the command streamd has always run.
|
|
func TestFFmpegAcceptsSingleHLSOutput(t *testing.T) {
|
|
ff := ffmpegPath(t)
|
|
dir := t.TempDir()
|
|
src := makeTestTS(t, ff, dir, 4)
|
|
www := filepath.Join(dir, "www", "news")
|
|
_, _ = runPlan(t, ff, src, www, sourceSpecForTest(), []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
})
|
|
mustExist(t, filepath.Join(www, "index.m3u8"))
|
|
mustGlob(t, filepath.Join(www, "seg_*.ts"))
|
|
}
|
|
|
|
// The real test of the tee spec: ffmpeg must accept it and every output must
|
|
// actually be written.
|
|
func TestFFmpegAcceptsTeeWithEveryFormat(t *testing.T) {
|
|
ff := ffmpegPath(t)
|
|
dir := t.TempDir()
|
|
src := makeTestTS(t, ff, dir, 4)
|
|
www := filepath.Join(dir, "www", "news")
|
|
_, out := runPlan(t, ff, src, www, sourceSpecForTest(), []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},
|
|
})
|
|
// onfail=ignore means a broken leg is silent, so check the files rather
|
|
// than trusting the exit status.
|
|
mustExist(t, filepath.Join(www, "index.m3u8"))
|
|
mustGlob(t, filepath.Join(www, "seg_*.ts"))
|
|
mustExist(t, filepath.Join(www, "cmaf", "index.m3u8"))
|
|
mustGlob(t, filepath.Join(www, "cmaf", "*.m4s"))
|
|
mustExist(t, filepath.Join(www, "cmaf", "init.mp4"))
|
|
mustExist(t, filepath.Join(www, "dash", "index.mpd"))
|
|
mustNonEmpty(t, filepath.Join(www, "pipe.ts"))
|
|
if strings.Contains(strings.ToLower(out), "invalid argument") {
|
|
t.Errorf("ffmpeg complained about the tee spec: %s", out)
|
|
}
|
|
}
|
|
|
|
// A path with a drive letter and a space is where tee escaping actually bites.
|
|
func TestFFmpegAcceptsTeeWithAwkwardPath(t *testing.T) {
|
|
ff := ffmpegPath(t)
|
|
dir := t.TempDir()
|
|
src := makeTestTS(t, ff, dir, 3)
|
|
// A space in a directory name, which is ordinary on Windows.
|
|
www := filepath.Join(dir, "www root", "city news")
|
|
_, _ = runPlan(t, ff, src, www, sourceSpecForTest(), []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "dash", Format: db.FormatDASH, Enabled: true},
|
|
})
|
|
mustExist(t, filepath.Join(www, "index.m3u8"))
|
|
mustExist(t, filepath.Join(www, "dash", "index.mpd"))
|
|
}
|
|
|
|
// The headline efficiency claim: adding formats to a transcoded rung must
|
|
// share one encode. Three formats through tee should cost far less than three
|
|
// times a single format — if it is ~3x, the encoder is running per output.
|
|
func TestTeeEncodesOnceNotPerOutput(t *testing.T) {
|
|
ff := ffmpegPath(t)
|
|
dir := t.TempDir()
|
|
// Long enough that encoding dominates process start-up cost.
|
|
src := makeTestTS(t, ff, dir, 8)
|
|
hd := db.VariantSpec{ID: "hd", Label: "HD", Enabled: true, Height: 720,
|
|
VideoBitrate: "2800k", AudioBitrate: "128k"}
|
|
|
|
oneCPU, _ := runPlan(t, ff, src, filepath.Join(dir, "one"), hd, []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
})
|
|
threeCPU, _ := runPlan(t, ff, src, filepath.Join(dir, "three"), hd, []db.OutputSpec{
|
|
{ID: "hls", Format: db.FormatHLS, Enabled: true},
|
|
{ID: "cmaf", Format: db.FormatCMAF, Enabled: true},
|
|
{ID: "dash", Format: db.FormatDASH, Enabled: true},
|
|
})
|
|
if oneCPU <= 0 || threeCPU <= 0 {
|
|
t.Skipf("no CPU accounting available (one=%v three=%v)", oneCPU, threeCPU)
|
|
}
|
|
ratio := float64(threeCPU) / float64(oneCPU)
|
|
t.Logf("CPU: 1 format = %v, 3 formats = %v (ratio %.2fx)", oneCPU, threeCPU, ratio)
|
|
// Muxing three times instead of once is not free, but it is nothing like
|
|
// encoding three times. Anything approaching 3x means tee is not in use.
|
|
if ratio > 2.0 {
|
|
t.Errorf("3 formats cost %.2fx the CPU of 1 — the encoder appears to be running "+
|
|
"per output rather than once through tee", ratio)
|
|
}
|
|
}
|
|
|
|
func mustExist(t *testing.T, path string) {
|
|
t.Helper()
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Errorf("expected %s to exist: %v", path, err)
|
|
return
|
|
}
|
|
if st.Size() == 0 {
|
|
t.Errorf("%s is empty", path)
|
|
}
|
|
}
|
|
|
|
func mustNonEmpty(t *testing.T, path string) {
|
|
t.Helper()
|
|
st, err := os.Stat(path)
|
|
if err != nil || st.Size() == 0 {
|
|
t.Errorf("expected %s to have content (err=%v)", path, err)
|
|
}
|
|
}
|
|
|
|
func mustGlob(t *testing.T, pattern string) {
|
|
t.Helper()
|
|
m, err := filepath.Glob(pattern)
|
|
if err != nil || len(m) == 0 {
|
|
t.Errorf("expected files matching %s (err=%v)", pattern, err)
|
|
}
|
|
}
|