-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqueezebox2.go
486 lines (423 loc) · 12.1 KB
/
squeezebox2.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
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
package main
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"net"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
type squeezebox2 struct {
Queue *Queue
conn *net.TCPConn
font psfFont
volume int
mac net.HardwareAddr
}
func (s *squeezebox2) GetID() string {
return s.mac.String()
}
func (s *squeezebox2) GetModel() string {
return "Squeezebox 2"
}
func (s *squeezebox2) GetName() string {
if v, ok := persistent.Clients[s.mac.String()]; ok {
return v.Name
}
return s.mac.String()
}
func (s *squeezebox2) Listener() {
// Load the font for this player
f, err := os.Open("ter-132n.psf")
if err != nil {
logger.Panicw("unable read font",
"err", err)
}
font := readPSF(f)
s.font = font
f.Close()
// Display init message
ctx, _ := context.WithTimeout(context.Background(), time.Second)
buf := <-s.DisplayText("SlimYTM", ctx)
s.Render(buf)
time.Sleep(time.Second * 2)
go s.Queue.Composite()
// Set the volume to 1/2 intially
s.SetVolume(50)
// Start receiving messages
for {
b := make([]byte, 1024)
s.conn.SetReadDeadline(time.Now().Add(HEARTBEAT_INTERVAL * 3))
_, err := s.conn.Read(b)
if errors.Is(err, os.ErrDeadlineExceeded) {
// Client has timed out, remove its queue
for k, v := range queues {
if v.Player.GetID() == s.GetID() && v.Player.(*squeezebox2).conn == s.conn {
queues = append(queues[:k], queues[k+1:]...)
}
}
logger.DPanic("player has timed out")
s.conn.Close()
metricConnectedPlayers.Dec()
return
} else if err != nil {
logger.Errorw("unable to read from connection",
"err", err)
return
}
metricPacketsRx.WithLabelValues(s.GetName()).Inc()
if string(b[:4]) == "STAT" {
// Status message from the squeezebox
if string(b[8:12]) == "STMa" {
s.Queue.Playing = true
s.Queue.Loading = false
s.Queue.UpdateClients()
}
elapsed := int(binary.BigEndian.Uint32(b[45:49]))
if s.Queue.ElapsedSecs != elapsed {
s.Queue.LastElapsedUpdate = time.Now()
}
s.Queue.ElapsedSecs = elapsed
} else if string(b[:4]) == "IR " {
if time.Since(lastIR) < IR_INTERVAL {
// Prevent duplicate IR commands from ruining our day
continue
}
// IR command from the remote
irCode := hex.EncodeToString(b[14:18])
logger.Debugw("ir event",
"code", irCode)
sendXPL(xplMessage{
messageType: "xpl-trig",
target: "*",
schema: "remote.basic",
body: map[string]string{
"keys": irCode,
"device": s.GetName(),
"zone": "slimserver",
"power": "on",
},
}, "slimdev-slimserv."+s.GetName())
if irCode == "7689807f" {
// Volume UP
s.SetVolume(s.volume + 5)
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
s.Queue.Texts = append(s.Queue.Texts, text{
bufs: s.DisplayText(fmt.Sprintf("Volume = %v/100", s.volume), ctx),
ctx: ctx,
})
} else if irCode == "768900ff" {
// Volume DOWN
s.SetVolume(s.volume - 5)
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
s.Queue.Texts = append(s.Queue.Texts, text{
bufs: s.DisplayText(fmt.Sprintf("Volume = %v/100", s.volume), ctx),
ctx: ctx,
})
} else if irCode == "7689a05f" {
// NEXT Song
s.Queue.Next()
} else if irCode == "7689c03f" {
// PREVIOUS Song
s.Queue.Previous()
} else if irCode == "768920df" {
// PAUSE/UNPAUSE Song
s.Queue.Pause()
} else if irCode == "768940bf" {
// RESET Queue
s.Queue.Reset()
}
lastIR = time.Now()
}
}
}
func (s *squeezebox2) Heartbeat() {
for {
// Send the strm t command to request status
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(28))
msg = append(msg, []byte("strm")...)
msg = append(msg, 't', '0', 'm', '?', '?', '?', '?', 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 35, 41, 0, 0, 0, 0)
_, err := s.conn.Write(msg)
if err != nil {
// Client probably dropped conn
logger.DPanicw("could not send heartbeat",
"err", err)
return
}
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
time.Sleep(HEARTBEAT_INTERVAL)
}
}
func (s *squeezebox2) DisplayClock() chan []byte {
out := make(chan []byte)
go func() {
for {
buf := make([]byte, 1280)
h, m, sec := time.Now().Local().Clock()
for k, v := range fmt.Sprintf(" %02d:%02d:%02d", h, m, sec) {
// Set each character individually with an offset
s.setChar(s.font.getChar(int(v)), k*8, buf)
}
out <- buf
}
}()
return out
}
// Return a channel of framebuffers, scrolling the text if needed.
func (s *squeezebox2) DisplayText(text string, ctx context.Context) chan []byte {
out := make(chan []byte)
if len(text) > 20 {
// Scroll text across screen
text += " "
variableFrame := make([]byte, 4*16*len(text))
for k, v := range text {
s.setChar(s.font.getChar(int(v)), k*8, variableFrame)
}
go s.scrollBuffer(variableFrame, ctx, out)
} else {
buf := make([]byte, 1280)
for k, v := range text {
// Set each character individually with an offset
s.setChar(s.font.getChar(int(v)), k*8, buf)
}
go func() {
// Output the framebuffer until the context expires
for {
select {
case <-ctx.Done():
return
case out <- buf:
}
}
}()
}
return out
}
func (s *squeezebox2) Play(videoID string) (cancel func()) {
retry:
start := time.Now()
// Get the player URL with yt-dlp
co := exec.Command("yt-dlp", "https://music.youtube.com/watch?v="+videoID, "-f", "bestaudio[ext=webm]", "-g")
logger.Debugw("getting audio download url",
"cmd", co.String())
b, err := co.CombinedOutput()
url := strings.Trim(string(b), " \n")
logger.Debugw("yt-dlp command output", "output", url)
if err != nil {
logger.Errorw("unable to get audio download url",
"err", err)
return
}
// Ensure the url that youtube music returns is actually valid
// (stupid google sometimes returns urls that 403)
resp, err := http.DefaultClient.Head(url)
if err != nil {
logger.DPanicw("could not request youtube music url",
"err", err)
return
}
if resp.StatusCode != 200 {
logger.Warnw("youtube music did not return 200 for url, retrying",
"url", url,
"status", resp.Status)
goto retry
}
// Start FFMPEG with the URL, piping stdout to our audio buffer
s.Queue.Buffer.Reset()
ctx, cancel := context.WithCancel(context.Background())
fcmd := NewCommand(ctx, "ffmpeg", "-reconnect", "1", "-reconnect_streamed", "1", "-reconnect_delay_max", "5", "-i",
string(url), "-f", "wav", "-ar", "44100", "-ac", "2", "-loglevel", "warning", "-vn", "-")
fcmd.Stdout = s.Queue.Buffer
fcmd.Stderr = os.Stderr
err = fcmd.Start()
if err != nil {
logger.Errorw("unable to start ffmpeg stream",
"err", err)
return
}
// Wait until with have at least AUDIO_PRELOAD seconds of audio in our buffer
for {
time.Sleep(50 * time.Millisecond)
if s.Queue.Buffer.Len() > 44100*2*2*AUDIO_PRELOAD {
break
}
}
// Send the strm command to the Squeezebox
header := fmt.Sprintf("GET /player/%v/audio.wav HTTP/1.0\n\n", s.GetID())
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(28+len(header)))
msg = append(msg, []byte("strm")...)
msg = append(msg, 's', '1', 'p', '1', '3', '2', '1', 0xff, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 35, 41, 0, 0, 0, 0)
msg = append(msg, []byte(header)...)
logger.Debugw("sending play",
"len", len(msg),
"data", msg,
"elapsedMs", time.Since(start)/time.Millisecond)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
s.Queue.Playing = true
s.Queue.Loading = false
s.Queue.UpdateClients()
metricLoadTime.Observe(float64(time.Since(start)) / float64(time.Second))
return cancel
}
func (s *squeezebox2) Stop() {
// Attempts to fix issue where squeezebox won't start next song
s.stop()
time.Sleep(100 * time.Millisecond)
s.stop()
}
func (s *squeezebox2) stop() {
// Send the strm command to the Squeezebox
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(28))
msg = append(msg, []byte("strm")...)
msg = append(msg, 'q', '1', 'p', '1', '3', '2', '1', 0xff, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 35, 41, 0, 0, 0, 0)
logger.Debugw("sending stop",
"len", len(msg),
"data", msg)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
}
func (s *squeezebox2) Pause() {
// Send the strm command to the Squeezebox
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(28))
msg = append(msg, []byte("strm")...)
msg = append(msg, 'p', '0', 'm', '?', '?', '?', '?', 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 35, 41, 0, 0, 0, 0)
logger.Debugw("sending pause",
"len", len(msg),
"data", msg)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
}
func (s *squeezebox2) Unpause() {
// Send the strm command to the Squeezebox
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(28))
msg = append(msg, []byte("strm")...)
msg = append(msg, 'u', '0', 'm', '?', '?', '?', '?', 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 35, 41, 0, 0, 0, 0)
logger.Debugw("sending unpause",
"len", len(msg),
"data", msg)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
}
func (s *squeezebox2) SetVolume(volume int) {
// Set the volume (0-100)
if volume < 0 {
volume = 0
} else if volume > 100 {
volume = 100
}
// Old gain for Squeezebox2 with firmware < 22
oldGain := make([]byte, 4)
binary.BigEndian.PutUint32(oldGain, uint32(float64(volume)/100*128))
// New gain with fancy dB stuff
newGain := make([]byte, 4)
m := 50 / float64(100+1)
db := m * (float64(volume) - 100)
floatMult := math.Pow(10, db/20)
if db >= -30 && db < 0 {
binary.BigEndian.PutUint32(newGain, uint32(floatMult*(1<<8)+0.5)*(1<<8))
} else {
binary.BigEndian.PutUint32(newGain, uint32(floatMult*(1<<16)+0.5))
}
if volume == 0 {
newGain = []byte{0, 0, 0, 0}
}
// Dispatch volume message
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, uint16(22))
msg = append(msg, []byte("audg")...)
msg = append(msg, oldGain...)
msg = append(msg, oldGain...)
msg = append(msg, 1, 255) // Always use digital volume and 255 preamp
msg = append(msg, newGain...)
msg = append(msg, newGain...)
logger.Debugw("sending volume",
"len", len(msg),
"data", msg)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
s.volume = volume
}
func (s *squeezebox2) GetVolume() int {
return s.volume
}
func (s *squeezebox2) Render(buf []byte) {
if len(buf) != 1280 {
logger.Panic("framebuffer has incorrect length")
}
// Send the current framebuffer to the Squeezebox
msg := make([]byte, 2)
binary.BigEndian.PutUint16(msg, 1288)
msg = append(msg, []byte("grfe")...)
msg = append(msg, 0, 0, 'c', 'c')
msg = append(msg, buf...)
s.conn.Write(msg)
metricPacketsTx.WithLabelValues(s.GetName()).Inc()
}
// Displays the whole buffer forever until it cancelled
func (s *squeezebox2) scrollBuffer(varBuffer []byte, ctx context.Context, out chan []byte) {
for {
// Wait until we are being composited before starting the timer
out <- varBuffer[:1280]
out <- varBuffer[:1280]
stationary := time.NewTimer(time.Second * 3)
outer:
for {
// Display the first 40 characters for 3 seconds
select {
case out <- varBuffer[:1280]:
case <-stationary.C:
break outer
case <-ctx.Done():
// Exit if we have been cancelled (by twitter)
return
}
}
// Scroll text across until we reach the start
for i := 0; i < len(varBuffer); i += 12 {
var frame []byte
if i > len(varBuffer)-1280 {
// Current frame overlaps end of buffer
frame = append(varBuffer[i:], varBuffer[0:1280+i-len(varBuffer)]...)
} else {
frame = varBuffer[i : i+1280]
}
select {
case out <- frame:
case <-ctx.Done():
return
}
}
// Continue doing this until we are cancelled
}
}
func (s *squeezebox2) setChar(chr []byte, offset int, buffer []byte) {
// Form chars into uint16 (font is 16 pix wide)
var wideChr []uint16
for i := 0; i < len(chr); i += 2 {
wideChr = append(wideChr, uint16(chr[i])<<8|uint16(chr[i+1]))
}
// Set the chr in the framebuffer
i := 4 * offset * 16
for col := 0; col < 16; col++ {
for _, v := range wideChr {
mask := uint16(0b1000000000000000 >> col)
if v&mask > 0 {
squeezeMask := byte(1 << (7 - i%8))
buffer[i/8] |= squeezeMask
}
i++
}
}
}