From d4aa09c63241205dd84a933f7cdccbabf3053906 Mon Sep 17 00:00:00 2001
From: Alexandre Iooss <erdnaxe@crans.org>
Date: Sun, 27 Sep 2020 11:14:22 +0200
Subject: [PATCH] Add basic SRT support

---
 docs/ghostream.example.yml |  3 +++
 go.mod                     |  2 ++
 go.sum                     |  4 ++++
 main.go                    |  4 ++++
 stream/srt/srt.go          | 37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 50 insertions(+)
 create mode 100644 stream/srt/srt.go

diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml
index a3162ec..9dce37d 100644
--- a/docs/ghostream.example.yml
+++ b/docs/ghostream.example.yml
@@ -28,3 +28,6 @@ web:
   hostname: localhost
   favicon: https://www.crans.org/images/favicon.ico
   widgetURL: https://example.com/
+
+srt:
+  listenAddress: 127.0.0.1:9710
\ No newline at end of file
diff --git a/go.mod b/go.mod
index 4ba4a8a..7d9ecc8 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,8 @@ go 1.13
 require (
 	github.com/go-ldap/ldap v3.0.3+incompatible
 	github.com/go-ldap/ldap/v3 v3.2.3
+	github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a
+	github.com/openfresh/gosrt v0.2.0
 	github.com/pion/webrtc/v3 v3.0.0-beta.5
 	github.com/prometheus/client_golang v1.7.1
 	github.com/spf13/viper v1.7.1
diff --git a/go.sum b/go.sum
index 128b952..a0f0996 100644
--- a/go.sum
+++ b/go.sum
@@ -130,6 +130,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
 github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
 github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
+github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a h1:JliMkv/mAqM5+QzG6Hkw1XcVl1crU8yIQGnhppMv7s0=
+github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a/go.mod h1:yVZ4oACfcnUAcxrh+0b6IuIWfkHLK3IAQ99tuuhRx54=
 github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
 github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
 github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -206,6 +208,8 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
 github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
 github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
 github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
+github.com/openfresh/gosrt v0.2.0 h1:L8Nq5k9G/88YxdBDorcFi+jxxfJdUmMv0151bCmYBKw=
+github.com/openfresh/gosrt v0.2.0/go.mod h1:lSCk9gKdRrO0xHbS3LlVgoofxId8QF56jmxGwBX3XlM=
 github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
 github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
 github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
diff --git a/main.go b/main.go
index 1c2b317..8418427 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
 	"gitlab.crans.org/nounous/ghostream/auth"
 	"gitlab.crans.org/nounous/ghostream/internal/monitoring"
 	"gitlab.crans.org/nounous/ghostream/stream"
+	"gitlab.crans.org/nounous/ghostream/stream/srt"
 	"gitlab.crans.org/nounous/ghostream/web"
 )
 
@@ -45,6 +46,7 @@ func loadConfiguration() {
 	viper.SetDefault("Auth.LDAP.URI", "ldap://127.0.0.1:389")
 	viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com")
 	viper.SetDefault("Monitoring.ListenAddress", ":2112")
+	viper.SetDefault("Srt.ListenAddress", ":9710")
 	viper.SetDefault("Web.ListenAddress", "127.0.0.1:8080")
 	viper.SetDefault("Web.Name", "Ghostream")
 	viper.SetDefault("Web.Hostname", "localhost")
@@ -57,6 +59,7 @@ func main() {
 	cfg := struct {
 		Auth       auth.Options
 		Monitoring monitoring.Options
+		Srt        srt.Options
 		Web        web.Options
 	}{}
 	if err := viper.Unmarshal(&cfg); err != nil {
@@ -75,6 +78,7 @@ func main() {
 	localSdpChan := make(chan webrtc.SessionDescription)
 
 	// Start stream, web and monitoring server
+	go srt.Serve(&cfg.Srt)
 	go stream.Serve(remoteSdpChan, localSdpChan)
 	go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web)
 	go monitoring.Serve(&cfg.Monitoring)
diff --git a/stream/srt/srt.go b/stream/srt/srt.go
new file mode 100644
index 0000000..2aa79f7
--- /dev/null
+++ b/stream/srt/srt.go
@@ -0,0 +1,37 @@
+package srt
+
+import (
+	"log"
+	"net"
+
+	"github.com/openfresh/gosrt/srt"
+)
+
+// Options holds web package configuration
+type Options struct {
+	ListenAddress string
+}
+
+// Serve SRT server
+func Serve(cfg *Options) {
+	log.Printf("SRT server listening on %s", cfg.ListenAddress)
+	l, _ := srt.Listen("srt", cfg.ListenAddress)
+	defer l.Close()
+	for {
+		conn, err := l.Accept()
+		if err != nil {
+			log.Println("Error on incoming SRT stream", err)
+		}
+		log.Printf("New incomming SRT stream from %s", conn.RemoteAddr())
+
+		go func(sc net.Conn) {
+			defer sc.Close()
+
+			for {
+				//mon := conn.(*srt.SRTConn).Stats()
+				//s, _ := json.MarshalIndent(mon, "", "\t")
+				//fmt.Println(string(s))
+			}
+		}(conn)
+	}
+}