diff --git a/server/jellyfin/README.md b/server/jellyfin/README.md index c0cf2a18e..802d8b414 100644 --- a/server/jellyfin/README.md +++ b/server/jellyfin/README.md @@ -283,7 +283,8 @@ track and Feishin on every song change, so lyric-less tracks are the hot path. Finamp opens its lyrics view only when the track has a `Lyric` `MediaStream` (`HasLyrics` is just a list badge). Browse lists set both from embedded lyrics only. `PlaybackInfo` runs the full pipeline -per track, so sidecar and plugin lyrics show up there. Feishin also requires server version ≥ 10.9 +per track with a short timeout bound, so sidecar and cached/fast plugin lyrics show up there without +stalling playback start on a slow cold plugin lookup. Feishin also requires server version ≥ 10.9 (we advertise 12.1.0). ## AudioMuse-AI compatible endpoints diff --git a/server/jellyfin/lyrics.go b/server/jellyfin/lyrics.go index 7299e5143..a0d769043 100644 --- a/server/jellyfin/lyrics.go +++ b/server/jellyfin/lyrics.go @@ -3,12 +3,39 @@ package jellyfin import ( "context" "net/http" + "time" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/server/jellyfin/dto" ) +// playbackInfoLyricsTimeout bounds how long getPlaybackInfo waits for lyrics before +// returning without the Lyric MediaStream; slow plugin lookups finish warming the cache in core/lyrics. +var playbackInfoLyricsTimeout = 100 * time.Millisecond + +// playbackLyrics resolves lyrics for getPlaybackInfo within playbackInfoLyricsTimeout. +func (api *Router) playbackLyrics(ctx context.Context, mf *model.MediaFile) model.LyricList { + resCh := make(chan model.LyricList, 1) + go func() { + defer close(resCh) + defer func() { + if r := recover(); r != nil { + log.Error(ctx, "Panic recovered in playback lyrics lookup", "id", mf.ID, "err", r) + } + }() + resCh <- api.lyricsFor(ctx, mf) + }() + select { + case list := <-resCh: + return list + case <-ctx.Done(): + return nil + case <-time.After(playbackInfoLyricsTimeout): + return nil + } +} + // lyricsFor resolves lyrics through the full source pipeline (embedded, sidecar, plugins). func (api *Router) lyricsFor(ctx context.Context, mf *model.MediaFile) model.LyricList { list, err := api.lyrics.GetLyrics(ctx, mf) diff --git a/server/jellyfin/lyrics_test.go b/server/jellyfin/lyrics_test.go index 1a0ee41ce..4ca89c551 100644 --- a/server/jellyfin/lyrics_test.go +++ b/server/jellyfin/lyrics_test.go @@ -16,11 +16,27 @@ import ( // fakeLyricsService returns canned lyrics per media-file ID. type fakeLyricsService struct { - lyrics map[string]model.LyricList - err error + lyrics map[string]model.LyricList + err error + panicVal any + blockCh <-chan struct{} + doneCh chan<- struct{} } -func (f *fakeLyricsService) GetLyrics(_ context.Context, mf *model.MediaFile) (model.LyricList, error) { +func (f *fakeLyricsService) GetLyrics(ctx context.Context, mf *model.MediaFile) (model.LyricList, error) { + if f.doneCh != nil { + defer close(f.doneCh) + } + if f.blockCh != nil { + select { + case <-f.blockCh: + case <-ctx.Done(): + return nil, ctx.Err() + } + } + if f.panicVal != nil { + panic(f.panicVal) + } if f.err != nil { return nil, f.err } diff --git a/server/jellyfin/stream.go b/server/jellyfin/stream.go index 9a627ee5e..5b2a4f978 100644 --- a/server/jellyfin/stream.go +++ b/server/jellyfin/stream.go @@ -50,10 +50,10 @@ func (api *Router) getPlaybackInfo(w http.ResponseWriter, r *http.Request) { return } src := dto.MediaSourceFromMediaFile(*mf) - // The mapper only sees embedded lyrics; per-track we can afford the full pipeline - // (sidecars, plugins) so Finamp's Lyric-stream gate reflects every source. + // The mapper only sees embedded lyrics; also check external sources (sidecars, plugins) + // within playbackInfoLyricsTimeout so Finamp's Lyric-stream gate reflects them when ready. if !slices.ContainsFunc(src.MediaStreams, func(s dto.MediaStream) bool { return s.Type == "Lyric" }) { - if _, found := servableLyric(api.lyricsFor(r.Context(), mf)); found { + if _, found := servableLyric(api.playbackLyrics(r.Context(), mf)); found { src.MediaStreams = append(src.MediaStreams, dto.MediaStream{ Type: "Lyric", Index: len(src.MediaStreams), IsExternal: true, }) diff --git a/server/jellyfin/stream_test.go b/server/jellyfin/stream_test.go index dc670376d..e8a6b5005 100644 --- a/server/jellyfin/stream_test.go +++ b/server/jellyfin/stream_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "strings" + "time" "github.com/navidrome/navidrome/core/stream" "github.com/navidrome/navidrome/model" @@ -161,6 +162,55 @@ var _ = Describe("Stream", func() { Expect(res.MediaSources[0].Id).To(Equal(dto.EncodeID(testID("s-err")))) Expect(lyricStreams(res)).To(BeEmpty()) }) + + It("does not block playback info when lyrics lookup is slow", func() { + origTimeout := playbackInfoLyricsTimeout + playbackInfoLyricsTimeout = 20 * time.Millisecond + DeferCleanup(func() { playbackInfoLyricsTimeout = origTimeout }) + + blockCh := make(chan struct{}) + doneCh := make(chan struct{}) + DeferCleanup(func() { + close(blockCh) + Eventually(doneCh).Should(BeClosed()) + }) + + ds.MediaFile().(*tests.MockMediaFileRepo).SetData(model.MediaFiles{ + {ID: testID("s1"), Title: "Slow Song", Suffix: "mp3", Duration: 100, Size: 1000, LibraryID: 1}, + }) + api.lyrics = &fakeLyricsService{ + lyrics: map[string]model.LyricList{ + testID("s1"): {{Kind: "main", Synced: true, Line: []model.Line{{Value: "delayed"}}}}, + }, + blockCh: blockCh, + doneCh: doneCh, + } + + start := time.Now() + res := playbackInfo() + duration := time.Since(start) + Expect(duration).To(BeNumerically("<", 150*time.Millisecond)) + Expect(res.MediaSources).To(HaveLen(1)) + Expect(lyricStreams(res)).To(BeEmpty()) + }) + + It("recovers panics in detached lyrics lookup and returns 200 without Lyric stream", func() { + ds.MediaFile().(*tests.MockMediaFileRepo).SetData(model.MediaFiles{ + {ID: testID("s-panic"), Title: "Panic Song", Suffix: "mp3", Duration: 100, Size: 1000, LibraryID: 1}, + }) + api.lyrics = &fakeLyricsService{panicVal: "lyrics plugin crash"} + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/Items/"+dto.EncodeID(testID("s-panic"))+"/PlaybackInfo", nil).WithContext(ctxUser()) + r = withChiURLParam(r, "itemId", dto.EncodeID(testID("s-panic"))) + api.getPlaybackInfo(w, r) + + Expect(w.Code).To(Equal(http.StatusOK)) + var res dto.PlaybackInfoResponse + Expect(json.Unmarshal(w.Body.Bytes(), &res)).To(Succeed()) + Expect(res.MediaSources).To(HaveLen(1)) + Expect(lyricStreams(res)).To(BeEmpty()) + }) }) Describe("streamAudio", func() {