From f62d36ac4feddacfeb3c84655af64726cafa0403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 5 Feb 2021 17:57:33 +0100 Subject: [PATCH] WebRTC SetVideoID. --- internal/types/webrtc.go | 1 + internal/webrtc/manager.go | 7 ++++--- internal/webrtc/peer.go | 17 +++++++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/types/webrtc.go b/internal/types/webrtc.go index 9c0bca3f..8564edcf 100644 --- a/internal/types/webrtc.go +++ b/internal/types/webrtc.go @@ -5,6 +5,7 @@ import "github.com/pion/webrtc/v3" type WebRTCPeer interface { SignalAnswer(sdp string) error SignalCandidate(candidate webrtc.ICECandidateInit) error + SetVideoID(videoID string) error Destroy() error } diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index 912b8a67..c854048c 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -137,7 +137,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess }) } - audioTransceiver, err := connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{ + _, err = connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{ Direction: webrtc.RTPTransceiverDirectionSendonly, }) if err != nil { @@ -151,7 +151,8 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess return nil, err } - if _, err := connection.CreateDataChannel("data", nil); err != nil { + _, err = connection.CreateDataChannel("data", nil) + if err != nil { return nil, err } @@ -206,7 +207,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.Sess settings: settings, connection: connection, configuration: configuration, - audioTransceiver: audioTransceiver, + videoTracks: manager.videoTracks, videoTransceiver: videoTransceiver, }) diff --git a/internal/webrtc/peer.go b/internal/webrtc/peer.go index f4240cac..2c72e048 100644 --- a/internal/webrtc/peer.go +++ b/internal/webrtc/peer.go @@ -1,6 +1,10 @@ package webrtc -import "github.com/pion/webrtc/v3" +import ( + "fmt" + + "github.com/pion/webrtc/v3" +) type WebRTCPeerCtx struct { api *webrtc.API @@ -8,7 +12,7 @@ type WebRTCPeerCtx struct { settings *webrtc.SettingEngine connection *webrtc.PeerConnection configuration *webrtc.Configuration - audioTransceiver *webrtc.RTPTransceiver + videoTracks map[string]*webrtc.TrackLocalStaticSample videoTransceiver *webrtc.RTPTransceiver } @@ -23,11 +27,12 @@ func (webrtc_peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateI return webrtc_peer.connection.AddICECandidate(candidate) } -func (webrtc_peer *WebRTCPeerCtx) ReplaceAudioTrack(track webrtc.TrackLocal) error { - return webrtc_peer.audioTransceiver.Sender().ReplaceTrack(track) -} +func (webrtc_peer *WebRTCPeerCtx) SetVideoID(videoID string) error { + track, ok := webrtc_peer.videoTracks[videoID] + if !ok { + return fmt.Errorf("videoID not found in available tracks") + } -func (webrtc_peer *WebRTCPeerCtx) ReplaceVideoTrack(track webrtc.TrackLocal) error { return webrtc_peer.videoTransceiver.Sender().ReplaceTrack(track) }