From b80e515eb04a8326336524ea80ecf711a3013293 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 29 Apr 2021 20:51:44 +0200 Subject: [PATCH] Implement -udp command-line flag. --- galene.go | 2 ++ group/group.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/galene.go b/galene.go index b384d52..66ff2be 100644 --- a/galene.go +++ b/galene.go @@ -45,6 +45,8 @@ func main() { "store mutex profile in `file`") flag.StringVar(&udpRange, "udp-range", "", "UDP port `range`") + flag.StringVar(&group.UDPAddr, "udp", "", + "UDP `address:port` (default dynamic)") flag.BoolVar(&group.UseMDNS, "mdns", false, "gather mDNS addresses") flag.BoolVar(&ice.ICERelayOnly, "relay-only", false, "require use of TURN relays for all media traffic") diff --git a/group/group.go b/group/group.go index c900f23..c2f3c64 100644 --- a/group/group.go +++ b/group/group.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "log" + "net" "os" "path" "path/filepath" @@ -21,6 +22,10 @@ var Directory string var UseMDNS bool var UDPMin, UDPMax uint16 +var UDPAddr string +var udpListenerMu sync.Mutex +var udpListener *net.UDPConn + var ErrNotAuthorised = errors.New("not authorised") type UserError string @@ -333,6 +338,14 @@ func APIFromCodecs(codecs []webrtc.RTPCodecParameters) (*webrtc.API, error) { webrtc.RTPHeaderExtensionCapability{sdp.SDESRTPStreamIDURI}, webrtc.RTPCodecTypeVideo) + listener, err := getUDPListener() + if err != nil { + return nil, err + } + if listener != nil { + s.SetICEUDPMux(webrtc.NewICEUDPMux(nil, listener)) + } + return webrtc.NewAPI( webrtc.WithSettingEngine(s), webrtc.WithMediaEngine(&m), @@ -356,6 +369,26 @@ func APIFromNames(names []string) (*webrtc.API, error) { return APIFromCodecs(codecs) } +func getUDPListener() (*net.UDPConn, error) { + if UDPAddr == "" { + return nil, nil + } + udpListenerMu.Lock() + defer udpListenerMu.Unlock() + if udpListener == nil { + addr, err := net.ResolveUDPAddr("udp", UDPAddr) + if err != nil { + return nil, err + } + udpListener, err = net.ListenUDP("udp", addr) + if err != nil { + udpListener = nil + return nil, err + } + } + return udpListener, nil +} + func Add(name string, desc *Description) (*Group, error) { g, notify, err := add(name, desc) for _, c := range notify {