2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
2021-06-30 10:04:41 +12:00
|
|
|
import (
|
2022-10-18 00:39:31 +13:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2021-06-30 10:04:41 +12:00
|
|
|
"sync"
|
|
|
|
|
2023-04-11 07:44:17 +12:00
|
|
|
"github.com/pion/interceptor/pkg/cc"
|
2023-04-11 07:21:11 +12:00
|
|
|
"github.com/pion/rtcp"
|
2021-06-30 10:04:41 +12:00
|
|
|
"github.com/pion/webrtc/v3"
|
2021-08-30 04:59:46 +12:00
|
|
|
"github.com/rs/zerolog"
|
2022-02-10 12:12:30 +13:00
|
|
|
|
2022-10-18 00:39:31 +13:00
|
|
|
"github.com/demodesk/neko/internal/webrtc/payload"
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
2023-04-11 07:44:17 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types/event"
|
|
|
|
"github.com/demodesk/neko/pkg/types/message"
|
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 {
|
2023-04-11 07:21:11 +12:00
|
|
|
mu sync.Mutex
|
|
|
|
logger zerolog.Logger
|
2023-04-11 07:37:39 +12:00
|
|
|
session types.Session
|
|
|
|
metrics *metrics
|
2023-04-11 07:21:11 +12:00
|
|
|
connection *webrtc.PeerConnection
|
2023-04-11 07:44:17 +12:00
|
|
|
estimator cc.BandwidthEstimator
|
2023-04-11 07:21:11 +12:00
|
|
|
// tracks & channels
|
|
|
|
audioTrack *Track
|
|
|
|
videoTrack *Track
|
|
|
|
dataChannel *webrtc.DataChannel
|
|
|
|
rtcpChannel chan []rtcp.Packet
|
|
|
|
// config
|
|
|
|
iceTrickle bool
|
|
|
|
// deprecated functions
|
2023-04-11 07:44:17 +12:00
|
|
|
videoId func() string
|
|
|
|
setPaused func(isPaused bool)
|
|
|
|
setVideoAuto func(auto bool)
|
|
|
|
getVideoAuto func() 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
|
|
|
}
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
func (peer *WebRTCPeerCtx) SetVideoBitrate(peerBitrate int) error {
|
2021-06-30 10:04:41 +12:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2023-04-11 07:44:17 +12:00
|
|
|
// when switching from manual to auto bitrate estimation, in case the estimator is
|
|
|
|
// idle (lastBitrate > maxBitrate), we want to go back to the previous estimated bitrate
|
|
|
|
if peerBitrate == 0 && peer.estimator != nil {
|
|
|
|
peerBitrate = peer.estimator.GetTargetBitrate()
|
|
|
|
peer.logger.Debug().
|
|
|
|
Int("peer_bitrate", peerBitrate).
|
|
|
|
Msg("evaluated bitrate")
|
|
|
|
}
|
|
|
|
|
|
|
|
changed, err := peer.videoTrack.SetBitrate(peerBitrate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !changed {
|
|
|
|
// TODO: return error?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
videoID := peer.videoTrack.stream.ID()
|
|
|
|
bitrate := peer.videoTrack.stream.Bitrate()
|
|
|
|
|
|
|
|
peer.metrics.SetVideoID(videoID)
|
|
|
|
peer.logger.Debug().
|
|
|
|
Int("peer_bitrate", peerBitrate).
|
|
|
|
Int("video_bitrate", bitrate).
|
|
|
|
Str("video_id", videoID).
|
|
|
|
Msg("peer bitrate triggered video stream change")
|
|
|
|
|
|
|
|
go peer.session.Send(
|
|
|
|
event.SIGNAL_VIDEO,
|
|
|
|
message.SignalVideo{
|
|
|
|
Video: videoID,
|
|
|
|
Bitrate: bitrate,
|
|
|
|
VideoAuto: peer.videoTrack.VideoAuto(),
|
|
|
|
})
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
2023-04-11 07:44:17 +12:00
|
|
|
changed, err := peer.videoTrack.SetVideoID(videoID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !changed {
|
|
|
|
// TODO: return error?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bitrate := peer.videoTrack.stream.Bitrate()
|
|
|
|
|
|
|
|
peer.logger.Debug().
|
|
|
|
Str("video_id", videoID).
|
|
|
|
Int("video_bitrate", bitrate).
|
|
|
|
Msg("peer video id triggered video stream change")
|
|
|
|
|
|
|
|
go peer.session.Send(
|
|
|
|
event.SIGNAL_VIDEO,
|
|
|
|
message.SignalVideo{
|
|
|
|
Video: videoID,
|
|
|
|
Bitrate: bitrate,
|
|
|
|
VideoAuto: peer.videoTrack.VideoAuto(),
|
|
|
|
})
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
return nil
|
2022-10-26 07:25:00 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Refactor.
|
2023-02-07 07:45:51 +13:00
|
|
|
func (peer *WebRTCPeerCtx) GetVideoID() string {
|
2022-10-26 07:25:00 +13:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
return peer.videoId()
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2022-03-27 11:20:38 +13:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-18 00:39:31 +13:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2023-02-07 07:45:51 +13:00
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) SetVideoAuto(auto bool) {
|
|
|
|
peer.setVideoAuto(auto)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebRTCPeerCtx) VideoAuto() bool {
|
|
|
|
return peer.getVideoAuto()
|
|
|
|
}
|