mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 02:17:25 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
127 lines
4.1 KiB
Go
127 lines
4.1 KiB
Go
// Package playback implements audio playback using PlaybackDevices. It is used to implement the Jukebox mode in turn.
|
|
// It makes use of the MPV library to do the playback. Major parts are:
|
|
// - decoder which includes decoding and transcoding of various audio file formats
|
|
// - device implementing the basic functions to work with audio devices like set, play, stop, skip, ...
|
|
// - queue a simple playlist
|
|
package playback
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/singleton"
|
|
)
|
|
|
|
type PlaybackServer interface {
|
|
Run(ctx context.Context) error
|
|
GetDeviceForUser(user string) (*playbackDevice, error)
|
|
GetMediaFile(id string) (*model.MediaFile, error)
|
|
}
|
|
|
|
type playbackServer struct {
|
|
ctx *context.Context
|
|
datastore model.DataStore
|
|
playbackDevices []playbackDevice
|
|
}
|
|
|
|
// GetInstance returns the playback-server singleton
|
|
func GetInstance(ds model.DataStore) PlaybackServer {
|
|
return singleton.GetInstance(func() *playbackServer {
|
|
return &playbackServer{datastore: ds}
|
|
})
|
|
}
|
|
|
|
// Run starts the playback server which serves request until canceled using the given context
|
|
func (ps *playbackServer) Run(ctx context.Context) error {
|
|
ps.ctx = &ctx
|
|
|
|
devices, err := ps.initDeviceStatus(ctx, conf.Server.Jukebox.Devices, conf.Server.Jukebox.Default)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ps.playbackDevices = devices
|
|
log.Info(ctx, fmt.Sprintf("%d audio devices found", len(devices)))
|
|
|
|
defaultDevice, _ := ps.getDefaultDevice()
|
|
|
|
log.Info(ctx, "Using audio device: "+defaultDevice.DeviceName)
|
|
|
|
<-ctx.Done()
|
|
|
|
// Should confirm all subprocess are terminated before returning
|
|
return nil
|
|
}
|
|
|
|
func (ps *playbackServer) initDeviceStatus(ctx context.Context, devices []conf.AudioDeviceDefinition, defaultDevice string) ([]playbackDevice, error) {
|
|
pbDevices := make([]playbackDevice, max(1, len(devices)))
|
|
defaultDeviceFound := false
|
|
|
|
if defaultDevice == "" {
|
|
// if there are no devices given and no default device, we create a synthetic device named "auto"
|
|
if len(devices) == 0 {
|
|
pbDevices[0] = *NewPlaybackDevice(ctx, ps, "auto", "auto")
|
|
}
|
|
|
|
// if there is but only one entry and no default given, just use that.
|
|
if len(devices) == 1 {
|
|
if len(devices[0]) != 2 {
|
|
return []playbackDevice{}, fmt.Errorf("audio device definition ought to contain 2 fields, found: %d ", len(devices[0]))
|
|
}
|
|
pbDevices[0] = *NewPlaybackDevice(ctx, ps, devices[0][0], devices[0][1])
|
|
}
|
|
|
|
if len(devices) > 1 {
|
|
return []playbackDevice{}, fmt.Errorf("number of audio device found is %d, but no default device defined. Set Jukebox.Default", len(devices))
|
|
}
|
|
|
|
pbDevices[0].Default = true
|
|
return pbDevices, nil
|
|
}
|
|
|
|
for idx, audioDevice := range devices {
|
|
if len(audioDevice) != 2 {
|
|
return []playbackDevice{}, fmt.Errorf("audio device definition ought to contain 2 fields, found: %d ", len(audioDevice))
|
|
}
|
|
|
|
pbDevices[idx] = *NewPlaybackDevice(ctx, ps, audioDevice[0], audioDevice[1])
|
|
|
|
if audioDevice[0] == defaultDevice {
|
|
pbDevices[idx].Default = true
|
|
defaultDeviceFound = true
|
|
}
|
|
}
|
|
|
|
if !defaultDeviceFound {
|
|
return []playbackDevice{}, fmt.Errorf("default device name not found: %s ", defaultDevice)
|
|
}
|
|
return pbDevices, nil
|
|
}
|
|
|
|
func (ps *playbackServer) getDefaultDevice() (*playbackDevice, error) {
|
|
for idx := range ps.playbackDevices {
|
|
if ps.playbackDevices[idx].Default {
|
|
return &ps.playbackDevices[idx], nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no default device found")
|
|
}
|
|
|
|
// GetMediaFile retrieves the MediaFile given by the id parameter
|
|
func (ps *playbackServer) GetMediaFile(id string) (*model.MediaFile, error) {
|
|
return ps.datastore.MediaFile().Get(*ps.ctx, id)
|
|
}
|
|
|
|
// GetDeviceForUser returns the audio playback device for the given user. As of now this is but only the default device.
|
|
func (ps *playbackServer) GetDeviceForUser(user string) (*playbackDevice, error) {
|
|
log.Debug("Processing GetDevice", "user", user)
|
|
// README: here we might plug-in the user-device mapping one fine day
|
|
device, err := ps.getDefaultDevice()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
device.User = user
|
|
return device, nil
|
|
}
|