From 0b589b3e7eb0f707f2681cdd0d3fb51b97840d81 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 30 Aug 2020 14:15:00 +0200 Subject: [PATCH] move sources inside paths --- main.go | 37 ++++++++++--------------------------- path.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index ddc900d1..af51ba98 100644 --- a/main.go +++ b/main.go @@ -160,13 +160,12 @@ type program struct { logFile *os.File metrics *metrics pprof *pprof - serverRtsp *serverTcp + paths map[string]*path serverRtp *serverUdp serverRtcp *serverUdp - sources []*source + serverRtsp *serverTcp clients map[*client]struct{} udpClientsByAddr map[udpClientAddr]*udpClient - paths map[string]*path publisherCount int readerCount int @@ -195,9 +194,9 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { p := &program{ conf: conf, + paths: make(map[string]*path), clients: make(map[*client]struct{}), udpClientsByAddr: make(map[udpClientAddr]*udpClient), - paths: make(map[string]*path), events: make(chan programEvent), done: make(chan struct{}), } @@ -211,20 +210,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { p.log("rtsp-simple-server %s", Version) - for name, confp := range conf.Paths { - if name == "all" { - continue - } - - p.paths[name] = newPath(p, name, confp, true) - - if confp.Source != "record" { - s := newSource(p, name, confp) - p.sources = append(p.sources, s) - p.paths[name].publisher = s - } - } - if conf.Metrics { p.metrics, err = newMetrics(p) if err != nil { @@ -239,6 +224,13 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { } } + for name, confp := range conf.Paths { + if name == "all" { + continue + } + p.paths[name] = newPath(p, name, confp, true) + } + if _, ok := conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok { p.serverRtp, err = newServerUdp(p, conf.RtpPort, gortsplib.StreamTypeRtp) if err != nil { @@ -271,10 +263,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { go p.serverRtsp.run() - for _, s := range p.sources { - go s.run() - } - for _, p := range p.paths { p.onInit() } @@ -532,11 +520,6 @@ outer: p.serverRtsp.close() - for _, s := range p.sources { - s.events <- sourceEventTerminate{} - <-s.done - } - if _, ok := p.conf.protocolsParsed[gortsplib.StreamProtocolUdp]; ok { p.serverRtcp.close() p.serverRtp.close() diff --git a/path.go b/path.go index 967011cb..e007c6d3 100644 --- a/path.go +++ b/path.go @@ -19,6 +19,7 @@ type path struct { name string confp *confPath permanent bool + source *source publisher publisher publisherReady bool publisherSdpText []byte @@ -37,10 +38,20 @@ func newPath(p *program, name string, confp *confPath, permanent bool) *path { permanent: permanent, } + if confp.Source != "record" { + s := newSource(p, name, confp) + pa.source = s + pa.publisher = s + } + return pa } func (pa *path) onInit() { + if pa.source != nil { + go pa.source.run() + } + if pa.confp.RunOnInit != "" { pa.p.log("starting on init command") @@ -58,6 +69,11 @@ func (pa *path) onInit() { } func (pa *path) onClose() { + if pa.source != nil { + pa.source.events <- sourceEventTerminate{} + <-pa.source.done + } + if pa.onInitCmd != nil { pa.p.log("stopping on init command (exited)") pa.onInitCmd.Process.Signal(os.Interrupt)