-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NACK interceptor sendBuffer overridden
Keeps a copy of packet in responder_interceptor, fixes #84
- Loading branch information
Showing
6 changed files
with
194 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ aler9 <[email protected]> | |
Antoine Baché <[email protected]> | ||
Atsushi Watanabe <[email protected]> | ||
boks1971 <[email protected]> | ||
David Zhao <[email protected]> | ||
Jonathan Müller <[email protected]> | ||
Mathis Engelbart <[email protected]> | ||
Sean DuBois <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package nack | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/pion/rtp" | ||
) | ||
|
||
const maxPayloadLen = 1460 | ||
|
||
type packetManager struct { | ||
headerPool *sync.Pool | ||
payloadPool *sync.Pool | ||
} | ||
|
||
func newPacketManager() *packetManager { | ||
return &packetManager{ | ||
headerPool: &sync.Pool{ | ||
New: func() interface{} { | ||
return &rtp.Header{} | ||
}, | ||
}, | ||
payloadPool: &sync.Pool{ | ||
New: func() interface{} { | ||
buf := make([]byte, maxPayloadLen) | ||
return &buf | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (m *packetManager) NewPacket(header *rtp.Header, payload []byte) (*retainablePacket, error) { | ||
if len(payload) > maxPayloadLen { | ||
return nil, io.ErrShortBuffer | ||
} | ||
|
||
p := &retainablePacket{ | ||
onRelease: m.releasePacket, | ||
// new packets have retain count of 1 | ||
count: 1, | ||
} | ||
|
||
p.header = m.headerPool.Get().(*rtp.Header) | ||
*p.header = *header | ||
|
||
if payload != nil { | ||
p.buffer = m.payloadPool.Get().(*[]byte) | ||
size := copy(*p.buffer, payload) | ||
p.payload = (*p.buffer)[:size] | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
func (m *packetManager) releasePacket(header *rtp.Header, payload *[]byte) { | ||
m.headerPool.Put(header) | ||
if payload != nil { | ||
m.payloadPool.Put(payload) | ||
} | ||
} | ||
|
||
type retainablePacket struct { | ||
onRelease func(*rtp.Header, *[]byte) | ||
|
||
countMu sync.Mutex | ||
count int | ||
|
||
header *rtp.Header | ||
buffer *[]byte | ||
payload []byte | ||
} | ||
|
||
func (p *retainablePacket) Header() *rtp.Header { | ||
return p.header | ||
} | ||
|
||
func (p *retainablePacket) Payload() []byte { | ||
return p.payload | ||
} | ||
|
||
func (p *retainablePacket) Retain() error { | ||
if p.count == 0 { | ||
// already released | ||
return errPacketReleased | ||
} | ||
p.countMu.Lock() | ||
p.count++ | ||
p.countMu.Unlock() | ||
return nil | ||
} | ||
|
||
func (p *retainablePacket) Release() { | ||
p.countMu.Lock() | ||
defer p.countMu.Unlock() | ||
p.count-- | ||
|
||
if p.count == 0 { | ||
// release back to pool | ||
p.onRelease(p.header, p.buffer) | ||
p.header = nil | ||
p.buffer = nil | ||
p.payload = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters