ghostream/main.go

108 lines
3.3 KiB
Go
Raw Normal View History

2020-09-28 15:36:40 +00:00
//go:generate pkger
2020-09-21 15:29:50 +00:00
package main
import (
2020-09-21 17:59:41 +00:00
"log"
2020-09-22 09:42:57 +00:00
"strings"
2020-09-21 17:59:41 +00:00
2020-09-22 09:42:57 +00:00
"github.com/spf13/viper"
"gitlab.crans.org/nounous/ghostream/auth"
2020-09-21 19:38:11 +00:00
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
2020-09-30 13:07:36 +00:00
"gitlab.crans.org/nounous/ghostream/stream/forwarding"
2020-09-27 09:14:22 +00:00
"gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
2020-09-21 15:47:31 +00:00
"gitlab.crans.org/nounous/ghostream/web"
2020-09-21 15:29:50 +00:00
)
2020-09-22 09:42:57 +00:00
func loadConfiguration() {
// Load configuration from environnement variables
// Replace "." to "_" for nested structs
// e.g. GHOSTREAM_LDAP_URI will apply to Config.LDAP.URI
viper.SetEnvPrefix("ghostream")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
// Load configuration file if exists
2020-09-27 19:46:33 +00:00
viper.SetConfigName("ghostream.yml")
2020-09-22 09:42:57 +00:00
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.ghostream")
viper.AddConfigPath("/etc/ghostream")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found, ignore and use defaults
log.Print(err)
} else {
// Config file was found but another error was produced
log.Fatal(err)
}
} else {
// Config loaded
log.Printf("Using config file: %s", viper.ConfigFileUsed())
}
// Define configuration default values
2020-09-22 14:39:06 +00:00
viper.SetDefault("Auth.Backend", "Basic")
2020-09-22 09:42:57 +00:00
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")
2020-09-27 09:14:22 +00:00
viper.SetDefault("Srt.ListenAddress", ":9710")
viper.SetDefault("Srt.MaxClients", 64)
2020-09-27 19:55:22 +00:00
viper.SetDefault("Web.ListenAddress", ":8080")
2020-09-22 09:42:57 +00:00
viper.SetDefault("Web.Name", "Ghostream")
viper.SetDefault("Web.Hostname", "localhost")
2020-09-30 13:52:26 +00:00
viper.SetDefault("Web.Favicon", "/static/img/favicon.svg")
viper.SetDefault("Web.ViewersCounterRefreshPeriod", 20000)
viper.SetDefault("WebRTC.MinPortUDP", 10000)
viper.SetDefault("WebRTC.MaxPortUDP", 10005)
2020-09-29 15:27:19 +00:00
viper.SetDefault("WebRTC.STUNServers", []string{"stun:stun.l.google.com:19302"})
2020-09-30 13:07:36 +00:00
viper.SetDefault("Forwarding", make(map[string][]string))
2020-09-29 15:27:19 +00:00
// Copy STUN configuration to clients
viper.Set("Web.STUNServers", viper.Get("WebRTC.STUNServers"))
2020-09-22 09:42:57 +00:00
}
2020-09-21 15:29:50 +00:00
func main() {
2020-09-21 17:59:41 +00:00
// Load configuration
2020-09-22 09:42:57 +00:00
loadConfiguration()
cfg := struct {
Auth auth.Options
2020-09-30 13:07:36 +00:00
Forwarding forwarding.Options
2020-09-22 09:42:57 +00:00
Monitoring monitoring.Options
2020-09-27 09:14:22 +00:00
Srt srt.Options
2020-09-22 09:42:57 +00:00
Web web.Options
WebRTC webrtc.Options
2020-09-22 09:42:57 +00:00
}{}
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatalln("Failed to load settings", err)
2020-09-21 17:59:41 +00:00
}
2020-09-22 10:54:12 +00:00
// Init authentification
authBackend, err := auth.New(&cfg.Auth)
if err != nil {
log.Fatalln("Failed to load authentification backend:", err)
}
defer authBackend.Close()
2020-09-22 10:54:12 +00:00
2020-09-24 09:24:13 +00:00
// WebRTC session description channels
remoteSdpChan := make(chan webrtc.SessionDescription)
localSdpChan := make(chan webrtc.SessionDescription)
2020-09-21 19:05:45 +00:00
// SRT channel, to propagate forwarding
forwardingChannel := make(chan srt.Packet)
2020-09-24 09:24:13 +00:00
// Start stream, web and monitoring server
go monitoring.Serve(&cfg.Monitoring)
go srt.Serve(&cfg.Srt, forwardingChannel)
2020-09-24 09:24:13 +00:00
go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web)
go webrtc.Serve(remoteSdpChan, localSdpChan, &cfg.WebRTC)
2020-09-21 19:33:32 +00:00
// Configure stream forwarding
forwarding.New(cfg.Forwarding, forwardingChannel)
2020-09-21 19:05:45 +00:00
// Wait for routines
select {}
2020-09-21 15:29:50 +00:00
}