mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package webrtc
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type WebRTCPeerCtx struct {
|
|
mu sync.Mutex
|
|
logger zerolog.Logger
|
|
api *webrtc.API
|
|
connection *webrtc.PeerConnection
|
|
dataChannel *webrtc.DataChannel
|
|
changeVideo func(videoID string) error
|
|
iceTrickle bool
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
|
|
ICERestart: ICERestart,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !peer.iceTrickle {
|
|
// 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
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
|
SDP: sdp,
|
|
Type: webrtc.SDPTypeAnswer,
|
|
})
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
|
|
return peer.connection.AddICECandidate(candidate)
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
peer.logger.Info().Str("video_id", videoID).Msg("change video id")
|
|
return peer.changeVideo(videoID)
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) Destroy() {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
|
return
|
|
}
|
|
|
|
// TODO: Send webrtc disconnect event via websocket.
|
|
|
|
if err := peer.connection.Close(); err != nil {
|
|
peer.logger.Warn().Err(err).Msg("peer connection destroyed with an error")
|
|
} else {
|
|
peer.logger.Info().Msg("peer connection destroyed")
|
|
}
|
|
|
|
peer.connection = nil
|
|
}
|