2020-01-25 04:47:37 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-13 12:13:33 +13:00
|
|
|
"io"
|
2020-02-11 18:15:59 +13:00
|
|
|
"strings"
|
2020-01-25 04:47:37 +13:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v2"
|
2020-02-13 12:13:33 +13:00
|
|
|
"github.com/pion/webrtc/v2/pkg/media"
|
2020-01-25 04:47:37 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"n.eko.moe/neko/internal/gst"
|
|
|
|
"n.eko.moe/neko/internal/types"
|
2020-01-27 22:00:49 +13:00
|
|
|
"n.eko.moe/neko/internal/types/config"
|
2020-02-11 18:15:59 +13:00
|
|
|
"n.eko.moe/neko/internal/xorg"
|
2020-01-25 04:47:37 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(sessions types.SessionManager, config *config.WebRTC) *WebRTCManager {
|
|
|
|
logger := log.With().Str("module", "webrtc").Logger()
|
2020-02-10 14:58:24 +13:00
|
|
|
settings := webrtc.SettingEngine{
|
2020-01-25 04:47:37 +13:00
|
|
|
LoggerFactory: loggerFactory{
|
|
|
|
logger: logger,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:13:40 +13:00
|
|
|
settings.SetLite(true)
|
2020-02-10 14:58:24 +13:00
|
|
|
settings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
|
2020-02-10 20:13:40 +13:00
|
|
|
settings.SetEphemeralUDPPortRange(config.EphemeralMin, config.EphemeralMax)
|
|
|
|
settings.SetNAT1To1IPs(config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
|
2020-01-26 04:14:46 +13:00
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
// Create MediaEngine based off sdp
|
|
|
|
engine := webrtc.MediaEngine{}
|
|
|
|
engine.RegisterDefaultCodecs()
|
|
|
|
|
|
|
|
// Create API with MediaEngine and SettingEngine
|
|
|
|
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings))
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
return &WebRTCManager{
|
|
|
|
logger: logger,
|
2020-02-10 14:58:24 +13:00
|
|
|
settings: settings,
|
2020-01-25 04:47:37 +13:00
|
|
|
cleanup: time.NewTicker(1 * time.Second),
|
|
|
|
shutdown: make(chan bool),
|
|
|
|
sessions: sessions,
|
2020-02-13 12:13:33 +13:00
|
|
|
engine: engine,
|
2020-01-25 04:47:37 +13:00
|
|
|
config: config,
|
2020-02-13 12:13:33 +13:00
|
|
|
api: api,
|
2020-01-25 04:47:37 +13:00
|
|
|
configuration: &webrtc.Configuration{
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebRTCManager struct {
|
|
|
|
logger zerolog.Logger
|
2020-02-10 14:58:24 +13:00
|
|
|
settings webrtc.SettingEngine
|
2020-02-13 12:13:33 +13:00
|
|
|
engine webrtc.MediaEngine
|
|
|
|
api *webrtc.API
|
|
|
|
videoTrack *webrtc.Track
|
|
|
|
audioTrack *webrtc.Track
|
2020-01-25 04:47:37 +13:00
|
|
|
videoPipeline *gst.Pipeline
|
|
|
|
audioPipeline *gst.Pipeline
|
2020-02-13 12:13:33 +13:00
|
|
|
sessions types.SessionManager
|
2020-01-25 04:47:37 +13:00
|
|
|
cleanup *time.Ticker
|
|
|
|
config *config.WebRTC
|
2020-02-13 12:13:33 +13:00
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
shutdown chan bool
|
|
|
|
configuration *webrtc.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WebRTCManager) Start() {
|
2020-02-13 12:13:33 +13:00
|
|
|
// Set display and change to default resolution
|
2020-02-11 18:15:59 +13:00
|
|
|
xorg.Display(m.config.Display)
|
2020-02-11 19:10:54 +13:00
|
|
|
if !xorg.ValidScreenSize(m.config.ScreenWidth, m.config.ScreenHeight, m.config.ScreenRate) {
|
|
|
|
m.logger.Warn().Msgf("invalid screen option %dx%d@%d", m.config.ScreenWidth, m.config.ScreenHeight, m.config.ScreenRate)
|
|
|
|
} else {
|
|
|
|
if err := xorg.ChangeScreenSize(m.config.ScreenWidth, m.config.ScreenHeight, m.config.ScreenRate); err != nil {
|
|
|
|
m.logger.Warn().Err(err).Msg("unable to change screen size")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
// Create video track/pipeline
|
2020-01-25 04:47:37 +13:00
|
|
|
videoPipeline, err := gst.CreatePipeline(
|
|
|
|
m.config.VideoCodec,
|
2020-02-11 18:15:59 +13:00
|
|
|
m.config.Display,
|
|
|
|
m.config.VideoParams,
|
2020-01-25 04:47:37 +13:00
|
|
|
)
|
2020-02-13 12:13:33 +13:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
|
|
|
|
}
|
|
|
|
m.videoPipeline = videoPipeline
|
2020-01-25 04:47:37 +13:00
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
video, err := m.createVideoTrack()
|
2020-01-25 04:47:37 +13:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
|
|
|
|
}
|
2020-02-13 12:13:33 +13:00
|
|
|
m.videoTrack = video
|
2020-01-25 04:47:37 +13:00
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
// Create audio track/pipeline
|
2020-01-25 04:47:37 +13:00
|
|
|
audioPipeline, err := gst.CreatePipeline(
|
|
|
|
m.config.AudioCodec,
|
2020-02-11 18:15:59 +13:00
|
|
|
m.config.Device,
|
|
|
|
m.config.AudioParams,
|
2020-01-25 04:47:37 +13:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
|
|
|
|
}
|
|
|
|
m.audioPipeline = audioPipeline
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
audio, err := m.createAudioTrack()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
|
|
|
|
}
|
|
|
|
m.audioTrack = audio
|
2020-01-25 04:47:37 +13:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
m.logger.Info().Msg("shutdown")
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.shutdown:
|
|
|
|
return
|
2020-02-11 18:15:59 +13:00
|
|
|
case sample := <-m.videoPipeline.Sample:
|
2020-02-13 12:13:33 +13:00
|
|
|
if err := m.videoTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
|
2020-01-26 23:43:08 +13:00
|
|
|
m.logger.Warn().Err(err).Msg("video pipeline failed to write")
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
2020-02-11 18:15:59 +13:00
|
|
|
case sample := <-m.audioPipeline.Sample:
|
2020-02-13 12:13:33 +13:00
|
|
|
if err := m.audioTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
|
2020-01-26 23:43:08 +13:00
|
|
|
m.logger.Warn().Err(err).Msg("audio pipeline failed to write")
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
case <-m.cleanup.C:
|
2020-02-11 18:15:59 +13:00
|
|
|
xorg.CheckKeys(time.Second * 10)
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
m.sessions.OnHostCleared(func(id string) {
|
2020-02-11 18:15:59 +13:00
|
|
|
xorg.ResetKeys()
|
2020-01-25 04:47:37 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
m.sessions.OnCreated(func(id string, session types.Session) {
|
|
|
|
m.logger.Debug().Str("id", id).Msg("session created")
|
|
|
|
})
|
|
|
|
|
|
|
|
m.sessions.OnDestroy(func(id string) {
|
|
|
|
m.logger.Debug().Str("id", id).Msg("session destroyed")
|
|
|
|
})
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
// start pipelines
|
|
|
|
videoPipeline.Start()
|
|
|
|
audioPipeline.Start()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
// TODO: log resolution, bit rate and codec parameters
|
|
|
|
m.logger.Info().
|
|
|
|
Str("video_display", m.config.Display).
|
|
|
|
Str("video_codec", m.config.VideoCodec).
|
|
|
|
Str("audio_device", m.config.Device).
|
|
|
|
Str("audio_codec", m.config.AudioCodec).
|
2020-02-11 18:15:59 +13:00
|
|
|
Str("ephemeral_port_range", fmt.Sprintf("%d-%d", m.config.EphemeralMin, m.config.EphemeralMax)).
|
|
|
|
Str("nat_ips", strings.Join(m.config.NAT1To1IPs, ",")).
|
|
|
|
Str("audio_pipeline_src", audioPipeline.Src).
|
|
|
|
Str("video_pipeline_src", videoPipeline.Src).
|
2020-01-25 04:47:37 +13:00
|
|
|
Msgf("webrtc streaming")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WebRTCManager) Shutdown() error {
|
|
|
|
m.logger.Info().Msgf("webrtc shutting down")
|
|
|
|
m.videoPipeline.Stop()
|
|
|
|
m.audioPipeline.Stop()
|
|
|
|
m.cleanup.Stop()
|
|
|
|
m.shutdown <- true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
func (m *WebRTCManager) CreatePeer(id string, session types.Session) (string, error) {
|
2020-01-26 03:29:52 +13:00
|
|
|
// Create new peer connection
|
2020-02-13 12:13:33 +13:00
|
|
|
connection, err := m.api.NewPeerConnection(*m.configuration)
|
2020-01-25 04:47:37 +13:00
|
|
|
if err != nil {
|
2020-02-13 12:13:33 +13:00
|
|
|
return "", err
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
if _, err = connection.AddTransceiverFromTrack(m.videoTrack, webrtc.RtpTransceiverInit{
|
2020-01-25 04:47:37 +13:00
|
|
|
Direction: webrtc.RTPTransceiverDirectionSendonly,
|
2020-02-13 12:13:33 +13:00
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
if _, err = connection.AddTransceiverFromTrack(m.audioTrack, webrtc.RtpTransceiverInit{
|
2020-01-25 04:47:37 +13:00
|
|
|
Direction: webrtc.RTPTransceiverDirectionSendonly,
|
2020-02-13 12:13:33 +13:00
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
description, err := connection.CreateOffer(nil)
|
2020-01-25 04:47:37 +13:00
|
|
|
if err != nil {
|
2020-02-13 12:13:33 +13:00
|
|
|
return "", err
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
connection.OnDataChannel(func(d *webrtc.DataChannel) {
|
|
|
|
d.OnMessage(func(msg webrtc.DataChannelMessage) {
|
|
|
|
if err = m.handle(id, msg); err != nil {
|
|
|
|
m.logger.Warn().Err(err).Msg("data handle failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
connection.SetLocalDescription(description)
|
2020-01-25 04:47:37 +13:00
|
|
|
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
|
|
|
switch state {
|
|
|
|
case webrtc.PeerConnectionStateDisconnected:
|
|
|
|
case webrtc.PeerConnectionStateFailed:
|
|
|
|
m.logger.Info().Str("id", id).Msg("peer disconnected")
|
|
|
|
m.sessions.Destroy(id)
|
|
|
|
break
|
|
|
|
case webrtc.PeerConnectionStateConnected:
|
|
|
|
m.logger.Info().Str("id", id).Msg("peer connected")
|
2020-02-13 12:13:33 +13:00
|
|
|
if err = session.SetConnected(true); err != nil {
|
|
|
|
m.logger.Warn().Err(err).Msg("unable to set connected on peer")
|
|
|
|
m.sessions.Destroy(id)
|
|
|
|
}
|
2020-01-25 04:47:37 +13:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-13 12:13:33 +13:00
|
|
|
if err := session.SetPeer(&Peer{
|
2020-01-25 04:47:37 +13:00
|
|
|
id: id,
|
2020-02-13 12:13:33 +13:00
|
|
|
manager: m,
|
2020-01-25 04:47:37 +13:00
|
|
|
connection: connection,
|
2020-02-13 12:13:33 +13:00
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return description.SDP, nil
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
2020-02-11 18:15:59 +13:00
|
|
|
|
|
|
|
func (m *WebRTCManager) ChangeScreenSize(width int, height int, rate int) error {
|
2020-02-11 19:10:54 +13:00
|
|
|
if !xorg.ValidScreenSize(width, height, rate) {
|
|
|
|
return fmt.Errorf("unknown configuration")
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
m.videoPipeline.Stop()
|
2020-02-11 19:10:54 +13:00
|
|
|
defer func() {
|
|
|
|
m.videoPipeline.Start()
|
|
|
|
m.logger.Info().Msg("starting pipeline")
|
|
|
|
}()
|
2020-02-11 18:15:59 +13:00
|
|
|
|
|
|
|
if err := xorg.ChangeScreenSize(width, height, rate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
videoPipeline, err := gst.CreatePipeline(
|
|
|
|
m.config.VideoCodec,
|
|
|
|
m.config.Display,
|
|
|
|
m.config.VideoParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Panic().Err(err).Msg("unable to create new video pipeline")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.videoPipeline = videoPipeline
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|