2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 19:32:59 +00:00
|
|
|
"context"
|
2021-05-08 20:18:30 +00:00
|
|
|
"crypto/rand"
|
2021-03-06 08:11:46 +00:00
|
|
|
"crypto/tls"
|
2021-05-08 20:18:30 +00:00
|
|
|
"encoding/binary"
|
2021-07-04 16:13:49 +00:00
|
|
|
"fmt"
|
2021-05-08 20:18:30 +00:00
|
|
|
"strconv"
|
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-08-01 14:56:53 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/headers"
|
2021-10-27 17:49:43 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/liberrors"
|
2020-12-08 11:21:06 +00:00
|
|
|
|
2021-06-15 20:15:51 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2020-10-19 20:17:48 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspServerParent interface {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspServer struct {
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods []headers.AuthMethod
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration
|
2021-04-27 11:43:15 +00:00
|
|
|
isTLS bool
|
|
|
|
rtspAddress string
|
2021-06-15 20:15:51 +00:00
|
|
|
protocols map[conf.Protocol]struct{}
|
2021-04-27 11:43:15 +00:00
|
|
|
runOnConnect string
|
|
|
|
runOnConnectRestart bool
|
2021-08-12 09:48:47 +00:00
|
|
|
metrics *metrics
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager *pathManager
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspServerParent
|
2021-04-27 11:43:15 +00:00
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
wg sync.WaitGroup
|
|
|
|
srv *gortsplib.Server
|
|
|
|
mutex sync.RWMutex
|
2021-07-24 13:55:42 +00:00
|
|
|
conns map[*gortsplib.ServerConn]*rtspConn
|
|
|
|
sessions map[*gortsplib.ServerSession]*rtspSession
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTSPServer(
|
|
|
|
parentCtx context.Context,
|
2021-04-24 16:25:19 +00:00
|
|
|
address string,
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods []headers.AuthMethod,
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
writeTimeout conf.StringDuration,
|
2021-03-06 08:11:46 +00:00
|
|
|
readBufferCount int,
|
2021-03-10 18:55:26 +00:00
|
|
|
readBufferSize int,
|
2021-03-06 08:11:46 +00:00
|
|
|
useUDP bool,
|
2021-06-19 17:20:41 +00:00
|
|
|
useMulticast bool,
|
2021-04-24 16:25:19 +00:00
|
|
|
rtpAddress string,
|
|
|
|
rtcpAddress string,
|
2021-06-19 17:20:41 +00:00
|
|
|
multicastIPRange string,
|
|
|
|
multicastRTPPort int,
|
|
|
|
multicastRTCPPort int,
|
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,
|
2021-06-15 20:15:51 +00:00
|
|
|
protocols map[conf.Protocol]struct{},
|
2021-04-27 11:43:15 +00:00
|
|
|
runOnConnect string,
|
|
|
|
runOnConnectRestart bool,
|
2021-08-12 09:48:47 +00:00
|
|
|
metrics *metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager *pathManager,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspServerParent) (*rtspServer, error) {
|
|
|
|
ctx, ctxCancel := context.WithCancel(parentCtx)
|
2021-05-10 19:32:59 +00:00
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
s := &rtspServer{
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods: authMethods,
|
2021-05-07 21:07:31 +00:00
|
|
|
readTimeout: readTimeout,
|
|
|
|
isTLS: isTLS,
|
|
|
|
rtspAddress: rtspAddress,
|
|
|
|
protocols: protocols,
|
2021-08-12 09:48:47 +00:00
|
|
|
metrics: metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager: pathManager,
|
2021-05-07 21:07:31 +00:00
|
|
|
parent: parent,
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
2021-07-24 13:55:42 +00:00
|
|
|
conns: make(map[*gortsplib.ServerConn]*rtspConn),
|
|
|
|
sessions: make(map[*gortsplib.ServerSession]*rtspSession),
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.srv = &gortsplib.Server{
|
|
|
|
Handler: s,
|
2021-09-26 21:06:40 +00:00
|
|
|
ReadTimeout: time.Duration(readTimeout),
|
|
|
|
WriteTimeout: time.Duration(writeTimeout),
|
2021-03-06 08:11:46 +00:00
|
|
|
ReadBufferCount: readBufferCount,
|
2021-03-10 18:55:26 +00:00
|
|
|
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-06-19 17:20:41 +00:00
|
|
|
if useMulticast {
|
|
|
|
s.srv.MulticastIPRange = multicastIPRange
|
|
|
|
s.srv.MulticastRTPPort = multicastRTPPort
|
|
|
|
s.srv.MulticastRTCPPort = multicastRTCPPort
|
|
|
|
}
|
|
|
|
|
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 != "" {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.log(logger.Info, "UDP/RTP listener opened on %s", s.srv.UDPRTPAddress)
|
2021-03-10 18:22:02 +00:00
|
|
|
}
|
2021-03-06 08:54:10 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
if s.srv.UDPRTCPAddress != "" {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.log(logger.Info, "UDP/RTCP listener opened on %s", s.srv.UDPRTCPAddress)
|
2021-03-10 18:22:02 +00:00
|
|
|
}
|
2021-03-06 08:54:10 +00:00
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
s.log(logger.Info, "TCP listener opened on %s", address)
|
2021-03-19 12:13:38 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if s.metrics != nil {
|
|
|
|
if !isTLS {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTSPServerSet(s)
|
2021-08-12 09:48:47 +00:00
|
|
|
} else {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTSPSServerSet(s)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
s.wg.Add(1)
|
2021-03-19 12:13:38 +00:00
|
|
|
go s.run()
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
func (s *rtspServer) log(level logger.Level, format string, args ...interface{}) {
|
2021-01-31 12:18:05 +00:00
|
|
|
label := func() string {
|
2021-04-27 11:43:15 +00:00
|
|
|
if s.isTLS {
|
|
|
|
return "RTSPS"
|
2021-01-31 12:18:05 +00:00
|
|
|
}
|
2021-04-27 11:43:15 +00:00
|
|
|
return "RTSP"
|
2021-01-31 12:18:05 +00:00
|
|
|
}()
|
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
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) close() {
|
2021-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
|
|
|
s.wg.Wait()
|
2021-10-27 19:01:00 +00:00
|
|
|
s.log(logger.Info, "closed")
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) run() {
|
2021-05-10 19:32:59 +00:00
|
|
|
defer s.wg.Done()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
serverErr := make(chan error)
|
2021-04-27 11:43:15 +00:00
|
|
|
go func() {
|
2021-10-30 11:55:05 +00:00
|
|
|
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:
|
2021-10-30 11:56:59 +00:00
|
|
|
s.log(logger.Error, "%s", err)
|
2021-05-07 21:07:31 +00:00
|
|
|
break outer
|
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
case <-s.ctx.Done():
|
2021-10-30 11:55:05 +00:00
|
|
|
s.srv.Close()
|
|
|
|
<-serverErr
|
2021-05-07 21:07:31 +00:00
|
|
|
break outer
|
2021-04-27 11:43:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if s.metrics != nil {
|
|
|
|
if !s.isTLS {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTSPServerSet(nil)
|
2021-08-12 09:48:47 +00:00
|
|
|
} else {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTSPSServerSet(nil)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
func (s *rtspServer) newSessionID() (string, error) {
|
|
|
|
for {
|
|
|
|
b := make([]byte, 4)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := binary.LittleEndian.Uint32(b)
|
|
|
|
u %= 899999999
|
|
|
|
u += 100000000
|
|
|
|
|
|
|
|
id := strconv.FormatUint(uint64(u), 10)
|
|
|
|
|
|
|
|
alreadyPresent := func() bool {
|
|
|
|
for _, s := range s.sessions {
|
|
|
|
if s.ID() == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
if !alreadyPresent {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnConnOpen implements gortsplib.ServerHandlerOnConnOpen.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) {
|
|
|
|
c := newRTSPConn(
|
2021-05-07 21:07:31 +00:00
|
|
|
s.rtspAddress,
|
2021-08-01 14:56:53 +00:00
|
|
|
s.authMethods,
|
2021-05-07 21:07:31 +00:00
|
|
|
s.readTimeout,
|
|
|
|
s.runOnConnect,
|
|
|
|
s.runOnConnectRestart,
|
2021-07-30 12:49:09 +00:00
|
|
|
s.pathManager,
|
2021-05-08 20:18:30 +00:00
|
|
|
ctx.Conn,
|
2021-05-07 21:07:31 +00:00
|
|
|
s)
|
|
|
|
|
|
|
|
s.mutex.Lock()
|
2021-05-09 12:41:18 +00:00
|
|
|
s.conns[ctx.Conn] = c
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnConnClose implements gortsplib.ServerHandlerOnConnClose.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnConnClose(ctx *gortsplib.ServerHandlerOnConnCloseCtx) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Lock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[ctx.Conn]
|
|
|
|
delete(s.conns, ctx.Conn)
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
c.onClose(ctx.Error)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnRequest implements gortsplib.ServerHandlerOnRequest.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnRequest(sc *gortsplib.ServerConn, req *base.Request) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Lock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[sc]
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
c.onRequest(req)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnResponse implements gortsplib.ServerHandlerOnResponse.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnResponse(sc *gortsplib.ServerConn, res *base.Response) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Lock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[sc]
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
|
|
|
|
|
|
|
c.OnResponse(res)
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnSessionOpen implements gortsplib.ServerHandlerOnSessionOpen.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) {
|
2021-05-08 20:18:30 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
id, _ := s.newSessionID()
|
2021-05-08 20:18:30 +00:00
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
se := newRTSPSession(
|
2021-08-20 08:32:43 +00:00
|
|
|
s.isTLS,
|
2021-05-07 21:07:31 +00:00
|
|
|
s.rtspAddress,
|
|
|
|
s.protocols,
|
2021-07-04 16:13:49 +00:00
|
|
|
id,
|
2021-05-08 20:18:30 +00:00
|
|
|
ctx.Session,
|
|
|
|
ctx.Conn,
|
2021-07-30 12:49:09 +00:00
|
|
|
s.pathManager,
|
2021-05-07 21:07:31 +00:00
|
|
|
s)
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
s.sessions[ctx.Session] = se
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnSessionClose implements gortsplib.ServerHandlerOnSessionClose.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnSessionClose(ctx *gortsplib.ServerHandlerOnSessionCloseCtx) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Lock()
|
2021-05-08 20:18:30 +00:00
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
delete(s.sessions, ctx.Session)
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.Unlock()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-08-12 08:50:29 +00:00
|
|
|
if se != nil {
|
2021-10-27 19:01:00 +00:00
|
|
|
se.onClose(ctx.Error)
|
2021-08-12 08:50:29 +00:00
|
|
|
}
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnDescribe implements gortsplib.ServerHandlerOnDescribe.
|
2021-09-09 21:05:54 +00:00
|
|
|
func (s *rtspServer) OnDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx,
|
|
|
|
) (*base.Response, *gortsplib.ServerStream, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[ctx.Conn]
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return c.onDescribe(ctx)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnAnnounce implements gortsplib.ServerHandlerOnAnnounce.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (*base.Response, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[ctx.Conn]
|
2021-05-07 21:07:31 +00:00
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return se.onAnnounce(c, ctx)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnSetup implements gortsplib.ServerHandlerOnSetup.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnSetup(ctx *gortsplib.ServerHandlerOnSetupCtx) (*base.Response, *gortsplib.ServerStream, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
2021-05-09 12:41:18 +00:00
|
|
|
c := s.conns[ctx.Conn]
|
2021-05-07 21:07:31 +00:00
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return se.onSetup(c, ctx)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnPlay implements gortsplib.ServerHandlerOnPlay.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return se.onPlay(ctx)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
2021-04-27 11:43:15 +00:00
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnRecord implements gortsplib.ServerHandlerOnRecord.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*base.Response, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return se.onRecord(ctx)
|
2021-04-27 11:43:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnPause implements gortsplib.ServerHandlerOnPause.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnPause(ctx *gortsplib.ServerHandlerOnPauseCtx) (*base.Response, error) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
return se.onPause(ctx)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 20:18:30 +00:00
|
|
|
// OnFrame implements gortsplib.ServerHandlerOnFrame.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspServer) OnFrame(ctx *gortsplib.ServerHandlerOnFrameCtx) {
|
2021-05-07 21:07:31 +00:00
|
|
|
s.mutex.RLock()
|
|
|
|
se := s.sessions[ctx.Session]
|
|
|
|
s.mutex.RUnlock()
|
2021-10-27 19:01:00 +00:00
|
|
|
se.onFrame(ctx)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onAPIRTSPSessionsList is called by api and metrics.
|
|
|
|
func (s *rtspServer) onAPIRTSPSessionsList(req apiRTSPSessionsListReq) apiRTSPSessionsListRes {
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return apiRTSPSessionsListRes{Err: fmt.Errorf("terminated")}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mutex.RLock()
|
2021-08-12 09:48:47 +00:00
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
|
|
|
|
data := &apiRTSPSessionsListData{
|
|
|
|
Items: make(map[string]apiRTSPSessionsListItem),
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
for _, s := range s.sessions {
|
2021-08-12 09:48:47 +00:00
|
|
|
data.Items[s.ID()] = apiRTSPSessionsListItem{
|
2021-07-04 16:13:49 +00:00
|
|
|
RemoteAddr: s.RemoteAddr().String(),
|
2021-08-11 10:25:19 +00:00
|
|
|
State: func() string {
|
|
|
|
switch s.safeState() {
|
|
|
|
case gortsplib.ServerSessionStatePreRead,
|
|
|
|
gortsplib.ServerSessionStateRead:
|
|
|
|
return "read"
|
|
|
|
|
|
|
|
case gortsplib.ServerSessionStatePrePublish,
|
|
|
|
gortsplib.ServerSessionStatePublish:
|
|
|
|
return "publish"
|
|
|
|
}
|
|
|
|
return "idle"
|
|
|
|
}(),
|
2021-08-07 14:07:08 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
return apiRTSPSessionsListRes{Data: data}
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onAPIRTSPSessionsKick is called by api.
|
|
|
|
func (s *rtspServer) onAPIRTSPSessionsKick(req apiRTSPSessionsKickReq) apiRTSPSessionsKickRes {
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return apiRTSPSessionsKickRes{Err: fmt.Errorf("terminated")}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mutex.RLock()
|
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
|
2021-08-12 08:50:29 +00:00
|
|
|
for key, se := range s.sessions {
|
|
|
|
if se.ID() == req.ID {
|
2021-10-27 19:01:00 +00:00
|
|
|
se.close()
|
2021-08-12 08:50:29 +00:00
|
|
|
delete(s.sessions, key)
|
2021-10-27 19:01:00 +00:00
|
|
|
se.onClose(liberrors.ErrServerTerminated{})
|
2021-07-04 16:13:49 +00:00
|
|
|
return apiRTSPSessionsKickRes{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiRTSPSessionsKickRes{Err: fmt.Errorf("not found")}
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|