mirror of
https://github.com/jech/galene.git
synced 2024-11-22 08:35:57 +01:00
Packetcache: implement KeyframeSeqno and Get(nil).
This commit is contained in:
parent
2a516674f2
commit
962c675ded
2 changed files with 37 additions and 6 deletions
|
@ -362,15 +362,21 @@ func get(seqno uint16, entries []entry, result []byte) (uint16, uint32, bool) {
|
||||||
if entries[i].lengthAndMarker == 0 || entries[i].seqno != seqno {
|
if entries[i].lengthAndMarker == 0 || entries[i].seqno != seqno {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
n := uint16(copy(
|
var n uint16
|
||||||
result[:entries[i].length()],
|
if len(result) > 0 {
|
||||||
entries[i].buf[:]))
|
n = uint16(copy(
|
||||||
|
result[:entries[i].length()],
|
||||||
|
entries[i].buf[:]))
|
||||||
|
} else {
|
||||||
|
n = entries[i].length()
|
||||||
|
}
|
||||||
return n, entries[i].timestamp, entries[i].marker()
|
return n, entries[i].timestamp, entries[i].marker()
|
||||||
}
|
}
|
||||||
return 0, 0, false
|
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 {
|
func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
|
||||||
cache.mu.Lock()
|
cache.mu.Lock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.Unlock()
|
||||||
|
@ -394,8 +400,7 @@ func (cache *Cache) Last() (bool, uint16, uint32) {
|
||||||
if !cache.lastValid {
|
if !cache.lastValid {
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
buf := make([]byte, BufSize)
|
len, ts, _ := get(cache.last, cache.entries, nil)
|
||||||
len, ts, _ := get(cache.last, cache.entries, buf)
|
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
|
@ -436,6 +441,17 @@ func (cache *Cache) Keyframe() (uint32, bool, []uint16) {
|
||||||
return cache.keyframe.timestamp, cache.keyframe.complete, seqnos
|
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) {
|
func (cache *Cache) resize(capacity int) {
|
||||||
if len(cache.entries) == capacity {
|
if len(cache.entries) == capacity {
|
||||||
return
|
return
|
||||||
|
|
|
@ -44,6 +44,10 @@ func TestCache(t *testing.T) {
|
||||||
if !bytes.Equal(buf[:l], buf1) {
|
if !bytes.Equal(buf[:l], buf1) {
|
||||||
t.Errorf("Couldn't get 13")
|
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)
|
l = cache.GetAt(13, i1, buf)
|
||||||
if !bytes.Equal(buf[:l], buf1) {
|
if !bytes.Equal(buf[:l], buf1) {
|
||||||
t.Errorf("Couldn't get 13 at %v", i1)
|
t.Errorf("Couldn't get 13 at %v", i1)
|
||||||
|
@ -170,6 +174,11 @@ func TestKeyframe(t *testing.T) {
|
||||||
packet := make([]byte, 1)
|
packet := make([]byte, 1)
|
||||||
buf := make([]byte, BufSize)
|
buf := make([]byte, BufSize)
|
||||||
|
|
||||||
|
found, _, _ := cache.KeyframeSeqno()
|
||||||
|
if found {
|
||||||
|
t.Errorf("Found keyframe in empty cache")
|
||||||
|
}
|
||||||
|
|
||||||
cache.Store(7, 57, true, false, packet)
|
cache.Store(7, 57, true, false, packet)
|
||||||
if cache.keyframe.complete {
|
if cache.keyframe.complete {
|
||||||
t.Errorf("Expected false, got true")
|
t.Errorf("Expected false, got true")
|
||||||
|
@ -183,6 +192,12 @@ func TestKeyframe(t *testing.T) {
|
||||||
if ts != 57 || !c || len(kf) != 2 {
|
if ts != 57 || !c || len(kf) != 2 {
|
||||||
t.Errorf("Got %v %v %v, expected %v %v", ts, c, len(kf), 57, 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 {
|
for _, i := range kf {
|
||||||
l := cache.Get(i, buf)
|
l := cache.Get(i, buf)
|
||||||
if int(l) != len(packet) {
|
if int(l) != len(packet) {
|
||||||
|
|
Loading…
Reference in a new issue