1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00
galene/packetcache/packetcache.go

547 lines
12 KiB
Go
Raw Normal View History

2020-10-08 18:46:52 +02:00
// Package packetcache implement a packet cache that maintains a history
// of recently seen packets, the last keyframe, and a number of statistics
// that are needed for sending receiver reports.
package packetcache
import (
"math/bits"
"sync"
)
2020-10-08 18:46:52 +02:00
// The maximum size of packets stored in the cache. Chosen to be
// a multiple of 8.
const BufSize = 1504
// The maximum number of packets that constitute a keyframe.
2020-10-04 17:08:42 +02:00
const maxFrame = 1024
2020-10-08 18:46:52 +02:00
// entry represents a cached packet.
type entry struct {
2020-10-08 17:58:58 +02:00
seqno uint16
2020-10-08 18:46:52 +02:00
lengthAndMarker uint16 // 1 bit of marker, 15 bits of length
2020-10-08 17:58:58 +02:00
timestamp uint32
buf [BufSize]byte
}
func (e *entry) length() uint16 {
return e.lengthAndMarker & 0x7FFF
}
func (e *entry) marker() bool {
return (e.lengthAndMarker & 0x8000) != 0
}
2020-10-08 18:46:52 +02:00
// bitmap keeps track of recent loss history
2020-10-04 17:08:42 +02:00
type bitmap struct {
valid bool
first uint16
bitmap uint32
}
2020-10-08 18:46:52 +02:00
// frame is used for storing the last keyframe
2020-10-04 17:08:42 +02:00
type frame struct {
timestamp uint32
2020-10-08 17:58:58 +02:00
complete bool
2020-10-04 17:08:42 +02:00
entries []entry
}
type Cache struct {
mu sync.Mutex
//stats
last uint16
cycle uint16
lastValid bool
expected uint32
lost uint32
totalLost uint32
// bitmap
2020-10-04 17:08:42 +02:00
bitmap bitmap
2020-10-03 12:54:17 +02:00
// buffered keyframe
2020-10-04 17:08:42 +02:00
keyframe frame
// the actual cache
2020-05-20 20:37:25 +02:00
tail uint16
entries []entry
}
2020-10-08 18:46:52 +02:00
// New creates a cache with the given capacity.
func New(capacity int) *Cache {
2020-05-20 20:37:25 +02:00
if capacity > int(^uint16(0)) {
return nil
}
return &Cache{
entries: make([]entry, capacity),
}
}
2020-10-08 18:46:52 +02:00
// compare performs comparison modulo 2^16.
2020-10-08 17:58:58 +02:00
func compare(s1, s2 uint16) int {
if s1 == s2 {
return 0
}
if ((s2 - s1) & 0x8000) != 0 {
return 1
}
return -1
}
2020-10-08 18:46:52 +02:00
// seqnoInvalid returns true if seqno is unreasonably far in the past
func seqnoInvalid(seqno, reference uint16) bool {
2020-10-08 17:58:58 +02:00
if compare(reference, seqno) < 0 {
return false
}
2020-05-20 19:36:33 +02:00
if reference-seqno > 0x100 {
return true
}
return false
}
2020-10-04 17:08:42 +02:00
// set sets a bit in the bitmap, shifting if necessary
func (bitmap *bitmap) set(seqno uint16) {
if !bitmap.valid || seqnoInvalid(seqno, bitmap.first) {
bitmap.first = seqno
bitmap.bitmap = 1
bitmap.valid = true
return
}
2020-10-08 17:58:58 +02:00
if compare(bitmap.first, seqno) > 0 {
return
}
2020-10-04 17:08:42 +02:00
if seqno-bitmap.first >= 32 {
shift := seqno - bitmap.first - 31
bitmap.bitmap >>= shift
bitmap.first += shift
}
2020-10-04 17:08:42 +02:00
if (bitmap.bitmap & 1) == 1 {
ones := bits.TrailingZeros32(^bitmap.bitmap)
bitmap.bitmap >>= ones
bitmap.first += uint16(ones)
}
2020-10-04 17:08:42 +02:00
bitmap.bitmap |= (1 << uint16(seqno-bitmap.first))
return
}
2020-10-04 17:08:42 +02:00
// BitmapGet shifts up to 17 bits out of the bitmap. It returns a boolean
// indicating if any were 0, the index of the first 0 bit, and a bitmap
// indicating any 0 bits after the first one.
func (cache *Cache) BitmapGet(next uint16) (bool, uint16, uint16) {
cache.mu.Lock()
defer cache.mu.Unlock()
return cache.bitmap.get(next)
}
func (bitmap *bitmap) get(next uint16) (bool, uint16, uint16) {
first := bitmap.first
2020-10-08 17:58:58 +02:00
if compare(first, next) >= 0 {
2020-10-04 17:08:42 +02:00
return false, first, 0
}
2020-10-08 17:58:58 +02:00
count := next - first
2020-10-04 17:08:42 +02:00
if count > 17 {
count = 17
}
bm := (^bitmap.bitmap) & ^((^uint32(0)) << count)
bitmap.bitmap >>= count
bitmap.first += count
if bm == 0 {
return false, first, 0
}
if (bm & 1) == 0 {
count := bits.TrailingZeros32(bm)
bm >>= count
first += uint16(count)
}
return true, first, uint16(bm >> 1)
}
2020-10-08 18:46:52 +02:00
// insert inserts a packet into a frame.
2020-10-08 17:58:58 +02:00
func (frame *frame) insert(seqno uint16, timestamp uint32, marker bool, data []byte) bool {
n := len(frame.entries)
i := 0
if n == 0 || seqno > frame.entries[n-1].seqno {
// fast path
i = n
} else {
for i < n {
if frame.entries[i].seqno >= seqno {
break
}
2020-10-08 17:58:58 +02:00
i++
2020-10-04 17:08:42 +02:00
}
2020-10-08 17:58:58 +02:00
if i < n && frame.entries[i].seqno == seqno {
// duplicate
return false
2020-10-04 17:08:42 +02:00
}
}
2020-10-08 17:58:58 +02:00
if n >= maxFrame {
2020-10-04 17:08:42 +02:00
// overflow
2020-10-08 17:58:58 +02:00
return false
2020-10-04 17:08:42 +02:00
}
2020-10-08 17:58:58 +02:00
lam := uint16(len(data))
if marker {
lam |= 0x8000
}
2020-10-04 17:08:42 +02:00
e := entry{
2020-10-08 17:58:58 +02:00
seqno: seqno,
lengthAndMarker: lam,
timestamp: timestamp,
2020-10-04 17:08:42 +02:00
}
copy(e.buf[:], data)
2020-10-08 17:58:58 +02:00
if i >= n {
2020-10-04 17:08:42 +02:00
frame.entries = append(frame.entries, e)
2020-10-08 17:58:58 +02:00
return true
2020-10-04 17:08:42 +02:00
}
frame.entries = append(frame.entries, entry{})
copy(frame.entries[i+1:], frame.entries[i:])
frame.entries[i] = e
2020-10-08 17:58:58 +02:00
return true
}
2020-10-08 18:46:52 +02:00
// store checks whether a packet is part of the current keyframe and, if
// so, inserts it.
2020-10-08 17:58:58 +02:00
func (frame *frame) store(seqno uint16, timestamp uint32, first bool, marker bool, data []byte) bool {
if first {
if frame.timestamp != timestamp {
frame.timestamp = timestamp
frame.complete = false
frame.entries = frame.entries[:0]
}
} else if len(frame.entries) > 0 {
if frame.timestamp != timestamp {
delta := seqno - frame.entries[0].seqno
if (delta&0x8000) == 0 && delta > 0x4000 {
frame.complete = false
frame.entries = frame.entries[:0]
}
return false
}
} else {
return false
}
done := frame.insert(seqno, timestamp, marker, data)
if done && !frame.complete {
marker := false
fst := frame.entries[0].seqno
for i := 1; i < len(frame.entries); i++ {
if frame.entries[i].seqno != fst+uint16(i) {
return done
}
if frame.entries[i].marker() {
marker = true
}
}
if marker {
frame.complete = true
}
}
return done
2020-10-04 17:08:42 +02:00
}
2020-10-08 18:46:52 +02:00
// Store stores a packet in the cache. It returns the first seqno in the
// bitmap, and the index at which the packet was stored.
2020-10-08 17:58:58 +02:00
func (cache *Cache) Store(seqno uint16, timestamp uint32, keyframe bool, marker bool, buf []byte) (uint16, uint16) {
cache.mu.Lock()
defer cache.mu.Unlock()
if !cache.lastValid || seqnoInvalid(seqno, cache.last) {
cache.last = seqno
cache.lastValid = true
cache.expected++
} else {
2020-10-08 17:58:58 +02:00
if compare(cache.last, seqno) <= 0 {
cache.expected += uint32(seqno - cache.last)
cache.lost += uint32(seqno - cache.last - 1)
if seqno < cache.last {
cache.cycle++
}
cache.last = seqno
} else {
if cache.lost > 0 {
cache.lost--
}
}
}
2020-10-04 17:08:42 +02:00
cache.bitmap.set(seqno)
2020-10-08 17:58:58 +02:00
done := cache.keyframe.store(seqno, timestamp, keyframe, marker, buf)
if done && !cache.keyframe.complete {
completeKeyframe(cache)
}
2020-10-03 12:54:17 +02:00
2020-05-20 20:37:25 +02:00
i := cache.tail
cache.entries[i].seqno = seqno
copy(cache.entries[i].buf[:], buf)
2020-10-08 17:58:58 +02:00
lam := uint16(len(buf))
if marker {
lam |= 0x8000
}
cache.entries[i].lengthAndMarker = lam
cache.entries[i].timestamp = timestamp
2020-05-20 20:37:25 +02:00
cache.tail = (i + 1) % uint16(len(cache.entries))
2020-10-04 17:08:42 +02:00
return cache.bitmap.first, i
}
2020-10-08 18:46:52 +02:00
// completeKeyFrame attempts to complete the current keyframe.
2020-10-08 17:58:58 +02:00
func completeKeyframe(cache *Cache) {
l := len(cache.keyframe.entries)
if l == 0 {
return
}
first := cache.keyframe.entries[0].seqno
last := cache.keyframe.entries[l-1].seqno
count := (last - first) // may wrap around
if count > 0x4000 {
// this shouldn't happen
return
}
var buf []byte
if count > 1 {
if buf == nil {
buf = make([]byte, BufSize)
}
for i := uint16(1); i < count; i++ {
n, ts, marker := get(first+i, cache.entries, buf)
if n > 0 {
cache.keyframe.store(
first+i, ts, false, marker, buf,
)
}
}
}
if !cache.keyframe.complete {
// Try to find packets after the last one.
for {
l := len(cache.keyframe.entries)
if cache.keyframe.entries[l-1].marker() {
break
}
if buf == nil {
buf = make([]byte, BufSize)
}
seqno := cache.keyframe.entries[l-1].seqno + 1
n, ts, marker := get(seqno, cache.entries, buf)
if n <= 0 {
break
}
done := cache.keyframe.store(
seqno, ts, false, marker, buf,
)
if !done || marker {
break
}
}
}
}
2020-10-08 18:46:52 +02:00
// Expect records that we expect n packets. It is used for loss statistics.
func (cache *Cache) Expect(n int) {
if n <= 0 {
return
}
cache.mu.Lock()
defer cache.mu.Unlock()
cache.expected += uint32(n)
}
2020-10-08 18:46:52 +02:00
// get retrieves a packet from a slice of entries.
2020-10-08 17:58:58 +02:00
func get(seqno uint16, entries []entry, result []byte) (uint16, uint32, bool) {
2020-10-03 12:54:17 +02:00
for i := range entries {
2020-10-08 17:58:58 +02:00
if entries[i].lengthAndMarker == 0 || entries[i].seqno != seqno {
continue
}
var n uint16
if len(result) > 0 {
n = uint16(copy(
result[:entries[i].length()],
entries[i].buf[:]))
} else {
n = entries[i].length()
}
2020-10-08 17:58:58 +02:00
return n, entries[i].timestamp, entries[i].marker()
}
2020-10-08 17:58:58 +02:00
return 0, 0, false
}
// Get retrieves a packet from the cache, returns the number of bytes
// copied. If result is of length 0, returns the size of the packet.
2020-10-03 12:54:17 +02:00
func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
cache.mu.Lock()
defer cache.mu.Unlock()
2020-10-08 17:58:58 +02:00
n, _, _ := get(seqno, cache.keyframe.entries, result)
2020-10-03 12:54:17 +02:00
if n > 0 {
return n
}
2020-10-08 17:58:58 +02:00
n, _, _ = get(seqno, cache.entries, result)
2020-10-03 12:54:17 +02:00
if n > 0 {
return n
}
return 0
}
2020-10-11 22:08:03 +02:00
func (cache *Cache) Last() (bool, uint16, uint32) {
cache.mu.Lock()
defer cache.mu.Unlock()
if !cache.lastValid {
return false, 0, 0
}
len, ts, _ := get(cache.last, cache.entries, nil)
2020-10-11 22:08:03 +02:00
if len == 0 {
return false, 0, 0
}
return true, cache.last, ts
}
2020-10-08 18:46:52 +02:00
// GetAt retrieves a packet from the cache assuming it is at the given index.
2020-05-20 20:37:25 +02:00
func (cache *Cache) GetAt(seqno uint16, index uint16, result []byte) uint16 {
cache.mu.Lock()
defer cache.mu.Unlock()
2021-04-12 17:02:43 +02:00
if int(index) >= len(cache.entries) {
2020-06-04 00:16:21 +02:00
return 0
}
2020-05-20 20:37:25 +02:00
if cache.entries[index].seqno != seqno {
return 0
}
return uint16(copy(
2020-10-08 17:58:58 +02:00
result[:cache.entries[index].length()],
2020-05-20 20:37:25 +02:00
cache.entries[index].buf[:]),
)
}
2020-10-08 18:46:52 +02:00
// Keyframe returns the last buffered keyframe. It returns the frame's
// timestamp and a boolean indicating if the frame is complete.
2020-10-08 17:58:58 +02:00
func (cache *Cache) Keyframe() (uint32, bool, []uint16) {
2020-10-03 12:54:17 +02:00
cache.mu.Lock()
defer cache.mu.Unlock()
if len(cache.keyframe.entries) == 0 {
2020-10-08 17:58:58 +02:00
return 0, false, nil
}
2020-10-04 17:08:42 +02:00
seqnos := make([]uint16, len(cache.keyframe.entries))
for i := range cache.keyframe.entries {
seqnos[i] = cache.keyframe.entries[i].seqno
2020-10-03 12:54:17 +02:00
}
2020-10-08 17:58:58 +02:00
return cache.keyframe.timestamp, cache.keyframe.complete, seqnos
2020-10-03 12:54:17 +02:00
}
func (cache *Cache) KeyframeSeqno() (bool, uint16, uint32) {
cache.mu.Lock()
defer cache.mu.Unlock()
if len(cache.keyframe.entries) == 0 {
return false, 0, 0
}
return true, cache.keyframe.entries[0].seqno, cache.keyframe.timestamp
}
2020-06-04 00:16:21 +02:00
func (cache *Cache) resize(capacity int) {
if len(cache.entries) == capacity {
return
}
entries := make([]entry, capacity)
if capacity > len(cache.entries) {
copy(entries, cache.entries[:cache.tail])
copy(entries[int(cache.tail)+capacity-len(cache.entries):],
cache.entries[cache.tail:])
} else if capacity > int(cache.tail) {
copy(entries, cache.entries[:cache.tail])
copy(entries[cache.tail:],
cache.entries[int(cache.tail)+
len(cache.entries)-capacity:])
} else {
// too bad, invalidate all indices
copy(entries,
cache.entries[int(cache.tail)-capacity:cache.tail])
cache.tail = 0
}
cache.entries = entries
}
2020-10-08 18:46:52 +02:00
// Resize resizes the cache to the given capacity. This might invalidate
// indices of recently stored packets.
2020-06-04 00:16:21 +02:00
func (cache *Cache) Resize(capacity int) {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.resize(capacity)
}
2020-10-08 18:46:52 +02:00
// ResizeCond is like Resize, but avoids invalidating recent indices.
2020-06-04 00:16:21 +02:00
func (cache *Cache) ResizeCond(capacity int) bool {
cache.mu.Lock()
defer cache.mu.Unlock()
current := len(cache.entries)
if current >= capacity*3/4 && current < capacity*2 {
2020-06-04 00:16:21 +02:00
return false
}
if capacity < current {
if int(cache.tail) > capacity {
// this would invalidate too many indices
return false
}
2020-06-04 00:16:21 +02:00
}
cache.resize(capacity)
return true
}
2020-10-08 18:46:52 +02:00
// GetStats returns statistics about received packets. If reset is true,
// the statistics are reset.
func (cache *Cache) GetStats(reset bool) (uint32, uint32, uint32, uint32) {
cache.mu.Lock()
defer cache.mu.Unlock()
expected := cache.expected
lost := cache.lost
totalLost := cache.totalLost + cache.lost
eseqno := uint32(cache.cycle)<<16 | uint32(cache.last)
if reset {
cache.expected = 0
cache.totalLost += cache.lost
cache.lost = 0
}
return expected, lost, totalLost, eseqno
}
2020-10-12 14:35:35 +02:00
// ToBitmap takes a non-empty sorted list of seqnos, and computes a bitmap
// covering a prefix of the list. It returns the part of the list that
// couldn't be covered.
func ToBitmap(seqnos []uint16) (first uint16, bitmap uint16, remain []uint16) {
first = seqnos[0]
bitmap = uint16(0)
remain = seqnos[1:]
for len(remain) > 0 {
delta := remain[0] - first - 1
if delta >= 16 {
break
}
bitmap = bitmap | (1 << delta)
remain = remain[1:]
}
return
}