mediamtx/internal/serverrtsp/server.go

299 lines
6.7 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"
2021-04-27 11:43:15 +00:00
"sync"
2021-03-06 08:11:46 +00:00
"time"
2020-12-06 17:01:10 +00:00
"github.com/aler9/gortsplib"
2021-04-27 11:43:15 +00:00
"github.com/aler9/gortsplib/pkg/base"
2021-04-27 11:43:15 +00:00
"github.com/aler9/rtsp-simple-server/internal/clientrtsp"
"github.com/aler9/rtsp-simple-server/internal/logger"
2021-04-27 11:43:15 +00:00
"github.com/aler9/rtsp-simple-server/internal/pathman"
2021-05-07 21:07:31 +00:00
"github.com/aler9/rtsp-simple-server/internal/sessionrtsp"
2021-04-27 11:43:15 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
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 {
2021-04-27 11:43:15 +00:00
readTimeout time.Duration
isTLS bool
rtspAddress string
protocols map[base.StreamProtocol]struct{}
runOnConnect string
runOnConnectRestart bool
stats *stats.Stats
pathMan *pathman.PathManager
parent Parent
2021-05-07 21:07:31 +00:00
srv *gortsplib.Server
mutex sync.RWMutex
clients map[*gortsplib.ServerConn]*clientrtsp.Client
sessions map[*gortsplib.ServerSession]*sessionrtsp.Session
2021-04-27 11:43:15 +00:00
// in
2021-05-07 21:07:31 +00:00
terminate chan struct{}
2020-10-19 20:17:48 +00:00
// out
2021-04-27 11:43:15 +00:00
done chan struct{}
2020-10-19 20:17:48 +00:00
}
2020-11-05 11:30:25 +00:00
// New allocates a Server.
func New(
address string,
2021-03-06 08:11:46 +00:00
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount int,
readBufferSize int,
2021-03-06 08:11:46 +00:00
useUDP bool,
rtpAddress string,
rtcpAddress string,
2021-04-27 11:43:15 +00:00
isTLS bool,
2021-03-06 08:11:46 +00:00
serverCert string,
serverKey string,
2021-04-27 11:43:15 +00:00
rtspAddress string,
protocols map[base.StreamProtocol]struct{},
runOnConnect string,
runOnConnectRestart bool,
stats *stats.Stats,
pathMan *pathman.PathManager,
2020-12-06 17:01:10 +00:00
parent Parent) (*Server, error) {
2021-05-07 21:07:31 +00:00
s := &Server{
readTimeout: readTimeout,
isTLS: isTLS,
rtspAddress: rtspAddress,
protocols: protocols,
stats: stats,
pathMan: pathMan,
parent: parent,
clients: make(map[*gortsplib.ServerConn]*clientrtsp.Client),
sessions: make(map[*gortsplib.ServerSession]*sessionrtsp.Session),
terminate: make(chan struct{}),
done: make(chan struct{}),
}
s.srv = &gortsplib.Server{
Handler: s,
2021-03-06 08:11:46 +00:00
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ReadBufferCount: readBufferCount,
ReadBufferSize: readBufferSize,
2021-03-06 08:11:46 +00:00
}
if useUDP {
2021-05-07 21:07:31 +00:00
s.srv.UDPRTPAddress = rtpAddress
s.srv.UDPRTCPAddress = rtcpAddress
2021-03-06 08:11:46 +00:00
}
2021-04-27 11:43:15 +00:00
if isTLS {
2021-03-06 08:11:46 +00:00
cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
if err != nil {
return nil, err
}
2021-05-07 21:07:31 +00:00
s.srv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
2021-03-06 08:11:46 +00:00
}
2021-05-07 21:07:31 +00:00
err := s.srv.Start(address)
2020-10-19 20:17:48 +00:00
if err != nil {
return nil, err
}
2021-05-07 21:07:31 +00:00
if s.srv.UDPRTPAddress != "" {
s.Log(logger.Info, "UDP/RTP listener opened on %s", s.srv.UDPRTPAddress)
}
2021-03-06 08:54:10 +00:00
2021-05-07 21:07:31 +00:00
if s.srv.UDPRTCPAddress != "" {
s.Log(logger.Info, "UDP/RTCP listener opened on %s", s.srv.UDPRTCPAddress)
}
2021-03-06 08:54:10 +00:00
2021-04-27 11:43:15 +00:00
s.Log(logger.Info, "TCP listener opened on %s", address)
go s.run()
return s, nil
}
2021-04-27 11:43:15 +00:00
// Log is the main logging function.
func (s *Server) Log(level logger.Level, format string, args ...interface{}) {
label := func() string {
2021-04-27 11:43:15 +00:00
if s.isTLS {
return "RTSPS"
}
2021-04-27 11:43:15 +00:00
return "RTSP"
}()
2021-04-27 11:43:15 +00:00
s.parent.Log(level, "[%s] "+format, append([]interface{}{label}, args...)...)
2020-10-19 20:17:48 +00:00
}
2020-11-05 11:30:25 +00:00
// Close closes a Server.
2020-10-19 20:17:48 +00:00
func (s *Server) Close() {
2021-04-27 11:43:15 +00:00
close(s.terminate)
2020-10-19 20:17:48 +00:00
<-s.done
}
func (s *Server) run() {
defer close(s.done)
2021-05-07 21:07:31 +00:00
serverDone := make(chan struct{})
serverErr := make(chan error)
2021-04-27 11:43:15 +00:00
go func() {
2021-05-07 21:07:31 +00:00
defer close(serverDone)
serverErr <- s.srv.Wait()
2021-04-27 11:43:15 +00:00
}()
outer:
2021-05-07 21:07:31 +00:00
select {
case err := <-serverErr:
s.Log(logger.Warn, "ERR: %s", err)
break outer
case <-s.terminate:
break outer
2021-04-27 11:43:15 +00:00
}
go func() {
2021-05-07 21:07:31 +00:00
for range serverErr {
2020-10-19 20:17:48 +00:00
}
2021-04-27 11:43:15 +00:00
}()
2020-10-19 20:17:48 +00:00
2021-04-27 11:43:15 +00:00
s.srv.Close()
2021-05-07 21:07:31 +00:00
<-serverDone
close(serverErr)
}
// OnConnOpen implements gortsplib.ServerHandlerOnConnOpenCtx.
func (s *Server) OnConnOpen(sc *gortsplib.ServerConn) {
c := clientrtsp.New(
s.rtspAddress,
s.readTimeout,
s.runOnConnect,
s.runOnConnectRestart,
s.pathMan,
s.stats,
sc,
s)
s.mutex.Lock()
s.clients[sc] = c
s.mutex.Unlock()
}
// OnConnClose implements gortsplib.ServerHandlerOnConnCloseCtx.
func (s *Server) OnConnClose(sc *gortsplib.ServerConn, err error) {
s.mutex.Lock()
c := s.clients[sc]
delete(s.clients, sc)
s.mutex.Unlock()
c.Close(err)
}
// OnRequest implements gortsplib.ServerHandlerOnRequestCtx.
func (s *Server) OnRequest(sc *gortsplib.ServerConn, req *base.Request) {
s.mutex.Lock()
c := s.clients[sc]
s.mutex.Unlock()
c.OnRequest(req)
}
// OnResponse implements gortsplib.ServerHandlerOnResponseCtx.
func (s *Server) OnResponse(sc *gortsplib.ServerConn, res *base.Response) {
s.mutex.Lock()
c := s.clients[sc]
s.mutex.Unlock()
c.OnResponse(res)
}
// OnSessionOpen implements gortsplib.ServerHandlerOnSessionOpenCtx.
func (s *Server) OnSessionOpen(ss *gortsplib.ServerSession) {
se := sessionrtsp.New(
s.rtspAddress,
s.protocols,
ss,
s.pathMan,
s)
s.mutex.Lock()
s.sessions[ss] = se
s.mutex.Unlock()
}
// OnSessionClose implements gortsplib.ServerHandlerOnSessionCloseCtx.
func (s *Server) OnSessionClose(ss *gortsplib.ServerSession, err error) {
s.mutex.Lock()
se := s.sessions[ss]
delete(s.sessions, ss)
s.mutex.Unlock()
2020-10-19 20:17:48 +00:00
2021-05-07 21:07:31 +00:00
se.Close()
}
// OnDescribe implements gortsplib.ServerHandlerOnDescribeCtx.
func (s *Server) OnDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx) (*base.Response, []byte, error) {
s.mutex.RLock()
c := s.clients[ctx.Conn]
s.mutex.RUnlock()
return c.OnDescribe(ctx)
}
// OnAnnounce implements gortsplib.ServerHandlerOnAnnounceCtx.
func (s *Server) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (*base.Response, error) {
s.mutex.RLock()
c := s.clients[ctx.Conn]
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
return se.OnAnnounce(c, ctx)
}
// OnSetup implements gortsplib.ServerHandlerOnSetupCtx.
func (s *Server) OnSetup(ctx *gortsplib.ServerHandlerOnSetupCtx) (*base.Response, error) {
s.mutex.RLock()
c := s.clients[ctx.Conn]
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
return se.OnSetup(c, ctx)
}
// OnPlay implements gortsplib.ServerHandlerOnPlayCtx.
func (s *Server) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response, error) {
s.mutex.RLock()
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
return se.OnPlay(ctx)
}
2021-04-27 11:43:15 +00:00
2021-05-07 21:07:31 +00:00
// OnRecord implements gortsplib.ServerHandlerOnRecordCtx.
func (s *Server) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*base.Response, error) {
s.mutex.RLock()
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
return se.OnRecord(ctx)
2021-04-27 11:43:15 +00:00
}
2021-05-07 21:07:31 +00:00
// OnPause implements gortsplib.ServerHandlerOnPauseCtx.
func (s *Server) OnPause(ctx *gortsplib.ServerHandlerOnPauseCtx) (*base.Response, error) {
s.mutex.RLock()
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
return se.OnPause(ctx)
2020-10-19 20:17:48 +00:00
}
2021-05-07 21:07:31 +00:00
// OnFrame implements gortsplib.ServerHandlerOnFrameCtx.
func (s *Server) OnFrame(ctx *gortsplib.ServerHandlerOnFrameCtx) {
s.mutex.RLock()
se := s.sessions[ctx.Session]
s.mutex.RUnlock()
se.OnIncomingFrame(ctx)
2020-10-19 20:17:48 +00:00
}