add WebRTC send function.

This commit is contained in:
Miroslav Šedivý 2021-02-12 21:06:42 +01:00
parent f22922191a
commit c8200e3bb7
3 changed files with 18 additions and 15 deletions

View File

@ -7,6 +7,7 @@ type WebRTCPeer interface {
SignalCandidate(candidate webrtc.ICECandidateInit) error
SetVideoID(videoID string) error
Send(data []byte) error
Destroy() error
}

View File

@ -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
}

View File

@ -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