Skip to content

Commit

Permalink
Jitter Buffer: Enable setting a minimum length
Browse files Browse the repository at this point in the history
Add additional helper method to set the minimum packet count
before the jitterbuffer will emit packets
  • Loading branch information
thatsnotright committed Feb 24, 2024
1 parent 5574fda commit 523991a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
47 changes: 45 additions & 2 deletions pkg/jitterbuffer/jitter_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type (
// provided timestamp
type JitterBuffer struct {
packets *PriorityQueue

Check failure on line 68 in pkg/jitterbuffer/jitter_buffer.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard,default (gci)
minStartCount uint16
lastSequence uint16
playoutHead uint16
playoutReady bool
Expand All @@ -90,13 +91,21 @@ type Stats struct {

// New will initialize a jitter buffer and its associated statistics
func New(opts ...Option) *JitterBuffer {
jb := &JitterBuffer{state: Buffering, stats: Stats{0, 0, 0}, packets: NewQueue(), listeners: make(map[Event][]EventListener)}
jb := &JitterBuffer{state: Buffering, stats: Stats{0, 0, 0}, minStartCount: 50, packets: NewQueue(), listeners: make(map[Event][]EventListener)}

Check failure on line 94 in pkg/jitterbuffer/jitter_buffer.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard,default (gci)
for _, o := range opts {
o(jb)
}
return jb
}

// WithMinimumPacketCount will set the required number of packets to be received before

Check failure on line 101 in pkg/jitterbuffer/jitter_buffer.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard,default (gci)
// any attempt to pop a packet can succeed
func WithMinimumPacketCount(count uint16) Option {
return func (jb *JitterBuffer) {
jb.minStartCount = count
}

Check warning on line 106 in pkg/jitterbuffer/jitter_buffer.go

View check run for this annotation

Codecov / codecov/patch

pkg/jitterbuffer/jitter_buffer.go#L103-L106

Added lines #L103 - L106 were not covered by tests
}

// Listen will register an event listener
// The jitter buffer may emit events correspnding, interested listerns should
// look at Event for available events
Expand Down Expand Up @@ -142,7 +151,7 @@ func (jb *JitterBuffer) emit(event Event) {

func (jb *JitterBuffer) updateState() {
// For now, we only look at the number of packets captured in the play buffer
if jb.packets.Length() >= 50 && jb.state == Buffering {
if jb.packets.Length() >= jb.minStartCount && jb.state == Buffering {
jb.state = Emitting
jb.playoutReady = true
jb.emit(BeginPlayback)
Expand Down Expand Up @@ -186,6 +195,40 @@ func (jb *JitterBuffer) Pop() (*rtp.Packet, error) {
return packet, nil
}

// PopAtSequence will pop an RTP packet from the jitter buffer at the specified Sequence
func (jb *JitterBuffer) PopAtSequence(sq uint16) (*rtp.Packet, error) {
jb.mutex.Lock()
defer jb.mutex.Unlock()
if jb.state != Emitting {
return nil, ErrPopWhileBuffering
}

Check warning on line 204 in pkg/jitterbuffer/jitter_buffer.go

View check run for this annotation

Codecov / codecov/patch

pkg/jitterbuffer/jitter_buffer.go#L203-L204

Added lines #L203 - L204 were not covered by tests
packet, err := jb.packets.PopAt(sq)
if err != nil {
jb.stats.underflowCount++
jb.emit(BufferUnderflow)
return (*rtp.Packet)(nil), err
}

Check warning on line 210 in pkg/jitterbuffer/jitter_buffer.go

View check run for this annotation

Codecov / codecov/patch

pkg/jitterbuffer/jitter_buffer.go#L207-L210

Added lines #L207 - L210 were not covered by tests
jb.playoutHead = (jb.playoutHead + 1) % math.MaxUint16
jb.updateState()
return packet, nil
}

// PeekAtSequence will return an RTP packet from the jitter buffer at the specified Sequence
// without removing it from the buffer
func (jb *JitterBuffer) PeekAtSequence(sq uint16) (*rtp.Packet, error) {
jb.mutex.Lock()
defer jb.mutex.Unlock()
if jb.state != Emitting {
return nil, ErrPopWhileBuffering
}

Check warning on line 223 in pkg/jitterbuffer/jitter_buffer.go

View check run for this annotation

Codecov / codecov/patch

pkg/jitterbuffer/jitter_buffer.go#L222-L223

Added lines #L222 - L223 were not covered by tests
packet, err := jb.packets.Find(sq)
if err != nil {
return (*rtp.Packet)(nil), err
}

Check warning on line 227 in pkg/jitterbuffer/jitter_buffer.go

View check run for this annotation

Codecov / codecov/patch

pkg/jitterbuffer/jitter_buffer.go#L226-L227

Added lines #L226 - L227 were not covered by tests
return packet, nil
}


// PopAtTimestamp pops an RTP packet from the jitter buffer with the provided timestamp
// Call this method repeatedly to drain the buffer at the timestamp
func (jb *JitterBuffer) PopAtTimestamp(ts uint32) (*rtp.Packet, error) {
Expand Down
22 changes: 22 additions & 0 deletions pkg/jitterbuffer/jitter_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,26 @@ func TestJitterBuffer(t *testing.T) {
assert.Equal(head.SequenceNumber, uint16(math.MaxUint16-32))
assert.Equal(err, nil)
})
t.Run("Peeks at timestamp with multiple packets", func(t *testing.T) {
jb := New()
for i := 0; i < 50; i++ {
sqnum := uint16((math.MaxUint16 - 32 + i) % math.MaxUint16)
jb.Push(&rtp.Packet{Header: rtp.Header{SequenceNumber: sqnum, Timestamp: uint32(512 + i)}, Payload: []byte{0x02}})
}
jb.Push(&rtp.Packet{Header: rtp.Header{SequenceNumber: 1019, Timestamp: uint32(9000)}, Payload: []byte{0x02}})
jb.Push(&rtp.Packet{Header: rtp.Header{SequenceNumber: 1020, Timestamp: uint32(9000)}, Payload: []byte{0x02}})
assert.Equal(jb.packets.Length(), uint16(52))
assert.Equal(jb.state, Emitting)
head, err := jb.PeekAtSequence(uint16(1019))
assert.Equal(head.SequenceNumber, uint16(1019))
assert.Equal(err, nil)
head, err = jb.PeekAtSequence(uint16(1020))
assert.Equal(head.SequenceNumber, uint16(1020))
assert.Equal(err, nil)

head, err = jb.PopAtSequence(uint16(math.MaxUint16 - 32))
assert.Equal(head.SequenceNumber, uint16(math.MaxUint16-32))
assert.Equal(err, nil)
})

Check failure on line 144 in pkg/jitterbuffer/jitter_buffer_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)
}

0 comments on commit 523991a

Please sign in to comment.