/
MediaPane.go
416 lines (325 loc) · 10.3 KB
/
MediaPane.go
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
package ui
import (
"encoding/json"
"image"
"math"
"sync"
"time"
"github.com/ninjasphere/gestic-tools/go-gestic-sdk"
"github.com/ninjasphere/go-ninja/api"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-ninja/config"
"github.com/ninjasphere/sphere-go-led-controller/util"
"github.com/ninjasphere/go-ninja/logger"
)
var volumeModeReset = config.MustDuration("led.media.volumeModeReset")
var mediaTapTimeout = config.MustDuration("led.media.tapInterval")
var volumeInterval = config.MustDuration("led.media.volumeInterval")
var airWheelReset = config.MustDuration("led.media.airWheelReset")
type MediaPane struct {
log *logger.Logger
conn *ninja.Connection
volumeMode bool
volumeModeReset *time.Timer
volumeUpDownMode bool
volumeUpDown *bool
lastAirWheelTime time.Time
lastAirWheel *uint8
countSinceLast int
volume float64
volumeImage util.Image
volumeUpImage util.Image
volumeDownImage util.Image
muteImage util.Image
lastVolumeTime time.Time
lastSentVolume float64
volumeUpDownTimer *time.Timer
ignoringTap bool
ignoreTapTimer *time.Timer
playingState string
playImage util.Image
pauseImage util.Image
stopImage util.Image
nextImage util.Image
gestureSync *sync.Mutex
controlDevices []*ninja.ServiceClient
volumeDevices []*ninja.ServiceClient
}
type MediaPaneImages struct {
Volume string
VolumeUp string
VolumeDown string
Mute string
Play string
Pause string
Stop string
Next string
}
var mediaImages = MediaPaneImages{
Volume: util.ResolveImagePath(config.MustString("led.media.images.volume")),
VolumeUp: util.ResolveImagePath(config.MustString("led.media.images.volumeUp")),
VolumeDown: util.ResolveImagePath(config.MustString("led.media.images.volumeDown")),
Mute: util.ResolveImagePath(config.MustString("led.media.images.mute")),
Play: util.ResolveImagePath(config.MustString("led.media.images.play")),
Pause: util.ResolveImagePath(config.MustString("led.media.images.pause")),
Stop: util.ResolveImagePath(config.MustString("led.media.images.stop")),
Next: util.ResolveImagePath(config.MustString("led.media.images.next")),
}
func NewMediaPane(conn *ninja.Connection) *MediaPane {
log := logger.GetLogger("MediaPane")
pane := &MediaPane{
log: log,
conn: conn,
gestureSync: &sync.Mutex{},
volumeImage: util.LoadImage(mediaImages.Volume),
volumeUpImage: util.LoadImage(mediaImages.VolumeUp),
volumeDownImage: util.LoadImage(mediaImages.VolumeDown),
muteImage: util.LoadImage(mediaImages.Mute),
playImage: util.LoadImage(mediaImages.Play),
pauseImage: util.LoadImage(mediaImages.Pause),
stopImage: util.LoadImage(mediaImages.Stop),
nextImage: util.LoadImage(mediaImages.Next),
playingState: "stopped",
lastVolumeTime: time.Now(),
//lastAirWheelTime: time.Now(),
}
e := func(state string) func(params *json.RawMessage, values map[string]string) bool {
return func(params *json.RawMessage, values map[string]string) bool {
if !pane.ignoringTap {
pane.log.Infof("Received control event. Setting playing state to %s", state)
pane.playingState = state
}
return true
}
}
listening := make(map[string]bool)
getChannelServicesContinuous("mediaplayer", "media-control", nil, func(devices []*ninja.ServiceClient, err error) {
if err != nil {
log.Infof("Failed to update control devices: %s", err)
} else {
pane.controlDevices = devices
log.Infof("Got %d media-control devices", len(devices))
for _, device := range devices {
if _, ok := listening[device.Topic]; !ok {
listening[device.Topic] = true
// New Device
log.Infof("Got new control device: %s", device.Topic)
device.OnEvent("playing", e("playing"))
device.OnEvent("buffering", e("playing"))
device.OnEvent("paused", e("paused"))
device.OnEvent("stopped", e("stopped"))
}
}
}
if len(pane.controlDevices) == 0 {
pane.volumeMode = true
pane.log.Debugf("No control devices. Forcing volume mode.")
}
})
getChannelServicesContinuous("mediaplayer", "volume", nil, func(devices []*ninja.ServiceClient, err error) {
if err != nil {
log.Infof("Failed to update volume devices: %s", err)
} else {
log.Infof("Got %d volume devices", len(devices))
pane.volumeDevices = devices
pane.volumeUpDownMode = false
for _, device := range devices {
if !isValueInList("set", device.SupportedMethods) && isValueInList("volumeUp", device.SupportedMethods) {
pane.log.Infof("Volume up/down mode!")
pane.volumeUpDownMode = true
}
}
for _, device := range devices {
log.Debugf("Checking volume device %s", device.Topic)
if _, ok := listening[device.Topic]; !ok {
listening[device.Topic] = true
// New device
pane.log.Infof("Got new volume device: %s supported: %v", device.Topic, device.SupportedMethods)
device.OnEvent("state", func(params *json.RawMessage, values map[string]string) bool {
if time.Since(pane.lastVolumeTime) > time.Millisecond*500 {
var volume channels.VolumeState
err := json.Unmarshal(*params, &volume)
if err != nil {
pane.log.Infof("Failed to unmarshal volume from %s error:%s", *params, err)
}
pane.volume = *volume.Level
}
return true
})
}
}
}
if len(pane.controlDevices) == 0 {
pane.volumeMode = true
pane.log.Debugf("No control devices. Forcing volume mode.")
}
})
pane.volumeModeReset = time.AfterFunc(0, func() {
if len(pane.controlDevices) > 0 {
pane.volumeMode = false
pane.log.Debugf("Going back to volume mode")
}
})
pane.ignoreTapTimer = time.AfterFunc(0, func() {
pane.ignoringTap = false
})
pane.volumeUpDownTimer = time.AfterFunc(0, func() {
pane.volumeUpDown = nil
})
return pane
}
func isValueInList(value string, list []string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
func (p *MediaPane) IsEnabled() bool {
return len(p.volumeDevices) > 0 || len(p.controlDevices) > 0
}
func (p *MediaPane) KeepAwake() bool {
return false
}
func (p *MediaPane) Gesture(gesture *gestic.GestureMessage) {
p.gestureSync.Lock()
defer p.gestureSync.Unlock()
//x, _ := json.Marshal(gesture)
//p.log.Infof("vol devices: %d last: %d counter: %d sinceLast: %d", len(p.volumeDevices), p.lastAirWheel, gesture.AirWheel.Counter, gesture.AirWheel.CountSinceLast)
if len(p.volumeDevices) > 0 && (p.lastAirWheel == nil || gesture.AirWheel.Counter != int(*p.lastAirWheel)) {
p.volumeMode = true
p.volumeModeReset.Reset(volumeModeReset)
if time.Since(p.lastAirWheelTime) > airWheelReset {
p.lastAirWheel = nil
}
if p.countSinceLast > gesture.AirWheel.CountSinceLast {
p.lastAirWheel = nil
}
p.countSinceLast = gesture.AirWheel.CountSinceLast
p.lastAirWheelTime = time.Now()
p.log.Debugf("Airwheel: %d", gesture.AirWheel.Counter)
if p.lastAirWheel != nil {
offset := int(gesture.AirWheel.Counter) - int(*p.lastAirWheel)
if offset > 30 {
offset -= 255
}
if offset < -30 {
offset += 255
}
p.log.Debugf("Airwheel New: %d Offset: %d Last: %d", gesture.AirWheel.Counter, offset, *p.lastAirWheel)
p.log.Debugf("Current volume %f", p.volume)
p.log.Debugf("Volume offset %f", float64(offset)/255.0)
if p.volumeUpDownMode {
if time.Since(p.lastVolumeTime) < volumeInterval {
p.log.Debugf("Volume rate limited")
} else {
p.lastVolumeTime = time.Now()
p.log.Debugf("Volume NOT rate limited")
dir := offset > 0
p.volumeUpDown = &dir
p.volumeUpDownTimer.Reset(time.Millisecond * 500)
go p.SendVolumeAdjust(dir)
}
} else {
var volume = p.volume + (float64(offset)/255.0)*float64(2)
volume = math.Max(volume, 0)
volume = math.Min(volume, 1)
p.log.Debugf("Adjusted volume %f:", volume)
p.volume = volume
if p.lastSentVolume != volume {
if time.Since(p.lastVolumeTime) < volumeInterval {
p.log.Debugf("Volume rate limited")
} else {
p.lastVolumeTime = time.Now()
p.log.Debugf("Volume NOT rate limited")
go p.SendVolume()
}
}
}
}
val := uint8(gesture.AirWheel.Counter)
p.lastAirWheel = &val
//spew.Dump("last2", p.lastAirWheel)
}
if len(p.controlDevices) > 0 && !p.ignoringTap && gesture.Tap.Active() {
p.log.Infof("Tap!")
p.ignoringTap = true
p.ignoreTapTimer.Reset(mediaTapTimeout)
switch p.playingState {
case "stopped":
p.SetControlState("playing")
case "playing":
p.SetControlState("paused")
case "paused":
p.SetControlState("playing")
}
p.volumeModeReset.Stop()
p.volumeMode = false
}
}
func (p *MediaPane) SetControlState(state string) {
p.log.Debugf("Setting playing state %s:", state)
var method = ""
switch state {
case "stopped":
method = "stop"
case "playing":
method = "play"
case "paused":
method = "pause"
}
for _, device := range p.controlDevices {
device.Call(method, nil, nil, 0)
}
p.playingState = state
}
func (p *MediaPane) SendVolumeAdjust(direction bool) {
p.log.Debugf("Sending volume direction %v:", direction)
for _, device := range p.volumeDevices {
if direction {
device.Call("volumeUp", nil, nil, 0)
} else {
device.Call("volumeDown", nil, nil, 0)
}
}
//p.onStateChange(state)
}
func (p *MediaPane) SendVolume() {
p.log.Debugf("New volume %f:", p.volume)
// p.volume = volume
p.lastSentVolume = p.volume
for _, device := range p.volumeDevices {
device.Call("set", channels.VolumeState{Level: &p.volume}, nil, 0)
}
//p.onStateChange(state)
}
func (p *MediaPane) Render() (*image.RGBA, error) {
if p.volumeMode {
if p.volumeUpDownMode {
if p.volumeUpDown != nil {
if *p.volumeUpDown {
return p.volumeUpImage.GetNextFrame(), nil
}
return p.volumeDownImage.GetNextFrame(), nil
}
return p.volumeImage.GetPositionFrame(0, true), nil
}
if p.volume > 0 {
return p.volumeImage.GetPositionFrame(1-p.volume, true), nil
}
return p.muteImage.GetNextFrame(), nil
}
switch p.playingState {
case "stopped":
return p.stopImage.GetNextFrame(), nil
case "playing":
return p.playImage.GetNextFrame(), nil
case "paused":
return p.pauseImage.GetNextFrame(), nil
}
return p.stopImage.GetNextFrame(), nil
}
func (p *MediaPane) IsDirty() bool {
return true
}