Add single UDP port for WebRTC Server

This commit is contained in:
Alexey Khit
2023-01-13 20:34:18 +03:00
parent 7a3adf17be
commit c6ad7ac39f
4 changed files with 32 additions and 22 deletions

View File

@@ -25,6 +25,7 @@ var stackSkip = [][]byte{
// webrtc/api.go // webrtc/api.go
[]byte("created by github.com/pion/ice/v2.NewTCPMuxDefault"), []byte("created by github.com/pion/ice/v2.NewTCPMuxDefault"),
[]byte("created by github.com/pion/ice/v2.NewUDPMuxDefault"),
} }
func stackHandler(w http.ResponseWriter, r *http.Request) { func stackHandler(w http.ResponseWriter, r *http.Request) {

View File

@@ -7,6 +7,7 @@ import (
) )
var candidates []string var candidates []string
var networks = []string{"udp", "tcp"}
func AddCandidate(address string) { func AddCandidate(address string) {
candidates = append(candidates, address) candidates = append(candidates, address)
@@ -20,15 +21,17 @@ func asyncCandidates(tr *api.Transport) {
continue continue
} }
cand, err := webrtc.NewCandidate(address) for _, network := range networks {
if err != nil { cand, err := webrtc.NewCandidate(network, address)
log.Warn().Err(err).Caller().Send() if err != nil {
continue log.Warn().Err(err).Caller().Send()
continue
}
log.Trace().Str("candidate", cand).Msg("[webrtc] config")
tr.Write(&api.Message{Type: "webrtc/candidate", Value: cand})
} }
log.Trace().Str("candidate", cand).Msg("[webrtc] config")
tr.Write(&api.Message{Type: "webrtc/candidate", Value: cand})
} }
} }
@@ -57,13 +60,15 @@ func syncCanditates(answer string) (string, error) {
continue continue
} }
cand, err := webrtc.NewCandidate(address) for _, network := range networks {
if err != nil { cand, err := webrtc.NewCandidate(network, address)
log.Warn().Err(err).Msg("[webrtc] candidate") if err != nil {
continue log.Warn().Err(err).Msg("[webrtc] candidate")
} continue
}
md.WithPropertyAttribute(cand) md.WithPropertyAttribute(cand)
}
} }
if end { if end {

View File

@@ -35,13 +35,17 @@ func NewAPI(address string) (*webrtc.API, error) {
s.SetICEMulticastDNSMode(ice.MulticastDNSModeDisabled) s.SetICEMulticastDNSMode(ice.MulticastDNSModeDisabled)
if address != "" { if address != "" {
ln, err := net.Listen("tcp", address) s.SetNetworkTypes([]webrtc.NetworkType{
if err == nil { webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6,
s.SetNetworkTypes([]webrtc.NetworkType{ webrtc.NetworkTypeTCP4, webrtc.NetworkTypeTCP6,
webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6, })
webrtc.NetworkTypeTCP4, webrtc.NetworkTypeTCP6,
})
if ln, err := net.ListenPacket("udp", address); err == nil {
udpMux := webrtc.NewICEUDPMux(nil, ln)
s.SetICEUDPMux(udpMux)
}
if ln, err := net.Listen("tcp", address); err == nil {
tcpMux := webrtc.NewICETCPMux(nil, ln, 8) tcpMux := webrtc.NewICETCPMux(nil, ln, 8)
s.SetICETCPMux(tcpMux) s.SetICETCPMux(tcpMux)
} }

View File

@@ -13,7 +13,7 @@ import (
"time" "time"
) )
func NewCandidate(address string) (string, error) { func NewCandidate(network, address string) (string, error) {
i := strings.LastIndexByte(address, ':') i := strings.LastIndexByte(address, ':')
if i < 0 { if i < 0 {
return "", errors.New("wrong candidate: " + address) return "", errors.New("wrong candidate: " + address)
@@ -26,7 +26,7 @@ func NewCandidate(address string) (string, error) {
} }
cand, err := ice.NewCandidateHost(&ice.CandidateHostConfig{ cand, err := ice.NewCandidateHost(&ice.CandidateHostConfig{
Network: "tcp", Network: network,
Address: host, Address: host,
Port: i, Port: i,
Component: ice.ComponentRTP, Component: ice.ComponentRTP,