2020-10-22 16:54:50 +02:00
|
|
|
package webrtc
|
|
|
|
|
2021-06-30 00:04:41 +02:00
|
|
|
import (
|
2022-10-17 13:39:31 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2021-06-30 00:04:41 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
2021-08-29 18:59:46 +02:00
|
|
|
"github.com/rs/zerolog"
|
2022-02-10 00:12:30 +01:00
|
|
|
|
2022-10-17 13:39:31 +02:00
|
|
|
"github.com/demodesk/neko/internal/webrtc/payload"
|
2022-07-14 00:58:22 +02:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
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
|
2022-10-25 20:25:00 +02:00
|
|
|
changeVideo func(bitrate int) error
|
|
|
|
videoId func() string
|
2022-03-26 23:20:38 +01:00
|
|
|
setPaused func(isPaused bool)
|
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()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return nil, types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2021-06-27 22:02:05 +02:00
|
|
|
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
|
|
|
|
ICERestart: ICERestart,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:36:47 +01:00
|
|
|
return peer.setLocalDescription(offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) CreateAnswer() (*webrtc.SessionDescription, error) {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return nil, types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:36:47 +01:00
|
|
|
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-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)
|
|
|
|
|
2021-11-28 18:36:47 +01:00
|
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
2021-06-27 22:02:05 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-gatherComplete
|
|
|
|
} else {
|
2021-11-28 18:36:47 +01:00
|
|
|
if err := peer.connection.SetLocalDescription(description); err != nil {
|
2021-06-27 22:02:05 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer.connection.LocalDescription(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:36:47 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2021-11-25 00:07:17 +01:00
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
|
|
|
SDP: sdp,
|
|
|
|
Type: webrtc.SDPTypeOffer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:36:47 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
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-11-28 18:36:47 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
return peer.connection.AddICECandidate(candidate)
|
2021-02-02 20:43:33 +01:00
|
|
|
}
|
|
|
|
|
2022-10-25 20:25:00 +02:00
|
|
|
func (peer *WebRTCPeerCtx) SetVideoBitrate(bitrate int) error {
|
2021-06-30 00:04:41 +02:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2022-02-10 00:12:30 +01:00
|
|
|
if peer.connection == nil {
|
|
|
|
return types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
2022-10-25 20:25:00 +02:00
|
|
|
peer.logger.Info().Int("bitrate", bitrate).Msg("change video bitrate")
|
|
|
|
return peer.changeVideo(bitrate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Refactor.
|
|
|
|
func (peer *WebRTCPeerCtx) GetVideoId() string {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
return peer.videoId()
|
2021-02-04 20:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 23:20:38 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetPaused(isPaused bool) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
if peer.connection == nil {
|
|
|
|
return types.ErrWebRTCConnectionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.logger.Info().Bool("is_paused", isPaused).Msg("set paused")
|
|
|
|
peer.setPaused(isPaused)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:39:31 +02:00
|
|
|
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
if peer.dataChannel == nil {
|
|
|
|
return types.ErrWebRTCDataChannelNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
if peer.dataChannel == nil {
|
|
|
|
return types.ErrWebRTCDataChannelNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
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-09-18 00:56:03 +02:00
|
|
|
if peer.connection != nil {
|
|
|
|
err := peer.connection.Close()
|
|
|
|
peer.logger.Err(err).Msg("peer connection destroyed")
|
|
|
|
peer.connection = nil
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
}
|