2021-01-31 12:18:05 +00:00
|
|
|
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-19 12:13:38 +00:00
|
|
|
"sync/atomic"
|
2021-03-06 08:11:46 +00:00
|
|
|
"time"
|
2020-12-06 17:01:10 +00:00
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
2020-12-08 11:21:06 +00:00
|
|
|
|
|
|
|
"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 {
|
2020-12-08 11:21:06 +00:00
|
|
|
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-03-19 12:13:38 +00:00
|
|
|
useTLS bool
|
2020-10-19 20:17:48 +00:00
|
|
|
parent Parent
|
|
|
|
|
2021-03-19 12:13:38 +00:00
|
|
|
srv *gortsplib.Server
|
|
|
|
closed uint32
|
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.
|
2021-01-15 17:42:53 +00:00
|
|
|
func New(
|
|
|
|
listenIP string,
|
|
|
|
port int,
|
2021-03-06 08:11:46 +00:00
|
|
|
readTimeout time.Duration,
|
|
|
|
writeTimeout time.Duration,
|
|
|
|
readBufferCount int,
|
2021-03-10 18:55:26 +00:00
|
|
|
readBufferSize int,
|
2021-03-06 08:11:46 +00:00
|
|
|
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,
|
2021-03-10 18:55:26 +00:00
|
|
|
ReadBufferSize: readBufferSize,
|
2021-03-06 08:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}}
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:42:53 +00:00
|
|
|
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-19 12:13:38 +00:00
|
|
|
useTLS: useTLS,
|
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-10 18:22:02 +00:00
|
|
|
if conf.UDPRTPAddress != "" {
|
|
|
|
parent.Log(logger.Info, "[RTSP/UDP/RTP listener] opened on %s", conf.UDPRTPAddress)
|
|
|
|
}
|
2021-03-06 08:54:10 +00:00
|
|
|
|
2021-03-10 18:22:02 +00:00
|
|
|
if conf.UDPRTCPAddress != "" {
|
|
|
|
parent.Log(logger.Info, "[RTSP/UDP/RTCP listener] opened on %s", conf.UDPRTCPAddress)
|
|
|
|
}
|
2021-03-06 08:54:10 +00:00
|
|
|
|
2021-03-19 12:13:38 +00:00
|
|
|
s.log(logger.Info, "opened on %s", address)
|
|
|
|
|
|
|
|
go s.run()
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) log(level logger.Level, format string, args ...interface{}) {
|
2021-01-31 12:18:05 +00:00
|
|
|
label := func() string {
|
2021-03-19 12:13:38 +00:00
|
|
|
if s.useTLS {
|
2021-03-06 08:11:46 +00:00
|
|
|
return "RTSP/TLS"
|
2021-01-31 12:18:05 +00:00
|
|
|
}
|
2021-03-06 08:11:46 +00:00
|
|
|
return "RTSP/TCP"
|
2021-01-31 12:18:05 +00:00
|
|
|
}()
|
2021-03-19 12:13:38 +00:00
|
|
|
s.parent.Log(level, "[%s listener] "+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() {
|
|
|
|
go func() {
|
|
|
|
for co := range s.accept {
|
|
|
|
co.Close()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-19 12:13:38 +00:00
|
|
|
atomic.StoreUint32(&s.closed, 1)
|
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 {
|
2021-03-19 12:13:38 +00:00
|
|
|
if atomic.LoadUint32(&s.closed) == 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.log(logger.Warn, "ERR: %s", err)
|
|
|
|
continue
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|