mediamtx/internal/pathman/pathman.go

334 lines
8.4 KiB
Go
Raw Normal View History

2020-10-19 20:17:48 +00:00
package pathman
import (
"fmt"
"sync"
"time"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/base"
"github.com/aler9/gortsplib/headers"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/client"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/path"
"github.com/aler9/rtsp-simple-server/internal/stats"
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 {
Log(string, ...interface{})
}
2020-11-05 11:30:25 +00:00
// PathManager is a path.Path manager.
2020-10-19 20:17:48 +00:00
type PathManager struct {
2020-11-01 16:33:06 +00:00
rtspPort int
readTimeout time.Duration
writeTimeout time.Duration
authMethods []headers.AuthMethod
pathConfs map[string]*conf.PathConf
stats *stats.Stats
parent Parent
2020-10-19 20:17:48 +00:00
paths map[string]*path.Path
wg sync.WaitGroup
// in
confReload chan map[string]*conf.PathConf
2020-10-19 20:17:48 +00:00
pathClose chan *path.Path
clientDescribe chan path.ClientDescribeReq
clientAnnounce chan path.ClientAnnounceReq
clientSetupPlay chan path.ClientSetupPlayReq
terminate chan struct{}
// out
clientClose chan *client.Client
done chan struct{}
}
2020-11-05 11:30:25 +00:00
// New allocates a PathManager.
func New(
2020-11-01 16:33:06 +00:00
rtspPort int,
2020-10-19 20:17:48 +00:00
readTimeout time.Duration,
writeTimeout time.Duration,
authMethods []headers.AuthMethod,
pathConfs map[string]*conf.PathConf,
stats *stats.Stats,
2020-10-19 20:17:48 +00:00
parent Parent) *PathManager {
pm := &PathManager{
2020-11-01 16:33:06 +00:00
rtspPort: rtspPort,
2020-10-19 20:17:48 +00:00
readTimeout: readTimeout,
writeTimeout: writeTimeout,
authMethods: authMethods,
pathConfs: pathConfs,
stats: stats,
2020-10-19 20:17:48 +00:00
parent: parent,
paths: make(map[string]*path.Path),
confReload: make(chan map[string]*conf.PathConf),
2020-10-19 20:17:48 +00:00
pathClose: make(chan *path.Path),
clientDescribe: make(chan path.ClientDescribeReq),
clientAnnounce: make(chan path.ClientAnnounceReq),
clientSetupPlay: make(chan path.ClientSetupPlayReq),
terminate: make(chan struct{}),
clientClose: make(chan *client.Client),
done: make(chan struct{}),
}
pm.createPaths()
2020-10-19 20:17:48 +00:00
go pm.run()
return pm
}
2020-11-05 11:30:25 +00:00
// Close closes a PathManager.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) Close() {
go func() {
for range pm.clientClose {
}
}()
close(pm.terminate)
<-pm.done
}
2020-11-05 11:30:25 +00:00
// Log is the main logging function.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) Log(format string, args ...interface{}) {
pm.parent.Log(format, args...)
}
func (pm *PathManager) run() {
defer close(pm.done)
outer:
for {
select {
case pathConfs := <-pm.confReload:
// remove confs
for pathName := range pm.pathConfs {
if _, ok := pathConfs[pathName]; !ok {
delete(pm.pathConfs, pathName)
}
}
// update confs
for pathName, oldConf := range pm.pathConfs {
if !oldConf.Equal(pathConfs[pathName]) {
pm.pathConfs[pathName] = pathConfs[pathName]
}
}
// add confs
for pathName, pathConf := range pathConfs {
if _, ok := pm.pathConfs[pathName]; !ok {
pm.pathConfs[pathName] = pathConf
}
}
// remove paths associated with a conf which doesn't exist anymore
// or has changed
for _, pa := range pm.paths {
if pathConf, ok := pm.pathConfs[pa.ConfName()]; !ok {
delete(pm.paths, pa.Name())
pa.Close()
} else if pathConf != pa.Conf() {
delete(pm.paths, pa.Name())
pa.Close()
}
}
// add paths
pm.createPaths()
2020-10-19 20:17:48 +00:00
case pa := <-pm.pathClose:
if _, ok := pm.paths[pa.Name()]; !ok {
continue
}
2020-10-19 20:17:48 +00:00
delete(pm.paths, pa.Name())
pa.Close()
case req := <-pm.clientDescribe:
pathName, pathConf, err := pm.findPathConf(req.PathName)
2020-10-19 20:17:48 +00:00
if err != nil {
req.Res <- path.ClientDescribeRes{nil, err}
continue
}
err = req.Client.Authenticate(pm.authMethods, pathConf.ReadIpsParsed,
pathConf.ReadUser, pathConf.ReadPass, req.Req)
if err != nil {
req.Res <- path.ClientDescribeRes{nil, err}
continue
}
// create path if it doesn't exist
if _, ok := pm.paths[req.PathName]; !ok {
2020-11-01 16:33:06 +00:00
pa := path.New(pm.rtspPort, pm.readTimeout, pm.writeTimeout,
pathName, pathConf, req.PathName, &pm.wg, pm.stats, pm)
2020-10-19 20:17:48 +00:00
pm.paths[req.PathName] = pa
}
pm.paths[req.PathName].OnPathManDescribe(req)
case req := <-pm.clientAnnounce:
pathName, pathConf, err := pm.findPathConf(req.PathName)
2020-10-19 20:17:48 +00:00
if err != nil {
req.Res <- path.ClientAnnounceRes{nil, err}
continue
}
err = req.Client.Authenticate(pm.authMethods,
pathConf.PublishIpsParsed, pathConf.PublishUser, pathConf.PublishPass, req.Req)
if err != nil {
req.Res <- path.ClientAnnounceRes{nil, err}
continue
}
// create path if it doesn't exist
if _, ok := pm.paths[req.PathName]; !ok {
2020-11-01 16:33:06 +00:00
pa := path.New(pm.rtspPort, pm.readTimeout, pm.writeTimeout,
pathName, pathConf, req.PathName, &pm.wg, pm.stats, pm)
2020-10-19 20:17:48 +00:00
pm.paths[req.PathName] = pa
}
pm.paths[req.PathName].OnPathManAnnounce(req)
case req := <-pm.clientSetupPlay:
if _, ok := pm.paths[req.PathName]; !ok {
req.Res <- path.ClientSetupPlayRes{nil, fmt.Errorf("no one is publishing to path '%s'", req.PathName)}
2020-10-19 20:17:48 +00:00
continue
}
_, pathConf, err := pm.findPathConf(req.PathName)
2020-10-19 20:17:48 +00:00
if err != nil {
req.Res <- path.ClientSetupPlayRes{nil, err}
continue
}
err = req.Client.Authenticate(pm.authMethods,
pathConf.ReadIpsParsed, pathConf.ReadUser, pathConf.ReadPass, req.Req)
if err != nil {
req.Res <- path.ClientSetupPlayRes{nil, err}
continue
}
pm.paths[req.PathName].OnPathManSetupPlay(req)
case <-pm.terminate:
break outer
}
}
go func() {
for {
select {
case _, ok := <-pm.confReload:
if !ok {
return
}
2020-10-19 20:17:48 +00:00
case _, ok := <-pm.pathClose:
if !ok {
return
}
case req := <-pm.clientDescribe:
req.Res <- path.ClientDescribeRes{nil, fmt.Errorf("terminated")}
case req := <-pm.clientAnnounce:
req.Res <- path.ClientAnnounceRes{nil, fmt.Errorf("terminated")}
case req := <-pm.clientSetupPlay:
req.Res <- path.ClientSetupPlayRes{nil, fmt.Errorf("terminated")}
}
}
}()
for _, pa := range pm.paths {
pa.Close()
}
pm.wg.Wait()
close(pm.confReload)
2020-10-19 20:17:48 +00:00
close(pm.clientClose)
close(pm.pathClose)
close(pm.clientDescribe)
close(pm.clientAnnounce)
close(pm.clientSetupPlay)
}
func (pm *PathManager) createPaths() {
for pathName, pathConf := range pm.pathConfs {
if pathConf.Regexp == nil {
2020-11-01 16:33:06 +00:00
pa := path.New(pm.rtspPort, pm.readTimeout, pm.writeTimeout,
pathName, pathConf, pathName, &pm.wg, pm.stats, pm)
pm.paths[pathName] = pa
}
}
}
func (pm *PathManager) findPathConf(name string) (string, *conf.PathConf, error) {
2020-10-19 20:17:48 +00:00
err := conf.CheckPathName(name)
if err != nil {
return "", nil, fmt.Errorf("invalid path name: %s (%s)", err, name)
2020-10-19 20:17:48 +00:00
}
// normal path
if pathConf, ok := pm.pathConfs[name]; ok {
return name, pathConf, nil
2020-10-19 20:17:48 +00:00
}
// regular expression path
for pathName, pathConf := range pm.pathConfs {
2020-10-19 20:17:48 +00:00
if pathConf.Regexp != nil && pathConf.Regexp.MatchString(name) {
return pathName, pathConf, nil
2020-10-19 20:17:48 +00:00
}
}
return "", nil, fmt.Errorf("unable to find a valid configuration for path '%s'", name)
}
2020-11-05 11:30:25 +00:00
// OnProgramConfReload is called by program.
func (pm *PathManager) OnProgramConfReload(pathConfs map[string]*conf.PathConf) {
pm.confReload <- pathConfs
2020-10-19 20:17:48 +00:00
}
2020-11-05 11:30:25 +00:00
// OnPathClose is called by path.Path.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) OnPathClose(pa *path.Path) {
pm.pathClose <- pa
}
2020-11-05 11:30:25 +00:00
// OnPathClientClose is called by path.Path.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) OnPathClientClose(c *client.Client) {
pm.clientClose <- c
}
2020-11-05 11:30:25 +00:00
// OnClientDescribe is called by client.Client.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) OnClientDescribe(c *client.Client, pathName string, req *base.Request) (client.Path, error) {
res := make(chan path.ClientDescribeRes)
pm.clientDescribe <- path.ClientDescribeReq{res, c, pathName, req}
re := <-res
return re.Path, re.Err
}
2020-11-05 11:30:25 +00:00
// OnClientAnnounce is called by client.Client.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) OnClientAnnounce(c *client.Client, pathName string, tracks gortsplib.Tracks, req *base.Request) (client.Path, error) {
res := make(chan path.ClientAnnounceRes)
pm.clientAnnounce <- path.ClientAnnounceReq{res, c, pathName, tracks, req}
re := <-res
return re.Path, re.Err
}
2020-11-05 11:30:25 +00:00
// OnClientSetupPlay is called by client.Client.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) OnClientSetupPlay(c *client.Client, pathName string, trackId int, req *base.Request) (client.Path, error) {
res := make(chan path.ClientSetupPlayRes)
pm.clientSetupPlay <- path.ClientSetupPlayReq{res, c, pathName, trackId, req}
re := <-res
return re.Path, re.Err
}
2020-11-05 11:30:25 +00:00
// ClientClose is called by client.Client.
2020-10-19 20:17:48 +00:00
func (pm *PathManager) ClientClose() chan *client.Client {
return pm.clientClose
}