From 962c675ded496bb743d536c38d909ce3ae6ea78d Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 12 Oct 2020 10:45:59 +0200 Subject: [PATCH] Packetcache: implement KeyframeSeqno and Get(nil). --- packetcache/packetcache.go | 28 ++++++++++++++++++++++------ packetcache/packetcache_test.go | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packetcache/packetcache.go b/packetcache/packetcache.go index a21f2e6..309b07a 100644 --- a/packetcache/packetcache.go +++ b/packetcache/packetcache.go @@ -362,15 +362,21 @@ func get(seqno uint16, entries []entry, result []byte) (uint16, uint32, bool) { if entries[i].lengthAndMarker == 0 || entries[i].seqno != seqno { continue } - n := uint16(copy( - result[:entries[i].length()], - entries[i].buf[:])) + var n uint16 + if len(result) > 0 { + n = uint16(copy( + result[:entries[i].length()], + entries[i].buf[:])) + } else { + n = entries[i].length() + } return n, entries[i].timestamp, entries[i].marker() } return 0, 0, false } -// Get retrieves a packet from the cache. +// 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. func (cache *Cache) Get(seqno uint16, result []byte) uint16 { cache.mu.Lock() defer cache.mu.Unlock() @@ -394,8 +400,7 @@ func (cache *Cache) Last() (bool, uint16, uint32) { if !cache.lastValid { return false, 0, 0 } - buf := make([]byte, BufSize) - len, ts, _ := get(cache.last, cache.entries, buf) + len, ts, _ := get(cache.last, cache.entries, nil) if len == 0 { return false, 0, 0 } @@ -436,6 +441,17 @@ func (cache *Cache) Keyframe() (uint32, bool, []uint16) { return cache.keyframe.timestamp, cache.keyframe.complete, seqnos } +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 +} + func (cache *Cache) resize(capacity int) { if len(cache.entries) == capacity { return diff --git a/packetcache/packetcache_test.go b/packetcache/packetcache_test.go index c378370..7f87163 100644 --- a/packetcache/packetcache_test.go +++ b/packetcache/packetcache_test.go @@ -44,6 +44,10 @@ func TestCache(t *testing.T) { if !bytes.Equal(buf[:l], buf1) { t.Errorf("Couldn't get 13") } + l = cache.Get(13, nil) + if l != uint16(len(buf1)) { + t.Errorf("Couldn't retrieve length") + } l = cache.GetAt(13, i1, buf) if !bytes.Equal(buf[:l], buf1) { t.Errorf("Couldn't get 13 at %v", i1) @@ -170,6 +174,11 @@ func TestKeyframe(t *testing.T) { packet := make([]byte, 1) buf := make([]byte, BufSize) + found, _, _ := cache.KeyframeSeqno() + if found { + t.Errorf("Found keyframe in empty cache") + } + cache.Store(7, 57, true, false, packet) if cache.keyframe.complete { t.Errorf("Expected false, got true") @@ -183,6 +192,12 @@ func TestKeyframe(t *testing.T) { if ts != 57 || !c || len(kf) != 2 { t.Errorf("Got %v %v %v, expected %v %v", ts, c, len(kf), 57, 2) } + + found, seqno, ts := cache.KeyframeSeqno() + if !found || seqno != 7 || ts != 57 { + t.Errorf("Got %v %v %v, expected %v %v", found, seqno, ts, 7, 57) + } + for _, i := range kf { l := cache.Get(i, buf) if int(l) != len(packet) {