Skip to content

Commit b41eb72

Browse files
author
Datanoise
committed
fix: resolve empty playlist adding issues and persist last loaded playlist
1 parent b44b003 commit b41eb72

6 files changed

Lines changed: 65 additions & 43 deletions

File tree

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type AutoDJConfig struct {
5656
Loop bool `json:"loop"`
5757
InjectMetadata bool `json:"inject_metadata"`
5858
Playlist []string `json:"playlist"`
59+
LastPlaylist string `json:"last_playlist"`
5960
MPDEnabled bool `json:"mpd_enabled"`
6061
MPDPort string `json:"mpd_port"`
6162
MPDPassword string `json:"mpd_password"`

playlists/abc.pls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[playlist]
2+
NumberOfEntries=0
3+
Version=2

playlists/neuer.pls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[playlist]
2+
NumberOfEntries=0
3+
Version=2

relay/streamer.go

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Streamer struct {
4040
InjectMetadata bool
4141
Visible bool
4242
MPDPassword string
43+
LastPlaylist string
4344

4445
relay *Relay
4546
cancel context.CancelFunc
@@ -269,19 +270,25 @@ func (s *Streamer) LoadPlaylist(filename string) error {
269270
path := filepath.Join("playlists", filename)
270271
f, err := os.Open(path)
271272
if err != nil {
272-
if os.IsNotExist(err) && filename == s.Name+".pls" {
273-
return s.SavePlaylist() // Create default empty
273+
if os.IsNotExist(err) {
274+
if filename == s.Name+".pls" {
275+
return s.SavePlaylist() // Create default empty
276+
}
277+
return nil // Just ignore if custom playlist doesn't exist yet
274278
}
275279
return err
276280
}
277281
defer f.Close()
278282

279283
s.mu.Lock()
280-
defer s.mu.Unlock()
281284
s.Playlist = []string{}
285+
s.LastPlaylist = filename
286+
s.mu.Unlock()
282287

283288
// Simple PLS parser
284289
scanner := bufio.NewScanner(f)
290+
s.mu.Lock()
291+
defer s.mu.Unlock()
285292
for scanner.Scan() {
286293
line := scanner.Text()
287294
if strings.HasPrefix(line, "File") {
@@ -348,42 +355,44 @@ type StreamerStats struct {
348355
MPDPort string
349356
MPDPassword string
350357
MusicDir string
351-
Loop bool
352-
InjectMetadata bool
353-
Visible bool
354-
}
355-
356-
func (s *Streamer) GetStats() StreamerStats {
357-
s.mu.RLock()
358-
defer s.mu.RUnlock()
359-
360-
mpdPort := ""
361-
mpdPassword := ""
362-
if s.MPDServer != nil {
363-
mpdPort = s.MPDServer.Port
364-
mpdPassword = s.MPDPassword
365-
}
366-
367-
return StreamerStats{
368-
Name: s.Name,
369-
Mount: s.OutputMount,
370-
State: s.State,
371-
CurrentSong: s.CurrentFile,
372-
StartTime: s.CurrentFileTime,
373-
Duration: s.CurrentFileDuration,
374-
PlaylistPos: s.CurrentPos,
375-
PlaylistLen: len(s.Playlist),
376-
Shuffle: s.Shuffle,
377-
MPDPort: mpdPort,
378-
MPDPassword: mpdPassword,
379-
MusicDir: s.MusicDir,
380-
Loop: s.Loop,
381-
InjectMetadata: s.InjectMetadata,
382-
Visible: s.Visible,
358+
Loop bool
359+
InjectMetadata bool
360+
Visible bool
361+
LastPlaylist string
362+
}
363+
364+
func (s *Streamer) GetStats() StreamerStats {
365+
s.mu.RLock()
366+
defer s.mu.RUnlock()
367+
368+
mpdPort := ""
369+
mpdPassword := ""
370+
if s.MPDServer != nil {
371+
mpdPort = s.MPDServer.Port
372+
mpdPassword = s.MPDPassword
373+
}
374+
375+
return StreamerStats{
376+
Name: s.Name,
377+
Mount: s.OutputMount,
378+
State: s.State,
379+
CurrentSong: s.CurrentFile,
380+
StartTime: s.CurrentFileTime,
381+
Duration: s.CurrentFileDuration,
382+
PlaylistPos: s.CurrentPos,
383+
PlaylistLen: len(s.Playlist),
384+
Shuffle: s.Shuffle,
385+
MPDPort: mpdPort,
386+
MPDPassword: mpdPassword,
387+
MusicDir: s.MusicDir,
388+
Loop: s.Loop,
389+
InjectMetadata: s.InjectMetadata,
390+
Visible: s.Visible,
391+
LastPlaylist: s.LastPlaylist,
392+
}
383393
}
384-
}
385394

386-
func (sm *StreamerManager) StartStreamer(name, mount, musicDir string, loop bool, format string, bitrate int, injectMetadata bool, initialPlaylist []string, mpdEnabled bool, mpdPort, mpdPassword string, visible bool) (*Streamer, error) {
395+
func (sm *StreamerManager) StartStreamer(name, mount, musicDir string, loop bool, format string, bitrate int, injectMetadata bool, initialPlaylist []string, mpdEnabled bool, mpdPort, mpdPassword string, visible bool, lastPlaylist string) (*Streamer, error) {
387396
sm.mu.Lock()
388397
defer sm.mu.Unlock()
389398

@@ -405,6 +414,7 @@ func (sm *StreamerManager) StartStreamer(name, mount, musicDir string, loop bool
405414
InjectMetadata: injectMetadata,
406415
Visible: visible,
407416
MPDPassword: mpdPassword,
417+
LastPlaylist: lastPlaylist,
408418
relay: sm.relay,
409419
cancel: cancel,
410420
titleCache: make(map[string]string),

server/server.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,14 @@ func (s *Server) Start() error {
438438
for _, adj := range s.Config.AutoDJs {
439439
if adj.Enabled {
440440
absMusicDir, _ := filepath.Abs(adj.MusicDir)
441-
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, absMusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible)
441+
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, absMusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible, adj.LastPlaylist)
442442
if err != nil {
443443
logrus.WithError(err).Errorf("Failed to start AutoDJ %s", adj.Name)
444444
} else {
445445
logrus.Infof("AutoDJ %s started on %s", adj.Name, adj.Mount)
446+
if adj.LastPlaylist != "" {
447+
streamer.LoadPlaylist(adj.LastPlaylist)
448+
}
446449
if adj.InjectMetadata {
447450
if st, ok := s.Relay.GetStream(adj.Mount); ok {
448451
st.SetVisible(adj.Visible)
@@ -2415,7 +2418,7 @@ func (s *Server) handleAddAutoDJ(w http.ResponseWriter, r *http.Request) {
24152418
s.Config.SaveConfig()
24162419

24172420
// Start it immediately
2418-
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, adj.MusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, nil, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible)
2421+
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, adj.MusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, nil, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible, "")
24192422
if err == nil {
24202423
if adj.InjectMetadata {
24212424
if st, ok := s.Relay.GetStream(adj.Mount); ok {
@@ -2479,7 +2482,7 @@ func (s *Server) handleToggleAutoDJ(w http.ResponseWriter, r *http.Request) {
24792482
if adj.Enabled {
24802483
if existing == nil {
24812484
absMusicDir, _ := filepath.Abs(adj.MusicDir)
2482-
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, absMusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible)
2485+
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, absMusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible, adj.LastPlaylist)
24832486
if err == nil {
24842487
if adj.InjectMetadata {
24852488
if st, ok := s.Relay.GetStream(adj.Mount); ok {
@@ -2539,6 +2542,7 @@ func (s *Server) handlePlayerLoadPlaylist(w http.ResponseWriter, r *http.Request
25392542
for _, adj := range s.Config.AutoDJs {
25402543
if adj.Mount == mount {
25412544
adj.Playlist = playlistCopy
2545+
adj.LastPlaylist = filename
25422546
s.Config.SaveConfig()
25432547
break
25442548
}
@@ -2821,6 +2825,7 @@ func (s *Server) handlePlayerPlaylistAction(w http.ResponseWriter, r *http.Reque
28212825
streamer.RemoveFromPlaylist(idx)
28222826
}
28232827
playlistCopy := streamer.GetPlaylist()
2828+
streamer.SavePlaylist()
28242829

28252830
// Persist
28262831
for _, adj := range s.Config.AutoDJs {
@@ -2831,7 +2836,7 @@ func (s *Server) handlePlayerPlaylistAction(w http.ResponseWriter, r *http.Reque
28312836
}
28322837
}
28332838

2834-
http.Redirect(w, r, "/admin#tab-streamer", http.StatusSeeOther)
2839+
w.WriteHeader(http.StatusOK)
28352840
}
28362841

28372842
func (s *Server) handleUpdateAutoDJ(w http.ResponseWriter, r *http.Request) {
@@ -2887,7 +2892,7 @@ func (s *Server) handleUpdateAutoDJ(w http.ResponseWriter, r *http.Request) {
28872892
// Restart streamer
28882893
s.StreamerM.StopStreamer(oldMount)
28892894
if adj.Enabled {
2890-
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, adj.MusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible)
2895+
streamer, err := s.StreamerM.StartStreamer(adj.Name, adj.Mount, adj.MusicDir, adj.Loop, adj.Format, adj.Bitrate, adj.InjectMetadata, adj.Playlist, adj.MPDEnabled, adj.MPDPort, adj.MPDPassword, adj.Visible, adj.LastPlaylist)
28912896
if err == nil {
28922897
if adj.InjectMetadata {
28932898
if st, ok := s.Relay.GetStream(adj.Mount); ok {

tinyice.pid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
36738
1+
36854

0 commit comments

Comments
 (0)