From cdda774faa44be47d7a62b488496e7053c38a59e Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 30 Aug 2020 13:18:43 +0200 Subject: [PATCH] allow onInitCmd only if path is not 'all' --- conf.go | 5 ++++- main.go | 34 +++++++++------------------------- path.go | 30 +++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/conf.go b/conf.go index f4a2e4af..6ba9c0c1 100644 --- a/conf.go +++ b/conf.go @@ -265,7 +265,10 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) { return nil, err } - if confp.RunOnDemand != "" && name == "all" { + if name == "all" && confp.RunOnInit != "" { + return nil, fmt.Errorf("path 'all' does not support option 'runOnInit'; use another path") + } + if name == "all" && confp.RunOnDemand != "" { return nil, fmt.Errorf("path 'all' does not support option 'runOnDemand'; use another path") } } diff --git a/main.go b/main.go index b0da6abd..73480ebd 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "net/http" _ "net/http/pprof" "os" - "os/exec" "time" "github.com/aler9/gortsplib" @@ -173,7 +172,6 @@ type program struct { clients map[*client]struct{} udpClientsByAddr map[udpClientAddr]*udpClient paths map[string]*path - cmds []*exec.Cmd publisherCount int readerCount int @@ -264,23 +262,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { return nil, err } - for name, confp := range conf.Paths { - if confp.RunOnInit != "" { - onInitCmd := exec.Command("/bin/sh", "-c", confp.RunOnInit) - onInitCmd.Env = append(os.Environ(), - "RTSP_SERVER_PATH="+name, - ) - onInitCmd.Stdout = os.Stdout - onInitCmd.Stderr = os.Stderr - err := onInitCmd.Start() - if err != nil { - p.log("ERR: %s", err) - } - - p.cmds = append(p.cmds, onInitCmd) - } - } - if p.metrics != nil { go p.metrics.run() } @@ -296,6 +277,10 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { go p.serverRtsp.run() + for _, p := range p.paths { + p.onInit() + } + go p.run() return p, nil @@ -322,7 +307,7 @@ outer: select { case <-checkPathsTicker.C: for _, path := range p.paths { - path.check() + path.onCheck() } case rawEvt := <-p.events: @@ -542,11 +527,6 @@ outer: } }() - for _, cmd := range p.cmds { - cmd.Process.Signal(os.Interrupt) - cmd.Wait() - } - p.serverRtsp.close() for _, s := range p.sources { @@ -564,6 +544,10 @@ outer: <-c.done } + for _, p := range p.paths { + p.onClose() + } + if p.metrics != nil { p.metrics.close() } diff --git a/path.go b/path.go index 0eef9d9e..8062b3f0 100644 --- a/path.go +++ b/path.go @@ -25,6 +25,7 @@ type path struct { publisherSdpParsed *sdp.SessionDescription lastRequested time.Time lastActivation time.Time + onInitCmd *exec.Cmd onDemandCmd *exec.Cmd } @@ -39,7 +40,34 @@ func newPath(p *program, name string, confp *confPath, permanent bool) *path { return pa } -func (pa *path) check() { +func (pa *path) onInit() { + if pa.confp.RunOnInit != "" { + pa.onInitCmd = exec.Command("/bin/sh", "-c", pa.confp.RunOnInit) + pa.onInitCmd.Env = append(os.Environ(), + "RTSP_SERVER_PATH="+pa.name, + ) + pa.onInitCmd.Stdout = os.Stdout + pa.onInitCmd.Stderr = os.Stderr + err := pa.onInitCmd.Start() + if err != nil { + pa.p.log("ERR: %s", err) + } + } +} + +func (pa *path) onClose() { + if pa.onInitCmd != nil { + pa.onInitCmd.Process.Signal(os.Interrupt) + pa.onInitCmd.Wait() + } + + if pa.onDemandCmd != nil { + pa.onDemandCmd.Process.Signal(os.Interrupt) + pa.onDemandCmd.Wait() + } +} + +func (pa *path) onCheck() { hasClientsWaitingDescribe := func() bool { for c := range pa.p.clients { if c.state == clientStateWaitingDescription && c.pathName == pa.name {