-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicycat.go
496 lines (390 loc) · 10.7 KB
/
icycat.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
package main
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/puellanivis/breton/lib/files"
"github.com/puellanivis/breton/lib/files/httpfiles"
_ "github.com/puellanivis/breton/lib/files/plugins"
"github.com/puellanivis/breton/lib/files/socketfiles"
"github.com/puellanivis/breton/lib/glog"
flag "github.com/puellanivis/breton/lib/gnuflag"
"github.com/puellanivis/breton/lib/io/bufpipe"
"github.com/puellanivis/breton/lib/metrics"
_ "github.com/puellanivis/breton/lib/metrics/http"
"github.com/puellanivis/breton/lib/mpeg/framer"
"github.com/puellanivis/breton/lib/mpeg/ts"
"github.com/puellanivis/breton/lib/mpeg/ts/dvb"
"github.com/puellanivis/breton/lib/mpeg/ts/psi"
"github.com/puellanivis/breton/lib/os/process"
)
// Version information ready for build-time injection.
var (
Version = "v2.0.0"
Buildstamp = "dev"
)
// Flags contains all of the flags defined for the application.
var Flags struct {
Output string `flag:",short=o" desc:"Specifies which file to write the output to"`
UserAgent string `flag:",default=icycat/2.0" desc:"Which User-Agent string to use"`
Quiet bool `flag:",short=q" desc:"If set, supresses output from subprocesses."`
// --packet-size defaults to 1316, which is 1500 - (1500 mod 188)
// Where 1500 is the typical ethernet MTU, and 188 is the mpegts packet size.
PacketSize int `flag:",default=1316" desc:"If outputing to udp, default to using this packet size."`
Timeout time.Duration `flag:",default=5s" desc:"The timeout between rapid copy errors."`
Metrics bool `desc:"If set, publish metrics to the given metrics-port or metrics-addr."`
MetricsPort int `desc:"Which port to publish metrics with. (default auto-assign)"`
MetricsAddress string `desc:"Which local address to listen on; overrides metrics-port flag."`
}
func init() {
flag.Struct("", &Flags)
}
var (
bwLifetime = metrics.Gauge("bandwidth_lifetime_bps", "bandwidth of the copy to output process (bits/second)")
bwRunning = metrics.Gauge("bandwidth_running_bps", "bandwidth of the copy to output process (bits/second)")
)
type headerer interface {
Header() (http.Header, error)
}
func printIcyHeaders(h headerer) (name string) {
header, err := h.Header()
if err != nil {
glog.Errorf("couldn’t get headers: %s", err)
return ""
}
var headers []string
for k := range header {
key := strings.ToUpper(k)
if strings.HasPrefix(key, "ICY-") {
headers = append(headers, k)
}
}
sort.Strings(headers)
for _, key := range headers {
val := header[key]
var v interface{} = val
if len(val) == 1 {
v = val[0]
}
glog.Infof("%s: %q\n", key, v)
}
return header.Get("Icy-Name")
}
var (
stderr = os.Stderr
mux *ts.Mux
)
type discontinuityMarker interface {
Discontinuity()
}
func openOutput(ctx context.Context, filename string) (io.WriteCloser, func(), error) {
discontinuity := func() {}
if !strings.HasPrefix(filename, "udp:") && !strings.HasPrefix(filename, "mpegts:") {
f, err := files.Create(ctx, filename)
if err != nil {
return nil, nil, err
}
glog.Infof("output: %s", f.Name())
return f, discontinuity, nil
}
filename = strings.TrimPrefix(filename, "mpegts:")
uri, err := url.Parse(filename)
if err != nil {
return nil, nil, err
}
var opts []files.Option
if uri.Scheme == "udp" {
// Default packet size: what the flag --packet-size is.
pktSize := Flags.PacketSize
q := uri.Query()
if urlPktSize := q.Get(socketfiles.FieldPacketSize); urlPktSize != "" {
// If the output URL has a urlPktSize value, override the default.
sz, err := strconv.ParseInt(urlPktSize, 0, strconv.IntSize)
if err != nil {
return nil, nil, errors.Errorf("bad %s value: %s: %+v", socketfiles.FieldPacketSize, urlPktSize, err)
}
pktSize = int(sz)
}
// Our packet size needs to be an integer multiple of the mpegts packet size.
pktSize -= (pktSize % ts.PacketSize)
// Our packet size needs to be at least the mpegts packet size.
if pktSize <= 0 {
pktSize = ts.PacketSize
}
q.Set(socketfiles.FieldPacketSize, fmt.Sprint(pktSize))
uri.RawQuery = q.Encode()
filename = uri.String()
opts = append(opts, socketfiles.WithIgnoreErrors(true))
}
f, err := files.Create(ctx, filename, opts...)
if err != nil {
return nil, nil, err
}
glog.Infof("output: %s", f.Name())
mux = ts.NewMux(f)
var wg sync.WaitGroup
wr, err := mux.Writer(ctx, 1, ts.ProgramTypeAudio)
if err != nil {
f.Close()
return nil, nil, err
}
if s, ok := wr.(discontinuityMarker); ok {
discontinuity = s.Discontinuity
}
pipe := bufpipe.New(ctx)
s := framer.NewScanner(pipe)
wg.Add(1)
go func() {
defer func() {
if err := wr.Close(); err != nil {
glog.Errorf("mux.Writer.Close: %+v", err)
}
wg.Done()
}()
for s.Scan() {
b := s.Bytes()
n, err := wr.Write(b)
if err != nil {
glog.Errorf("mux.Writer.Write: %+v", err)
return
}
if n < len(b) {
glog.Errorf("mux.Writer.Write: %+v", io.ErrShortWrite)
}
}
if err := s.Err(); err != nil {
glog.Errorf("framer.Scanner: %s: %+v", filename, err)
}
}()
out := newTriggerWriter(pipe)
go func() {
<-out.Trigger()
for err := range mux.Serve(ctx) {
glog.Fatalf("mux.Serve: %+v", err)
}
}()
go func() {
wg.Wait()
for err := range mux.Close() {
glog.Errorf("mux.Close: %+v", err)
}
}()
return out, discontinuity, nil
}
// DVBService sets the dvb.ServiceDescriptor to be used by the muxer.
func DVBService(desc *dvb.ServiceDescriptor) {
if mux != nil {
service := &dvb.Service{
ID: 0x0001,
}
service.Descriptors = append(service.Descriptors, desc)
sdt := &dvb.ServiceDescriptorTable{
Syntax: &psi.SectionSyntax{
TableIDExtension: 1,
Current: true,
},
OriginalNetworkID: 0xFF01,
Services: []*dvb.Service{service},
}
mux.SetDVBSDT(sdt)
switch {
case glog.V(5) == true:
glog.Infof("dvb.sdt: %v", sdt)
case glog.V(2) == true:
glog.Infof("DVB Service Description: %v", desc)
}
}
}
// ICECASTReader returns an io.Reader from the given filename that reads an ICECAST stream.
func ICECASTReader(ctx context.Context, filename string, discontinuity func()) (io.Reader, error) {
reopen := func() (files.Reader, error) {
discontinuity()
// BUG: if you attempt to load a SHOUTcast 1.9.x address,
// it will return an HTTP version field of "ICY" not "HTTP/x.y",
// and Go’s net/http library will barf and return an error.
// There is no way at this time to tell it to treat said HTTP version as "HTTP/1.0"
// without possibly hijacking the stream through a text transform that looks to see if it
// starts with ICY, and replaces that with HTTP/1.0…
//
// BETTER: net/http should allow one to say "ICY" maps to HTTP/1.0,
// it already has short-circuits for "HTTP/1.0" and "HTTP/1.1" after all.
f, err := files.Open(ctx, filename)
if err != nil {
return nil, err
}
if glog.V(2) && f.Name() != filename {
glog.Infof("catting %s", f.Name())
}
return f, err
}
f, err := reopen()
if err != nil {
return nil, err
}
if h, ok := f.(headerer); ok {
name := printIcyHeaders(h)
if name == "" {
name = f.Name()
}
ServiceDesc := &dvb.ServiceDescriptor{
Type: dvb.ServiceTypeRadio,
Provider: "icycat",
Name: name,
}
DVBService(ServiceDesc)
}
opts := []files.CopyOption{
files.WithWatchdogTimeout(Flags.Timeout),
}
pipe := bufpipe.New(ctx)
go func() {
defer pipe.Close()
for {
start := time.Now()
wait := time.After(Flags.Timeout)
if glog.V(1) {
glog.Infof("copying to buffer: %s", f.Name())
}
n, err := files.Copy(ctx, pipe, f, opts...)
// We reopen in every loop, so after files.Copy, we have to Close it.
if err2 := f.Close(); err == nil {
err = err2
}
if err != nil {
glog.Error(err)
if n > 0 {
glog.Errorf("%d bytes copied in %v", n, time.Since(start))
}
} else if glog.V(2) {
glog.Infof("%d bytes copied in %v", n, time.Since(start))
}
select {
case <-wait:
case <-ctx.Done():
return
}
f, err = reopen()
if err != nil {
glog.Errorf("%+v", err)
}
}
}()
return pipe, nil
}
func main() {
ctx, finish := process.Init("icycat", Version, Buildstamp)
defer finish()
ctx = httpfiles.WithUserAgent(ctx, Flags.UserAgent)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
process.Exit(1)
}
if Flags.Quiet {
stderr = nil
}
if glog.V(2) {
if err := flag.Set("stderrthreshold", "INFO"); err != nil {
glog.Error(err)
}
}
if Flags.MetricsPort != 0 || Flags.MetricsAddress != "" {
Flags.Metrics = true
}
if Flags.Metrics {
go func() {
addr := Flags.MetricsAddress
if addr == "" {
addr = fmt.Sprintf(":%d", Flags.MetricsPort)
}
l, err := net.Listen("tcp", addr)
if err != nil {
glog.Fatal("net.Listen: ", err)
}
msg := fmt.Sprintf("metrics available at: http://%s/metrics", l.Addr())
fmt.Fprintln(os.Stderr, msg)
glog.Info(msg)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/metrics", http.StatusMovedPermanently)
})
srv := &http.Server{}
go func() {
if err := srv.Serve(l); err != nil {
if err != http.ErrServerClosed {
glog.Fatal("http.Server.Serve: ", err)
}
}
}()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
glog.Error("http.Server.Shutdown: ", err)
}
l.Close()
}()
}
out, discontinuity, err := openOutput(ctx, Flags.Output)
if err != nil {
glog.Fatal(err)
}
defer func() {
if err := out.Close(); err != nil {
glog.Error(err)
}
}()
arg, args := args[0], args[1:]
var opts []files.CopyOption
if Flags.Metrics {
opts = append(opts,
files.WithMetricsScale(8), // bits instead of bytes
files.WithBandwidthMetrics(bwLifetime),
files.WithIntervalBandwidthMetrics(bwRunning, 10, 1*time.Second),
)
}
in, err := ICECASTReader(ctx, arg, discontinuity)
if err != nil {
glog.Fatalf("ICECASTReader: %+v", err)
}
for {
select {
case <-ctx.Done():
glog.Error(ctx.Err())
return
default:
}
start := time.Now()
wait := time.After(Flags.Timeout)
n, err := files.Copy(ctx, out, in, opts...)
if err != nil && err != io.EOF {
glog.Error(err)
if n > 0 {
glog.Errorf("%d bytes copied in %v", n, time.Since(start))
}
} else if glog.V(2) {
glog.Infof("%d bytes copied in %v", n, time.Since(start))
}
if err == io.EOF {
break
}
// minimum Flags.Timeout wait.
select {
case <-wait:
case <-ctx.Done():
glog.Error(ctx.Err())
return
}
}
}