move estimator to peer.

This commit is contained in:
Miroslav Šedivý 2023-04-17 01:10:20 +02:00
parent 1ba561b192
commit fcf6822833
2 changed files with 42 additions and 32 deletions

View File

@ -370,6 +370,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
rtcpChannel: videoRtcp, rtcpChannel: videoRtcp,
// config // config
iceTrickle: manager.config.ICETrickle, iceTrickle: manager.config.ICETrickle,
estimatorPassive: manager.config.EstimatorPassive,
} }
logger.Info(). logger.Info().
@ -381,33 +382,6 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
return nil, err return nil, err
} }
// if estimator is enabled, use it to change video stream
if estimator != nil {
go func() {
// use a ticker to get current client target bitrate
ticker := time.NewTicker(bitrateCheckInterval)
defer ticker.Stop()
for range ticker.C {
targetBitrate := estimator.GetTargetBitrate()
metrics.SetReceiverEstimatedTargetBitrate(float64(targetBitrate))
if connection.ConnectionState() == webrtc.PeerConnectionStateClosed {
break
}
if !videoTrack.VideoAuto() {
continue
}
if !manager.config.EstimatorPassive {
err := peer.SetVideoBitrate(targetBitrate)
if err != nil {
logger.Warn().Err(err).Msg("failed to set video bitrate")
}
}
}
}()
}
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()).
@ -602,6 +576,9 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
go metrics.rtcpReceiver(videoRtcp) go metrics.rtcpReceiver(videoRtcp)
go metrics.connectionStats(connection) go metrics.connectionStats(connection)
// start estimator reader
go peer.estimatorReader()
return offer, nil return offer, nil
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"sync" "sync"
"time"
"github.com/pion/interceptor/pkg/cc" "github.com/pion/interceptor/pkg/cc"
"github.com/pion/rtcp" "github.com/pion/rtcp"
@ -30,6 +31,7 @@ type WebRTCPeerCtx struct {
rtcpChannel chan []rtcp.Packet rtcpChannel chan []rtcp.Packet
// config // config
iceTrickle bool iceTrickle bool
estimatorPassive bool
} }
// //
@ -106,6 +108,37 @@ func (peer *WebRTCPeerCtx) Destroy() {
} }
} }
func (peer *WebRTCPeerCtx) estimatorReader() {
// if estimator is disabled, do nothing
if peer.estimator == nil {
return
}
// use a ticker to get current client target bitrate
ticker := time.NewTicker(bitrateCheckInterval)
defer ticker.Stop()
for range ticker.C {
targetBitrate := peer.estimator.GetTargetBitrate()
peer.metrics.SetReceiverEstimatedTargetBitrate(float64(targetBitrate))
if peer.connection.ConnectionState() == webrtc.PeerConnectionStateClosed {
break
}
if !peer.videoTrack.VideoAuto() {
continue
}
if !peer.estimatorPassive {
err := peer.SetVideoBitrate(targetBitrate)
if err != nil {
peer.logger.Warn().Err(err).Msg("failed to set video bitrate")
}
}
}
}
// //
// video // video
// //
@ -116,7 +149,7 @@ func (peer *WebRTCPeerCtx) SetVideoBitrate(peerBitrate int) error {
// when switching from manual to auto bitrate estimation, in case the estimator is // 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 // idle (lastBitrate > maxBitrate), we want to go back to the previous estimated bitrate
if peerBitrate == 0 && peer.estimator != nil { if peerBitrate == 0 && peer.estimator != nil && !peer.estimatorPassive {
peerBitrate = peer.estimator.GetTargetBitrate() peerBitrate = peer.estimator.GetTargetBitrate()
peer.logger.Debug(). peer.logger.Debug().
Int("peer_bitrate", peerBitrate). Int("peer_bitrate", peerBitrate).
@ -205,8 +238,8 @@ func (peer *WebRTCPeerCtx) SetPaused(isPaused bool) error {
} }
func (peer *WebRTCPeerCtx) SetVideoAuto(videoAuto bool) { func (peer *WebRTCPeerCtx) SetVideoAuto(videoAuto bool) {
// if estimator is enabled, enable video auto bitrate // if estimator is enabled and is not passive, enable video auto bitrate
if peer.estimator != nil { if peer.estimator != nil && !peer.estimatorPassive {
peer.videoTrack.SetVideoAuto(videoAuto) peer.videoTrack.SetVideoAuto(videoAuto)
} else { } else {
peer.logger.Warn().Msg("estimator is disabled or in passive mode, cannot change video auto") peer.logger.Warn().Msg("estimator is disabled or in passive mode, cannot change video auto")