Load SRT host and port from configuration

This commit is contained in:
Alexandre Iooss 2020-09-29 16:49:30 +02:00
parent c905eadc78
commit 29eeb2c0fd
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
3 changed files with 28 additions and 6 deletions

View File

@ -31,3 +31,6 @@ web:
srt: srt:
listenAddress: 127.0.0.1:9710 listenAddress: 127.0.0.1:9710
# Max number of active SRT connections
maxClients: 64

View File

@ -49,6 +49,7 @@ func loadConfiguration() {
viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com") viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com")
viper.SetDefault("Monitoring.ListenAddress", ":2112") viper.SetDefault("Monitoring.ListenAddress", ":2112")
viper.SetDefault("Srt.ListenAddress", ":9710") viper.SetDefault("Srt.ListenAddress", ":9710")
viper.SetDefault("Srt.MaxClients", "64")
viper.SetDefault("Web.ListenAddress", ":8080") viper.SetDefault("Web.ListenAddress", ":8080")
viper.SetDefault("Web.Name", "Ghostream") viper.SetDefault("Web.Name", "Ghostream")
viper.SetDefault("Web.Hostname", "localhost") viper.SetDefault("Web.Hostname", "localhost")

View File

@ -2,6 +2,8 @@ package srt
import ( import (
"log" "log"
"net"
"strconv"
"github.com/haivision/srtgo" "github.com/haivision/srtgo"
"github.com/pion/rtp" "github.com/pion/rtp"
@ -10,18 +12,34 @@ import (
// Options holds web package configuration // Options holds web package configuration
type Options struct { type Options struct {
ListenAddress string 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 // Serve SRT server
func Serve(cfg *Options) { func Serve(cfg *Options) {
log.Printf("SRT server listening on %s", cfg.ListenAddress)
options := make(map[string]string) options := make(map[string]string)
options["transtype"] = "live" options["transtype"] = "live"
// FIXME: cfg.ListenAddress -> host and port log.Printf("SRT server listening on %s", cfg.ListenAddress)
sck := srtgo.NewSrtSocket("0.0.0.0", 9710, options) host, port := splitHostPort(cfg.ListenAddress)
sck.Listen(1) sck := srtgo.NewSrtSocket(host, uint16(port), options)
sck.Listen(cfg.MaxClients)
for { for {
s, err := sck.Accept() s, err := sck.Accept()