2021-05-09 15:02:14 +00:00
|
|
|
package rtmpserver
|
2021-01-31 15:58:57 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 19:32:59 +00:00
|
|
|
"context"
|
2021-01-31 15:58:57 +00:00
|
|
|
"net"
|
2021-04-27 16:32:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
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"
|
2021-05-09 15:22:24 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmpconn"
|
2021-04-27 16:32:23 +00:00
|
|
|
"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{})
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:41:18 +00:00
|
|
|
// Server is a RTMP server.
|
2021-01-31 15:58:57 +00:00
|
|
|
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
|
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
wg sync.WaitGroup
|
|
|
|
l net.Listener
|
|
|
|
conns map[*rtmpconn.Conn]struct{}
|
2021-04-27 16:32:23 +00:00
|
|
|
|
|
|
|
// in
|
2021-05-09 15:02:14 +00:00
|
|
|
connClose chan *rtmpconn.Conn
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New allocates a Server.
|
|
|
|
func New(
|
2021-05-11 15:20:32 +00:00
|
|
|
ctxParent context.Context,
|
2021-04-24 16:25:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-11 15:20:32 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(ctxParent)
|
2021-05-10 19:32:59 +00:00
|
|
|
|
2021-01-31 15:58:57 +00:00
|
|
|
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,
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
2021-04-27 16:32:23 +00:00
|
|
|
l: l,
|
2021-05-09 15:02:14 +00:00
|
|
|
conns: make(map[*rtmpconn.Conn]struct{}),
|
|
|
|
connClose: make(chan *rtmpconn.Conn),
|
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
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
s.wg.Add(1)
|
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-03-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 15:58:57 +00:00
|
|
|
// Close closes a Server.
|
|
|
|
func (s *Server) Close() {
|
2021-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
|
|
|
s.wg.Wait()
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) run() {
|
2021-05-10 19:32:59 +00:00
|
|
|
defer s.wg.Done()
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2021-04-27 16:32:23 +00:00
|
|
|
s.wg.Add(1)
|
2021-04-27 17:04:05 +00:00
|
|
|
connNew := make(chan net.Conn)
|
2021-04-27 16:32:23 +00:00
|
|
|
acceptErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
2021-05-10 19:32:59 +00:00
|
|
|
err := func() error {
|
2021-04-27 16:32:23 +00:00
|
|
|
for {
|
|
|
|
conn, err := s.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
select {
|
|
|
|
case connNew <- conn:
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
conn.Close()
|
|
|
|
}
|
2021-04-27 16:32:23 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-05-10 19:32:59 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case acceptErr <- err:
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
}
|
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
|
|
|
|
|
2021-04-27 17:04:05 +00:00
|
|
|
case nconn := <-connNew:
|
2021-05-09 15:02:14 +00:00
|
|
|
c := rtmpconn.New(
|
2021-05-11 15:20:32 +00:00
|
|
|
s.ctx,
|
2021-04-27 16:32:23 +00:00
|
|
|
s.rtspAddress,
|
|
|
|
s.readTimeout,
|
|
|
|
s.writeTimeout,
|
|
|
|
s.readBufferCount,
|
|
|
|
s.runOnConnect,
|
|
|
|
s.runOnConnectRestart,
|
|
|
|
&s.wg,
|
|
|
|
s.stats,
|
|
|
|
nconn,
|
|
|
|
s.pathMan,
|
|
|
|
s)
|
2021-05-09 12:41:18 +00:00
|
|
|
s.conns[c] = struct{}{}
|
2021-04-27 16:32:23 +00:00
|
|
|
|
2021-05-09 12:41:18 +00:00
|
|
|
case c := <-s.connClose:
|
|
|
|
if _, ok := s.conns[c]; !ok {
|
2021-04-27 16:32:23 +00:00
|
|
|
continue
|
2021-03-19 12:13:38 +00:00
|
|
|
}
|
2021-05-09 12:41:18 +00:00
|
|
|
s.doConnClose(c)
|
2021-04-27 16:32:23 +00:00
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
case <-s.ctx.Done():
|
2021-04-27 16:32:23 +00:00
|
|
|
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-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
2021-04-27 16:32:23 +00:00
|
|
|
|
|
|
|
s.l.Close()
|
|
|
|
|
2021-05-09 12:41:18 +00:00
|
|
|
for c := range s.conns {
|
|
|
|
s.doConnClose(c)
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
2021-04-27 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 15:02:14 +00:00
|
|
|
func (s *Server) doConnClose(c *rtmpconn.Conn) {
|
2021-05-09 12:41:18 +00:00
|
|
|
delete(s.conns, c)
|
2021-05-09 14:12:15 +00:00
|
|
|
c.ParentClose()
|
2021-04-27 16:32:23 +00:00
|
|
|
c.Close()
|
2021-01-31 20:42:27 +00:00
|
|
|
}
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2021-05-09 15:02:14 +00:00
|
|
|
// OnConnClose is called by rtmpconn.Conn.
|
|
|
|
func (s *Server) OnConnClose(c *rtmpconn.Conn) {
|
2021-05-10 19:32:59 +00:00
|
|
|
select {
|
|
|
|
case s.connClose <- c:
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
}
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|