1
Fork 0

Rework sending of NACKs.

This commit is contained in:
Juliusz Chroboczek 2020-05-02 22:26:09 +02:00
parent 98a1776ce7
commit 92de141b97
3 changed files with 36 additions and 24 deletions

View File

@ -349,9 +349,9 @@ func upLoop(conn *upConnection, track *upTrack) {
first := track.cache.Store(packet.SequenceNumber, buf[:bytes]) first := track.cache.Store(packet.SequenceNumber, buf[:bytes])
if packet.SequenceNumber-first > 24 { if packet.SequenceNumber-first > 24 {
first, bitmap := track.cache.BitmapGet() found, first, bitmap := track.cache.BitmapGet()
if bitmap != ^uint16(0) { if found {
err := conn.sendNACK(track, first, ^bitmap) err := conn.sendNACK(track, first, bitmap)
if err != nil { if err != nil {
log.Printf("%v", err) log.Printf("%v", err)
} }

View File

@ -59,16 +59,6 @@ func (cache *Cache) set(seqno uint16) {
return return
} }
if seqno == cache.first {
cache.bitmap >>= 1
cache.first += 1
for (cache.bitmap & 1) == 1 {
cache.bitmap >>= 1
cache.first += 1
}
return
}
if seqno-cache.first < 32 { if seqno-cache.first < 32 {
cache.bitmap |= (1 << uint16(seqno-cache.first)) cache.bitmap |= (1 << uint16(seqno-cache.first))
return return
@ -140,16 +130,28 @@ func (cache *Cache) Get(seqno uint16) []byte {
return nil return nil
} }
// Shift 17 bits out of the bitmap, return first index and remaining 16. // Shift 17 bits out of the bitmap. Return a boolean indicating if any
func (cache *Cache) BitmapGet() (uint16, uint16) { // were 0, the index of the first 0 bit, and a bitmap indicating any
// 0 bits after the first one.
func (cache *Cache) BitmapGet() (bool, uint16, uint16) {
cache.mu.Lock() cache.mu.Lock()
defer cache.mu.Unlock() defer cache.mu.Unlock()
first := cache.first first := cache.first
bitmap := uint16((cache.bitmap >> 1) & 0xFFFF) bitmap := (^cache.bitmap) & 0x1FFFF
cache.bitmap >>= 17 cache.bitmap >>= 17
cache.first += 17 cache.first += 17
return first, bitmap
if bitmap == 0 {
return false, first, 0
}
for bitmap & 1 == 0 {
bitmap >>= 1
first++
}
return true, first, uint16(bitmap >> 1)
} }
func (cache *Cache) GetStats(reset bool) (uint32, uint32, uint32, uint32) { func (cache *Cache) GetStats(reset bool) (uint32, uint32, uint32, uint32) {

View File

@ -109,10 +109,13 @@ func TestBitmapGet(t *testing.T) {
pos := uint16(42) pos := uint16(42)
for cache.bitmap != 0 { for cache.bitmap != 0 {
first, bitmap := cache.BitmapGet() found, first, bitmap := cache.BitmapGet()
if first < pos || first >= pos+64 { if first < pos || first >= pos+64 {
t.Errorf("First is %v, pos is %v", first, pos) t.Errorf("First is %v, pos is %v", first, pos)
} }
if !found {
t.Fatalf("Didn't find any 0 bits")
}
value >>= (first - pos) value >>= (first - pos)
pos = first pos = first
if (value & 1) != 0 { if (value & 1) != 0 {
@ -120,11 +123,14 @@ func TestBitmapGet(t *testing.T) {
} }
value >>= 1 value >>= 1
pos += 1 pos += 1
if bitmap != uint16(value&0xFFFF) { for bitmap != 0 {
t.Errorf("Got %b, expected %b", bitmap, (value & 0xFFFF)) if uint8(bitmap & 1) == uint8(value & 1) {
t.Errorf("Bitmap mismatch")
}
bitmap >>= 1
value >>= 1
pos += 1
} }
value >>= 16
pos += 16
} }
if value != 0 { if value != 0 {
t.Errorf("Value is %v", value) t.Errorf("Value is %v", value)
@ -143,9 +149,13 @@ func TestBitmapPacket(t *testing.T) {
} }
} }
first, bitmap := cache.BitmapGet() found, first, bitmap := cache.BitmapGet()
p := rtcp.NackPair{first, rtcp.PacketBitmap(^bitmap)} if !found {
t.Fatalf("Didn't find any 0 bits")
}
p := rtcp.NackPair{first, rtcp.PacketBitmap(bitmap)}
pl := p.PacketList() pl := p.PacketList()
for _, s := range pl { for _, s := range pl {