-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
server.go
309 lines (260 loc) · 6.76 KB
/
server.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
// Package hls contains a HLS server.
package hls
import (
"context"
"errors"
"fmt"
"sort"
"sync"
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/stream"
)
// ErrMuxerNotFound is returned when a muxer is not found.
var ErrMuxerNotFound = errors.New("muxer not found")
type serverGetMuxerRes struct {
muxer *muxer
err error
}
type serverGetMuxerReq struct {
path string
remoteAddr string
query string
sourceOnDemand bool
res chan serverGetMuxerRes
}
type serverAPIMuxersListRes struct {
data *defs.APIHLSMuxerList
err error
}
type serverAPIMuxersListReq struct {
res chan serverAPIMuxersListRes
}
type serverAPIMuxersGetRes struct {
data *defs.APIHLSMuxer
err error
}
type serverAPIMuxersGetReq struct {
name string
res chan serverAPIMuxersGetRes
}
type serverPathManager interface {
FindPathConf(req defs.PathFindPathConfReq) (*conf.Path, error)
AddReader(req defs.PathAddReaderReq) (defs.Path, *stream.Stream, error)
}
type serverParent interface {
logger.Writer
}
// Server is a HLS server.
type Server struct {
Address string
Encryption bool
ServerKey string
ServerCert string
AllowOrigin string
TrustedProxies conf.IPNetworks
AlwaysRemux bool
Variant conf.HLSVariant
SegmentCount int
SegmentDuration conf.StringDuration
PartDuration conf.StringDuration
SegmentMaxSize conf.StringSize
Directory string
ReadTimeout conf.StringDuration
WriteQueueSize int
PathManager serverPathManager
Parent serverParent
ctx context.Context
ctxCancel func()
wg sync.WaitGroup
httpServer *httpServer
muxers map[string]*muxer
// in
chPathReady chan defs.Path
chPathNotReady chan defs.Path
chGetMuxer chan serverGetMuxerReq
chCloseMuxer chan *muxer
chAPIMuxerList chan serverAPIMuxersListReq
chAPIMuxerGet chan serverAPIMuxersGetReq
}
// Initialize initializes the server.
func (s *Server) Initialize() error {
ctx, ctxCancel := context.WithCancel(context.Background())
s.ctx = ctx
s.ctxCancel = ctxCancel
s.muxers = make(map[string]*muxer)
s.chPathReady = make(chan defs.Path)
s.chPathNotReady = make(chan defs.Path)
s.chGetMuxer = make(chan serverGetMuxerReq)
s.chCloseMuxer = make(chan *muxer)
s.chAPIMuxerList = make(chan serverAPIMuxersListReq)
s.chAPIMuxerGet = make(chan serverAPIMuxersGetReq)
s.httpServer = &httpServer{
address: s.Address,
encryption: s.Encryption,
serverKey: s.ServerKey,
serverCert: s.ServerCert,
allowOrigin: s.AllowOrigin,
trustedProxies: s.TrustedProxies,
readTimeout: s.ReadTimeout,
pathManager: s.PathManager,
parent: s,
}
err := s.httpServer.initialize()
if err != nil {
ctxCancel()
return err
}
s.Log(logger.Info, "listener opened on "+s.Address)
s.wg.Add(1)
go s.run()
return nil
}
// Log implements logger.Writer.
func (s *Server) Log(level logger.Level, format string, args ...interface{}) {
s.Parent.Log(level, "[HLS] "+format, args...)
}
// Close closes the server.
func (s *Server) Close() {
s.Log(logger.Info, "listener is closing")
s.ctxCancel()
s.wg.Wait()
}
func (s *Server) run() {
defer s.wg.Done()
outer:
for {
select {
case pa := <-s.chPathReady:
if s.AlwaysRemux && !pa.SafeConf().SourceOnDemand {
if _, ok := s.muxers[pa.Name()]; !ok {
s.createMuxer(pa.Name(), "", "")
}
}
case pa := <-s.chPathNotReady:
c, ok := s.muxers[pa.Name()]
if ok && c.remoteAddr == "" { // created with "always remux"
c.Close()
delete(s.muxers, pa.Name())
}
case req := <-s.chGetMuxer:
mux, ok := s.muxers[req.path]
switch {
case ok:
req.res <- serverGetMuxerRes{muxer: mux}
case s.AlwaysRemux && !req.sourceOnDemand:
req.res <- serverGetMuxerRes{err: fmt.Errorf("muxer is waiting to be created")}
default:
req.res <- serverGetMuxerRes{muxer: s.createMuxer(req.path, req.remoteAddr, req.query)}
}
case c := <-s.chCloseMuxer:
if c2, ok := s.muxers[c.PathName()]; ok && c2 == c {
delete(s.muxers, c.PathName())
}
case req := <-s.chAPIMuxerList:
data := &defs.APIHLSMuxerList{
Items: []*defs.APIHLSMuxer{},
}
for _, muxer := range s.muxers {
data.Items = append(data.Items, muxer.apiItem())
}
sort.Slice(data.Items, func(i, j int) bool {
return data.Items[i].Created.Before(data.Items[j].Created)
})
req.res <- serverAPIMuxersListRes{
data: data,
}
case req := <-s.chAPIMuxerGet:
muxer, ok := s.muxers[req.name]
if !ok {
req.res <- serverAPIMuxersGetRes{err: ErrMuxerNotFound}
continue
}
req.res <- serverAPIMuxersGetRes{data: muxer.apiItem()}
case <-s.ctx.Done():
break outer
}
}
s.ctxCancel()
s.httpServer.close()
}
func (s *Server) createMuxer(pathName string, remoteAddr string, query string) *muxer {
r := &muxer{
parentCtx: s.ctx,
remoteAddr: remoteAddr,
variant: s.Variant,
segmentCount: s.SegmentCount,
segmentDuration: s.SegmentDuration,
partDuration: s.PartDuration,
segmentMaxSize: s.SegmentMaxSize,
directory: s.Directory,
writeQueueSize: s.WriteQueueSize,
wg: &s.wg,
pathName: pathName,
pathManager: s.PathManager,
parent: s,
query: query,
}
r.initialize()
s.muxers[pathName] = r
return r
}
// closeMuxer is called by muxer.
func (s *Server) closeMuxer(c *muxer) {
select {
case s.chCloseMuxer <- c:
case <-s.ctx.Done():
}
}
func (s *Server) getMuxer(req serverGetMuxerReq) (*muxer, error) {
req.res = make(chan serverGetMuxerRes)
select {
case s.chGetMuxer <- req:
res := <-req.res
return res.muxer, res.err
case <-s.ctx.Done():
return nil, fmt.Errorf("terminated")
}
}
// PathReady is called by pathManager.
func (s *Server) PathReady(pa defs.Path) {
select {
case s.chPathReady <- pa:
case <-s.ctx.Done():
}
}
// PathNotReady is called by pathManager.
func (s *Server) PathNotReady(pa defs.Path) {
select {
case s.chPathNotReady <- pa:
case <-s.ctx.Done():
}
}
// APIMuxersList is called by api.
func (s *Server) APIMuxersList() (*defs.APIHLSMuxerList, error) {
req := serverAPIMuxersListReq{
res: make(chan serverAPIMuxersListRes),
}
select {
case s.chAPIMuxerList <- req:
res := <-req.res
return res.data, res.err
case <-s.ctx.Done():
return nil, fmt.Errorf("terminated")
}
}
// APIMuxersGet is called by api.
func (s *Server) APIMuxersGet(name string) (*defs.APIHLSMuxer, error) {
req := serverAPIMuxersGetReq{
name: name,
res: make(chan serverAPIMuxersGetRes),
}
select {
case s.chAPIMuxerGet <- req:
res := <-req.res
return res.data, res.err
case <-s.ctx.Done():
return nil, fmt.Errorf("terminated")
}
}