add runOnInit

This commit is contained in:
aler9 2020-07-31 18:12:42 +02:00
parent 8305967c07
commit 8ffa1d53e9
4 changed files with 36 additions and 22 deletions

View File

@ -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
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
```
Publish the webcam:
```
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
@ -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

View File

@ -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"`
@ -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 == "" {
@ -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")
}
}

21
main.go
View File

@ -8,6 +8,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"time"
"github.com/aler9/gortsplib"
@ -176,6 +177,7 @@ type program struct {
sources []*source
clients map[*client]struct{}
paths map[string]*path
cmds []*exec.Cmd
publisherCount int
readerCount int
@ -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()
}
@ -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

View File

@ -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.