From 1ec0e7540a2b3668d61af4a9fba7a49b46f52a3e Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sat, 8 Aug 2020 13:44:33 +0200 Subject: [PATCH] Passive ICE TCP. --- group/group.go | 23 +++++++++++++++++++++++ sfu.go | 14 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/group/group.go b/group/group.go index b2ab024..8f54a8f 100644 --- a/group/group.go +++ b/group/group.go @@ -8,6 +8,7 @@ package group import ( "encoding/json" "log" + "net" "os" "path/filepath" "sort" @@ -136,6 +137,18 @@ func (g *Group) API() *webrtc.API { return groups.api } +var tcpListener net.Listener + +func StartTCPListener(addr *net.TCPAddr) error { + if tcpListener != nil { + tcpListener.Close() + tcpListener = nil + } + var err error + tcpListener, err = net.ListenTCP("tcp", addr) + return err +} + func Add(name string, desc *description) (*Group, error) { groups.mu.Lock() defer groups.mu.Unlock() @@ -143,6 +156,16 @@ func Add(name string, desc *description) (*Group, error) { if groups.groups == nil { groups.groups = make(map[string]*Group) s := webrtc.SettingEngine{} + if tcpListener != nil { + mux := webrtc.NewICETCPMux(nil, tcpListener, 8) + s.SetICETCPMux(mux) + s.SetNetworkTypes([]webrtc.NetworkType{ + webrtc.NetworkTypeUDP4, + webrtc.NetworkTypeUDP6, + webrtc.NetworkTypeTCP4, + webrtc.NetworkTypeTCP6, + }) + } m := webrtc.MediaEngine{} m.RegisterCodec(webrtc.NewRTPVP8CodecExt( webrtc.DefaultPayloadTypeVP8, 90000, diff --git a/sfu.go b/sfu.go index 7e57ab4..a9f8d10 100644 --- a/sfu.go +++ b/sfu.go @@ -8,6 +8,7 @@ package main import ( "flag" "log" + "net" "os" "os/signal" "path/filepath" @@ -22,10 +23,14 @@ import ( func main() { var cpuprofile, memprofile, mutexprofile, httpAddr, dataDir string + var tcpPort int flag.StringVar(&httpAddr, "http", ":8443", "web server `address`") flag.StringVar(&webserver.StaticRoot, "static", "./static/", "web server root `directory`") + flag.IntVar(&tcpPort, "tcp-port", -1, + "TCP listener `port`. If 0, an ephemeral port is used.\n"+ + "If -1, the TCP listerer is disabled") flag.StringVar(&dataDir, "data", "./data/", "data `directory`") flag.StringVar(&group.Directory, "groups", "./groups/", @@ -80,6 +85,15 @@ func main() { group.IceFilename = filepath.Join(dataDir, "ice-servers.json") + if tcpPort >= 0 { + err := group.StartTCPListener(&net.TCPAddr{ + Port: tcpPort, + }) + if err != nil { + log.Fatalf("Couldn't start ICE TCP: %v", err) + } + } + go group.ReadPublicGroups() serverDone := make(chan struct{})