diff --git a/internal/types/webrtc.go b/internal/types/webrtc.go index 637d15c3..d727a887 100644 --- a/internal/types/webrtc.go +++ b/internal/types/webrtc.go @@ -7,6 +7,7 @@ type WebRTCPeer interface { SignalCandidate(candidate webrtc.ICECandidateInit) error SetVideoID(videoID string) error + Send(data []byte) error Destroy() error } diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index db1e0c32..83713cb3 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -213,9 +213,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin } } - connection.OnNegotiationNeeded(func() { - logger.Warn().Msg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!negotiation needed!") - }) + peer := &WebRTCPeerCtx{ + api: api, + connection: connection, + changeVideo: changeVideo, + } connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { @@ -237,6 +239,8 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin }) connection.OnDataChannel(func(channel *webrtc.DataChannel) { + peer.dataChannel = channel + channel.OnMessage(func(message webrtc.DataChannelMessage) { if !session.IsHost() { return @@ -248,15 +252,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin }) }) - session.SetWebRTCPeer(&WebRTCPeerCtx{ - api: api, - engine: engine, - settings: settings, - connection: connection, - configuration: configuration, - changeVideo: changeVideo, - }) - + session.SetWebRTCPeer(peer) return connection.LocalDescription(), nil } diff --git a/internal/webrtc/peer.go b/internal/webrtc/peer.go index be46f150..2754a4ef 100644 --- a/internal/webrtc/peer.go +++ b/internal/webrtc/peer.go @@ -4,10 +4,8 @@ import "github.com/pion/webrtc/v3" type WebRTCPeerCtx struct { api *webrtc.API - engine *webrtc.MediaEngine - settings *webrtc.SettingEngine connection *webrtc.PeerConnection - configuration *webrtc.Configuration + dataChannel *webrtc.DataChannel changeVideo func(videoID string) error } @@ -26,6 +24,14 @@ func (webrtc_peer *WebRTCPeerCtx) SetVideoID(videoID string) error { return webrtc_peer.changeVideo(videoID) } +func (webrtc_peer *WebRTCPeerCtx) Send(data []byte) error { + if webrtc_peer.dataChannel == nil { + return nil + } + + return webrtc_peer.dataChannel.Send(data) +} + func (webrtc_peer *WebRTCPeerCtx) Destroy() error { if webrtc_peer.connection == nil || webrtc_peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { return nil