-
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.
Move buffer for RTP packets into internal
Can be used by NACK and JitterBuffer now
- Loading branch information
Showing
11 changed files
with
237 additions
and
237 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package rtpbuffer | ||
|
||
import "errors" | ||
|
||
// ErrInvalidSize is returned by newReceiveLog/newRTPBuffer, when an incorrect buffer size is supplied. | ||
var ErrInvalidSize = errors.New("invalid buffer size") | ||
|
||
var ( | ||
errPacketReleased = errors.New("could not retain packet, already released") | ||
errFailedToCastHeaderPool = errors.New("could not access header pool, failed cast") | ||
errFailedToCastPayloadPool = errors.New("could not access payload pool, failed cast") | ||
) |
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,61 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package rtpbuffer | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pion/rtp" | ||
) | ||
|
||
// RetainablePacket is a referenced counted RTP packet | ||
type RetainablePacket struct { | ||
onRelease func(*rtp.Header, *[]byte) | ||
|
||
countMu sync.Mutex | ||
count int | ||
|
||
header *rtp.Header | ||
buffer *[]byte | ||
payload []byte | ||
|
||
sequenceNumber uint16 | ||
} | ||
|
||
// Header returns the RTP Header of the RetainablePacket | ||
func (p *RetainablePacket) Header() *rtp.Header { | ||
return p.header | ||
} | ||
|
||
// Payload returns the RTP Payload of the RetainablePacket | ||
func (p *RetainablePacket) Payload() []byte { | ||
return p.payload | ||
} | ||
|
||
// Retain increases the reference count of the RetainablePacket | ||
func (p *RetainablePacket) Retain() error { | ||
p.countMu.Lock() | ||
defer p.countMu.Unlock() | ||
if p.count == 0 { | ||
// already released | ||
return errPacketReleased | ||
} | ||
p.count++ | ||
return nil | ||
} | ||
|
||
// Release decreases the reference count of the RetainablePacket and frees if needed | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package rtpbuffer provides a buffer for storing RTP packets | ||
package rtpbuffer | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
// Uint16SizeHalf is half of a math.Uint16 | ||
Uint16SizeHalf = 1 << 15 | ||
|
||
maxPayloadLen = 1460 | ||
) | ||
|
||
// RTPBuffer stores RTP packets and allows custom logic around the lifetime of them via the PacketFactory | ||
type RTPBuffer struct { | ||
packets []*RetainablePacket | ||
size uint16 | ||
lastAdded uint16 | ||
started bool | ||
} | ||
|
||
// NewRTPBuffer constructs a new RTPBuffer | ||
func NewRTPBuffer(size uint16) (*RTPBuffer, error) { | ||
allowedSizes := make([]uint16, 0) | ||
correctSize := false | ||
for i := 0; i < 16; i++ { | ||
if size == 1<<i { | ||
correctSize = true | ||
break | ||
} | ||
allowedSizes = append(allowedSizes, 1<<i) | ||
} | ||
|
||
if !correctSize { | ||
return nil, fmt.Errorf("%w: %d is not a valid size, allowed sizes: %v", ErrInvalidSize, size, allowedSizes) | ||
} | ||
|
||
return &RTPBuffer{ | ||
packets: make([]*RetainablePacket, size), | ||
size: size, | ||
}, nil | ||
} | ||
|
||
// Add places the RetainablePacket in the RTPBuffer | ||
func (r *RTPBuffer) Add(packet *RetainablePacket) { | ||
seq := packet.sequenceNumber | ||
if !r.started { | ||
r.packets[seq%r.size] = packet | ||
r.lastAdded = seq | ||
r.started = true | ||
return | ||
} | ||
|
||
diff := seq - r.lastAdded | ||
if diff == 0 { | ||
return | ||
} else if diff < Uint16SizeHalf { | ||
for i := r.lastAdded + 1; i != seq; i++ { | ||
idx := i % r.size | ||
prevPacket := r.packets[idx] | ||
if prevPacket != nil { | ||
prevPacket.Release() | ||
} | ||
r.packets[idx] = nil | ||
} | ||
} | ||
|
||
idx := seq % r.size | ||
prevPacket := r.packets[idx] | ||
if prevPacket != nil { | ||
prevPacket.Release() | ||
} | ||
r.packets[idx] = packet | ||
r.lastAdded = seq | ||
} | ||
|
||
// Get returns the RetainablePacket for the requested sequence number | ||
func (r *RTPBuffer) Get(seq uint16) *RetainablePacket { | ||
diff := r.lastAdded - seq | ||
if diff >= Uint16SizeHalf { | ||
return nil | ||
} | ||
|
||
if diff >= r.size { | ||
return nil | ||
} | ||
|
||
pkt := r.packets[seq%r.size] | ||
if pkt != nil { | ||
if pkt.sequenceNumber != seq { | ||
return nil | ||
} | ||
// already released | ||
if err := pkt.Retain(); err != nil { | ||
return nil | ||
} | ||
} | ||
return pkt | ||
} |
Oops, something went wrong.