Commit 962c675d authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Packetcache: implement KeyframeSeqno and Get(nil).

parent 2a516674
......@@ -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
......
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment