diff --git a/README.md b/README.md index 7d663903..100b9f0b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod index ea30ab4d..a270f760 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 24b29874..27064b2a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/externalcmd/externalcmd.go b/internal/externalcmd/externalcmd.go index 88bd5678..ea5565f7 100644 --- a/internal/externalcmd/externalcmd.go +++ b/internal/externalcmd/externalcmd.go @@ -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 - } -} diff --git a/internal/externalcmd/externalcmd_unix.go b/internal/externalcmd/externalcmd_unix.go new file mode 100644 index 00000000..418f9c1b --- /dev/null +++ b/internal/externalcmd/externalcmd_unix.go @@ -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 + } +} diff --git a/internal/externalcmd/externalcmd_win.go b/internal/externalcmd/externalcmd_win.go new file mode 100644 index 00000000..1c4c5535 --- /dev/null +++ b/internal/externalcmd/externalcmd_win.go @@ -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 + } +} diff --git a/internal/syslog/syslog_win.go b/internal/syslog/syslog_win.go index 7f23156b..de070cc0 100644 --- a/internal/syslog/syslog_win.go +++ b/internal/syslog/syslog_win.go @@ -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 -}