Skip to content

Commit

Permalink
add runOnInit
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 31, 2020
1 parent 8305967 commit 8ffa1d5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
27 changes: 7 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,18 @@ paths:
source: rtsp://original-url
```
Start the server:
```
./rtsp-simple-server
```

Users can then connect to `rtsp://localhost:8554/proxied`, instead of connecting to the original url. The server supports any number of source streams, it's enough to add additional entries to the `paths` section.
After starting the server, users can connect to `rtsp://localhost:8554/proxied`, instead of connecting to the original url. The server supports any number of source streams, it's enough to add additional entries to the `paths` section.

#### Convert a webcam into a RTSP server

Start the server:
```
./rtsp-simple-server
```

Publish the webcam:
```
ffmpeg -f v4l2 -i /dev/video0 -f rtsp rtsp://localhost:8554/mystream
Edit `rtsp-simple-server.yml` and replace everything inside section `paths` with the following content:
```yaml
paths:
webcam:
runOnInit: ffmpeg -f v4l2 -i /dev/video0 -f rtsp rtsp://localhost:8554/mystream
```

The last command works only on Linux; for Windows and Mac equivalents, read the [ffmpeg wiki](https://trac.ffmpeg.org/wiki/Capture/Webcam).
After starting the server, the webcam can be opened with `rtsp://localhost:8554/webcam`. The ffmpeg command works only on Linux; for Windows and Mac equivalents, read the [ffmpeg wiki](https://trac.ffmpeg.org/wiki/Capture/Webcam).

#### On-demand publishing

Expand Down Expand Up @@ -141,11 +133,6 @@ paths:
publishPass: mypassword
```

Start the server:
```
./rtsp-simple-server
```

Only publishers that provide both username and password will be able to proceed:
```
ffmpeg -re -stream_loop -1 -i file.ts -c copy -f rtsp rtsp://admin:mypassword@localhost:8554/mystream
Expand Down
5 changes: 3 additions & 2 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type confPath struct {
SourceProtocol string `yaml:"sourceProtocol"`
sourceProtocolParsed gortsplib.StreamProtocol ``
SourceOnDemand bool `yaml:"sourceOnDemand"`
RunOnInit string `yaml:"runOnInit"`
RunOnDemand string `yaml:"runOnDemand"`
RunOnPublish string `yaml:"runOnPublish"`
RunOnRead string `yaml:"runOnRead"`
Expand Down Expand Up @@ -185,7 +186,7 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {

if confp.Source != "record" {
if path == "all" {
return nil, fmt.Errorf("path 'all' cannot have a RTSP source")
return nil, fmt.Errorf("path 'all' cannot have a RTSP source; use another path")
}

if confp.SourceProtocol == "" {
Expand Down Expand Up @@ -260,7 +261,7 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
}

if confp.RunOnDemand != "" && path == "all" {
return nil, fmt.Errorf("option 'runOnDemand' cannot be used in path 'all'")
return nil, fmt.Errorf("path 'all' does not support option 'runOnDemand'; use another path")
}
}

Expand Down
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"time"

"github.com/aler9/gortsplib"
Expand Down Expand Up @@ -176,6 +177,7 @@ type program struct {
sources []*source
clients map[*client]struct{}
paths map[string]*path
cmds []*exec.Cmd
publisherCount int
readerCount int

Expand Down Expand Up @@ -263,6 +265,20 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
return nil, err
}

for _, confp := range conf.Paths {
if confp.RunOnInit != "" {
onInitCmd := exec.Command("/bin/sh", "-c", confp.RunOnInit)
onInitCmd.Stdout = os.Stdout
onInitCmd.Stderr = os.Stderr
err := onInitCmd.Start()
if err != nil {
p.log("ERR: %s", err)
}

p.cmds = append(p.cmds, onInitCmd)
}
}

if p.metrics != nil {
go p.metrics.run()
}
Expand Down Expand Up @@ -495,6 +511,11 @@ outer:
}
}()

for _, cmd := range p.cmds {
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
}

for _, s := range p.sources {
s.events <- sourceEventTerminate{}
<-s.done
Expand Down
5 changes: 5 additions & 0 deletions rtsp-simple-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ paths:
# is connected, saving bandwidth
sourceOnDemand: no

# command to run when this path is loaded by the program.
# this can be used, for example, to publish a stream and keep it always opened.
# This is terminated with SIGINT when the program closes.
runOnInit:

# command to run when this path is requested.
# This can be used, for example, to publish a stream on demand.
# This is terminated with SIGINT when the path is not requested anymore.
Expand Down

0 comments on commit 8ffa1d5

Please sign in to comment.