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
[]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) {

View File

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

View File

@@ -35,13 +35,17 @@ func NewAPI(address string) (*webrtc.API, error) {
s.SetICEMulticastDNSMode(ice.MulticastDNSModeDisabled)
if address != "" {
ln, err := net.Listen("tcp", address)
if err == nil {
s.SetNetworkTypes([]webrtc.NetworkType{
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)
s.SetICETCPMux(tcpMux)
}

View File

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