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,7 +21,8 @@ func asyncCandidates(tr *api.Transport) {
continue continue
} }
cand, err := webrtc.NewCandidate(address) for _, network := range networks {
cand, err := webrtc.NewCandidate(network, address)
if err != nil { if err != nil {
log.Warn().Err(err).Caller().Send() log.Warn().Err(err).Caller().Send()
continue continue
@@ -31,6 +33,7 @@ func asyncCandidates(tr *api.Transport) {
tr.Write(&api.Message{Type: "webrtc/candidate", Value: cand}) tr.Write(&api.Message{Type: "webrtc/candidate", Value: cand})
} }
} }
}
func syncCanditates(answer string) (string, error) { func syncCanditates(answer string) (string, error) {
if len(candidates) == 0 { if len(candidates) == 0 {
@@ -57,7 +60,8 @@ func syncCanditates(answer string) (string, error) {
continue continue
} }
cand, err := webrtc.NewCandidate(address) for _, network := range networks {
cand, err := webrtc.NewCandidate(network, address)
if err != nil { if err != nil {
log.Warn().Err(err).Msg("[webrtc] candidate") log.Warn().Err(err).Msg("[webrtc] candidate")
continue continue
@@ -65,6 +69,7 @@ func syncCanditates(answer string) (string, error) {
md.WithPropertyAttribute(cand) md.WithPropertyAttribute(cand)
} }
}
if end { if end {
md.WithPropertyAttribute("end-of-candidates") md.WithPropertyAttribute("end-of-candidates")

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)
if err == nil {
s.SetNetworkTypes([]webrtc.NetworkType{ s.SetNetworkTypes([]webrtc.NetworkType{
webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6, webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6,
webrtc.NetworkTypeTCP4, webrtc.NetworkTypeTCP6, 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,