* feat(nodeconfig): harden config watcher for integrated-mode use - RequestReload(): non-blocking, coalescing reload nudge that runs on the poll goroutine, so concurrent requests can never swap a stale snapshot over a newer one (unlike ForceReload from request handlers) - Skip OnChange callbacks when the reloaded config is deep-equal to the previous one, so the 60s poll doesn't fire rebuild/log callbacks on no-op reloads - Add RedisURL to BootstrapOverrides; previously a reload clobbered an env-provided Redis URL in the live config - Split reload into fetchSettings/applySettings and add unit tests Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(config): hot-reload config watcher in integrated mode Start nodeconfig.Watcher in integrated/api mode (previously only proxy/ transcode worker modes hot-reloaded). Expose the live config to the API and jellycompat routers via func-typed LiveConfig/OnConfigChange fields with nil fallbacks to the startup snapshot, and wire the admin settings update hook to RequestReload so same-process changes apply immediately even without Redis. No consumer reads the live config yet — conversions land separately. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(admin): truthful restart-required banner for settings saves The settings UI showed 'restart required' after every save regardless of the key. The backend now classifies each key via a central registry (internal/config/restart_keys.go) and PUT /admin/settings/{key} reports restart_required per key; useSettingsForm only raises the banner when a saved key actually needs a restart (and keeps it raised until restart). The registry is conservative: every currently startup-frozen key is marked restart-required; subsequent hot-reload conversions shrink it. Settings read live from the settings repo (branding, overlays, markers, download.*, ...) default to no-restart. DownloadSettings/OverlaySettings drop their hardcoded restartRequired={false} special-casing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(logging): hot-reload server.log_level and server.log_quiet Share one slog.LevelVar across the handler chain and make logfilter.Handler's quiet-prefix list an atomic pointer shared with WithAttrs/WithGroup clones (New previously returned the inner handler unwrapped when the quiet list was empty, leaving nothing to update). The integrated-mode config watcher now applies both settings live; their keys leave the restart-required registry. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(auth): hot-reload access/refresh token expiries JWTService stores expiries as atomics with a SetExpiries hook; all three instances (main API, ABS compat, jellycompat) re-apply them on config reload. Applies to newly issued tokens; outstanding tokens keep their original expiry. The JWT secret stays fixed for the process lifetime. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(playback): read transcode config live at session start The playback and stream handlers pull ffmpeg path / hwaccel / transcode dir from the live config when starting a transcode or extracting subtitles, instead of values frozen at router construction. Each session snapshots the config once so its output dir and binary stay consistent. Also fixes a real bug: playback.hw_device was parsed into the config but never wired into the integrated-mode handler, so local transcodes always ran with an empty HWDevice while transcode nodes honored it. playback.transcode_dir leaves the restart-required registry (the handler is its only consumer); ffmpeg_path/hw_accel stay restart-required until scanner/chapterthumbs/audiobook consumers convert. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(jellycompat): read compat identity settings live per request System/Auth handlers take a config provider instead of the startup snapshot, so jellyfin_compat.public_url, .server_name, and .emulated_server_version apply without restart. server_id stays restart-required (generate-once, baked into the resource mapper), as do the session-store TTLs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(scanner,metadata,mdblist): hot-reload worker pools and API key scanner.workers, matcher.workers/batch_size, metadata.cache_images, and mdblist.api_key convert to atomic fields with setters wired to the config watcher. Worker counts apply on the next scan/match cycle (the loops read them per cycle); the MDBList key applies to the next request. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(ai): hot-reload AI connection, models, toggles, and quotas The shared llm.Client holds its config behind an atomic pointer (UpdateConfig; each request snapshots once), and the subtitle/metadata AI services gain UpdateConfig plus setters on the translator (batching) and Whisper transcriber (ffmpeg path, chunk seconds). The router derives their configs from shared helpers used both at construction and in OnConfigChange callbacks, re-evaluating the chat-only-gateway transcribe guard on each reload and warning only when it newly fires. Everything on the AI Services page now applies without restart except ai.max_concurrent_jobs (fixed-capacity dispatch semaphore). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(playback): wire transcode_enabled; remove dead playback/scanner knobs playback.transcode_enabled was parsed into the config but the resolver always received a hardcoded true — the admin toggle did nothing. It now reads the live config per playback start, so disabling transcodes applies without restart. Remove settings that were wired to nothing so 'save + restart' stops pretending: playback.allow_hevc_encoding (resolver field never assigned), playback.transcode_ahead_segments and playback.segment_duration (parsed, never consumed — segment duration is per-session from the client), scanner.file_removal_grace (DeleteMissing is never called). UI fields removed and the config struct fields pruned so they don't resurrect; YAML import still tolerates the legacy keys. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
262 lines
8.1 KiB
Go
262 lines
8.1 KiB
Go
// Package playback provides play method resolution, streaming, transcoding,
|
|
// and session management for Silo.
|
|
package playback
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/access"
|
|
"github.com/Silo-Server/silo-server/internal/models"
|
|
)
|
|
|
|
// PlayMethod represents how a media file will be streamed.
|
|
type PlayMethod string
|
|
|
|
const (
|
|
PlayDirect PlayMethod = "direct"
|
|
PlayRemux PlayMethod = "remux"
|
|
PlayTranscode PlayMethod = "transcode"
|
|
)
|
|
|
|
// ClientCapabilities describes what the client can play natively.
|
|
//
|
|
// AudioPassthroughCodecs are codecs the connected audio sink can decode bit-
|
|
// exact (e.g. an HDMI AVR accepting EAC3/Atmos). They are treated as supported
|
|
// audio codecs for resolution purposes so we can stream-copy surround audio
|
|
// instead of downmixing+re-encoding to AAC. Distinct from CodecsAudio, which
|
|
// describes what the client itself can decode.
|
|
type ClientCapabilities struct {
|
|
CodecsVideo []string `json:"codecs_video"` // e.g., h264, hevc, av1
|
|
CodecsAudio []string `json:"codecs_audio"` // e.g., aac, opus, flac
|
|
AudioPassthroughCodecs []string `json:"audio_passthrough_codecs,omitempty"`
|
|
Containers []string `json:"containers"` // e.g., mp4, webm, mkv
|
|
MaxResolution string `json:"max_resolution"` // e.g., 1080p, 2160p
|
|
HDR bool `json:"hdr"`
|
|
}
|
|
|
|
// AdminSettings controls server-side playback constraints.
|
|
type AdminSettings struct {
|
|
TranscodeEnabled bool
|
|
Allow4KTranscode bool
|
|
}
|
|
|
|
// PlayDecision is the result of resolving how to play a file.
|
|
type PlayDecision struct {
|
|
Method PlayMethod
|
|
File *models.MediaFile
|
|
Reason string // human-readable explanation
|
|
TranscodeAudio bool // true when remuxing should transcode audio to AAC
|
|
}
|
|
|
|
type VersionSelectionFilter struct {
|
|
EditionKey string
|
|
PresentationKind string
|
|
PresentationGroupKey string
|
|
}
|
|
|
|
// Resolve determines the play method for a given file and client capabilities.
|
|
// Returns direct if client supports codec+container, remux if codec matches
|
|
// but container doesn't, transcode otherwise.
|
|
func Resolve(file *models.MediaFile, caps ClientCapabilities, settings AdminSettings) *PlayDecision {
|
|
// Check if client supports the video codec.
|
|
videoOK := containsStr(caps.CodecsVideo, file.CodecVideo)
|
|
// Audio is considered OK if the client can decode the codec itself OR its
|
|
// sink can passthrough it. Passthrough lets us stream-copy surround audio
|
|
// (EAC3/AC3/DTS/TrueHD) to HDMI AVRs instead of re-encoding to stereo AAC.
|
|
audioOK := containsStr(caps.CodecsAudio, file.CodecAudio) ||
|
|
containsStr(caps.AudioPassthroughCodecs, file.CodecAudio)
|
|
// Check if client supports the container.
|
|
containerOK := containsStr(caps.Containers, file.Container)
|
|
|
|
// Check resolution constraint.
|
|
if !resolutionFits(file.Resolution, caps.MaxResolution) {
|
|
if !settings.TranscodeEnabled {
|
|
return &PlayDecision{
|
|
Method: PlayDirect,
|
|
File: file,
|
|
Reason: "file resolution exceeds client max but transcode disabled; attempting direct",
|
|
}
|
|
}
|
|
// Need transcode to lower resolution.
|
|
return &PlayDecision{
|
|
Method: PlayTranscode,
|
|
File: file,
|
|
Reason: "file resolution exceeds client max resolution",
|
|
}
|
|
}
|
|
|
|
// Case 1: Client supports codec + container → direct play.
|
|
if videoOK && audioOK && containerOK {
|
|
return &PlayDecision{
|
|
Method: PlayDirect,
|
|
File: file,
|
|
Reason: "client supports all codecs and container",
|
|
}
|
|
}
|
|
|
|
// Case 2: Client supports codecs but not container → remux.
|
|
if videoOK && audioOK && !containerOK {
|
|
return &PlayDecision{
|
|
Method: PlayRemux,
|
|
File: file,
|
|
Reason: "client supports codecs but not container; remuxing",
|
|
}
|
|
}
|
|
|
|
// Case 3: Video OK but audio codec unsupported → remux with audio transcode.
|
|
// This is much cheaper than a full video transcode.
|
|
if videoOK && !audioOK {
|
|
return &PlayDecision{
|
|
Method: PlayRemux,
|
|
File: file,
|
|
TranscodeAudio: true,
|
|
Reason: "client supports video codec but not audio; remuxing with audio transcode to AAC",
|
|
}
|
|
}
|
|
|
|
// Case 4: Client can't play video codec → full transcode.
|
|
if !settings.TranscodeEnabled {
|
|
return &PlayDecision{
|
|
Method: PlayDirect,
|
|
File: file,
|
|
Reason: "transcode needed but disabled; attempting direct play",
|
|
}
|
|
}
|
|
|
|
return &PlayDecision{
|
|
Method: PlayTranscode,
|
|
File: file,
|
|
Reason: "client cannot play video codec; transcoding",
|
|
}
|
|
}
|
|
|
|
// SelectVersion chooses the best file version from a list of available files
|
|
// based on client capabilities and admin settings.
|
|
// Priority: direct-playable > remux > transcode, then highest quality, then smallest file.
|
|
func SelectVersion(files []*models.MediaFile, caps ClientCapabilities, settings AdminSettings) (*PlayDecision, error) {
|
|
return SelectVersionFiltered(files, caps, settings, VersionSelectionFilter{})
|
|
}
|
|
|
|
// SelectVersionFiltered chooses the best interchangeable file version within
|
|
// one edition/presentation group.
|
|
func SelectVersionFiltered(
|
|
files []*models.MediaFile,
|
|
caps ClientCapabilities,
|
|
settings AdminSettings,
|
|
filter VersionSelectionFilter,
|
|
) (*PlayDecision, error) {
|
|
if len(files) == 0 {
|
|
return nil, ErrNoVersions
|
|
}
|
|
|
|
candidates := files
|
|
if filter.EditionKey != "" || filter.PresentationKind != "" || filter.PresentationGroupKey != "" {
|
|
filtered := make([]*models.MediaFile, 0, len(files))
|
|
for _, f := range files {
|
|
if filter.EditionKey != "" && f.EditionKey != filter.EditionKey {
|
|
continue
|
|
}
|
|
if filter.PresentationKind != "" && f.PresentationKind != filter.PresentationKind {
|
|
continue
|
|
}
|
|
if filter.PresentationGroupKey != "" && f.PresentationGroupKey != filter.PresentationGroupKey {
|
|
continue
|
|
}
|
|
filtered = append(filtered, f)
|
|
}
|
|
if len(filtered) > 0 {
|
|
candidates = filtered
|
|
}
|
|
}
|
|
|
|
var directFiles, remuxFiles, transcodeFiles []*PlayDecision
|
|
|
|
for _, f := range candidates {
|
|
// Filter by client max resolution.
|
|
if !resolutionFits(f.Resolution, caps.MaxResolution) {
|
|
// If 4K transcoding disabled and this is 4K, skip entirely.
|
|
if is4K(f.Resolution) && !settings.Allow4KTranscode {
|
|
continue
|
|
}
|
|
}
|
|
|
|
decision := Resolve(f, caps, settings)
|
|
|
|
switch decision.Method {
|
|
case PlayDirect:
|
|
directFiles = append(directFiles, decision)
|
|
case PlayRemux:
|
|
remuxFiles = append(remuxFiles, decision)
|
|
case PlayTranscode:
|
|
transcodeFiles = append(transcodeFiles, decision)
|
|
}
|
|
}
|
|
|
|
// Prefer direct > remux > transcode.
|
|
if len(directFiles) > 0 {
|
|
return bestQuality(directFiles), nil
|
|
}
|
|
if len(remuxFiles) > 0 {
|
|
return bestQuality(remuxFiles), nil
|
|
}
|
|
if len(transcodeFiles) > 0 {
|
|
return bestQuality(transcodeFiles), nil
|
|
}
|
|
|
|
// Fallback: use first file with direct play.
|
|
return &PlayDecision{
|
|
Method: PlayDirect,
|
|
File: candidates[0],
|
|
Reason: "no compatible version found; falling back to first file",
|
|
}, nil
|
|
}
|
|
|
|
// bestQuality picks the highest quality file. Among ties, picks smallest file.
|
|
func bestQuality(decisions []*PlayDecision) *PlayDecision {
|
|
best := decisions[0]
|
|
for _, d := range decisions[1:] {
|
|
if access.CompareQuality(d.File.Resolution, best.File.Resolution) > 0 {
|
|
best = d
|
|
} else if access.CompareQuality(d.File.Resolution, best.File.Resolution) == 0 && d.File.FileSize < best.File.FileSize {
|
|
best = d
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
// resolutionOrder returns a numeric value for sorting resolutions.
|
|
func resolutionOrder(res string) int {
|
|
switch {
|
|
case access.CompareQuality(res, "4320p") == 0:
|
|
return 5
|
|
case access.CompareQuality(res, "2160p") == 0:
|
|
return 4
|
|
case access.CompareQuality(res, "1080p") == 0:
|
|
return 3
|
|
case access.CompareQuality(res, "720p") == 0:
|
|
return 2
|
|
case access.CompareQuality(res, "480p") == 0:
|
|
return 1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// resolutionFits checks if the file resolution fits within the client's max.
|
|
func resolutionFits(fileRes, maxRes string) bool {
|
|
if maxRes == "" {
|
|
return true // no constraint
|
|
}
|
|
return resolutionOrder(fileRes) <= resolutionOrder(maxRes)
|
|
}
|
|
|
|
// is4K returns true if the resolution is 2160p or higher.
|
|
func is4K(res string) bool {
|
|
return access.CompareQuality(res, "2160p") >= 0
|
|
}
|
|
|
|
// containsStr checks if a slice contains a string.
|
|
func containsStr(slice []string, s string) bool {
|
|
return slices.Contains(slice, s)
|
|
}
|