Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/webrtc/manager.go

227 lines
5.6 KiB
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package webrtc
import (
2020-01-19 12:30:09 +13:00
"fmt"
2020-01-14 21:36:48 +13:00
"time"
2020-01-13 21:05:38 +13:00
"github.com/pion/webrtc/v2"
2020-01-19 12:30:09 +13:00
"github.com/pkg/errors"
2020-01-13 21:05:38 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/config"
"n.eko.moe/neko/internal/event"
2020-01-13 21:05:38 +13:00
"n.eko.moe/neko/internal/gst"
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/hid"
"n.eko.moe/neko/internal/message"
"n.eko.moe/neko/internal/session"
2020-01-13 21:05:38 +13:00
)
2020-01-19 12:30:09 +13:00
func New(sessions *session.SessionManager, conf *config.WebRTC) *WebRTCManager {
logger := log.With().Str("module", "webrtc").Logger()
2020-01-13 21:05:38 +13:00
engine := webrtc.MediaEngine{}
2020-01-19 12:30:09 +13:00
engine.RegisterDefaultCodecs()
2020-01-13 21:05:38 +13:00
2020-01-19 12:30:09 +13:00
setings := webrtc.SettingEngine{
LoggerFactory: loggerFactory{
logger: logger,
},
2020-01-13 21:05:38 +13:00
}
return &WebRTCManager{
2020-01-19 12:30:09 +13:00
logger: logger,
engine: engine,
setings: setings,
api: webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(setings)),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
sessions: sessions,
conf: conf,
2020-01-13 21:05:38 +13:00
config: webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
},
2020-01-19 12:30:09 +13:00
}
2020-01-13 21:05:38 +13:00
}
type WebRTCManager struct {
2020-01-14 21:36:48 +13:00
logger zerolog.Logger
engine webrtc.MediaEngine
2020-01-19 12:30:09 +13:00
setings webrtc.SettingEngine
2020-01-14 21:36:48 +13:00
config webrtc.Configuration
2020-01-19 12:30:09 +13:00
sessions *session.SessionManager
api *webrtc.API
2020-01-14 21:36:48 +13:00
video *webrtc.Track
audio *webrtc.Track
videoPipeline *gst.Pipeline
audioPipeline *gst.Pipeline
2020-01-19 12:30:09 +13:00
cleanup *time.Ticker
conf *config.WebRTC
shutdown chan bool
2020-01-14 21:36:48 +13:00
}
2020-01-19 12:30:09 +13:00
func (m *WebRTCManager) Start() {
hid.Display(m.conf.Display)
switch m.conf.VideoCodec {
case "vp8":
if err := m.createVideoTrack(webrtc.DefaultPayloadTypeVP8); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
case "vp9":
if err := m.createVideoTrack(webrtc.DefaultPayloadTypeVP9); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
case "h264":
if err := m.createVideoTrack(webrtc.DefaultPayloadTypeH264); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
default:
m.logger.Panic().Err(errors.Errorf("unknown video codec %s", m.conf.AudioCodec)).Msg("unable to start webrtc manager")
}
switch m.conf.AudioCodec {
case "opus":
if err := m.createAudioTrack(webrtc.DefaultPayloadTypeOpus); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
case "g722":
if err := m.createAudioTrack(webrtc.DefaultPayloadTypeG722); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
case "pcmu":
if err := m.createAudioTrack(webrtc.DefaultPayloadTypePCMU); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
case "pcma":
if err := m.createAudioTrack(webrtc.DefaultPayloadTypePCMA); err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
default:
m.logger.Panic().Err(errors.Errorf("unknown audio codec %s", m.conf.AudioCodec)).Msg("unable to start webrtc manager")
}
m.videoPipeline.Start()
m.audioPipeline.Start()
2020-01-14 21:36:48 +13:00
go func() {
2020-01-19 12:30:09 +13:00
defer func() {
m.logger.Info().Msg("shutdown")
}()
2020-01-14 21:36:48 +13:00
for {
select {
2020-01-19 12:30:09 +13:00
case <-m.shutdown:
2020-01-14 21:36:48 +13:00
return
2020-01-19 12:30:09 +13:00
case <-m.cleanup.C:
hid.Check(time.Second * 10)
2020-01-14 21:36:48 +13:00
}
}
}()
2020-01-19 12:30:09 +13:00
m.sessions.OnHostCleared(func(id string) {
hid.Reset()
})
m.sessions.OnCreated(func(id string, session *session.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")
})
// TODO: log resolution, bit rate and codec parameters
m.logger.Info().
Str("video_display", m.conf.Display).
Str("video_codec", m.conf.VideoCodec).
Str("audio_device", m.conf.Device).
Str("audio_codec", m.conf.AudioCodec).
Msgf("webrtc streaming")
}
func (m *WebRTCManager) Shutdown() error {
m.logger.Info().Msgf("webrtc shutting down")
m.cleanup.Stop()
m.shutdown <- true
m.videoPipeline.Stop()
m.audioPipeline.Stop()
2020-01-14 21:36:48 +13:00
return nil
}
2020-01-19 12:30:09 +13:00
func (m *WebRTCManager) CreatePeer(id string, sdp string) error {
session, ok := m.sessions.Get(id)
if !ok {
return fmt.Errorf("invalid session id %s", id)
}
peer, err := m.api.NewPeerConnection(m.config)
if err != nil {
return err
}
if _, err := peer.AddTransceiverFromTrack(m.video, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return err
}
if _, err := peer.AddTransceiverFromTrack(m.audio, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return err
}
peer.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp,
Type: webrtc.SDPTypeOffer,
})
answer, err := peer.CreateAnswer(nil)
if err != nil {
return err
}
if err = peer.SetLocalDescription(answer); err != nil {
return err
}
2020-01-21 03:38:07 +13:00
if err := session.Send(message.Signal{
2020-01-21 05:22:24 +13:00
Event: event.SIGNAL_ANSWER,
SDP: answer.SDP,
2020-01-19 12:30:09 +13:00
}); err != nil {
return err
}
peer.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")
}
})
})
peer.OnConnectionStateChange(func(connectionState webrtc.PeerConnectionState) {
switch connectionState {
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")
break
}
})
m.sessions.SetPeer(id, peer)
2020-01-14 21:36:48 +13:00
return nil
2020-01-13 21:05:38 +13:00
}