1
0

Fixed last songs displaying.

This commit is contained in:
Alexander Andreev 2023-03-12 21:31:18 +04:00
parent 1ebecc91d9
commit 517dd0e534
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -24,7 +24,6 @@ const (
var ( var (
lastPlayedCache []Song lastPlayedCache []Song
lastPlayedCacheMutex sync.Mutex lastPlayedCacheMutex sync.Mutex
isJustStarted bool = true
) )
type IcecastStatusDTO struct { type IcecastStatusDTO struct {
@ -79,15 +78,12 @@ func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) {
}, nil }, nil
} }
func IcecastLastPlayedSongs(lastNSongs int, playlistPath string) ([]Song, error) { func IcecastLastSongs(playlistPath string) ([]Song, error) {
lastPlayedCacheMutex.Lock() lastPlayedCacheMutex.Lock()
defer lastPlayedCacheMutex.Unlock() defer lastPlayedCacheMutex.Unlock()
if lpcLen := len(lastPlayedCache); lpcLen > 0 { if lpcLen := len(lastPlayedCache); lpcLen > 0 {
if lastNSongs > lpcLen { ret := make([]Song, 0, lpcLen)
lastNSongs = lpcLen ret = append(ret, lastPlayedCache...)
}
var ret []Song = make([]Song, lastNSongs)
copy(ret[:], lastPlayedCache[lpcLen-lastNSongs:])
return ret, nil return ret, nil
} }
@ -104,7 +100,6 @@ func IcecastLastSong(playlistPath string) (*Song, error) {
} }
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) { func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
songs := make([]Song, n)
var buf []byte var buf []byte
var offset int64 = 0 var offset int64 = 0
@ -134,16 +129,18 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
lines := bytes.Split(buf, []byte("\n")) lines := bytes.Split(buf, []byte("\n"))
if len(lines) < 3 { if len(lines) < 2 {
return nil, nil return nil, nil
} }
lines = lines[:len(lines)-2] lines = lines[:len(lines)-1]
if len(lines) > n { if len(lines) > n {
lines = lines[len(lines)-n:] lines = lines[len(lines)-n:]
} }
songs := make([]Song, 0, len(lines))
for _, line := range lines { for _, line := range lines {
fields := bytes.Split(line, []byte("|")) fields := bytes.Split(line, []byte("|"))
@ -184,27 +181,21 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask) pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
lastPlayedCache, err = icecastLastPlayedSongs(playlistPath, n)
if err != nil {
return errors.Wrap(err, "cannot instantiate last played cache")
}
go func() { go func() {
for { for {
select { select {
case mask := <-pw.changed: case mask := <-pw.changed:
if mask&syscall.IN_MODIFY > 0 { if mask&syscall.IN_MODIFY > 0 {
if isJustStarted {
isJustStarted = false
} else {
lastPlayedCacheMutex.Lock() lastPlayedCacheMutex.Lock()
if lastPlayedCache == nil { if songs, err := icecastLastPlayedSongs(playlistPath, n); err == nil && len(songs) > 0 {
lastPlayedCache = make([]Song, n) lastPlayedCache = songs
}
if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 {
if len(lastPlayedCache) == n {
lastPlayedCache = append(lastPlayedCache[1:], songs...)
} else {
lastPlayedCache = append(lastPlayedCache, songs...)
}
} }
lastPlayedCacheMutex.Unlock() lastPlayedCacheMutex.Unlock()
}
} else if mask&syscall.IN_IGNORED > 0 { } else if mask&syscall.IN_IGNORED > 0 {
pw.Close() pw.Close()
pw.Watch(playlistPath, n) pw.Watch(playlistPath, n)