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 SignalCandidate(candidate webrtc.ICECandidateInit) error
SetVideoID(videoID string) error SetVideoID(videoID string) error
Send(data []byte) error
Destroy() error Destroy() error
} }

View File

@ -213,9 +213,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
} }
} }
connection.OnNegotiationNeeded(func() { peer := &WebRTCPeerCtx{
logger.Warn().Msg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!negotiation needed!") api: api,
}) connection: connection,
changeVideo: changeVideo,
}
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
@ -237,6 +239,8 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
}) })
connection.OnDataChannel(func(channel *webrtc.DataChannel) { connection.OnDataChannel(func(channel *webrtc.DataChannel) {
peer.dataChannel = channel
channel.OnMessage(func(message webrtc.DataChannelMessage) { channel.OnMessage(func(message webrtc.DataChannelMessage) {
if !session.IsHost() { if !session.IsHost() {
return return
@ -248,15 +252,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
}) })
}) })
session.SetWebRTCPeer(&WebRTCPeerCtx{ session.SetWebRTCPeer(peer)
api: api,
engine: engine,
settings: settings,
connection: connection,
configuration: configuration,
changeVideo: changeVideo,
})
return connection.LocalDescription(), nil return connection.LocalDescription(), nil
} }

View File

@ -4,10 +4,8 @@ import "github.com/pion/webrtc/v3"
type WebRTCPeerCtx struct { type WebRTCPeerCtx struct {
api *webrtc.API api *webrtc.API
engine *webrtc.MediaEngine
settings *webrtc.SettingEngine
connection *webrtc.PeerConnection connection *webrtc.PeerConnection
configuration *webrtc.Configuration dataChannel *webrtc.DataChannel
changeVideo func(videoID string) error changeVideo func(videoID string) error
} }
@ -26,6 +24,14 @@ func (webrtc_peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
return webrtc_peer.changeVideo(videoID) 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 { func (webrtc_peer *WebRTCPeerCtx) Destroy() error {
if webrtc_peer.connection == nil || webrtc_peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { if webrtc_peer.connection == nil || webrtc_peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
return nil return nil