实现tcp方式连接

This commit is contained in:
dexter
2023-03-14 09:36:13 +08:00
parent 2ab769cdb6
commit b5f0948f02

42
main.go
View File

@@ -5,6 +5,8 @@ import (
"net" "net"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"strings"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
@@ -59,12 +61,7 @@ type WebRTCConfig struct {
config.Subscribe config.Subscribe
ICEServers []string ICEServers []string
PublicIP []string PublicIP []string
PortMin uint16 `default:"9000"` Port string `default:"tcp:9000"`
PortMax uint16 `default:"10000"`
InvitePortFixed bool `default:"false"` // 设备将流发送的端口,是否固定 on 发送流到多路复用端口 如9000 off 自动从 mix_port - max_port 之间的值中 选一个可以用的端口
IceUdpMux int `default:"9000"` // 接收设备端rtp流的多路复用端口
PLI time.Duration `default:"2s"` // 视频流丢包后发送PLI请求 PLI time.Duration `default:"2s"` // 视频流丢包后发送PLI请求
m MediaEngine m MediaEngine
s SettingEngine s SettingEngine
@@ -79,25 +76,42 @@ func (conf *WebRTCConfig) OnEvent(event any) {
if len(conf.PublicIP) > 0 { if len(conf.PublicIP) > 0 {
conf.s.SetNAT1To1IPs(conf.PublicIP, ICECandidateTypeHost) conf.s.SetNAT1To1IPs(conf.PublicIP, ICECandidateTypeHost)
} }
if conf.PortMin > 0 && conf.PortMax > 0 {
conf.s.SetEphemeralUDPPortRange(conf.PortMin, conf.PortMax)
}
// 是否多路复用UDP端口 protocol, port, _ := strings.Cut(conf.Port, ":")
if conf.InvitePortFixed { if protocol == "tcp" {
tcpport, _ := strconv.Atoi(port)
tcpl, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IP{0, 0, 0, 0},
Port: tcpport,
})
if err != nil {
WebRTCPlugin.Fatal("webrtc listener tcp", zap.Error(err))
}
WebRTCPlugin.Info("webrtc start listen", zap.Int("port", tcpport))
conf.s.SetICETCPMux(NewICETCPMux(nil, tcpl, 4096))
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeTCP4, NetworkTypeTCP6})
} else {
r := strings.Split(port, "-")
if len(r) == 2 {
min, _ := strconv.Atoi(r[0])
max, _ := strconv.Atoi(r[1])
conf.s.SetEphemeralUDPPortRange(uint16(min), uint16(max))
} else {
udpport, _ := strconv.Atoi(port)
// 创建共享WEBRTC端口 默认9000 // 创建共享WEBRTC端口 默认9000
udpListener, err := net.ListenUDP("udp", &net.UDPAddr{ udpListener, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IP{0, 0, 0, 0}, IP: net.IP{0, 0, 0, 0},
Port: conf.IceUdpMux, Port: udpport,
}) })
if err != nil { if err != nil {
WebRTCPlugin.Fatal("webrtc listener udp", zap.Error(err)) WebRTCPlugin.Fatal("webrtc listener udp", zap.Error(err))
} }
WebRTCPlugin.Info("webrtc start listen", zap.Int("port", conf.IceUdpMux)) WebRTCPlugin.Info("webrtc start listen", zap.Int("port", udpport))
conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener)) conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener))
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeUDP4, NetworkTypeUDP6})
}
} }
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeUDP4, NetworkTypeUDP6})
if err := RegisterDefaultInterceptors(&conf.m, i); err != nil { if err := RegisterDefaultInterceptors(&conf.m, i); err != nil {
panic(err) panic(err)
} }