Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,15 @@ type RTCConfig struct {

// Deprecated: use PacketBufferSizeVideo and PacketBufferSizeAudio
PacketBufferSize int `yaml:"packet_buffer_size,omitempty"`
// Number of packets to buffer for NACK - video
// Number of packets to buffer for NACK - video (max capacity)
PacketBufferSizeVideo int `yaml:"packet_buffer_size_video,omitempty"`
// Initial bucket size for video packets. If set, the bucket starts at
// this size instead of InitPacketBufferSizeVideo (300). Useful for
// high-PPS H.264 streams under network delay where burst patterns
// exceed the default initial size but the PPS-based growth logic
// doesn't detect the need because it uses average PPS, not peak burst.
// If not set or zero, falls back to InitPacketBufferSizeVideo.
PacketBufferInitVideo int `yaml:"packet_buffer_init_video,omitempty"`
// Number of packets to buffer for NACK - audio
PacketBufferSizeAudio int `yaml:"packet_buffer_size_audio,omitempty"`

Expand Down
2 changes: 2 additions & 0 deletions pkg/rtc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type WebRTCConfig struct {

type ReceiverConfig struct {
PacketBufferSizeVideo int
PacketBufferInitVideo int // Initial bucket size (0 = use InitPacketBufferSizeVideo)
PacketBufferSizeAudio int
}

Expand Down Expand Up @@ -117,6 +118,7 @@ func NewWebRTCConfig(conf *config.Config) (*WebRTCConfig, error) {
WebRTCConfig: *webRTCConfig,
Receiver: ReceiverConfig{
PacketBufferSizeVideo: rtcConf.PacketBufferSizeVideo,
PacketBufferInitVideo: rtcConf.PacketBufferInitVideo,
PacketBufferSizeAudio: rtcConf.PacketBufferSizeAudio,
},
Publisher: publisherConfig,
Expand Down
2 changes: 1 addition & 1 deletion pkg/rtc/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func NewRoom(
participantRequestSources: make(map[livekit.ParticipantIdentity]routing.MessageSource),
hasPublished: make(map[livekit.ParticipantIdentity]bool),
agentParticpants: make(map[livekit.ParticipantIdentity]*agentJob),
bufferFactory: buffer.NewFactoryOfBufferFactory(config.Receiver.PacketBufferSizeVideo, config.Receiver.PacketBufferSizeAudio),
bufferFactory: buffer.NewFactoryOfBufferFactory(config.Receiver.PacketBufferSizeVideo, config.Receiver.PacketBufferInitVideo, config.Receiver.PacketBufferSizeAudio),
batchedUpdates: make(map[livekit.ParticipantIdentity]*participantUpdate),
closed: make(chan struct{}),
trailer: []byte(utils.RandomSecret()),
Expand Down
17 changes: 12 additions & 5 deletions pkg/sfu/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Buffer struct {
bucket *bucket.Bucket[uint64]
nacker *nack.NackQueue
maxVideoPkts int
initVideoPkts int
maxAudioPkts int
codecType webrtc.RTPCodecType
payloadType uint8
Expand Down Expand Up @@ -142,12 +143,13 @@ type Buffer struct {
}

// NewBuffer constructs a new Buffer
func NewBuffer(ssrc uint32, maxVideoPkts, maxAudioPkts int) *Buffer {
func NewBuffer(ssrc uint32, maxVideoPkts, initVideoPkts, maxAudioPkts int) *Buffer {
l := logger.GetLogger() // will be reset with correct context via SetLogger
b := &Buffer{
mediaSSRC: ssrc,
maxVideoPkts: maxVideoPkts,
maxAudioPkts: maxAudioPkts,
mediaSSRC: ssrc,
maxVideoPkts: maxVideoPkts,
initVideoPkts: initVideoPkts,
maxAudioPkts: maxAudioPkts,
snRangeMap: utils.NewRangeMap[uint64, uint64](100),
pliThrottle: int64(500 * time.Millisecond),
logger: l.WithComponent(sutils.ComponentPub).WithComponent(sutils.ComponentSFU),
Expand Down Expand Up @@ -260,7 +262,12 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili

case strings.HasPrefix(b.mime, "video/"):
b.codecType = webrtc.RTPCodecTypeVideo
b.bucket = bucket.NewBucket[uint64](InitPacketBufferSizeVideo)
// Use initVideoPkts from config if set, otherwise fall back to default.
initSize := InitPacketBufferSizeVideo
if b.initVideoPkts > 0 {
initSize = b.initVideoPkts
}
b.bucket = bucket.NewBucket[uint64](initSize)
if b.frameRateCalculator[0] == nil {
if strings.EqualFold(codec.MimeType, webrtc.MimeTypeVP8) {
b.frameRateCalculator[0] = NewFrameRateCalculatorVP8(b.clockRate, b.logger)
Expand Down
8 changes: 6 additions & 2 deletions pkg/sfu/buffer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ import (

type FactoryOfBufferFactory struct {
trackingPacketsVideo int
initPacketsVideo int
trackingPacketsAudio int
}

func NewFactoryOfBufferFactory(trackingPacketsVideo int, trackingPacketsAudio int) *FactoryOfBufferFactory {
func NewFactoryOfBufferFactory(trackingPacketsVideo int, initPacketsVideo int, trackingPacketsAudio int) *FactoryOfBufferFactory {
return &FactoryOfBufferFactory{
trackingPacketsVideo: trackingPacketsVideo,
initPacketsVideo: initPacketsVideo,
trackingPacketsAudio: trackingPacketsAudio,
}
}

func (f *FactoryOfBufferFactory) CreateBufferFactory() *Factory {
return &Factory{
trackingPacketsVideo: f.trackingPacketsVideo,
initPacketsVideo: f.initPacketsVideo,
trackingPacketsAudio: f.trackingPacketsAudio,
rtpBuffers: make(map[uint32]*Buffer),
rtcpReaders: make(map[uint32]*RTCPReader),
Expand All @@ -46,6 +49,7 @@ func (f *FactoryOfBufferFactory) CreateBufferFactory() *Factory {
type Factory struct {
sync.RWMutex
trackingPacketsVideo int
initPacketsVideo int
trackingPacketsAudio int
rtpBuffers map[uint32]*Buffer
rtcpReaders map[uint32]*RTCPReader
Expand All @@ -72,7 +76,7 @@ func (f *Factory) GetOrNew(packetType packetio.BufferPacketType, ssrc uint32) io
if reader, ok := f.rtpBuffers[ssrc]; ok {
return reader
}
buffer := NewBuffer(ssrc, f.trackingPacketsVideo, f.trackingPacketsAudio)
buffer := NewBuffer(ssrc, f.trackingPacketsVideo, f.initPacketsVideo, f.trackingPacketsAudio)
f.rtpBuffers[ssrc] = buffer
for repair, base := range f.rtxPair {
if repair == ssrc {
Expand Down
Loading