mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-12 20:37:12 +02:00
* feat(jellyfin): advertise Jellyfin server version 12.1.0
Streamyfin, jellyfin-android and jellyfin-androidtv refuse servers older than
10.10, Swiftfin warns below 12.0, and @jellyfin/sdk flags anything below its
minimum as unsupported, so 10.9.11 locked those clients out. 12.1.0 is the
current Jellyfin release (after 10.11 Jellyfin renumbered to 12.0). No client
checked has an upper bound or assumes the major is 10, and the value keeps
three parts because the Kotlin SDK and Swiftfin reject two-part versions.
* feat(jellyfin): acknowledge POST /Sessions/Playing/Ping
Jellyfin clients ping this endpoint to keep a transcode job alive while
paused. Navidrome ties transcodes to the stream request, so there is nothing
to keep alive; answer 204 like Jellyfin instead of a 404. It shares one no-op
handler with Sessions/Capabilities, renamed to acknowledge.
* feat(jellyfin): reorder playlist entries via Items/{entryId}/Move
Adds POST /Playlists/{id}/Items/{entryId}/Move/{newIndex}, which clients use
to reorder playlists. It maps the entry's PlaylistItemId (its position) and
Jellyfin's zero-based newIndex onto the existing core ReorderTrack, which
enforces ownership. As in Jellyfin, an index past the end appends and an
unknown entry is a no-op; out-of-range positions never reach Reorder, which
would otherwise shift unrelated rows.
* feat(jellyfin): honor position when adding items to a playlist
POST /Playlists/{id}/Items takes an optional zero-based position (added to
the Jellyfin spec in 12.0). Match Jellyfin: zero or negative prepends, past
the end appends, otherwise the new items are inserted at that index in the
order they were added.
Adds PlaylistTrackRepository.Insert and core playlists.InsertTracks, which
shift the following entries and insert in one transaction, instead of
appending and moving each new track with its own ReorderTrack call.
* feat(jellyfin): add type-specific InstantMix routes
Jellyfin exposes InstantMix under Songs/, Albums/, Artists/ and Playlists/
as well as Items/, plus the legacy Artists/InstantMix and
MusicGenres/InstantMix forms that take the seed as ?id=. Clients generated
from the Jellyfin SDKs call the type-specific routes, which 404ed. All of them
now share the existing Items/{id}/InstantMix handler; the external provider
already builds mixes from song, album, artist, playlist and genre seeds.
* feat(jellyfin): honor Width, Height and Fill* image size params
The image endpoint only read MaxWidth/MaxHeight, so clients that size covers
with fillWidth/fillHeight (Manet, Finamp) or width/height got the full-size
original on a cold artwork cache: a 578 KB PNG instead of a 13 KB resize.
Jellyfin applies Width/Height, caps them with MaxWidth/MaxHeight, then
shrinks to the smallest size that still covers the Fill box. Navidrome
resizes on one dimension, so the tightest bound wins and a fill box counts
as its larger side.
* feat(jellyfin): answer HEAD on audio, file and image routes
Fintunes sends HEAD to /Audio/{id}/universal to read the content type and
detect direct play (a Content-Length means direct play), and to the audio and
image URLs before a download, aborting the download when it fails. Those routes
were GET-only, so HEAD got a 404. HEAD now reuses the GET handlers. Direct
play goes through Stream.Serve, which already answers HEAD; a transcode
answers with the target content type and no length without starting ffmpeg,
so a probe never costs a transcode.
* fix(jellyfin): clamp playlist move and insert positions before adding one
movePlaylistItem computed min(newIndex+1, SongCount): newIndex=MaxInt wrapped
to a negative position, and Reorder then left the playlist with a gap
(positions 2, 3, 999998), making the moved entry unmovable. Clamp against the
playlist length first. addToPlaylist's min(position, MaxInt32)+1 wrapped on
32-bit builds and req.Int truncated large values there, so a far-past-the-end
position prepended; parse as int64 and clamp before converting.
* fix(playlists): validate reorder positions inside the write transaction
Reorder never checked its positions, so a source outside the playlist or a
destination past its end shifted rows around a missing entry and left a gap
(e.g. ids 1 and 3), which made the moved entry unmovable afterwards. The
Jellyfin Move handler guarded this with a SongCount read before ReorderTrack,
but a concurrent removal between the two reopened it, and the native API
reorder endpoint passed client positions through unchecked.
Reorder now reads the last position in the same transaction, returns
ErrNotFound for a source outside the playlist and clamps the destination.
ReorderTrack uses an immediate transaction so that read and the updates are
atomic against other writers. The Jellyfin handler drops its pre-check and
maps ErrNotFound to Jellyfin's no-op 204; the native API now answers 404 for
an unknown track instead of corrupting the order.
120 lines
4.4 KiB
Go
120 lines
4.4 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/navidrome/navidrome/core/scrobbler"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/server/jellyfin/dto"
|
|
)
|
|
|
|
// playbackReport is the subset of Jellyfin's PlaybackStartInfo/PlaybackProgressInfo
|
|
// fields Navidrome needs to keep its playback/scrobbling state in sync.
|
|
type playbackReport struct {
|
|
ItemId string `json:"ItemId"`
|
|
PositionTicks int64 `json:"PositionTicks"`
|
|
IsPaused bool `json:"IsPaused"`
|
|
}
|
|
|
|
// decodeReport reads the playback report body. ItemId falls back to a query param (some clients send
|
|
// it there) and is decoded here since it flows straight into scrobbler lookups by media file id.
|
|
// These endpoints always answer 204 (see reportPlaybackStart), so a malformed id isn't a request
|
|
// failure; it just can't identify a track, same as an absent one.
|
|
func (api *Router) decodeReport(r *http.Request) playbackReport {
|
|
var body playbackReport
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
if body.ItemId == "" {
|
|
body.ItemId = r.URL.Query().Get("itemid")
|
|
}
|
|
if id, ok := dto.DecodeID(body.ItemId); ok {
|
|
body.ItemId = id
|
|
} else {
|
|
body.ItemId = ""
|
|
}
|
|
return body
|
|
}
|
|
|
|
// clientIdentity returns the scrobbler cache key/display name for the caller's
|
|
// player. Both are zero values if withPlayer could not resolve a player.
|
|
func clientIdentity(ctx context.Context) (id, name string) {
|
|
player, _ := request.PlayerFrom(ctx)
|
|
return player.ID, player.Client
|
|
}
|
|
|
|
// reportPlaybackStart handles POST /Sessions/Playing, sent once when a client starts an item.
|
|
//
|
|
// These Sessions endpoints report only the caller's own playback and never expose content, so unlike
|
|
// browse/stream they are intentionally not library-access-gated.
|
|
func (api *Router) reportPlaybackStart(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := api.decodeReport(r)
|
|
clientId, clientName := clientIdentity(ctx)
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: dto.MillisFromTicks(body.PositionTicks),
|
|
State: scrobbler.StatePlaying,
|
|
PlaybackRate: 1.0,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback start failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// reportPlaybackProgress handles POST /Sessions/Playing/Progress, sent periodically
|
|
// (and on pause/resume/seek) while a client keeps playing an item.
|
|
func (api *Router) reportPlaybackProgress(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := api.decodeReport(r)
|
|
state := scrobbler.StatePlaying
|
|
if body.IsPaused {
|
|
state = scrobbler.StatePaused
|
|
}
|
|
clientId, clientName := clientIdentity(ctx)
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: dto.MillisFromTicks(body.PositionTicks),
|
|
State: state,
|
|
PlaybackRate: 1.0,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback progress failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// reportPlaybackStopped handles POST /Sessions/Playing/Stopped, sent once when playback ends.
|
|
//
|
|
// Jellyfin clients (Finamp) send a Stopped report on *every* stop, even an immediate track switch,
|
|
// so the play threshold is applied server-side: ReportPlayback's StateStopped logic counts the play
|
|
// only past 50% (capped at 4 minutes). Force-submitting here would mark a one-second skip as played.
|
|
func (api *Router) reportPlaybackStopped(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := api.decodeReport(r)
|
|
clientId, clientName := clientIdentity(ctx)
|
|
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: dto.MillisFromTicks(body.PositionTicks),
|
|
State: scrobbler.StateStopped,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback stopped failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// acknowledge answers requests Navidrome keeps no state for: session capabilities (not tracked per
|
|
// session) and playback pings (transcodes live only as long as their stream request).
|
|
func (api *Router) acknowledge(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|