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
|
//
// 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"
"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
}
|