mediamtx/internal/serverrtsp/server.go

125 lines
2.2 KiB
Go
Raw Normal View History

package serverrtsp
2020-10-19 20:17:48 +00:00
import (
2021-03-06 08:11:46 +00:00
"crypto/tls"
2020-12-06 17:01:10 +00:00
"strconv"
2021-03-06 08:11:46 +00:00
"time"
2020-12-06 17:01:10 +00:00
"github.com/aler9/gortsplib"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-10-19 20:17:48 +00:00
)
2020-11-05 11:30:25 +00:00
// Parent is implemented by program.
2020-10-19 20:17:48 +00:00
type Parent interface {
Log(logger.Level, string, ...interface{})
2020-10-19 20:17:48 +00:00
}
2021-01-31 15:58:57 +00:00
// Server is a RTSP listener.
2020-10-19 20:17:48 +00:00
type Server struct {
parent Parent
2021-03-06 08:54:10 +00:00
srv *gortsplib.Server
2020-10-19 20:17:48 +00:00
// out
2020-12-06 17:01:10 +00:00
accept chan *gortsplib.ServerConn
2020-10-19 20:17:48 +00:00
done chan struct{}
}
2020-11-05 11:30:25 +00:00
// New allocates a Server.
func New(
listenIP string,
port int,
2021-03-06 08:11:46 +00:00
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount int,
useUDP bool,
rtpPort int,
rtcpPort int,
useTLS bool,
serverCert string,
serverKey string,
2020-12-06 17:01:10 +00:00
parent Parent) (*Server, error) {
2021-03-06 08:11:46 +00:00
conf := gortsplib.ServerConf{
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ReadBufferCount: readBufferCount,
}
if useUDP {
2021-03-06 08:54:10 +00:00
conf.UDPRTPAddress = listenIP + ":" + strconv.FormatInt(int64(rtpPort), 10)
conf.UDPRTCPAddress = listenIP + ":" + strconv.FormatInt(int64(rtcpPort), 10)
2021-03-06 08:11:46 +00:00
}
if useTLS {
cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
if err != nil {
return nil, err
}
conf.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}
address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
srv, err := conf.Serve(address)
2020-10-19 20:17:48 +00:00
if err != nil {
return nil, err
}
s := &Server{
2021-03-06 08:54:10 +00:00
parent: parent,
srv: srv,
accept: make(chan *gortsplib.ServerConn),
done: make(chan struct{}),
2020-10-19 20:17:48 +00:00
}
2021-03-06 08:54:10 +00:00
parent.Log(logger.Info, "[RTSP/UDP/RTP listener] opened on %s", conf.UDPRTPAddress)
parent.Log(logger.Info, "[RTSP/UDP/RTCP listener] opened on %s", conf.UDPRTCPAddress)
label := func() string {
if conf.TLSConfig != nil {
2021-03-06 08:11:46 +00:00
return "RTSP/TLS"
}
2021-03-06 08:11:46 +00:00
return "RTSP/TCP"
}()
parent.Log(logger.Info, "[%s listener] opened on %s", label, address)
2020-10-19 20:17:48 +00:00
go s.run()
2021-01-31 15:58:57 +00:00
2020-10-19 20:17:48 +00:00
return s, nil
}
2020-11-05 11:30:25 +00:00
// Close closes a Server.
2020-10-19 20:17:48 +00:00
func (s *Server) Close() {
go func() {
for co := range s.accept {
co.Close()
}
}()
2021-03-06 08:11:46 +00:00
2020-12-06 17:01:10 +00:00
s.srv.Close()
2020-10-19 20:17:48 +00:00
<-s.done
}
func (s *Server) run() {
defer close(s.done)
for {
2020-12-06 17:01:10 +00:00
conn, err := s.srv.Accept()
2020-10-19 20:17:48 +00:00
if err != nil {
break
}
s.accept <- conn
}
close(s.accept)
}
2020-11-05 11:30:25 +00:00
// Accept returns a channel to accept incoming connections.
2020-12-13 22:43:31 +00:00
func (s *Server) Accept() chan *gortsplib.ServerConn {
2020-10-19 20:17:48 +00:00
return s.accept
}