fix execution of external commands with quotes on Windows (#113)

This commit is contained in:
aler9 2020-11-08 23:12:43 +01:00
parent 7a3db78de4
commit 2c9e07376c
7 changed files with 115 additions and 70 deletions

View File

@ -119,7 +119,20 @@ paths:
runOnInitRestart: yes
```
After starting the server, the webcam is available on `rtsp://localhost:8554/cam`. The ffmpeg command works only on Linux; for Windows and Mac equivalents, read the [ffmpeg wiki](https://trac.ffmpeg.org/wiki/Capture/Webcam).
If the platform is Windows:
```yml
paths:
cam:
runOnInit: ffmpeg -f dshow -i video="USB2.0 HD UVC WebCam" -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH
runOnInitRestart: yes
```
Where `USB2.0 HD UVC WebCam` is the name of your webcam, that can be obtained with:
```
ffmpeg -list_devices true -f dshow -i dummy
```
After starting the server, the webcam can be reached on `rtsp://localhost:8554/cam`.
### Serve a Raspberry Pi Camera

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/aler9/gortsplib v0.0.0-20201108190150-2deddcffab35
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/notedit/rtmp v0.0.2
github.com/pion/rtp v1.6.1 // indirect
github.com/stretchr/testify v1.6.1

2
go.sum
View File

@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/notedit/rtmp v0.0.2 h1:5+to4yezKATiJgnrcETu9LbV5G/QsWkOV9Ts2M/p33w=
github.com/notedit/rtmp v0.0.2/go.mod h1:vzuE21rowz+lT1NGsWbreIvYulgBpCGnQyeTyFblUHc=
github.com/pion/randutil v0.0.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=

View File

@ -1,10 +1,6 @@
package externalcmd
import (
"os"
"os/exec"
"runtime"
"strings"
"time"
)
@ -81,55 +77,3 @@ func (e *ExternalCmd) run() {
}
}
}
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
tmp := strings.ReplaceAll(e.cmdstr, "$RTSP_PATH", e.env.Path)
tmp = strings.ReplaceAll(tmp, "$RTSP_PORT", e.env.Port)
args := strings.Fields(tmp)
cmd = exec.Command(args[0], args[1:]...)
} else {
cmd = exec.Command("/bin/sh", "-c", "exec "+e.cmdstr)
}
cmd.Env = append(os.Environ(),
"RTSP_PATH="+e.env.Path,
"RTSP_PORT="+e.env.Port,
)
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
}
}

View File

@ -0,0 +1,41 @@
// +build !windows
package externalcmd
import (
"os"
"os/exec"
)
func (e *ExternalCmd) runInner() bool {
cmd := exec.Command("/bin/sh", "-c", "exec "+e.cmdstr)
cmd.Env = append(os.Environ(),
"RTSP_PATH="+e.env.Path,
"RTSP_PORT="+e.env.Port,
)
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:
cmd.Process.Signal(os.Interrupt)
<-cmdDone
return false
case <-cmdDone:
return true
}
}

View File

@ -0,0 +1,57 @@
// +build windows
package externalcmd
import (
"os"
"os/exec"
"strings"
"github.com/kballard/go-shellquote"
)
func (e *ExternalCmd) runInner() bool {
// on Windows the shell is not used and command is started directly
// variables are replaced manually in order to guarantee compatibility
// with Linux commands
tmp := strings.ReplaceAll(e.cmdstr, "$RTSP_PATH", e.env.Path)
tmp = strings.ReplaceAll(tmp, "$RTSP_PORT", e.env.Port)
parts, err := shellquote.Split(tmp)
if err != nil {
return true
}
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Env = append(os.Environ(),
"RTSP_PATH="+e.env.Path,
"RTSP_PORT="+e.env.Port,
)
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
cmd.Process.Kill()
<-cmdDone
return false
case <-cmdDone:
return true
}
}

View File

@ -7,20 +7,7 @@ import (
"io"
)
type syslog struct {
}
// New allocates a io.WriteCloser that writes to the system log.
func New(prefix string) (io.WriteCloser, error) {
return nil, fmt.Errorf("not implemented on windows")
}
// Close implements io.WriteCloser.
func (ls *syslog) Close() error {
return nil
}
// Write implements io.WriteCloser.
func (ls *syslog) Write(p []byte) (int, error) {
return 0, nil
}