Remove callbacks queue from sfu/buffer. (#650)

This commit is contained in:
Raja Subramanian
2022-04-24 21:39:28 +05:30
committed by GitHub
parent 26700ea135
commit 0a802077dd
3 changed files with 25 additions and 33 deletions
+20 -28
View File
@@ -20,7 +20,6 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/audio" "github.com/livekit/livekit-server/pkg/sfu/audio"
"github.com/livekit/livekit-server/pkg/sfu/twcc" "github.com/livekit/livekit-server/pkg/sfu/twcc"
"github.com/livekit/livekit-server/pkg/utils"
) )
const ( const (
@@ -83,10 +82,8 @@ type Buffer struct {
lastFractionLostToReport uint8 // Last fraction lost from subscribers, should report to publisher; Audio only lastFractionLostToReport uint8 // Last fraction lost from subscribers, should report to publisher; Audio only
// callbacks // callbacks
onClose func() onClose func()
feedbackCB func([]rtcp.Packet) onRtcpFeedback func([]rtcp.Packet)
callbacksQueue *utils.OpsQueue
// logger // logger
logger logger.Logger logger logger.Logger
@@ -101,12 +98,11 @@ type Options struct {
func NewBuffer(ssrc uint32, vp, ap *sync.Pool) *Buffer { func NewBuffer(ssrc uint32, vp, ap *sync.Pool) *Buffer {
l := logger.GetDefaultLogger() // will be reset with correct context via SetLogger l := logger.GetDefaultLogger() // will be reset with correct context via SetLogger
b := &Buffer{ b := &Buffer{
mediaSSRC: ssrc, mediaSSRC: ssrc,
videoPool: vp, videoPool: vp,
audioPool: ap, audioPool: ap,
pliThrottle: int64(500 * time.Millisecond), pliThrottle: int64(500 * time.Millisecond),
logger: l, logger: l,
callbacksQueue: utils.NewOpsQueue(l, "sfu-buffer", 50),
} }
b.extPackets.SetMinCapacity(7) b.extPackets.SetMinCapacity(7)
return b return b
@@ -117,7 +113,6 @@ func (b *Buffer) SetLogger(logger logger.Logger) {
defer b.Unlock() defer b.Unlock()
b.logger = logger b.logger = logger
b.callbacksQueue.SetLogger(logger)
if b.rtpStats != nil { if b.rtpStats != nil {
b.rtpStats.SetLogger(logger) b.rtpStats.SetLogger(logger)
} }
@@ -152,8 +147,6 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
b.connectionQualitySnapshotId = b.rtpStats.NewSnapshotId() b.connectionQualitySnapshotId = b.rtpStats.NewSnapshotId()
b.deltaStatsSnapshotId = b.rtpStats.NewSnapshotId() b.deltaStatsSnapshotId = b.rtpStats.NewSnapshotId()
b.callbacksQueue.Start()
b.clockRate = codec.ClockRate b.clockRate = codec.ClockRate
b.lastReport = time.Now().UnixNano() b.lastReport = time.Now().UnixNano()
b.mime = strings.ToLower(codec.MimeType) b.mime = strings.ToLower(codec.MimeType)
@@ -288,8 +281,9 @@ func (b *Buffer) Close() error {
b.logger.Debugw("rtp stats", "stats", b.rtpStats.ToString()) b.logger.Debugw("rtp stats", "stats", b.rtpStats.ToString())
} }
b.callbacksQueue.Enqueue(b.onClose) if b.onClose != nil {
b.callbacksQueue.Stop() b.onClose()
}
}) })
return nil return nil
} }
@@ -320,9 +314,9 @@ func (b *Buffer) SendPLI() {
&rtcp.PictureLossIndication{SenderSSRC: rand.Uint32(), MediaSSRC: b.mediaSSRC}, &rtcp.PictureLossIndication{SenderSSRC: rand.Uint32(), MediaSSRC: b.mediaSSRC},
} }
b.callbacksQueue.Enqueue(func() { if b.onRtcpFeedback != nil {
b.feedbackCB(pli) b.onRtcpFeedback(pli)
}) }
} }
func (b *Buffer) SetRTT(rtt uint32) { func (b *Buffer) SetRTT(rtt uint32) {
@@ -480,9 +474,9 @@ func (b *Buffer) doNACKs() {
} }
if r, numSeqNumsNacked := b.buildNACKPacket(); r != nil { if r, numSeqNumsNacked := b.buildNACKPacket(); r != nil {
b.callbacksQueue.Enqueue(func() { if b.onRtcpFeedback != nil {
b.feedbackCB(r) b.onRtcpFeedback(r)
}) }
if b.rtpStats != nil { if b.rtpStats != nil {
b.rtpStats.UpdateNack(uint32(numSeqNumsNacked)) b.rtpStats.UpdateNack(uint32(numSeqNumsNacked))
} }
@@ -499,10 +493,8 @@ func (b *Buffer) doReports(arrivalTime int64) {
// RTCP reports // RTCP reports
pkts := b.getRTCP() pkts := b.getRTCP()
if pkts != nil { if pkts != nil && b.onRtcpFeedback != nil {
b.callbacksQueue.Enqueue(func() { b.onRtcpFeedback(pkts)
b.feedbackCB(pkts)
})
} }
} }
@@ -566,8 +558,8 @@ func (b *Buffer) GetPacket(buff []byte, sn uint16) (int, error) {
return b.bucket.GetPacket(buff, sn) return b.bucket.GetPacket(buff, sn)
} }
func (b *Buffer) OnFeedback(fn func(fb []rtcp.Packet)) { func (b *Buffer) OnRtcpFeedback(fn func(fb []rtcp.Packet)) {
b.feedbackCB = fn b.onRtcpFeedback = fn
} }
// GetMediaSSRC returns the associated SSRC of the RTP stream // GetMediaSSRC returns the associated SSRC of the RTP stream
+4 -4
View File
@@ -46,7 +46,7 @@ func TestNack(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
// 5 tries // 5 tries
wg.Add(5) wg.Add(5)
buff.OnFeedback(func(fb []rtcp.Packet) { buff.OnRtcpFeedback(func(fb []rtcp.Packet) {
for _, pkt := range fb { for _, pkt := range fb {
switch p := pkt.(type) { switch p := pkt.(type) {
case *rtcp.TransportLayerNack: case *rtcp.TransportLayerNack:
@@ -96,7 +96,7 @@ func TestNack(t *testing.T) {
1: 0, 1: 0,
} }
wg.Add(5 * len(expects)) // retry 5 times wg.Add(5 * len(expects)) // retry 5 times
buff.OnFeedback(func(fb []rtcp.Packet) { buff.OnRtcpFeedback(func(fb []rtcp.Packet) {
for _, pkt := range fb { for _, pkt := range fb {
switch p := pkt.(type) { switch p := pkt.(type) {
case *rtcp.TransportLayerNack: case *rtcp.TransportLayerNack:
@@ -194,7 +194,7 @@ func TestNewBuffer(t *testing.T) {
buff := NewBuffer(123, pool, pool) buff := NewBuffer(123, pool, pool)
buff.codecType = webrtc.RTPCodecTypeVideo buff.codecType = webrtc.RTPCodecTypeVideo
require.NotNil(t, buff) require.NotNil(t, buff)
buff.OnFeedback(func(_ []rtcp.Packet) { buff.OnRtcpFeedback(func(_ []rtcp.Packet) {
}) })
buff.Bind(webrtc.RTPParameters{ buff.Bind(webrtc.RTPParameters{
HeaderExtensions: nil, HeaderExtensions: nil,
@@ -224,7 +224,7 @@ func TestFractionLostReport(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
buff.SetLastFractionLostReport(55) buff.SetLastFractionLostReport(55)
buff.OnFeedback(func(fb []rtcp.Packet) { buff.OnRtcpFeedback(func(fb []rtcp.Packet) {
for _, pkt := range fb { for _, pkt := range fb {
switch p := pkt.(type) { switch p := pkt.(type) {
case *rtcp.ReceiverReport: case *rtcp.ReceiverReport:
+1 -1
View File
@@ -277,7 +277,7 @@ func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buff
ObserveDuration: w.audioConfig.UpdateInterval, ObserveDuration: w.audioConfig.UpdateInterval,
SmoothIntervals: w.audioConfig.SmoothIntervals, SmoothIntervals: w.audioConfig.SmoothIntervals,
}) })
buff.OnFeedback(w.sendRTCP) buff.OnRtcpFeedback(w.sendRTCP)
var duration time.Duration var duration time.Duration
switch track.RID() { switch track.RID() {