Archived
2
0

server -> client signaling

This commit is contained in:
Craig 2020-02-12 23:13:33 +00:00
parent 1f8b4d44c7
commit d5bff32302
17 changed files with 181 additions and 286 deletions

View File

@ -2,15 +2,7 @@ import EventEmitter from 'eventemitter3'
import { OPCODE } from './data' import { OPCODE } from './data'
import { EVENT, WebSocketEvents } from './events' import { EVENT, WebSocketEvents } from './events'
import { import { WebSocketMessages, WebSocketPayloads, SignalProvidePayload } from './messages'
WebSocketMessages,
WebSocketPayloads,
IdentityPayload,
SignalPayload,
MemberListPayload,
MemberPayload,
ControlPayload,
} from './messages'
export interface BaseEvents { export interface BaseEvents {
info: (...message: any[]) => void info: (...message: any[]) => void
@ -26,6 +18,11 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
protected _timeout?: NodeJS.Timeout protected _timeout?: NodeJS.Timeout
protected _username?: string protected _username?: string
protected _state: RTCIceConnectionState = 'disconnected' protected _state: RTCIceConnectionState = 'disconnected'
protected _id = ''
get id() {
return this._id
}
get supported() { get supported() {
return typeof RTCPeerConnection !== 'undefined' && typeof RTCPeerConnection.prototype.addTransceiver !== 'undefined' return typeof RTCPeerConnection !== 'undefined' && typeof RTCPeerConnection.prototype.addTransceiver !== 'undefined'
@ -93,6 +90,8 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
} }
this._state = 'disconnected' this._state = 'disconnected'
this._username = undefined
this._id = ''
} }
public sendData(event: 'wheel' | 'mousemove', data: { x: number; y: number }): void public sendData(event: 'wheel' | 'mousemove', data: { x: number; y: number }): void
@ -157,7 +156,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this._ws!.send(JSON.stringify({ event, ...payload })) this._ws!.send(JSON.stringify({ event, ...payload }))
} }
public createPeer() { public createPeer(sdp: string) {
this.emit('debug', `creating peer`) this.emit('debug', `creating peer`)
if (!this.socketOpen) { if (!this.socketOpen) {
this.emit( this.emit(
@ -175,18 +174,6 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this._peer = new RTCPeerConnection() this._peer = new RTCPeerConnection()
this._peer.onicecandidate = event => {
if (event.candidate === null && this._peer!.localDescription) {
this.emit('debug', `sending event '${EVENT.SIGNAL.PROVIDE}' with payload`, this._peer!.localDescription.sdp)
this._ws!.send(
JSON.stringify({
event: EVENT.SIGNAL.PROVIDE,
sdp: this._peer!.localDescription.sdp,
}),
)
}
}
this._peer.onconnectionstatechange = event => { this._peer.onconnectionstatechange = event => {
this.emit('debug', `peer connection state chagned`, this._peer ? this._peer.connectionState : undefined) this.emit('debug', `peer connection state chagned`, this._peer ? this._peer.connectionState : undefined)
} }
@ -227,43 +214,40 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this._channel.onmessage = this.onData.bind(this) this._channel.onmessage = this.onData.bind(this)
this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed')) this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed'))
this._peer.setRemoteDescription({ type: 'offer', sdp })
this._peer this._peer
.createOffer() .createAnswer()
.then(d => this._peer!.setLocalDescription(d)) .then(d => {
this._peer!.setLocalDescription(d)
this._ws!.send(
JSON.stringify({
event: EVENT.SIGNAL.ANSWER,
sdp: d.sdp,
username: this._username,
}),
)
})
.catch(err => this.emit('error', err)) .catch(err => this.emit('error', err))
} }
private setRemoteDescription(payload: SignalPayload) {
if (this.peerConnected) {
this.emit('warn', `attempting to set remote description while peer connected`, payload)
return
}
this.emit('debug', `remote description recieved: \n`, payload.sdp)
this._peer!.setRemoteDescription({ type: 'answer', sdp: payload.sdp })
}
private onMessage(e: MessageEvent) { private onMessage(e: MessageEvent) {
const { event, ...payload } = JSON.parse(e.data) as WebSocketMessages const { event, ...payload } = JSON.parse(e.data) as WebSocketMessages
this.emit('debug', `received websocket event ${event} ${payload ? `with payload: ` : ''}`, payload) this.emit('debug', `received websocket event ${event} ${payload ? `with payload: ` : ''}`, payload)
switch (event) { if (event === EVENT.SIGNAL.PROVIDE) {
case EVENT.IDENTITY.PROVIDE: const { sdp, id } = payload as SignalProvidePayload
this[EVENT.IDENTITY.PROVIDE](payload as IdentityPayload) this._id = id
this.createPeer() this.createPeer(sdp)
break return
case EVENT.SIGNAL.ANSWER: }
this.setRemoteDescription(payload as SignalPayload)
break // @ts-ignore
default: if (typeof this[event] === 'function') {
// @ts-ignore // @ts-ignore
if (typeof this[event] === 'function') { this[event](payload)
// @ts-ignore } else {
this[event](payload) this[EVENT.MESSAGE](event, payload)
} else {
this[EVENT.MESSAGE](event, payload)
}
} }
} }
@ -295,14 +279,6 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
return return
} }
this.emit('debug', `sending event '${EVENT.IDENTITY.DETAILS}' with payload`, { username: this._username })
this._ws!.send(
JSON.stringify({
event: EVENT.IDENTITY.DETAILS,
username: this._username,
}),
)
this.emit('debug', `connected`) this.emit('debug', `connected`)
this[EVENT.CONNECTED]() this[EVENT.CONNECTED]()
} }
@ -330,5 +306,4 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
protected abstract [EVENT.DISCONNECTED](reason?: Error): void protected abstract [EVENT.DISCONNECTED](reason?: Error): void
protected abstract [EVENT.TRACK](event: RTCTrackEvent): void protected abstract [EVENT.TRACK](event: RTCTrackEvent): void
protected abstract [EVENT.DATA](data: any): void protected abstract [EVENT.DATA](data: any): void
protected abstract [EVENT.IDENTITY.PROVIDE](payload: IdentityPayload): void
} }

View File

@ -15,10 +15,6 @@ export const EVENT = {
ANSWER: 'signal/answer', ANSWER: 'signal/answer',
PROVIDE: 'signal/provide', PROVIDE: 'signal/provide',
}, },
IDENTITY: {
PROVIDE: 'identity/provide',
DETAILS: 'identity/details',
},
MEMBER: { MEMBER: {
LIST: 'member/list', LIST: 'member/list',
CONNECTED: 'member/connected', CONNECTED: 'member/connected',
@ -59,7 +55,6 @@ export type Events = typeof EVENT
export type WebSocketEvents = export type WebSocketEvents =
| SystemEvents | SystemEvents
| ControlEvents | ControlEvents
| IdentityEvents
| MemberEvents | MemberEvents
| SignalEvents | SignalEvents
| ChatEvents | ChatEvents
@ -74,7 +69,6 @@ export type ControlEvents =
| typeof EVENT.CONTROL.CLIPBOARD | typeof EVENT.CONTROL.CLIPBOARD
export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT
export type IdentityEvents = typeof EVENT.IDENTITY.PROVIDE | typeof EVENT.IDENTITY.DETAILS
export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED
export type SignalEvents = typeof EVENT.SIGNAL.ANSWER | typeof EVENT.SIGNAL.PROVIDE export type SignalEvents = typeof EVENT.SIGNAL.ANSWER | typeof EVENT.SIGNAL.PROVIDE
export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE

View File

@ -7,7 +7,7 @@ import { accessor } from '~/store'
import { import {
DisconnectPayload, DisconnectPayload,
IdentityPayload, SignalProvidePayload,
MemberListPayload, MemberListPayload,
MemberDisconnectPayload, MemberDisconnectPayload,
MemberPayload, MemberPayload,
@ -28,10 +28,6 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
private $vue!: Vue private $vue!: Vue
private $accessor!: typeof accessor private $accessor!: typeof accessor
private get id() {
return this.$accessor.user.id
}
init(vue: Vue) { init(vue: Vue) {
this.$vue = vue this.$vue = vue
this.$accessor = vue.$accessor this.$accessor = vue.$accessor
@ -72,6 +68,7 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
} }
protected [EVENT.CONNECTED]() { protected [EVENT.CONNECTED]() {
this.$accessor.user.setMember(this.id)
this.$accessor.setConnected(true) this.$accessor.setConnected(true)
this.$accessor.setConnected(true) this.$accessor.setConnected(true)
@ -121,13 +118,6 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
}) })
} }
/////////////////////////////
// Identity Events
/////////////////////////////
protected [EVENT.IDENTITY.PROVIDE]({ id }: IdentityPayload) {
this.$accessor.user.setMember(id)
}
///////////////////////////// /////////////////////////////
// Member Events // Member Events
///////////////////////////// /////////////////////////////

View File

@ -3,7 +3,6 @@ import {
WebSocketEvents, WebSocketEvents,
SystemEvents, SystemEvents,
ControlEvents, ControlEvents,
IdentityEvents,
MemberEvents, MemberEvents,
SignalEvents, SignalEvents,
ChatEvents, ChatEvents,
@ -14,8 +13,8 @@ import { Member, ScreenConfigurations, ScreenResolution } from './types'
export type WebSocketMessages = export type WebSocketMessages =
| WebSocketMessage | WebSocketMessage
| IdentityMessage | SignalProvideMessage
| SignalMessage | SignalAnswerMessage
| MemberListMessage | MemberListMessage
| MembeConnectMessage | MembeConnectMessage
| MembeDisconnectMessage | MembeDisconnectMessage
@ -25,8 +24,8 @@ export type WebSocketMessages =
| ChatMessage | ChatMessage
export type WebSocketPayloads = export type WebSocketPayloads =
| IdentityPayload | SignalProvidePayload
| SignalPayload | SignalAnswerPayload
| MemberListPayload | MemberListPayload
| Member | Member
| ControlPayload | ControlPayload
@ -53,26 +52,25 @@ export interface DisconnectPayload {
message: string message: string
} }
/*
IDENTITY MESSAGES/PAYLOADS
*/
// identity/provide
export interface IdentityMessage extends WebSocketMessage, IdentityPayload {
event: typeof EVENT.IDENTITY.PROVIDE
}
export interface IdentityPayload {
id: string
}
/* /*
SIGNAL MESSAGES/PAYLOADS SIGNAL MESSAGES/PAYLOADS
*/ */
// signal/provide
export interface SignalProvideMessage extends WebSocketMessage, SignalProvidePayload {
event: typeof EVENT.SIGNAL.PROVIDE
}
export interface SignalProvidePayload {
id: string
sdp: string
}
// signal/answer // signal/answer
export interface SignalMessage extends WebSocketMessage, SignalPayload { export interface SignalAnswerMessage extends WebSocketMessage, SignalAnswerPayload {
event: typeof EVENT.SIGNAL.ANSWER event: typeof EVENT.SIGNAL.ANSWER
} }
export interface SignalPayload { export interface SignalAnswerPayload {
sdp: string sdp: string
username: string
} }
/* /*

View File

@ -131,32 +131,6 @@ func (manager *SessionManager) Brodcast(v interface{}, exclude interface{}) erro
return nil return nil
} }
func (manager *SessionManager) WriteVideoSample(sample types.Sample) error {
for _, session := range manager.members {
if !session.connected {
continue
}
if err := session.WriteVideoSample(sample); err != nil {
return err
}
}
return nil
}
func (manager *SessionManager) WriteAudioSample(sample types.Sample) error {
for _, session := range manager.members {
if !session.connected {
continue
}
if err := session.WriteAudioSample(sample); err != nil {
return err
}
}
return nil
}
func (manager *SessionManager) OnHost(listener func(id string)) { func (manager *SessionManager) OnHost(listener func(id string)) {
manager.emmiter.On("host", func(payload ...interface{}) { manager.emmiter.On("host", func(payload ...interface{}) {
listener(payload[0].(string)) listener(payload[0].(string))

View File

@ -64,8 +64,6 @@ func (session *Session) SetMuted(muted bool) {
func (session *Session) SetName(name string) error { func (session *Session) SetName(name string) error {
session.name = name session.name = name
session.connected = true
session.manager.emmiter.Emit("connected", session.id, session)
return nil return nil
} }
@ -79,6 +77,14 @@ func (session *Session) SetPeer(peer types.Peer) error {
return nil return nil
} }
func (session *Session) SetConnected(connected bool) error {
session.connected = connected
if connected {
session.manager.emmiter.Emit("connected", session.id, session)
}
return nil
}
func (session *Session) Kick(reason string) error { func (session *Session) Kick(reason string) error {
if session.socket == nil { if session.socket == nil {
return nil return nil
@ -107,18 +113,11 @@ func (session *Session) Write(v interface{}) error {
return session.socket.Send(v) return session.socket.Send(v)
} }
func (session *Session) WriteVideoSample(sample types.Sample) error { func (session *Session) SignalAnwser(sdp string) error {
if session.peer == nil || !session.connected { if session.peer == nil {
return nil return nil
} }
return session.peer.WriteVideoSample(sample) return session.peer.SignalAnwser(sdp)
}
func (session *Session) WriteAudioSample(sample types.Sample) error {
if session.peer == nil || !session.connected {
return nil
}
return session.peer.WriteAudioSample(sample)
} }
func (session *Session) destroy() error { func (session *Session) destroy() error {

View File

@ -9,11 +9,6 @@ const (
SIGNAL_PROVIDE = "signal/provide" SIGNAL_PROVIDE = "signal/provide"
) )
const (
IDENTITY_PROVIDE = "identity/provide"
IDENTITY_DETAILS = "identity/details"
)
const ( const (
MEMBER_LIST = "member/list" MEMBER_LIST = "member/list"
MEMBER_CONNECTED = "member/connected" MEMBER_CONNECTED = "member/connected"

View File

@ -13,19 +13,16 @@ type Disconnect struct {
Message string `json:"message"` Message string `json:"message"`
} }
type Identity struct { type SignalProvide struct {
Event string `json:"event"` Event string `json:"event"`
ID string `json:"id"` ID string `json:"id"`
SDP string `json:"sdp"`
} }
type IdentityDetails struct { type SignalAnswer struct {
Event string `json:"event"` Event string `json:"event"`
Username string `json:"username"` Username string `json:"username"`
} SDP string `json:"sdp"`
type Signal struct {
Event string `json:"event"`
SDP string `json:"sdp"`
} }
type MembersList struct { type MembersList struct {

View File

@ -16,14 +16,14 @@ type Session interface {
Member() *Member Member() *Member
SetMuted(muted bool) SetMuted(muted bool)
SetName(name string) error SetName(name string) error
SetConnected(connected bool) error
SetSocket(socket WebScoket) error SetSocket(socket WebScoket) error
SetPeer(peer Peer) error SetPeer(peer Peer) error
Address() *string Address() *string
Kick(message string) error Kick(message string) error
Write(v interface{}) error Write(v interface{}) error
Send(v interface{}) error Send(v interface{}) error
WriteAudioSample(sample Sample) error SignalAnwser(sdp string) error
WriteVideoSample(sample Sample) error
} }
type SessionManager interface { type SessionManager interface {
@ -39,8 +39,6 @@ type SessionManager interface {
Destroy(id string) error Destroy(id string) error
Clear() error Clear() error
Brodcast(v interface{}, exclude interface{}) error Brodcast(v interface{}, exclude interface{}) error
WriteAudioSample(sample Sample) error
WriteVideoSample(sample Sample) error
OnHost(listener func(id string)) OnHost(listener func(id string))
OnHostCleared(listener func(id string)) OnHostCleared(listener func(id string))
OnDestroy(listener func(id string)) OnDestroy(listener func(id string))

View File

@ -8,13 +8,12 @@ type Sample struct {
type WebRTCManager interface { type WebRTCManager interface {
Start() Start()
Shutdown() error Shutdown() error
CreatePeer(id string, sdp string) (string, Peer, error) CreatePeer(id string, session Session) (string, error)
ChangeScreenSize(width int, height int, rate int) error ChangeScreenSize(width int, height int, rate int) error
} }
type Peer interface { type Peer interface {
WriteVideoSample(sample Sample) error SignalAnwser(sdp string) error
WriteAudioSample(sample Sample) error
WriteData(v interface{}) error WriteData(v interface{}) error
Destroy() error Destroy() error
} }

View File

@ -4,32 +4,17 @@ import (
"sync" "sync"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v2/pkg/media"
"n.eko.moe/neko/internal/types"
) )
type Peer struct { type Peer struct {
id string id string
engine webrtc.MediaEngine manager *WebRTCManager
api *webrtc.API
video *webrtc.Track
audio *webrtc.Track
connection *webrtc.PeerConnection connection *webrtc.PeerConnection
mu sync.Mutex mu sync.Mutex
} }
func (peer *Peer) WriteAudioSample(sample types.Sample) error { func (peer *Peer) SignalAnwser(sdp string) error {
if err := peer.audio.WriteSample(media.Sample(sample)); err != nil { return peer.connection.SetRemoteDescription(webrtc.SessionDescription{SDP: sdp, Type: webrtc.SDPTypeAnswer})
return err
}
return nil
}
func (peer *Peer) WriteVideoSample(sample types.Sample) error {
if err := peer.video.WriteSample(media.Sample(sample)); err != nil {
return err
}
return nil
} }
func (peer *Peer) WriteData(v interface{}) error { func (peer *Peer) WriteData(v interface{}) error {

View File

@ -7,9 +7,9 @@ import (
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
func (m *WebRTCManager) createVideoTrack(engine webrtc.MediaEngine) (*webrtc.Track, error) { func (m *WebRTCManager) createVideoTrack() (*webrtc.Track, error) {
var codec *webrtc.RTPCodec var codec *webrtc.RTPCodec
for _, videoCodec := range engine.GetCodecsByKind(webrtc.RTPCodecTypeVideo) { for _, videoCodec := range m.engine.GetCodecsByKind(webrtc.RTPCodecTypeVideo) {
if videoCodec.Name == m.videoPipeline.CodecName { if videoCodec.Name == m.videoPipeline.CodecName {
codec = videoCodec codec = videoCodec
break break
@ -23,9 +23,9 @@ func (m *WebRTCManager) createVideoTrack(engine webrtc.MediaEngine) (*webrtc.Tra
return webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec) return webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec)
} }
func (m *WebRTCManager) createAudioTrack(engine webrtc.MediaEngine) (*webrtc.Track, error) { func (m *WebRTCManager) createAudioTrack() (*webrtc.Track, error) {
var codec *webrtc.RTPCodec var codec *webrtc.RTPCodec
for _, videoCodec := range engine.GetCodecsByKind(webrtc.RTPCodecTypeAudio) { for _, videoCodec := range m.engine.GetCodecsByKind(webrtc.RTPCodecTypeAudio) {
if videoCodec.Name == m.audioPipeline.CodecName { if videoCodec.Name == m.audioPipeline.CodecName {
codec = videoCodec codec = videoCodec
break break

View File

@ -2,10 +2,12 @@ package webrtc
import ( import (
"fmt" "fmt"
"io"
"strings" "strings"
"time" "time"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v2/pkg/media"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -28,13 +30,22 @@ func New(sessions types.SessionManager, config *config.WebRTC) *WebRTCManager {
settings.SetEphemeralUDPPortRange(config.EphemeralMin, config.EphemeralMax) settings.SetEphemeralUDPPortRange(config.EphemeralMin, config.EphemeralMax)
settings.SetNAT1To1IPs(config.NAT1To1IPs, webrtc.ICECandidateTypeHost) settings.SetNAT1To1IPs(config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
// 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))
return &WebRTCManager{ return &WebRTCManager{
logger: logger, logger: logger,
settings: settings, settings: settings,
cleanup: time.NewTicker(1 * time.Second), cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool), shutdown: make(chan bool),
sessions: sessions, sessions: sessions,
engine: engine,
config: config, config: config,
api: api,
configuration: &webrtc.Configuration{ configuration: &webrtc.Configuration{
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback, SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}, },
@ -44,18 +55,23 @@ func New(sessions types.SessionManager, config *config.WebRTC) *WebRTCManager {
type WebRTCManager struct { type WebRTCManager struct {
logger zerolog.Logger logger zerolog.Logger
settings webrtc.SettingEngine settings webrtc.SettingEngine
sessions types.SessionManager engine webrtc.MediaEngine
api *webrtc.API
videoTrack *webrtc.Track
audioTrack *webrtc.Track
videoPipeline *gst.Pipeline videoPipeline *gst.Pipeline
audioPipeline *gst.Pipeline audioPipeline *gst.Pipeline
sessions types.SessionManager
cleanup *time.Ticker cleanup *time.Ticker
config *config.WebRTC config *config.WebRTC
shutdown chan bool shutdown chan bool
configuration *webrtc.Configuration configuration *webrtc.Configuration
} }
func (m *WebRTCManager) Start() { func (m *WebRTCManager) Start() {
// Set display and change to default resolution
xorg.Display(m.config.Display) xorg.Display(m.config.Display)
if !xorg.ValidScreenSize(m.config.ScreenWidth, m.config.ScreenHeight, m.config.ScreenRate) { 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) m.logger.Warn().Msgf("invalid screen option %dx%d@%d", m.config.ScreenWidth, m.config.ScreenHeight, m.config.ScreenRate)
} else { } else {
@ -64,31 +80,39 @@ func (m *WebRTCManager) Start() {
} }
} }
// Create video track/pipeline
videoPipeline, err := gst.CreatePipeline( videoPipeline, err := gst.CreatePipeline(
m.config.VideoCodec, m.config.VideoCodec,
m.config.Display, m.config.Display,
m.config.VideoParams, m.config.VideoParams,
) )
if err != nil { if err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager") m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
} }
m.videoPipeline = videoPipeline
video, err := m.createVideoTrack()
if err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
m.videoTrack = video
// Create audio track/pipeline
audioPipeline, err := gst.CreatePipeline( audioPipeline, err := gst.CreatePipeline(
m.config.AudioCodec, m.config.AudioCodec,
m.config.Device, m.config.Device,
m.config.AudioParams, m.config.AudioParams,
) )
if err != nil { if err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager") m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
} }
m.videoPipeline = videoPipeline
m.audioPipeline = audioPipeline m.audioPipeline = audioPipeline
videoPipeline.Start() audio, err := m.createAudioTrack()
audioPipeline.Start() if err != nil {
m.logger.Panic().Err(err).Msg("unable to start webrtc manager")
}
m.audioTrack = audio
go func() { go func() {
defer func() { defer func() {
@ -100,11 +124,11 @@ func (m *WebRTCManager) Start() {
case <-m.shutdown: case <-m.shutdown:
return return
case sample := <-m.videoPipeline.Sample: case sample := <-m.videoPipeline.Sample:
if err := m.sessions.WriteVideoSample(sample); err != nil { if err := m.videoTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
m.logger.Warn().Err(err).Msg("video pipeline failed to write") m.logger.Warn().Err(err).Msg("video pipeline failed to write")
} }
case sample := <-m.audioPipeline.Sample: case sample := <-m.audioPipeline.Sample:
if err := m.sessions.WriteAudioSample(sample); err != nil { if err := m.audioTrack.WriteSample(media.Sample(sample)); err != nil && err != io.ErrClosedPipe {
m.logger.Warn().Err(err).Msg("audio pipeline failed to write") m.logger.Warn().Err(err).Msg("audio pipeline failed to write")
} }
case <-m.cleanup.C: case <-m.cleanup.C:
@ -125,6 +149,10 @@ func (m *WebRTCManager) Start() {
m.logger.Debug().Str("id", id).Msg("session destroyed") m.logger.Debug().Str("id", id).Msg("session destroyed")
}) })
// start pipelines
videoPipeline.Start()
audioPipeline.Start()
// TODO: log resolution, bit rate and codec parameters // TODO: log resolution, bit rate and codec parameters
m.logger.Info(). m.logger.Info().
Str("video_display", m.config.Display). Str("video_display", m.config.Display).
@ -147,64 +175,28 @@ func (m *WebRTCManager) Shutdown() error {
return nil return nil
} }
func (m *WebRTCManager) CreatePeer(id string, sdp string) (string, types.Peer, error) { func (m *WebRTCManager) CreatePeer(id string, session types.Session) (string, error) {
// Create SessionDescription
description := webrtc.SessionDescription{
SDP: sdp,
Type: webrtc.SDPTypeOffer,
}
// Create MediaEngine based off sdp
engine := webrtc.MediaEngine{}
engine.PopulateFromSDP(description)
// Create API with MediaEngine and SettingEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(m.settings))
// Create new peer connection // Create new peer connection
connection, err := api.NewPeerConnection(*m.configuration) connection, err := m.api.NewPeerConnection(*m.configuration)
if err != nil { if err != nil {
return "", nil, err return "", err
} }
// Create video track if _, err = connection.AddTransceiverFromTrack(m.videoTrack, webrtc.RtpTransceiverInit{
video, err := m.createVideoTrack(engine)
if err != nil {
return "", nil, err
}
_, err = connection.AddTransceiverFromTrack(video, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly, Direction: webrtc.RTPTransceiverDirectionSendonly,
}) }); err != nil {
return "", err
if err != nil {
return "", nil, err
} }
// Create audio track if _, err = connection.AddTransceiverFromTrack(m.audioTrack, webrtc.RtpTransceiverInit{
audio, err := m.createAudioTrack(engine)
if err != nil {
return "", nil, err
}
_, err = connection.AddTransceiverFromTrack(audio, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly, Direction: webrtc.RTPTransceiverDirectionSendonly,
}) }); err != nil {
return "", err
if err != nil {
return "", nil, err
} }
// Set remote description description, err := connection.CreateOffer(nil)
connection.SetRemoteDescription(description)
answer, err := connection.CreateAnswer(nil)
if err != nil { if err != nil {
return "", nil, err return "", err
}
if err = connection.SetLocalDescription(answer); err != nil {
return "", nil, err
} }
connection.OnDataChannel(func(d *webrtc.DataChannel) { connection.OnDataChannel(func(d *webrtc.DataChannel) {
@ -215,6 +207,7 @@ func (m *WebRTCManager) CreatePeer(id string, sdp string) (string, types.Peer, e
}) })
}) })
connection.SetLocalDescription(description)
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateDisconnected:
@ -224,18 +217,23 @@ func (m *WebRTCManager) CreatePeer(id string, sdp string) (string, types.Peer, e
break break
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
m.logger.Info().Str("id", id).Msg("peer connected") m.logger.Info().Str("id", id).Msg("peer connected")
if err = session.SetConnected(true); err != nil {
m.logger.Warn().Err(err).Msg("unable to set connected on peer")
m.sessions.Destroy(id)
}
break break
} }
}) })
return answer.SDP, &Peer{ if err := session.SetPeer(&Peer{
id: id, id: id,
api: api, manager: m,
engine: engine,
video: video,
audio: audio,
connection: connection, connection: connection,
}, nil }); err != nil {
return "", err
}
return description.SDP, nil
} }
func (m *WebRTCManager) ChangeScreenSize(width int, height int, rate int) error { func (m *WebRTCManager) ChangeScreenSize(width int, height int, rate int) error {

View File

@ -57,18 +57,11 @@ func (h *MessageHandler) Message(id string, raw []byte) error {
switch header.Event { switch header.Event {
// Signal Events // Signal Events
case event.SIGNAL_PROVIDE: case event.SIGNAL_ANSWER:
payload := &message.Signal{} payload := &message.SignalAnswer{}
return errors.Wrapf( return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error { utils.Unmarshal(payload, raw, func() error {
return h.createPeer(id, session, payload) return h.signalAnswer(id, session, payload)
}), "%s failed", header.Event)
// Identity Events
case event.IDENTITY_DETAILS:
payload := &message.IdentityDetails{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.identityDetails(id, session, payload)
}), "%s failed", header.Event) }), "%s failed", header.Event)
// Control Events // Control Events

View File

@ -1,34 +0,0 @@
package websocket
import (
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
)
func (h *MessageHandler) identityDetails(id string, session types.Session, payload *message.IdentityDetails) error {
if err := session.SetName(payload.Username); err != nil {
return err
}
return nil
}
func (h *MessageHandler) createPeer(id string, session types.Session, payload *message.Signal) error {
sdp, peer, err := h.webrtc.CreatePeer(id, payload.SDP)
if err != nil {
return err
}
if err := session.SetPeer(peer); err != nil {
return err
}
if err := session.Send(message.Signal{
Event: event.SIGNAL_ANSWER,
SDP: sdp,
}); err != nil {
return err
}
return nil
}

View File

@ -7,10 +7,8 @@ import (
) )
func (h *MessageHandler) SessionCreated(id string, session types.Session) error { func (h *MessageHandler) SessionCreated(id string, session types.Session) error {
if err := session.Send(message.Identity{ // send sdp and id over to client
Event: event.IDENTITY_PROVIDE, if err := h.signalProvide(id, session); err != nil {
ID: id,
}); err != nil {
return err return err
} }
@ -20,7 +18,7 @@ func (h *MessageHandler) SessionCreated(id string, session types.Session) error
} }
if session.Admin() { if session.Admin() {
// send screen configurations // send screen configurations if admin
if err := h.screenConfigurations(id, session); err != nil { if err := h.screenConfigurations(id, session); err != nil {
return err return err
} }

View File

@ -0,0 +1,36 @@
package websocket
import (
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
)
func (h *MessageHandler) signalProvide(id string, session types.Session) error {
sdp, err := h.webrtc.CreatePeer(id, session)
if err != nil {
return err
}
if err := session.Send(message.SignalProvide{
Event: event.SIGNAL_PROVIDE,
ID: id,
SDP: sdp,
}); err != nil {
return err
}
return nil
}
func (h *MessageHandler) signalAnswer(id string, session types.Session, payload *message.SignalAnswer) error {
if err := session.SetName(payload.Username); err != nil {
return err
}
if err := session.SignalAnwser(payload.SDP); err != nil {
return err
}
return nil
}