webrtc improved logging & close on disconnect or fail.

This commit is contained in:
Miroslav Šedivý 2021-02-02 16:21:20 +01:00
parent 5a1fbadcd5
commit 45cf44835c

View File

@ -89,7 +89,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
settings := webrtc.SettingEngine{ settings := webrtc.SettingEngine{
LoggerFactory: loggerFactory{ LoggerFactory: loggerFactory{
logger: manager.logger, logger: manager.logger.With().Str("id", session.ID()).Logger(),
}, },
} }
@ -108,7 +108,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost) settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
// Create MediaEngine based off sdp // Create MediaEngine
engine := &webrtc.MediaEngine{} engine := &webrtc.MediaEngine{}
if err := manager.videoCodec.Register(engine); err != nil { if err := manager.videoCodec.Register(engine); err != nil {
@ -119,10 +119,10 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
return "", manager.config.ICELite, manager.config.ICEServers, err return "", manager.config.ICELite, manager.config.ICEServers, err
} }
// Create API with MediaEngine and SettingEngine // Create NewAPI with MediaEngine and SettingEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings)) api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings))
// Create new peer connection // Create NewPeerConnection
connection, err := api.NewPeerConnection(*configuration) connection, err := api.NewPeerConnection(*configuration)
if err != nil { if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return "", manager.config.ICELite, manager.config.ICEServers, err
@ -150,18 +150,6 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
return "", manager.config.ICELite, manager.config.ICEServers, err return "", manager.config.ICELite, manager.config.ICEServers, err
} }
connection.OnDataChannel(func(channel *webrtc.DataChannel) {
channel.OnMessage(func(message webrtc.DataChannelMessage) {
if !session.IsHost() {
return
}
if err = manager.handle(message); err != nil {
manager.logger.Warn().Err(err).Msg("data handle failed")
}
})
})
// TODO: Refactor, send request to client. // TODO: Refactor, send request to client.
gatherComplete := webrtc.GatheringCompletePromise(connection) gatherComplete := webrtc.GatheringCompletePromise(connection)
@ -174,20 +162,28 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
manager.logger.Info().Str("id", session.ID()).Msg("peer connected")
session.SetWebRTCConnected(true) session.SetWebRTCConnected(true)
case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateDisconnected:
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected") fallthrough
session.SetWebRTCConnected(false)
case webrtc.PeerConnectionStateFailed: case webrtc.PeerConnectionStateFailed:
manager.logger.Warn().Str("id", session.ID()).Msg("peer failed") connection.Close()
session.SetWebRTCConnected(false)
case webrtc.PeerConnectionStateClosed: case webrtc.PeerConnectionStateClosed:
manager.logger.Warn().Str("id", session.ID()).Msg("peer closed")
session.SetWebRTCConnected(false) session.SetWebRTCConnected(false)
} }
}) })
connection.OnDataChannel(func(channel *webrtc.DataChannel) {
channel.OnMessage(func(message webrtc.DataChannelMessage) {
if !session.IsHost() {
return
}
if err = manager.handle(message); err != nil {
manager.logger.Warn().Err(err).Msg("data handle failed")
}
})
})
session.SetWebRTCPeer(&WebRTCPeerCtx{ session.SetWebRTCPeer(&WebRTCPeerCtx{
api: api, api: api,
engine: engine, engine: engine,