allow onInitCmd only if path is not 'all'

This commit is contained in:
aler9 2020-08-30 13:18:43 +02:00
parent fc8bf8c830
commit cdda774faa
3 changed files with 42 additions and 27 deletions

View file

@ -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")
}
}

34
main.go
View file

@ -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()
}

30
path.go
View file

@ -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 {