forked from aler9/rtsp-simple-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (203 loc) · 4.79 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
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
package main
import (
"fmt"
"log"
"os"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)
var Version = "v0.0.0"
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 streamConf struct {
Url string `yaml:"url"`
Protocol string `yaml:"protocol"`
}
type conf struct {
ReadTimeout string `yaml:"readTimeout"`
WriteTimeout string `yaml:"writeTimeout"`
Server struct {
Protocols []string `yaml:"protocols"`
RtspPort int `yaml:"rtspPort"`
RtpPort int `yaml:"rtpPort"`
RtcpPort int `yaml:"rtcpPort"`
ReadUser string `yaml:"readUser"`
ReadPass string `yaml:"readPass"`
} `yaml:"server"`
Streams map[string]streamConf `yaml:"streams"`
}
func loadConf(confPath string) (*conf, error) {
if confPath == "stdin" {
var ret conf
err := yaml.NewDecoder(os.Stdin).Decode(&ret)
if err != nil {
return nil, err
}
return &ret, nil
} else {
f, err := os.Open(confPath)
if err != nil {
return nil, err
}
defer f.Close()
var ret conf
err = yaml.NewDecoder(f).Decode(&ret)
if err != nil {
return nil, err
}
return &ret, nil
}
}
type args struct {
version bool
confPath string
}
type program struct {
conf conf
readTimeout time.Duration
writeTimeout time.Duration
protocols map[streamProtocol]struct{}
streams map[string]*stream
tcpl *serverTcpListener
udplRtp *serverUdpListener
udplRtcp *serverUdpListener
}
func newProgram(sargs []string) (*program, error) {
kingpin.CommandLine.Help = "rtsp-simple-proxy " + Version + "\n\n" +
"RTSP proxy."
argVersion := kingpin.Flag("version", "print version").Bool()
argConfPath := kingpin.Arg("confpath", "path of a config file. The default is conf.yml. Use 'stdin' to read config from stdin").Default("conf.yml").String()
kingpin.MustParse(kingpin.CommandLine.Parse(sargs))
args := args{
version: *argVersion,
confPath: *argConfPath,
}
if args.version == true {
fmt.Println(Version)
os.Exit(0)
}
conf, err := loadConf(args.confPath)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
if conf.ReadTimeout == "" {
conf.ReadTimeout = "5s"
}
if conf.WriteTimeout == "" {
conf.WriteTimeout = "5s"
}
if len(conf.Server.Protocols) == 0 {
conf.Server.Protocols = []string{"tcp", "udp"}
}
if conf.Server.RtspPort == 0 {
conf.Server.RtspPort = 8554
}
if conf.Server.RtpPort == 0 {
conf.Server.RtpPort = 8050
}
if conf.Server.RtcpPort == 0 {
conf.Server.RtcpPort = 8051
}
readTimeout, err := time.ParseDuration(conf.ReadTimeout)
if err != nil {
return nil, fmt.Errorf("unable to parse read timeout: %s", err)
}
writeTimeout, err := time.ParseDuration(conf.WriteTimeout)
if err != nil {
return nil, fmt.Errorf("unable to parse write timeout: %s", err)
}
protocols := make(map[streamProtocol]struct{})
for _, proto := range conf.Server.Protocols {
switch proto {
case "udp":
protocols[_STREAM_PROTOCOL_UDP] = struct{}{}
case "tcp":
protocols[_STREAM_PROTOCOL_TCP] = struct{}{}
default:
return nil, fmt.Errorf("unsupported protocol: '%v'", proto)
}
}
if len(protocols) == 0 {
return nil, fmt.Errorf("no protocols provided")
}
if (conf.Server.RtpPort % 2) != 0 {
return nil, fmt.Errorf("rtp port must be even")
}
if conf.Server.RtcpPort != (conf.Server.RtpPort + 1) {
return nil, fmt.Errorf("rtcp port must be rtp port plus 1")
}
if len(conf.Streams) == 0 {
return nil, fmt.Errorf("no streams provided")
}
log.Printf("rtsp-simple-proxy %s", Version)
p := &program{
conf: *conf,
readTimeout: readTimeout,
writeTimeout: writeTimeout,
protocols: protocols,
streams: make(map[string]*stream),
}
for path, val := range p.conf.Streams {
var err error
p.streams[path], err = newStream(p, path, val)
if err != nil {
return nil, fmt.Errorf("error in stream '%s': %s", path, err)
}
}
p.udplRtp, err = newServerUdpListener(p, p.conf.Server.RtpPort, _TRACK_FLOW_RTP)
if err != nil {
return nil, err
}
p.udplRtcp, err = newServerUdpListener(p, p.conf.Server.RtcpPort, _TRACK_FLOW_RTCP)
if err != nil {
return nil, err
}
p.tcpl, err = newServerTcpListener(p)
if err != nil {
return nil, err
}
for _, s := range p.streams {
go s.run()
}
go p.udplRtp.run()
go p.udplRtcp.run()
go p.tcpl.run()
return p, nil
}
func (p *program) close() {
for _, s := range p.streams {
s.close()
}
p.tcpl.close()
p.udplRtcp.close()
p.udplRtp.close()
}
func main() {
_, err := newProgram(os.Args[1:])
if err != nil {
log.Fatal("ERR: ", err)
}
select {}
}