forked from bluenviron/mediamtx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.go
293 lines (251 loc) · 7.04 KB
/
path.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
package main
import (
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/aler9/rtsp-simple-server/conf"
"github.com/aler9/rtsp-simple-server/externalcmd"
)
const (
describeTimeout = 5 * time.Second
sourceStopAfterDescribePeriod = 10 * time.Second
onDemandCmdStopAfterDescribePeriod = 10 * time.Second
)
// a source can be a client, a sourceRtsp or a sourceRtmp
type source interface {
isSource()
}
type path struct {
p *program
name string
conf *conf.PathConf
source source
sourceReady bool
sourceTrackCount int
sourceSdp []byte
lastDescribeReq time.Time
lastDescribeActivation time.Time
onInitCmd *externalcmd.ExternalCmd
onDemandCmd *externalcmd.ExternalCmd
}
func newPath(p *program, name string, conf *conf.PathConf) *path {
pa := &path{
p: p,
name: name,
conf: conf,
}
if strings.HasPrefix(conf.Source, "rtsp://") {
s := newSourceRtsp(p, pa)
pa.source = s
} else if strings.HasPrefix(conf.Source, "rtmp://") {
s := newSourceRtmp(p, pa)
pa.source = s
}
return pa
}
func (pa *path) log(format string, args ...interface{}) {
pa.p.log("[path "+pa.name+"] "+format, args...)
}
func (pa *path) onInit() {
if source, ok := pa.source.(*sourceRtsp); ok {
go source.run(source.state)
} else if source, ok := pa.source.(*sourceRtmp); ok {
go source.run(source.state)
}
if pa.conf.RunOnInit != "" {
pa.log("starting on init command")
var err error
pa.onInitCmd, err = externalcmd.New(pa.conf.RunOnInit, pa.name)
if err != nil {
pa.log("ERR: %s", err)
}
}
}
func (pa *path) onClose() {
if source, ok := pa.source.(*sourceRtsp); ok {
close(source.terminate)
<-source.done
} else if source, ok := pa.source.(*sourceRtmp); ok {
close(source.terminate)
<-source.done
}
if pa.onInitCmd != nil {
pa.log("stopping on init command (closing)")
pa.onInitCmd.Close()
}
if pa.onDemandCmd != nil {
pa.log("stopping on demand command (closing)")
pa.onDemandCmd.Close()
}
for c := range pa.p.clients {
if c.path == pa {
if c.state == clientStateWaitDescription {
c.path = nil
c.state = clientStateInitial
c.describe <- describeRes{nil, fmt.Errorf("publisher of path '%s' has timed out", pa.name)}
} else {
c.close()
}
}
}
}
func (pa *path) hasClients() bool {
for c := range pa.p.clients {
if c.path == pa {
return true
}
}
return false
}
func (pa *path) hasClientsWaitingDescribe() bool {
for c := range pa.p.clients {
if c.state == clientStateWaitDescription && c.path == pa {
return true
}
}
return false
}
func (pa *path) hasClientReaders() bool {
for c := range pa.p.clients {
if c.path == pa && c != pa.source {
return true
}
}
return false
}
func (pa *path) onCheck() {
// reply to DESCRIBE requests if they are in timeout
if pa.hasClientsWaitingDescribe() &&
time.Since(pa.lastDescribeActivation) >= describeTimeout {
for c := range pa.p.clients {
if c.state == clientStateWaitDescription &&
c.path == pa {
c.path = nil
c.state = clientStateInitial
c.describe <- describeRes{nil, fmt.Errorf("publisher of path '%s' has timed out", pa.name)}
}
}
}
// stop on demand rtsp source if needed
if source, ok := pa.source.(*sourceRtsp); ok {
if pa.conf.SourceOnDemand &&
source.state == sourceRtspStateRunning &&
!pa.hasClients() &&
time.Since(pa.lastDescribeReq) >= sourceStopAfterDescribePeriod {
pa.log("stopping on demand rtsp source (not requested anymore)")
atomic.AddInt64(pa.p.countSourcesRtspRunning, -1)
source.state = sourceRtspStateStopped
source.setState <- source.state
}
// stop on demand rtmp source if needed
} else if source, ok := pa.source.(*sourceRtmp); ok {
if pa.conf.SourceOnDemand &&
source.state == sourceRtmpStateRunning &&
!pa.hasClients() &&
time.Since(pa.lastDescribeReq) >= sourceStopAfterDescribePeriod {
pa.log("stopping on demand rtmp source (not requested anymore)")
atomic.AddInt64(pa.p.countSourcesRtmpRunning, -1)
source.state = sourceRtmpStateStopped
source.setState <- source.state
}
}
// stop on demand command if needed
if pa.onDemandCmd != nil &&
!pa.hasClientReaders() &&
time.Since(pa.lastDescribeReq) >= onDemandCmdStopAfterDescribePeriod {
pa.log("stopping on demand command (not requested anymore)")
pa.onDemandCmd.Close()
pa.onDemandCmd = nil
}
// remove regular expression paths
if pa.conf.Regexp != nil &&
pa.source == nil &&
!pa.hasClients() {
pa.onClose()
delete(pa.p.paths, pa.name)
}
}
func (pa *path) onSourceRemove() {
pa.source = nil
// close all clients that are reading or waiting for reading
for c := range pa.p.clients {
if c.path == pa &&
c.state != clientStateWaitDescription &&
c != pa.source {
c.close()
}
}
}
func (pa *path) onSourceSetReady() {
pa.sourceReady = true
// reply to all clients that are waiting for a description
for c := range pa.p.clients {
if c.state == clientStateWaitDescription &&
c.path == pa {
c.path = nil
c.state = clientStateInitial
c.describe <- describeRes{pa.sourceSdp, nil}
}
}
}
func (pa *path) onSourceSetNotReady() {
pa.sourceReady = false
// close all clients that are reading or waiting for reading
for c := range pa.p.clients {
if c.path == pa &&
c.state != clientStateWaitDescription &&
c != pa.source {
c.close()
}
}
}
func (pa *path) onDescribe(client *client) {
pa.lastDescribeReq = time.Now()
// publisher not found
if pa.source == nil {
// on demand command is available: put the client on hold
if pa.conf.RunOnDemand != "" {
if pa.onDemandCmd == nil { // start if needed
pa.log("starting on demand command")
pa.lastDescribeActivation = time.Now()
var err error
pa.onDemandCmd, err = externalcmd.New(pa.conf.RunOnDemand, pa.name)
if err != nil {
pa.log("ERR: %s", err)
}
}
client.path = pa
client.state = clientStateWaitDescription
// no on-demand: reply with 404
} else {
client.describe <- describeRes{nil, fmt.Errorf("no one is publishing on path '%s'", pa.name)}
}
// publisher was found but is not ready: put the client on hold
} else if !pa.sourceReady {
// start rtsp source if needed
if source, ok := pa.source.(*sourceRtsp); ok {
if source.state == sourceRtspStateStopped {
pa.log("starting on demand rtsp source")
pa.lastDescribeActivation = time.Now()
atomic.AddInt64(pa.p.countSourcesRtspRunning, +1)
source.state = sourceRtspStateRunning
source.setState <- source.state
}
// start rtmp source if needed
} else if source, ok := pa.source.(*sourceRtmp); ok {
if source.state == sourceRtmpStateStopped {
pa.log("starting on demand rtmp source")
pa.lastDescribeActivation = time.Now()
atomic.AddInt64(pa.p.countSourcesRtmpRunning, +1)
source.state = sourceRtmpStateRunning
source.setState <- source.state
}
}
client.path = pa
client.state = clientStateWaitDescription
// publisher was found and is ready
} else {
client.describe <- describeRes{pa.sourceSdp, nil}
}
}