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:
@@ -48,13 +48,14 @@ type MediaTrackParams struct {
|
|||||||
ParticipantID livekit.ParticipantID
|
ParticipantID livekit.ParticipantID
|
||||||
ParticipantIdentity livekit.ParticipantIdentity
|
ParticipantIdentity livekit.ParticipantIdentity
|
||||||
// channel to send RTCP packets to the source
|
// channel to send RTCP packets to the source
|
||||||
RTCPChan chan []rtcp.Packet
|
RTCPChan chan []rtcp.Packet
|
||||||
BufferFactory *buffer.Factory
|
BufferFactory *buffer.Factory
|
||||||
ReceiverConfig ReceiverConfig
|
ReceiverConfig ReceiverConfig
|
||||||
SubscriberConfig DirectionConfig
|
SubscriberConfig DirectionConfig
|
||||||
AudioConfig config.AudioConfig
|
PLIThrottleConfig config.PLIThrottleConfig
|
||||||
Telemetry telemetry.TelemetryService
|
AudioConfig config.AudioConfig
|
||||||
Logger logger.Logger
|
Telemetry telemetry.TelemetryService
|
||||||
|
Logger logger.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTrack {
|
func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTrack {
|
||||||
@@ -169,7 +170,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
|||||||
track,
|
track,
|
||||||
t.PublisherID(),
|
t.PublisherID(),
|
||||||
t.params.Logger,
|
t.params.Logger,
|
||||||
sfu.WithPliThrottle(0),
|
sfu.WithPliThrottle(t.params.PLIThrottleConfig),
|
||||||
sfu.WithLoadBalanceThreshold(20),
|
sfu.WithLoadBalanceThreshold(20),
|
||||||
sfu.WithStreamTrackers(),
|
sfu.WithStreamTrackers(),
|
||||||
)
|
)
|
||||||
|
|||||||
+5
-29
@@ -49,7 +49,7 @@ type ParticipantParams struct {
|
|||||||
AudioConfig config.AudioConfig
|
AudioConfig config.AudioConfig
|
||||||
ProtocolVersion types.ProtocolVersion
|
ProtocolVersion types.ProtocolVersion
|
||||||
Telemetry telemetry.TelemetryService
|
Telemetry telemetry.TelemetryService
|
||||||
ThrottleConfig config.PLIThrottleConfig
|
PLIThrottleConfig config.PLIThrottleConfig
|
||||||
CongestionControlConfig config.CongestionControlConfig
|
CongestionControlConfig config.CongestionControlConfig
|
||||||
EnabledCodecs []*livekit.Codec
|
EnabledCodecs []*livekit.Codec
|
||||||
Hidden bool
|
Hidden bool
|
||||||
@@ -82,8 +82,7 @@ type ParticipantImpl struct {
|
|||||||
// JSON encoded metadata to pass to clients
|
// JSON encoded metadata to pass to clients
|
||||||
metadata string
|
metadata string
|
||||||
|
|
||||||
rtcpCh chan []rtcp.Packet
|
rtcpCh chan []rtcp.Packet
|
||||||
pliThrottle *pliThrottle
|
|
||||||
|
|
||||||
// hold reference for MediaTrack
|
// hold reference for MediaTrack
|
||||||
twcc *twcc.Responder
|
twcc *twcc.Responder
|
||||||
@@ -127,7 +126,6 @@ func NewParticipant(params ParticipantParams, perms *livekit.ParticipantPermissi
|
|||||||
p := &ParticipantImpl{
|
p := &ParticipantImpl{
|
||||||
params: params,
|
params: params,
|
||||||
rtcpCh: make(chan []rtcp.Packet, 50),
|
rtcpCh: make(chan []rtcp.Packet, 50),
|
||||||
pliThrottle: newPLIThrottle(params.ThrottleConfig),
|
|
||||||
pendingTracks: make(map[string]*pendingTrackInfo),
|
pendingTracks: make(map[string]*pendingTrackInfo),
|
||||||
subscribedTracks: make(map[livekit.TrackID]types.SubscribedTrack),
|
subscribedTracks: make(map[livekit.TrackID]types.SubscribedTrack),
|
||||||
subscribedTracksSettings: make(map[livekit.TrackID]*livekit.UpdateTrackSettings),
|
subscribedTracksSettings: make(map[livekit.TrackID]*livekit.UpdateTrackSettings),
|
||||||
@@ -1426,6 +1424,7 @@ func (p *ParticipantImpl) mediaTrackReceived(track *webrtc.TrackRemote, rtpRecei
|
|||||||
Telemetry: p.params.Telemetry,
|
Telemetry: p.params.Telemetry,
|
||||||
Logger: LoggerWithTrack(p.params.Logger, livekit.TrackID(ti.Sid)),
|
Logger: LoggerWithTrack(p.params.Logger, livekit.TrackID(ti.Sid)),
|
||||||
SubscriberConfig: p.params.Config.Subscriber,
|
SubscriberConfig: p.params.Config.Subscriber,
|
||||||
|
PLIThrottleConfig: p.params.PLIThrottleConfig,
|
||||||
})
|
})
|
||||||
|
|
||||||
for ssrc, info := range p.params.SimTracks {
|
for ssrc, info := range p.params.SimTracks {
|
||||||
@@ -1444,7 +1443,6 @@ func (p *ParticipantImpl) mediaTrackReceived(track *webrtc.TrackRemote, rtpRecei
|
|||||||
}
|
}
|
||||||
|
|
||||||
ssrc := uint32(track.SSRC())
|
ssrc := uint32(track.SSRC())
|
||||||
p.pliThrottle.addTrack(ssrc, track.RID())
|
|
||||||
if p.twcc == nil {
|
if p.twcc == nil {
|
||||||
p.twcc = twcc.NewTransportWideCCResponder(ssrc)
|
p.twcc = twcc.NewTransportWideCCResponder(ssrc)
|
||||||
p.twcc.OnFeedback(func(pkt rtcp.RawPacket) {
|
p.twcc.OnFeedback(func(pkt rtcp.RawPacket) {
|
||||||
@@ -1548,30 +1546,8 @@ func (p *ParticipantImpl) rtcpSendWorker() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fwdPkts := make([]rtcp.Packet, 0, len(pkts))
|
if err := p.publisher.pc.WriteRTCP(pkts); err != nil {
|
||||||
for _, pkt := range pkts {
|
p.params.Logger.Errorw("could not write RTCP to participant", err)
|
||||||
switch packet := pkt.(type) {
|
|
||||||
case *rtcp.PictureLossIndication:
|
|
||||||
mediaSSRC := packet.MediaSSRC
|
|
||||||
if p.pliThrottle.canSend(mediaSSRC) {
|
|
||||||
p.params.Logger.Debugw("send pli", "ssrc", mediaSSRC)
|
|
||||||
fwdPkts = append(fwdPkts, pkt)
|
|
||||||
}
|
|
||||||
case *rtcp.FullIntraRequest:
|
|
||||||
mediaSSRC := packet.MediaSSRC
|
|
||||||
if p.pliThrottle.canSend(mediaSSRC) {
|
|
||||||
p.params.Logger.Debugw("send fir", "ssrc", mediaSSRC)
|
|
||||||
fwdPkts = append(fwdPkts, pkt)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
fwdPkts = append(fwdPkts, pkt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(fwdPkts) > 0 {
|
|
||||||
if err := p.publisher.pc.WriteRTCP(fwdPkts); err != nil {
|
|
||||||
p.params.Logger.Errorw("could not write RTCP to participant", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -368,11 +368,11 @@ func newParticipantForTestWithOpts(identity livekit.ParticipantIdentity, opts *p
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
p, _ := NewParticipant(ParticipantParams{
|
p, _ := NewParticipant(ParticipantParams{
|
||||||
Identity: identity,
|
Identity: identity,
|
||||||
Config: rtcConf,
|
Config: rtcConf,
|
||||||
Sink: &routingfakes.FakeMessageSink{},
|
Sink: &routingfakes.FakeMessageSink{},
|
||||||
ProtocolVersion: opts.protocolVersion,
|
ProtocolVersion: opts.protocolVersion,
|
||||||
ThrottleConfig: conf.RTC.PLIThrottle,
|
PLIThrottleConfig: conf.RTC.PLIThrottle,
|
||||||
}, opts.permissions)
|
}, opts.permissions)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
package rtc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
|
||||||
"github.com/livekit/livekit-server/pkg/sfu"
|
|
||||||
)
|
|
||||||
|
|
||||||
type pliThrottle struct {
|
|
||||||
config config.PLIThrottleConfig
|
|
||||||
mu sync.RWMutex
|
|
||||||
periods map[uint32]int64
|
|
||||||
lastSent map[uint32]int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPLIThrottle(conf config.PLIThrottleConfig) *pliThrottle {
|
|
||||||
return &pliThrottle{
|
|
||||||
config: conf,
|
|
||||||
periods: make(map[uint32]int64),
|
|
||||||
lastSent: make(map[uint32]int64),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *pliThrottle) addTrack(ssrc uint32, rid string) {
|
|
||||||
t.mu.Lock()
|
|
||||||
defer t.mu.Unlock()
|
|
||||||
|
|
||||||
var duration time.Duration
|
|
||||||
switch rid {
|
|
||||||
case sfu.FullResolution:
|
|
||||||
duration = t.config.HighQuality
|
|
||||||
case sfu.HalfResolution:
|
|
||||||
duration = t.config.MidQuality
|
|
||||||
case sfu.QuarterResolution:
|
|
||||||
duration = t.config.LowQuality
|
|
||||||
default:
|
|
||||||
duration = t.config.MidQuality
|
|
||||||
}
|
|
||||||
|
|
||||||
t.periods[ssrc] = duration.Nanoseconds()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *pliThrottle) canSend(ssrc uint32) bool {
|
|
||||||
t.mu.Lock()
|
|
||||||
defer t.mu.Unlock()
|
|
||||||
|
|
||||||
if period, ok := t.periods[ssrc]; ok {
|
|
||||||
if n := time.Now().UnixNano(); n-t.lastSent[ssrc] > period {
|
|
||||||
t.lastSent[ssrc] = n
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
@@ -242,7 +242,7 @@ func (r *RoomManager) StartSession(ctx context.Context, roomName livekit.RoomNam
|
|||||||
AudioConfig: r.config.Audio,
|
AudioConfig: r.config.Audio,
|
||||||
ProtocolVersion: pv,
|
ProtocolVersion: pv,
|
||||||
Telemetry: r.telemetry,
|
Telemetry: r.telemetry,
|
||||||
ThrottleConfig: r.config.RTC.PLIThrottle,
|
PLIThrottleConfig: r.config.RTC.PLIThrottle,
|
||||||
CongestionControlConfig: r.config.RTC.CongestionControl,
|
CongestionControlConfig: r.config.RTC.CongestionControl,
|
||||||
EnabledCodecs: room.Room.EnabledCodecs,
|
EnabledCodecs: room.Room.EnabledCodecs,
|
||||||
Grants: pi.Grants,
|
Grants: pi.Grants,
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ type Buffer struct {
|
|||||||
lastSRRecv int64 // Represents wall clock of the most recent sender report arrival
|
lastSRRecv int64 // Represents wall clock of the most recent sender report arrival
|
||||||
lastTransit uint32
|
lastTransit uint32
|
||||||
|
|
||||||
|
pliThrottle int64
|
||||||
|
lastPli int64
|
||||||
|
|
||||||
started bool
|
started bool
|
||||||
stats StreamStats
|
stats StreamStats
|
||||||
rrSnapshot *receiverReportSnapshot
|
rrSnapshot *receiverReportSnapshot
|
||||||
@@ -109,6 +112,7 @@ func NewBuffer(ssrc uint32, vp, ap *sync.Pool) *Buffer {
|
|||||||
mediaSSRC: ssrc,
|
mediaSSRC: ssrc,
|
||||||
videoPool: vp,
|
videoPool: vp,
|
||||||
audioPool: ap,
|
audioPool: ap,
|
||||||
|
pliThrottle: int64(500 * time.Millisecond),
|
||||||
logger: logger.Logger(logger.GetLogger()), // will be reset with correct context via SetLogger
|
logger: logger.Logger(logger.GetLogger()), // will be reset with correct context via SetLogger
|
||||||
callbackOps: make(chan func(), 50),
|
callbackOps: make(chan func(), 50),
|
||||||
}
|
}
|
||||||
@@ -278,11 +282,27 @@ func (b *Buffer) OnClose(fn func()) {
|
|||||||
b.onClose = fn
|
b.onClose = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) SendPLI() {
|
func (b *Buffer) SetPLIThrottle(duration int64) {
|
||||||
b.Lock()
|
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.stats.TotalPLIs++
|
||||||
b.Unlock()
|
b.Unlock()
|
||||||
|
|
||||||
|
b.logger.Debugw("send pli", "ssrc", b.mediaSSRC)
|
||||||
pli := []rtcp.Packet{
|
pli := []rtcp.Packet{
|
||||||
&rtcp.PictureLossIndication{SenderSSRC: rand.Uint32(), MediaSSRC: b.mediaSSRC},
|
&rtcp.PictureLossIndication{SenderSSRC: rand.Uint32(), MediaSSRC: b.mediaSSRC},
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-15
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/rs/zerolog/log"
|
"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/buffer"
|
||||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||||
)
|
)
|
||||||
@@ -45,6 +46,8 @@ type TrackReceiver interface {
|
|||||||
type WebRTCReceiver struct {
|
type WebRTCReceiver struct {
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
|
|
||||||
|
pliThrottleConfig config.PLIThrottleConfig
|
||||||
|
|
||||||
peerID livekit.ParticipantID
|
peerID livekit.ParticipantID
|
||||||
trackID livekit.TrackID
|
trackID livekit.TrackID
|
||||||
streamID string
|
streamID string
|
||||||
@@ -57,10 +60,7 @@ type WebRTCReceiver struct {
|
|||||||
closed atomicBool
|
closed atomicBool
|
||||||
useTrackers bool
|
useTrackers bool
|
||||||
|
|
||||||
rtcpMu sync.Mutex
|
rtcpCh chan []rtcp.Packet
|
||||||
rtcpCh chan []rtcp.Packet
|
|
||||||
lastPli atomicInt64
|
|
||||||
pliThrottle int64
|
|
||||||
|
|
||||||
bufferMu sync.RWMutex
|
bufferMu sync.RWMutex
|
||||||
buffers [DefaultMaxLayerSpatial + 1]*buffer.Buffer
|
buffers [DefaultMaxLayerSpatial + 1]*buffer.Buffer
|
||||||
@@ -98,9 +98,9 @@ func RidToLayer(rid string) int32 {
|
|||||||
type ReceiverOpts func(w *WebRTCReceiver) *WebRTCReceiver
|
type ReceiverOpts func(w *WebRTCReceiver) *WebRTCReceiver
|
||||||
|
|
||||||
// WithPliThrottle indicates minimum time(ms) between sending PLIs
|
// WithPliThrottle indicates minimum time(ms) between sending PLIs
|
||||||
func WithPliThrottle(period int64) ReceiverOpts {
|
func WithPliThrottle(pliThrottleConfig config.PLIThrottleConfig) ReceiverOpts {
|
||||||
return func(w *WebRTCReceiver) *WebRTCReceiver {
|
return func(w *WebRTCReceiver) *WebRTCReceiver {
|
||||||
w.pliThrottle = period * 1e6
|
w.pliThrottleConfig = pliThrottleConfig
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,7 +143,6 @@ func NewWebRTCReceiver(
|
|||||||
kind: track.Kind(),
|
kind: track.Kind(),
|
||||||
// LK-TODO: this should be based on VideoLayers protocol message rather than RID based
|
// LK-TODO: this should be based on VideoLayers protocol message rather than RID based
|
||||||
isSimulcast: len(track.RID()) > 0,
|
isSimulcast: len(track.RID()) > 0,
|
||||||
pliThrottle: 500e6,
|
|
||||||
downTracks: make([]TrackSender, 0),
|
downTracks: make([]TrackSender, 0),
|
||||||
index: make(map[livekit.ParticipantID]int),
|
index: make(map[livekit.ParticipantID]int),
|
||||||
free: make(map[int]struct{}),
|
free: make(map[int]struct{}),
|
||||||
@@ -236,8 +235,22 @@ func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buff
|
|||||||
buff.SetLogger(w.logger)
|
buff.SetLogger(w.logger)
|
||||||
buff.OnFeedback(w.sendRTCP)
|
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.upTrackMu.Lock()
|
||||||
w.upTracks[layer] = track
|
w.upTracks[layer] = track
|
||||||
w.upTrackMu.Unlock()
|
w.upTrackMu.Unlock()
|
||||||
@@ -362,12 +375,6 @@ func (w *WebRTCReceiver) SendPLI(layer int32) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
throttled := time.Now().UnixNano()-w.lastPli.get() < w.pliThrottle
|
|
||||||
if throttled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.lastPli.set(time.Now().UnixNano())
|
|
||||||
|
|
||||||
buff.SendPLI()
|
buff.SendPLI()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,7 +537,6 @@ func (w *WebRTCReceiver) storeDownTrack(track TrackSender) {
|
|||||||
func (w *WebRTCReceiver) DebugInfo() map[string]interface{} {
|
func (w *WebRTCReceiver) DebugInfo() map[string]interface{} {
|
||||||
info := map[string]interface{}{
|
info := map[string]interface{}{
|
||||||
"Simulcast": w.isSimulcast,
|
"Simulcast": w.isSimulcast,
|
||||||
"LastPli": w.lastPli,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.upTrackMu.RLock()
|
w.upTrackMu.RLock()
|
||||||
|
|||||||
Reference in New Issue
Block a user