2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
2021-08-03 20:47:05 +12:00
|
|
|
"errors"
|
2020-10-23 03:54:50 +13:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-02-15 02:40:17 +13:00
|
|
|
"strings"
|
2021-02-15 06:16:24 +13:00
|
|
|
"sync"
|
2021-02-26 05:16:37 +13:00
|
|
|
"time"
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-02 11:50:18 +13:00
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
"github.com/pion/webrtc/v3/pkg/media"
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2021-02-03 08:43:33 +13:00
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2021-03-01 06:37:35 +13:00
|
|
|
"demodesk/neko/internal/webrtc/cursor"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2021-02-26 05:16:37 +13:00
|
|
|
// the duration without network activity before a Agent is considered disconnected. Default is 5 Seconds
|
|
|
|
const disconnectedTimeout = 4 * time.Second
|
|
|
|
|
|
|
|
// the duration without network activity before a Agent is considered failed after disconnected. Default is 25 Seconds
|
|
|
|
const failedTimeout = 6 * time.Second
|
|
|
|
|
|
|
|
// how often the ICE Agent sends extra traffic if there is no activity, if media is flowing no traffic will be sent. Default is 2 seconds
|
|
|
|
const keepAliveInterval = 2 * time.Second
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx {
|
|
|
|
return &WebRTCManagerCtx{
|
2021-09-27 09:26:22 +13:00
|
|
|
logger: log.With().Str("module", "webrtc").Logger(),
|
|
|
|
config: config,
|
|
|
|
|
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
|
|
|
curImage: cursor.NewImage(desktop),
|
|
|
|
curPosition: cursor.NewPosition(desktop),
|
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
participants: 0,
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type WebRTCManagerCtx struct {
|
2021-09-27 09:26:22 +13:00
|
|
|
mu sync.Mutex
|
|
|
|
logger zerolog.Logger
|
|
|
|
config *config.WebRTC
|
|
|
|
|
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
|
|
|
curImage *cursor.ImageCtx
|
|
|
|
curPosition *cursor.PositionCtx
|
|
|
|
|
|
|
|
audioTrack *webrtc.TrackLocalStaticSample
|
|
|
|
audioListener func(sample types.Sample)
|
|
|
|
participants uint32
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) Start() {
|
2020-10-23 03:54:50 +13:00
|
|
|
var err error
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2021-02-02 11:50:18 +13:00
|
|
|
// create audio track
|
2021-02-06 05:40:29 +13:00
|
|
|
audio := manager.capture.Audio()
|
|
|
|
manager.audioTrack, err = webrtc.NewTrackLocalStaticSample(audio.Codec().Capability, "audio", "stream")
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create audio track")
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:26:22 +13:00
|
|
|
manager.audioListener = func(sample types.Sample) {
|
2021-08-30 03:09:13 +12:00
|
|
|
if err := manager.audioTrack.WriteSample(media.Sample(sample)); err != nil {
|
2021-08-03 20:47:05 +12:00
|
|
|
if errors.Is(err, io.ErrClosedPipe) {
|
|
|
|
// The peerConnection has been closed.
|
|
|
|
return
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.logger.Warn().Err(err).Msg("audio pipeline failed to write")
|
|
|
|
}
|
2021-02-07 00:59:57 +13:00
|
|
|
}
|
2021-09-27 09:26:22 +13:00
|
|
|
audio.AddListener(&manager.audioListener)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-08-30 04:59:46 +12:00
|
|
|
manager.curImage.Start()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.logger.Info().
|
2021-08-03 21:06:34 +12:00
|
|
|
Bool("icelite", manager.config.ICELite).
|
|
|
|
Bool("icetrickle", manager.config.ICETrickle).
|
|
|
|
Interface("iceservers", manager.config.ICEServers).
|
2021-03-18 03:47:49 +13:00
|
|
|
Str("nat1to1", strings.Join(manager.config.NAT1To1IPs, ",")).
|
|
|
|
Str("epr", fmt.Sprintf("%d-%d", manager.config.EphemeralMin, manager.config.EphemeralMax)).
|
2021-08-03 21:06:34 +12:00
|
|
|
Msg("webrtc starting")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) Shutdown() error {
|
2021-09-02 10:00:29 +12:00
|
|
|
manager.logger.Info().Msg("shutdown")
|
2021-02-07 00:59:57 +13:00
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
manager.curImage.Shutdown()
|
|
|
|
manager.curPosition.Shutdown()
|
|
|
|
|
2021-09-27 09:26:22 +13:00
|
|
|
audio := manager.capture.Audio()
|
|
|
|
audio.RemoveListener(&manager.audioListener)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 03:47:49 +13:00
|
|
|
func (manager *WebRTCManagerCtx) ICEServers() []types.ICEServer {
|
2021-02-03 07:21:48 +13:00
|
|
|
return manager.config.ICEServers
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-08 05:07:55 +13:00
|
|
|
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (*webrtc.SessionDescription, error) {
|
2021-08-30 04:59:46 +12:00
|
|
|
// add session id to logger context
|
2021-03-14 11:27:28 +13:00
|
|
|
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Info().Msg("creating webrtc peer")
|
2021-02-03 08:43:33 +13:00
|
|
|
|
2021-09-27 09:26:22 +13:00
|
|
|
// all videos must have the same codec
|
|
|
|
videoStream, ok := manager.capture.Video(videoID)
|
|
|
|
if !ok {
|
|
|
|
return nil, types.ErrWebRTCVideoNotFound
|
2021-03-24 01:46:54 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:26:22 +13:00
|
|
|
connection, err := manager.newPeerConnection(videoStream.Codec(), logger)
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
2021-02-03 07:21:48 +13:00
|
|
|
return nil, err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:26:22 +13:00
|
|
|
// asynchronously send local ICE Candidates
|
2021-02-03 09:12:11 +13:00
|
|
|
if manager.config.ICETrickle {
|
|
|
|
connection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
|
|
|
if candidate == nil {
|
|
|
|
logger.Debug().Msg("all local ice candidates sent")
|
|
|
|
return
|
|
|
|
}
|
2021-02-03 08:43:33 +13:00
|
|
|
|
2021-09-02 07:58:39 +12:00
|
|
|
session.Send(
|
|
|
|
event.SIGNAL_CANDIDATE,
|
2021-02-03 09:12:11 +13:00
|
|
|
message.SignalCandidate{
|
2021-09-02 07:58:39 +12:00
|
|
|
ICECandidateInit: candidate.ToJSON(),
|
2021-02-03 09:12:11 +13:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-02-03 08:43:33 +13:00
|
|
|
|
2021-02-07 06:16:24 +13:00
|
|
|
// create video track
|
|
|
|
videoTrack, err := webrtc.NewTrackLocalStaticSample(videoStream.Codec().Capability, "video", "stream")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
videoListener := func(sample types.Sample) {
|
2021-08-30 03:09:13 +12:00
|
|
|
if err := videoTrack.WriteSample(media.Sample(sample)); err != nil {
|
2021-08-03 20:47:05 +12:00
|
|
|
if errors.Is(err, io.ErrClosedPipe) {
|
|
|
|
// The peerConnection has been closed.
|
|
|
|
return
|
|
|
|
}
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Warn().Err(err).Msg("video pipeline failed to write")
|
2021-02-07 06:16:24 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
|
2021-02-07 06:16:24 +13:00
|
|
|
// should be stream started
|
|
|
|
if videoStream.ListenersCount() == 0 {
|
|
|
|
if err := videoStream.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
videoStream.AddListener(&videoListener)
|
|
|
|
|
|
|
|
// start audio, when first participant connects
|
|
|
|
if !manager.capture.Audio().Started() {
|
|
|
|
if err := manager.capture.Audio().Start(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to start audio stream")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.participants = manager.participants + 1
|
|
|
|
manager.mu.Unlock()
|
2021-02-07 06:16:24 +13:00
|
|
|
|
|
|
|
changeVideo := func(videoID string) error {
|
|
|
|
newVideoStream, ok := manager.capture.Video(videoID)
|
|
|
|
if !ok {
|
2021-08-30 03:09:13 +12:00
|
|
|
return types.ErrWebRTCVideoNotFound
|
2021-02-07 06:16:24 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// should be new stream started
|
|
|
|
if newVideoStream.ListenersCount() == 0 {
|
|
|
|
if err := newVideoStream.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
// switch videoListeners
|
|
|
|
videoStream.RemoveListener(&videoListener)
|
|
|
|
newVideoStream.AddListener(&videoListener)
|
2021-02-07 06:16:24 +13:00
|
|
|
|
|
|
|
// should be old stream stopped
|
|
|
|
if videoStream.ListenersCount() == 0 {
|
|
|
|
videoStream.Stop()
|
|
|
|
}
|
2021-02-15 02:40:17 +13:00
|
|
|
|
2021-02-07 06:16:24 +13:00
|
|
|
videoStream = newVideoStream
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-04 06:53:35 +13:00
|
|
|
rtpAudio, err := connection.AddTrack(manager.audioTrack)
|
2021-02-05 09:39:48 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-04 06:53:35 +13:00
|
|
|
rtpVideo, err := connection.AddTrack(videoTrack)
|
2021-02-05 09:39:48 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-13 11:45:16 +13:00
|
|
|
dataChannel, err := connection.CreateDataChannel("data", nil)
|
2021-02-06 05:57:33 +13:00
|
|
|
if err != nil {
|
2021-02-03 07:36:11 +13:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-14 06:25:40 +13:00
|
|
|
peer := &WebRTCPeerCtx{
|
2021-08-30 04:59:46 +12:00
|
|
|
logger: logger,
|
2021-02-15 02:40:17 +13:00
|
|
|
connection: connection,
|
|
|
|
dataChannel: dataChannel,
|
2021-09-03 06:48:15 +12:00
|
|
|
changeVideo: changeVideo,
|
2021-06-28 08:05:37 +12:00
|
|
|
iceTrickle: manager.config.ICETrickle,
|
2021-02-14 06:25:40 +13:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
cursorImage := func(entry *cursor.ImageEntry) {
|
|
|
|
if err := peer.SendCursorImage(entry.Cursor, entry.Image); err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
logger.Err(err).Msg("could not send cursor image")
|
2021-02-14 06:25:40 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorPosition := func(x, y int) {
|
|
|
|
if session.IsHost() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := peer.SendCursorPosition(x, y); err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
logger.Err(err).Msg("could not send cursor position")
|
2021-02-14 06:25:40 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
|
|
|
switch state {
|
2020-12-04 05:03:15 +13:00
|
|
|
case webrtc.PeerConnectionStateConnected:
|
2021-02-15 05:17:06 +13:00
|
|
|
session.SetWebRTCConnected(peer, true)
|
2020-10-23 03:54:50 +13:00
|
|
|
case webrtc.PeerConnectionStateDisconnected:
|
2021-02-03 04:21:20 +13:00
|
|
|
fallthrough
|
2020-10-23 03:54:50 +13:00
|
|
|
case webrtc.PeerConnectionStateFailed:
|
2021-02-03 04:21:20 +13:00
|
|
|
connection.Close()
|
2020-12-04 05:03:15 +13:00
|
|
|
case webrtc.PeerConnectionStateClosed:
|
2021-02-15 06:16:24 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
|
2021-02-15 05:17:06 +13:00
|
|
|
session.SetWebRTCConnected(peer, false)
|
2021-02-15 06:16:24 +13:00
|
|
|
videoStream.RemoveListener(&videoListener)
|
2021-02-07 06:16:24 +13:00
|
|
|
|
|
|
|
// should be stream stopped
|
|
|
|
if videoStream.ListenersCount() == 0 {
|
|
|
|
videoStream.Stop()
|
|
|
|
}
|
2021-02-14 06:25:40 +13:00
|
|
|
|
2021-02-15 06:16:24 +13:00
|
|
|
// decrease participants
|
|
|
|
manager.participants = manager.participants - 1
|
|
|
|
|
|
|
|
// stop audio, if last participant disonnects
|
|
|
|
if manager.participants <= 0 {
|
|
|
|
manager.participants = 0
|
|
|
|
|
|
|
|
if manager.capture.Audio().Started() {
|
|
|
|
manager.capture.Audio().Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.mu.Unlock()
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-14 06:25:40 +13:00
|
|
|
dataChannel.OnOpen(func() {
|
2021-03-01 06:37:35 +13:00
|
|
|
manager.curImage.AddListener(&cursorImage)
|
|
|
|
manager.curPosition.AddListener(&cursorPosition)
|
2021-02-18 09:55:21 +13:00
|
|
|
|
|
|
|
// send initial cursor image
|
2021-03-01 06:50:32 +13:00
|
|
|
entry, err := manager.curImage.Get()
|
2021-02-26 02:24:17 +13:00
|
|
|
if err == nil {
|
2021-03-01 06:37:35 +13:00
|
|
|
cursorImage(entry)
|
2021-02-26 02:24:17 +13:00
|
|
|
} else {
|
2021-09-02 09:10:06 +12:00
|
|
|
logger.Err(err).Msg("failed to get cursor image")
|
2021-02-26 02:24:17 +13:00
|
|
|
}
|
2021-02-21 12:41:04 +13:00
|
|
|
|
2021-02-18 09:55:21 +13:00
|
|
|
// send initial cursor position
|
2021-03-01 06:50:32 +13:00
|
|
|
x, y := manager.curPosition.Get()
|
2021-02-18 09:55:21 +13:00
|
|
|
cursorPosition(x, y)
|
2021-02-14 06:25:40 +13:00
|
|
|
})
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
dataChannel.OnClose(func() {
|
|
|
|
manager.curImage.RemoveListener(&cursorImage)
|
|
|
|
manager.curPosition.RemoveListener(&cursorPosition)
|
|
|
|
})
|
|
|
|
|
2021-02-13 11:45:16 +13:00
|
|
|
dataChannel.OnMessage(func(message webrtc.DataChannelMessage) {
|
|
|
|
if !session.IsHost() {
|
|
|
|
return
|
|
|
|
}
|
2021-02-13 09:06:42 +13:00
|
|
|
|
2021-09-03 07:06:58 +12:00
|
|
|
if err = manager.handle(message.Data, session); err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
logger.Err(err).Msg("data handle failed")
|
2021-02-13 11:45:16 +13:00
|
|
|
}
|
|
|
|
})
|
2021-02-03 04:21:20 +13:00
|
|
|
|
2021-03-04 06:53:35 +13:00
|
|
|
go func() {
|
|
|
|
rtcpBuf := make([]byte, 1500)
|
|
|
|
for {
|
|
|
|
if _, _, err := rtpAudio.Read(rtcpBuf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
rtcpBuf := make([]byte, 1500)
|
|
|
|
for {
|
|
|
|
if _, _, err := rtpVideo.Read(rtcpBuf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-14 06:25:40 +13:00
|
|
|
session.SetWebRTCPeer(peer)
|
2021-06-28 08:05:37 +12:00
|
|
|
return peer.CreateOffer(false)
|
2021-02-03 07:21:48 +13:00
|
|
|
}
|