Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy, record and playback video and audio streams.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

55 lines
1.1 KiB

package externalcmd
import (
"os"
"os/exec"
"runtime"
"strings"
)
type ExternalCmd struct {
cmd *exec.Cmd
}
func New(cmdstr string, pathName string) (*ExternalCmd, error) {
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 allow
// compatibility with linux commands
cmdstr = strings.ReplaceAll(cmdstr, "$RTSP_SERVER_PATH", pathName)
args := strings.Fields(cmdstr)
cmd = exec.Command(args[0], args[1:]...)
} else {
cmd = exec.Command("/bin/sh", "-c", "exec "+cmdstr)
}
// variables are available through environment variables
cmd.Env = append(os.Environ(),
"RTSP_SERVER_PATH="+pathName,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return nil, err
}
return &ExternalCmd{
cmd: cmd,
}, nil
}
func (e *ExternalCmd) Close() {
// on Windows it's not possible to send os.Interrupt to a process
// Kill() is the only supported way
if runtime.GOOS == "windows" {
e.cmd.Process.Kill()
} else {
e.cmd.Process.Signal(os.Interrupt)
}
e.cmd.Wait()
}