11// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
22// SPDX-License-Identifier: MIT
33
4- package nack
4+ package rtpbuffer
55
66import (
77 "encoding/binary"
@@ -11,16 +11,22 @@ import (
1111 "github.com/pion/rtp"
1212)
1313
14- const maxPayloadLen = 1460
14+ // PacketFactory allows custom logic around the handle of RTP Packets before they added to the RTPBuffer.
15+ // The NoOpPacketFactory doesn't copy packets, while the RetainablePacket will take a copy before adding
16+ type PacketFactory interface {
17+ NewPacket (header * rtp.Header , payload []byte , rtxSsrc uint32 , rtxPayloadType uint8 ) (* RetainablePacket , error )
18+ }
1519
16- type packetManager struct {
20+ // PacketFactoryCopy is PacketFactory that takes a copy of packets when added to the RTPBuffer
21+ type PacketFactoryCopy struct {
1722 headerPool * sync.Pool
1823 payloadPool * sync.Pool
1924 rtxSequencer rtp.Sequencer
2025}
2126
22- func newPacketManager () * packetManager {
23- return & packetManager {
27+ // NewPacketFactoryCopy constructs a PacketFactory that takes a copy of packets when added to the RTPBuffer
28+ func NewPacketFactoryCopy () * PacketFactoryCopy {
29+ return & PacketFactoryCopy {
2430 headerPool : & sync.Pool {
2531 New : func () interface {} {
2632 return & rtp.Header {}
@@ -36,12 +42,13 @@ func newPacketManager() *packetManager {
3642 }
3743}
3844
39- func (m * packetManager ) NewPacket (header * rtp.Header , payload []byte , rtxSsrc uint32 , rtxPayloadType uint8 ) (* retainablePacket , error ) {
45+ // NewPacket constructs a new RetainablePacket that can be added to the RTPBuffer
46+ func (m * PacketFactoryCopy ) NewPacket (header * rtp.Header , payload []byte , rtxSsrc uint32 , rtxPayloadType uint8 ) (* RetainablePacket , error ) {
4047 if len (payload ) > maxPayloadLen {
4148 return nil , io .ErrShortBuffer
4249 }
4350
44- p := & retainablePacket {
51+ p := & RetainablePacket {
4552 onRelease : m .releasePacket ,
4653 sequenceNumber : header .SequenceNumber ,
4754 // new packets have retain count of 1
@@ -92,17 +99,19 @@ func (m *packetManager) NewPacket(header *rtp.Header, payload []byte, rtxSsrc ui
9299 return p , nil
93100}
94101
95- func (m * packetManager ) releasePacket (header * rtp.Header , payload * []byte ) {
102+ func (m * PacketFactoryCopy ) releasePacket (header * rtp.Header , payload * []byte ) {
96103 m .headerPool .Put (header )
97104 if payload != nil {
98105 m .payloadPool .Put (payload )
99106 }
100107}
101108
102- type noOpPacketFactory struct {}
109+ // PacketFactoryNoOp is a PacketFactory implementation that doesn't copy packets
110+ type PacketFactoryNoOp struct {}
103111
104- func (f * noOpPacketFactory ) NewPacket (header * rtp.Header , payload []byte , _ uint32 , _ uint8 ) (* retainablePacket , error ) {
105- return & retainablePacket {
112+ // NewPacket constructs a new RetainablePacket that can be added to the RTPBuffer
113+ func (f * PacketFactoryNoOp ) NewPacket (header * rtp.Header , payload []byte , _ uint32 , _ uint8 ) (* RetainablePacket , error ) {
114+ return & RetainablePacket {
106115 onRelease : f .releasePacket ,
107116 count : 1 ,
108117 header : header ,
@@ -111,52 +120,6 @@ func (f *noOpPacketFactory) NewPacket(header *rtp.Header, payload []byte, _ uint
111120 }, nil
112121}
113122
114- func (f * noOpPacketFactory ) releasePacket (_ * rtp.Header , _ * []byte ) {
123+ func (f * PacketFactoryNoOp ) releasePacket (_ * rtp.Header , _ * []byte ) {
115124 // no-op
116125}
117-
118- type retainablePacket struct {
119- onRelease func (* rtp.Header , * []byte )
120-
121- countMu sync.Mutex
122- count int
123-
124- header * rtp.Header
125- buffer * []byte
126- payload []byte
127-
128- sequenceNumber uint16
129- }
130-
131- func (p * retainablePacket ) Header () * rtp.Header {
132- return p .header
133- }
134-
135- func (p * retainablePacket ) Payload () []byte {
136- return p .payload
137- }
138-
139- func (p * retainablePacket ) Retain () error {
140- p .countMu .Lock ()
141- defer p .countMu .Unlock ()
142- if p .count == 0 {
143- // already released
144- return errPacketReleased
145- }
146- p .count ++
147- return nil
148- }
149-
150- func (p * retainablePacket ) Release () {
151- p .countMu .Lock ()
152- defer p .countMu .Unlock ()
153- p .count --
154-
155- if p .count == 0 {
156- // release back to pool
157- p .onRelease (p .header , p .buffer )
158- p .header = nil
159- p .buffer = nil
160- p .payload = nil
161- }
162- }
0 commit comments