2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2021-01-31 15:58:57 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 19:32:59 +00:00
|
|
|
"context"
|
2021-07-04 16:13:49 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2021-01-31 15:58:57 +00:00
|
|
|
"net"
|
2021-07-04 16:13:49 +00:00
|
|
|
"strconv"
|
2021-04-27 16:32:23 +00:00
|
|
|
"sync"
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2021-12-21 11:39:32 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/externalcmd"
|
2021-01-31 15:58:57 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
type rtmpServerAPIConnsListItem struct {
|
|
|
|
RemoteAddr string `json:"remoteAddr"`
|
|
|
|
State string `json:"state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rtmpServerAPIConnsListData struct {
|
|
|
|
Items map[string]rtmpServerAPIConnsListItem `json:"items"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rtmpServerAPIConnsListRes struct {
|
2022-01-14 22:42:41 +00:00
|
|
|
data *rtmpServerAPIConnsListData
|
|
|
|
err error
|
2021-11-05 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rtmpServerAPIConnsListReq struct {
|
2022-01-14 22:42:41 +00:00
|
|
|
res chan rtmpServerAPIConnsListRes
|
2021-11-05 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rtmpServerAPIConnsKickRes struct {
|
2022-01-14 22:42:41 +00:00
|
|
|
err error
|
2021-11-05 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rtmpServerAPIConnsKickReq struct {
|
2022-01-14 22:42:41 +00:00
|
|
|
id string
|
|
|
|
res chan rtmpServerAPIConnsKickRes
|
2021-11-05 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtmpServerParent interface {
|
2021-01-31 15:58:57 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtmpServer struct {
|
2021-12-22 18:13:56 +00:00
|
|
|
externalAuthenticationURL string
|
|
|
|
readTimeout conf.StringDuration
|
|
|
|
writeTimeout conf.StringDuration
|
|
|
|
readBufferCount int
|
|
|
|
rtspAddress string
|
|
|
|
runOnConnect string
|
|
|
|
runOnConnectRestart bool
|
|
|
|
externalCmdPool *externalcmd.Pool
|
|
|
|
metrics *metrics
|
|
|
|
pathManager *pathManager
|
|
|
|
parent rtmpServerParent
|
2021-04-27 16:32:23 +00:00
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
wg sync.WaitGroup
|
|
|
|
l net.Listener
|
2021-07-24 13:55:42 +00:00
|
|
|
conns map[*rtmpConn]struct{}
|
2021-04-27 16:32:23 +00:00
|
|
|
|
|
|
|
// in
|
2021-11-05 16:53:24 +00:00
|
|
|
connClose chan *rtmpConn
|
|
|
|
apiConnsList chan rtmpServerAPIConnsListReq
|
|
|
|
apiConnsKick chan rtmpServerAPIConnsKickReq
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTMPServer(
|
|
|
|
parentCtx context.Context,
|
2021-12-22 18:13:56 +00:00
|
|
|
externalAuthenticationURL string,
|
2021-04-24 16:25:19 +00:00
|
|
|
address string,
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
writeTimeout conf.StringDuration,
|
2021-04-27 16:32:23 +00:00
|
|
|
readBufferCount int,
|
|
|
|
rtspAddress string,
|
|
|
|
runOnConnect string,
|
|
|
|
runOnConnectRestart bool,
|
2021-12-22 18:13:56 +00:00
|
|
|
externalCmdPool *externalcmd.Pool,
|
2021-08-12 09:48:47 +00:00
|
|
|
metrics *metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager *pathManager,
|
2022-04-07 10:50:35 +00:00
|
|
|
parent rtmpServerParent,
|
|
|
|
) (*rtmpServer, error) {
|
2021-01-31 15:58:57 +00:00
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(parentCtx)
|
2021-05-10 19:32:59 +00:00
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
s := &rtmpServer{
|
2021-12-22 18:13:56 +00:00
|
|
|
externalAuthenticationURL: externalAuthenticationURL,
|
|
|
|
readTimeout: readTimeout,
|
|
|
|
writeTimeout: writeTimeout,
|
|
|
|
readBufferCount: readBufferCount,
|
|
|
|
rtspAddress: rtspAddress,
|
|
|
|
runOnConnect: runOnConnect,
|
|
|
|
runOnConnectRestart: runOnConnectRestart,
|
|
|
|
externalCmdPool: externalCmdPool,
|
|
|
|
metrics: metrics,
|
|
|
|
pathManager: pathManager,
|
|
|
|
parent: parent,
|
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
|
|
|
l: l,
|
|
|
|
conns: make(map[*rtmpConn]struct{}),
|
|
|
|
connClose: make(chan *rtmpConn),
|
|
|
|
apiConnsList: make(chan rtmpServerAPIConnsListReq),
|
|
|
|
apiConnsKick: make(chan rtmpServerAPIConnsKickReq),
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
s.log(logger.Info, "listener opened on %s", address)
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if s.metrics != nil {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTMPServerSet(s)
|
2021-08-12 09:48:47 +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-10-27 19:01:00 +00:00
|
|
|
func (s *rtmpServer) log(level logger.Level, format string, args ...interface{}) {
|
2021-04-27 16:32:23 +00:00
|
|
|
s.parent.Log(level, "[RTMP] "+format, append([]interface{}{}, args...)...)
|
2021-03-19 12:13:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtmpServer) close() {
|
2021-12-21 23:15:15 +00:00
|
|
|
s.log(logger.Info, "listener is closing")
|
2021-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
|
|
|
s.wg.Wait()
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtmpServer) 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:
|
2021-10-30 11:56:59 +00:00
|
|
|
s.log(logger.Error, "%s", err)
|
2021-04-27 16:32:23 +00:00
|
|
|
break outer
|
|
|
|
|
2021-04-27 17:04:05 +00:00
|
|
|
case nconn := <-connNew:
|
2021-07-04 16:13:49 +00:00
|
|
|
id, _ := s.newConnID()
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
c := newRTMPConn(
|
2021-05-11 15:20:32 +00:00
|
|
|
s.ctx,
|
2021-07-04 16:13:49 +00:00
|
|
|
id,
|
2021-12-22 18:13:56 +00:00
|
|
|
s.externalAuthenticationURL,
|
2021-04-27 16:32:23 +00:00
|
|
|
s.rtspAddress,
|
|
|
|
s.readTimeout,
|
|
|
|
s.writeTimeout,
|
|
|
|
s.readBufferCount,
|
|
|
|
s.runOnConnect,
|
|
|
|
s.runOnConnectRestart,
|
|
|
|
&s.wg,
|
|
|
|
nconn,
|
2021-12-22 18:13:56 +00:00
|
|
|
s.externalCmdPool,
|
2021-07-30 12:49:09 +00:00
|
|
|
s.pathManager,
|
2021-04-27 16:32:23 +00:00
|
|
|
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-08-12 08:50:29 +00:00
|
|
|
delete(s.conns, c)
|
2021-04-27 16:32:23 +00:00
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
case req := <-s.apiConnsList:
|
|
|
|
data := &rtmpServerAPIConnsListData{
|
|
|
|
Items: make(map[string]rtmpServerAPIConnsListItem),
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
for c := range s.conns {
|
2021-11-05 16:53:24 +00:00
|
|
|
data.Items[c.ID()] = rtmpServerAPIConnsListItem{
|
2021-07-04 16:13:49 +00:00
|
|
|
RemoteAddr: c.RemoteAddr().String(),
|
2021-08-11 10:25:19 +00:00
|
|
|
State: func() string {
|
|
|
|
switch c.safeState() {
|
2022-02-18 09:21:04 +00:00
|
|
|
case rtmpConnStateRead:
|
2021-08-11 10:25:19 +00:00
|
|
|
return "read"
|
|
|
|
|
2022-02-18 09:21:04 +00:00
|
|
|
case rtmpConnStatePublish:
|
2021-08-11 10:25:19 +00:00
|
|
|
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
|
|
|
|
2022-01-14 22:42:41 +00:00
|
|
|
req.res <- rtmpServerAPIConnsListRes{data: data}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
case req := <-s.apiConnsKick:
|
2021-07-04 16:13:49 +00:00
|
|
|
res := func() bool {
|
|
|
|
for c := range s.conns {
|
2022-01-14 22:42:41 +00:00
|
|
|
if c.ID() == req.id {
|
2021-08-12 08:50:29 +00:00
|
|
|
delete(s.conns, c)
|
2021-10-27 19:01:00 +00:00
|
|
|
c.close()
|
2021-07-04 16:13:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
if res {
|
2022-01-14 22:42:41 +00:00
|
|
|
req.res <- rtmpServerAPIConnsKickRes{}
|
2021-07-04 16:13:49 +00:00
|
|
|
} else {
|
2022-01-14 22:42:41 +00:00
|
|
|
req.res <- rtmpServerAPIConnsKickRes{fmt.Errorf("not found")}
|
2021-07-04 16:13:49 +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-08-12 09:48:47 +00:00
|
|
|
|
|
|
|
if s.metrics != nil {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.metrics.onRTMPServerSet(s)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2021-04-27 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
func (s *rtmpServer) newConnID() (string, error) {
|
|
|
|
for {
|
|
|
|
b := make([]byte, 4)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
u := uint32(b[3])<<24 | uint32(b[2])<<16 | uint32(b[1])<<8 | uint32(b[0])
|
2021-07-04 16:13:49 +00:00
|
|
|
u %= 899999999
|
|
|
|
u += 100000000
|
|
|
|
|
|
|
|
id := strconv.FormatUint(uint64(u), 10)
|
|
|
|
|
|
|
|
alreadyPresent := func() bool {
|
|
|
|
for c := range s.conns {
|
|
|
|
if c.ID() == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
if !alreadyPresent {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onConnClose is called by rtmpConn.
|
|
|
|
func (s *rtmpServer) onConnClose(c *rtmpConn) {
|
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
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
// onAPIConnsList is called by api.
|
|
|
|
func (s *rtmpServer) onAPIConnsList(req rtmpServerAPIConnsListReq) rtmpServerAPIConnsListRes {
|
2022-01-14 22:42:41 +00:00
|
|
|
req.res = make(chan rtmpServerAPIConnsListRes)
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
2021-11-05 16:53:24 +00:00
|
|
|
case s.apiConnsList <- req:
|
2022-01-14 22:42:41 +00:00
|
|
|
return <-req.res
|
2021-11-05 16:53:24 +00:00
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
case <-s.ctx.Done():
|
2022-01-14 22:42:41 +00:00
|
|
|
return rtmpServerAPIConnsListRes{err: fmt.Errorf("terminated")}
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
// onAPIConnsKick is called by api.
|
|
|
|
func (s *rtmpServer) onAPIConnsKick(req rtmpServerAPIConnsKickReq) rtmpServerAPIConnsKickRes {
|
2022-01-14 22:42:41 +00:00
|
|
|
req.res = make(chan rtmpServerAPIConnsKickRes)
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
2021-11-05 16:53:24 +00:00
|
|
|
case s.apiConnsKick <- req:
|
2022-01-14 22:42:41 +00:00
|
|
|
return <-req.res
|
2021-11-05 16:53:24 +00:00
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
case <-s.ctx.Done():
|
2022-01-14 22:42:41 +00:00
|
|
|
return rtmpServerAPIConnsKickRes{err: fmt.Errorf("terminated")}
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
|
|
|
}
|