Separate from ion-sfu (#171)
* Separate from ion-sfu changes: 1. extract pkg/buffer, twcc, sfu, relay, stats, logger 2. to solve cycle import, move ion-sfu/pkg/logger to pkg/sfu/logger 3. replace pion/ion-sfu => ./ reason: will change import pion/ion-sfu/pkg/* to livekit-server/pkg/* after this pr merged. Just not change any code in this pr, because it will confused with the separate code from ion-sfu in review. * Move code from ion-sfu to pkg/sfu * fix build error for resovle conflict Co-authored-by: cnderrauber <zengjie9004@gmail.com>
This commit is contained in:
co-authored by
cnderrauber
parent
289ebd32ff
commit
1e1aaeb86b
@@ -0,0 +1,313 @@
|
||||
package twcc
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/gammazero/deque"
|
||||
"github.com/pion/rtcp"
|
||||
)
|
||||
|
||||
const (
|
||||
baseSequenceNumberOffset = 8
|
||||
packetStatusCountOffset = 10
|
||||
referenceTimeOffset = 12
|
||||
|
||||
tccReportDelta = 1e8
|
||||
tccReportDeltaAfterMark = 50e6
|
||||
)
|
||||
|
||||
type rtpExtInfo struct {
|
||||
ExtTSN uint32
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
// Responder will get the transport wide sequence number from rtp
|
||||
// extension header, and reply with the rtcp feedback message
|
||||
// according to:
|
||||
// https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
|
||||
type Responder struct {
|
||||
sync.Mutex
|
||||
|
||||
extInfo []rtpExtInfo
|
||||
lastReport int64
|
||||
cycles uint32
|
||||
lastExtSN uint32
|
||||
pktCtn uint8
|
||||
lastSn uint16
|
||||
lastExtInfo uint16
|
||||
mSSRC uint32
|
||||
sSSRC uint32
|
||||
|
||||
len uint16
|
||||
deltaLen uint16
|
||||
payload [100]byte
|
||||
deltas [200]byte
|
||||
chunk uint16
|
||||
|
||||
onFeedback func(packet rtcp.RawPacket)
|
||||
}
|
||||
|
||||
func NewTransportWideCCResponder(ssrc uint32) *Responder {
|
||||
return &Responder{
|
||||
extInfo: make([]rtpExtInfo, 0, 101),
|
||||
sSSRC: rand.Uint32(),
|
||||
mSSRC: ssrc,
|
||||
}
|
||||
}
|
||||
|
||||
// Push a sequence number read from rtp packet ext packet
|
||||
func (t *Responder) Push(sn uint16, timeNS int64, marker bool) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
if sn < 0x0fff && (t.lastSn&0xffff) > 0xf000 {
|
||||
t.cycles += 1 << 16
|
||||
}
|
||||
t.extInfo = append(t.extInfo, rtpExtInfo{
|
||||
ExtTSN: t.cycles | uint32(sn),
|
||||
Timestamp: timeNS / 1e3,
|
||||
})
|
||||
if t.lastReport == 0 {
|
||||
t.lastReport = timeNS
|
||||
}
|
||||
t.lastSn = sn
|
||||
delta := timeNS - t.lastReport
|
||||
if len(t.extInfo) > 20 && t.mSSRC != 0 &&
|
||||
(delta >= tccReportDelta || len(t.extInfo) > 100 || (marker && delta >= tccReportDeltaAfterMark)) {
|
||||
if pkt := t.buildTransportCCPacket(); pkt != nil {
|
||||
t.onFeedback(pkt)
|
||||
}
|
||||
t.lastReport = timeNS
|
||||
}
|
||||
}
|
||||
|
||||
// OnFeedback sets the callback for the formed twcc feedback rtcp packet
|
||||
func (t *Responder) OnFeedback(f func(p rtcp.RawPacket)) {
|
||||
t.onFeedback = f
|
||||
}
|
||||
|
||||
func (t *Responder) buildTransportCCPacket() rtcp.RawPacket {
|
||||
if len(t.extInfo) == 0 {
|
||||
return nil
|
||||
}
|
||||
sort.Slice(t.extInfo, func(i, j int) bool {
|
||||
return t.extInfo[i].ExtTSN < t.extInfo[j].ExtTSN
|
||||
})
|
||||
tccPkts := make([]rtpExtInfo, 0, int(float64(len(t.extInfo))*1.2))
|
||||
for _, tccExtInfo := range t.extInfo {
|
||||
if tccExtInfo.ExtTSN < t.lastExtSN {
|
||||
continue
|
||||
}
|
||||
if t.lastExtSN != 0 {
|
||||
for j := t.lastExtSN + 1; j < tccExtInfo.ExtTSN; j++ {
|
||||
tccPkts = append(tccPkts, rtpExtInfo{ExtTSN: j})
|
||||
}
|
||||
}
|
||||
t.lastExtSN = tccExtInfo.ExtTSN
|
||||
tccPkts = append(tccPkts, tccExtInfo)
|
||||
}
|
||||
t.extInfo = t.extInfo[:0]
|
||||
|
||||
firstRecv := false
|
||||
same := true
|
||||
timestamp := int64(0)
|
||||
lastStatus := rtcp.TypeTCCPacketReceivedWithoutDelta
|
||||
maxStatus := rtcp.TypeTCCPacketNotReceived
|
||||
|
||||
var statusList deque.Deque
|
||||
statusList.SetMinCapacity(3)
|
||||
|
||||
for _, stat := range tccPkts {
|
||||
status := rtcp.TypeTCCPacketNotReceived
|
||||
if stat.Timestamp != 0 {
|
||||
var delta int64
|
||||
if !firstRecv {
|
||||
firstRecv = true
|
||||
refTime := stat.Timestamp / 64e3
|
||||
timestamp = refTime * 64e3
|
||||
t.writeHeader(
|
||||
uint16(tccPkts[0].ExtTSN),
|
||||
uint16(len(tccPkts)),
|
||||
uint32(refTime),
|
||||
)
|
||||
t.pktCtn++
|
||||
}
|
||||
|
||||
delta = (stat.Timestamp - timestamp) / 250
|
||||
if delta < 0 || delta > 255 {
|
||||
status = rtcp.TypeTCCPacketReceivedLargeDelta
|
||||
rDelta := int16(delta)
|
||||
if int64(rDelta) != delta {
|
||||
if rDelta > 0 {
|
||||
rDelta = math.MaxInt16
|
||||
} else {
|
||||
rDelta = math.MinInt16
|
||||
}
|
||||
}
|
||||
t.writeDelta(status, uint16(rDelta))
|
||||
} else {
|
||||
status = rtcp.TypeTCCPacketReceivedSmallDelta
|
||||
t.writeDelta(status, uint16(delta))
|
||||
}
|
||||
timestamp = stat.Timestamp
|
||||
}
|
||||
|
||||
if same && status != lastStatus && lastStatus != rtcp.TypeTCCPacketReceivedWithoutDelta {
|
||||
if statusList.Len() > 7 {
|
||||
t.writeRunLengthChunk(lastStatus, uint16(statusList.Len()))
|
||||
statusList.Clear()
|
||||
lastStatus = rtcp.TypeTCCPacketReceivedWithoutDelta
|
||||
maxStatus = rtcp.TypeTCCPacketNotReceived
|
||||
same = true
|
||||
} else {
|
||||
same = false
|
||||
}
|
||||
}
|
||||
statusList.PushBack(status)
|
||||
if status > maxStatus {
|
||||
maxStatus = status
|
||||
}
|
||||
lastStatus = status
|
||||
|
||||
if !same && maxStatus == rtcp.TypeTCCPacketReceivedLargeDelta && statusList.Len() > 6 {
|
||||
for i := 0; i < 7; i++ {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit, statusList.PopFront().(uint16), i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit)
|
||||
lastStatus = rtcp.TypeTCCPacketReceivedWithoutDelta
|
||||
maxStatus = rtcp.TypeTCCPacketNotReceived
|
||||
same = true
|
||||
|
||||
for i := 0; i < statusList.Len(); i++ {
|
||||
status = statusList.At(i).(uint16)
|
||||
if status > maxStatus {
|
||||
maxStatus = status
|
||||
}
|
||||
if same && lastStatus != rtcp.TypeTCCPacketReceivedWithoutDelta && status != lastStatus {
|
||||
same = false
|
||||
}
|
||||
lastStatus = status
|
||||
}
|
||||
} else if !same && statusList.Len() > 13 {
|
||||
for i := 0; i < 14; i++ {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit, statusList.PopFront().(uint16), i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit)
|
||||
lastStatus = rtcp.TypeTCCPacketReceivedWithoutDelta
|
||||
maxStatus = rtcp.TypeTCCPacketNotReceived
|
||||
same = true
|
||||
}
|
||||
}
|
||||
|
||||
if statusList.Len() > 0 {
|
||||
if same {
|
||||
t.writeRunLengthChunk(lastStatus, uint16(statusList.Len()))
|
||||
} else if maxStatus == rtcp.TypeTCCPacketReceivedLargeDelta {
|
||||
for i := 0; i < statusList.Len(); i++ {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit, statusList.PopFront().(uint16), i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit)
|
||||
} else {
|
||||
for i := 0; i < statusList.Len(); i++ {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit, statusList.PopFront().(uint16), i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit)
|
||||
}
|
||||
}
|
||||
|
||||
pLen := t.len + t.deltaLen + 4
|
||||
pad := pLen%4 != 0
|
||||
var padSize uint8
|
||||
for pLen%4 != 0 {
|
||||
padSize++
|
||||
pLen++
|
||||
}
|
||||
hdr := rtcp.Header{
|
||||
Padding: pad,
|
||||
Length: (pLen / 4) - 1,
|
||||
Count: rtcp.FormatTCC,
|
||||
Type: rtcp.TypeTransportSpecificFeedback,
|
||||
}
|
||||
hb, _ := hdr.Marshal()
|
||||
pkt := make(rtcp.RawPacket, pLen)
|
||||
copy(pkt, hb)
|
||||
copy(pkt[4:], t.payload[:t.len])
|
||||
copy(pkt[4+t.len:], t.deltas[:t.deltaLen])
|
||||
if pad {
|
||||
pkt[len(pkt)-1] = padSize
|
||||
}
|
||||
t.deltaLen = 0
|
||||
return pkt
|
||||
}
|
||||
|
||||
func (t *Responder) writeHeader(bSN, packetCount uint16, refTime uint32) {
|
||||
/*
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| SSRC of packet sender |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| SSRC of media source |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| base sequence number | packet status count |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| reference time | fb pkt. count |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
binary.BigEndian.PutUint32(t.payload[0:], t.sSSRC)
|
||||
binary.BigEndian.PutUint32(t.payload[4:], t.mSSRC)
|
||||
binary.BigEndian.PutUint16(t.payload[baseSequenceNumberOffset:], bSN)
|
||||
binary.BigEndian.PutUint16(t.payload[packetStatusCountOffset:], packetCount)
|
||||
binary.BigEndian.PutUint32(t.payload[referenceTimeOffset:], refTime<<8|uint32(t.pktCtn))
|
||||
t.len = 16
|
||||
}
|
||||
|
||||
func (t *Responder) writeRunLengthChunk(symbol uint16, runLength uint16) {
|
||||
/*
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|T| S | Run Length |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
binary.BigEndian.PutUint16(t.payload[t.len:], symbol<<13|runLength)
|
||||
t.len += 2
|
||||
}
|
||||
|
||||
func (t *Responder) createStatusSymbolChunk(symbolSize, symbol uint16, i int) {
|
||||
/*
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|T|S| symbol list |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
numOfBits := symbolSize + 1
|
||||
t.chunk = setNBitsOfUint16(t.chunk, numOfBits, numOfBits*uint16(i)+2, symbol)
|
||||
}
|
||||
|
||||
func (t *Responder) writeStatusSymbolChunk(symbolSize uint16) {
|
||||
t.chunk = setNBitsOfUint16(t.chunk, 1, 0, 1)
|
||||
t.chunk = setNBitsOfUint16(t.chunk, 1, 1, symbolSize)
|
||||
binary.BigEndian.PutUint16(t.payload[t.len:], t.chunk)
|
||||
t.chunk = 0
|
||||
t.len += 2
|
||||
}
|
||||
|
||||
func (t *Responder) writeDelta(deltaType, delta uint16) {
|
||||
if deltaType == rtcp.TypeTCCPacketReceivedSmallDelta {
|
||||
t.deltas[t.deltaLen] = byte(delta)
|
||||
t.deltaLen++
|
||||
return
|
||||
}
|
||||
binary.BigEndian.PutUint16(t.deltas[t.deltaLen:], delta)
|
||||
t.deltaLen += 2
|
||||
}
|
||||
|
||||
// setNBitsOfUint16 will truncate the value to size, left-shift to startIndex position and set
|
||||
func setNBitsOfUint16(src, size, startIndex, val uint16) uint16 {
|
||||
if startIndex+size > 16 {
|
||||
return 0
|
||||
}
|
||||
// truncate val to size bits
|
||||
val &= (1 << size) - 1
|
||||
return src | (val << (16 - size - startIndex))
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
package twcc
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTransportWideCC_writeRunLengthChunk(t1 *testing.T) {
|
||||
type fields struct {
|
||||
len uint16
|
||||
}
|
||||
type args struct {
|
||||
symbol uint16
|
||||
runLength uint16
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
wantBytes []byte
|
||||
}{
|
||||
{
|
||||
name: "Must not return error",
|
||||
|
||||
args: args{
|
||||
symbol: rtcp.TypeTCCPacketNotReceived,
|
||||
runLength: 221,
|
||||
},
|
||||
wantErr: false,
|
||||
wantBytes: []byte{0, 0xdd},
|
||||
}, {
|
||||
name: "Must set run length after padding",
|
||||
fields: fields{
|
||||
len: 1,
|
||||
},
|
||||
args: args{
|
||||
symbol: rtcp.TypeTCCPacketReceivedWithoutDelta,
|
||||
runLength: 24,
|
||||
},
|
||||
wantBytes: []byte{0, 0x60, 0x18},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t1.Run(tt.name, func(t1 *testing.T) {
|
||||
t := &Responder{
|
||||
len: tt.fields.len,
|
||||
}
|
||||
t.writeRunLengthChunk(tt.args.symbol, tt.args.runLength)
|
||||
assert.Equal(t1, tt.wantBytes, t.payload[:t.len])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportWideCC_writeStatusSymbolChunk(t1 *testing.T) {
|
||||
type fields struct {
|
||||
len uint16
|
||||
}
|
||||
type args struct {
|
||||
symbolSize uint16
|
||||
symbolList []uint16
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantBytes []byte
|
||||
}{
|
||||
{
|
||||
name: "Must not return error",
|
||||
args: args{
|
||||
symbolSize: rtcp.TypeTCCSymbolSizeOneBit,
|
||||
symbolList: []uint16{rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived},
|
||||
},
|
||||
wantBytes: []byte{0x9F, 0x1C},
|
||||
},
|
||||
{
|
||||
name: "Must set symbol chunk after padding",
|
||||
fields: fields{
|
||||
len: 1,
|
||||
},
|
||||
args: args{
|
||||
symbolSize: rtcp.TypeTCCSymbolSizeTwoBit,
|
||||
symbolList: []uint16{
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedWithoutDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived},
|
||||
},
|
||||
wantBytes: []byte{0x0, 0xcd, 0x50},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t1.Run(tt.name, func(t1 *testing.T) {
|
||||
t := &Responder{
|
||||
len: tt.fields.len,
|
||||
}
|
||||
for i, v := range tt.args.symbolList {
|
||||
t.createStatusSymbolChunk(tt.args.symbolSize, v, i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(tt.args.symbolSize)
|
||||
assert.Equal(t1, tt.wantBytes, t.payload[:t.len])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportWideCC_writeDelta(t1 *testing.T) {
|
||||
a := -32768
|
||||
type fields struct {
|
||||
deltaLen uint16
|
||||
}
|
||||
type args struct {
|
||||
deltaType uint16
|
||||
delta uint16
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []byte
|
||||
}{
|
||||
{
|
||||
name: "Must set correct small delta",
|
||||
args: args{
|
||||
deltaType: rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
delta: 255,
|
||||
},
|
||||
want: []byte{0xff},
|
||||
},
|
||||
{
|
||||
name: "Must set correct small delta with padding",
|
||||
fields: fields{
|
||||
deltaLen: 1,
|
||||
},
|
||||
args: args{
|
||||
deltaType: rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
delta: 255,
|
||||
},
|
||||
want: []byte{0, 0xff},
|
||||
},
|
||||
{
|
||||
name: "Must set correct large delta",
|
||||
args: args{
|
||||
deltaType: rtcp.TypeTCCPacketReceivedLargeDelta,
|
||||
delta: 32767,
|
||||
},
|
||||
want: []byte{0x7F, 0xFF},
|
||||
},
|
||||
{
|
||||
name: "Must set correct large delta with padding",
|
||||
fields: fields{
|
||||
deltaLen: 1,
|
||||
},
|
||||
args: args{
|
||||
deltaType: rtcp.TypeTCCPacketReceivedLargeDelta,
|
||||
delta: uint16(a),
|
||||
},
|
||||
want: []byte{0, 0x80, 0x00},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t1.Run(tt.name, func(t1 *testing.T) {
|
||||
t := &Responder{
|
||||
deltaLen: tt.fields.deltaLen,
|
||||
}
|
||||
t.writeDelta(tt.args.deltaType, tt.args.delta)
|
||||
assert.Equal(t1, tt.want, t.deltas[:t.deltaLen])
|
||||
assert.Equal(t1, tt.fields.deltaLen+tt.args.deltaType, t.deltaLen)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportWideCC_writeHeader(t1 *testing.T) {
|
||||
type fields struct {
|
||||
tccPktCtn uint8
|
||||
sSSRC uint32
|
||||
mSSRC uint32
|
||||
}
|
||||
type args struct {
|
||||
bSN uint16
|
||||
packetCount uint16
|
||||
refTime uint32
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []byte
|
||||
}{
|
||||
{
|
||||
name: "Must construct correct header",
|
||||
fields: fields{
|
||||
tccPktCtn: 23,
|
||||
sSSRC: 4195875351,
|
||||
mSSRC: 1124282272,
|
||||
},
|
||||
args: args{
|
||||
bSN: 153,
|
||||
packetCount: 1,
|
||||
refTime: 4057090,
|
||||
},
|
||||
want: []byte{
|
||||
0xfa, 0x17, 0xfa, 0x17,
|
||||
0x43, 0x3, 0x2f, 0xa0,
|
||||
0x0, 0x99, 0x0, 0x1,
|
||||
0x3d, 0xe8, 0x2, 0x17},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t1.Run(tt.name, func(t1 *testing.T) {
|
||||
t := &Responder{
|
||||
pktCtn: tt.fields.tccPktCtn,
|
||||
sSSRC: tt.fields.sSSRC,
|
||||
mSSRC: tt.fields.mSSRC,
|
||||
}
|
||||
t.writeHeader(tt.args.bSN, tt.args.packetCount, tt.args.refTime)
|
||||
assert.Equal(t1, tt.want, t.payload[0:16])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTccPacket(t1 *testing.T) {
|
||||
want := []byte{
|
||||
0xfa, 0x17, 0xfa, 0x17,
|
||||
0x43, 0x3, 0x2f, 0xa0,
|
||||
0x0, 0x99, 0x0, 0x1,
|
||||
0x3d, 0xe8, 0x2, 0x17,
|
||||
0x60, 0x18, 0x0, 0xdd,
|
||||
0x9F, 0x1C, 0xcd, 0x50,
|
||||
}
|
||||
|
||||
delta := []byte{
|
||||
0xff, 0x80, 0xaa,
|
||||
}
|
||||
|
||||
symbol1 := []uint16{rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived}
|
||||
symbol2 := []uint16{
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketReceivedWithoutDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketReceivedSmallDelta,
|
||||
rtcp.TypeTCCPacketNotReceived,
|
||||
rtcp.TypeTCCPacketNotReceived}
|
||||
|
||||
t := &Responder{
|
||||
pktCtn: 23,
|
||||
sSSRC: 4195875351,
|
||||
mSSRC: 1124282272,
|
||||
}
|
||||
t.writeHeader(153, 1, 4057090)
|
||||
t.writeRunLengthChunk(rtcp.TypeTCCPacketReceivedWithoutDelta, 24)
|
||||
t.writeRunLengthChunk(rtcp.TypeTCCPacketNotReceived, 221)
|
||||
for i, v := range symbol1 {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit, v, i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeOneBit)
|
||||
for i, v := range symbol2 {
|
||||
t.createStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit, v, i)
|
||||
}
|
||||
t.writeStatusSymbolChunk(rtcp.TypeTCCSymbolSizeTwoBit)
|
||||
t.deltaLen = uint16(len(delta))
|
||||
assert.Equal(t1, want, t.payload[:24])
|
||||
|
||||
pLen := t.len + t.deltaLen + 4
|
||||
pad := pLen%4 != 0
|
||||
for pLen%4 != 0 {
|
||||
pLen++
|
||||
}
|
||||
hdr := rtcp.Header{
|
||||
Padding: pad,
|
||||
Length: (pLen / 4) - 1,
|
||||
Count: rtcp.FormatTCC,
|
||||
Type: rtcp.TypeTransportSpecificFeedback,
|
||||
}
|
||||
assert.Equal(t1, int(pLen), len(want)+3+4+1)
|
||||
hb, _ := hdr.Marshal()
|
||||
pkt := make([]byte, pLen)
|
||||
copy(pkt, hb)
|
||||
assert.Equal(t1, hb, pkt[:len(hb)])
|
||||
copy(pkt[4:], t.payload[:t.len])
|
||||
assert.Equal(t1, append(hb, t.payload[:t.len]...), pkt[:len(hb)+int(t.len)])
|
||||
copy(pkt[4+t.len:], delta[:t.deltaLen])
|
||||
assert.Equal(t1, delta, pkt[len(hb)+int(t.len):len(pkt)-1])
|
||||
var ss rtcp.TransportLayerCC
|
||||
err := ss.Unmarshal(pkt)
|
||||
assert.NoError(t1, err)
|
||||
|
||||
assert.Equal(t1, hdr, ss.Header)
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkBuildPacket(b *testing.B) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
b.ReportAllocs()
|
||||
n := 1 + rand.Intn(4-1+1)
|
||||
var twcc Responder
|
||||
tm := time.Now()
|
||||
for i := 1; i < 100; i++ {
|
||||
tm := tm.Add(time.Duration(60*n) * time.Millisecond)
|
||||
twcc.extInfo = append(twcc.extInfo, rtpExtInfo{
|
||||
ExtTSN: uint32(i),
|
||||
Timestamp: tm.UnixNano(),
|
||||
})
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = twcc.buildTransportCCPacket()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user