feat: add publicport config

This commit is contained in:
langhuihui
2023-05-04 10:01:59 +08:00
parent a9dbb24a08
commit 29dd7a2505

44
main.go
View File

@@ -5,8 +5,6 @@ import (
"net" "net"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"strings"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
@@ -17,6 +15,7 @@ import (
"github.com/pion/interceptor" "github.com/pion/interceptor"
. "github.com/pion/webrtc/v3" . "github.com/pion/webrtc/v3"
"m7s.live/engine/v4/config" "m7s.live/engine/v4/config"
"m7s.live/engine/v4/util"
"m7s.live/plugin/webrtc/v4/webrtc" "m7s.live/plugin/webrtc/v4/webrtc"
) )
@@ -61,6 +60,7 @@ type WebRTCConfig struct {
config.Subscribe config.Subscribe
ICEServers []ICEServer ICEServers []ICEServer
PublicIP []string PublicIP []string
PublicPort uint16
Port string `default:"tcp:9000"` Port string `default:"tcp:9000"`
PLI time.Duration `default:"2s"` // 视频流丢包后发送PLI请求 PLI time.Duration `default:"2s"` // 视频流丢包后发送PLI请求
m MediaEngine m MediaEngine
@@ -82,9 +82,12 @@ 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)
} }
protocol, port, _ := strings.Cut(conf.Port, ":") protocol, ports := util.Conf2Listener(conf.Port)
if len(ports) == 0 {
WebRTCPlugin.Fatal("webrtc port config error")
}
if protocol == "tcp" { if protocol == "tcp" {
tcpport, _ := strconv.Atoi(port) tcpport := int(ports[0])
tcpl, err := net.ListenTCP("tcp", &net.TCPAddr{ tcpl, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IP{0, 0, 0, 0}, IP: net.IP{0, 0, 0, 0},
Port: tcpport, Port: tcpport,
@@ -95,26 +98,20 @@ func (conf *WebRTCConfig) OnEvent(event any) {
WebRTCPlugin.Info("webrtc start listen", zap.Int("port", tcpport)) WebRTCPlugin.Info("webrtc start listen", zap.Int("port", tcpport))
conf.s.SetICETCPMux(NewICETCPMux(nil, tcpl, 4096)) conf.s.SetICETCPMux(NewICETCPMux(nil, tcpl, 4096))
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeTCP4, NetworkTypeTCP6}) conf.s.SetNetworkTypes([]NetworkType{NetworkTypeTCP4, NetworkTypeTCP6})
} else if len(ports) == 2 {
conf.s.SetEphemeralUDPPortRange(ports[0], ports[1])
} else { } else {
r := strings.Split(port, "-") // 创建共享WEBRTC端口 默认9000
if len(r) == 2 { udpListener, err := net.ListenUDP("udp", &net.UDPAddr{
min, _ := strconv.Atoi(r[0]) IP: net.IP{0, 0, 0, 0},
max, _ := strconv.Atoi(r[1]) Port: int(ports[0]),
conf.s.SetEphemeralUDPPortRange(uint16(min), uint16(max)) })
} else { if err != nil {
udpport, _ := strconv.Atoi(port) WebRTCPlugin.Fatal("webrtc listener udp", zap.Error(err))
// 创建共享WEBRTC端口 默认9000
udpListener, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IP{0, 0, 0, 0},
Port: udpport,
})
if err != nil {
WebRTCPlugin.Fatal("webrtc listener udp", zap.Error(err))
}
WebRTCPlugin.Info("webrtc start listen", zap.Int("port", udpport))
conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener))
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeUDP4, NetworkTypeUDP6})
} }
WebRTCPlugin.Info("webrtc start listen", zap.Uint16("port", ports[0]))
conf.s.SetICEUDPMux(NewICEUDPMux(nil, udpListener))
conf.s.SetNetworkTypes([]NetworkType{NetworkTypeUDP4, NetworkTypeUDP6})
} }
if err := RegisterDefaultInterceptors(&conf.m, i); err != nil { if err := RegisterDefaultInterceptors(&conf.m, i); err != nil {
@@ -140,6 +137,9 @@ func (conf *WebRTCConfig) Play_(w http.ResponseWriter, r *http.Request) {
suber.OnICECandidate(func(ice *ICECandidate) { suber.OnICECandidate(func(ice *ICECandidate) {
if ice != nil { if ice != nil {
suber.Info(ice.ToJSON().Candidate) suber.Info(ice.ToJSON().Candidate)
if conf.PublicPort != 0 {
ice.Port = conf.PublicPort
}
} }
}) })
if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: suber.SDP}); err != nil { if err = suber.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: suber.SDP}); err != nil {