@@ -19,6 +19,7 @@ import (
1919 "time"
2020
2121 "github.com/ethanv2/podbit/data"
22+ ev "github.com/ethanv2/podbit/event"
2223
2324 "github.com/blang/mpv"
2425)
3435 // to idle mpv ready to receive instructions.
3536 PlayerArgs = []string {"--idle" , "--no-video" , "--input-ipc-server=" + PlayerRPC }
3637 // UpdateTime is the time between queue checks and supervision updates.
37- UpdateTime = 200 * time .Millisecond
38+ UpdateTime = 500 * time .Millisecond
3839)
3940
4041// Internal: Types of actions.
@@ -61,6 +62,10 @@ type WaitFunc func(u chan int)
6162type Player struct {
6263 proc * exec.Cmd
6364
65+ hndl ev.Handler
66+ event chan int
67+ dlchan chan struct {}
68+
6469 act chan int
6570 dat chan interface {}
6671
@@ -111,7 +116,7 @@ func endWait(u chan int) {
111116func downloadWait (u chan int ) {
112117 var id int
113118 for y := true ; y && DownloadAtHead (Plr .download ); y , id = data .Downloads .IsDownloading (Plr .download .Path ) {
114- time . Sleep ( UpdateTime )
119+ <- Plr . dlchan
115120 }
116121
117122 Plr .waiting = false
@@ -125,23 +130,19 @@ func downloadWait(u chan int) {
125130 head --
126131 }
127132
128- u <- 1
129- }
130-
131- func newWait (u chan int ) {
132- for head >= len (queue ) {
133- time .Sleep (UpdateTime )
134- }
135-
136- Plr .waiting = false
133+ Plr .hndl .Post (ev .PlayerChanged )
137134 u <- 1
138135}
139136
140137// NewPlayer constructs a new player. This does not yet
141138// launch any processes or play any media.
142- func NewPlayer () (p Player , err error ) {
139+ func NewPlayer (events * ev. Handler ) (p Player , err error ) {
143140 p .act = make (chan int )
144141 p .dat = make (chan interface {})
142+ p .dlchan = make (chan struct {}, 1 )
143+
144+ p .hndl = * events
145+ p .event = events .Register ()
145146
146147 return
147148}
@@ -377,10 +378,23 @@ func (p *Player) Wait() {
377378
378379 now , _ := p .ctrl .Filename ()
379380 for filename := now ; filename == now ; filename , _ = p .ctrl .Filename () {
381+ if ! p .isPaused () {
382+ p .hndl .Post (ev .PlayerChanged )
383+ }
380384 time .Sleep (UpdateTime )
381385 }
382386}
383387
388+ func (p * Player ) Event (e int ) {
389+ switch e {
390+ case ev .DownloadChanged :
391+ select {
392+ case p .dlchan <- struct {}{}:
393+ default :
394+ }
395+ }
396+ }
397+
384398// Mainloop - Main sound handling thread loop
385399//
386400// Loops infinitely waiting for sound events, managing the queue, mpv
@@ -434,7 +448,9 @@ func Mainloop() {
434448 select {
435449 case <- u :
436450 keepWaiting = false
437-
451+ Plr .hndl .Post (ev .PlayerChanged )
452+ case e := <- Plr .event :
453+ Plr .Event (e )
438454 case action := <- Plr .act :
439455 switch action {
440456 case actTerm :
@@ -472,5 +488,7 @@ func Mainloop() {
472488 }
473489 }
474490 }
491+
492+ Plr .hndl .Post (ev .PlayerChanged )
475493 }
476494}
0 commit comments