Skip to content

Commit f607fa8

Browse files
committed
add command line option --protocols to set what protocols are available
1 parent e78f3f2 commit f607fa8

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ rtsp-simple-server v0.0.0
7272
RTSP server.
7373

7474
Flags:
75-
--help Show context-sensitive help (also try --help-long and --help-man).
76-
--version print rtsp-simple-server version
77-
--rtsp-port=8554 port of the RTSP TCP listener
78-
--rtp-port=8000 port of the RTP UDP listener
79-
--rtcp-port=8001 port of the RTCP UDP listener
80-
--publish-key="" optional authentication key required to publish
75+
--help Show context-sensitive help (also try --help-long and --help-man).
76+
--version print rtsp-simple-server version
77+
--protocols="udp,tcp" supported protocols
78+
--rtsp-port=8554 port of the RTSP TCP listener
79+
--rtp-port=8000 port of the RTP UDP listener
80+
--rtcp-port=8001 port of the RTCP UDP listener
81+
--publish-key="" optional authentication key required to publish
8182
```
8283
8384

client.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@ func (c *client) handleRequest(req *rtsp.Request) bool {
386386
}
387387
return false
388388
}() {
389+
if _, ok := c.p.protocols[_STREAM_PROTOCOL_UDP]; !ok {
390+
c.log("ERR: udp streaming is disabled")
391+
c.rconn.WriteResponse(&rtsp.Response{
392+
StatusCode: 461,
393+
Status: "Unsupported Transport",
394+
Headers: map[string]string{
395+
"CSeq": cseq,
396+
},
397+
})
398+
return false
399+
}
400+
389401
rtpPort, rtcpPort := th.getClientPorts()
390402
if rtpPort == 0 || rtcpPort == 0 {
391403
c.writeResError(req, fmt.Errorf("transport header does not have valid client ports (%s)", transportStr))
@@ -447,6 +459,18 @@ func (c *client) handleRequest(req *rtsp.Request) bool {
447459

448460
// play via TCP
449461
} else if _, ok := th["RTP/AVP/TCP"]; ok {
462+
if _, ok := c.p.protocols[_STREAM_PROTOCOL_TCP]; !ok {
463+
c.log("ERR: tcp streaming is disabled")
464+
c.rconn.WriteResponse(&rtsp.Response{
465+
StatusCode: 461,
466+
Status: "Unsupported Transport",
467+
Headers: map[string]string{
468+
"CSeq": cseq,
469+
},
470+
})
471+
return false
472+
}
473+
450474
if c.path != "" && path != c.path {
451475
c.writeResError(req, fmt.Errorf("path has changed"))
452476
return false
@@ -529,7 +553,19 @@ func (c *client) handleRequest(req *rtsp.Request) bool {
529553
return true
530554
}
531555
return false
532-
}(); ok {
556+
}() {
557+
if _, ok := c.p.protocols[_STREAM_PROTOCOL_UDP]; !ok {
558+
c.log("ERR: udp streaming is disabled")
559+
c.rconn.WriteResponse(&rtsp.Response{
560+
StatusCode: 461,
561+
Status: "Unsupported Transport",
562+
Headers: map[string]string{
563+
"CSeq": cseq,
564+
},
565+
})
566+
return false
567+
}
568+
533569
rtpPort, rtcpPort := th.getClientPorts()
534570
if rtpPort == 0 || rtcpPort == 0 {
535571
c.writeResError(req, fmt.Errorf("transport header does not have valid client ports (%s)", transportStr))
@@ -580,6 +616,18 @@ func (c *client) handleRequest(req *rtsp.Request) bool {
580616

581617
// record via TCP
582618
} else if _, ok := th["RTP/AVP/TCP"]; ok {
619+
if _, ok := c.p.protocols[_STREAM_PROTOCOL_TCP]; !ok {
620+
c.log("ERR: tcp streaming is disabled")
621+
c.rconn.WriteResponse(&rtsp.Response{
622+
StatusCode: 461,
623+
Status: "Unsupported Transport",
624+
Headers: map[string]string{
625+
"CSeq": cseq,
626+
},
627+
})
628+
return false
629+
}
630+
583631
var interleaved string
584632
err = func() error {
585633
c.p.mutex.Lock()

main.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"os"
88
"regexp"
9+
"strings"
910
"sync"
1011

1112
"gopkg.in/alecthomas/kingpin.v2"
@@ -40,6 +41,7 @@ func (s streamProtocol) String() string {
4041
}
4142

4243
type program struct {
44+
protocols map[streamProtocol]struct{}
4345
rtspPort int
4446
rtpPort int
4547
rtcpPort int
@@ -52,7 +54,24 @@ type program struct {
5254
publishers map[string]*client
5355
}
5456

55-
func newProgram(rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*program, error) {
57+
func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*program, error) {
58+
protocols := make(map[streamProtocol]struct{})
59+
for _, proto := range strings.Split(protocolsStr, ",") {
60+
switch proto {
61+
case "udp":
62+
protocols[_STREAM_PROTOCOL_UDP] = struct{}{}
63+
64+
case "tcp":
65+
protocols[_STREAM_PROTOCOL_TCP] = struct{}{}
66+
67+
default:
68+
return nil, fmt.Errorf("unsupported protocol: %s", proto)
69+
}
70+
}
71+
if len(protocols) == 0 {
72+
return nil, fmt.Errorf("no protocols supplied")
73+
}
74+
5675
if publishKey != "" {
5776
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(publishKey) {
5877
return nil, fmt.Errorf("publish key must be alphanumeric")
@@ -62,6 +81,7 @@ func newProgram(rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*pr
6281
log.Printf("rtsp-simple-server %s", Version)
6382

6483
p := &program{
84+
protocols: protocols,
6585
rtspPort: rtspPort,
6686
rtpPort: rtpPort,
6787
rtcpPort: rtcpPort,
@@ -128,6 +148,7 @@ func main() {
128148

129149
version := kingpin.Flag("version", "print rtsp-simple-server version").Bool()
130150

151+
protocols := kingpin.Flag("protocols", "supported protocols").Default("udp,tcp").String()
131152
rtspPort := kingpin.Flag("rtsp-port", "port of the RTSP TCP listener").Default("8554").Int()
132153
rtpPort := kingpin.Flag("rtp-port", "port of the RTP UDP listener").Default("8000").Int()
133154
rtcpPort := kingpin.Flag("rtcp-port", "port of the RTCP UDP listener").Default("8001").Int()
@@ -140,7 +161,7 @@ func main() {
140161
os.Exit(0)
141162
}
142163

143-
p, err := newProgram(*rtspPort, *rtpPort, *rtcpPort, *publishKey)
164+
p, err := newProgram(*protocols, *rtspPort, *rtpPort, *rtcpPort, *publishKey)
144165
if err != nil {
145166
log.Fatal("ERR: ", err)
146167
}

0 commit comments

Comments
 (0)