cleanup peer.

This commit is contained in:
Miroslav Šedivý 2023-04-10 21:21:11 +02:00
parent e66cd3978d
commit 7cd469f7bd
2 changed files with 23 additions and 51 deletions

View File

@ -450,16 +450,21 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
peer := &WebRTCPeerCtx{ peer := &WebRTCPeerCtx{
logger: logger, logger: logger,
connection: connection, connection: connection,
// tracks & channels
audioTrack: audioTrack,
videoTrack: videoTrack,
dataChannel: dataChannel, dataChannel: dataChannel,
rtcpChannel: videoRtcp,
// config
iceTrickle: manager.config.ICETrickle,
// deprecated functions
changeVideoFromBitrate: changeVideoFromBitrate, changeVideoFromBitrate: changeVideoFromBitrate,
changeVideoFromID: changeVideoFromID, changeVideoFromID: changeVideoFromID,
// TODO: Refactor.
videoId: videoTrack.stream.ID, videoId: videoTrack.stream.ID,
setPaused: func(isPaused bool) { setPaused: func(isPaused bool) {
videoTrack.SetPaused(isPaused) videoTrack.SetPaused(isPaused)
audioTrack.SetPaused(isPaused) audioTrack.SetPaused(isPaused)
}, },
iceTrickle: manager.config.ICETrickle,
setVideoAuto: func(videoAuto bool) { setVideoAuto: func(videoAuto bool) {
// if estimator is enabled and not in passive mode, enable video auto bitrate // if estimator is enabled and not in passive mode, enable video auto bitrate
if manager.config.EstimatorEnabled && !manager.config.EstimatorPassive { if manager.config.EstimatorEnabled && !manager.config.EstimatorPassive {

View File

@ -5,6 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"sync" "sync"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -16,24 +17,26 @@ type WebRTCPeerCtx struct {
mu sync.Mutex mu sync.Mutex
logger zerolog.Logger logger zerolog.Logger
connection *webrtc.PeerConnection connection *webrtc.PeerConnection
// tracks & channels
audioTrack *Track
videoTrack *Track
dataChannel *webrtc.DataChannel dataChannel *webrtc.DataChannel
rtcpChannel chan []rtcp.Packet
// config
iceTrickle bool
// deprecated functions
changeVideoFromBitrate func(bitrate int) changeVideoFromBitrate func(bitrate int)
changeVideoFromID func(id string) int changeVideoFromID func(id string) int
videoId func() string videoId func() string
setPaused func(isPaused bool) setPaused func(isPaused bool)
setVideoAuto func(auto bool) setVideoAuto func(auto bool)
getVideoAuto func() bool getVideoAuto func() bool
iceTrickle bool
} }
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) { func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return nil, types.ErrWebRTCConnectionNotFound
}
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{ offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
ICERestart: ICERestart, ICERestart: ICERestart,
}) })
@ -48,10 +51,6 @@ func (peer *WebRTCPeerCtx) CreateAnswer() (*webrtc.SessionDescription, error) {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return nil, types.ErrWebRTCConnectionNotFound
}
answer, err := peer.connection.CreateAnswer(nil) answer, err := peer.connection.CreateAnswer(nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -83,10 +82,6 @@ func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{ return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp, SDP: sdp,
Type: webrtc.SDPTypeOffer, Type: webrtc.SDPTypeOffer,
@ -97,10 +92,6 @@ func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{ return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp, SDP: sdp,
Type: webrtc.SDPTypeAnswer, Type: webrtc.SDPTypeAnswer,
@ -111,10 +102,6 @@ func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.AddICECandidate(candidate) return peer.connection.AddICECandidate(candidate)
} }
@ -122,10 +109,6 @@ func (peer *WebRTCPeerCtx) SetVideoBitrate(peerBitrate int) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
peer.changeVideoFromBitrate(peerBitrate) peer.changeVideoFromBitrate(peerBitrate)
return nil return nil
} }
@ -134,10 +117,6 @@ func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
peer.changeVideoFromID(videoID) peer.changeVideoFromID(videoID)
return nil return nil
} }
@ -154,10 +133,6 @@ func (peer *WebRTCPeerCtx) SetPaused(isPaused bool) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
peer.logger.Info().Bool("is_paused", isPaused).Msg("set paused") peer.logger.Info().Bool("is_paused", isPaused).Msg("set paused")
peer.setPaused(isPaused) peer.setPaused(isPaused)
return nil return nil
@ -167,10 +142,6 @@ func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.dataChannel == nil {
return types.ErrWebRTCDataChannelNotFound
}
data := payload.CursorPosition{ data := payload.CursorPosition{
Header: payload.Header{ Header: payload.Header{
Event: payload.OP_CURSOR_POSITION, Event: payload.OP_CURSOR_POSITION,
@ -192,10 +163,6 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) e
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.dataChannel == nil {
return types.ErrWebRTCDataChannelNotFound
}
data := payload.CursorImage{ data := payload.CursorImage{
Header: payload.Header{ Header: payload.Header{
Event: payload.OP_CURSOR_IMAGE, Event: payload.OP_CURSOR_IMAGE,