2023-10-01 00:47:07 +04:00
|
|
|
package radio
|
|
|
|
|
|
|
|
import (
|
2023-10-01 06:44:36 +04:00
|
|
|
"dwelling-radio/pkg/watcher"
|
|
|
|
"log"
|
2023-10-01 00:47:07 +04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-10-01 06:44:36 +04:00
|
|
|
"syscall"
|
2023-10-01 00:47:07 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Playlist holds a list of paths to a song files.
|
|
|
|
type Playlist struct {
|
2023-10-04 18:37:13 +04:00
|
|
|
sync.Mutex
|
2023-10-01 00:47:07 +04:00
|
|
|
filePath string
|
|
|
|
playlist []string
|
|
|
|
cur int
|
|
|
|
repeat bool
|
2023-10-01 06:44:36 +04:00
|
|
|
watcher *watcher.InotifyWatcher
|
|
|
|
changed chan uint32
|
2023-10-01 00:47:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlaylist returns an instance of a Playlist struct with a loaded playlist.
|
|
|
|
// Returns an error if failed to load a playlist file.
|
|
|
|
func NewPlaylist(filePath string, repeat bool) (*Playlist, error) {
|
|
|
|
p := &Playlist{filePath: filePath, repeat: repeat}
|
2023-10-01 06:40:05 +04:00
|
|
|
return p, p.load()
|
2023-10-01 00:47:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next song to play. Returns an empty string if repeat is
|
|
|
|
// false and the end of a playlist was reached.
|
|
|
|
func (p *Playlist) Next() (song string) {
|
2023-10-04 18:37:13 +04:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
2023-10-01 00:47:07 +04:00
|
|
|
if p.cur == len(p.playlist) {
|
|
|
|
// If the end of a playlist was reached and repeat is set to true,
|
|
|
|
// then go back to the head of it, thus repeating it. Return an empty
|
|
|
|
// string otherwise.
|
|
|
|
if p.repeat {
|
|
|
|
p.cur = 0
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
song = p.playlist[p.cur]
|
|
|
|
p.cur++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-01 06:40:05 +04:00
|
|
|
// Load reads a playlist file.
|
|
|
|
func (p *Playlist) load() error {
|
2023-10-01 00:47:07 +04:00
|
|
|
data, err := os.ReadFile(p.filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-04 18:37:13 +04:00
|
|
|
p.Lock()
|
2023-10-01 00:47:07 +04:00
|
|
|
p.playlist = strings.Split(string(data), "\n")
|
|
|
|
p.cur = 0
|
2023-10-04 18:37:13 +04:00
|
|
|
p.Unlock()
|
2023-10-01 00:47:07 +04:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-01 06:40:05 +04:00
|
|
|
|
|
|
|
func (p *Playlist) Reload() error {
|
|
|
|
return p.load()
|
|
|
|
}
|
2023-10-01 06:44:36 +04:00
|
|
|
|
|
|
|
func (p *Playlist) Watch() (err error) {
|
|
|
|
p.watcher, err = watcher.NewInotifyWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.watcher.AddWatch(p.filePath, watcher.ModIgnMask)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.watcher.WatchForMask(p.changed, watcher.ModIgnMask)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
mask := <-p.changed
|
|
|
|
|
|
|
|
if mask&syscall.IN_MODIFY > 0 {
|
|
|
|
if err := p.load(); err != nil {
|
|
|
|
log.Fatalln("cannot reload a changed playlist:", err)
|
|
|
|
}
|
|
|
|
} else if mask&syscall.IN_IGNORED > 0 {
|
|
|
|
p.watcher.Close()
|
|
|
|
p.Watch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|