Cleaning/simplifying some buffer bits (#398)

* Cleaning/simplifying some buffer bits

1. NACKs are always inserted in order. So, get rid of
bunch of out-of-order handling in there and simplify.
2. For now, removing triggering a key frame from NACKs.
Let subs drive it.
3. Move to 16-bit sequence numbers except for receiver
report handling. Simplify bits about unwrapping sequence
number on all packets.
4. Remove unused code.

* remove unused field
This commit is contained in:
Raja Subramanian
2022-02-04 08:43:32 +05:30
committed by GitHub
parent 6f6d55345b
commit 05c8a6b2fd
4 changed files with 222 additions and 346 deletions
+64 -62
View File
@@ -1,7 +1,7 @@
package buffer
import (
"sort"
"time"
"github.com/pion/rtcp"
)
@@ -10,97 +10,99 @@ const maxNackTimes = 3 // Max number of times a packet will be NACKed
const maxNackCache = 100 // Max NACK sn the sfu will keep reference
type nack struct {
sn uint32
nacked uint8
seqNum uint16
nacked uint8
lastNackTime time.Time
}
type NackQueue struct {
nacks []nack
kfSN uint32
nacks []*nack
rtt time.Duration
}
func NewNACKQueue() *NackQueue {
return &NackQueue{
nacks: make([]nack, 0, maxNackCache+1),
nacks: make([]*nack, 0, maxNackCache),
}
}
func (n *NackQueue) Remove(extSN uint32) {
i := sort.Search(len(n.nacks), func(i int) bool { return n.nacks[i].sn >= extSN })
if i >= len(n.nacks) || n.nacks[i].sn != extSN {
return
}
copy(n.nacks[i:], n.nacks[i+1:])
n.nacks = n.nacks[:len(n.nacks)-1]
func (n *NackQueue) SetRTT(rtt int) {
n.rtt = time.Duration(rtt) * time.Millisecond
}
func (n *NackQueue) Push(extSN uint32) {
i := sort.Search(len(n.nacks), func(i int) bool { return n.nacks[i].sn >= extSN })
if i < len(n.nacks) && n.nacks[i].sn == extSN {
return
}
func (n *NackQueue) Remove(sn uint16) {
for idx, nack := range n.nacks {
if nack.seqNum != sn {
continue
}
nck := nack{
sn: extSN,
nacked: 0,
}
if i == len(n.nacks) {
n.nacks = append(n.nacks, nck)
} else {
n.nacks = append(n.nacks[:i+1], n.nacks[i:]...)
n.nacks[i] = nck
}
if len(n.nacks) >= maxNackCache {
copy(n.nacks, n.nacks[1:])
copy(n.nacks[idx:], n.nacks[idx+1:])
n.nacks = n.nacks[:len(n.nacks)-1]
break
}
}
func (n *NackQueue) Pairs(headSN uint32) ([]rtcp.NackPair, bool) {
func (n *NackQueue) Push(sn uint16) {
// if at capacity, pop the first one
if len(n.nacks) == cap(n.nacks) {
copy(n.nacks[0:], n.nacks[1:])
n.nacks = n.nacks[:len(n.nacks)-1]
}
n.nacks = append(n.nacks, &nack{seqNum: sn, nacked: 0, lastNackTime: time.Now()})
}
func (n *NackQueue) Pairs() []rtcp.NackPair {
if len(n.nacks) == 0 {
return nil, false
return nil
}
i := 0
askKF := false
now := time.Now()
// set it far back to get the first pair
baseSN := n.nacks[0].seqNum - 17
snsToPurge := []uint16{}
isPairActive := false
var np rtcp.NackPair
var nps []rtcp.NackPair
lostIdx := -1
for _, nck := range n.nacks {
if nck.nacked >= maxNackTimes {
if nck.sn > n.kfSN {
n.kfSN = nck.sn
askKF = true
for _, nack := range n.nacks {
if nack.nacked >= maxNackTimes || now.Sub(nack.lastNackTime) < n.rtt {
if nack.nacked >= maxNackTimes {
snsToPurge = append(snsToPurge, nack.seqNum)
}
continue
}
if nck.sn >= headSN-2 {
n.nacks[i] = nck
i++
continue
}
n.nacks[i] = nack{
sn: nck.sn,
nacked: nck.nacked + 1,
}
i++
// first nackpair or need a new nackpair
if lostIdx < 0 || nck.sn > n.nacks[lostIdx].sn+16 {
if lostIdx >= 0 {
nack.nacked++
nack.lastNackTime = now
if (nack.seqNum - baseSN) > 16 {
// need a new nack pair
if isPairActive {
nps = append(nps, np)
isPairActive = false
}
np.PacketID = uint16(nck.sn)
baseSN = nack.seqNum
np.PacketID = nack.seqNum
np.LostPackets = 0
lostIdx = i - 1
continue
isPairActive = true
} else {
np.LostPackets |= 1 << (nack.seqNum - baseSN - 1)
}
np.LostPackets |= 1 << ((nck.sn) - n.nacks[lostIdx].sn - 1)
}
// append last nackpair
if lostIdx != -1 {
// add any left over
if isPairActive {
nps = append(nps, np)
}
n.nacks = n.nacks[:i]
return nps, askKF
for _, sn := range snsToPurge {
n.Remove(sn)
}
return nps
}