From 29eeb2c0fd760c940baca9084d08f145aaf0ebb6 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Tue, 29 Sep 2020 16:49:30 +0200 Subject: [PATCH] Load SRT host and port from configuration --- docs/ghostream.example.yml | 5 ++++- main.go | 1 + stream/srt/srt.go | 28 +++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml index 9dce37d..98cb131 100644 --- a/docs/ghostream.example.yml +++ b/docs/ghostream.example.yml @@ -30,4 +30,7 @@ web: widgetURL: https://example.com/ srt: - listenAddress: 127.0.0.1:9710 \ No newline at end of file + listenAddress: 127.0.0.1:9710 + + # Max number of active SRT connections + maxClients: 64 diff --git a/main.go b/main.go index eb3e963..c6491d2 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,7 @@ func loadConfiguration() { viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com") viper.SetDefault("Monitoring.ListenAddress", ":2112") viper.SetDefault("Srt.ListenAddress", ":9710") + viper.SetDefault("Srt.MaxClients", "64") viper.SetDefault("Web.ListenAddress", ":8080") viper.SetDefault("Web.Name", "Ghostream") viper.SetDefault("Web.Hostname", "localhost") diff --git a/stream/srt/srt.go b/stream/srt/srt.go index 1c75370..f920046 100644 --- a/stream/srt/srt.go +++ b/stream/srt/srt.go @@ -2,6 +2,8 @@ package srt import ( "log" + "net" + "strconv" "github.com/haivision/srtgo" "github.com/pion/rtp" @@ -10,18 +12,34 @@ import ( // Options holds web package configuration type Options struct { ListenAddress string + MaxClients int +} + +// Split host and port from listen address +func splitHostPort(hostport string) (string, uint16) { + host, portS, err := net.SplitHostPort(hostport) + if err != nil { + log.Fatalf("Failed to split host and port from %s", hostport) + } + if host == "" { + host = "0.0.0.0" + } + port64, err := strconv.ParseUint(portS, 10, 16) + if err != nil { + log.Fatalf("Port is not a integer: %s", err) + } + return host, uint16(port64) } // Serve SRT server func Serve(cfg *Options) { - log.Printf("SRT server listening on %s", cfg.ListenAddress) - options := make(map[string]string) options["transtype"] = "live" - // FIXME: cfg.ListenAddress -> host and port - sck := srtgo.NewSrtSocket("0.0.0.0", 9710, options) - sck.Listen(1) + log.Printf("SRT server listening on %s", cfg.ListenAddress) + host, port := splitHostPort(cfg.ListenAddress) + sck := srtgo.NewSrtSocket(host, uint16(port), options) + sck.Listen(cfg.MaxClients) for { s, err := sck.Accept()