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"
|
2022-08-16 11:53:04 +00:00
|
|
|
"crypto/tls"
|
2021-07-04 16:13:49 +00:00
|
|
|
"fmt"
|
2021-01-31 15:58:57 +00:00
|
|
|
"net"
|
2021-04-27 16:32:23 +00:00
|
|
|
"sync"
|
2022-08-05 12:39:07 +00:00
|
|
|
"time"
|
2021-01-31 15:58:57 +00:00
|
|
|
|
2023-04-01 17:52:06 +00:00
|
|
|
"github.com/aler9/mediamtx/internal/conf"
|
|
|
|
"github.com/aler9/mediamtx/internal/externalcmd"
|
|
|
|
"github.com/aler9/mediamtx/internal/logger"
|
2021-01-31 15:58:57 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 16:53:24 +00:00
|
|
|
type rtmpServerAPIConnsListItem struct {
|
2022-11-11 10:59:52 +00:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
RemoteAddr string `json:"remoteAddr"`
|
|
|
|
State string `json:"state"`
|
|
|
|
BytesReceived uint64 `json:"bytesReceived"`
|
|
|
|
BytesSent uint64 `json:"bytesSent"`
|
2021-11-05 16:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-08-16 11:53:04 +00:00
|
|
|
isTLS bool
|
2021-12-22 18:13:56 +00:00
|
|
|
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
|
2022-08-16 11:53:04 +00:00
|
|
|
ln net.Listener
|
2021-07-24 13:55:42 +00:00
|
|
|
conns map[*rtmpConn]struct{}
|
2021-04-27 16:32:23 +00:00
|
|
|
|
|
|
|
// in
|
2022-08-04 19:07:17 +00:00
|
|
|
chConnClose chan *rtmpConn
|
|
|
|
chAPIConnsList chan rtmpServerAPIConnsListReq
|
|
|
|
chAPIConnsKick 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,
|
2022-08-16 11:53:04 +00:00
|
|
|
isTLS bool,
|
|
|
|
serverCert string,
|
|
|
|
serverKey string,
|
2021-04-27 16:32:23 +00:00
|
|
|
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) {
|
2022-08-16 11:53:04 +00:00
|
|
|
ln, err := func() (net.Listener, error) {
|
|
|
|
if !isTLS {
|
2023-04-10 20:48:33 +00:00
|
|
|
return net.Listen(restrictNetwork("tcp", address))
|
2022-08-16 11:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-10 20:48:33 +00:00
|
|
|
network, address := restrictNetwork("tcp", address)
|
|
|
|
return tls.Listen(network, address, &tls.Config{Certificates: []tls.Certificate{cert}})
|
2022-08-16 11:53:04 +00:00
|
|
|
}()
|
2021-01-31 15:58:57 +00:00
|
|
|
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,
|
2022-08-16 11:53:04 +00:00
|
|
|
isTLS: isTLS,
|
2021-12-22 18:13:56 +00:00
|
|
|
externalCmdPool: externalCmdPool,
|
|
|
|
metrics: metrics,
|
|
|
|
pathManager: pathManager,
|
|
|
|
parent: parent,
|
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
2022-08-16 11:53:04 +00:00
|
|
|
ln: ln,
|
2021-12-22 18:13:56 +00:00
|
|
|
conns: make(map[*rtmpConn]struct{}),
|
2022-08-04 19:07:17 +00:00
|
|
|
chConnClose: make(chan *rtmpConn),
|
|
|
|
chAPIConnsList: make(chan rtmpServerAPIConnsListReq),
|
|
|
|
chAPIConnsKick: 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 {
|
2022-08-04 19:07:17 +00:00
|
|
|
s.metrics.rtmpServerSet(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{}) {
|
2022-08-16 11:53:04 +00:00
|
|
|
label := func() string {
|
|
|
|
if s.isTLS {
|
|
|
|
return "RTMPS"
|
|
|
|
}
|
|
|
|
return "RTMP"
|
|
|
|
}()
|
|
|
|
s.parent.Log(level, "[%s] "+format, append([]interface{}{label}, 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 {
|
2022-08-16 11:53:04 +00:00
|
|
|
conn, err := s.ln.Accept()
|
2021-04-27 16:32:23 +00:00
|
|
|
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-24 13:55:42 +00:00
|
|
|
c := newRTMPConn(
|
2021-05-11 15:20:32 +00:00
|
|
|
s.ctx,
|
2022-08-23 11:47:48 +00:00
|
|
|
s.isTLS,
|
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
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
case c := <-s.chConnClose:
|
2021-08-12 08:50:29 +00:00
|
|
|
delete(s.conns, c)
|
2021-04-27 16:32:23 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
case req := <-s.chAPIConnsList:
|
2021-11-05 16:53:24 +00:00
|
|
|
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 {
|
2022-11-09 18:31:52 +00:00
|
|
|
data.Items[c.uuid.String()] = rtmpServerAPIConnsListItem{
|
2022-08-05 12:39:07 +00:00
|
|
|
Created: c.created,
|
|
|
|
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"
|
|
|
|
}(),
|
2022-11-11 10:59:52 +00:00
|
|
|
BytesReceived: c.conn.BytesReceived(),
|
|
|
|
BytesSent: c.conn.BytesSent(),
|
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
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
case req := <-s.chAPIConnsKick:
|
2021-07-04 16:13:49 +00:00
|
|
|
res := func() bool {
|
|
|
|
for c := range s.conns {
|
2022-11-09 18:31:52 +00:00
|
|
|
if c.uuid.String() == 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
|
|
|
|
2022-08-16 11:53:04 +00:00
|
|
|
s.ln.Close()
|
2021-08-12 09:48:47 +00:00
|
|
|
|
|
|
|
if s.metrics != nil {
|
2022-08-04 19:07:17 +00:00
|
|
|
s.metrics.rtmpServerSet(s)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2021-04-27 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// connClose is called by rtmpConn.
|
|
|
|
func (s *rtmpServer) connClose(c *rtmpConn) {
|
2021-05-10 19:32:59 +00:00
|
|
|
select {
|
2022-08-04 19:07:17 +00:00
|
|
|
case s.chConnClose <- c:
|
2021-05-10 19:32:59 +00:00
|
|
|
case <-s.ctx.Done():
|
|
|
|
}
|
2021-01-31 15:58:57 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiConnsList is called by api.
|
2022-11-09 17:31:31 +00:00
|
|
|
func (s *rtmpServer) apiConnsList() rtmpServerAPIConnsListRes {
|
|
|
|
req := rtmpServerAPIConnsListReq{
|
|
|
|
res: make(chan rtmpServerAPIConnsListRes),
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
2022-08-04 19:07:17 +00:00
|
|
|
case s.chAPIConnsList <- 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiConnsKick is called by api.
|
2022-11-09 17:31:31 +00:00
|
|
|
func (s *rtmpServer) apiConnsKick(id string) rtmpServerAPIConnsKickRes {
|
|
|
|
req := rtmpServerAPIConnsKickReq{
|
|
|
|
id: id,
|
|
|
|
res: make(chan rtmpServerAPIConnsKickRes),
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
2022-08-04 19:07:17 +00:00
|
|
|
case s.chAPIConnsKick <- 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
|
|
|
}
|
|
|
|
}
|