WebRTC channel from server.

This commit is contained in:
Miroslav Šedivý 2021-02-12 23:45:16 +01:00
parent 90aa2ce51a
commit 8a3b3f14cb

View File

@ -188,7 +188,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
return nil, err return nil, err
} }
_, err = connection.CreateDataChannel("data", nil) dataChannel, err := connection.CreateDataChannel("data", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -213,12 +213,6 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
} }
} }
peer := &WebRTCPeerCtx{
api: api,
connection: connection,
changeVideo: changeVideo,
}
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
@ -238,21 +232,23 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
} }
}) })
connection.OnDataChannel(func(channel *webrtc.DataChannel) { dataChannel.OnMessage(func(message webrtc.DataChannelMessage) {
peer.dataChannel = channel if !session.IsHost() {
return
}
channel.OnMessage(func(message webrtc.DataChannelMessage) { if err = manager.handle(message); err != nil {
if !session.IsHost() { logger.Warn().Err(err).Msg("data handle failed")
return }
} })
if err = manager.handle(message); err != nil { session.SetWebRTCPeer(&WebRTCPeerCtx{
logger.Warn().Err(err).Msg("data handle failed") api: api,
} connection: connection,
}) changeVideo: changeVideo,
dataChannel: dataChannel,
}) })
session.SetWebRTCPeer(peer)
return connection.LocalDescription(), nil return connection.LocalDescription(), nil
} }