From f5626961762063b382f5fd680ff3f0851f5eac93 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Mon, 8 Jul 2024 14:28:09 -0400 Subject: [PATCH] Add APPEND_CANDIDATE Performs SDP munging so users can insert arbitrary candidates --- README.md | 4 +++- internal/webrtc/webrtc.go | 14 ++++++++++++-- internal/webrtc/whep.go | 2 +- internal/webrtc/whip.go | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7ae90a1..482e214 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ The backend can be configured with the following environment variables. - `SSL_CERT` - Path to SSL certificate if using Broadcast Box's HTTP Server - `SSL_KEY` - Path to SSL key if using Broadcast Box's HTTP Server -- `NAT_1_TO_1_IP` - Announce IPs that don't belong to local machine (like Public IP). delineated by ',' +- `NAT_1_TO_1_IP` - Announce IPs that don't belong to local machine (like Public IP). delineated by '|' - `INCLUDE_PUBLIC_IP_IN_NAT_1_TO_1_IP` - Like `NAT_1_TO_1_IP` but autoconfigured - `INTERFACE_FILTER` - Only use a certain interface for UDP traffic - `NAT_ICE_CANDIDATE_TYPE` - By default setting a NAT_1_TO_1_IP overrides. Set this to `srflx` to instead append IPs @@ -216,6 +216,8 @@ The backend can be configured with the following environment variables. - `TCP_MUX_ADDRESS` - If you wish to make WebRTC traffic available via TCP. - `TCP_MUX_FORCE` - If you wish to make WebRTC traffic only available via TCP. +- `APPEND_CANDIDATE` - Append candidates to Offer that ICE Agent did not generate. Worse version of `NAT_1_TO_1_IP` + ## Network Test on Start When running in Docker Broadcast Box runs a network tests on startup. This tests that WebRTC traffic can be established diff --git a/internal/webrtc/webrtc.go b/internal/webrtc/webrtc.go index a6f0e87..9a409a9 100644 --- a/internal/webrtc/webrtc.go +++ b/internal/webrtc/webrtc.go @@ -190,14 +190,15 @@ func createSettingEngine(isWHIP bool, udpMuxCache map[int]*ice.MultiUDPMuxDefaul udpMuxOpts []ice.UDPMuxFromPortOption err error ) - networkTypes := []webrtc.NetworkType{webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6} + networkTypes := []webrtc.NetworkType{webrtc.NetworkTypeUDP4} + udpMuxOpts = append(udpMuxOpts, ice.UDPMuxFromPortWithNetworks(ice.NetworkTypeUDP4)) if os.Getenv("INCLUDE_PUBLIC_IP_IN_NAT_1_TO_1_IP") != "" { NAT1To1IPs = append(NAT1To1IPs, getPublicIP()) } if os.Getenv("NAT_1_TO_1_IP") != "" { - NAT1To1IPs = append(NAT1To1IPs, strings.Split(os.Getenv("NAT_1_TO_1_IP"), ",")...) + NAT1To1IPs = append(NAT1To1IPs, strings.Split(os.Getenv("NAT_1_TO_1_IP"), "|")...) } natICECandidateType := webrtc.ICECandidateTypeHost @@ -348,6 +349,15 @@ func newPeerConnection(api *webrtc.API) (*webrtc.PeerConnection, error) { return api.NewPeerConnection(cfg) } +func appendOffer(in string) string { + if extraCandidate := os.Getenv("APPEND_CANDIDATE"); extraCandidate != "" { + index := strings.Index(in, "a=end-of-candidates") + in = in[:index] + extraCandidate + in[index:] + } + + return in +} + func Configure() { streamMap = map[string]*stream{} diff --git a/internal/webrtc/whep.go b/internal/webrtc/whep.go index cdfbc40..dfac82e 100644 --- a/internal/webrtc/whep.go +++ b/internal/webrtc/whep.go @@ -151,7 +151,7 @@ func WHEP(offer, streamKey string) (string, string, error) { timestamp: 50000, } stream.whepSessions[whepSessionId].currentLayer.Store("") - return peerConnection.LocalDescription().SDP, whepSessionId, nil + return appendOffer(peerConnection.LocalDescription().SDP), whepSessionId, nil } func (w *whepSession) sendVideoPacket(rtpPkt *rtp.Packet, layer string, timeDiff int64, sequenceDiff int, codec videoTrackCodec) { diff --git a/internal/webrtc/whip.go b/internal/webrtc/whip.go index d3c3bea..eea58a0 100644 --- a/internal/webrtc/whip.go +++ b/internal/webrtc/whip.go @@ -185,5 +185,5 @@ func WHIP(offer, streamKey string) (string, error) { } <-gatherComplete - return peerConnection.LocalDescription().SDP, nil + return appendOffer(peerConnection.LocalDescription().SDP), nil }