better pli throttling
This commit is contained in:
@@ -285,7 +285,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
||||
})
|
||||
t.params.Stats.AddPublishedTrack(t.kind.String())
|
||||
}
|
||||
t.receiver.AddUpTrack(track, buff, true)
|
||||
t.receiver.AddUpTrack(track, buff, false)
|
||||
// when RID is set, track is simulcasted
|
||||
t.simulcasted = track.RID() != ""
|
||||
|
||||
|
||||
+13
-18
@@ -401,7 +401,6 @@ func (p *ParticipantImpl) Close() error {
|
||||
if onClose != nil {
|
||||
onClose(p)
|
||||
}
|
||||
p.pliThrottle.close()
|
||||
p.publisher.Close()
|
||||
p.subscriber.Close()
|
||||
close(p.rtcpCh)
|
||||
@@ -839,9 +838,6 @@ func (p *ParticipantImpl) handleTrackPublished(track types.PublishedTrack) {
|
||||
if p.IsReady() && p.onTrackUpdated != nil {
|
||||
p.onTrackUpdated(p, track)
|
||||
}
|
||||
if mt, ok := track.(*MediaTrack); ok {
|
||||
p.pliThrottle.removeTrack(uint32(mt.ssrc))
|
||||
}
|
||||
track.OnClose(nil)
|
||||
})
|
||||
|
||||
@@ -933,36 +929,35 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
|
||||
func (p *ParticipantImpl) rtcpSendWorker() {
|
||||
defer Recover()
|
||||
|
||||
write := func(pkts []rtcp.Packet) {
|
||||
if err := p.publisher.pc.WriteRTCP(pkts); err != nil {
|
||||
logger.Errorw("could not write RTCP to participant", err,
|
||||
"participant", p.Identity())
|
||||
}
|
||||
}
|
||||
|
||||
// read from rtcpChan
|
||||
for pkts := range p.rtcpCh {
|
||||
if pkts == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fwdPkts := make([]rtcp.Packet, 0, len(pkts))
|
||||
for _, pkt := range pkts {
|
||||
var mediaSSRC uint32
|
||||
switch pkt.(type) {
|
||||
case *rtcp.PictureLossIndication:
|
||||
mediaSSRC = pkt.(*rtcp.PictureLossIndication).MediaSSRC
|
||||
mediaSSRC := pkt.(*rtcp.PictureLossIndication).MediaSSRC
|
||||
if p.pliThrottle.canSend(mediaSSRC) {
|
||||
fwdPkts = append(fwdPkts, pkt)
|
||||
}
|
||||
case *rtcp.FullIntraRequest:
|
||||
mediaSSRC = pkt.(*rtcp.FullIntraRequest).MediaSSRC
|
||||
mediaSSRC := pkt.(*rtcp.FullIntraRequest).MediaSSRC
|
||||
if p.pliThrottle.canSend(mediaSSRC) {
|
||||
fwdPkts = append(fwdPkts, pkt)
|
||||
}
|
||||
default:
|
||||
fwdPkts = append(fwdPkts, pkt)
|
||||
continue
|
||||
}
|
||||
|
||||
p.pliThrottle.add(mediaSSRC, func() { write([]rtcp.Packet{pkt}) })
|
||||
}
|
||||
|
||||
if len(fwdPkts) > 0 {
|
||||
write(fwdPkts)
|
||||
if err := p.publisher.pc.WriteRTCP(fwdPkts); err != nil {
|
||||
logger.Errorw("could not write RTCP to participant", err,
|
||||
"participant", p.Identity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-31
@@ -4,15 +4,14 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/frostbyte73/go-throttle"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
)
|
||||
|
||||
type pliThrottle struct {
|
||||
config config.PLIThrottleConfig
|
||||
mu sync.RWMutex
|
||||
throttles map[uint32]func(func())
|
||||
config config.PLIThrottleConfig
|
||||
mu sync.RWMutex
|
||||
periods map[uint32]int64
|
||||
lastSent map[uint32]int64
|
||||
}
|
||||
|
||||
// github.com/pion/ion-sfu/pkg/sfu/simulcast.go
|
||||
@@ -24,8 +23,9 @@ const (
|
||||
|
||||
func newPLIThrottle(conf config.PLIThrottleConfig) *pliThrottle {
|
||||
return &pliThrottle{
|
||||
config: conf,
|
||||
throttles: make(map[uint32]func(func())),
|
||||
config: conf,
|
||||
periods: make(map[uint32]int64),
|
||||
lastSent: make(map[uint32]int64),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,34 +45,20 @@ func (t *pliThrottle) addTrack(ssrc uint32, rid string) {
|
||||
duration = t.config.MidQuality
|
||||
}
|
||||
|
||||
t.throttles[ssrc] = throttle.New(duration)
|
||||
t.periods[ssrc] = duration.Nanoseconds()
|
||||
}
|
||||
|
||||
func (t *pliThrottle) add(ssrc uint32, f func()) {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
|
||||
if trackThrottle, ok := t.throttles[ssrc]; ok {
|
||||
trackThrottle(f)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *pliThrottle) removeTrack(ssrc uint32) {
|
||||
func (t *pliThrottle) canSend(ssrc uint32) bool {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if trackThrottle, ok := t.throttles[ssrc]; ok {
|
||||
trackThrottle(func() {})
|
||||
delete(t.throttles, ssrc)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *pliThrottle) close() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
for ssrc, trackThrottle := range t.throttles {
|
||||
trackThrottle(func() {})
|
||||
delete(t.throttles, ssrc)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user