From 50490b7942c385fa40dd5dcdc7d4d65cd1a0614d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 13 Jan 2016 21:01:59 +0100 Subject: player no can be used with external logger diff --git a/player/player.go b/player/player.go index f9b5335..97d599a 100644 --- a/player/player.go +++ b/player/player.go @@ -27,6 +27,8 @@ package player import ( "fmt" "github.com/spreadspace/go-gstreamer" + "io/ioutil" + "log" "path" ) @@ -72,34 +74,35 @@ type Player struct { pipe *gst.Element bus *gst.Bus basepath string + log *log.Logger loadChan chan loadRequest playChan chan playRequest pauseChan chan pauseRequest stopChan chan stopRequest } -func (p *Player) onMessage(bus *gst.Bus, msg *gst.Message) { +func (p *Player) onMessage(msg *gst.Message) { switch msg.GetType() { case gst.MESSAGE_EOS: - fmt.Printf("GStreamer Pipeline: EOS reached!\n") + p.log.Printf("GStreamer Pipeline: EOS reached!\n") p.pipe.SetState(gst.STATE_NULL) case gst.MESSAGE_WARNING: warn, _ := msg.ParseWarning() - fmt.Printf("GStreamer Pipeline Warning: %s\n", warn) + p.log.Printf("GStreamer Pipeline Warning: %s\n", warn) case gst.MESSAGE_ERROR: p.pipe.SetState(gst.STATE_NULL) err, _ := msg.ParseError() - fmt.Printf("GStreamer Pipeline Error: %s\n", err) + p.log.Printf("GStreamer Pipeline Error: %s\n", err) case gst.MESSAGE_ASYNC_DONE: len, ok := p.pipe.QueryDuration(gst.FORMAT_TIME) if ok { - fmt.Printf("GStreamer Pipeline: loaded file has length: %d.%d s\n", len/1000000000, len%1000000000) + p.log.Printf("GStreamer Pipeline: loaded file has length: %d.%d s\n", len/1000000000, len%1000000000) } else { - fmt.Printf("GStreamer Pipeline Error: unable to query duration of file\n") + p.log.Printf("GStreamer Pipeline Error: unable to query duration of file\n") } case gst.MESSAGE_STATE_CHANGED: default: - fmt.Printf("GStreamer Message: unknown type '%s'\n", msg.GetTypeName()) + p.log.Printf("GStreamer Message: unknown type '%s'\n", msg.GetTypeName()) } } @@ -126,6 +129,10 @@ func (p *Player) stop() (resp stopResult) { } func (p *Player) dispatchRequests() { + msgChan := make(chan gst.Message) + p.bus.AddSignalWatch() + p.bus.Connect("message", func(bus *gst.Bus, msg *gst.Message) { msgChan <- *msg }) + for { select { case req := <-p.loadChan: @@ -136,6 +143,8 @@ func (p *Player) dispatchRequests() { req.response <- p.pause() case req := <-p.stopChan: req.response <- p.stop() + case msg := <-msgChan: + p.onMessage(&msg) } } } @@ -213,9 +222,14 @@ func (p *Player) GetInterface() *PlayerChan { return ch } -func NewPlayer(basepath string) (p *Player, err error) { +func NewPlayer(basepath string, logger *log.Logger) (p *Player, err error) { p = &Player{} p.basepath = path.Clean(basepath) + if logger != nil { + p.log = logger + } else { + p.log = log.New(ioutil.Discard, "rhrd-go.player", log.LstdFlags) + } p.loadChan = make(chan loadRequest) p.playChan = make(chan playRequest) p.pauseChan = make(chan pauseRequest) @@ -228,9 +242,6 @@ func NewPlayer(basepath string) (p *Player, err error) { return } - p.bus.AddSignalWatch() - p.bus.Connect("message", func(bus *gst.Bus, msg *gst.Message) { p.onMessage(bus, msg) }) - go p.dispatchRequests() return } -- cgit v0.10.2