Refactor WebRTC candidates processing

This commit is contained in:
Alexey Khit
2023-02-24 11:28:14 +03:00
parent 3fb917f00f
commit 4328d2a573
4 changed files with 105 additions and 45 deletions
+33
View File
@@ -7,6 +7,7 @@ import (
"github.com/pion/ice/v2"
"github.com/pion/stun"
"github.com/pion/webrtc/v3"
"hash/crc32"
"net"
"strconv"
"strings"
@@ -157,3 +158,35 @@ func MimeType(codec *streamer.Codec) string {
}
panic("not implemented")
}
const PriorityHost = (1 << 24) * uint32(126)
const PriorityLocalUDP = (1 << 8) * uint32(65535)
const PriorityLocalTCPPassive = (1 << 8) * uint32((1<<13)*4+8191)
const PriorityComponentRTP = uint32(256 - ice.ComponentRTP)
func CandidateHostUDP(host string, port int) string {
foundation := crc32.ChecksumIEEE([]byte("host" + host + "udp4"))
priority := PriorityHost + PriorityLocalUDP + PriorityComponentRTP
// 1. Foundation
// 2. Component, always 1 because RTP
// 3. udp or tcp
// 4. Priority
// 5. Host - IP4 or IP6 or domain name
// 6. Port
// 7. typ host
return fmt.Sprintf(
"candidate:%d 1 udp %d %s %d typ host",
foundation, priority, host, port,
)
}
func CandidateHostTCPPassive(address string, port int) string {
foundation := crc32.ChecksumIEEE([]byte("host" + address + "tcp4"))
priority := PriorityHost + PriorityLocalTCPPassive + PriorityComponentRTP
return fmt.Sprintf(
"candidate:%d 1 tcp %d %s %d typ host tcptype passive",
foundation, priority, address, port,
)
}
+17 -5
View File
@@ -5,19 +5,31 @@ import (
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestName(t *testing.T) {
i, _ := ice.NewCandidateHost(&ice.CandidateHostConfig{
func TestCandidates(t *testing.T) {
conf := &ice.CandidateHostConfig{
Network: "udp",
Address: "192.168.1.123",
Port: 8555,
Component: ice.ComponentRTP,
}
cand, err := ice.NewCandidateHost(conf)
require.Nil(t, err)
assert.Equal(t, "candidate:"+cand.Marshal(), CandidateHostUDP(conf.Address, conf.Port))
conf = &ice.CandidateHostConfig{
Network: "tcp",
Address: "192.168.1.123",
Port: 8555,
Component: ice.ComponentRTP,
TCPType: ice.TCPTypePassive,
})
a := i.Marshal()
println(a)
}
cand, err = ice.NewCandidateHost(conf)
require.Nil(t, err)
assert.Equal(t, "candidate:"+cand.Marshal(), CandidateHostTCPPassive(conf.Address, conf.Port))
}
func TestPublicIP(t *testing.T) {