diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-06-01 14:45:14 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-06-01 14:45:14 +0200 |
| commit | 5d7a7c837732519c7013cccc7819d92f321a284c (patch) | |
| tree | a7409ce1ba6f84bbed6eb90afb7812160b27a5c4 | |
| parent | 434601366f09fb47346171c1dff05b4e9f1bd484 (diff) | |
| download | WRMS-5d7a7c837732519c7013cccc7819d92f321a284c.tar.gz WRMS-5d7a7c837732519c7013cccc7819d92f321a284c.zip | |
synchronize playback control
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | wrms.go | 59 |
2 files changed, 43 insertions, 17 deletions
@@ -24,6 +24,7 @@ - [ ] cancel old searches - [ ] make wrms thread safe + - [ ] synchronize Player.runMpv with the rest of Wrms - [X] tag events with ids - [X] paralyze search - [X] report results incrementally @@ -292,6 +292,11 @@ func (wrms *Wrms) DeleteSong(songUri string) { } func (wrms *Wrms) Next() { + wrms.rwlock.Lock() + wrms._next() +} + +func (wrms *Wrms) _next() { llog.DDebug("Next Song") next := wrms.queue.PopSong() @@ -313,35 +318,55 @@ func (wrms *Wrms) Next() { } } + var cmd string if wrms.Playing { - wrms.Broadcast(newEvent("play", []Song{*next})) wrms.Player.Play(next) + cmd = "play" } else if wrms.Player.mpv != nil { - wrms.Broadcast(newEvent("next", []Song{*next})) wrms.Player.terminateMpv() + cmd = "next" } + + wrms.rwlock.Unlock() + + wrms.Broadcast(newEvent(cmd, []Song{*next})) } func (wrms *Wrms) PlayPause() { - wrms.Playing = !wrms.Playing + wrms.rwlock.Lock() + // Toggle the playback state + defer func() { wrms.Playing = !wrms.Playing }() + // Wrms is playing -> pause the player if wrms.Playing { - currentSong := wrms.CurrentSong.Load() - if currentSong == nil { - llog.Info("No song currently playing play the next") - wrms.Next() - } else { - wrms.Broadcast(newEvent("play", []Song{*currentSong})) - if wrms.Player.mpv != nil { - wrms.Player.Continue() - } else { - wrms.Player.Play(currentSong) - } - } - } else { - wrms.Broadcast(newNotification("pause")) wrms.Player.Pause() + wrms.rwlock.Unlock() + wrms.Broadcast(newNotification("pause")) + return + } + + // Wrms is not playing + currentSong := wrms.CurrentSong.Load() + + // Wrms has no current song -> try to play the next song + if currentSong == nil { + llog.Info("No song currently playing play the next") + // _next() releases the rwlock + wrms._next() + return } + + // Wrms has an active mpv process -> continue playing + if wrms.Player.mpv != nil { + wrms.Player.Continue() + // Wrms has a current song but no mpv process playing it -> start a new one + } else { + wrms.Player.Play(currentSong) + } + + wrms.rwlock.Unlock() + + wrms.Broadcast(newEvent("play", []Song{*currentSong})) } func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string) { |
