Skip to content

Commit

Permalink
Merge pull request #2 from tekdoc/script
Browse files Browse the repository at this point in the history
Add option to run a script when client connects or disconnects (Linux…
  • Loading branch information
aler9 authored Feb 16, 2020
2 parents 6fefb89 + 7f90724 commit a17cd5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Features:
* Each stream can have multiple video and audio tracks
* Supports the RTP/RTCP streaming protocol
* Optional authentication mechanism for publishers
* Run a script when a client connects or disconnects
* Compatible with Linux and Windows, does not require any dependency or interpreter, it's a single executable

## Installation
Expand Down Expand Up @@ -73,6 +74,8 @@ Flags:
--rtp-port=8000 port of the RTP UDP listener
--rtcp-port=8001 port of the RTCP UDP listener
--publish-key="" optional authentication key required to publish
--pre-script="" optional script to run on client connect
--post-script="" optional script to run on client disconnect
```
## Links
Expand Down
20 changes: 20 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"net/url"
"os/exec"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -162,7 +163,18 @@ func (c *client) log(format string, args ...interface{}) {
}

func (c *client) run() {
defer func() {
if c.p.postScript != "" {
postScript := exec.Command(c.p.postScript)
err := postScript.Run()
if err != nil {
c.log("ERR: %s", err)
}
}
}()

defer c.log("disconnected")

defer func() {
c.p.mutex.Lock()
defer c.p.mutex.Unlock()
Expand All @@ -174,6 +186,14 @@ func (c *client) run() {

c.log("connected")

if c.p.preScript != "" {
preScript := exec.Command(c.p.preScript)
err := preScript.Run()
if err != nil {
c.log("ERR: %s", err)
}
}

for {
req, err := c.conn.ReadRequest()
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type program struct {
rtpPort int
rtcpPort int
publishKey string
preScript string
postScript string
mutex sync.RWMutex
rtspl *serverTcpListener
rtpl *serverUdpListener
Expand All @@ -61,7 +63,7 @@ type program struct {
publishers map[string]*client
}

func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, publishKey string) (*program, error) {
func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, publishKey string, preScript string, postScript string) (*program, error) {

if rtspPort == 0 {
return nil, fmt.Errorf("rtsp port not provided")
Expand Down Expand Up @@ -114,6 +116,8 @@ func newProgram(protocolsStr string, rtspPort int, rtpPort int, rtcpPort int, pu
rtpPort: rtpPort,
rtcpPort: rtcpPort,
publishKey: publishKey,
preScript: preScript,
postScript: postScript,
clients: make(map[*client]struct{}),
publishers: make(map[string]*client),
}
Expand Down Expand Up @@ -189,6 +193,8 @@ func main() {
rtpPort := kingpin.Flag("rtp-port", "port of the RTP UDP listener").Default("8000").Int()
rtcpPort := kingpin.Flag("rtcp-port", "port of the RTCP UDP listener").Default("8001").Int()
publishKey := kingpin.Flag("publish-key", "optional authentication key required to publish").Default("").String()
preScript := kingpin.Flag("pre-script", "optional script to run on client connect").Default("").String()
postScript := kingpin.Flag("post-script", "optional script to run on client disconnect").Default("").String()

kingpin.Parse()

Expand All @@ -197,7 +203,7 @@ func main() {
os.Exit(0)
}

p, err := newProgram(*protocols, *rtspPort, *rtpPort, *rtcpPort, *publishKey)
p, err := newProgram(*protocols, *rtspPort, *rtpPort, *rtcpPort, *publishKey, *preScript, *postScript)
if err != nil {
log.Fatal("ERR: ", err)
}
Expand Down

0 comments on commit a17cd5d

Please sign in to comment.