add support for OpenSubsonic playbackReport extension (#910)
* add support for OpenSubsonic playbackReport extension * better style
This commit is contained in:
@@ -302,6 +302,10 @@ type CanSavePlayQueue interface {
|
||||
GetPlayQueue() (*SavedPlayQueue, error)
|
||||
}
|
||||
|
||||
type CanReportPlayback interface {
|
||||
ReportPlayback(trackID string, positionMs int64, state string) error
|
||||
}
|
||||
|
||||
type LyricsProvider interface {
|
||||
GetLyrics(track *Track) (*Lyrics, error)
|
||||
}
|
||||
|
||||
@@ -37,6 +37,9 @@ type subsonicMediaProvider struct {
|
||||
|
||||
radiosCached []*mediaprovider.RadioStation
|
||||
radiosCachedAt int64 // unix
|
||||
|
||||
playbackReportOnce sync.Once
|
||||
playbackReportSupported bool
|
||||
}
|
||||
|
||||
func SubsonicMediaProvider(subsonicClient *subsonic.Client) mediaprovider.MediaProvider {
|
||||
@@ -459,6 +462,30 @@ func (s *subsonicMediaProvider) GetLyrics(track *mediaprovider.Track) (*mediapro
|
||||
// CanSavePlayQueue interface
|
||||
var _ mediaprovider.CanSavePlayQueue = (*subsonicMediaProvider)(nil)
|
||||
|
||||
// CanReportPlayback interface
|
||||
var _ mediaprovider.CanReportPlayback = (*subsonicMediaProvider)(nil)
|
||||
|
||||
func (s *subsonicMediaProvider) ReportPlayback(trackID string, positionMs int64, state string) error {
|
||||
s.playbackReportOnce.Do(func() {
|
||||
ext, err := s.client.GetOpenSubsonicExtensions()
|
||||
s.playbackReportSupported = err == nil && slices.ContainsFunc(ext,
|
||||
func(e *subsonic.OpenSubsonicExtension) bool {
|
||||
return e.Name == subsonic.PlaybackReport
|
||||
})
|
||||
})
|
||||
if !s.playbackReportSupported {
|
||||
return nil
|
||||
}
|
||||
ignoreScrobble := true
|
||||
return s.client.ReportPlayback(subsonic.ReportPlaybackParameters{
|
||||
MediaID: trackID,
|
||||
MediaType: subsonic.PlaybackMediaTypeSong,
|
||||
PositionMs: positionMs,
|
||||
State: subsonic.PlaybackState(state),
|
||||
IgnoreScrobble: &ignoreScrobble,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *subsonicMediaProvider) SavePlayQueue(trackIDs []string, currentTrackIdx int, timeSeconds int) error {
|
||||
if len(trackIDs) == 0 {
|
||||
return nil // don't save an empty queue
|
||||
|
||||
@@ -170,17 +170,24 @@ func (p *playbackEngine) registerPlayerCallbacks(pl player.BasePlayer) {
|
||||
pl.OnSeek(func() {
|
||||
p.handleTimePosUpdate(true)
|
||||
p.invokeNoArgCallbacks(p.onSeek)
|
||||
seekState := "playing"
|
||||
if p.PlaybackStatus().State == player.Paused {
|
||||
seekState = "paused"
|
||||
}
|
||||
p.reportPlayback(seekState)
|
||||
})
|
||||
pl.OnStopped(p.handleOnStopped)
|
||||
pl.OnPaused(func() {
|
||||
p.playTimeStopwatch.Stop()
|
||||
p.stopPollTimePos()
|
||||
p.invokeNoArgCallbacks(p.onPaused)
|
||||
p.reportPlayback("paused")
|
||||
})
|
||||
pl.OnPlaying(func() {
|
||||
p.playTimeStopwatch.Start()
|
||||
p.startPollTimePos()
|
||||
p.invokeNoArgCallbacks(p.onPlaying)
|
||||
p.reportPlayback("playing")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -926,6 +933,7 @@ func (p *playbackEngine) handleOnStopped() {
|
||||
p.handleTimePosUpdate(false)
|
||||
p.invokeOnSongChangeCallbacks()
|
||||
p.invokeNoArgCallbacks(p.onStopped)
|
||||
p.reportPlayback("stopped")
|
||||
p.alreadyScrobbled = false
|
||||
p.wasStopped = true
|
||||
p.nowPlayingIdx = -1
|
||||
@@ -1091,6 +1099,7 @@ func (p *playbackEngine) sendNowPlayingScrobble() {
|
||||
track.PlayCount += 1
|
||||
}
|
||||
go p.sm.Server.TrackBeganPlayback(track.ID)
|
||||
p.reportPlayback("starting")
|
||||
}
|
||||
|
||||
// creates a deep copy of the track info so that we can maintain our own state
|
||||
@@ -1113,6 +1122,22 @@ func (p *playbackEngine) invokeOnSongChangeCallbacks() {
|
||||
p.lastScrobbled = nil
|
||||
}
|
||||
|
||||
func (p *playbackEngine) reportPlayback(state string) {
|
||||
reporter, ok := p.sm.Server.(mediaprovider.CanReportPlayback)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if p.getPlayQueueLength() == 0 || p.nowPlayingIdx < 0 {
|
||||
return
|
||||
}
|
||||
np := p.NowPlaying()
|
||||
if np == nil || np.Metadata().Type != mediaprovider.MediaItemTypeTrack {
|
||||
return
|
||||
}
|
||||
posMs := int64(p.latestTrackPosition * 1000)
|
||||
go reporter.ReportPlayback(np.Metadata().ID, posMs, state)
|
||||
}
|
||||
|
||||
func (pm *playbackEngine) invokeNoArgCallbacks(cbs []func()) {
|
||||
if pm.callbacksDisabled {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user