2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2021-02-02 11:50:18 +13:00
|
|
|
"demodesk/neko/internal/types/codec"
|
2021-02-03 08:43:33 +13:00
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx {
|
|
|
|
return &WebRTCManagerCtx{
|
2021-02-06 05:40:29 +13:00
|
|
|
logger: log.With().Str("module", "webrtc").Logger(),
|
|
|
|
defaultVideoID: capture.VideoIDs()[0],
|
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
|
|
|
config: config,
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type WebRTCManagerCtx struct {
|
2021-02-06 05:40:29 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
videoTracks map[string]*webrtc.TrackLocalStaticSample
|
|
|
|
audioTrack *webrtc.TrackLocalStaticSample
|
2021-02-07 00:59:57 +13:00
|
|
|
unsubscribe []func()
|
2021-02-06 05:40:29 +13:00
|
|
|
defaultVideoID string
|
|
|
|
audioCodec codec.RTPCodec
|
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
|
|
|
config *config.WebRTC
|
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-02-07 00:59:57 +13:00
|
|
|
listener := func(sample types.Sample) {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := manager.audioTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
|
|
|
|
manager.logger.Warn().Err(err).Msg("audio pipeline failed to write")
|
|
|
|
}
|
2021-02-07 00:59:57 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
audio.AddListener(listener)
|
|
|
|
manager.unsubscribe = append(manager.unsubscribe, func(){
|
|
|
|
audio.RemoveListener(listener)
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
|
2021-02-06 05:40:29 +13:00
|
|
|
videoIDs := manager.capture.VideoIDs()
|
|
|
|
manager.videoTracks = map[string]*webrtc.TrackLocalStaticSample{}
|
|
|
|
for _, videoID := range videoIDs {
|
2021-02-06 08:35:30 +13:00
|
|
|
videoID := videoID
|
|
|
|
|
2021-02-06 06:07:58 +13:00
|
|
|
video, ok := manager.capture.Video(videoID)
|
|
|
|
if !ok {
|
|
|
|
manager.logger.Warn().Str("videoID", videoID).Msg("video stream not found, skipping")
|
|
|
|
continue
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-06 05:40:29 +13:00
|
|
|
track, err := webrtc.NewTrackLocalStaticSample(video.Codec().Capability, "video", "stream")
|
|
|
|
if err != nil {
|
2021-02-06 08:35:30 +13:00
|
|
|
manager.logger.Panic().Err(err).Str("videoID", videoID).Msg("unable to create video track")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-02-07 00:59:57 +13:00
|
|
|
|
|
|
|
listener := func(sample types.Sample) {
|
2021-02-06 05:40:29 +13:00
|
|
|
if err := track.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
|
2021-02-06 08:35:30 +13:00
|
|
|
manager.logger.Warn().Err(err).Str("videoID", videoID).Msg("vide pipeline failed to write")
|
2021-02-06 05:40:29 +13:00
|
|
|
}
|
2021-02-07 00:59:57 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
video.AddListener(listener)
|
|
|
|
manager.unsubscribe = append(manager.unsubscribe, func(){
|
|
|
|
video.RemoveListener(listener)
|
2021-02-06 05:40:29 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
manager.videoTracks[videoID] = track
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)).
|
2021-02-03 09:12:11 +13:00
|
|
|
Str("ice_trickle", fmt.Sprintf("%t", manager.config.ICETrickle)).
|
2020-10-23 03:54:50 +13:00
|
|
|
Str("ice_servers", strings.Join(manager.config.ICEServers, ",")).
|
|
|
|
Str("ephemeral_port_range", fmt.Sprintf("%d-%d", manager.config.EphemeralMin, manager.config.EphemeralMax)).
|
|
|
|
Str("nat_ips", strings.Join(manager.config.NAT1To1IPs, ",")).
|
|
|
|
Msgf("webrtc starting")
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) Shutdown() error {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.logger.Info().Msgf("webrtc shutting down")
|
2021-02-07 00:59:57 +13:00
|
|
|
|
|
|
|
for _, unsubscribe := range manager.unsubscribe {
|
|
|
|
unsubscribe()
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) ICELite() bool {
|
|
|
|
return manager.config.ICELite
|
|
|
|
}
|
2020-10-29 08:54:09 +13:00
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) ICEServers() []string {
|
|
|
|
return manager.config.ICEServers
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.SessionDescription, error) {
|
2021-02-03 08:43:33 +13:00
|
|
|
logger := manager.logger.With().Str("id", session.ID()).Logger()
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
// Create MediaEngine
|
2021-02-03 07:21:48 +13:00
|
|
|
engine, err := manager.mediaEngine()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-02-02 11:50:18 +13:00
|
|
|
}
|
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
// Custom settings & configuration
|
2021-02-03 08:43:33 +13:00
|
|
|
settings := manager.apiSettings(logger)
|
2021-02-03 07:21:48 +13:00
|
|
|
configuration := manager.apiConfiguration()
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-03 04:21:20 +13:00
|
|
|
// Create NewAPI with MediaEngine and SettingEngine
|
2021-02-03 07:21:48 +13:00
|
|
|
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(*settings))
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
connection, err := api.NewPeerConnection(*configuration)
|
|
|
|
if err != nil {
|
2021-02-03 07:21:48 +13:00
|
|
|
return nil, err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-02-03 08:43:33 +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-02-03 09:12:11 +13:00
|
|
|
ICECandidateInit := candidate.ToJSON()
|
|
|
|
err := session.Send(
|
|
|
|
message.SignalCandidate{
|
|
|
|
Event: event.SIGNAL_CANDIDATE,
|
|
|
|
ICECandidateInit: &ICECandidateInit,
|
|
|
|
})
|
2021-02-03 08:43:33 +13:00
|
|
|
|
2021-02-03 09:12:11 +13:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn().Err(err).Msg("sending ice candidate failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-03 08:43:33 +13:00
|
|
|
|
2021-02-06 08:35:30 +13:00
|
|
|
_, err = connection.AddTrack(manager.audioTrack)
|
2021-02-05 09:39:48 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-06 08:35:30 +13:00
|
|
|
videoSender, err := connection.AddTrack(manager.videoTracks[manager.defaultVideoID])
|
2021-02-05 09:39:48 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-06 05:57:33 +13:00
|
|
|
_, err = connection.CreateDataChannel("data", nil)
|
|
|
|
if err != nil {
|
2021-02-03 07:36:11 +13:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-02 11:50:18 +13:00
|
|
|
offer, err := connection.CreateOffer(nil)
|
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-02-03 09:12:11 +13:00
|
|
|
if !manager.config.ICETrickle {
|
|
|
|
// Create channel that is blocked until ICE Gathering is complete
|
|
|
|
gatherComplete := webrtc.GatheringCompletePromise(connection)
|
|
|
|
|
|
|
|
if err := connection.SetLocalDescription(offer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-gatherComplete
|
|
|
|
} else {
|
|
|
|
if err := connection.SetLocalDescription(offer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-29 08:56:14 +13:00
|
|
|
}
|
|
|
|
|
2021-02-07 00:52:02 +13:00
|
|
|
connection.OnNegotiationNeeded(func() {
|
|
|
|
logger.Warn().Msg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!negotiation needed!")
|
|
|
|
})
|
|
|
|
|
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:
|
|
|
|
session.SetWebRTCConnected(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:
|
2020-11-26 08:26:11 +13:00
|
|
|
session.SetWebRTCConnected(false)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-03 04:21:20 +13:00
|
|
|
connection.OnDataChannel(func(channel *webrtc.DataChannel) {
|
|
|
|
channel.OnMessage(func(message webrtc.DataChannelMessage) {
|
|
|
|
if !session.IsHost() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = manager.handle(message); err != nil {
|
2021-02-03 08:43:33 +13:00
|
|
|
logger.Warn().Err(err).Msg("data handle failed")
|
2021-02-03 04:21:20 +13:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-26 06:41:40 +13:00
|
|
|
session.SetWebRTCPeer(&WebRTCPeerCtx{
|
2021-02-06 08:35:30 +13:00
|
|
|
api: api,
|
|
|
|
engine: engine,
|
|
|
|
settings: settings,
|
|
|
|
connection: connection,
|
|
|
|
configuration: configuration,
|
|
|
|
videoTracks: manager.videoTracks,
|
|
|
|
videoSender: videoSender,
|
2020-11-01 09:46:29 +13:00
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
return connection.LocalDescription(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *WebRTCManagerCtx) mediaEngine() (*webrtc.MediaEngine, error) {
|
|
|
|
engine := &webrtc.MediaEngine{}
|
|
|
|
|
2021-02-06 05:40:29 +13:00
|
|
|
// all videos must have the same codec
|
2021-02-06 06:07:58 +13:00
|
|
|
video, ok := manager.capture.Video(manager.defaultVideoID)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("default video track not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
videoCodec := video.Codec()
|
2021-02-06 05:40:29 +13:00
|
|
|
if err := videoCodec.Register(engine); err != nil {
|
2021-02-03 07:21:48 +13:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-06 05:40:29 +13:00
|
|
|
audioCodec := manager.capture.Audio().Codec()
|
|
|
|
if err := audioCodec.Register(engine); err != nil {
|
2021-02-03 07:21:48 +13:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}
|
|
|
|
|
2021-02-03 08:43:33 +13:00
|
|
|
func (manager *WebRTCManagerCtx) apiSettings(logger zerolog.Logger) *webrtc.SettingEngine {
|
2021-02-03 07:21:48 +13:00
|
|
|
settings := &webrtc.SettingEngine{
|
|
|
|
LoggerFactory: loggerFactory{
|
2021-02-03 08:43:33 +13:00
|
|
|
logger: logger,
|
2021-02-03 07:21:48 +13:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
|
|
|
|
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
|
|
|
|
settings.SetLite(manager.config.ICELite)
|
2021-02-03 09:12:11 +13:00
|
|
|
|
2021-02-03 07:21:48 +13:00
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *WebRTCManagerCtx) apiConfiguration() *webrtc.Configuration {
|
|
|
|
if manager.config.ICELite {
|
|
|
|
return &webrtc.Configuration{
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &webrtc.Configuration{
|
|
|
|
ICEServers: []webrtc.ICEServer{
|
|
|
|
{
|
|
|
|
URLs: manager.config.ICEServers,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
}
|