mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
208 lines
4.5 KiB
Go
208 lines
4.5 KiB
Go
package webrtc
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"sync"
|
|
|
|
"github.com/pion/rtcp"
|
|
"github.com/pion/webrtc/v3"
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/demodesk/neko/internal/webrtc/payload"
|
|
"github.com/demodesk/neko/pkg/types"
|
|
)
|
|
|
|
type WebRTCPeerCtx struct {
|
|
mu sync.Mutex
|
|
logger zerolog.Logger
|
|
connection *webrtc.PeerConnection
|
|
// tracks & channels
|
|
audioTrack *Track
|
|
videoTrack *Track
|
|
dataChannel *webrtc.DataChannel
|
|
rtcpChannel chan []rtcp.Packet
|
|
// config
|
|
iceTrickle bool
|
|
// deprecated functions
|
|
changeVideoFromBitrate func(bitrate int)
|
|
changeVideoFromID func(id string) int
|
|
videoId func() string
|
|
setPaused func(isPaused bool)
|
|
setVideoAuto func(auto bool)
|
|
getVideoAuto func() 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
|
|
}
|
|
|
|
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) {
|
|
if !peer.iceTrickle {
|
|
// Create channel that is blocked until ICE Gathering is complete
|
|
gatherComplete := webrtc.GatheringCompletePromise(peer.connection)
|
|
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
<-gatherComplete
|
|
} else {
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return peer.connection.LocalDescription(), nil
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
|
SDP: sdp,
|
|
Type: webrtc.SDPTypeOffer,
|
|
})
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
|
SDP: sdp,
|
|
Type: webrtc.SDPTypeAnswer,
|
|
})
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
return peer.connection.AddICECandidate(candidate)
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoBitrate(peerBitrate int) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
peer.changeVideoFromBitrate(peerBitrate)
|
|
return nil
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
peer.changeVideoFromID(videoID)
|
|
return nil
|
|
}
|
|
|
|
// TODO: Refactor.
|
|
func (peer *WebRTCPeerCtx) GetVideoID() string {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
return peer.videoId()
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetPaused(isPaused bool) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
peer.logger.Info().Bool("is_paused", isPaused).Msg("set paused")
|
|
peer.setPaused(isPaused)
|
|
return nil
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
data := payload.CursorPosition{
|
|
Header: payload.Header{
|
|
Event: payload.OP_CURSOR_POSITION,
|
|
Length: 7,
|
|
},
|
|
X: uint16(x),
|
|
Y: uint16(y),
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
|
|
return err
|
|
}
|
|
|
|
return peer.dataChannel.Send(buffer.Bytes())
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) error {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
data := payload.CursorImage{
|
|
Header: payload.Header{
|
|
Event: payload.OP_CURSOR_IMAGE,
|
|
Length: uint16(11 + len(img)),
|
|
},
|
|
Width: cur.Width,
|
|
Height: cur.Height,
|
|
Xhot: cur.Xhot,
|
|
Yhot: cur.Yhot,
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := binary.Write(buffer, binary.BigEndian, img); err != nil {
|
|
return err
|
|
}
|
|
|
|
return peer.dataChannel.Send(buffer.Bytes())
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) Destroy() {
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
|
|
if peer.connection != nil {
|
|
err := peer.connection.Close()
|
|
peer.logger.Err(err).Msg("peer connection destroyed")
|
|
peer.connection = nil
|
|
}
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoAuto(auto bool) {
|
|
peer.setVideoAuto(auto)
|
|
}
|
|
|
|
func (peer *WebRTCPeerCtx) VideoAuto() bool {
|
|
return peer.getVideoAuto()
|
|
}
|