From e3c7fb6ca48cbb8d8abdedd73b8c128ec3df38d2 Mon Sep 17 00:00:00 2001 From: Tomohiro Matsuzawa Date: Fri, 3 Apr 2020 21:42:23 +0900 Subject: [PATCH] added example to use SRT Access Control --- README.md | 69 ++++++++++++++++++----------------- examples/livetransmit/main.go | 26 +++++++++++++ 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 04bbdec..a54f1de 100644 --- a/README.md +++ b/README.md @@ -44,44 +44,47 @@ tc, err := d.DialContext(ctx, "srt", "127.0.0.1:5001") Following table show how gosrt option corresponds to SRT C API options. -| gosrt option | SRT C API option | -|---------------|--------------------| -| transtype | SRTO_TRANSTYPE | -| maxbw | SRTO_MAXBW | -| pbkeylen | SRTO_PBKEYLEN | -| passphrase | SRTO_PASSPHRASE | -| mss | SRTO_MSS | -| fc | SRTO_FC | -| sndbuf | SRTO_SNDBUF | -| rcvbuf | SRTO_RCVBUF | -| ipttl | SRTO_IPTTL | -| iptos | SRTO_IPTOS | -| inputbw | SRTO_INPUTBW | -| oheadbw | SRTO_OHEADBW | -| latency | SRTO_LATENCY | -| tsbpdmode | SRTO_TSBPDMODE | -| tlpktdrop | SRTO_TLPKTDROP | -| snddropdelay | SRTO_SNDDROPDELAY | -| nakreport | SRTO_NAKREPORT | -| conntimeo | SRTO_CONNTIMEO | -| lossmaxttl | SRTO_LOSSMAXTTL | -| rcvlatency | SRTO_RCVLATENCY | -| peerlatency | SRTO_PEERLATENCY | -| minversion | SRTO_MINVERSION | -| streamid | SRTO_STREAMID | -| congestion | SRTO_CONGESTION | -| messageapi | SRTO_MESSAGEAPI | -| payloadsize | SRTO_PAYLOADSIZE | -| kmrefreshrate | SRTO_KMREFRESHRATE | -| kmpreannounce | SRTO_KMPREANNOUNCE | -| strictenc | SRTO_STRICTENC | +| gosrt option | SRT C API option | +|--------------------|-------------------------| +| transtype | SRTO_TRANSTYPE | +| maxbw | SRTO_MAXBW | +| pbkeylen | SRTO_PBKEYLEN | +| passphrase | SRTO_PASSPHRASE | +| mss | SRTO_MSS | +| fc | SRTO_FC | +| sndbuf | SRTO_SNDBUF | +| rcvbuf | SRTO_RCVBUF | +| ipttl | SRTO_IPTTL | +| iptos | SRTO_IPTOS | +| inputbw | SRTO_INPUTBW | +| oheadbw | SRTO_OHEADBW | +| latency | SRTO_LATENCY | +| tsbpdmode | SRTO_TSBPDMODE | +| tlpktdrop | SRTO_TLPKTDROP | +| snddropdelay | SRTO_SNDDROPDELAY | +| nakreport | SRTO_NAKREPORT | +| conntimeo | SRTO_CONNTIMEO | +| lossmaxttl | SRTO_LOSSMAXTTL | +| rcvlatency | SRTO_RCVLATENCY | +| peerlatency | SRTO_PEERLATENCY | +| minversion | SRTO_MINVERSION | +| streamid | SRTO_STREAMID | +| congestion | SRTO_CONGESTION | +| messageapi | SRTO_MESSAGEAPI | +| payloadsize | SRTO_PAYLOADSIZE | +| kmrefreshrate | SRTO_KMREFRESHRATE | +| kmpreannounce | SRTO_KMPREANNOUNCE | +| enforcedencryption | SRTO_ENFORCEDENCRYPTION | +| peeridletimeo | SRTO_PEERIDLETIMEO | +| packetfilter | SRTO_PACKETFILTER | ## Run the Example app with Docker The example app receives SRT packets and sends them to the target address specified in .env file. In the following steps, you can send a test stream from ffmpeg to the gosrt example app, and ffplay play it. 1. Install ffmpeg with srt support ```sh -$ brew install ffmpeg --with-srt --with-fontconfig --with-freetype +$ brew tap homebrew-ffmpeg/ffmpeg +$ brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-srt ``` 2. Run ffplay @@ -100,5 +103,5 @@ $ docker-compose up $ ffmpeg -re -f lavfi -i testsrc=size=1280x720:rate=30 -f lavfi -i sine \ -vf drawtext="text='%{localtime\:%X}':fontsize=20:fontcolor=white:x=7:y=7" \ -vcodec libx264 -vb 2000k -preset ultrafast -x264-params keyint=60 \ --acodec aac -f mpegts srt://127.0.0.1:5000 +-acodec aac -f mpegts 'srt://127.0.0.1:5000?streamid=#!::u=johnny,t=file,m=publish,r=results.csv' ``` diff --git a/examples/livetransmit/main.go b/examples/livetransmit/main.go index 6084b09..c655a11 100644 --- a/examples/livetransmit/main.go +++ b/examples/livetransmit/main.go @@ -56,6 +56,32 @@ func main() { log.Fatal(err) } fmt.Printf("accepted: %s\n", conn.RemoteAddr()) + sc := conn.(*srt.SRTConn) + if sc == nil { + fmt.Printf("conn is not srtConn: %s\n", conn.RemoteAddr()) + } else { + username := "" + streamID, err := sc.StreamID() + if err != nil { + log.Fatal(err) + } + if strings.HasPrefix(streamID, "#!::") { + items := strings.Split(streamID[4:], ",") + for i := range items { + kv := strings.Split(items[i], "=") + if len(kv) == 2 && kv[0] == "u" { + username = kv[1] + } + } + if username == "" { + fmt.Println("USER NOT FOUND") + } + } else { + // By default the whole streamid is username + username = streamID + } + fmt.Printf("username is %s\n", username) + } target := "" if i < len(targets) { target = targets[i]