mediamtx/internal/serverrtmp/server.go

193 lines
3.6 KiB
Go
Raw Normal View History

2021-01-31 15:58:57 +00:00
package serverrtmp
import (
"net"
2021-04-27 16:32:23 +00:00
"sync"
"time"
2021-01-31 15:58:57 +00:00
2021-04-27 16:32:23 +00:00
"github.com/aler9/rtsp-simple-server/internal/clientrtmp"
2021-01-31 15:58:57 +00:00
"github.com/aler9/rtsp-simple-server/internal/logger"
2021-04-27 16:32:23 +00:00
"github.com/aler9/rtsp-simple-server/internal/pathman"
"github.com/aler9/rtsp-simple-server/internal/stats"
2021-01-31 15:58:57 +00:00
)
// Parent is implemented by program.
type Parent interface {
Log(logger.Level, string, ...interface{})
}
// Server is a RTMP listener.
type Server struct {
2021-04-27 16:32:23 +00:00
readTimeout time.Duration
writeTimeout time.Duration
readBufferCount int
rtspAddress string
runOnConnect string
runOnConnectRestart bool
stats *stats.Stats
pathMan *pathman.PathManager
parent Parent
l net.Listener
wg sync.WaitGroup
clients map[*clientrtmp.Client]struct{}
// in
clientClose chan *clientrtmp.Client
terminate chan struct{}
// out
2021-04-27 16:32:23 +00:00
done chan struct{}
2021-01-31 15:58:57 +00:00
}
// New allocates a Server.
func New(
address string,
2021-04-27 16:32:23 +00:00
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount int,
rtspAddress string,
runOnConnect string,
runOnConnectRestart bool,
stats *stats.Stats,
pathMan *pathman.PathManager,
2021-01-31 15:58:57 +00:00
parent Parent) (*Server, error) {
l, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
s := &Server{
2021-04-27 16:32:23 +00:00
readTimeout: readTimeout,
writeTimeout: writeTimeout,
readBufferCount: readBufferCount,
rtspAddress: rtspAddress,
runOnConnect: runOnConnect,
runOnConnectRestart: runOnConnectRestart,
stats: stats,
pathMan: pathMan,
parent: parent,
l: l,
clients: make(map[*clientrtmp.Client]struct{}),
clientClose: make(chan *clientrtmp.Client),
terminate: make(chan struct{}),
done: make(chan struct{}),
2021-01-31 15:58:57 +00:00
}
2021-04-27 16:32:23 +00:00
s.Log(logger.Info, "listener opened on %s", address)
2021-01-31 15:58:57 +00:00
go s.run()
return s, nil
}
2021-04-27 16:32:23 +00:00
// Log is the main logging function.
func (s *Server) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[RTMP] "+format, append([]interface{}{}, args...)...)
}
2021-01-31 15:58:57 +00:00
// Close closes a Server.
func (s *Server) Close() {
2021-04-27 16:32:23 +00:00
close(s.terminate)
<-s.done
2021-01-31 15:58:57 +00:00
}
func (s *Server) run() {
defer close(s.done)
2021-01-31 15:58:57 +00:00
2021-04-27 16:32:23 +00:00
s.wg.Add(1)
connNew := make(chan net.Conn)
2021-04-27 16:32:23 +00:00
acceptErr := make(chan error)
go func() {
defer s.wg.Done()
acceptErr <- func() error {
for {
conn, err := s.l.Accept()
if err != nil {
return err
}
connNew <- conn
2021-04-27 16:32:23 +00:00
}
}()
}()
outer:
2021-01-31 15:58:57 +00:00
for {
2021-04-27 16:32:23 +00:00
select {
case err := <-acceptErr:
s.Log(logger.Warn, "ERR: %s", err)
break outer
case nconn := <-connNew:
2021-04-27 16:32:23 +00:00
c := clientrtmp.New(
s.rtspAddress,
s.readTimeout,
s.writeTimeout,
s.readBufferCount,
s.runOnConnect,
s.runOnConnectRestart,
&s.wg,
s.stats,
nconn,
s.pathMan,
s)
s.clients[c] = struct{}{}
case c := <-s.clientClose:
if _, ok := s.clients[c]; !ok {
continue
}
2021-04-27 16:32:23 +00:00
s.doClientClose(c)
case <-s.terminate:
break outer
2021-01-31 15:58:57 +00:00
}
2021-04-27 16:32:23 +00:00
}
2021-01-31 15:58:57 +00:00
2021-04-27 16:32:23 +00:00
go func() {
for {
select {
case _, ok := <-acceptErr:
if !ok {
return
}
case conn, ok := <-connNew:
2021-04-27 16:32:23 +00:00
if !ok {
return
}
conn.Close()
case _, ok := <-s.clientClose:
if !ok {
return
}
}
}
}()
s.l.Close()
for c := range s.clients {
s.doClientClose(c)
2021-01-31 15:58:57 +00:00
}
2021-04-27 16:32:23 +00:00
s.wg.Wait()
close(acceptErr)
close(connNew)
2021-04-27 16:32:23 +00:00
close(s.clientClose)
}
func (s *Server) doClientClose(c *clientrtmp.Client) {
delete(s.clients, c)
c.Close()
}
2021-01-31 15:58:57 +00:00
2021-04-27 17:29:43 +00:00
// OnClientClose is called by clientrtmp.Client.
2021-04-27 16:32:23 +00:00
func (s *Server) OnClientClose(c *clientrtmp.Client) {
s.clientClose <- c
2021-01-31 15:58:57 +00:00
}