2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
2021-06-30 10:04:41 +12:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
2021-08-30 04:59:46 +12:00
|
|
|
"github.com/rs/zerolog"
|
2021-06-30 10:04:41 +12:00
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-26 06:41:40 +13:00
|
|
|
type WebRTCPeerCtx struct {
|
2021-06-30 10:04:41 +12:00
|
|
|
mu sync.Mutex
|
2021-08-30 04:59:46 +12:00
|
|
|
logger zerolog.Logger
|
2021-02-15 02:40:17 +13:00
|
|
|
connection *webrtc.PeerConnection
|
|
|
|
dataChannel *webrtc.DataChannel
|
|
|
|
changeVideo func(videoID string) error
|
2021-06-28 08:05:37 +12:00
|
|
|
iceTrickle bool
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-06-28 08:05:37 +12:00
|
|
|
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
|
2021-06-30 10:04:41 +12:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-06-28 08:02:05 +12:00
|
|
|
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
|
|
|
|
ICERestart: ICERestart,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
return peer.setLocalDescription(offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) CreateAnswer() (*webrtc.SessionDescription, error) {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
answer, err := peer.connection.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer.setLocalDescription(answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) setLocalDescription(description webrtc.SessionDescription) (*webrtc.SessionDescription, error) {
|
2021-06-28 08:05:37 +12:00
|
|
|
if !peer.iceTrickle {
|
2021-06-28 08:02:05 +12:00
|
|
|
// Create channel that is blocked until ICE Gathering is complete
|
|
|
|
gatherComplete := webrtc.GatheringCompletePromise(peer.connection)
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
2021-06-28 08:02:05 +12:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-gatherComplete
|
|
|
|
} else {
|
2021-11-29 06:36:47 +13:00
|
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
2021-06-28 08:02:05 +12:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer.connection.LocalDescription(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-11-25 12:07:17 +13:00
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
|
|
|
SDP: sdp,
|
|
|
|
Type: webrtc.SDPTypeOffer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
2021-02-15 02:40:17 +13:00
|
|
|
SDP: sdp,
|
2020-11-02 04:09:48 +13:00
|
|
|
Type: webrtc.SDPTypeAnswer,
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
return peer.connection.AddICECandidate(candidate)
|
2021-02-03 08:43:33 +13:00
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
2021-06-30 10:04:41 +12:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-08-30 04:59:46 +12:00
|
|
|
peer.logger.Info().Str("video_id", videoID).Msg("change video id")
|
2021-02-15 05:11:21 +13:00
|
|
|
return peer.changeVideo(videoID)
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-08-30 05:17:10 +12:00
|
|
|
func (peer *WebRTCPeerCtx) Destroy() {
|
2021-06-30 10:04:41 +12:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2021-09-18 10:56:03 +12:00
|
|
|
if peer.connection != nil {
|
|
|
|
err := peer.connection.Close()
|
|
|
|
peer.logger.Err(err).Msg("peer connection destroyed")
|
|
|
|
peer.connection = nil
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|