forked from voc/srtrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (104 loc) · 2.78 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
package main
import (
"context"
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/haivision/srtgo"
"github.com/voc/srtrelay/api"
"github.com/voc/srtrelay/config"
"github.com/voc/srtrelay/relay"
"github.com/voc/srtrelay/srt"
)
func handleSignal(ctx context.Context, cancel context.CancelFunc) {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM)
go func() {
for {
select {
case <-ctx.Done():
return
case s := <-c:
log.Println("caught signal", s)
if s == syscall.SIGHUP {
continue
}
cancel()
}
}
}()
}
func main() {
// allow specifying config path
configFlags := flag.NewFlagSet("config", flag.ContinueOnError)
configFlags.SetOutput(ioutil.Discard)
configPath := configFlags.String("config", "config.toml", "")
configFlags.Parse(os.Args[1:])
// parse config
conf, err := config.Parse([]string{*configPath, "/etc/srtrelay/config.toml"})
if err != nil {
log.Fatal(err)
}
// flag just for usage
flag.String("config", "config.toml", "path to config file")
// actual flags, use config as default and storage
var addresses string
flag.StringVar(&addresses, "addresses", strings.Join(conf.App.Addresses, ","), "relay bind addresses, separated by commata")
flag.UintVar(&conf.App.Latency, "latency", conf.App.Latency, "srt protocol latency in ms")
flag.UintVar(&conf.App.Buffersize, "buffersize", conf.App.Buffersize,
`relay buffer size in bytes, determines maximum delay of a client`)
flag.Parse()
conf.App.Addresses = strings.Split(addresses, ",")
auth, err := config.GetAuthenticator(conf.Auth)
if err != nil {
log.Println(err)
}
serverConfig := srt.Config{
Server: srt.ServerConfig{
Addresses: conf.App.Addresses,
PublicAddress: conf.App.PublicAddress,
Latency: conf.App.Latency,
LossMaxTTL: conf.App.LossMaxTTL,
SyncClients: conf.App.SyncClients,
Auth: auth,
},
Relay: relay.RelayConfig{
Buffersize: conf.App.Buffersize, // 1s @ 3Mbits/
},
}
// setup graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
handleSignal(ctx, cancel)
// create server
srtgo.InitSRT()
srtServer := srt.NewServer(&serverConfig)
err = srtServer.Listen(ctx)
if err != nil {
log.Fatal(err)
}
var apiServer *api.Server
if conf.API.Enabled {
apiServer = api.NewServer(conf.API, srtServer)
err := apiServer.Listen(ctx)
if err != nil {
log.Fatal(err)
}
log.Printf("API listening on %s\n", conf.API.Address)
}
// Wait for graceful shutdown
srtServer.Wait()
if apiServer != nil {
apiServer.Wait()
}
srtgo.CleanupSRT()
}