Skip to content

Commit

Permalink
fix execution of external commands with quotes on Windows (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Nov 8, 2020
1 parent 7a3db78 commit 2c9e073
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 70 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
56 changes: 0 additions & 56 deletions internal/externalcmd/externalcmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package externalcmd

import (
"os"
"os/exec"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -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
}
}
41 changes: 41 additions & 0 deletions internal/externalcmd/externalcmd_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
}
57 changes: 57 additions & 0 deletions internal/externalcmd/externalcmd_win.go
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 0 additions & 13 deletions internal/syslog/syslog_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2c9e073

Please # to comment.