move change funcs to peer.

This commit is contained in:
Miroslav Šedivý 2023-04-10 21:44:17 +02:00
parent ec8560b290
commit 9d362ed036
2 changed files with 110 additions and 116 deletions

View File

@ -340,76 +340,42 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
// let video stream bucket manager handle stream subscriptions // let video stream bucket manager handle stream subscriptions
video.SetReceiver(videoTrack) video.SetReceiver(videoTrack)
changeVideoFromBitrate := func(peerBitrate int) { // data channel
// when switching from manual to auto bitrate estimation, in case the estimator is
// idle (lastBitrate > maxBitrate), we want to go back to the previous estimated bitrate
if peerBitrate == 0 && estimator != nil && !manager.config.EstimatorPassive {
peerBitrate = estimator.GetTargetBitrate()
manager.logger.Debug().
Int("peer_bitrate", peerBitrate).
Msg("evaluated bitrate")
}
ok, err := videoTrack.SetBitrate(peerBitrate) dataChannel, err := connection.CreateDataChannel("data", nil)
if err != nil { if err != nil {
logger.Error().Err(err). return nil, err
Int("peer_bitrate", peerBitrate).
Msg("unable to set video bitrate")
return
}
if !ok {
return
}
videoID := videoTrack.stream.ID()
bitrate := videoTrack.stream.Bitrate()
metrics.SetVideoID(videoID)
manager.logger.Debug().
Int("peer_bitrate", peerBitrate).
Int("video_bitrate", bitrate).
Str("video_id", videoID).
Msg("peer bitrate triggered video stream change")
go session.Send(
event.SIGNAL_VIDEO,
message.SignalVideo{
Video: videoID,
Bitrate: bitrate,
VideoAuto: videoTrack.VideoAuto(),
})
} }
changeVideoFromID := func(videoID string) (bitrate int) { peer := &WebRTCPeerCtx{
changed, err := videoTrack.SetVideoID(videoID) logger: logger,
if err != nil { session: session,
logger.Error().Err(err). metrics: metrics,
Str("video_id", videoID). connection: connection,
Msg("unable to set video stream") estimator: estimator,
return // tracks & channels
} audioTrack: audioTrack,
videoTrack: videoTrack,
if !changed { dataChannel: dataChannel,
return rtcpChannel: videoRtcp,
} // config
iceTrickle: manager.config.ICETrickle,
bitrate = videoTrack.stream.Bitrate() // deprecated functions
videoId: videoTrack.stream.ID,
manager.logger.Debug(). setPaused: func(isPaused bool) {
Str("video_id", videoID). videoTrack.SetPaused(isPaused)
Int("video_bitrate", bitrate). audioTrack.SetPaused(isPaused)
Msg("peer video id triggered video stream change") },
setVideoAuto: func(videoAuto bool) {
go session.Send( // if estimator is enabled and not in passive mode, enable video auto bitrate
event.SIGNAL_VIDEO, if manager.config.EstimatorEnabled && !manager.config.EstimatorPassive {
message.SignalVideo{ videoTrack.SetVideoAuto(videoAuto)
Video: videoID, } else {
Bitrate: bitrate, logger.Warn().Msg("estimator is disabled or in passive mode, cannot change video auto")
VideoAuto: videoTrack.VideoAuto(), videoTrack.SetVideoAuto(false) // ensure video auto is disabled
}) }
},
return getVideoAuto: videoTrack.VideoAuto,
} }
manager.logger.Info(). manager.logger.Info().
@ -417,7 +383,9 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
Msg("estimated initial peer bitrate") Msg("estimated initial peer bitrate")
// set initial video bitrate // set initial video bitrate
changeVideoFromBitrate(bitrate) if err := peer.SetVideoBitrate(bitrate); err != nil {
return nil, err
}
// if estimator is enabled, use it to change video stream // if estimator is enabled, use it to change video stream
if estimator != nil { if estimator != nil {
@ -437,51 +405,15 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
continue continue
} }
if !manager.config.EstimatorPassive { if !manager.config.EstimatorPassive {
changeVideoFromBitrate(targetBitrate) err := peer.SetVideoBitrate(targetBitrate)
if err != nil {
logger.Warn().Err(err).Msg("failed to set video bitrate")
}
} }
} }
}() }()
} }
// data channel
dataChannel, err := connection.CreateDataChannel("data", nil)
if err != nil {
return nil, err
}
peer := &WebRTCPeerCtx{
logger: logger,
session: session,
metrics: metrics,
connection: connection,
// tracks & channels
audioTrack: audioTrack,
videoTrack: videoTrack,
dataChannel: dataChannel,
rtcpChannel: videoRtcp,
// config
iceTrickle: manager.config.ICETrickle,
// deprecated functions
changeVideoFromBitrate: changeVideoFromBitrate,
changeVideoFromID: changeVideoFromID,
videoId: videoTrack.stream.ID,
setPaused: func(isPaused bool) {
videoTrack.SetPaused(isPaused)
audioTrack.SetPaused(isPaused)
},
setVideoAuto: func(videoAuto bool) {
// if estimator is enabled and not in passive mode, enable video auto bitrate
if manager.config.EstimatorEnabled && !manager.config.EstimatorPassive {
videoTrack.SetVideoAuto(videoAuto)
} else {
logger.Warn().Msg("estimator is disabled or in passive mode, cannot change video auto")
videoTrack.SetVideoAuto(false) // ensure video auto is disabled
}
},
getVideoAuto: videoTrack.VideoAuto,
}
connection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { connection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
logger := logger.With(). logger := logger.With().
Str("kind", track.Kind().String()). Str("kind", track.Kind().String()).

View File

@ -5,12 +5,15 @@ import (
"encoding/binary" "encoding/binary"
"sync" "sync"
"github.com/pion/interceptor/pkg/cc"
"github.com/pion/rtcp" "github.com/pion/rtcp"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/demodesk/neko/internal/webrtc/payload" "github.com/demodesk/neko/internal/webrtc/payload"
"github.com/demodesk/neko/pkg/types" "github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
) )
type WebRTCPeerCtx struct { type WebRTCPeerCtx struct {
@ -19,6 +22,7 @@ type WebRTCPeerCtx struct {
session types.Session session types.Session
metrics *metrics metrics *metrics
connection *webrtc.PeerConnection connection *webrtc.PeerConnection
estimator cc.BandwidthEstimator
// tracks & channels // tracks & channels
audioTrack *Track audioTrack *Track
videoTrack *Track videoTrack *Track
@ -27,12 +31,10 @@ type WebRTCPeerCtx struct {
// config // config
iceTrickle bool iceTrickle bool
// deprecated functions // deprecated functions
changeVideoFromBitrate func(bitrate int) videoId func() string
changeVideoFromID func(id string) int setPaused func(isPaused bool)
videoId func() string setVideoAuto func(auto bool)
setPaused func(isPaused bool) getVideoAuto func() bool
setVideoAuto func(auto bool)
getVideoAuto func() bool
} }
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) { func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
@ -111,7 +113,43 @@ func (peer *WebRTCPeerCtx) SetVideoBitrate(peerBitrate int) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
peer.changeVideoFromBitrate(peerBitrate) // when switching from manual to auto bitrate estimation, in case the estimator is
// idle (lastBitrate > maxBitrate), we want to go back to the previous estimated bitrate
if peerBitrate == 0 && peer.estimator != nil {
peerBitrate = peer.estimator.GetTargetBitrate()
peer.logger.Debug().
Int("peer_bitrate", peerBitrate).
Msg("evaluated bitrate")
}
changed, err := peer.videoTrack.SetBitrate(peerBitrate)
if err != nil {
return err
}
if !changed {
// TODO: return error?
return nil
}
videoID := peer.videoTrack.stream.ID()
bitrate := peer.videoTrack.stream.Bitrate()
peer.metrics.SetVideoID(videoID)
peer.logger.Debug().
Int("peer_bitrate", peerBitrate).
Int("video_bitrate", bitrate).
Str("video_id", videoID).
Msg("peer bitrate triggered video stream change")
go peer.session.Send(
event.SIGNAL_VIDEO,
message.SignalVideo{
Video: videoID,
Bitrate: bitrate,
VideoAuto: peer.videoTrack.VideoAuto(),
})
return nil return nil
} }
@ -119,7 +157,31 @@ func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
peer.changeVideoFromID(videoID) changed, err := peer.videoTrack.SetVideoID(videoID)
if err != nil {
return err
}
if !changed {
// TODO: return error?
return nil
}
bitrate := peer.videoTrack.stream.Bitrate()
peer.logger.Debug().
Str("video_id", videoID).
Int("video_bitrate", bitrate).
Msg("peer video id triggered video stream change")
go peer.session.Send(
event.SIGNAL_VIDEO,
message.SignalVideo{
Video: videoID,
Bitrate: bitrate,
VideoAuto: peer.videoTrack.VideoAuto(),
})
return nil return nil
} }