1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-10 02:35:58 +01:00

Make packetcache.Get use a caller-allocated buffer.

This commit is contained in:
Juliusz Chroboczek 2020-05-20 20:32:30 +02:00
parent a6ff98a313
commit f641e263f1
3 changed files with 26 additions and 16 deletions

View file

@ -855,13 +855,14 @@ func sendNACK(pc *webrtc.PeerConnection, ssrc uint32, first uint16, bitmap uint1
func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) { func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) {
var packet rtp.Packet var packet rtp.Packet
buf := make([]byte, packetcache.BufSize)
for _, nack := range p.Nacks { for _, nack := range p.Nacks {
for _, seqno := range nack.PacketList() { for _, seqno := range nack.PacketList() {
raw := track.remote.cache.Get(seqno) l := track.remote.cache.Get(seqno, buf)
if raw == nil { if l == 0 {
continue continue
} }
err := packet.Unmarshal(raw) err := packet.Unmarshal(buf[:l])
if err != nil { if err != nil {
continue continue
} }
@ -870,7 +871,7 @@ func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) {
log.Printf("%v", err) log.Printf("%v", err)
continue continue
} }
track.rate.Add(uint32(len(raw))) track.rate.Add(uint32(l))
} }
} }
} }

View file

@ -115,7 +115,7 @@ func (cache *Cache) Expect(n int) {
cache.expected += uint32(n) cache.expected += uint32(n)
} }
func (cache *Cache) Get(seqno uint16) []byte { func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
cache.mu.Lock() cache.mu.Lock()
defer cache.mu.Unlock() defer cache.mu.Unlock()
@ -124,11 +124,12 @@ func (cache *Cache) Get(seqno uint16) []byte {
cache.entries[i].seqno != seqno { cache.entries[i].seqno != seqno {
continue continue
} }
buf := make([]byte, cache.entries[i].length) return uint16(copy(
copy(buf, cache.entries[i].buf[:]) result[:cache.entries[i].length],
return buf cache.entries[i].buf[:]),
)
} }
return nil return 0
} }
// Shift 17 bits out of the bitmap. Return a boolean indicating if any // Shift 17 bits out of the bitmap. Return a boolean indicating if any

View file

@ -23,13 +23,20 @@ func TestCache(t *testing.T) {
cache.Store(13, buf1) cache.Store(13, buf1)
cache.Store(17, buf2) cache.Store(17, buf2)
if bytes.Compare(cache.Get(13), buf1) != 0 { buf := make([]byte, BufSize)
l := cache.Get(13, buf)
if bytes.Compare(buf[:l], buf1) != 0 {
t.Errorf("Couldn't get 13") t.Errorf("Couldn't get 13")
} }
if bytes.Compare(cache.Get(17), buf2) != 0 {
l = cache.Get(17, buf)
if bytes.Compare(buf[:l], buf2) != 0 {
t.Errorf("Couldn't get 17") t.Errorf("Couldn't get 17")
} }
if cache.Get(42) != nil {
l = cache.Get(42, buf)
if l != 0 {
t.Errorf("Creation ex nihilo") t.Errorf("Creation ex nihilo")
} }
} }
@ -42,14 +49,15 @@ func TestCacheOverflow(t *testing.T) {
} }
for i := 0; i < 32; i++ { for i := 0; i < 32; i++ {
buf := cache.Get(uint16(i)) buf := make([]byte, BufSize)
l := cache.Get(uint16(i), buf)
if i < 16 { if i < 16 {
if buf != nil { if l > 0 {
t.Errorf("Creation ex nihilo: %v", i) t.Errorf("Creation ex nihilo: %v", i)
} }
} else { } else {
if len(buf) != 1 || buf[0] != uint8(i) { if l != 1 || buf[0] != uint8(i) {
t.Errorf("Expected [%v], got %v", i, buf) t.Errorf("Expected [%v], got %v", i, buf[:l])
} }
} }
} }