// // rhlibrary // // The Radio Helsinki Rivendell Library // // // Copyright (C) 2016 Christian Pointner // // This file is part of rhlibrary. // // rhlibrary is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // rhlibrary is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with rhlibrary. If not, see . // package player import ( "fmt" "github.com/spreadspace/go-gstreamer" "io/ioutil" "log" "path" ) func init() { gst.Init(nil) } type loadResult struct { err error } type loadRequest struct { cart uint cut uint response chan<- loadResult } type playResult struct { err error } type playRequest struct { response chan<- playResult } type pauseResult struct { err error } type pauseRequest struct { response chan<- pauseResult } type stopResult struct { err error } type stopRequest struct { response chan<- stopResult } 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(msg *gst.Message) { switch msg.GetType() { case gst.MESSAGE_EOS: p.log.Printf("GStreamer Pipeline: EOS reached!\n") p.pipe.SetState(gst.STATE_NULL) case gst.MESSAGE_WARNING: warn, _ := msg.ParseWarning() p.log.Printf("GStreamer Pipeline Warning: %s\n", warn) case gst.MESSAGE_ERROR: p.pipe.SetState(gst.STATE_NULL) err, _ := msg.ParseError() p.log.Printf("GStreamer Pipeline Error: %s\n", err) case gst.MESSAGE_ASYNC_DONE: len, ok := p.pipe.QueryDuration(gst.FORMAT_TIME) if ok { p.log.Printf("GStreamer Pipeline: loaded file has length: %d.%d s\n", len/1000000000, len%1000000000) } else { p.log.Printf("GStreamer Pipeline Error: unable to query duration of file\n") } case gst.MESSAGE_STATE_CHANGED: default: p.log.Printf("GStreamer Message: unknown type '%s'\n", msg.GetTypeName()) } } func (p *Player) load(cart, cut uint) (resp loadResult) { filename := path.Join(p.basepath, fmt.Sprintf("%06d_%03d.wav", cart, cut)) p.pipe.SetProperty("uri", "file://"+filename) p.pipe.SetState(gst.STATE_PAUSED) return } func (p *Player) play() (resp playResult) { p.pipe.SetState(gst.STATE_PLAYING) return } func (p *Player) pause() (resp pauseResult) { p.pipe.SetState(gst.STATE_PAUSED) return } func (p *Player) stop() (resp stopResult) { p.pipe.SetState(gst.STATE_NULL) return } 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: req.response <- p.load(req.cart, req.cut) case req := <-p.playChan: req.response <- p.play() case req := <-p.pauseChan: req.response <- p.pause() case req := <-p.stopChan: req.response <- p.stop() case msg := <-msgChan: p.onMessage(&msg) } } } // ********************************************************* // Public Interface type PlayerChan struct { load chan<- loadRequest play chan<- playRequest pause chan<- pauseRequest stop chan<- stopRequest } func (p *PlayerChan) Load(cart, cut uint) error { resCh := make(chan loadResult) req := loadRequest{} req.cart = cart req.cut = cut req.response = resCh p.load <- req res := <-resCh if res.err != nil { return res.err } return nil } func (p *PlayerChan) Play() error { resCh := make(chan playResult) req := playRequest{} req.response = resCh p.play <- req res := <-resCh if res.err != nil { return res.err } return nil } func (p *PlayerChan) Pause() error { resCh := make(chan pauseResult) req := pauseRequest{} req.response = resCh p.pause <- req res := <-resCh if res.err != nil { return res.err } return nil } func (p *PlayerChan) Stop() error { resCh := make(chan stopResult) req := stopRequest{} req.response = resCh p.stop <- req res := <-resCh if res.err != nil { return res.err } return nil } func (p *Player) GetInterface() *PlayerChan { ch := &PlayerChan{} ch.load = p.loadChan ch.play = p.playChan ch.pause = p.pauseChan ch.stop = p.stopChan return ch } 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) p.stopChan = make(chan stopRequest) if p.pipe, err = gst.ElementFactoryMake("playbin", "autoplay"); err != nil { return } if p.bus, err = p.pipe.GetBus(); err != nil { return } go p.dispatchRequests() return }