diff --git a/client.go b/client.go index 10dd469..a472455 100644 --- a/client.go +++ b/client.go @@ -855,13 +855,14 @@ func sendNACK(pc *webrtc.PeerConnection, ssrc uint32, first uint16, bitmap uint1 func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) { var packet rtp.Packet + buf := make([]byte, packetcache.BufSize) for _, nack := range p.Nacks { for _, seqno := range nack.PacketList() { - raw := track.remote.cache.Get(seqno) - if raw == nil { + l := track.remote.cache.Get(seqno, buf) + if l == 0 { continue } - err := packet.Unmarshal(raw) + err := packet.Unmarshal(buf[:l]) if err != nil { continue } @@ -870,7 +871,7 @@ func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) { log.Printf("%v", err) continue } - track.rate.Add(uint32(len(raw))) + track.rate.Add(uint32(l)) } } } diff --git a/packetcache/packetcache.go b/packetcache/packetcache.go index 4be0beb..e52b9e9 100644 --- a/packetcache/packetcache.go +++ b/packetcache/packetcache.go @@ -115,7 +115,7 @@ func (cache *Cache) Expect(n int) { cache.expected += uint32(n) } -func (cache *Cache) Get(seqno uint16) []byte { +func (cache *Cache) Get(seqno uint16, result []byte) uint16 { cache.mu.Lock() defer cache.mu.Unlock() @@ -124,11 +124,12 @@ func (cache *Cache) Get(seqno uint16) []byte { cache.entries[i].seqno != seqno { continue } - buf := make([]byte, cache.entries[i].length) - copy(buf, cache.entries[i].buf[:]) - return buf + return uint16(copy( + result[:cache.entries[i].length], + cache.entries[i].buf[:]), + ) } - return nil + return 0 } // Shift 17 bits out of the bitmap. Return a boolean indicating if any diff --git a/packetcache/packetcache_test.go b/packetcache/packetcache_test.go index cef3a11..1163906 100644 --- a/packetcache/packetcache_test.go +++ b/packetcache/packetcache_test.go @@ -23,13 +23,20 @@ func TestCache(t *testing.T) { cache.Store(13, buf1) 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") } - 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") } - if cache.Get(42) != nil { + + l = cache.Get(42, buf) + if l != 0 { t.Errorf("Creation ex nihilo") } } @@ -42,14 +49,15 @@ func TestCacheOverflow(t *testing.T) { } 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 buf != nil { + if l > 0 { t.Errorf("Creation ex nihilo: %v", i) } } else { - if len(buf) != 1 || buf[0] != uint8(i) { - t.Errorf("Expected [%v], got %v", i, buf) + if l != 1 || buf[0] != uint8(i) { + t.Errorf("Expected [%v], got %v", i, buf[:l]) } } }