diff options
Diffstat (limited to 'player')
-rw-r--r-- | player/player.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/player/player.go b/player/player.go index 90c607c..5ab8f72 100644 --- a/player/player.go +++ b/player/player.go @@ -71,12 +71,21 @@ type stopRequest struct { response chan<- stopResult } +type State int + +const ( + IDLE State = 1 + iota + PAUSED + PLAYING +) + type Player struct { pipe *gst.Element bus *gst.Bus basepath string stdlog *log.Logger dbglog *log.Logger + state State loadChan chan loadRequest playChan chan playRequest pauseChan chan pauseRequest @@ -86,13 +95,15 @@ type Player struct { func (p *Player) onMessage(msg *gst.Message) { switch msg.GetType() { case gst.MESSAGE_EOS: - p.stdlog.Printf("GStreamer Pipeline: EOS reached!\n") p.pipe.SetState(gst.STATE_NULL) + p.state = IDLE + p.stdlog.Printf("GStreamer Pipeline: EOS reached!\n") case gst.MESSAGE_WARNING: warn, _ := msg.ParseWarning() p.stdlog.Printf("GStreamer Pipeline Warning: %s\n", warn) case gst.MESSAGE_ERROR: p.pipe.SetState(gst.STATE_NULL) + p.state = IDLE err, _ := msg.ParseError() p.stdlog.Printf("GStreamer Pipeline Error: %s\n", err) case gst.MESSAGE_ASYNC_DONE: @@ -127,23 +138,48 @@ func (p *Player) load(cart, cut uint) (resp loadResult) { } file.Close() + if p.state != IDLE { + p.pipe.SetState(gst.STATE_NULL) + p.state = IDLE + } p.pipe.SetProperty("uri", "file://"+filename) p.pipe.SetState(gst.STATE_PAUSED) + p.state = PAUSED return } func (p *Player) play() (resp playResult) { + if p.state != PAUSED { + resp.err = fmt.Errorf("player: no file loaded") + return + } p.pipe.SetState(gst.STATE_PLAYING) + p.state = PLAYING return } func (p *Player) pause() (resp pauseResult) { + if p.state != PLAYING { + resp.err = fmt.Errorf("player: not playing") + return + } p.pipe.SetState(gst.STATE_PAUSED) + p.state = PAUSED return } func (p *Player) stop() (resp stopResult) { - p.pipe.SetState(gst.STATE_NULL) + switch p.state { + case IDLE: + p.pipe.SetState(gst.STATE_NULL) + p.state = IDLE + case PLAYING: + fallthrough + case PAUSED: + p.pipe.SetState(gst.STATE_NULL) + p.pipe.SetState(gst.STATE_PAUSED) + p.state = PAUSED + } return } |