@ -15,6 +15,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
"github.com/pion/ice/v2"
"github.com/pion/webrtc/v3"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/logger"
"github.com/aler9/rtsp-simple-server/internal/logger"
@ -81,9 +83,13 @@ type webRTCServer struct {
ctxCancel func ( )
ctxCancel func ( )
wg sync . WaitGroup
wg sync . WaitGroup
ln net . Listener
ln net . Listener
tcpMuxLn net . Listener
tlsConfig * tls . Config
tlsConfig * tls . Config
conns map [ * webRTCConn ] struct { }
conns map [ * webRTCConn ] struct { }
iceTCPMux ice . TCPMux
iceHostNAT1To1IPs [ ] string
// in
// in
connNew chan webRTCConnNewReq
connNew chan webRTCConnNewReq
chConnClose chan * webRTCConn
chConnClose chan * webRTCConn
@ -105,6 +111,8 @@ func newWebRTCServer(
pathManager * pathManager ,
pathManager * pathManager ,
metrics * metrics ,
metrics * metrics ,
parent webRTCServerParent ,
parent webRTCServerParent ,
iceHostNAT1To1IPs [ ] string ,
iceTCPMuxAddress string ,
) ( * webRTCServer , error ) {
) ( * webRTCServer , error ) {
ln , err := net . Listen ( "tcp" , address )
ln , err := net . Listen ( "tcp" , address )
if err != nil {
if err != nil {
@ -124,6 +132,18 @@ func newWebRTCServer(
}
}
}
}
var iceTCPMux ice . TCPMux
var tcpMuxLn net . Listener
if iceTCPMuxAddress != "" {
tcpMuxLn , err = net . Listen ( "tcp" , iceTCPMuxAddress )
if err != nil {
tcpMuxLn . Close ( )
return nil , err
}
iceTCPMux = webrtc . NewICETCPMux ( nil , tcpMuxLn , 8 )
}
ctx , ctxCancel := context . WithCancel ( parentCtx )
ctx , ctxCancel := context . WithCancel ( parentCtx )
s := & webRTCServer {
s := & webRTCServer {
@ -138,7 +158,10 @@ func newWebRTCServer(
ctx : ctx ,
ctx : ctx ,
ctxCancel : ctxCancel ,
ctxCancel : ctxCancel ,
ln : ln ,
ln : ln ,
tcpMuxLn : tcpMuxLn ,
tlsConfig : tlsConfig ,
tlsConfig : tlsConfig ,
iceTCPMux : iceTCPMux ,
iceHostNAT1To1IPs : iceHostNAT1To1IPs ,
conns : make ( map [ * webRTCConn ] struct { } ) ,
conns : make ( map [ * webRTCConn ] struct { } ) ,
connNew : make ( chan webRTCConnNewReq ) ,
connNew : make ( chan webRTCConnNewReq ) ,
chConnClose : make ( chan * webRTCConn ) ,
chConnClose : make ( chan * webRTCConn ) ,
@ -148,6 +171,10 @@ func newWebRTCServer(
s . log ( logger . Info , "listener opened on " + address )
s . log ( logger . Info , "listener opened on " + address )
if tcpMuxLn != nil {
s . log ( logger . Info , "ice mux tcp listener opened on " + iceTCPMuxAddress )
}
if s . metrics != nil {
if s . metrics != nil {
s . metrics . webRTCServerSet ( s )
s . metrics . webRTCServerSet ( s )
}
}
@ -206,6 +233,8 @@ outer:
& s . wg ,
& s . wg ,
s . pathManager ,
s . pathManager ,
s ,
s ,
s . iceTCPMux ,
s . iceHostNAT1To1IPs ,
)
)
s . conns [ c ] = struct { } { }
s . conns [ c ] = struct { } { }
@ -253,6 +282,11 @@ outer:
s . ctxCancel ( )
s . ctxCancel ( )
hs . Shutdown ( context . Background ( ) )
hs . Shutdown ( context . Background ( ) )
if s . tcpMuxLn != nil {
s . tcpMuxLn . Close ( )
}
s . ln . Close ( ) // in case Shutdown() is called before Serve()
s . ln . Close ( ) // in case Shutdown() is called before Serve()
}
}