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/webrtc.go

202 lines
6.1 KiB
Go
Raw Normal View History

2020-01-25 04:47:37 +13:00
package webrtc
import (
"fmt"
2020-02-13 12:13:33 +13:00
"io"
2020-04-06 10:34:51 +12:00
"math/rand"
2020-02-11 18:15:59 +13:00
"strings"
2020-01-25 04:47:37 +13:00
"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/types"
"n.eko.moe/neko/internal/types/config"
2020-01-25 04:47:37 +13:00
)
2020-04-06 10:34:51 +12:00
func New(sessions types.SessionManager, remote types.RemoteManager, config *config.WebRTC) *WebRTCManager {
2020-01-25 04:47:37 +13:00
return &WebRTCManager{
2020-04-05 19:07:45 +12:00
logger: log.With().Str("module", "webrtc").Logger(),
2020-04-06 10:34:51 +12:00
remote: remote,
2020-01-25 04:47:37 +13:00
sessions: sessions,
config: config,
}
}
type WebRTCManager struct {
2020-04-06 10:34:51 +12:00
logger zerolog.Logger
videoTrack *webrtc.Track
audioTrack *webrtc.Track
videoCodec *webrtc.RTPCodec
audioCodec *webrtc.RTPCodec
sessions types.SessionManager
remote types.RemoteManager
config *config.WebRTC
2020-01-25 04:47:37 +13:00
}
2020-04-06 10:34:51 +12:00
func (manager *WebRTCManager) Start() {
2020-02-14 16:52:43 +13:00
var err error
2020-04-06 10:34:51 +12:00
manager.audioTrack, manager.audioCodec, err = manager.createTrack(manager.remote.AudioCodec())
2020-02-13 12:13:33 +13:00
if err != nil {
2020-04-06 10:34:51 +12:00
manager.logger.Panic().Err(err).Msg("unable to create audio track")
2020-02-13 12:13:33 +13:00
}
2020-01-25 04:47:37 +13:00
2020-04-06 10:34:51 +12:00
manager.remote.OnAudioFrame(func(sample types.Sample) {
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-01-25 04:47:37 +13:00
}
})
2020-04-06 10:34:51 +12:00
manager.videoTrack, manager.videoCodec, err = manager.createTrack(manager.remote.VideoCodec())
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video track")
}
2020-01-25 04:47:37 +13:00
2020-04-06 10:34:51 +12:00
manager.remote.OnVideoFrame(func(sample types.Sample) {
if err := manager.videoTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
manager.logger.Warn().Err(err).Msg("video pipeline failed to write")
}
2020-01-25 04:47:37 +13:00
})
2020-04-06 10:34:51 +12:00
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-01-25 04:47:37 +13:00
}
2020-04-06 10:34:51 +12:00
func (manager *WebRTCManager) Shutdown() error {
manager.logger.Info().Msgf("webrtc shutting down")
2020-01-25 04:47:37 +13:00
return nil
}
2020-04-06 10:34:51 +12:00
func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (string, bool, []string, error) {
2020-04-05 19:07:45 +12:00
configuration := &webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
2020-04-06 10:34:51 +12:00
URLs: manager.config.ICEServers,
2020-04-05 19:07:45 +12:00
},
},
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
settings := webrtc.SettingEngine{
LoggerFactory: loggerFactory{
2020-04-06 10:34:51 +12:00
logger: manager.logger,
2020-04-05 19:07:45 +12:00
},
}
2020-04-06 10:34:51 +12:00
if manager.config.ICELite {
2020-04-05 19:07:45 +12:00
configuration = &webrtc.Configuration{
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
settings.SetLite(true)
}
2020-04-06 10:34:51 +12:00
settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
2020-04-05 19:07:45 +12:00
// Create MediaEngine based off sdp
engine := webrtc.MediaEngine{}
2020-04-06 10:34:51 +12:00
engine.RegisterCodec(manager.audioCodec)
engine.RegisterCodec(manager.videoCodec)
2020-04-05 19:07:45 +12:00
// Create API with MediaEngine and SettingEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings))
2020-01-26 03:29:52 +13:00
// Create new peer connection
2020-04-05 19:07:45 +12:00
connection, err := api.NewPeerConnection(*configuration)
2020-01-25 04:47:37 +13:00
if err != nil {
2020-04-06 10:34:51 +12:00
return "", manager.config.ICELite, manager.config.ICEServers, err
2020-01-25 04:47:37 +13:00
}
2020-04-06 10:34:51 +12:00
if _, err = connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{
2020-01-25 04:47:37 +13:00
Direction: webrtc.RTPTransceiverDirectionSendonly,
2020-02-13 12:13:33 +13:00
}); err != nil {
2020-04-06 10:34:51 +12:00
return "", manager.config.ICELite, manager.config.ICEServers, err
2020-01-25 04:47:37 +13:00
}
2020-04-06 10:34:51 +12:00
if _, err = connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{
2020-01-25 04:47:37 +13:00
Direction: webrtc.RTPTransceiverDirectionSendonly,
2020-02-13 12:13:33 +13:00
}); err != nil {
2020-04-06 10:34:51 +12:00
return "", manager.config.ICELite, manager.config.ICEServers, 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-04-06 10:34:51 +12:00
return "", manager.config.ICELite, manager.config.ICEServers, err
2020-01-25 04:47:37 +13:00
}
connection.OnDataChannel(func(d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) {
2020-04-06 10:34:51 +12:00
if err = manager.handle(id, msg); err != nil {
manager.logger.Warn().Err(err).Msg("data handle failed")
2020-01-25 04:47:37 +13:00
}
})
})
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:
2020-04-06 10:34:51 +12:00
manager.logger.Info().Str("id", id).Msg("peer disconnected")
manager.sessions.Destroy(id)
2020-01-25 04:47:37 +13:00
break
case webrtc.PeerConnectionStateConnected:
2020-04-06 10:34:51 +12:00
manager.logger.Info().Str("id", id).Msg("peer connected")
2020-02-13 12:13:33 +13:00
if err = session.SetConnected(true); err != nil {
2020-04-06 10:34:51 +12:00
manager.logger.Warn().Err(err).Msg("unable to set connected on peer")
manager.sessions.Destroy(id)
2020-02-13 12:13:33 +13:00
}
2020-01-25 04:47:37 +13:00
break
}
})
2020-02-13 12:13:33 +13:00
if err := session.SetPeer(&Peer{
2020-04-05 19:07:45 +12:00
id: id,
api: api,
engine: &engine,
2020-04-06 10:34:51 +12:00
manager: manager,
2020-04-05 19:07:45 +12:00
settings: &settings,
connection: connection,
configuration: configuration,
2020-02-13 12:13:33 +13:00
}); err != nil {
2020-04-06 10:34:51 +12:00
return "", manager.config.ICELite, manager.config.ICEServers, err
2020-02-13 12:13:33 +13:00
}
2020-04-06 10:34:51 +12:00
return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil
2020-01-25 04:47:37 +13:00
}
2020-02-11 18:15:59 +13:00
2020-04-06 10:34:51 +12:00
func (m *WebRTCManager) createTrack(codecName string) (*webrtc.Track, *webrtc.RTPCodec, error) {
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)
2020-02-11 18:15:59 +13:00
}
2020-04-06 10:34:51 +12:00
track, err := webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec)
2020-02-11 18:15:59 +13:00
if err != nil {
2020-04-06 10:34:51 +12:00
return nil, nil, err
2020-02-11 18:15:59 +13:00
}
2020-04-06 10:34:51 +12:00
return track, codec, nil
2020-02-11 18:15:59 +13:00
}