diff --git a/internal/radio/icecast.go b/internal/radio/icecast.go index 967c481..fc57a46 100644 --- a/internal/radio/icecast.go +++ b/internal/radio/icecast.go @@ -22,6 +22,7 @@ const ( ) var ( + currentlyPlaying Song lastPlayedCache []Song lastPlayedCacheMutex sync.Mutex ) @@ -129,12 +130,10 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) { lines := bytes.Split(buf, []byte("\n")) - if len(lines) < 2 { + if len(lines) < 1 { return nil, nil } - lines = lines[:len(lines)] - if len(lines) > n { lines = lines[len(lines)-n:] } @@ -190,10 +189,15 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) { if mask&syscall.IN_MODIFY > 0 { lastPlayedCacheMutex.Lock() if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 { - if len(lastPlayedCache) == n { - lastPlayedCache = append(lastPlayedCache[1:], songs...) + if currentlyPlaying.Time == "" { + currentlyPlaying = songs[0] } else { - lastPlayedCache = append(lastPlayedCache, songs...) + if len(lastPlayedCache) == n { + lastPlayedCache = append(lastPlayedCache[1:], currentlyPlaying) + } else { + lastPlayedCache = append(lastPlayedCache, currentlyPlaying) + } + currentlyPlaying = songs[0] } } lastPlayedCacheMutex.Unlock()