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

393 lines
8.6 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
}
type Cache struct {
mu sync.Mutex
//stats
last uint16
cycle uint16
lastValid bool
expected uint32
totalExpected uint32
received uint32
totalReceived uint32
// last seen keyframe
keyframe uint16
keyframeValid bool
// bitmap
2020-10-04 17:08:42 +02:00
bitmap bitmap
// 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
// 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++
cache.received++
} else {
cmp := compare(cache.last, seqno)
if cmp < 0 {
cache.received++
cache.expected += uint32(seqno - cache.last)
if seqno < cache.last {
cache.cycle++
}
cache.last = seqno
if cache.keyframeValid &&
compare(cache.keyframe, seqno) > 0 {
cache.keyframeValid = false
}
} else if cmp > 0 {
if cache.received < cache.expected {
cache.received++
}
}
}
2020-10-04 17:08:42 +02:00
cache.bitmap.set(seqno)
if keyframe {
cache.keyframe = seqno
cache.keyframeValid = true
2020-10-08 17:58:58 +02:00
}
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
}
// Expect records that we expect n additional packets.
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()
n, _, _ := get(seqno, cache.entries, result)
2020-10-03 12:54:17 +02:00
if n > 0 {
return n
}
return 0
}
func (cache *Cache) Last() (uint16, bool) {
2020-10-11 22:08:03 +02:00
cache.mu.Lock()
defer cache.mu.Unlock()
if !cache.lastValid {
return 0, false
2020-10-11 22:08:03 +02:00
}
return cache.last, true
2020-10-11 22:08:03 +02:00
}
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[:]),
)
}
// Keyframe returns the seqno of the last seen keyframe
func (cache *Cache) Keyframe() (uint16, bool) {
cache.mu.Lock()
defer cache.mu.Unlock()
if !cache.keyframeValid {
return 0, false
}
return cache.keyframe, true
}
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
}
// Stats contains cache statistics
type Stats struct {
Received, TotalReceived uint32
Expected, TotalExpected uint32
ESeqno uint32
}
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) Stats {
cache.mu.Lock()
defer cache.mu.Unlock()
s := Stats{
Received: cache.received,
TotalReceived: cache.totalReceived + cache.received,
Expected: cache.expected,
TotalExpected: cache.totalExpected + cache.expected,
ESeqno: uint32(cache.cycle)<<16 | uint32(cache.last),
}
if reset {
cache.totalExpected += cache.expected
cache.expected = 0
cache.totalReceived += cache.received
cache.received = 0
}
return s
}
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
}