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)
|
||||
|
||||
@@ -28,6 +28,7 @@ type TrackSender interface {
|
||||
// ID is the globally unique identifier for this Track.
|
||||
ID() string
|
||||
SetTrackType(isSimulcast bool)
|
||||
Codec() webrtc.RTPCodecCapability
|
||||
PeerID() string
|
||||
}
|
||||
|
||||
@@ -194,7 +195,7 @@ func NewDownTrack(c webrtc.RTPCodecCapability, r TrackReceiver, bf *buffer.Facto
|
||||
}
|
||||
|
||||
if strings.ToLower(c.MimeType) == "video/vp8" {
|
||||
d.payload = packetFactory.Get().(*[]byte)
|
||||
d.payload = PacketFactory.Get().(*[]byte)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
@@ -457,7 +458,7 @@ func (d *DownTrack) Close() {
|
||||
d.closeOnce.Do(func() {
|
||||
Logger.V(1).Info("Closing sender", "peer_id", d.peerID, "kind", d.kind)
|
||||
if d.payload != nil {
|
||||
packetFactory.Put(d.payload)
|
||||
PacketFactory.Put(d.payload)
|
||||
}
|
||||
if d.onCloseHandler != nil {
|
||||
d.onCloseHandler()
|
||||
@@ -592,9 +593,6 @@ func (d *DownTrack) CreateSenderReport() *rtcp.SenderReport {
|
||||
nowNTP := toNtpTime(now)
|
||||
|
||||
diff := (uint64(now.Sub(ntpTime(srNTP).Time())) * uint64(d.codec.ClockRate)) / uint64(time.Second)
|
||||
if diff < 0 {
|
||||
diff = 0
|
||||
}
|
||||
octets, packets := d.getSRStats()
|
||||
return &rtcp.SenderReport{
|
||||
SSRC: d.ssrc,
|
||||
@@ -792,8 +790,8 @@ func (d *DownTrack) maybeTranslateVP8(pkt *rtp.Packet, meta packetMeta) error {
|
||||
}
|
||||
|
||||
func (d *DownTrack) retransmitPackets(nackedPackets []packetMeta) {
|
||||
src := packetFactory.Get().(*[]byte)
|
||||
defer packetFactory.Put(src)
|
||||
src := PacketFactory.Get().(*[]byte)
|
||||
defer PacketFactory.Put(src)
|
||||
for _, meta := range nackedPackets {
|
||||
pktBuff := *src
|
||||
n, err := d.receiver.ReadRTP(pktBuff, meta.layer, meta.sourceSeqNo)
|
||||
|
||||
+7
-15
@@ -25,20 +25,21 @@ type TrackReceiver interface {
|
||||
DeleteDownTrack(peerID string)
|
||||
SendPLI(layer int32)
|
||||
GetSenderReportTime(layer int32) (rtpTS uint32, ntpTS uint64)
|
||||
Codec() webrtc.RTPCodecCapability
|
||||
}
|
||||
|
||||
// Receiver defines a interface for a track receivers
|
||||
type Receiver interface {
|
||||
TrackID() string
|
||||
StreamID() string
|
||||
Codec() webrtc.RTPCodecParameters
|
||||
Codec() webrtc.RTPCodecCapability
|
||||
AddUpTrack(track *webrtc.TrackRemote, buffer *buffer.Buffer)
|
||||
AddDownTrack(track TrackSender)
|
||||
SetUpTrackPaused(paused bool)
|
||||
NumAvailableSpatialLayers() int
|
||||
GetBitrateTemporalCumulative() [3][4]uint64
|
||||
ReadRTP(buf []byte, layer uint8, sn uint16) (int, error)
|
||||
DeleteDownTrack(peerID string)
|
||||
DeleteDownTrack(ID string)
|
||||
OnCloseHandler(fn func())
|
||||
SendPLI(layer int32)
|
||||
SetRTCPCh(ch chan []rtcp.Packet)
|
||||
@@ -47,10 +48,6 @@ type Receiver interface {
|
||||
DebugInfo() map[string]interface{}
|
||||
}
|
||||
|
||||
const (
|
||||
lostUpdateDelta = 1e9
|
||||
)
|
||||
|
||||
var (
|
||||
defaultBitratesCumulative = [][]uint64{{60000, 90000, 150000, 0}, {200000, 300000, 500000, 0}, {400000, 600000, 1000000, 0}}
|
||||
)
|
||||
@@ -61,7 +58,6 @@ type WebRTCReceiver struct {
|
||||
trackID string
|
||||
streamID string
|
||||
kind webrtc.RTPCodecType
|
||||
stream string
|
||||
receiver *webrtc.RTPReceiver
|
||||
codec webrtc.RTPCodecParameters
|
||||
isSimulcast bool
|
||||
@@ -89,10 +85,6 @@ type WebRTCReceiver struct {
|
||||
free map[int]struct{}
|
||||
numProcs int
|
||||
lbThreshold int
|
||||
|
||||
fracLostMu sync.Mutex
|
||||
maxDownFracLost uint8
|
||||
maxDownFracLostTs time.Time
|
||||
}
|
||||
|
||||
type ReceiverOpts func(w *WebRTCReceiver) *WebRTCReceiver
|
||||
@@ -173,8 +165,8 @@ func (w *WebRTCReceiver) SSRC(layer int) uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) Codec() webrtc.RTPCodecParameters {
|
||||
return w.codec
|
||||
func (w *WebRTCReceiver) Codec() webrtc.RTPCodecCapability {
|
||||
return w.codec.RTPCodecCapability
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) Kind() webrtc.RTPCodecType {
|
||||
@@ -349,7 +341,7 @@ func (w *WebRTCReceiver) GetBitrateTemporalCumulative() [3][4]uint64 {
|
||||
tls := make([]uint64, 4)
|
||||
if w.hasSpatialLayer(int32(i)) {
|
||||
tls = buff.BitrateTemporalCumulative()
|
||||
w.maybeUseDefaultBitrate(tls, i)
|
||||
MaybeUseDefaultBitrate(tls, i)
|
||||
}
|
||||
|
||||
for j := 0; j < len(br[i]); j++ {
|
||||
@@ -533,7 +525,7 @@ func (w *WebRTCReceiver) storeDownTrack(track TrackSender) {
|
||||
w.downTracks = append(w.downTracks, track)
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) maybeUseDefaultBitrate(tlbs []uint64, layer int) {
|
||||
func MaybeUseDefaultBitrate(tlbs []uint64, layer int) {
|
||||
for _, tlb := range tlbs {
|
||||
if tlb != uint64(0) {
|
||||
// some layer has data
|
||||
|
||||
+2
-2
@@ -10,12 +10,12 @@ import (
|
||||
var Logger logr.Logger = logr.Discard()
|
||||
|
||||
var (
|
||||
packetFactory *sync.Pool
|
||||
PacketFactory *sync.Pool
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Init packet factory
|
||||
packetFactory = &sync.Pool{
|
||||
PacketFactory = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
b := make([]byte, 1460)
|
||||
return &b
|
||||
|
||||
Reference in New Issue
Block a user