mirror of
https://github.com/truewhile/MeBox.git
synced 2026-11-16 20:54:23 +08:00
fix(playback): reuse completed HLS output and preserve resume positions
This commit is contained in:
@@ -59,6 +59,9 @@ type TranscoderService struct {
|
||||
availMu sync.Mutex
|
||||
availCheckedAt time.Time
|
||||
availValue bool
|
||||
availKey string
|
||||
ffmpegMu sync.Mutex
|
||||
ffmpegCache ffmpegValidationCache
|
||||
}
|
||||
|
||||
// hlsJob holds the live state of one ffmpeg run.
|
||||
@@ -83,6 +86,11 @@ type hlsJob struct {
|
||||
quality string
|
||||
// done is closed when the ffmpeg goroutine fully exits (after process death).
|
||||
done chan struct{}
|
||||
// Completed outputs stay reusable until idle eviction without occupying a
|
||||
// running-process slot. The binary and config are fixed for this generation.
|
||||
completed bool
|
||||
ffmpegPath string
|
||||
config *config.Config
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -117,24 +125,37 @@ func (t *TranscoderService) Available() bool {
|
||||
if t == nil || t.cfg == nil {
|
||||
return false
|
||||
}
|
||||
if !t.cfg.Transcoder.Enabled {
|
||||
cfg := t.transcodeConfig()
|
||||
if !cfg.Transcoder.Enabled {
|
||||
return false
|
||||
}
|
||||
encoder := ffmpegArgSettingsFromConfig(cfg, nil).encoder
|
||||
key := cfg.App.FFmpegPath + "\x00" + encoder
|
||||
t.availMu.Lock()
|
||||
defer t.availMu.Unlock()
|
||||
if !t.availCheckedAt.IsZero() && time.Since(t.availCheckedAt) < transcodeAvailabilityTTL {
|
||||
return t.availValue
|
||||
if !t.availValue && t.availKey == key && !t.availCheckedAt.IsZero() && time.Since(t.availCheckedAt) < transcodeAvailabilityTTL {
|
||||
return false
|
||||
}
|
||||
_, err := t.resolveFFmpegPath()
|
||||
_, err := t.resolveFFmpegPathFor(context.Background(), cfg.App.FFmpegPath, encoder)
|
||||
available := err == nil
|
||||
if !available && t.log != nil {
|
||||
t.log.Debug("local HLS renditions unavailable", zap.Error(err))
|
||||
}
|
||||
t.availValue = available
|
||||
t.availCheckedAt = time.Now()
|
||||
t.availKey = key
|
||||
return available
|
||||
}
|
||||
|
||||
func (t *TranscoderService) transcodeConfig() *config.Config {
|
||||
config.RuntimeMu.RLock()
|
||||
defer config.RuntimeMu.RUnlock()
|
||||
return &config.Config{
|
||||
App: config.AppConfig{FFmpegPath: t.cfg.App.FFmpegPath, VAAPIDevice: t.cfg.App.VAAPIDevice},
|
||||
Transcoder: t.cfg.Transcoder,
|
||||
}
|
||||
}
|
||||
|
||||
// HLSDir is the per-media directory that holds index.m3u8 + segment files.
|
||||
func (t *TranscoderService) HLSDir(mediaID string) string {
|
||||
return filepath.Join(t.cfg.Cache.CacheDir, "hls", mediaID)
|
||||
@@ -174,7 +195,8 @@ func (t *TranscoderService) EnsureJobFromSubtitle(ctx context.Context, mediaID s
|
||||
// EnsureJobFromSubtitleQuality is EnsureJobFromSubtitle with an explicit local
|
||||
// HLS quality profile. Empty qualityID keeps the global transcoder config.
|
||||
func (t *TranscoderService) EnsureJobFromSubtitleQuality(ctx context.Context, mediaID string, startSec float64, seekGen int64, subtitleStream int, qualityID string) (string, error) {
|
||||
if !t.cfg.Transcoder.Enabled {
|
||||
cfg := t.transcodeConfig()
|
||||
if !cfg.Transcoder.Enabled {
|
||||
return "", ErrTranscodeDisabled
|
||||
}
|
||||
if startSec < 0 {
|
||||
@@ -203,12 +225,17 @@ func (t *TranscoderService) EnsureJobFromSubtitleQuality(ctx context.Context, me
|
||||
|
||||
t.mu.Lock()
|
||||
if existing, ok := t.jobs[mediaID]; ok {
|
||||
if sameHLSConfiguration(existing, startSec, subtitleStream, qualityID) {
|
||||
reusable := true
|
||||
if existing.completed {
|
||||
info, err := os.Stat(t.PlaylistPath(mediaID))
|
||||
reusable = err == nil && info.Size() > 0
|
||||
}
|
||||
if reusable && sameHLSConfiguration(existing, startSec, subtitleStream, qualityID) {
|
||||
t.touchJobLocked(mediaID)
|
||||
t.mu.Unlock()
|
||||
return t.PlaylistPath(mediaID), nil
|
||||
}
|
||||
if !shouldReplaceHLSJobConfigurationQuality(existing, startSec, seekGen, subtitleStream, qualityID) {
|
||||
if reusable && !shouldReplaceHLSJobConfigurationQuality(existing, startSec, seekGen, subtitleStream, qualityID) {
|
||||
t.touchJobLocked(mediaID)
|
||||
t.mu.Unlock()
|
||||
return t.PlaylistPath(mediaID), nil
|
||||
@@ -230,7 +257,13 @@ func (t *TranscoderService) EnsureJobFromSubtitleQuality(ctx context.Context, me
|
||||
}
|
||||
input.Quality = quality
|
||||
t.maybeFillDuration(ctx, m, input)
|
||||
if _, err := t.resolveFFmpegPath(); err != nil {
|
||||
settings := ffmpegArgSettingsFromConfig(cfg, quality)
|
||||
jobEncoder := settings.encoder
|
||||
if settings.noScale || subtitleStream >= 0 {
|
||||
jobEncoder = ""
|
||||
}
|
||||
bin, err := t.resolveFFmpegPathFor(ctx, cfg.App.FFmpegPath, jobEncoder)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -242,29 +275,15 @@ func (t *TranscoderService) EnsureJobFromSubtitleQuality(ctx context.Context, me
|
||||
}
|
||||
|
||||
t.mu.Lock()
|
||||
if existing, ok := t.jobs[mediaID]; ok {
|
||||
if sameHLSConfiguration(existing, startSec, subtitleStream, qualityID) {
|
||||
t.touchJobLocked(mediaID)
|
||||
t.mu.Unlock()
|
||||
return t.PlaylistPath(mediaID), nil
|
||||
}
|
||||
if !shouldReplaceHLSJobConfigurationQuality(existing, startSec, seekGen, subtitleStream, qualityID) {
|
||||
t.touchJobLocked(mediaID)
|
||||
t.mu.Unlock()
|
||||
return t.PlaylistPath(mediaID), nil
|
||||
}
|
||||
prev := t.detachJobLocked(mediaID)
|
||||
t.mu.Unlock()
|
||||
waitJobExit(prev, 12*time.Second)
|
||||
t.mu.Lock()
|
||||
}
|
||||
if max := t.maxConcurrent(); max > 0 && len(t.jobs) >= max {
|
||||
if max := t.maxConcurrent(); max > 0 && t.runningJobCountLocked() >= max {
|
||||
t.mu.Unlock()
|
||||
return "", ErrTranscodeBusy
|
||||
}
|
||||
|
||||
jobCtx, cancel := context.WithCancel(context.Background())
|
||||
jobEncoder := t.effectiveEncoder()
|
||||
if jobEncoder == "" {
|
||||
jobEncoder = "software"
|
||||
}
|
||||
if subtitleStream >= 0 {
|
||||
jobEncoder = "software (subtitle burn-in)"
|
||||
}
|
||||
@@ -280,6 +299,8 @@ func (t *TranscoderService) EnsureJobFromSubtitleQuality(ctx context.Context, me
|
||||
subtitleStream: subtitleStream,
|
||||
quality: qualityID,
|
||||
done: make(chan struct{}),
|
||||
ffmpegPath: bin,
|
||||
config: cfg,
|
||||
}
|
||||
t.jobs[mediaID] = job
|
||||
t.mu.Unlock()
|
||||
|
||||
@@ -11,29 +11,17 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/config"
|
||||
)
|
||||
|
||||
func (t *TranscoderService) runFFmpeg(ctx context.Context, job *hlsJob, input transcodeInput) {
|
||||
bin, err := t.resolveFFmpegPath()
|
||||
if err != nil {
|
||||
t.log.Warn("ffmpeg unavailable", zap.String("media_id", job.mediaID), zap.Error(err))
|
||||
t.mu.Lock()
|
||||
delete(t.jobs, job.mediaID)
|
||||
t.mu.Unlock()
|
||||
t.hub.Publish("transcode", map[string]any{
|
||||
"media_id": job.mediaID,
|
||||
"status": "error",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
playlist := filepath.Join(job.outputDir, "index.m3u8")
|
||||
segments := filepath.Join(job.outputDir, "seg_%05d.ts")
|
||||
|
||||
args := buildFFmpegArgsForInput(t.cfg, input, playlist, segments)
|
||||
args := buildFFmpegArgsForInput(job.config, input, playlist, segments)
|
||||
|
||||
cmd := exec.CommandContext(ctx, bin, args...) // #nosec G204 -- bin is resolved by resolveFFmpegPath and args are passed without a shell.
|
||||
cmd := exec.CommandContext(ctx, job.ffmpegPath, args...) // #nosec G204 -- the binary was validated before creating this job.
|
||||
setFFmpegSysProcAttr(cmd)
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
@@ -50,7 +38,8 @@ func (t *TranscoderService) runFFmpeg(ctx context.Context, job *hlsJob, input tr
|
||||
"start_sec": input.StartSec,
|
||||
})
|
||||
|
||||
if err := cmd.Run(); err != nil && !errors.Is(ctx.Err(), context.Canceled) {
|
||||
err := cmd.Run()
|
||||
if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
|
||||
t.log.Warn("ffmpeg exited",
|
||||
zap.String("media_id", job.mediaID),
|
||||
zap.Float64("start_sec", input.StartSec),
|
||||
@@ -59,11 +48,18 @@ func (t *TranscoderService) runFFmpeg(ctx context.Context, job *hlsJob, input tr
|
||||
}
|
||||
|
||||
t.mu.Lock()
|
||||
// Only drop the map entry if we are still the registered generation.
|
||||
// Only update the map entry if we are still the registered generation.
|
||||
if cur, ok := t.jobs[job.mediaID]; ok && cur == job {
|
||||
delete(t.jobs, job.mediaID)
|
||||
if err == nil && ctx.Err() == nil {
|
||||
job.completed = true
|
||||
} else {
|
||||
delete(t.jobs, job.mediaID)
|
||||
}
|
||||
}
|
||||
t.mu.Unlock()
|
||||
if err != nil || ctx.Err() != nil {
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
t.hub.Publish("transcode", map[string]any{
|
||||
"media_id": job.mediaID,
|
||||
@@ -73,19 +69,53 @@ func (t *TranscoderService) runFFmpeg(ctx context.Context, job *hlsJob, input tr
|
||||
}
|
||||
|
||||
func (t *TranscoderService) resolveFFmpegPath() (string, error) {
|
||||
t.mu.Lock()
|
||||
configuredPath := strings.TrimSpace(t.cfg.App.FFmpegPath)
|
||||
t.mu.Unlock()
|
||||
cfg := t.transcodeConfig()
|
||||
return t.resolveFFmpegPathFor(context.Background(), cfg.App.FFmpegPath, ffmpegArgSettingsFromConfig(cfg, nil).encoder)
|
||||
}
|
||||
|
||||
type ffmpegValidationCache struct {
|
||||
configuredPath string
|
||||
path string
|
||||
encoder string
|
||||
size int64
|
||||
modTime time.Time
|
||||
checkedAt time.Time
|
||||
}
|
||||
|
||||
func (t *TranscoderService) resolveFFmpegPathFor(ctx context.Context, configuredPath, encoder string) (string, error) {
|
||||
t.ffmpegMu.Lock()
|
||||
defer t.ffmpegMu.Unlock()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
configuredPath = strings.TrimSpace(configuredPath)
|
||||
cached := t.ffmpegCache
|
||||
if cached.path != "" && cached.encoder == encoder &&
|
||||
(configuredPath == cached.configuredPath || configuredPath == cached.path) &&
|
||||
time.Since(cached.checkedAt) < transcodeAvailabilityTTL {
|
||||
if info, err := os.Stat(cached.path); err == nil && info.Size() == cached.size && info.ModTime().Equal(cached.modTime) {
|
||||
return cached.path, nil
|
||||
}
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
for _, bin := range executableCandidates(configuredPath, "ffmpeg") {
|
||||
if err := validateFFmpegForTranscode(context.Background(), bin, t.effectiveEncoder()); err != nil {
|
||||
if err := validateFFmpegForTranscode(ctx, bin, encoder); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return "", ctx.Err()
|
||||
}
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
t.mu.Lock()
|
||||
t.cfg.App.FFmpegPath = bin
|
||||
t.mu.Unlock()
|
||||
if info, err := os.Stat(bin); err == nil {
|
||||
t.ffmpegCache = ffmpegValidationCache{configuredPath: configuredPath, path: bin, encoder: encoder,
|
||||
size: info.Size(), modTime: info.ModTime(), checkedAt: time.Now()}
|
||||
}
|
||||
config.RuntimeMu.Lock()
|
||||
if strings.TrimSpace(t.cfg.App.FFmpegPath) == configuredPath {
|
||||
t.cfg.App.FFmpegPath = bin
|
||||
}
|
||||
config.RuntimeMu.Unlock()
|
||||
return bin, nil
|
||||
}
|
||||
if lastErr != nil {
|
||||
@@ -95,10 +125,11 @@ func (t *TranscoderService) resolveFFmpegPath() (string, error) {
|
||||
}
|
||||
|
||||
func (t *TranscoderService) effectiveEncoder() string {
|
||||
if !t.cfg.Transcoder.HardwareAccel {
|
||||
cfg := t.transcodeConfig()
|
||||
if !cfg.Transcoder.HardwareAccel {
|
||||
return ""
|
||||
}
|
||||
return normalizedHardwareEncoder(t.cfg.Transcoder.Encoder)
|
||||
return normalizedHardwareEncoder(cfg.Transcoder.Encoder)
|
||||
}
|
||||
|
||||
func normalizedHardwareEncoder(encoder string) string {
|
||||
@@ -160,6 +191,7 @@ func hasFFmpegListEntry(output, name string) bool {
|
||||
|
||||
// HumanFFmpegProfile is exposed for the admin UI / settings view.
|
||||
func (t *TranscoderService) HumanFFmpegProfile() string {
|
||||
cfg := t.transcodeConfig()
|
||||
return fmt.Sprintf("ffmpeg=%s, encoder=%s, output=%s",
|
||||
t.cfg.App.FFmpegPath, t.cfg.Transcoder.Encoder, filepath.Join(t.cfg.Cache.CacheDir, "hls"))
|
||||
cfg.App.FFmpegPath, cfg.Transcoder.Encoder, filepath.Join(t.cfg.Cache.CacheDir, "hls"))
|
||||
}
|
||||
|
||||
@@ -6,41 +6,58 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/config"
|
||||
)
|
||||
|
||||
// WaitReady blocks (with a deadline) until the playlist file shows up on
|
||||
// disk for the *current* job generation. Stale playlists left behind by a
|
||||
// failed RemoveAll / still-exiting ffmpeg must not unblock a mid-file restart.
|
||||
func (t *TranscoderService) WaitReady(ctx context.Context, mediaID string, timeout time.Duration) bool {
|
||||
deadline := time.Now().Add(timeout)
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
ticker := time.NewTicker(250 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
t.mu.Lock()
|
||||
job, ok := t.jobs[mediaID]
|
||||
var started time.Time
|
||||
var completed bool
|
||||
if ok {
|
||||
started = job.startedAt
|
||||
completed = job.completed
|
||||
}
|
||||
t.mu.Unlock()
|
||||
if !ok {
|
||||
return false // A failed/exited job cannot become ready later.
|
||||
}
|
||||
if ok {
|
||||
if info, err := os.Stat(t.PlaylistPath(mediaID)); err == nil {
|
||||
// Allow a small clock skew; reject anything older than this job.
|
||||
if !info.ModTime().Before(started.Add(-2 * time.Second)) {
|
||||
t.mu.Lock()
|
||||
if j, exists := t.jobs[mediaID]; exists {
|
||||
if j, exists := t.jobs[mediaID]; exists && j == job {
|
||||
j.playlistOK = true
|
||||
t.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
t.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if time.Now().After(deadline) || ctx.Err() != nil {
|
||||
return false
|
||||
if completed {
|
||||
return false // A completed job cannot regenerate a missing playlist.
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-time.After(250 * time.Millisecond):
|
||||
case <-timer.C:
|
||||
return false
|
||||
case <-ticker.C:
|
||||
case <-job.done:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +118,9 @@ func (t *TranscoderService) Active() []ActiveJob {
|
||||
defer t.mu.Unlock()
|
||||
out := make([]ActiveJob, 0, len(t.jobs))
|
||||
for _, j := range t.jobs {
|
||||
if j.completed {
|
||||
continue
|
||||
}
|
||||
out = append(out, ActiveJob{
|
||||
MediaID: j.mediaID,
|
||||
Encoder: j.encoder,
|
||||
@@ -111,7 +131,19 @@ func (t *TranscoderService) Active() []ActiveJob {
|
||||
return out
|
||||
}
|
||||
|
||||
func (t *TranscoderService) runningJobCountLocked() int {
|
||||
n := 0
|
||||
for _, job := range t.jobs {
|
||||
if !job.completed {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *TranscoderService) maxConcurrent() int {
|
||||
config.RuntimeMu.RLock()
|
||||
defer config.RuntimeMu.RUnlock()
|
||||
if t.cfg.Transcoder.MaxConcurrent <= 0 {
|
||||
return 1
|
||||
}
|
||||
@@ -119,6 +151,8 @@ func (t *TranscoderService) maxConcurrent() int {
|
||||
}
|
||||
|
||||
func (t *TranscoderService) idleTimeout() time.Duration {
|
||||
config.RuntimeMu.RLock()
|
||||
defer config.RuntimeMu.RUnlock()
|
||||
if t.cfg.Transcoder.IdleTimeoutSeconds <= 0 {
|
||||
return 120 * time.Second
|
||||
}
|
||||
@@ -137,21 +171,38 @@ func (t *TranscoderService) monitorIdle(ctx context.Context, job *hlsJob) {
|
||||
case <-ticker.C:
|
||||
t.mu.Lock()
|
||||
current, ok := t.jobs[job.mediaID]
|
||||
if !ok {
|
||||
if !ok || current != job {
|
||||
t.mu.Unlock()
|
||||
return
|
||||
}
|
||||
idleFor := time.Since(current.lastAccess)
|
||||
t.mu.Unlock()
|
||||
if idleFor >= timeout {
|
||||
t.log.Info("transcode idle timeout",
|
||||
zap.String("media_id", job.mediaID),
|
||||
zap.Duration("idle_for", idleFor),
|
||||
zap.Duration("timeout", timeout),
|
||||
)
|
||||
t.StopJob(job.mediaID)
|
||||
if idleFor >= timeout && t.stopIdleJob(job, timeout) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TranscoderService) stopIdleJob(job *hlsJob, timeout time.Duration) bool {
|
||||
gate := t.mediaStartGate(job.mediaID)
|
||||
gate.Lock()
|
||||
defer gate.Unlock()
|
||||
t.mu.Lock()
|
||||
current := t.jobs[job.mediaID]
|
||||
if current != job {
|
||||
t.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
idleFor := time.Since(job.lastAccess)
|
||||
if idleFor < timeout {
|
||||
t.mu.Unlock()
|
||||
return false
|
||||
}
|
||||
t.detachJobLocked(job.mediaID)
|
||||
t.mu.Unlock()
|
||||
waitJobExit(job, 12*time.Second)
|
||||
t.log.Info("transcode idle timeout", zap.String("media_id", job.mediaID),
|
||||
zap.Duration("idle_for", idleFor), zap.Duration("timeout", timeout))
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/config"
|
||||
"github.com/truewhile/MeBox/internal/model"
|
||||
"github.com/truewhile/MeBox/internal/repository"
|
||||
)
|
||||
|
||||
func TestHLSCompletedTranscodeStaysReadyAndIsReused(t *testing.T) {
|
||||
bin, err := exec.LookPath("ffmpeg")
|
||||
if err != nil {
|
||||
t.Skip("ffmpeg is required for the real HLS lifecycle test")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
source := filepath.Join(t.TempDir(), "short.mp4")
|
||||
cmd := exec.CommandContext(ctx, bin, "-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc=size=64x64:rate=10", "-t", "1", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-threads", "1", source)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("generate source: %v: %s", err, output)
|
||||
}
|
||||
cfg := &config.Config{}
|
||||
cfg.App.FFmpegPath = bin
|
||||
cfg.Cache.CacheDir = t.TempDir()
|
||||
cfg.Transcoder.Enabled = true
|
||||
cfg.Transcoder.MaxConcurrent = 1
|
||||
cfg.Transcoder.MaxHeight = 64
|
||||
cfg.Transcoder.SegmentSeconds = 1
|
||||
cfg.Transcoder.Threads = 1
|
||||
db := newServiceTestDB(t)
|
||||
media := model.Media{Path: source, DurationSec: 1}
|
||||
if err := db.Create(&media).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc := NewTranscoderService(cfg, zap.NewNop(), repository.New(db), NewHub(zap.NewNop()))
|
||||
t.Cleanup(svc.StopAll)
|
||||
playlist, err := svc.EnsureJob(ctx, media.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc.mu.Lock()
|
||||
job := svc.jobs[media.ID]
|
||||
svc.mu.Unlock()
|
||||
if job == nil {
|
||||
t.Fatal("transcode lost its job before the playlist could be read")
|
||||
}
|
||||
select {
|
||||
case <-job.done:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("short transcode did not finish")
|
||||
}
|
||||
data, err := os.ReadFile(playlist)
|
||||
if err != nil || !strings.Contains(string(data), "#EXT-X-ENDLIST") {
|
||||
t.Fatalf("transcode did not produce a complete playlist: err=%v, data=%s", err, data)
|
||||
}
|
||||
if !svc.WaitReady(ctx, media.ID, 100*time.Millisecond) {
|
||||
t.Fatal("completed HLS playlist is incorrectly reported as not ready")
|
||||
}
|
||||
if len(svc.Active()) != 0 {
|
||||
t.Fatal("completed transcode still occupies an active job slot")
|
||||
}
|
||||
if _, err := svc.EnsureJob(ctx, media.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc.mu.Lock()
|
||||
reused := svc.jobs[media.ID] == job
|
||||
svc.mu.Unlock()
|
||||
if !reused {
|
||||
t.Fatal("refreshing a completed playlist started a second transcode")
|
||||
}
|
||||
other := model.Media{Path: source + ".second", DurationSec: 1}
|
||||
if err := os.WriteFile(other.Path, mustReadTestFile(t, source), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&other).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := svc.EnsureJob(ctx, other.ID); err != nil {
|
||||
t.Fatalf("completed cache must not consume MaxConcurrent: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustReadTestFile(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func TestHLSWaitReadyReturnsImmediatelyForMissingJob(t *testing.T) {
|
||||
svc := NewTranscoderService(&config.Config{}, zap.NewNop(), nil, NewHub(zap.NewNop()))
|
||||
started := time.Now()
|
||||
if svc.WaitReady(t.Context(), "failed", time.Second) {
|
||||
t.Fatal("missing job must not be ready")
|
||||
}
|
||||
if elapsed := time.Since(started); elapsed >= 500*time.Millisecond {
|
||||
t.Fatalf("missing/failed job waited for the readiness deadline: %v", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func countingFFmpeg(t *testing.T) string {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("test ffmpeg uses a POSIX shell")
|
||||
}
|
||||
bin := filepath.Join(t.TempDir(), "ffmpeg")
|
||||
script := `#!/bin/sh
|
||||
printf '%s\n' "$*" >> "$0.calls"
|
||||
case " $* " in
|
||||
*" -encoders "*) printf ' V..... libx264\n V..... h264_nvenc\n' ;;
|
||||
*" -muxers "*) printf ' E hls\n' ;;
|
||||
esac
|
||||
`
|
||||
if err := os.WriteFile(bin, []byte(script), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
func TestFFmpegValidationIsCachedAndInvalidatedByEncoder(t *testing.T) {
|
||||
bin := countingFFmpeg(t)
|
||||
cfg := &config.Config{}
|
||||
cfg.App.FFmpegPath = bin
|
||||
svc := NewTranscoderService(cfg, zap.NewNop(), nil, NewHub(zap.NewNop()))
|
||||
for range 3 {
|
||||
if _, err := svc.resolveFFmpegPath(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
calls := strings.Count(string(mustReadTestFile(t, bin+".calls")), "\n")
|
||||
if calls != 2 {
|
||||
t.Fatalf("repeated resolution spawned ffmpeg %d times, want one encoder/muxer probe pair", calls)
|
||||
}
|
||||
ApplyRuntimeSetting(cfg, "transcoder.hardware_accel", "true")
|
||||
ApplyRuntimeSetting(cfg, "transcoder.encoder", "nvenc")
|
||||
if _, err := svc.resolveFFmpegPath(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls = strings.Count(string(mustReadTestFile(t, bin+".calls")), "\n")
|
||||
if calls != 4 {
|
||||
t.Fatalf("encoder change must revalidate capabilities, calls=%d", calls)
|
||||
}
|
||||
changed := time.Now().Add(time.Second)
|
||||
if err := os.Chtimes(bin, changed, changed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := svc.resolveFFmpegPath(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls = strings.Count(string(mustReadTestFile(t, bin+".calls")), "\n")
|
||||
if calls != 6 {
|
||||
t.Fatalf("binary replacement must invalidate validation, calls=%d", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHLSIdleCleanupPreservesReplacementAndRecentActivity(t *testing.T) {
|
||||
svc := NewTranscoderService(&config.Config{}, zap.NewNop(), nil, NewHub(zap.NewNop()))
|
||||
done := make(chan struct{})
|
||||
close(done)
|
||||
cancelled := false
|
||||
old := &hlsJob{mediaID: "m", lastAccess: time.Now().Add(-time.Hour)}
|
||||
current := &hlsJob{mediaID: "m", lastAccess: time.Now(), done: done, cancel: func() { cancelled = true }}
|
||||
svc.jobs["m"] = current
|
||||
if !svc.stopIdleJob(old, time.Minute) || cancelled || svc.jobs["m"] != current {
|
||||
t.Fatal("an old watchdog must leave the replacement generation intact")
|
||||
}
|
||||
if svc.stopIdleJob(current, time.Minute) || cancelled {
|
||||
t.Fatal("recent segment access must prevent idle cancellation")
|
||||
}
|
||||
current.lastAccess = time.Now().Add(-time.Hour)
|
||||
if !svc.stopIdleJob(current, time.Minute) || !cancelled || svc.jobs["m"] != nil {
|
||||
t.Fatal("an idle generation must release its resources")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHLSOriginalQualityValidatesSoftwareEncoder(t *testing.T) {
|
||||
bin := countingFFmpeg(t)
|
||||
softwareOnly := strings.ReplaceAll(string(mustReadTestFile(t, bin)), ` V..... h264_nvenc\n`, "")
|
||||
if err := os.WriteFile(bin, []byte(softwareOnly), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
t.Chdir(t.TempDir())
|
||||
cfg := &config.Config{}
|
||||
cfg.App.FFmpegPath = bin
|
||||
cfg.Transcoder.Enabled = true
|
||||
cfg.Transcoder.HardwareAccel = true
|
||||
cfg.Transcoder.Encoder = "nvenc"
|
||||
cfg.Cache.CacheDir = t.TempDir()
|
||||
db := newServiceTestDB(t)
|
||||
source := filepath.Join(t.TempDir(), "movie.mkv")
|
||||
if err := os.WriteFile(source, []byte("source"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
media := model.Media{Path: source, DurationSec: 60}
|
||||
if err := db.Create(&media).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc := NewTranscoderService(cfg, zap.NewNop(), repository.New(db), NewHub(zap.NewNop()))
|
||||
t.Cleanup(svc.StopAll)
|
||||
if _, err := svc.EnsureJobFromSubtitleQuality(t.Context(), media.ID, 0, 1, -1, "source"); err != nil {
|
||||
t.Fatalf("source quality uses software and must not require NVENC: %v", err)
|
||||
}
|
||||
svc.mu.Lock()
|
||||
job := svc.jobs[media.ID]
|
||||
svc.mu.Unlock()
|
||||
select {
|
||||
case <-job.done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("fake ffmpeg did not finish")
|
||||
}
|
||||
calls := string(mustReadTestFile(t, bin+".calls"))
|
||||
if strings.Count(calls, "\n") != 3 || !strings.Contains(calls, "-c:v libx264") {
|
||||
t.Fatalf("startup must probe once and encode with libx264, calls=%s", calls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user