Browse Source

webrtc: add webrtcICEUDPMuxAddress

pull/1364/head
aler9 4 years ago
parent
commit
b26f848613
  1. 2
      apidocs/openapi.yaml
  2. 1
      internal/conf/conf.go
  3. 2
      internal/core/core.go
  4. 17
      internal/core/webrtc_conn.go
  5. 38
      internal/core/webrtc_server.go
  6. 6
      rtsp-simple-server.yml

2
apidocs/openapi.yaml

@ -151,6 +151,8 @@ components:
type: array type: array
items: items:
type: string type: string
webrtcICEUDPMuxAddress:
type: string
webrtcICETCPMuxAddress: webrtcICETCPMuxAddress:
type: string type: string

1
internal/conf/conf.go

@ -233,6 +233,7 @@ type Conf struct {
WebRTCTrustedProxies IPsOrCIDRs `json:"webrtcTrustedProxies"` WebRTCTrustedProxies IPsOrCIDRs `json:"webrtcTrustedProxies"`
WebRTCICEServers []string `json:"webrtcICEServers"` WebRTCICEServers []string `json:"webrtcICEServers"`
WebRTCICEHostNAT1To1IPs []string `json:"webrtcICEHostNAT1To1IPs"` WebRTCICEHostNAT1To1IPs []string `json:"webrtcICEHostNAT1To1IPs"`
WebRTCICEUDPMuxAddress string `json:"webrtcICEUDPMuxAddress"`
WebRTCICETCPMuxAddress string `json:"webrtcICETCPMuxAddress"` WebRTCICETCPMuxAddress string `json:"webrtcICETCPMuxAddress"`
// paths // paths

2
internal/core/core.go

@ -414,6 +414,7 @@ func (p *Core) createResources(initial bool) error {
p.metrics, p.metrics,
p, p,
p.conf.WebRTCICEHostNAT1To1IPs, p.conf.WebRTCICEHostNAT1To1IPs,
p.conf.WebRTCICEUDPMuxAddress,
p.conf.WebRTCICETCPMuxAddress, p.conf.WebRTCICETCPMuxAddress,
) )
if err != nil { if err != nil {
@ -578,6 +579,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
closeMetrics || closeMetrics ||
closePathManager || closePathManager ||
!reflect.DeepEqual(newConf.WebRTCICEHostNAT1To1IPs, p.conf.WebRTCICEHostNAT1To1IPs) || !reflect.DeepEqual(newConf.WebRTCICEHostNAT1To1IPs, p.conf.WebRTCICEHostNAT1To1IPs) ||
newConf.WebRTCICEUDPMuxAddress != p.conf.WebRTCICEUDPMuxAddress ||
newConf.WebRTCICETCPMuxAddress != p.conf.WebRTCICETCPMuxAddress newConf.WebRTCICETCPMuxAddress != p.conf.WebRTCICETCPMuxAddress
closeAPI := newConf == nil || closeAPI := newConf == nil ||

17
internal/core/webrtc_conn.go

@ -126,6 +126,7 @@ type webRTCConn struct {
wg *sync.WaitGroup wg *sync.WaitGroup
pathManager webRTCConnPathManager pathManager webRTCConnPathManager
parent webRTCConnParent parent webRTCConnParent
iceUDPMux ice.UDPMux
iceTCPMux ice.TCPMux iceTCPMux ice.TCPMux
iceHostNAT1To1IPs []string iceHostNAT1To1IPs []string
@ -146,8 +147,9 @@ func newWebRTCConn(
wg *sync.WaitGroup, wg *sync.WaitGroup,
pathManager webRTCConnPathManager, pathManager webRTCConnPathManager,
parent webRTCConnParent, parent webRTCConnParent,
iceTCPMux ice.TCPMux,
iceHostNAT1To1IPs []string, iceHostNAT1To1IPs []string,
iceUDPMux ice.UDPMux,
iceTCPMux ice.TCPMux,
) *webRTCConn { ) *webRTCConn {
ctx, ctxCancel := context.WithCancel(parentCtx) ctx, ctxCancel := context.WithCancel(parentCtx)
@ -163,6 +165,7 @@ func newWebRTCConn(
ctxCancel: ctxCancel, ctxCancel: ctxCancel,
uuid: uuid.New(), uuid: uuid.New(),
created: time.Now(), created: time.Now(),
iceUDPMux: iceUDPMux,
iceTCPMux: iceTCPMux, iceTCPMux: iceTCPMux,
iceHostNAT1To1IPs: iceHostNAT1To1IPs, iceHostNAT1To1IPs: iceHostNAT1To1IPs,
} }
@ -289,15 +292,19 @@ func (c *webRTCConn) runInner(ctx context.Context) error {
configuration := webrtc.Configuration{ICEServers: c.genICEServers()} configuration := webrtc.Configuration{ICEServers: c.genICEServers()}
settingsEngine := webrtc.SettingEngine{} settingsEngine := webrtc.SettingEngine{}
if len(c.iceHostNAT1To1IPs) != 0 {
settingsEngine.SetNAT1To1IPs(c.iceHostNAT1To1IPs, webrtc.ICECandidateTypeHost)
}
if c.iceUDPMux != nil {
settingsEngine.SetICEUDPMux(c.iceUDPMux)
}
if c.iceTCPMux != nil { if c.iceTCPMux != nil {
settingsEngine.SetICETCPMux(c.iceTCPMux) settingsEngine.SetICETCPMux(c.iceTCPMux)
settingsEngine.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeTCP4}) settingsEngine.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeTCP4})
} }
if len(c.iceHostNAT1To1IPs) != 0 {
settingsEngine.SetNAT1To1IPs(c.iceHostNAT1To1IPs, webrtc.ICECandidateTypeHost)
}
pc, err := newPeerConnection(configuration, webrtc.WithSettingEngine(settingsEngine)) pc, err := newPeerConnection(configuration, webrtc.WithSettingEngine(settingsEngine))
if err != nil { if err != nil {
return err return err

38
internal/core/webrtc_server.go

@ -83,12 +83,13 @@ type webRTCServer struct {
ctxCancel func() ctxCancel func()
wg sync.WaitGroup wg sync.WaitGroup
ln net.Listener ln net.Listener
udpMuxLn net.PacketConn
tcpMuxLn net.Listener tcpMuxLn net.Listener
tlsConfig *tls.Config tlsConfig *tls.Config
conns map[*webRTCConn]struct{} conns map[*webRTCConn]struct{}
iceTCPMux ice.TCPMux
iceHostNAT1To1IPs []string iceHostNAT1To1IPs []string
iceUDPMux ice.UDPMux
iceTCPMux ice.TCPMux
// in // in
connNew chan webRTCConnNewReq connNew chan webRTCConnNewReq
@ -112,6 +113,7 @@ func newWebRTCServer(
metrics *metrics, metrics *metrics,
parent webRTCServerParent, parent webRTCServerParent,
iceHostNAT1To1IPs []string, iceHostNAT1To1IPs []string,
iceUDPMuxAddress string,
iceTCPMuxAddress string, iceTCPMuxAddress string,
) (*webRTCServer, error) { ) (*webRTCServer, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen("tcp", address)
@ -132,6 +134,16 @@ func newWebRTCServer(
} }
} }
var iceUDPMux ice.UDPMux
var udpMuxLn net.PacketConn
if iceUDPMuxAddress != "" {
udpMuxLn, err = net.ListenPacket("udp", iceUDPMuxAddress)
if err != nil {
return nil, err
}
iceUDPMux = webrtc.NewICEUDPMux(nil, udpMuxLn)
}
var iceTCPMux ice.TCPMux var iceTCPMux ice.TCPMux
var tcpMuxLn net.Listener var tcpMuxLn net.Listener
if iceTCPMuxAddress != "" { if iceTCPMuxAddress != "" {
@ -139,7 +151,6 @@ func newWebRTCServer(
if err != nil { if err != nil {
return nil, err return nil, err
} }
iceTCPMux = webrtc.NewICETCPMux(nil, tcpMuxLn, 8) iceTCPMux = webrtc.NewICETCPMux(nil, tcpMuxLn, 8)
} }
@ -157,8 +168,10 @@ func newWebRTCServer(
ctx: ctx, ctx: ctx,
ctxCancel: ctxCancel, ctxCancel: ctxCancel,
ln: ln, ln: ln,
udpMuxLn: udpMuxLn,
tcpMuxLn: tcpMuxLn, tcpMuxLn: tcpMuxLn,
tlsConfig: tlsConfig, tlsConfig: tlsConfig,
iceUDPMux: iceUDPMux,
iceTCPMux: iceTCPMux, iceTCPMux: iceTCPMux,
iceHostNAT1To1IPs: iceHostNAT1To1IPs, iceHostNAT1To1IPs: iceHostNAT1To1IPs,
conns: make(map[*webRTCConn]struct{}), conns: make(map[*webRTCConn]struct{}),
@ -168,11 +181,14 @@ func newWebRTCServer(
chAPIConnsKick: make(chan webRTCServerAPIConnsKickReq), chAPIConnsKick: make(chan webRTCServerAPIConnsKickReq),
} }
s.log(logger.Info, "listener opened on "+address) str := "listener opened on " + address + " (HTTP)"
if udpMuxLn != nil {
str += ", " + iceUDPMuxAddress + " (ICE/UDP)"
}
if tcpMuxLn != nil { if tcpMuxLn != nil {
s.log(logger.Info, "ice mux tcp listener opened on "+iceTCPMuxAddress) str += ", " + iceTCPMuxAddress + " (ICE/TCP)"
} }
s.log(logger.Info, str)
if s.metrics != nil { if s.metrics != nil {
s.metrics.webRTCServerSet(s) s.metrics.webRTCServerSet(s)
@ -232,8 +248,9 @@ outer:
&s.wg, &s.wg,
s.pathManager, s.pathManager,
s, s,
s.iceTCPMux,
s.iceHostNAT1To1IPs, s.iceHostNAT1To1IPs,
s.iceUDPMux,
s.iceTCPMux,
) )
s.conns[c] = struct{}{} s.conns[c] = struct{}{}
@ -281,12 +298,15 @@ outer:
s.ctxCancel() s.ctxCancel()
hs.Shutdown(context.Background()) hs.Shutdown(context.Background())
s.ln.Close() // in case Shutdown() is called before Serve()
if s.udpMuxLn != nil {
s.udpMuxLn.Close()
}
if s.tcpMuxLn != nil { if s.tcpMuxLn != nil {
s.tcpMuxLn.Close() s.tcpMuxLn.Close()
} }
s.ln.Close() // in case Shutdown() is called before Serve()
} }
func (s *webRTCServer) onRequest(ctx *gin.Context) { func (s *webRTCServer) onRequest(ctx *gin.Context) {

6
rtsp-simple-server.yml

@ -199,9 +199,15 @@ webrtcICEServers: [stun:stun.l.google.com:19302]
# List of public IP addresses that are to be used as a host. # List of public IP addresses that are to be used as a host.
# This is used typically for servers that are behind 1:1 D-NAT. # This is used typically for servers that are behind 1:1 D-NAT.
webrtcICEHostNAT1To1IPs: [] webrtcICEHostNAT1To1IPs: []
# Address of a ICE UDP listener in format host:port.
# If filled, ICE traffic will come through a single UDP port,
# allowing the deployment of the server inside a container or behind a NAT.
webrtcICEUDPMuxAddress:
# Address of a ICE TCP listener in format host:port. # Address of a ICE TCP listener in format host:port.
# If filled, ICE traffic will come through a single TCP port, # If filled, ICE traffic will come through a single TCP port,
# allowing the deployment of the server inside a container or behind a NAT. # allowing the deployment of the server inside a container or behind a NAT.
# At the moment, setting this parameter forces usage of the TCP protocol,
# which is not optimal for WebRTC.
webrtcICETCPMuxAddress: webrtcICETCPMuxAddress:
############################################### ###############################################

Loading…
Cancel
Save