mediamtx/externalcmd/externalcmd.go

124 lines
2.0 KiB
Go
Raw Normal View History

package externalcmd
import (
"os"
"os/exec"
"runtime"
"strings"
"time"
)
const (
retryPause = 5 * time.Second
)
type ExternalCmd struct {
cmdstr string
restart bool
pathName string
// in
terminate chan struct{}
// out
done chan struct{}
}
func New(cmdstr string, restart bool, pathName string) *ExternalCmd {
e := &ExternalCmd{
cmdstr: cmdstr,
restart: restart,
pathName: pathName,
terminate: make(chan struct{}),
done: make(chan struct{}),
}
go e.run()
return e
}
func (e *ExternalCmd) Close() {
close(e.terminate)
<-e.done
}
func (e *ExternalCmd) run() {
defer close(e.done)
for {
2020-10-31 16:03:03 +00:00
ok := func() bool {
ok := e.runInner()
if !ok {
return false
}
2020-10-31 16:03:03 +00:00
if !e.restart {
<-e.terminate
return false
}
t := time.NewTimer(retryPause)
2020-10-31 16:03:03 +00:00
defer t.Stop()
2020-10-31 16:03:03 +00:00
select {
case <-t.C:
return true
case <-e.terminate:
return false
}
}()
if !ok {
break
}
}
}
2020-10-31 16:03:03 +00:00
func (e *ExternalCmd) runInner() bool {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
// on Windows the shell is not used and command is started directly
// variables are replaced manually in order to guarantee compatibility
// with Linux commands
args := strings.Fields(strings.ReplaceAll(e.cmdstr, "$RTSP_PATH", e.pathName))
cmd = exec.Command(args[0], args[1:]...)
} else {
cmd = exec.Command("/bin/sh", "-c", "exec "+e.cmdstr)
}
// variables are inserted into the environment
cmd.Env = append(os.Environ(),
"RTSP_PATH="+e.pathName,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return true
}
cmdDone := make(chan struct{})
go func() {
defer close(cmdDone)
cmd.Wait()
}()
select {
case <-e.terminate:
// on Windows it's not possible to send os.Interrupt to a process
// Kill() is the only supported way
if runtime.GOOS == "windows" {
cmd.Process.Kill()
} else {
cmd.Process.Signal(os.Interrupt)
}
<-cmdDone
return false
case <-cmdDone:
return true
}
}