unarr/internal/engine/hls_ratecontrol_test.go
Deivid Soto 556c5cb05f fix(hls): forced-idr en NVENC/QSV — los segmentos ignoraban force_key_frames
NVENC (ffmpeg 6.1 + drivers actuales) emite los keyframes forzados por
-force_key_frames como I-frames NO-IDR; el muxer HLS solo corta en IDR,
así que cada segmento se estiraba en silencio al GOP por defecto
(250 frames ≈ 10.4 s @24fps) mientras la playlist server-side seguía
prometiendo 2 s por segmento. Con los PTS reales ~5× fuera del mapa de
la playlist, los seeks aterrizaban donde podían y los subtítulos se
desincronizaban en cuanto se mezclaban segmentos de runs distintos
(seek-restart) en el mismo dir.

Medido: 3 segmentos por 30 s de encode en vez de 15; con -forced-idr 1
exactamente 15, y post-fix seg-150/151/158 arrancan en 300.0/302.0/316.0
clavados. Afecta a TODO el HLS por NVENC histórico (no era del rate
control nuevo: la config de bitrate fijo producía lo mismo). QSV recibe
su grafía -forced_idr. Las entradas de caché viejas nunca llegaron a
sellarse (el conteo de segmentos no cuadraba), así que no hay migración:
solo sesiones vivas estaban afectadas.
2026-06-10 10:44:18 +02:00

114 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package engine
import (
"strings"
"testing"
)
func TestDoubleBitrate(t *testing.T) {
cases := map[string]string{
"6000k": "12000k",
"25000k": "50000k",
"1500k": "3000k",
"5M": "10M",
"1.5M": "3M",
"2.5m": "5m",
"800000": "1600000",
"": "",
"garbage": "garbage", // unparseable → unchanged (1× bufsize fallback)
"-5M": "-5M", // non-positive → unchanged
}
for in, want := range cases {
if got := doubleBitrate(in); got != want {
t.Errorf("doubleBitrate(%q) = %q, want %q", in, got, want)
}
}
}
// segmentIdxForTime must be the exact inverse of segmentStartSec so the
// resume-aware first spawn (HLSSessionConfig.StartSec) lands on the same
// segment the player's hls.js startPosition will request.
func TestSegmentIdxForTime(t *testing.T) {
cases := map[float64]int{
0: 0,
-3: 0,
0.5: 0,
1.99: 0,
2: 1,
3.9: 1,
60: 30,
3599.9: 1799,
}
for sec, want := range cases {
if got := segmentIdxForTime(sec); got != want {
t.Errorf("segmentIdxForTime(%v) = %d, want %d", sec, got, want)
}
}
// Round-trip: the start time of the segment we resolve must never be
// AFTER the requested position (the player would miss its first frames).
for _, sec := range []float64{0, 1, 2, 7.3, 119.9, 4321} {
idx := segmentIdxForTime(sec)
if start := segmentStartSec(idx); start > sec {
t.Errorf("segmentStartSec(segmentIdxForTime(%v)) = %v > %v", sec, start, sec)
}
}
}
// Capped constant-quality rate control: libx264 gets -crf (no -b:v), NVENC
// gets -cq with -b:v 0, both keep -maxrate at the level-coherent cap and a
// 2× -bufsize. VAAPI (and the other vendor encoders) keep the proven
// fixed-bitrate triple untouched.
func TestBuildHLSFFmpegArgsRateControl(t *testing.T) {
probe := &StreamProbe{Width: 1920, Height: 1080, DurationSec: 100}
base := HLSSessionConfig{
SessionID: "test",
SourcePath: "/media/Movie.mkv",
Quality: "1080p",
Transcode: TranscodeRuntime{
FFmpegPath: "/usr/bin/ffmpeg",
FFprobePath: "/usr/bin/ffprobe",
},
}
t.Run("libx264 capped CRF", func(t *testing.T) {
cfg := base
cfg.Transcode.HWAccel = HWAccelNone
got := strings.Join(buildHLSFFmpegArgsAt(cfg, probe, "/tmp/tmpdir", 0, 0), " ")
for _, want := range []string{"-crf 23", "-maxrate 6000k", "-bufsize 12000k"} {
if !strings.Contains(got, want) {
t.Errorf("libx264 argv missing %q\n%s", want, got)
}
}
if strings.Contains(got, "-b:v 6000k") {
t.Errorf("libx264 argv must not carry -b:v alongside -crf\n%s", got)
}
})
t.Run("nvenc constant-quality VBR", func(t *testing.T) {
cfg := base
cfg.Transcode.HWAccel = HWAccelNVENC
got := strings.Join(buildHLSFFmpegArgsAt(cfg, probe, "/tmp/tmpdir", 0, 0), " ")
// -forced-idr 1 is load-bearing: without it NVENC emits the forced
// keyframes as non-IDR and every HLS segment stretches to the full
// GOP, desyncing the playlist timeline (subs/seeks).
for _, want := range []string{"-rc vbr", "-cq 23", "-b:v 0", "-maxrate 6000k", "-bufsize 12000k", "-forced-idr 1"} {
if !strings.Contains(got, want) {
t.Errorf("nvenc argv missing %q\n%s", want, got)
}
}
})
t.Run("vaapi keeps fixed-bitrate triple", func(t *testing.T) {
cfg := base
cfg.Transcode.HWAccel = HWAccelVAAPI
got := strings.Join(buildHLSFFmpegArgsAt(cfg, probe, "/tmp/tmpdir", 0, 0), " ")
for _, want := range []string{"-b:v 6000k", "-maxrate 6000k", "-bufsize 6000k"} {
if !strings.Contains(got, want) {
t.Errorf("vaapi argv missing %q\n%s", want, got)
}
}
if strings.Contains(got, "-crf") || strings.Contains(got, "-cq") {
t.Errorf("vaapi argv must not carry constant-quality flags\n%s", got)
}
})
}