choose video with CreatePeer.

This commit is contained in:
Miroslav Šedivý 2021-02-07 17:07:55 +01:00
parent 5cdb6e074e
commit dd2ca03955
4 changed files with 28 additions and 28 deletions

View File

@ -46,6 +46,7 @@ type SignalProvide struct {
Lite bool `json:"lite"` Lite bool `json:"lite"`
ICE []string `json:"ice"` ICE []string `json:"ice"`
Videos []string `json:"videos"` Videos []string `json:"videos"`
Video string `json:"video"`
} }
type SignalCandidate struct { type SignalCandidate struct {

View File

@ -17,5 +17,5 @@ type WebRTCManager interface {
ICELite() bool ICELite() bool
ICEServers() []string ICEServers() []string
CreatePeer(session Session) (*webrtc.SessionDescription, error) CreatePeer(session Session, videoID string) (*webrtc.SessionDescription, error)
} }

View File

@ -19,23 +19,21 @@ import (
func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx { func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx {
return &WebRTCManagerCtx{ return &WebRTCManagerCtx{
logger: log.With().Str("module", "webrtc").Logger(), logger: log.With().Str("module", "webrtc").Logger(),
defaultVideoID: capture.VideoIDs()[0], desktop: desktop,
desktop: desktop, capture: capture,
capture: capture, config: config,
config: config,
} }
} }
type WebRTCManagerCtx struct { type WebRTCManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
audioTrack *webrtc.TrackLocalStaticSample audioTrack *webrtc.TrackLocalStaticSample
unsubscribe []func() audioCodec codec.RTPCodec
defaultVideoID string audioStop func()
audioCodec codec.RTPCodec desktop types.DesktopManager
desktop types.DesktopManager capture types.CaptureManager
capture types.CaptureManager config *config.WebRTC
config *config.WebRTC
} }
func (manager *WebRTCManagerCtx) Start() { func (manager *WebRTCManagerCtx) Start() {
@ -55,9 +53,9 @@ func (manager *WebRTCManagerCtx) Start() {
} }
audio.AddListener(&listener) audio.AddListener(&listener)
manager.unsubscribe = append(manager.unsubscribe, func(){ manager.audioStop = func(){
audio.RemoveListener(&listener) audio.RemoveListener(&listener)
}) }
manager.logger.Info(). manager.logger.Info().
Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)). Str("ice_lite", fmt.Sprintf("%t", manager.config.ICELite)).
@ -71,10 +69,7 @@ func (manager *WebRTCManagerCtx) Start() {
func (manager *WebRTCManagerCtx) Shutdown() error { func (manager *WebRTCManagerCtx) Shutdown() error {
manager.logger.Info().Msgf("webrtc shutting down") manager.logger.Info().Msgf("webrtc shutting down")
for _, unsubscribe := range manager.unsubscribe { manager.audioStop()
unsubscribe()
}
return nil return nil
} }
@ -86,11 +81,11 @@ func (manager *WebRTCManagerCtx) ICEServers() []string {
return manager.config.ICEServers 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() logger := manager.logger.With().Str("id", session.ID()).Logger()
// Create MediaEngine // Create MediaEngine
engine, err := manager.mediaEngine() engine, err := manager.mediaEngine(videoID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -129,9 +124,9 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess
} }
// create video track // create video track
videoStream, ok := manager.capture.Video(manager.defaultVideoID) videoStream, ok := manager.capture.Video(videoID)
if !ok { 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 return nil, err
} }
@ -265,11 +260,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess
return connection.LocalDescription(), nil return connection.LocalDescription(), nil
} }
func (manager *WebRTCManagerCtx) mediaEngine() (*webrtc.MediaEngine, error) { func (manager *WebRTCManagerCtx) mediaEngine(videoID string) (*webrtc.MediaEngine, error) {
engine := &webrtc.MediaEngine{} engine := &webrtc.MediaEngine{}
// all videos must have the same codec // all videos must have the same codec
video, ok := manager.capture.Video(manager.defaultVideoID) video, ok := manager.capture.Video(videoID)
if !ok { if !ok {
return nil, fmt.Errorf("default video track not found") return nil, fmt.Errorf("default video track not found")
} }

View File

@ -11,7 +11,10 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error {
return nil 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 { if err != nil {
return err return err
} }
@ -22,7 +25,8 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error {
SDP: offer.SDP, SDP: offer.SDP,
Lite: h.webrtc.ICELite(), Lite: h.webrtc.ICELite(),
ICE: h.webrtc.ICEServers(), ICE: h.webrtc.ICEServers(),
Videos: h.capture.VideoIDs(), Videos: videos,
Video: defaultVideo,
}) })
} }