-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
342 lines (313 loc) · 8.66 KB
/
main.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
// MIT+NoAI License
//
// # Copyright (c) 2024 Uģis Ģērmanis
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights///
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// This code may not be used to train artificial intelligence computer models
// or retrieved by artificial intelligence software or hardware.
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/davecgh/go-spew/spew"
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/av1"
)
const (
sblastMONITOR = "sblast.monitor"
LOGO_PATH = "logo.png"
VERSION = "v0.7.0"
)
//go:embed logo.png
var logobytes []byte
var logsblast = new(bool)
func main() {
// check for dependencies
exes := []string{
"pactl",
"parec",
"ffmpeg",
}
for _, exe := range exes {
if _, err := exec.LookPath(exe); err != nil {
fmt.Fprintln(os.Stderr, "dependency:", err)
os.Exit(1)
}
}
device := flag.String("device", "", "dlna device's friendly name")
source := flag.String("source", "", "audio source (pactl list sources short | cut -f2)")
ip := flag.String("ip", "", "host ip address")
port := flag.Int("port", 9000, "stream port")
chunk := flag.Int("chunk", 1, "chunk size in seconds")
bitrate := flag.Int("bitrate", 320, "audio format bitrate")
format := flag.String("format", "mp3", "stream audio format")
mime := flag.String("mime", "audio/mpeg", "stream mime type")
useaac := flag.Bool("useaac", false, "use aac audio")
useflac := flag.Bool("useflac", false, "use flac audio")
uselpcm := flag.Bool("uselpcm", false, "use lpcm audio")
uselpcmle := flag.Bool("uselpcmle", false, "use lpcm little-endian audio")
usewav := flag.Bool("usewav", false, "use wav audio")
bits := flag.Int("bits", 16, "audio bitdepth")
rate := flag.Int("rate", 44100, "audio sample rate")
channels := flag.Int("channels", 2, "audio channels")
dummy := flag.Bool("dummy", false, "only serve content")
debug := flag.Bool("debug", false, "print debug info")
headers := flag.Bool("headers", false, "print request headers")
logsblast = flag.Bool("log", false, "log parec and ffmpeg stderr")
nochunked := flag.Bool("nochunked", false, "disable chunked tranfer endcoding")
version := flag.Bool("version", false, "show sblast version")
flag.Parse()
if *version {
fmt.Fprintln(os.Stderr, VERSION)
os.Exit(0)
}
var (
sblastSinkID []byte
isPlaying bool
DLNADevice *goupnp.MaybeRootDevice
err error
)
// trap ctrl+c and kill and terminal hang up
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
cleanup := func() {
if sblastSinkID != nil {
log.Println("unloading the sblast sink")
exec.Command("pactl", "unload-module", string(sblastSinkID)).Run()
}
}
go func() {
<-sig
fmt.Println()
cleanup()
if isPlaying && !*dummy {
log.Println("stopping avtransport and exiting")
AVStop(DLNADevice)
}
fmt.Println("terminated...")
os.Exit(0)
}()
if !*dummy {
DLNADevice, err = chooseUPNPDevice(*device)
if err != nil {
fmt.Fprintln(os.Stderr, "upnp:", err)
os.Exit(1)
}
}
if *debug {
spew.Fdump(os.Stderr, DLNADevice)
var location string
urn := detectAVtransport(DLNADevice)
switch {
case urn == av1.URN_AVTransport_1:
clients, err := av1.NewAVTransport1ClientsByURL(DLNADevice.Location)
if err == nil {
location = clients[0].Location.String()
}
spew.Fdump(os.Stderr, clients, err)
case urn == av1.URN_AVTransport_2:
clients, err := av1.NewAVTransport2ClientsByURL(DLNADevice.Location)
if err == nil {
location = clients[0].Location.String()
}
spew.Fdump(os.Stderr, clients, err)
}
get := func() {
if location == "" {
return
}
resp, err := http.Get(location)
if err != nil {
return
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return
}
spew.Fprintln(os.Stderr, string(data))
}
get()
if !*headers {
os.Exit(0)
}
}
if *device == "" {
fmt.Println("----------")
}
sink, err := chooseAudioSource(*source)
if err != nil {
fmt.Fprintln(os.Stderr, "audio:", err)
os.Exit(1)
}
// on-demand handling of sblast sink
if sink == sblastMONITOR {
sblastSink := exec.Command(
"pactl", "load-module", "module-null-sink", "sink_name=sblast",
)
var err error
sblastSinkID, err = sblastSink.Output()
if err != nil {
fmt.Fprintln(os.Stderr, "sblast sink:", err)
os.Exit(1)
}
sblastSinkID = bytes.TrimSpace(sblastSinkID)
}
if *source == "" {
fmt.Println("----------")
}
streamHost, err := chooseStreamIP(*ip)
if err != nil {
fmt.Fprintln(os.Stderr, "network:", err)
cleanup()
os.Exit(1)
}
if *ip == "" {
fmt.Println("----------")
}
log.Printf(
"starting the stream on port %d "+
"(configure your firewall if necessary)",
*port,
)
streamHandler := stream{
sink: sink,
mime: *mime,
format: *format,
bitrate: *bitrate,
chunk: *chunk,
printheaders: *headers,
bitdepth: *bits,
samplerate: *rate,
channels: *channels,
nochunked: *nochunked,
}
switch {
case *useaac:
streamHandler.format = "adts"
streamHandler.mime = "audio/aac"
case *useflac:
streamHandler.format = "flac"
streamHandler.mime = "audio/flac"
streamHandler.bitrate = 0
case *uselpcm:
streamHandler.format = "lpcm"
streamHandler.mime = fmt.Sprintf("audio/L%d;rate=%d;channels=%d", *bits, *rate, *channels)
streamHandler.bitrate = 0
streamHandler.be = true
case *uselpcmle:
streamHandler.format = "lpcm"
streamHandler.mime = fmt.Sprintf("audio/L%d;rate=%d;channels=%d", *bits, *rate, *channels)
streamHandler.bitrate = 0
case *usewav:
streamHandler.format = "wav"
streamHandler.mime = "audio/wav"
streamHandler.bitrate = 0
}
streamHandler.contentfeat = dlnaContentFeatures{
profileName: strings.ToUpper(streamHandler.format),
supportTimeSeek: true,
supportRange: false,
flags: DLNA_ORG_FLAG_DLNA_V15 |
DLNA_ORG_FLAG_CONNECTION_STALL |
DLNA_ORG_FLAG_STREAMING_TRANSFER_MODE |
DLNA_ORG_FLAG_BACKGROUND_TRANSFERT_MODE,
}
streamPath := "stream." + strings.ToLower(streamHandler.format)
mux := http.NewServeMux()
mux.Handle("/"+streamPath, streamHandler)
var logoHandler logo = logobytes
mux.Handle("/"+LOGO_PATH, logoHandler)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
ReadTimeout: -1,
WriteTimeout: -1,
Handler: mux,
}
go func() {
err := httpServer.ListenAndServe()
if err != nil {
fmt.Fprintln(os.Stderr, "server:", err)
cleanup()
os.Exit(1)
}
}()
// detect when the stream server is up
for {
_, err := net.Dial("tcp", fmt.Sprintf(":%d", *port))
if err == nil {
break
}
}
var (
streamURI string
logoURI string
protocol = "http"
)
if !*dummy && *format == "mp3" && detectSonos(DLNADevice) {
protocol = "x-rincon-mp3radio"
}
if streamHost.To4() != nil {
streamURI = fmt.Sprintf("%s://%s:%d/%s",
protocol, streamHost, *port, streamPath)
logoURI = fmt.Sprintf("http://%s:%d/%s",
streamHost, *port, LOGO_PATH)
} else {
var zone string
if streamHost.IsLinkLocalUnicast() {
ifname, err := findInterface(streamHost)
if err == nil {
zone = "%" + ifname
}
}
streamURI = fmt.Sprintf("%s://[%s%s]:%d/%s",
protocol, streamHost, zone, *port, streamPath)
logoURI = fmt.Sprintf("http://[%s%s]:%d/%s",
streamHost, zone, *port, LOGO_PATH)
}
log.Printf("stream URI: %s\n", streamURI)
log.Println("setting avtransport URI and playing")
if !*dummy {
av := avsetup{
device: DLNADevice,
stream: streamHandler,
logoURI: logoURI,
streamURI: streamURI,
}
err = AVSetAndPlay(av)
if err != nil {
fmt.Fprintln(os.Stderr, "transport:", err)
cleanup()
os.Exit(1)
}
}
isPlaying = true
select {}
}