diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 110ce4a1..51a4130b 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -46,6 +46,7 @@ type SignalProvide struct { Lite bool `json:"lite"` ICE []string `json:"ice"` Videos []string `json:"videos"` + Video string `json:"video"` } type SignalCandidate struct { diff --git a/internal/types/webrtc.go b/internal/types/webrtc.go index 8564edcf..637d15c3 100644 --- a/internal/types/webrtc.go +++ b/internal/types/webrtc.go @@ -17,5 +17,5 @@ type WebRTCManager interface { ICELite() bool ICEServers() []string - CreatePeer(session Session) (*webrtc.SessionDescription, error) + CreatePeer(session Session, videoID string) (*webrtc.SessionDescription, error) } diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index 7c401daa..db1e0c32 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -19,23 +19,21 @@ import ( func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx { return &WebRTCManagerCtx{ - logger: log.With().Str("module", "webrtc").Logger(), - defaultVideoID: capture.VideoIDs()[0], - desktop: desktop, - capture: capture, - config: config, + logger: log.With().Str("module", "webrtc").Logger(), + desktop: desktop, + capture: capture, + config: config, } } type WebRTCManagerCtx struct { - logger zerolog.Logger - audioTrack *webrtc.TrackLocalStaticSample - unsubscribe []func() - defaultVideoID string - audioCodec codec.RTPCodec - desktop types.DesktopManager - capture types.CaptureManager - config *config.WebRTC + logger zerolog.Logger + audioTrack *webrtc.TrackLocalStaticSample + audioCodec codec.RTPCodec + audioStop func() + desktop types.DesktopManager + capture types.CaptureManager + config *config.WebRTC } func (manager *WebRTCManagerCtx) Start() { @@ -55,9 +53,9 @@ func (manager *WebRTCManagerCtx) Start() { } audio.AddListener(&listener) - manager.unsubscribe = append(manager.unsubscribe, func(){ + manager.audioStop = func(){ audio.RemoveListener(&listener) - }) + } manager.logger.Info(). Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)). @@ -71,10 +69,7 @@ func (manager *WebRTCManagerCtx) Start() { func (manager *WebRTCManagerCtx) Shutdown() error { manager.logger.Info().Msgf("webrtc shutting down") - for _, unsubscribe := range manager.unsubscribe { - unsubscribe() - } - + manager.audioStop() return nil } @@ -86,11 +81,11 @@ func (manager *WebRTCManagerCtx) ICEServers() []string { return manager.config.ICEServers } -func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.SessionDescription, error) { +func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (*webrtc.SessionDescription, error) { logger := manager.logger.With().Str("id", session.ID()).Logger() // Create MediaEngine - engine, err := manager.mediaEngine() + engine, err := manager.mediaEngine(videoID) if err != nil { return nil, err } @@ -129,9 +124,9 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess } // create video track - videoStream, ok := manager.capture.Video(manager.defaultVideoID) + videoStream, ok := manager.capture.Video(videoID) if !ok { - manager.logger.Warn().Str("videoID", manager.defaultVideoID).Msg("default video stream not found") + manager.logger.Warn().Str("videoID", videoID).Msg("video stream not found") return nil, err } @@ -265,11 +260,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess return connection.LocalDescription(), nil } -func (manager *WebRTCManagerCtx) mediaEngine() (*webrtc.MediaEngine, error) { +func (manager *WebRTCManagerCtx) mediaEngine(videoID string) (*webrtc.MediaEngine, error) { engine := &webrtc.MediaEngine{} // all videos must have the same codec - video, ok := manager.capture.Video(manager.defaultVideoID) + video, ok := manager.capture.Video(videoID) if !ok { return nil, fmt.Errorf("default video track not found") } diff --git a/internal/websocket/handler/signal.go b/internal/websocket/handler/signal.go index 8e036d22..6d200c09 100644 --- a/internal/websocket/handler/signal.go +++ b/internal/websocket/handler/signal.go @@ -11,7 +11,10 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error { return nil } - offer, err := h.webrtc.CreatePeer(session) + videos := h.capture.VideoIDs() + defaultVideo := videos[0] + + offer, err := h.webrtc.CreatePeer(session, defaultVideo) if err != nil { return err } @@ -22,7 +25,8 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error { SDP: offer.SDP, Lite: h.webrtc.ICELite(), ICE: h.webrtc.ICEServers(), - Videos: h.capture.VideoIDs(), + Videos: videos, + Video: defaultVideo, }) }