Skip to content

Commit

Permalink
Merge pull request #15 from jmillerv/9-stop-signal
Browse files Browse the repository at this point in the history
9-stop-signal: implement stop signal
  • Loading branch information
jmillerv authored Nov 6, 2022
2 parents 74926a9 + 6280ee8 commit a6058d4
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ A web radio station can be supplied via URL in the config
- [ ] Scheduled Programming
- [x] Local Folder support
- [ ] Remote file support
- [ ] Stop signal
- [x] Stop signal
- [ ] WAV Support
- [ ] [Funkwhale API integration](https://docs.funkwhale.audio/api.html)
-
### Extras
- [ ] UI for scheduler & running the daemon
- [ ] Unit Tests
Expand Down
4 changes: 3 additions & 1 deletion content/announcement.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package content

import log "github.com/sirupsen/logrus"

// Not yet implemented

type Announcement struct {
Expand All @@ -18,5 +20,5 @@ func (a *Announcement) Play() {
}

func (a *Announcement) Stop() {
panic("implement me")
log.Infof("Stopping stream from %v ", a.Path)
}
2 changes: 1 addition & 1 deletion content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func (l *LocalFile) Play() {
}

func (l *LocalFile) Stop() {
panic("implement me")
log.Infof("Stopping stream from %v ", l.Path)
}
2 changes: 1 addition & 1 deletion content/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (f *Folder) Play() {
}

func (f *Folder) Stop() {
panic("implement me")
log.Infof("Stopping stream from %v ", f.Path)
}

func (f *Folder) getLocalFile(file fs.FileInfo) *LocalFile {
Expand Down
21 changes: 21 additions & 0 deletions content/funkwhale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package content

// FunkWhale is for interacting with the FunkWhale API
// https://docs.funkwhale.audio/api.html
type FunkWhale struct {
}

func (f FunkWhale) Get() {
//TODO implement me
panic("implement me")
}

func (f FunkWhale) Play() {
//TODO implement me
panic("implement me")
}

func (f FunkWhale) Stop() {
//TODO implement me
panic("implement me")
}
2 changes: 2 additions & 0 deletions content/media.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package content

// content type should be able to be set from the configuration

const (
podcastContent MediaType = "podcast"
announcementContent MediaType = "announcement"
Expand Down
4 changes: 3 additions & 1 deletion content/podcast.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package content

import log "github.com/sirupsen/logrus"

type Podcast struct {
Name string
URL string
Expand All @@ -16,5 +18,5 @@ func (p *Podcast) Play() {
}

func (p *Podcast) Stop() {
panic("implement me")
log.Infof("Stopping stream from %v ", p.Path)
}
3 changes: 3 additions & 0 deletions content/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (p *Program) mediaFactory() Media {
radio.URL = p.Source
log.Debugf("returning WebRadio: %v", formatter.StructToString(radio))
return radio
case *FunkWhale:
funkwhale := m.(*FunkWhale)
return funkwhale
}
return nil
}
55 changes: 52 additions & 3 deletions content/schedule.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package content

import (
"fmt"
"github.com/jmillerv/go-utilities/formatter"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
)

Expand Down Expand Up @@ -51,10 +55,19 @@ type Scheduler struct {

func (s *Scheduler) Run() error {
log.Info("Starting Daemon")

log.Infof("Press ESC to quit")
// set up the loop to continuously check for key entries
now := time.Now()
ts := getTimeSlot(&now)
// if randomized mode do x

// setup signal listeners
sigchnl := make(chan os.Signal, 1)
signal.Notify(sigchnl)
exitchnl := make(chan int)

// check content from scheduler and run through it
for _, p := range s.Content.Programs {
log.Debugf("program %v", formatter.StructToIndentedString(p))
// Check Timeslots
Expand All @@ -63,16 +76,23 @@ func (s *Scheduler) Run() error {
content := p.GetMedia()
log.Debugf("media struct: %v", content)
content.Get()
// TODO go routine to check for interrupts
go func() {
for {
stop := <-sigchnl
s.Stop(stop, content)
}
}()
content.Play() // play will block until done
}
}
// if radio station start 30 minute counter.
// smartly allocate programs to timeslots based on length if known
// if time between TimeSlotMap
// if time between TimeSlotMap do x
// play program from that slot.
// wait for program to finish
// get next
exitcode := <-exitchnl
os.Exit(exitcode)
return nil
}

Expand All @@ -83,18 +103,47 @@ func (s *Scheduler) Shuffle() error {
func(i, j int) {
s.Content.Programs[i], s.Content.Programs[j] = s.Content.Programs[j], s.Content.Programs[i]
})

// setup signal listeners
sigchnl := make(chan os.Signal, 1)
signal.Notify(sigchnl)
exitchnl := make(chan int)

for _, p := range s.Content.Programs {
log.Debugf("program %v", formatter.StructToIndentedString(p))
log.Infof("getting media type: %v", p.Type)
content := p.GetMedia()
log.Debugf("media struct: %v", content)
content.Get()
// TODO go routine to check for interrupts
go func() {
for {
stop := <-sigchnl
s.Stop(stop, content)
}
}()
content.Play() // play will block until done
}

exitcode := <-exitchnl
os.Exit(exitcode)
return nil
}

func (s *Scheduler) Stop(signal os.Signal, media Media) {
if signal == syscall.SIGTERM {
log.Info("Got kill signal. ")
media.Stop()
log.Info("Program will terminate now.")
os.Exit(0)
} else if signal == syscall.SIGINT {
media.Stop()
log.Info("Got CTRL+C signal")
media.Stop()
fmt.Println("Closing.")
os.Exit(0)
}
}

func getTimeSlot(t *time.Time) Timeslot {
// if t between certain times return Timeslot
return All
Expand Down
29 changes: 23 additions & 6 deletions content/web_radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const player = "mpv"
type WebRadio struct {
Name string
URL string
Player wrCommand
Player webRadioCommand
}

type wrCommand struct {
type webRadioCommand struct {
playerName string
url string
isPlaying bool
Expand All @@ -28,7 +28,7 @@ type wrCommand struct {
pipeChan chan io.ReadCloser
}

var wrc wrCommand
var wrc webRadioCommand

func (w *WebRadio) Get() {
var err error
Expand All @@ -48,7 +48,7 @@ func (w *WebRadio) Get() {
}

func (w *WebRadio) Play() {
log.Infof("streaming from URL %v ", w.URL)
log.Infof("streaming from %v ", w.URL)
if !w.Player.isPlaying {
err := w.Player.command.Start()
if err != nil {
Expand All @@ -62,9 +62,26 @@ func (w *WebRadio) Play() {
}()
<-done
}

}

func (w *WebRadio) Stop() {
panic("implement me")
log.Infof("Stopping stream from %v ", w.URL)
if w.Player.isPlaying {
w.Player.isPlaying = false
_, err := w.Player.in.Write([]byte("q"))
if err != nil {
log.WithError(err).Error("error stopping web radio player: w.Player.in.Write()")
}
err = w.Player.in.Close()
if err != nil {
log.WithError(err).Error("error stopping web radio player: w.Player.in.Close()")
}
err = w.Player.out.Close()
if err != nil {
log.WithError(err).Error("error stopping web radio player: w.Player.out.Close()")
}
w.Player.command = nil

w.Player.url = ""
}
}
28 changes: 27 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jmillerv/go-dj

go 1.16
go 1.19

require (
github.com/faiface/beep v1.1.0
Expand All @@ -9,3 +9,29 @@ require (
github.com/spf13/viper v1.8.1
github.com/urfave/cli v1.22.3
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hajimehoshi/go-mp3 v0.3.0 // indirect
github.com/hajimehoshi/oto v0.7.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.3.5 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,9 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit a6058d4

Please sign in to comment.