summaryrefslogtreecommitdiff
path: root/player/player.go
blob: e60b41658f5ab136db84775908db6712448619b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//
//  rhlibrary
//
//  The Radio Helsinki Rivendell Library
//
//
//  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
//  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 <http://www.gnu.org/licenses/>.
//

package player

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path"
	"time"

	"github.com/spreadspace/go-gstreamer"
)

func init() {
	gst.Init(nil)
}

type loadResult struct {
	err error
}

type loadRequest struct {
	cart     uint
	cut      uint
	response chan<- loadResult
}

type unloadResult struct {
	err error
}

type unloadRequest struct {
	response chan<- unloadResult
}

type playResult struct {
	err error
}

type playRequest struct {
	response chan<- playResult
}

type pauseResult struct {
	err error
}

type pauseRequest struct {
	response chan<- pauseResult
}

type playPauseResult struct {
	err error
}

type playPauseRequest struct {
	response chan<- playPauseResult
}

type stopResult struct {
	err error
}

type stopRequest struct {
	response chan<- stopResult
}

type seekResult struct {
	err error
}

type seekRequest struct {
	pos      float64
	response chan<- seekResult
}

type addUpdateHandlerResult struct {
	err error
}

type addUpdateHandlerRequest struct {
	userdata interface{}
	callback UpdateCB
	response chan<- addUpdateHandlerResult
}

type addStateChangeHandlerResult struct {
	err error
}

type addStateChangeHandlerRequest struct {
	userdata interface{}
	callback StateChangeCB
	response chan<- addStateChangeHandlerResult
}

type MeterChannel struct {
	Peak  float64
	Decay float64
}
type Meter []MeterChannel
type updateData struct {
	duration time.Duration
	pos      time.Duration
	meter    Meter
}
type UpdateCB func(duration time.Duration, pos time.Duration, meter Meter, userdata interface{}) bool
type pUpdateCB struct {
	cb       UpdateCB
	userdata interface{}
}

type State int

const (
	IDLE State = 1 + iota
	PAUSED
	PLAYING
)

type StateChangeCB func(state State, userdata interface{}) bool
type pStateChangeCB struct {
	cb       StateChangeCB
	userdata interface{}
}

type Player struct {
	pipe                      *gst.Pipeline
	src                       *gst.Element
	level                     *gst.Element
	bus                       *gst.Bus
	basepath                  string
	stdlog                    *log.Logger
	dbglog                    *log.Logger
	state                     State
	duration                  time.Duration
	loadChan                  chan loadRequest
	unloadChan                chan unloadRequest
	playChan                  chan playRequest
	pauseChan                 chan pauseRequest
	playPauseChan             chan playPauseRequest
	stopChan                  chan stopRequest
	seekChan                  chan seekRequest
	addUpdateHandlerChan      chan addUpdateHandlerRequest
	updateCBs                 []*pUpdateCB
	updateChan                chan updateData
	addStateChangeHandlerChan chan addStateChangeHandlerRequest
	stateChangeCBs            []*pStateChangeCB
	stateChangeChan           chan State
}

func (p *Player) changeState(new State) {
	p.state = new
	p.stateChangeChan <- new
	if new == IDLE {
		p.updateChan <- updateData{}
	}
}

func (p *Player) onMessage(bus *gst.Bus, msg *gst.Message) {
	switch msg.GetType() {
	case gst.MESSAGE_EOS:
		p.pipe.SetState(gst.STATE_NULL)
		p.pipe.SetState(gst.STATE_PAUSED)
		p.changeState(PAUSED)
		p.duration = 0
	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.changeState(IDLE)
		p.duration = 0
		p.updateChan <- updateData{duration: p.duration}
		err, _ := msg.ParseError()
		p.stdlog.Printf("GStreamer Pipeline Error: %s", err)
	case gst.MESSAGE_DURATION_CHANGED, gst.MESSAGE_ASYNC_DONE:
		duration, ok := p.pipe.QueryDuration(gst.FORMAT_TIME)
		if ok {
			p.duration = time.Duration(duration)
			pos, ok := p.pipe.QueryPosition(gst.FORMAT_TIME)
			if !ok {
				p.stdlog.Printf("GStreamer Pipeline Error: unable to query playback position")
			}
			p.updateChan <- updateData{duration: p.duration, pos: time.Duration(pos)}
		} else {
			p.stdlog.Printf("GStreamer Pipeline Error: unable to query duration of file")
		}

	case gst.MESSAGE_ELEMENT:
		s := msg.GetStructure()
		peak := s.Data["peak"].(gst.GValueArray)
		decay := s.Data["decay"].(gst.GValueArray)

		meter := make([]MeterChannel, len(peak))
		for i := 0; i < len(peak); i++ {
			meter[i] = MeterChannel{Peak: peak[i].(float64), Decay: decay[i].(float64)}
		}
		pos := time.Duration(s.Data["stream-time"].(uint64))
		p.updateChan <- updateData{duration: p.duration, pos: pos, meter: meter}
	case gst.MESSAGE_STATE_CHANGED:
	case gst.MESSAGE_STREAM_STATUS:
	case gst.MESSAGE_STREAM_START:
	case gst.MESSAGE_TAG:
	case gst.MESSAGE_NEW_CLOCK:
	case gst.MESSAGE_RESET_TIME:
	default:
		p.dbglog.Printf("GStreamer Message: unknown type '%s'", msg.GetTypeName())
	}
}

func (p *Player) load(cart, cut uint) (resp loadResult) {
	filename := path.Join(p.basepath, fmt.Sprintf("%06d_%03d.wav", cart, cut))

	var file *os.File
	if file, resp.err = os.Open(filename); resp.err != nil {
		resp.err = fmt.Errorf("player: %s", resp.err)
		return
	}
	if info, err := file.Stat(); err != nil {
		resp.err = fmt.Errorf("player: %s", err)
		return
	} else {
		if info.IsDir() {
			resp.err = fmt.Errorf("player error: '%s' is a directory", filename)
			return
		}
	}
	file.Close()

	if p.state != IDLE {
		p.pipe.SetState(gst.STATE_NULL)
		p.changeState(IDLE)
	}
	p.src.SetProperty("uri", "file://"+filename)
	p.pipe.SetState(gst.STATE_PAUSED)
	p.changeState(PAUSED)
	return
}

func (p *Player) unload() (resp unloadResult) {
	p.pipe.SetState(gst.STATE_NULL)
	p.changeState(IDLE)
	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.changeState(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.changeState(PAUSED)
	return
}

func (p *Player) playPause() (resp playPauseResult) {
	switch p.state {
	case IDLE, PAUSED:
		p.pipe.SetState(gst.STATE_PLAYING)
		p.changeState(PLAYING)
	case PLAYING:
		p.pipe.SetState(gst.STATE_PAUSED)
		p.changeState(PAUSED)
	}
	return
}

func (p *Player) stop() (resp stopResult) {
	switch p.state {
	case IDLE:
		p.pipe.SetState(gst.STATE_NULL)
		p.changeState(IDLE)
	case PLAYING, PAUSED:
		p.pipe.SetState(gst.STATE_NULL)
		p.pipe.SetState(gst.STATE_PAUSED)
		p.changeState(PAUSED)
	}
	return
}

func (p *Player) seek(pos float64) (resp seekResult) {
	postime := int64(float64(p.duration) * pos)
	switch p.state {
	case PLAYING, PAUSED:
		p.pipe.SeekSimple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH|gst.SEEK_FLAG_KEY_UNIT, postime)
	}
	return
}

func (p *Player) addUpdateHandler(callback UpdateCB, userdata interface{}) (resp addUpdateHandlerResult) {
	p.updateCBs = append(p.updateCBs, &pUpdateCB{callback, userdata})
	return
}

func (p *Player) sendUpdate(duration time.Duration, pos time.Duration, meter Meter) {
	for _, cb := range p.updateCBs {
		if cb.cb != nil {
			if keep := cb.cb(duration, pos, meter, cb.userdata); !keep {
				cb.cb = nil
			}
		}
	}
	return
}

func (p *Player) addStateChangeHandler(callback StateChangeCB, userdata interface{}) (resp addStateChangeHandlerResult) {
	p.stateChangeCBs = append(p.stateChangeCBs, &pStateChangeCB{callback, userdata})
	return
}

func (p *Player) sendStateChange(s State) {
	for _, cb := range p.stateChangeCBs {
		if cb.cb != nil {
			if keep := cb.cb(s, cb.userdata); !keep {
				cb.cb = nil
			}
		}
	}
	return
}

func (p *Player) dispatchRequests() {
	p.bus.AddSignalWatch()
	p.bus.Connect("message", func(bus *gst.Bus, msg *gst.Message) { p.onMessage(bus, msg) })

	for {
		select {
		case req := <-p.loadChan:
			req.response <- p.load(req.cart, req.cut)
		case req := <-p.unloadChan:
			req.response <- p.unload()
		case req := <-p.playChan:
			req.response <- p.play()
		case req := <-p.pauseChan:
			req.response <- p.pause()
		case req := <-p.playPauseChan:
			req.response <- p.playPause()
		case req := <-p.stopChan:
			req.response <- p.stop()
		case req := <-p.seekChan:
			req.response <- p.seek(req.pos)
		case req := <-p.addUpdateHandlerChan:
			req.response <- p.addUpdateHandler(req.callback, req.userdata)
		case u := <-p.updateChan:
			p.sendUpdate(u.duration, u.pos, u.meter)
		case req := <-p.addStateChangeHandlerChan:
			req.response <- p.addStateChangeHandler(req.callback, req.userdata)
		case s := <-p.stateChangeChan:
			p.sendStateChange(s)
		}
	}
}

func (p *Player) createPipeline() (err error) {
	if p.pipe, err = gst.PipelineNew("rhlibrary"); err != nil {
		return
	}

	var conv1, conv2, sink *gst.Element
	if p.src, err = gst.ElementFactoryMake("uridecodebin", "source"); err != nil {
		return
	}
	if conv1, err = gst.ElementFactoryMake("audioconvert", "conv1"); err != nil {
		return
	}
	if p.level, err = gst.ElementFactoryMake("level", "meter"); err != nil {
		return
	}
	p.level.SetProperty("message", true)
	p.level.SetProperty("interval", 25000000)
	p.level.SetProperty("peak-ttl", 300000000)
	p.level.SetProperty("peak-falloff", 25)
	if conv2, err = gst.ElementFactoryMake("audioconvert", "conv2"); err != nil {
		return
	}
	if sink, err = gst.ElementFactoryMake("autoaudiosink", "sink"); err != nil {
		return
	}

	p.pipe.Add(p.src)
	p.pipe.Add(conv1)
	p.pipe.Add(p.level)
	p.pipe.Add(conv2)
	p.pipe.Add(sink)

	sinkpad, err := conv1.GetStaticPad("sink")
	if err != nil {
		return fmt.Errorf("player error getting sink pad from conv1: %s", err)
	}
	p.src.Connect("pad-added", func(_ interface{}, srcpad *gst.Pad) {
		if ret := srcpad.Link(sinkpad); ret != gst.PAD_LINK_OK {
			p.stdlog.Printf("player error linking src with conv1 (code: %v)", ret)
		}
		p.dbglog.Println("player succesfully linked src with conv1")
	})
	conv1.Link(p.level)
	p.level.Link(conv2)
	conv2.Link(sink)

	if p.bus, err = p.pipe.GetBus(); err != nil {
		return
	}
	return
}

// *********************************************************
// Public Interface

type PlayerChan struct {
	load                  chan<- loadRequest
	unload                chan<- unloadRequest
	play                  chan<- playRequest
	playPause             chan<- playPauseRequest
	pause                 chan<- pauseRequest
	stop                  chan<- stopRequest
	seek                  chan<- seekRequest
	addUpdateHandler      chan<- addUpdateHandlerRequest
	addStateChangeHandler chan<- addStateChangeHandlerRequest
}

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) Unload() error {
	resCh := make(chan unloadResult)
	req := unloadRequest{}
	req.response = resCh
	p.unload <- 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) PlayPause() error {
	resCh := make(chan playPauseResult)
	req := playPauseRequest{}
	req.response = resCh
	p.playPause <- 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 *PlayerChan) Seek(pos float64) error {
	resCh := make(chan seekResult)
	req := seekRequest{}
	req.pos = pos
	req.response = resCh
	p.seek <- req

	res := <-resCh
	if res.err != nil {
		return res.err
	}
	return nil
}

func (p *PlayerChan) AddUpdateHandler(callback UpdateCB, userdata interface{}) error {
	resCh := make(chan addUpdateHandlerResult)
	req := addUpdateHandlerRequest{}
	req.callback = callback
	req.userdata = userdata
	req.response = resCh
	p.addUpdateHandler <- req

	res := <-resCh
	if res.err != nil {
		return res.err
	}
	return nil
}

func (p *PlayerChan) AddStateChangeHandler(callback StateChangeCB, userdata interface{}) error {
	resCh := make(chan addStateChangeHandlerResult)
	req := addStateChangeHandlerRequest{}
	req.callback = callback
	req.userdata = userdata
	req.response = resCh
	p.addStateChangeHandler <- req

	res := <-resCh
	if res.err != nil {
		return res.err
	}
	return nil
}

func (p *Player) GetInterface() *PlayerChan {
	ch := &PlayerChan{}
	ch.load = p.loadChan
	ch.unload = p.unloadChan
	ch.play = p.playChan
	ch.pause = p.pauseChan
	ch.playPause = p.playPauseChan
	ch.stop = p.stopChan
	ch.seek = p.seekChan
	ch.addUpdateHandler = p.addUpdateHandlerChan
	ch.addStateChangeHandler = p.addStateChangeHandlerChan
	return ch
}

func NewPlayer(basepath string, stdlog *log.Logger, dbglog *log.Logger) (p *Player, err error) {
	p = &Player{}
	p.basepath = path.Clean(basepath)
	p.state = IDLE
	p.duration = 0
	if stdlog != nil {
		p.stdlog = stdlog
	} else {
		p.stdlog = log.New(ioutil.Discard, "rhrd-go.player", log.LstdFlags)
	}
	if dbglog != nil {
		p.dbglog = dbglog
	} else {
		p.dbglog = log.New(ioutil.Discard, "rhrd-go.player-dbg", log.LstdFlags)
	}
	p.loadChan = make(chan loadRequest)
	p.unloadChan = make(chan unloadRequest)
	p.playChan = make(chan playRequest)
	p.pauseChan = make(chan pauseRequest)
	p.playPauseChan = make(chan playPauseRequest)
	p.stopChan = make(chan stopRequest)
	p.seekChan = make(chan seekRequest)
	p.addUpdateHandlerChan = make(chan addUpdateHandlerRequest)
	p.updateChan = make(chan updateData, 20)
	p.addStateChangeHandlerChan = make(chan addStateChangeHandlerRequest)
	p.stateChangeChan = make(chan State, 10)

	if err = p.createPipeline(); err != nil {
		return
	}

	go p.dispatchRequests()
	return
}