2020-10-22 16:54:50 +02:00
|
|
|
package webrtc
|
|
|
|
|
2021-06-30 00:04:41 +02:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
2021-08-29 18:59:46 +02:00
|
|
|
"github.com/rs/zerolog"
|
2021-06-30 00:04:41 +02:00
|
|
|
)
|
2020-10-22 16:54:50 +02:00
|
|
|
|
2020-11-25 18:41:40 +01:00
|
|
|
type WebRTCPeerCtx struct {
|
2021-06-30 00:04:41 +02:00
|
|
|
mu sync.Mutex
|
2021-08-29 18:59:46 +02:00
|
|
|
logger zerolog.Logger
|
2021-02-14 14:40:17 +01:00
|
|
|
connection *webrtc.PeerConnection
|
|
|
|
dataChannel *webrtc.DataChannel
|
|
|
|
changeVideo func(videoID string) error
|
2021-06-27 22:05:37 +02:00
|
|
|
iceTrickle bool
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 22:05:37 +02:00
|
|
|
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
|
2021-06-30 00:04:41 +02:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-06-27 22:02:05 +02:00
|
|
|
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
|
|
|
|
ICERestart: ICERestart,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-27 22:05:37 +02:00
|
|
|
if !peer.iceTrickle {
|
2021-06-27 22:02:05 +02:00
|
|
|
// Create channel that is blocked until ICE Gathering is complete
|
|
|
|
gatherComplete := webrtc.GatheringCompletePromise(peer.connection)
|
|
|
|
|
|
|
|
if err := peer.connection.SetLocalDescription(offer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-gatherComplete
|
|
|
|
} else {
|
|
|
|
if err := peer.connection.SetLocalDescription(offer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer.connection.LocalDescription(), nil
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
2021-02-14 14:40:17 +01:00
|
|
|
SDP: sdp,
|
2020-11-01 16:09:48 +01:00
|
|
|
Type: webrtc.SDPTypeAnswer,
|
|
|
|
})
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
|
|
|
|
return peer.connection.AddICECandidate(candidate)
|
2021-02-02 20:43:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
2021-06-30 00:04:41 +02:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-08-29 18:59:46 +02:00
|
|
|
peer.logger.Info().Str("video_id", videoID).Msg("change video id")
|
2021-02-14 17:11:21 +01:00
|
|
|
return peer.changeVideo(videoID)
|
2021-02-04 20:39:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 19:17:10 +02:00
|
|
|
func (peer *WebRTCPeerCtx) Destroy() {
|
2021-06-30 00:04:41 +02:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
2021-08-29 19:17:10 +02:00
|
|
|
return
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
2021-02-02 18:28:32 +01:00
|
|
|
|
2021-08-29 19:17:10 +02:00
|
|
|
// TODO: Send webrtc disconnect event via websocket.
|
|
|
|
|
2021-09-02 21:58:00 +02:00
|
|
|
err := peer.connection.Close()
|
|
|
|
peer.logger.Err(err).Msg("peer connection destroyed")
|
2021-08-29 19:17:10 +02:00
|
|
|
|
|
|
|
peer.connection = nil
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|