mediamtx/main.go

170 lines
3.6 KiB
Go
Raw Normal View History

2019-12-28 21:07:03 +00:00
package main
import (
"fmt"
"log"
"net"
"os"
2020-01-03 21:39:55 +00:00
"regexp"
"strings"
2019-12-28 21:07:03 +00:00
"sync"
"gopkg.in/alecthomas/kingpin.v2"
)
var Version string = "v0.0.0"
2019-12-31 12:48:17 +00:00
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 = iota
_STREAM_PROTOCOL_TCP
)
func (s streamProtocol) String() string {
if s == _STREAM_PROTOCOL_UDP {
return "udp"
}
return "tcp"
}
2019-12-28 21:07:03 +00:00
type program struct {
protocols map[streamProtocol]struct{}
2019-12-31 13:55:46 +00:00
rtspPort int
rtpPort int
rtcpPort int
2020-01-03 21:39:55 +00:00
publishKey string
2019-12-31 13:55:46 +00:00
mutex sync.RWMutex
rtspl *rtspListener
rtpl *udpListener
rtcpl *udpListener
clients map[*client]struct{}
publishers map[string]*client
2019-12-28 21:07:03 +00:00
}
func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*program, error) {
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 supplied")
}
2020-01-03 21:39:55 +00:00
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)
2019-12-28 21:07:03 +00:00
p := &program{
protocols: protocols,
2019-12-31 13:55:46 +00:00
rtspPort: rtspPort,
rtpPort: rtpPort,
rtcpPort: rtcpPort,
2020-01-03 21:39:55 +00:00
publishKey: publishKey,
2019-12-31 13:55:46 +00:00
clients: make(map[*client]struct{}),
publishers: make(map[string]*client),
2019-12-28 21:07:03 +00:00
}
var err error
2019-12-31 12:48:17 +00:00
p.rtpl, err = newUdpListener(p, rtpPort, _TRACK_FLOW_RTP)
2019-12-28 21:07:03 +00:00
if err != nil {
return nil, err
}
2019-12-31 12:48:17 +00:00
p.rtcpl, err = newUdpListener(p, rtcpPort, _TRACK_FLOW_RTCP)
2019-12-28 21:07:03 +00:00
if err != nil {
return nil, err
}
p.rtspl, err = newRtspListener(p)
if err != nil {
return nil, err
}
return p, nil
}
func (p *program) run() {
2019-12-29 00:52:52 +00:00
go p.rtpl.run()
go p.rtcpl.run()
go p.rtspl.run()
2019-12-28 21:07:03 +00:00
2019-12-29 00:52:52 +00:00
infty := make(chan struct{})
<-infty
2019-12-28 21:07:03 +00:00
}
2019-12-31 13:55:46 +00:00
func (p *program) forwardTrack(path string, id int, flow trackFlow, frame []byte) {
2019-12-29 00:05:25 +00:00
for c := range p.clients {
2019-12-31 13:55:46 +00:00
if c.path == path && c.state == "PLAY" {
2019-12-31 12:48:17 +00:00
if c.streamProtocol == _STREAM_PROTOCOL_UDP {
if flow == _TRACK_FLOW_RTP {
p.rtpl.nconn.WriteTo(frame, &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtpPort,
})
} else {
p.rtcpl.nconn.WriteTo(frame, &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtcpPort,
})
}
2019-12-29 00:05:25 +00:00
} else {
2019-12-31 13:55:46 +00:00
c.rconn.WriteInterleavedFrame(trackToInterleavedChannel(id, flow), frame)
2019-12-29 00:05:25 +00:00
}
}
}
}
2019-12-28 21:07:03 +00:00
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()
2019-12-28 21:07:03 +00:00
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()
2020-01-03 21:39:55 +00:00
publishKey := kingpin.Flag("publish-key", "optional authentication key required to publish").Default("").String()
2019-12-28 21:07:03 +00:00
kingpin.Parse()
if *version == true {
fmt.Println("rtsp-simple-server " + Version)
os.Exit(0)
}
p, err := newProgram(*protocols, *rtspPort, *rtpPort, *rtcpPort, *publishKey)
2019-12-28 21:07:03 +00:00
if err != nil {
2019-12-29 11:32:54 +00:00
log.Fatal("ERR: ", err)
2019-12-28 21:07:03 +00:00
}
p.run()
}