-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
149 lines (126 loc) · 3.85 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
package main
import (
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/Glimesh/rtmp-ingest/pkg/protocols/ftl"
"github.com/Glimesh/rtmp-ingest/pkg/orchestrator"
"github.com/Glimesh/rtmp-ingest/pkg/services/glimesh"
"net/http"
_ "net/http/pprof"
"github.com/evalphobia/logrus_sentry"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
func main() {
log := logrus.New()
log.Level = logrus.DebugLevel
// log.SetReportCaller(true)
log.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
})
var streamManager StreamManager
hostname, err := os.Hostname()
if err != nil {
// How tf
log.Fatal(err)
}
sentryDsn, setupSentry := os.LookupEnv("SENTRY_DSN")
if setupSentry {
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
hook, err := logrus_sentry.NewSentryHook(sentryDsn, []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
})
if err == nil {
log.Hooks.Add(hook)
}
}
_, debugVideo := os.LookupEnv("RTMP_INGEST_DEBUG_VIDEO")
// Should use viper or something in the future
glimeshService := glimesh.New(glimesh.Config{
Address: os.Getenv("RTMP_INGEST_GLIMESH_ADDRESS"),
ClientID: os.Getenv("RTMP_INGEST_GLIMESH_CLIENT_ID"),
ClientSecret: os.Getenv("RTMP_INGEST_GLIMESH_CLIENT_SECRET"),
})
err = glimeshService.Connect()
if err != nil {
log.Fatal(err)
}
orchTransport, err := net.Dial("tcp", os.Getenv("RTMP_INGEST_ORCHESTRATOR_ADDRESS"))
if err != nil {
log.Fatal(err)
}
var orch orchestrator.Client
orch = orchestrator.NewClient(orchestrator.Config{
RegionCode: "global",
Hostname: hostname,
Logger: log.WithFields(logrus.Fields{"app": "orchestrator"}),
Callbacks: orchestrator.Callbacks{
OnStreamRelaying: func(header orchestrator.MessageHeader, message orchestrator.StreamRelayingMessage) {
if message.Context == 1 {
go func() {
attempts := 1
for {
if attempts >= 5 {
log.Errorf("Ran out of orchestrator relay attempts for %d to %s", message.ChannelID, message.TargetHostname)
return
}
log.Infof("Starting relay for %d to %s attempt %d", message.ChannelID, message.TargetHostname, attempts)
// This is a blocking call, once if returns we should conditionally stop the relay
err := streamManager.RelayMedia(message.ChannelID, message.TargetHostname, ftl.DefaultPort, message.StreamKey)
if err == nil {
// Successful exit, just return
log.Infof("Ended relay for %d to %s", message.ChannelID, message.TargetHostname)
return
}
attempts++
log.Infof("Errored relay for %d to %s", message.ChannelID, message.TargetHostname)
log.Error(err)
time.Sleep(time.Second)
}
}()
} else {
log.Infof("Removing relay for %d to %s per orchestrator", message.ChannelID, message.TargetHostname)
err := streamManager.StopRelay(message.ChannelID, message.TargetHostname)
if err != nil {
log.Error(err)
}
}
orch.SendResponseMessage(orchestrator.TypeStreamRelaying, header.ID, []byte{})
},
},
})
if err := orch.Connect(orchTransport); err != nil {
log.Fatal(err)
}
streamManager = NewStreamManager(orch, glimeshService)
closeHandler(orch, streamManager)
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// Blocking call to start the RTMP server
NewRTMPServer(streamManager, log.WithFields(logrus.Fields{"app": "rtmp"}), debugVideo)
}
func closeHandler(orch orchestrator.Client, streamManager StreamManager) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-c
// Stop all streams we're handling
for k := range streamManager.streams {
streamManager.StopStream(k)
}
// Tell orchestrator goodbye for now
orch.Close()
os.Exit(0)
}()
}