Skip to content

Commit

Permalink
packetPool: flush old packets whenever new payload starts (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 authored Apr 10, 2021
1 parent 077af8e commit c44c340
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions packet_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ func (b *packetPool) add(p *Packet) (ps []*Packet) {
return
}

// Add packet
if len(mps) > 0 || (len(mps) == 0 && p.Header.PayloadUnitStartIndicator) {
mps = append(mps, p)
}

// Check payload unit start indicator
if p.Header.PayloadUnitStartIndicator && len(mps) > 1 {
ps = mps[:len(mps)-1]
// Flush buffer if new payload starts here
if p.Header.PayloadUnitStartIndicator {
ps = mps
mps = []*Packet{p}
} else {
mps = append(mps, p)
}

// Assign
Expand Down
4 changes: 2 additions & 2 deletions packet_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestPacketPool(t *testing.T) {
ps := b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 0, HasPayload: true, PID: 1}})
assert.Len(t, ps, 0)
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 1, HasPayload: true, PayloadUnitStartIndicator: true, PID: 1}})
assert.Len(t, ps, 0)
assert.Len(t, ps, 1)
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 1, HasPayload: true, PayloadUnitStartIndicator: true, PID: 2}})
assert.Len(t, ps, 0)
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 2, HasPayload: true, PID: 1}})
Expand All @@ -35,7 +35,7 @@ func TestPacketPool(t *testing.T) {
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 5, HasPayload: true, PID: 1}})
assert.Len(t, ps, 0)
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 6, HasPayload: true, PayloadUnitStartIndicator: true, PID: 1}})
assert.Len(t, ps, 0)
assert.Len(t, ps, 1)
ps = b.add(&Packet{Header: &PacketHeader{ContinuityCounter: 7, HasPayload: true, PID: 1}})
assert.Len(t, ps, 0)
ps = b.dump()
Expand Down

0 comments on commit c44c340

Please sign in to comment.