From 2ae9d877c4a98f3f327162c55a03743e10c191e8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 15 Jan 2016 05:20:42 +0100 Subject: improved type handling diff --git a/player/player.go b/player/player.go index 5fe631c..eb0887a 100644 --- a/player/player.go +++ b/player/player.go @@ -26,12 +26,12 @@ package player import ( "fmt" - "github.com/gotk3/gotk3/glib" "github.com/spreadspace/go-gstreamer" "io/ioutil" "log" "os" "path" + "time" ) func init() { @@ -86,13 +86,13 @@ type MeterChannel struct { peak float64 decay float64 } -type Meter map[int]MeterChannel +type Meter []MeterChannel type updateData struct { - len int64 - pos int64 + len time.Duration + pos time.Duration meter Meter } -type UpdateCB func(len int64, pos int64, meter Meter, userdata interface{}) bool +type UpdateCB func(len time.Duration, pos time.Duration, meter Meter, userdata interface{}) bool type State int @@ -111,6 +111,7 @@ type Player struct { stdlog *log.Logger dbglog *log.Logger state State + len time.Duration loadChan chan loadRequest playChan chan playRequest pauseChan chan pauseRequest @@ -124,19 +125,23 @@ func (p *Player) onMessage(bus *gst.Bus, msg *gst.Message) { case gst.MESSAGE_EOS: p.pipe.SetState(gst.STATE_NULL) p.state = IDLE - p.updateChan <- updateData{} + p.len = 0 + p.updateChan <- updateData{len: p.len} case gst.MESSAGE_WARNING: warn, _ := msg.ParseWarning() p.stdlog.Printf("GStreamer Pipeline Warning: %s", warn) case gst.MESSAGE_ERROR: p.pipe.SetState(gst.STATE_NULL) p.state = IDLE + p.len = 0 + p.updateChan <- updateData{len: p.len} err, _ := msg.ParseError() p.stdlog.Printf("GStreamer Pipeline Error: %s", err) case gst.MESSAGE_ASYNC_DONE: len, ok := p.pipe.QueryDuration(gst.FORMAT_TIME) if ok { - p.updateChan <- updateData{len: len} + p.len = time.Duration(len) + p.updateChan <- updateData{len: p.len} } else { p.stdlog.Printf("GStreamer Pipeline Error: unable to query duration of file") } @@ -145,21 +150,12 @@ func (p *Player) onMessage(bus *gst.Bus, msg *gst.Message) { peak := s.Data["peak"].(gst.GValueArray) decay := s.Data["decay"].(gst.GValueArray) - meter := make(map[int]MeterChannel, len(peak)) + meter := make([]MeterChannel, len(peak)) for i := 0; i < len(peak); i++ { meter[i] = MeterChannel{peak: peak[i].(float64), decay: decay[i].(float64)} } - - len, ok := p.pipe.QueryDuration(gst.FORMAT_TIME) - if !ok { - p.stdlog.Printf("GStreamer Pipeline Error: unable to query duration of file") - } - pos, ok := p.pipe.QueryPosition(gst.FORMAT_TIME) - if !ok { - p.stdlog.Printf("GStreamer Pipeline Error: unable to query duration of file") - } - - p.updateChan <- updateData{len: len, pos: pos, meter: meter} + pos := time.Duration(s.Data["stream-time"].(uint64)) + p.updateChan <- updateData{len: p.len, pos: pos, meter: meter} case gst.MESSAGE_STATE_CHANGED: default: p.dbglog.Printf("GStreamer Message: unknown type '%s'", msg.GetTypeName()) @@ -235,8 +231,8 @@ func (p *Player) addUpdateHandler(callback UpdateCB, userdata interface{}) (resp return } -func (p *Player) sendUpdate(len int64, pos int64, meter Meter) { - p.dbglog.Printf("player update: len: %d, pos: %d, meter: %+v", len, pos, meter) +func (p *Player) sendUpdate(len time.Duration, pos time.Duration, meter Meter) { + p.dbglog.Printf("player update: len: %s, pos: %s, meter: %+v", len.String(), pos.String(), meter) // TODO: send this to all subscribed handler return } @@ -298,7 +294,7 @@ func (p *Player) createPipeline() (err error) { if err != nil { return fmt.Errorf("player error getting sink pad from conv1: %s", err) } - p.src.Connect("pad-added", func(_ *glib.Object, srcpad *gst.Pad) { + p.src.Connect("pad-added", func(_ interface{}, srcpad *gst.Pad) { if ret := srcpad.Link(sinkpad); ret != gst.PAD_LINK_OK { p.stdlog.Println("player error linking src with conv1 (code: %v)", ret) } @@ -407,6 +403,7 @@ func (p *Player) GetInterface() *PlayerChan { func NewPlayer(basepath string, stdlog *log.Logger, dbglog *log.Logger) (p *Player, err error) { p = &Player{} p.basepath = path.Clean(basepath) + p.len = 0 if stdlog != nil { p.stdlog = stdlog } else { -- cgit v0.10.2