/
stream.go
276 lines (222 loc) · 6.77 KB
/
stream.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
package main
import (
"errors"
"fmt"
"net"
"github.com/Glimesh/rtmp-ingest/pkg/orchestrator"
"github.com/Glimesh/rtmp-ingest/pkg/protocols/ftl"
"github.com/Glimesh/rtmp-ingest/pkg/services"
"github.com/pion/rtp/v2"
)
type Stream struct {
// authenticated is set after the stream has successfully authed with a remote service
authenticated bool
// mediaStarted is set after media bytes have come in from the client
mediaStarted bool
relays map[string]*StreamRelay
// edgeWriter *edgeWriter
channelID ftl.ChannelID
streamID ftl.StreamID
streamKey []byte
}
type StreamRelay struct {
targetHostname string
ftlControlPort int
ftlMediaPort int
ftlClient *ftl.Conn
ftlMedia *net.UDPConn
ftlMediaAddr *net.UDPAddr
}
type StreamManager struct {
service services.Service
orchestrator orchestrator.Client
streams map[ftl.ChannelID]*Stream
}
func NewStreamManager(orchestrator orchestrator.Client, service services.Service) StreamManager {
return StreamManager{
orchestrator: orchestrator,
service: service,
streams: make(map[ftl.ChannelID]*Stream),
}
}
func (mgr *StreamManager) NewStream(channelID ftl.ChannelID) error {
// edgeWriter := NewEdgeWriter()
stream := &Stream{
authenticated: false,
mediaStarted: false,
channelID: channelID,
relays: make(map[string]*StreamRelay),
// edgeWriter: edgeWriter,
}
if _, exists := mgr.streams[channelID]; exists {
return errors.New("stream already exists in stream manager state")
}
mgr.streams[channelID] = stream
return nil
}
func (mgr *StreamManager) Authenticate(channelID ftl.ChannelID, streamKey []byte) error {
stream, err := mgr.GetStream(channelID)
if err != nil {
return err
}
actualKey, err := mgr.service.GetHmacKey(channelID)
if err != nil {
return err
}
if string(streamKey) != string(actualKey) {
return errors.New("incorrect stream key")
}
stream.authenticated = true
stream.streamKey = streamKey
return nil
}
func (mgr *StreamManager) StartStream(channelID ftl.ChannelID) (*Stream, error) {
stream, err := mgr.GetStream(channelID)
if err != nil {
return &Stream{}, err
}
streamID, err := mgr.service.StartStream(channelID)
if err != nil {
return &Stream{}, err
}
stream.streamID = streamID
err = mgr.orchestrator.SendStreamPublishing(orchestrator.StreamPublishingMessage{
Context: 1,
ChannelID: stream.channelID,
StreamID: stream.streamID,
})
if err != nil {
return &Stream{}, err
}
return stream, err
}
func (mgr *StreamManager) StopStream(channelID ftl.ChannelID) (err error) {
stream, err := mgr.GetStream(channelID)
if err != nil {
return err
}
fmt.Printf("stream.relays: %#v\n", stream.relays)
// Close all of our FTL relay connections
for name, relay := range stream.relays {
fmt.Printf("Closing %s\n", name)
if err := relay.close(); err != nil {
return err
}
}
// Tell the orchestrator the stream has ended
if err := mgr.orchestrator.SendStreamPublishing(orchestrator.StreamPublishingMessage{
Context: 0,
ChannelID: stream.channelID,
StreamID: stream.streamID,
}); err != nil {
return err
}
// Tell the service the stream has ended
if err := mgr.service.EndStream(stream.streamID); err != nil {
return err
}
return nil
}
func (mgr *StreamManager) RelayMedia(channelID ftl.ChannelID, targetHostname string, ftlPort int, streamKey []byte) error {
stream, err := mgr.GetStream(channelID)
if err != nil {
return err
}
if _, exists := stream.relays[targetHostname]; exists {
return errors.New("already sending media packets to this relay")
}
// We should likely just write the last keyframe packet we have
// in order to get the relay started
// Actual this needs to be sizable chunk of RTP so ignore for now
// Request to relay
// func(mediaConn net.Conn) error {
// if _, exists := stream.relays[targetHostname]; !exists {
// return nil
// }
// stream.rtpWriter.Remove(mediaConn)
// fmt.Println("Removing RTP writer")
// return nil
// }
ftlClient, err := ftl.Dial(targetHostname, ftlPort, channelID, streamKey)
if err != nil {
return err
}
stream.relays[targetHostname] = &StreamRelay{
targetHostname: targetHostname,
ftlControlPort: ftlPort,
ftlMediaPort: ftlClient.AssignedMediaPort,
ftlClient: ftlClient,
ftlMedia: ftlClient.MediaConn,
ftlMediaAddr: ftlClient.MediaAddr,
}
// setting the rtpWriter is enough to get a stream of packets coming through
// ftlClient.MediaConn.SetDeadline(time.Now().Add(time.Second * 5))
// stream.rtpWriter.Append(ftlClient.MediaConn)
// stream.edgeWriter.new(targetHostname, ftlClient.MediaConn)
// Heartbeat (blocking thread until we get disconnected)
err = ftlClient.Heartbeat()
fmt.Println("Post heartbeat")
mgr.cleanupRelay(channelID, targetHostname)
fmt.Println("Post cleanup")
return err
}
func (mgr *StreamManager) StopRelay(channelID ftl.ChannelID, targetHostname string) error {
return mgr.cleanupRelay(channelID, targetHostname)
}
func (mgr *StreamManager) cleanupRelay(channelID ftl.ChannelID, targetHostname string) error {
stream, err := mgr.GetStream(channelID)
if err != nil {
return err
}
// Relay might not actually exist in our state
if _, exists := stream.relays[targetHostname]; !exists {
return errors.New("relay does not exist in state")
}
// Remove from MultiWriter
// stream.edgeWriter.remove(targetHostname)
if err := stream.relays[targetHostname].close(); err != nil {
return err
}
delete(stream.relays, targetHostname)
return nil
}
func (stream *Stream) WriteRTP(packet *rtp.Packet) error {
buf, err := packet.Marshal()
if err != nil {
return err
}
// This can error if the relay is removed in another thread, which is common because an edge will stop being a relay for a stream when there are no viewers on it.
// TODO: Figure out how to conditionally error from this.
// stream.edgeWriter.write(buf)
for _, relay := range stream.relays {
relay.ftlMedia.Write(buf)
}
return nil
}
// AddStream adds a stream to the manager in an unauthenticated state
//func (mgr *StreamManager) AddStream(connHandler *ConnHandler) error {
// if _, exists := mgr.streams[connHandler.channelID]; exists {
// return errors.New("stream already exists in state")
// }
// mgr.streams[connHandler.channelID] = connHandler
// return nil
//}
func (mgr *StreamManager) RemoveStream(id ftl.ChannelID) error {
if _, exists := mgr.streams[id]; !exists {
return errors.New("RemoveStream stream does not exist in state")
}
delete(mgr.streams, id)
return nil
}
func (mgr *StreamManager) GetStream(id ftl.ChannelID) (*Stream, error) {
if _, exists := mgr.streams[id]; !exists {
return &Stream{}, errors.New("GetStream stream does not exist in state")
}
return mgr.streams[id], nil
}
func (relay *StreamRelay) close() error {
if err := relay.ftlClient.Close(); err != nil {
return err
}
return nil
}