-
Notifications
You must be signed in to change notification settings - Fork 0
/
slimproto.go
584 lines (513 loc) · 15.9 KB
/
slimproto.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
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
/*
* (c) 2012 Bart Lauret
*
* This file is part of slimgo.
*
* slimgo 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
* (at your option) any later version.
*
* slimgo 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 slimgo. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/binary"
"errors"
"fmt"
"github.com/terual/alsa-go"
"log"
"net"
"strconv"
"time"
// "os"
)
// Try a discovery on slimproto address and port
func slimprotoDisco() (addr net.IP, port int) {
type discoSend struct {
JSON [11]byte
}
type discoResponse struct {
Type [4]byte
Lenght uint8
}
conn, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.IPv4bcast,
Port: 3483,
})
if err != nil {
log.Fatalf("Fatal error: %s", err.Error())
}
defer conn.Close()
//conn.SetDeadline(time.Time(1e9))
// send a packet
msg := discoSend{}
copy(msg.JSON[:], "eNAME\x00JSON\x00")
//log.Println(msg)
err = binary.Write(conn, binary.BigEndian, &msg)
if err != nil {
log.Fatalf("Fatal error: %s", err.Error())
}
// receive the response
conn, err = net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4zero,
Port: 3483,
})
//conn.SetDeadline(time.Time(1e9))
if err != nil {
if e, ok := err.(*net.OpError); ok {
log.Println("Error", e)
//if e.Err.(os.Errno) == 98 {
// Presumably is it already in use by LMS on same machine
log.Println("Discovery failed due to the discovery port already in use, so we presume that the server is running on this machine. Please supply command line parameters if otherwise.")
return net.IP{127, 0, 0, 1}, 3483
//}
}
}
remoteAddr := new(net.UDPAddr)
for i := 0; i < 5; i++ {
data := make([]byte, 1)
_, remoteAddr, err = conn.ReadFromUDP(data)
if string(data[:]) == "E" {
break
} else if i == 4 {
log.Fatalln("No servers found using discovery, supply IP and port with command line arguments.")
}
}
var response discoResponse
err = binary.Read(conn, binary.BigEndian, &response)
checkError(err)
if *debug {
log.Printf("Response: %s %s:%s\n", remoteAddr, response.Type, response.Lenght)
}
// Parse response
//addr = net.IPv4(response.IPaddr[0],response.IPaddr[1],response.IPaddr[2],response.IPaddr[3])
//port = int(byte(response.Port[0])+byte(response.Port[1]))
return
}
// Connect to slimproto
func slimprotoConnect(addr net.IP, port int) {
sbsAddr := new(net.TCPAddr)
sbsAddr.IP = addr
sbsAddr.Port = port
var err error
slimproto.Conn, err = net.DialTCP("tcp", nil, sbsAddr)
checkError(err)
//slimproto.Conn.SetDeadline(time.Time(10e9))
if *debug {
log.Println("Connected to slimproto")
}
return
}
type header struct {
Lenght uint16
CommandHeader [4]byte
}
type strm struct {
Command uint8
Autostart uint8
Formatbyte uint8
Pcmsamplesize uint8
Pcmsamplerate uint8
Pcmchannels uint8
Pcmendian uint8
Threshold uint8
Spdif_enable uint8
Trans_period uint8
Trans_type uint8
Flags uint8
Output_threshold uint8
RESERVED uint8
Replay_gain uint32
Server_port uint16
Server_ip [4]byte
}
type audg struct {
Old_left uint32 // 0-128
Old_right uint32 // 0-128
Dvc uint8
Preamp uint8
New_left uint32 // 0-65536
New_right uint32 // 0-65536
SequenceNumber uint32
}
// Receive from slimproto and act upon
func slimprotoRecv() (errProto error) {
var headerResponse header
errProto = binary.Read(slimproto.Conn, binary.BigEndian, &headerResponse)
if errProto == nil {
// convert [4]uint8 to string
var cmdHdr = string(headerResponse.CommandHeader[:])
switch cmdHdr {
case "strm":
// read into strm struct
var streamResponse strm
errProto = binary.Read(slimproto.Conn, binary.BigEndian, &streamResponse)
if *debug {
log.Printf("[Recv strm] Command: %s, Autostart: %s, Formatbyte: %s, Pcmsamplesize: %s, Pcmsamplerate: %s, Pcmchannels: %s, Pcmendian: %s\n",
string(streamResponse.Command), string(streamResponse.Autostart), string(streamResponse.Formatbyte),
string(streamResponse.Pcmsamplesize), string(streamResponse.Pcmsamplerate),
string(streamResponse.Pcmchannels), string(streamResponse.Pcmendian))
}
switch string(streamResponse.Command) {
case "t":
_ = slimprotoSend(slimproto.Conn, streamResponse.Replay_gain, "STMt")
case "s":
slimaudio.State = "PLAY"
_ = slimprotoSend(slimproto.Conn, 0, "STMc")
case "p":
slimaudio.Handle.Pause()
slimaudio.State = "PAUSE"
if streamResponse.Replay_gain == 0 {
_ = slimprotoSend(slimproto.Conn, 0, "STMp")
} else {
// if non-zero, an interval (ms) to pause for and then automatically resume
// no STMp & STMr status messages are sent in this case.
time.Sleep(time.Duration(int64(streamResponse.Replay_gain) * 1e6))
slimaudio.Handle.Unpause()
// if slimaudio.State == "PAUSED" we should send to
// slimaudioChannel to wake the goroutine (unlikely)
if slimaudio.State == "PAUSED" {
slimaudioChannel <- 1
}
}
case "u":
if slimaudio.State == "PAUSED" || slimaudio.State == "PAUSE" {
if streamResponse.Replay_gain != 0 {
// if non-zero, the player-specific internal timestamp (ms) at which to unpause
if *debug {
log.Printf("Waiting for jiffie %v, now: %v", streamResponse.Replay_gain, jiffies())
}
for jiffies() >= streamResponse.Replay_gain {
time.Sleep(1e6) //1ms
}
}
slimaudio.Handle.Unpause()
// if slimaudio.State == "PAUSED" we should send to
// slimaudioChannel to wake the goroutine
if slimaudio.State == "PAUSED" {
slimaudioChannel <- 1
}
slimaudio.State = "PLAYING"
_ = slimprotoSend(slimproto.Conn, 0, "STMr")
}
case "q":
slimaudio.Handle.Pause()
err := slimaudio.Handle.Drop()
if err != nil {
log.Printf("ALSA drop failed. %s", err)
}
_ = slimbuffer.Reader.Flush()
slimaudio.Handle.SampleFormat = alsa.SampleFormatUnknown
slimaudio.Handle.SampleRate = 0
slimaudio.Handle.Channels = 0
slimaudio.State = "STOPPED"
_ = slimprotoSend(slimproto.Conn, 0, "STMf")
case "f":
//flush
slimaudio.Handle.Pause()
err := slimaudio.Handle.Drop()
if err != nil {
log.Printf("ALSA drop failed. %s", err)
}
_ = slimbuffer.Reader.Flush()
slimaudio.Handle.SampleFormat = alsa.SampleFormatUnknown
slimaudio.Handle.SampleRate = 0
slimaudio.Handle.Channels = 0
_ = slimprotoSend(slimproto.Conn, 0, "STMf")
case "a":
//skip-ahead
// replay_gain field: if non-zero, an interval (ms) to skip over (not play).
framesToSkip := int(streamResponse.Replay_gain) * slimaudio.Handle.SampleRate / 1000
if *debug {
log.Printf("Skipping %v frames, %v ms", framesToSkip, streamResponse.Replay_gain)
}
framesSkipped, err := slimaudio.Handle.SkipFrames(framesToSkip)
if *debug {
log.Printf("Skipped %v frames, err: %s", framesSkipped, err)
}
default:
if *debug {
log.Println("Did not recognise strm message with cmd: %s", string(streamResponse.Command))
}
}
if *debug {
log.Printf("slimaudio.State: %s\n", slimaudio.State)
}
// check if a http header is sent
if headerResponse.Lenght > 28 {
// Check flags
/*switch streamResponse.Flags {
case 64: //0x40
// stream without restarting decoder
slimaudio.NewTrack = false
default:
slimaudio.NewTrack = true
log.Printf("Flag: %v", streamResponse.Flags)
}*/
slimaudio.NewTrack = true
httpHeader := make([]byte, headerResponse.Lenght-28)
_, errProto = slimproto.Conn.Read(httpHeader[0:])
if string(streamResponse.Formatbyte) == "p" {
port := strconv.Itoa(int(streamResponse.Server_port))
go slimbufferOpen(httpHeader,
slimproto.Addr.String(),
port,
streamResponse.Pcmsamplesize,
streamResponse.Pcmsamplerate,
streamResponse.Pcmchannels,
streamResponse.Pcmendian)
_ = slimprotoSend(slimproto.Conn, 0, "STMh")
slimaudio.State = "PLAYING"
} else {
if *debug {
log.Printf("Format not supported, Formatbyte: %s", string(streamResponse.Formatbyte))
}
_ = slimprotoSend(slimproto.Conn, 0, "STMn")
}
}
case "audg":
// -------- -------- - - --- ---- --- ---- -------
// [0 0 0 46 0 0 0 46 1 255 0 0 15 0 0 0 15 0 0 0 0 0]
if headerResponse.Lenght == 26 {
// read into audg struct
var audioGainResponse audg
errProto = binary.Read(slimproto.Conn, binary.BigEndian, &audioGainResponse)
if *debug {
log.Printf("audioGainResponse, Old_left: %v, Old_right: %v, New_left: %v, New_right: %v",
audioGainResponse.Old_left, audioGainResponse.Old_right,
audioGainResponse.New_left, audioGainResponse.New_right)
}
} else {
body := make([]byte, headerResponse.Lenght-4)
_, errProto = slimproto.Conn.Read(body[0:])
}
case "stat":
// Request a STAT update from the player
body := make([]byte, headerResponse.Lenght-4)
_, errProto = slimproto.Conn.Read(body[0:])
log.Println("stat:", body)
case "serv":
// Tells the client to switch to another server.
body := make([]byte, headerResponse.Lenght-4)
_, errProto = slimproto.Conn.Read(body[0:])
log.Println("serv:", body)
default:
// Rest is ignored
body := make([]byte, headerResponse.Lenght-4)
_, errProto = slimproto.Conn.Read(body[0:])
}
}
return
}
type STAT struct {
Operation [4]byte
Length uint32
EventCode [4]byte
CRLF uint8
MASInit uint8
MASMode uint8
BufferSize uint32
BufferFullness uint32
BytesReceived uint64
WirelessStrength uint16
Jiffies uint32
OutputBufferSize uint32
OutputBufferFullness uint32
ElapsedSeconds uint32
Voltage uint16
ElapsedMillis uint32
Timestamp uint32
ErrorCode uint16
}
/*
u32 Event Code (a 4 byte string)
u8 Number of consecutive CRLF recieved while parsing headers
u8 MAS Initalized - 'm' or 'p'
u8 MAS Mode - serdes mode?
u32 buffer size - in bytes, of the player's (network/stream) buffer
u32 fullness - data bytes in the player's (network/stream) buffer
u64 Bytes Recieved
u16 Wireless Signal Strength (0-100 - Larger values mean hardwired)
u32 jiffies - a timestamp from the player (@1kHz)
u32 output buffer size - the decoded audio data buffer size
u32 output buffer fullness - bytes in the decoded audio data buffer
u32 elapsed seconds - of the current stream
u16 voltage
u32 elapsed milliseconds - of the current stream
u32 server timestamp - reflected from an strm-t command
u16 error code - used with STMn */
// Send STAT message
func slimprotoSend(conn *net.TCPConn, timestamp uint32, eventcode string) (err error) {
var elapsedSeconds int
var elapsedMillis uint64
var elapsedFrames int
if slimaudio.FramesWritten > 0 && slimaudio.Handle.SampleRate > 0 {
elapsedFrames, err = slimaudioElapsedFrames()
if err == nil {
elapsedMillis = (uint64(elapsedFrames) * 1000) / uint64(slimaudio.Handle.SampleRate)
elapsedSeconds = int(elapsedMillis / 1000)
elapsedMillis = elapsedMillis % 1000
}
if *debug {
log.Printf("frames written: %v, elapsedFrames: %v, ElapsedSeconds: %v, ElapsedMillis: %v",
slimaudio.FramesWritten, elapsedFrames, elapsedSeconds, elapsedMillis)
}
} else {
elapsedSeconds = 0
elapsedMillis = 0
}
var BufferFullness int
var BufferSize int
if slimbuffer.Init == true {
BufferFullness = slimbuffer.Reader.Buffered()
BufferSize = slimbuffer.Reader.Size()
} else {
BufferFullness = 0
BufferSize = 0
}
if *debug {
log.Printf("BufferFullness: %v, BufferSize: %v", BufferFullness, BufferSize)
}
msg := STAT{Length: 53,
Timestamp: timestamp,
WirelessStrength: 65534,
Jiffies: jiffies(),
OutputBufferSize: uint32(BufferSize),
OutputBufferFullness: uint32(BufferFullness),
ElapsedSeconds: uint32(elapsedSeconds),
ElapsedMillis: uint32(elapsedMillis)}
copy(msg.Operation[:], "STAT")
copy(msg.EventCode[:], eventcode)
err = binary.Write(conn, binary.BigEndian, &msg)
if *debug {
log.Printf("[Sent %s]", eventcode)
}
return
}
// Close slimproto
func slimprotoClose() {
err := slimproto.Conn.Close()
checkError(err)
if *debug {
log.Println("Connection to slimproto closed")
}
}
// Send a HELO message
func slimprotoHello(macAddr [6]uint8, maxRate int) (err error) {
capabilities := "model=squeezeplay,modelName=SlimGo,pcm,MaxSampleRate=" + strconv.Itoa(maxRate)
type HELO58 struct { //58
Operation [4]byte
Length uint32
DeviceID uint8
Revision uint8
MAC [6]uint8
UUID [16]uint8
WLanChannelList [2]uint8
Bytes_recv [8]uint8
Language [2]uint8
Capabilities [58]byte
}
type HELO59 struct { //59
Operation [4]byte
Length uint32
DeviceID uint8
Revision uint8
MAC [6]uint8
UUID [16]uint8
WLanChannelList [2]uint8
Bytes_recv [8]uint8
Language [2]uint8
Capabilities [59]byte
}
// send a packet
switch len(capabilities) {
case 58:
msg := HELO58{Length: 36 + uint32(len(capabilities)), DeviceID: 12, Revision: 255, MAC: macAddr}
copy(msg.Operation[:], "HELO")
copy(msg.Capabilities[:], capabilities)
err = binary.Write(slimproto.Conn, binary.BigEndian, &msg)
case 59:
msg := HELO59{Length: 36 + uint32(len(capabilities)), DeviceID: 12, Revision: 255, MAC: macAddr}
copy(msg.Operation[:], "HELO")
copy(msg.Capabilities[:], capabilities)
err = binary.Write(slimproto.Conn, binary.BigEndian, &msg)
default:
return errors.New(fmt.Sprintf("Samplerate has no valid len: %v", maxRate))
}
return
}
// Send a BYE! message
func slimprotoBye() (err error) {
type BYE struct {
Operation [4]byte
Length uint32
Upgrade uint8
}
// send a packet
msg := BYE{Length: 1, Upgrade: 0}
copy(msg.Operation[:], "BYE!")
err = binary.Write(slimproto.Conn, binary.BigEndian, &msg)
if *debug {
log.Printf("Sent BYE! msg: %v", msg)
}
return
}
// Check for errors in err
func checkError(err error) {
if err != nil {
log.Println("reader %v\n", err)
}
/*
if err != nil {
// print error string e.g.
// "read tcp example.com:80: resource temporarily unavailable"
fmt.Printf("reader %v\n", err)
// print type of the error, e.g. "*net.OpError"
fmt.Printf("%T\n", err)
if err == os.EINVAL {
// socket is not valid or already closed
fmt.Println("EINVAL");
}
if err == os.EOF {
// remote peer closed socket
fmt.Println("EOF");
}
// matching rest of the codes needs typecasting, errno is
// wrapped on OpError
if e, ok := err.(*net.OpError); ok {
// print wrapped error string e.g.
// "os.Errno : resource temporarily unavailable"
fmt.Printf("%T : %v\n", e.Error, e.Error)
if e.Timeout() {
// is this timeout error?
fmt.Println("TIMEOUT")
}
if e.Temporary() {
// is this temporary error? True on timeout,
// socket interrupts or when buffer is full
fmt.Println("TEMPORARY")
}
// specific granular error codes in case we're interested
switch e.Error {
case os.EAGAIN:
// timeout
fmt.Println("EAGAIN")
case os.EPIPE:
// broken pipe (e.g. on connection reset)
fmt.Println("EPIPE")
default:
// just write raw errno code, can be platform specific
// (see syscall for definitions)
fmt.Printf("%d\n", int64(e.Error.(os.Errno)))
}
}
}
*/
}