From 1c98754624f1c27d992d0410d173bd84a8da6f7f Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Tue, 29 Sep 2020 17:04:23 +0200 Subject: [PATCH] Restructure projet and add webrtc settings --- docs/ghostream.example.yml | 19 +++++++++++++------ main.go | 14 ++++++++------ stream/stream_test.go | 1 - stream/{stream.go => webrtc/webrtc.go} | 22 +++++++++++++++++----- stream/webrtc/webrtc_test.go | 1 + 5 files changed, 39 insertions(+), 18 deletions(-) delete mode 100644 stream/stream_test.go rename stream/{stream.go => webrtc/webrtc.go} (91%) create mode 100644 stream/webrtc/webrtc_test.go diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml index 98cb131..cf80f3d 100644 --- a/docs/ghostream.example.yml +++ b/docs/ghostream.example.yml @@ -21,7 +21,14 @@ auth: monitoring: listenAddress: 127.0.0.1:2112 -# Customize web server +# Configure SRT server +srt: + listenAddress: 127.0.0.1:9710 + + # Max number of active SRT connections + maxClients: 64 + +# Configure web server web: listenAddress: 127.0.0.1:8080 name: Demo @@ -29,8 +36,8 @@ web: favicon: https://www.crans.org/images/favicon.ico widgetURL: https://example.com/ -srt: - listenAddress: 127.0.0.1:9710 - - # Max number of active SRT connections - maxClients: 64 +# Configure WebRTC server +webrtc: + # UDP port range used to stream + minPortUDP: 10000 + maxPortUDP: 10005 diff --git a/main.go b/main.go index c6491d2..784cde1 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,11 @@ import ( "log" "strings" - "github.com/pion/webrtc/v3" "github.com/spf13/viper" "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/stream/webrtc" "gitlab.crans.org/nounous/ghostream/web" ) @@ -49,11 +48,13 @@ 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("Srt.MaxClients", 64) viper.SetDefault("Web.ListenAddress", ":8080") viper.SetDefault("Web.Name", "Ghostream") viper.SetDefault("Web.Hostname", "localhost") viper.SetDefault("Web.Favicon", "/favicon.ico") + viper.SetDefault("WebRTC.MinPortUDP", 10000) + viper.SetDefault("WebRTC.MaxPortUDP", 10005) } func main() { @@ -64,6 +65,7 @@ func main() { Monitoring monitoring.Options Srt srt.Options Web web.Options + WebRTC webrtc.Options }{} if err := viper.Unmarshal(&cfg); err != nil { log.Fatalln("Failed to load settings", err) @@ -81,10 +83,10 @@ 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) + go srt.Serve(&cfg.Srt) + go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web) + go webrtc.Serve(remoteSdpChan, localSdpChan, &cfg.WebRTC) // Wait for routines select {} diff --git a/stream/stream_test.go b/stream/stream_test.go deleted file mode 100644 index 11541cc..0000000 --- a/stream/stream_test.go +++ /dev/null @@ -1 +0,0 @@ -package stream diff --git a/stream/stream.go b/stream/webrtc/webrtc.go similarity index 91% rename from stream/stream.go rename to stream/webrtc/webrtc.go index ff0f1cc..419ddbf 100644 --- a/stream/stream.go +++ b/stream/webrtc/webrtc.go @@ -1,4 +1,4 @@ -package stream +package webrtc import ( "fmt" @@ -14,6 +14,16 @@ import ( "github.com/pion/webrtc/v3/pkg/media/oggreader" ) +// Options holds web package configuration +type Options struct { + MinPortUDP uint16 + MaxPortUDP uint16 +} + +// SessionDescription contains SDP data +// to initiate a WebRTC connection between one client and this app +type SessionDescription = webrtc.SessionDescription + const ( audioFileName = "output.ogg" videoFileName = "output.ivf" @@ -36,7 +46,7 @@ func removeTrack(tracks []*webrtc.Track, track *webrtc.Track) []*webrtc.Track { // newPeerHandler is called when server receive a new session description // this initiates a WebRTC connection and return server description -func newPeerHandler(remoteSdp webrtc.SessionDescription) webrtc.SessionDescription { +func newPeerHandler(remoteSdp webrtc.SessionDescription, cfg *Options) webrtc.SessionDescription { // Create media engine using client SDP mediaEngine := webrtc.MediaEngine{} if err := mediaEngine.PopulateFromSDP(remoteSdp); err != nil { @@ -46,7 +56,7 @@ func newPeerHandler(remoteSdp webrtc.SessionDescription) webrtc.SessionDescripti // Create a new PeerConnection settingsEngine := webrtc.SettingEngine{} - if err := settingsEngine.SetEphemeralUDPPortRange(10000, 10005); err != nil { + if err := settingsEngine.SetEphemeralUDPPortRange(cfg.MinPortUDP, cfg.MaxPortUDP); err != nil { log.Println("Failed to set min/max UDP ports", err) return webrtc.SessionDescription{} } @@ -227,7 +237,9 @@ func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecNa } // Serve WebRTC media streaming server -func Serve(remoteSdpChan chan webrtc.SessionDescription, localSdpChan chan webrtc.SessionDescription) { +func Serve(remoteSdpChan, localSdpChan chan webrtc.SessionDescription, cfg *Options) { + log.Printf("WebRTC server using UDP from %d to %d", cfg.MinPortUDP, cfg.MaxPortUDP) + go playVideo() go playAudio() @@ -236,6 +248,6 @@ func Serve(remoteSdpChan chan webrtc.SessionDescription, localSdpChan chan webrt // Wait for incoming session description // then send the local description to browser offer := <-remoteSdpChan - localSdpChan <- newPeerHandler(offer) + localSdpChan <- newPeerHandler(offer, cfg) } } diff --git a/stream/webrtc/webrtc_test.go b/stream/webrtc/webrtc_test.go new file mode 100644 index 0000000..efb4a82 --- /dev/null +++ b/stream/webrtc/webrtc_test.go @@ -0,0 +1 @@ +package webrtc