export structure & functions for relay (#214)
* export structure & functions for relay Co-authored-by: jie.zeng <zengjie9004@gmail.com>
This commit is contained in:
@@ -47,12 +47,12 @@ func (b *Bucket) AddPacket(pkt []byte, sn uint16, latest bool) ([]byte, error) {
|
||||
func (b *Bucket) GetPacket(buf []byte, sn uint16) (i int, err error) {
|
||||
p := b.get(sn)
|
||||
if p == nil {
|
||||
err = errPacketNotFound
|
||||
err = ErrPacketNotFound
|
||||
return
|
||||
}
|
||||
i = len(p)
|
||||
if cap(buf) < i {
|
||||
err = errBufferTooSmall
|
||||
err = ErrBufferTooSmall
|
||||
return
|
||||
}
|
||||
if len(buf) < i {
|
||||
@@ -94,7 +94,7 @@ func (b *Bucket) get(sn uint16) []byte {
|
||||
|
||||
func (b *Bucket) set(sn uint16, pkt []byte) ([]byte, error) {
|
||||
if b.headSN-sn >= uint16(b.maxSteps+1) {
|
||||
return nil, errPacketTooOld
|
||||
return nil, ErrPacketTooOld
|
||||
}
|
||||
pos := b.step - int(b.headSN-sn+1)
|
||||
if pos < 0 {
|
||||
@@ -102,11 +102,11 @@ func (b *Bucket) set(sn uint16, pkt []byte) ([]byte, error) {
|
||||
}
|
||||
off := pos * maxPktSize
|
||||
if off > len(b.buf) || off < 0 {
|
||||
return nil, errPacketTooOld
|
||||
return nil, ErrPacketTooOld
|
||||
}
|
||||
// Do not overwrite if packet exist
|
||||
if binary.BigEndian.Uint16(b.buf[off+4:off+6]) == sn {
|
||||
return nil, errRTXPacket
|
||||
return nil, ErrRTXPacket
|
||||
}
|
||||
binary.BigEndian.PutUint16(b.buf[off:], uint16(len(pkt)))
|
||||
copy(b.buf[off+2:], pkt)
|
||||
|
||||
@@ -78,7 +78,7 @@ func Test_queue(t *testing.T) {
|
||||
assert.Equal(t, expectedSN, np.SequenceNumber)
|
||||
|
||||
_, err = q.AddPacket(buf, 8, false)
|
||||
assert.ErrorIs(t, err, errRTXPacket)
|
||||
assert.ErrorIs(t, err, ErrRTXPacket)
|
||||
}
|
||||
|
||||
func Test_queue_edges(t *testing.T) {
|
||||
|
||||
+24
-24
@@ -19,7 +19,7 @@ import (
|
||||
const (
|
||||
MaxSN = 1 << 16
|
||||
|
||||
reportDelta = 1e9
|
||||
ReportDelta = 1e9
|
||||
)
|
||||
|
||||
// Logger is an implementation of logr.Logger. If is not provided - will be turned off.
|
||||
@@ -31,19 +31,19 @@ type pendingPackets struct {
|
||||
}
|
||||
|
||||
type ExtPacket struct {
|
||||
Head bool
|
||||
Cycle uint32
|
||||
Arrival int64
|
||||
Packet rtp.Packet
|
||||
Payload interface{}
|
||||
KeyFrame bool
|
||||
Head bool
|
||||
Arrival int64
|
||||
Packet rtp.Packet
|
||||
Payload interface{}
|
||||
KeyFrame bool
|
||||
RawPacket []byte
|
||||
}
|
||||
|
||||
// Buffer contains all packets
|
||||
type Buffer struct {
|
||||
sync.Mutex
|
||||
bucket *Bucket
|
||||
nacker *nackQueue
|
||||
nacker *NackQueue
|
||||
videoPool *sync.Pool
|
||||
audioPool *sync.Pool
|
||||
codecType webrtc.RTPCodecType
|
||||
@@ -159,7 +159,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
b.twcc = true
|
||||
case webrtc.TypeRTCPFBNACK:
|
||||
b.logger.V(1).Info("Setting feedback", "type", webrtc.TypeRTCPFBNACK)
|
||||
b.nacker = newNACKQueue()
|
||||
b.nacker = NewNACKQueue()
|
||||
b.nack = true
|
||||
}
|
||||
}
|
||||
@@ -215,7 +215,7 @@ func (b *Buffer) Read(buff []byte) (n int, err error) {
|
||||
b.Lock()
|
||||
if b.pPackets != nil && len(b.pPackets) > b.lastPacketRead {
|
||||
if len(buff) < len(b.pPackets[b.lastPacketRead].packet) {
|
||||
err = errBufferTooSmall
|
||||
err = ErrBufferTooSmall
|
||||
b.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -281,10 +281,10 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
if b.nack {
|
||||
if isNewer {
|
||||
for i := b.seqHdlr.MaxSeqNo() + 1; i < extSN; i++ {
|
||||
b.nacker.push(i)
|
||||
b.nacker.Push(i)
|
||||
}
|
||||
} else {
|
||||
b.nacker.remove(extSN)
|
||||
b.nacker.Remove(extSN)
|
||||
}
|
||||
}
|
||||
if isNewer {
|
||||
@@ -296,7 +296,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
var p rtp.Packet
|
||||
pb, err := b.bucket.AddPacket(pkt, sn, headPkt)
|
||||
if err != nil {
|
||||
if err == errRTXPacket {
|
||||
if err == ErrRTXPacket {
|
||||
return
|
||||
}
|
||||
return
|
||||
@@ -308,7 +308,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
// submit to TWCC even if it is a padding only packet. Clients use padding only packets as probes
|
||||
// for bandwidth estimation
|
||||
if b.twcc {
|
||||
if ext := p.GetExtension(b.twccExt); ext != nil && len(ext) > 1 {
|
||||
if ext := p.GetExtension(b.twccExt); len(ext) > 1 {
|
||||
b.feedbackTWCC(binary.BigEndian.Uint16(ext[0:2]), arrivalTime, p.Marker)
|
||||
}
|
||||
}
|
||||
@@ -317,10 +317,10 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
b.stats.PacketCount++
|
||||
|
||||
ep := ExtPacket{
|
||||
Head: headPkt,
|
||||
Cycle: b.seqHdlr.Cycles(),
|
||||
Packet: p,
|
||||
Arrival: arrivalTime,
|
||||
Head: headPkt,
|
||||
Packet: p,
|
||||
Arrival: arrivalTime,
|
||||
RawPacket: pb,
|
||||
}
|
||||
|
||||
if len(p.Payload) == 0 {
|
||||
@@ -340,7 +340,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
ep.KeyFrame = vp8Packet.IsKeyFrame
|
||||
temporalLayer = int32(vp8Packet.TID)
|
||||
case "video/h264":
|
||||
ep.KeyFrame = isH264Keyframe(p.Payload)
|
||||
ep.KeyFrame = IsH264Keyframe(p.Payload)
|
||||
}
|
||||
|
||||
if b.minPacketProbe < 25 {
|
||||
@@ -401,7 +401,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
b.bitrateHelper[temporalLayer] += uint64(len(pkt))
|
||||
|
||||
diff := arrivalTime - b.lastReport
|
||||
if diff >= reportDelta {
|
||||
if diff >= ReportDelta {
|
||||
//
|
||||
// As this happens in the data path, if there are no packets received
|
||||
// in an interval, the bitrate will be stuck with the old value.
|
||||
@@ -413,7 +413,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
bitrates = make([]uint64, len(b.bitrateHelper))
|
||||
}
|
||||
for i := 0; i < len(b.bitrateHelper); i++ {
|
||||
br := (8 * b.bitrateHelper[i] * uint64(reportDelta)) / uint64(diff)
|
||||
br := (8 * b.bitrateHelper[i] * uint64(ReportDelta)) / uint64(diff)
|
||||
bitrates[i] = br
|
||||
b.bitrateHelper[i] = 0
|
||||
}
|
||||
@@ -424,7 +424,7 @@ func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
}
|
||||
|
||||
func (b *Buffer) buildNACKPacket() []rtcp.Packet {
|
||||
if nacks, askKeyframe := b.nacker.pairs(b.seqHdlr.MaxSeqNo()); (nacks != nil && len(nacks) > 0) || askKeyframe {
|
||||
if nacks, askKeyframe := b.nacker.Pairs(b.seqHdlr.MaxSeqNo()); len(nacks) > 0 || askKeyframe {
|
||||
var pkts []rtcp.Packet
|
||||
if len(nacks) > 0 {
|
||||
pkts = []rtcp.Packet{&rtcp.TransportLayerNack{
|
||||
@@ -644,7 +644,7 @@ func IsLaterTimestamp(timestamp1 uint32, timestamp2 uint32) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func isNewerUint16(val1, val2 uint16) bool {
|
||||
func IsNewerUint16(val1, val2 uint16) bool {
|
||||
return val1 != val2 && val1-val2 < 0x8000
|
||||
}
|
||||
|
||||
@@ -666,7 +666,7 @@ func (s *SeqWrapHandler) Unwrap(seq uint16) (uint32, bool) {
|
||||
maxSeqNo := uint16(s.maxSeqNo)
|
||||
delta := int32(seq) - int32(maxSeqNo)
|
||||
|
||||
newer := isNewerUint16(seq, maxSeqNo)
|
||||
newer := IsNewerUint16(seq, maxSeqNo)
|
||||
|
||||
if newer {
|
||||
if delta < 0 {
|
||||
|
||||
@@ -3,8 +3,8 @@ package buffer
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
errPacketNotFound = errors.New("packet not found in cache")
|
||||
errBufferTooSmall = errors.New("buffer too small")
|
||||
errPacketTooOld = errors.New("received packet too old")
|
||||
errRTXPacket = errors.New("packet already received")
|
||||
ErrPacketNotFound = errors.New("packet not found in cache")
|
||||
ErrBufferTooSmall = errors.New("buffer too small")
|
||||
ErrPacketTooOld = errors.New("received packet too old")
|
||||
ErrRTXPacket = errors.New("packet already received")
|
||||
)
|
||||
|
||||
@@ -222,10 +222,10 @@ func VP8PictureIdSizeDiff(mBit1 bool, mBit2 bool) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// isH264Keyframe detects if h264 payload is a keyframe
|
||||
// IsH264Keyframe detects if h264 payload is a keyframe
|
||||
// this code was taken from https://github.com/jech/galene/blob/codecs/rtpconn/rtpreader.go#L45
|
||||
// all credits belongs to Juliusz Chroboczek @jech and the awesome Galene SFU
|
||||
func isH264Keyframe(payload []byte) bool {
|
||||
func IsH264Keyframe(payload []byte) bool {
|
||||
if len(payload) < 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -14,18 +14,18 @@ type nack struct {
|
||||
nacked uint8
|
||||
}
|
||||
|
||||
type nackQueue struct {
|
||||
type NackQueue struct {
|
||||
nacks []nack
|
||||
kfSN uint32
|
||||
}
|
||||
|
||||
func newNACKQueue() *nackQueue {
|
||||
return &nackQueue{
|
||||
func NewNACKQueue() *NackQueue {
|
||||
return &NackQueue{
|
||||
nacks: make([]nack, 0, maxNackCache+1),
|
||||
}
|
||||
}
|
||||
|
||||
func (n *nackQueue) remove(extSN uint32) {
|
||||
func (n *NackQueue) Remove(extSN uint32) {
|
||||
i := sort.Search(len(n.nacks), func(i int) bool { return n.nacks[i].sn >= extSN })
|
||||
if i >= len(n.nacks) || n.nacks[i].sn != extSN {
|
||||
return
|
||||
@@ -34,7 +34,7 @@ func (n *nackQueue) remove(extSN uint32) {
|
||||
n.nacks = n.nacks[:len(n.nacks)-1]
|
||||
}
|
||||
|
||||
func (n *nackQueue) push(extSN uint32) {
|
||||
func (n *NackQueue) Push(extSN uint32) {
|
||||
i := sort.Search(len(n.nacks), func(i int) bool { return n.nacks[i].sn >= extSN })
|
||||
if i < len(n.nacks) && n.nacks[i].sn == extSN {
|
||||
return
|
||||
@@ -56,7 +56,7 @@ func (n *nackQueue) push(extSN uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *nackQueue) pairs(headSN uint32) ([]rtcp.NackPair, bool) {
|
||||
func (n *NackQueue) Pairs(headSN uint32) ([]rtcp.NackPair, bool) {
|
||||
if len(n.nacks) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
+12
-12
@@ -67,13 +67,13 @@ func Test_nackQueue_pairs(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := &nackQueue{
|
||||
n := &NackQueue{
|
||||
nacks: tt.fields.nacks,
|
||||
}
|
||||
for _, sn := range tt.args {
|
||||
n.push(sn)
|
||||
n.Push(sn)
|
||||
}
|
||||
got, _ := n.pairs(75530)
|
||||
got, _ := n.Pairs(75530)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("pairs() = %v, want %v", got, tt.want)
|
||||
}
|
||||
@@ -108,11 +108,11 @@ func Test_nackQueue_push(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := &nackQueue{
|
||||
n := &NackQueue{
|
||||
nacks: tt.fields.nacks,
|
||||
}
|
||||
for _, sn := range tt.args.sn {
|
||||
n.push(sn)
|
||||
n.Push(sn)
|
||||
}
|
||||
var newSN []uint32
|
||||
for _, sn := range n.nacks {
|
||||
@@ -148,13 +148,13 @@ func Test_nackQueue(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := nackQueue{}
|
||||
n := NackQueue{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < 100; i++ {
|
||||
assert.NotPanics(t, func() {
|
||||
n.push(uint32(r.Intn(60000)))
|
||||
n.remove(uint32(r.Intn(60000)))
|
||||
n.pairs(60001)
|
||||
n.Push(uint32(r.Intn(60000)))
|
||||
n.Remove(uint32(r.Intn(60000)))
|
||||
n.Pairs(60001)
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -181,11 +181,11 @@ func Test_nackQueue_remove(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := nackQueue{}
|
||||
n := NackQueue{}
|
||||
for _, sn := range tt.args.sn {
|
||||
n.push(sn)
|
||||
n.Push(sn)
|
||||
}
|
||||
n.remove(5)
|
||||
n.Remove(5)
|
||||
var newSN []uint32
|
||||
for _, sn := range n.nacks {
|
||||
newSN = append(newSN, sn.sn)
|
||||
|
||||
Reference in New Issue
Block a user