2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
"github.com/pion/webrtc/v2/pkg/media"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
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{
|
2020-10-23 03:54:50 +13:00
|
|
|
logger: log.With().Str("module", "webrtc").Logger(),
|
2020-11-02 04:09:48 +13:00
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
2020-10-23 03:54:50 +13:00
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type WebRTCManagerCtx struct {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
videoTrack *webrtc.Track
|
|
|
|
audioTrack *webrtc.Track
|
|
|
|
videoCodec *webrtc.RTPCodec
|
|
|
|
audioCodec *webrtc.RTPCodec
|
2020-11-02 04:09:48 +13:00
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
2020-10-23 03:54:50 +13:00
|
|
|
config *config.WebRTC
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
manager.audioTrack, manager.audioCodec, err = manager.createTrack(manager.capture.AudioCodec())
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create audio track")
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.capture.OnAudioFrame(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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.videoTrack, manager.videoCodec, err = manager.createTrack(manager.capture.VideoCodec())
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create video track")
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.capture.OnVideoFrame(func(sample types.Sample) {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := manager.videoTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
|
|
|
|
manager.logger.Warn().Err(err).Msg("video pipeline failed to write")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)).
|
|
|
|
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")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool, []string, error) {
|
2020-10-23 03:54:50 +13:00
|
|
|
configuration := &webrtc.Configuration{
|
|
|
|
ICEServers: []webrtc.ICEServer{
|
|
|
|
{
|
|
|
|
URLs: manager.config.ICEServers,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := webrtc.SettingEngine{
|
|
|
|
LoggerFactory: loggerFactory{
|
|
|
|
logger: manager.logger,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager.config.ICELite {
|
|
|
|
configuration = &webrtc.Configuration{
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
settings.SetLite(true)
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:54:09 +13:00
|
|
|
err := settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
|
|
|
|
if err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
|
|
|
|
|
|
|
|
// Create MediaEngine based off sdp
|
|
|
|
engine := webrtc.MediaEngine{}
|
|
|
|
|
|
|
|
engine.RegisterCodec(manager.audioCodec)
|
|
|
|
engine.RegisterCodec(manager.videoCodec)
|
|
|
|
|
|
|
|
// Create API with MediaEngine and SettingEngine
|
|
|
|
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings))
|
|
|
|
|
|
|
|
// Create new peer connection
|
|
|
|
connection, err := api.NewPeerConnection(*configuration)
|
|
|
|
if err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionSendonly,
|
|
|
|
}); err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionSendonly,
|
|
|
|
}); err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
|
|
|
description, err := connection.CreateOffer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.OnDataChannel(func(d *webrtc.DataChannel) {
|
|
|
|
d.OnMessage(func(msg webrtc.DataChannelMessage) {
|
2020-11-02 04:09:48 +13:00
|
|
|
if !session.IsHost() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = manager.handle(msg); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.logger.Warn().Err(err).Msg("data handle failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-29 08:56:14 +13:00
|
|
|
if err := connection.SetLocalDescription(description); err != nil {
|
|
|
|
return "", manager.config.ICELite, manager.config.ICEServers, err
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
manager.logger.Info().Str("id", session.ID()).Msg("peer connected")
|
|
|
|
session.SetWebRTCConnected(true)
|
2020-10-23 03:54:50 +13:00
|
|
|
case webrtc.PeerConnectionStateDisconnected:
|
2020-12-04 05:03:15 +13:00
|
|
|
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected")
|
2020-12-04 04:10:52 +13:00
|
|
|
session.SetWebRTCConnected(false)
|
2020-10-23 03:54:50 +13:00
|
|
|
case webrtc.PeerConnectionStateFailed:
|
2020-12-04 05:03:15 +13:00
|
|
|
manager.logger.Warn().Str("id", session.ID()).Msg("peer failed")
|
|
|
|
session.SetWebRTCConnected(false)
|
|
|
|
case webrtc.PeerConnectionStateClosed:
|
|
|
|
manager.logger.Warn().Str("id", session.ID()).Msg("peer closed")
|
2020-11-26 08:26:11 +13:00
|
|
|
session.SetWebRTCConnected(false)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-26 06:41:40 +13:00
|
|
|
session.SetWebRTCPeer(&WebRTCPeerCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
api: api,
|
|
|
|
engine: &engine,
|
|
|
|
settings: &settings,
|
|
|
|
connection: connection,
|
|
|
|
configuration: configuration,
|
2020-11-01 09:46:29 +13:00
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (m *WebRTCManagerCtx) createTrack(codecName string) (*webrtc.Track, *webrtc.RTPCodec, error) {
|
2020-10-23 03:54:50 +13:00
|
|
|
var codec *webrtc.RTPCodec
|
|
|
|
switch codecName {
|
|
|
|
case webrtc.VP8:
|
|
|
|
codec = webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000)
|
|
|
|
case webrtc.VP9:
|
|
|
|
codec = webrtc.NewRTPVP9Codec(webrtc.DefaultPayloadTypeVP9, 90000)
|
|
|
|
case webrtc.H264:
|
|
|
|
codec = webrtc.NewRTPH264Codec(webrtc.DefaultPayloadTypeH264, 90000)
|
|
|
|
case webrtc.Opus:
|
|
|
|
codec = webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000)
|
|
|
|
case webrtc.G722:
|
|
|
|
codec = webrtc.NewRTPG722Codec(webrtc.DefaultPayloadTypeG722, 8000)
|
|
|
|
case webrtc.PCMU:
|
|
|
|
codec = webrtc.NewRTPPCMUCodec(webrtc.DefaultPayloadTypePCMU, 8000)
|
|
|
|
case webrtc.PCMA:
|
|
|
|
codec = webrtc.NewRTPPCMACodec(webrtc.DefaultPayloadTypePCMA, 8000)
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("unknown codec %s", codecName)
|
|
|
|
}
|
|
|
|
|
|
|
|
track, err := webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return track, codec, nil
|
|
|
|
}
|