Consolidating PLI throttle (#419)
* Consolidating PLI throttle Use the throttler in `sfu.WebRTCReceiver`. Does change shape of config object. * Move PLIThrottleConfig to sfu.WebRTCReceiver * fix test compile * Cleaning up unused stuff * readability improvement
This commit is contained in:
@@ -72,6 +72,9 @@ type Buffer struct {
|
||||
lastSRRecv int64 // Represents wall clock of the most recent sender report arrival
|
||||
lastTransit uint32
|
||||
|
||||
pliThrottle int64
|
||||
lastPli int64
|
||||
|
||||
started bool
|
||||
stats StreamStats
|
||||
rrSnapshot *receiverReportSnapshot
|
||||
@@ -109,6 +112,7 @@ func NewBuffer(ssrc uint32, vp, ap *sync.Pool) *Buffer {
|
||||
mediaSSRC: ssrc,
|
||||
videoPool: vp,
|
||||
audioPool: ap,
|
||||
pliThrottle: int64(500 * time.Millisecond),
|
||||
logger: logger.Logger(logger.GetLogger()), // will be reset with correct context via SetLogger
|
||||
callbackOps: make(chan func(), 50),
|
||||
}
|
||||
@@ -278,11 +282,27 @@ func (b *Buffer) OnClose(fn func()) {
|
||||
b.onClose = fn
|
||||
}
|
||||
|
||||
func (b *Buffer) SendPLI() {
|
||||
func (b *Buffer) SetPLIThrottle(duration int64) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
b.pliThrottle = duration
|
||||
}
|
||||
|
||||
func (b *Buffer) SendPLI() {
|
||||
now := time.Now().UnixNano()
|
||||
|
||||
b.Lock()
|
||||
throttled := now-b.lastPli < b.pliThrottle
|
||||
if throttled {
|
||||
b.Unlock()
|
||||
return
|
||||
}
|
||||
b.lastPli = now
|
||||
b.stats.TotalPLIs++
|
||||
b.Unlock()
|
||||
|
||||
b.logger.Debugw("send pli", "ssrc", b.mediaSSRC)
|
||||
pli := []rtcp.Packet{
|
||||
&rtcp.PictureLossIndication{SenderSSRC: rand.Uint32(), MediaSSRC: b.mediaSSRC},
|
||||
}
|
||||
|
||||
+21
-15
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
)
|
||||
@@ -45,6 +46,8 @@ type TrackReceiver interface {
|
||||
type WebRTCReceiver struct {
|
||||
logger logger.Logger
|
||||
|
||||
pliThrottleConfig config.PLIThrottleConfig
|
||||
|
||||
peerID livekit.ParticipantID
|
||||
trackID livekit.TrackID
|
||||
streamID string
|
||||
@@ -57,10 +60,7 @@ type WebRTCReceiver struct {
|
||||
closed atomicBool
|
||||
useTrackers bool
|
||||
|
||||
rtcpMu sync.Mutex
|
||||
rtcpCh chan []rtcp.Packet
|
||||
lastPli atomicInt64
|
||||
pliThrottle int64
|
||||
rtcpCh chan []rtcp.Packet
|
||||
|
||||
bufferMu sync.RWMutex
|
||||
buffers [DefaultMaxLayerSpatial + 1]*buffer.Buffer
|
||||
@@ -98,9 +98,9 @@ func RidToLayer(rid string) int32 {
|
||||
type ReceiverOpts func(w *WebRTCReceiver) *WebRTCReceiver
|
||||
|
||||
// WithPliThrottle indicates minimum time(ms) between sending PLIs
|
||||
func WithPliThrottle(period int64) ReceiverOpts {
|
||||
func WithPliThrottle(pliThrottleConfig config.PLIThrottleConfig) ReceiverOpts {
|
||||
return func(w *WebRTCReceiver) *WebRTCReceiver {
|
||||
w.pliThrottle = period * 1e6
|
||||
w.pliThrottleConfig = pliThrottleConfig
|
||||
return w
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,6 @@ func NewWebRTCReceiver(
|
||||
kind: track.Kind(),
|
||||
// LK-TODO: this should be based on VideoLayers protocol message rather than RID based
|
||||
isSimulcast: len(track.RID()) > 0,
|
||||
pliThrottle: 500e6,
|
||||
downTracks: make([]TrackSender, 0),
|
||||
index: make(map[livekit.ParticipantID]int),
|
||||
free: make(map[int]struct{}),
|
||||
@@ -236,8 +235,22 @@ func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buff
|
||||
buff.SetLogger(w.logger)
|
||||
buff.OnFeedback(w.sendRTCP)
|
||||
|
||||
layer := RidToLayer(track.RID())
|
||||
var duration time.Duration
|
||||
switch track.RID() {
|
||||
case FullResolution:
|
||||
duration = w.pliThrottleConfig.HighQuality
|
||||
case HalfResolution:
|
||||
duration = w.pliThrottleConfig.MidQuality
|
||||
case QuarterResolution:
|
||||
duration = w.pliThrottleConfig.LowQuality
|
||||
default:
|
||||
duration = w.pliThrottleConfig.MidQuality
|
||||
}
|
||||
if duration != 0 {
|
||||
buff.SetPLIThrottle(duration.Nanoseconds())
|
||||
}
|
||||
|
||||
layer := RidToLayer(track.RID())
|
||||
w.upTrackMu.Lock()
|
||||
w.upTracks[layer] = track
|
||||
w.upTrackMu.Unlock()
|
||||
@@ -362,12 +375,6 @@ func (w *WebRTCReceiver) SendPLI(layer int32) {
|
||||
return
|
||||
}
|
||||
|
||||
throttled := time.Now().UnixNano()-w.lastPli.get() < w.pliThrottle
|
||||
if throttled {
|
||||
return
|
||||
}
|
||||
w.lastPli.set(time.Now().UnixNano())
|
||||
|
||||
buff.SendPLI()
|
||||
}
|
||||
|
||||
@@ -530,7 +537,6 @@ func (w *WebRTCReceiver) storeDownTrack(track TrackSender) {
|
||||
func (w *WebRTCReceiver) DebugInfo() map[string]interface{} {
|
||||
info := map[string]interface{}{
|
||||
"Simulcast": w.isSimulcast,
|
||||
"LastPli": w.lastPli,
|
||||
}
|
||||
|
||||
w.upTrackMu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user