forked from bluenviron/mediamtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (168 loc) · 4.27 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
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
package main
import (
"fmt"
"log"
"net"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/aler9/gortsplib"
"gopkg.in/alecthomas/kingpin.v2"
)
var Version string = "v0.0.0"
const (
_READ_TIMEOUT = 5 * time.Second
_WRITE_TIMEOUT = 5 * time.Second
)
type trackFlow int
const (
_TRACK_FLOW_RTP trackFlow = iota
_TRACK_FLOW_RTCP
)
type track struct {
rtpPort int
rtcpPort int
}
type streamProtocol int
const (
_STREAM_PROTOCOL_UDP streamProtocol = iota
_STREAM_PROTOCOL_TCP
)
func (s streamProtocol) String() string {
if s == _STREAM_PROTOCOL_UDP {
return "udp"
}
return "tcp"
}
type program struct {
protocols map[streamProtocol]struct{}
rtspPort int
rtpPort int
rtcpPort int
publishKey string
mutex sync.RWMutex
rtspl *serverTcpListener
rtpl *serverUdpListener
rtcpl *serverUdpListener
clients map[*client]struct{}
publishers map[string]*client
}
func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*program, error) {
if rtspPort == 0 {
return nil, fmt.Errorf("rtsp port not provided")
}
if rtpPort == 0 {
return nil, fmt.Errorf("rtp port not provided")
}
if rtcpPort == 0 {
return nil, fmt.Errorf("rtcp port not provided")
}
if (rtpPort % 2) != 0 {
return nil, fmt.Errorf("rtp port must be even")
}
if rtcpPort != (rtpPort + 1) {
return nil, fmt.Errorf("rtcp port must be rtp port plus 1")
}
protocols := make(map[streamProtocol]struct{})
for _, proto := range strings.Split(protocolsStr, ",") {
switch proto {
case "udp":
protocols[_STREAM_PROTOCOL_UDP] = struct{}{}
case "tcp":
protocols[_STREAM_PROTOCOL_TCP] = struct{}{}
default:
return nil, fmt.Errorf("unsupported protocol: %s", proto)
}
}
if len(protocols) == 0 {
return nil, fmt.Errorf("no protocols provided")
}
if publishKey != "" {
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(publishKey) {
return nil, fmt.Errorf("publish key must be alphanumeric")
}
}
log.Printf("rtsp-simple-server %s", Version)
p := &program{
protocols: protocols,
rtspPort: rtspPort,
rtpPort: rtpPort,
rtcpPort: rtcpPort,
publishKey: publishKey,
clients: make(map[*client]struct{}),
publishers: make(map[string]*client),
}
var err error
p.rtpl, err = newServerUdpListener(p, rtpPort, _TRACK_FLOW_RTP)
if err != nil {
return nil, err
}
p.rtcpl, err = newServerUdpListener(p, rtcpPort, _TRACK_FLOW_RTCP)
if err != nil {
return nil, err
}
p.rtspl, err = newServerTcpListener(p)
if err != nil {
return nil, err
}
return p, nil
}
func (p *program) run() {
go p.rtpl.run()
go p.rtcpl.run()
go p.rtspl.run()
infty := make(chan struct{})
<-infty
}
func (p *program) forwardTrack(path string, id int, flow trackFlow, frame []byte) {
for c := range p.clients {
if c.path == path && c.state == _CLIENT_STATE_PLAY {
if c.streamProtocol == _STREAM_PROTOCOL_UDP {
if flow == _TRACK_FLOW_RTP {
p.rtpl.chanWrite <- &udpWrite{
addr: &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtpPort,
},
buf: frame,
}
} else {
p.rtcpl.chanWrite <- &udpWrite{
addr: &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtcpPort,
},
buf: frame,
}
}
} else {
c.chanWrite <- &gortsplib.InterleavedFrame{
Channel: trackToInterleavedChannel(id, flow),
Content: frame,
}
}
}
}
}
func main() {
kingpin.CommandLine.Help = "rtsp-simple-server " + Version + "\n\n" +
"RTSP server."
version := kingpin.Flag("version", "print rtsp-simple-server version").Bool()
protocols := kingpin.Flag("protocols", "supported protocols").Default("udp,tcp").String()
rtspPort := kingpin.Flag("rtsp-port", "port of the RTSP TCP listener").Default("8554").Int()
rtpPort := kingpin.Flag("rtp-port", "port of the RTP UDP listener").Default("8000").Int()
rtcpPort := kingpin.Flag("rtcp-port", "port of the RTCP UDP listener").Default("8001").Int()
publishKey := kingpin.Flag("publish-key", "optional authentication key required to publish").Default("").String()
kingpin.Parse()
if *version == true {
fmt.Println("rtsp-simple-server " + Version)
os.Exit(0)
}
p, err := newProgram(*protocols, *rtspPort, *rtpPort, *rtcpPort, *publishKey)
if err != nil {
log.Fatal("ERR: ", err)
}
p.run()
}