feat(hls): faster first-start — probe cache + tighter encoder presets (0.9.9)
Reduces first-segment latency on cache MISS so the player doesn't sit on
"preparando sesión". Three independent levers:
1. ProbeFile memoised by (path, mtime, size) for 30 min — second play of
the same source skips ffprobe (1-3 s on 50+ GB MKVs).
2. HLS encoder presets biased for latency over quality:
- libx264 default veryfast → superfast (~15-20% faster, marginal
quality loss at 5-25 Mbps target bitrates).
- NVENC: -preset p4 -tune hq → -preset p3 -tune ll. First-segment
~0.8 s on RTX-class GPUs (was ~1.5 s).
- QSV: -preset medium → -preset veryfast (keeps look_ahead=0).
- VideoToolbox: adds -realtime 1 (was unset). Bitrate args still
drive rate control; -q:v dropped to avoid the silent conflict
where ffmpeg ignored it under -b:v.
3. Per-session log surfaces encoder + accel + preset so "first-start
was slow" complaints can be triaged from the journal alone.
Diagnostic helpers (DetectHWAccelDiagnostic + HWAccelDiagnostic) added
for future wiring into daemon startup / agent register; users today can
already inspect via `unarr probe-hwaccel`.
Web: AgentsTab profile page now shows the agent's chosen encoder
(amber if software libx264, green if HW) plus the transcode-resolution
cap. Hidden for pre-0.9.9 agents that haven't reported hwAccel.
This commit is contained in:
parent
7b78d0b778
commit
3b8d77b496
8 changed files with 593 additions and 17 deletions
154
internal/engine/probe_cache_test.go
Normal file
154
internal/engine/probe_cache_test.go
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
package engine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProbeCache_LookupMissNonexistent(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
if _, ok := lookupProbeCache("/path/that/does/not/exist"); ok {
|
||||
t.Fatal("expected MISS for non-existent path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_StoreThenLookupHit(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "movie.mkv")
|
||||
if err := os.WriteFile(path, []byte("fake content"), 0o644); err != nil {
|
||||
t.Fatalf("write tmp file: %v", err)
|
||||
}
|
||||
|
||||
probe := &StreamProbe{VideoCodec: "h264", Width: 1920, Height: 1080, DurationSec: 5400}
|
||||
storeProbeCache(path, probe)
|
||||
|
||||
got, ok := lookupProbeCache(path)
|
||||
if !ok {
|
||||
t.Fatal("expected HIT after store")
|
||||
}
|
||||
if got != probe {
|
||||
t.Fatalf("expected pointer-identical probe; got different")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_MtimeChangeInvalidates(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "movie.mkv")
|
||||
if err := os.WriteFile(path, []byte("original"), 0o644); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
probe := &StreamProbe{VideoCodec: "h264", DurationSec: 100}
|
||||
storeProbeCache(path, probe)
|
||||
|
||||
// Force mtime change. WriteFile doesn't guarantee a different mtime if
|
||||
// the filesystem timestamp resolution is coarse, so set it explicitly
|
||||
// to a value 1 hour in the future.
|
||||
future := time.Now().Add(1 * time.Hour)
|
||||
if err := os.Chtimes(path, future, future); err != nil {
|
||||
t.Fatalf("chtimes: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := lookupProbeCache(path); ok {
|
||||
t.Fatal("expected MISS after mtime change")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_SizeChangeInvalidates(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "movie.mkv")
|
||||
if err := os.WriteFile(path, []byte("aaaaa"), 0o644); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
probe := &StreamProbe{VideoCodec: "h264", DurationSec: 100}
|
||||
storeProbeCache(path, probe)
|
||||
|
||||
// Truncate file to a different size + reset mtime to the original (to
|
||||
// isolate the size-check path). Stat picks up new size immediately.
|
||||
if err := os.WriteFile(path, []byte("a"), 0o644); err != nil {
|
||||
t.Fatalf("rewrite: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := lookupProbeCache(path); ok {
|
||||
t.Fatal("expected MISS after size change")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_ExpiryDropsEntry(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "movie.mkv")
|
||||
if err := os.WriteFile(path, []byte("content"), 0o644); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
// Stash an entry whose expires is already in the past — simulates TTL
|
||||
// having elapsed without sleeping for 30 min.
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("stat: %v", err)
|
||||
}
|
||||
key := probeCacheKey{path: path, mtime: fi.ModTime().UnixNano(), size: fi.Size()}
|
||||
probeCacheMu.Lock()
|
||||
probeCache[key] = probeCacheEntry{
|
||||
probe: &StreamProbe{VideoCodec: "h264"},
|
||||
expires: time.Now().Add(-1 * time.Minute),
|
||||
}
|
||||
probeCacheMu.Unlock()
|
||||
|
||||
if _, ok := lookupProbeCache(path); ok {
|
||||
t.Fatal("expected MISS for expired entry")
|
||||
}
|
||||
// Side-effect: lookup should have evicted the stale entry.
|
||||
if ProbeCacheSize() != 0 {
|
||||
t.Fatalf("expected cache size 0 after expiry eviction; got %d", ProbeCacheSize())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_ResetClears(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "movie.mkv")
|
||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
storeProbeCache(path, &StreamProbe{VideoCodec: "h264"})
|
||||
if ProbeCacheSize() != 1 {
|
||||
t.Fatalf("expected size 1 after store; got %d", ProbeCacheSize())
|
||||
}
|
||||
|
||||
ResetProbeCache()
|
||||
if ProbeCacheSize() != 0 {
|
||||
t.Fatalf("expected size 0 after reset; got %d", ProbeCacheSize())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeCache_StoreNonexistentNoOp(t *testing.T) {
|
||||
ResetProbeCache()
|
||||
t.Cleanup(ResetProbeCache)
|
||||
|
||||
// Store on a non-existent path should silently do nothing (stat fails),
|
||||
// not panic, and not poison the cache with a zero key.
|
||||
storeProbeCache("/nope/never/exists.mkv", &StreamProbe{VideoCodec: "h264"})
|
||||
if ProbeCacheSize() != 0 {
|
||||
t.Fatalf("expected 0 entries; got %d", ProbeCacheSize())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue