Skip to content

Commit

Permalink
Move common BW estimator types to internal package
Browse files Browse the repository at this point in the history
Avoids import loops but still keeps common types like DataRate and
PacketResult available for pkg/cc/interceptor and new bandwidth
estimators.
  • Loading branch information
mengelbart committed Nov 5, 2021
1 parent f33f518 commit 57e9724
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 130 deletions.
9 changes: 8 additions & 1 deletion pkg/cc/datarate.go → internal/types/datarate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc
package types

const (
// BitPerSecond is a data rate of 1 bit per second
Expand All @@ -24,3 +24,10 @@ func MaxDataRate(a, b DataRate) DataRate {
}
return b
}

func MinDataRate(a, b DataRate) DataRate {
if a < b {
return a
}
return b
}
20 changes: 20 additions & 0 deletions internal/types/packet_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types

import (
"time"

"github.com/pion/rtp"
)

type SentPacket struct {
SendTime time.Time
Header rtp.Header
}

// PacketResult holds information about a packet and if/when it has been
// sent/received.
type PacketResult struct {
SentPacket SentPacket
ReceiveTime time.Time
Received bool
}
44 changes: 16 additions & 28 deletions pkg/cc/feedback_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/types"
"github.com/pion/rtcp"
"github.com/pion/rtp"
)
Expand All @@ -14,29 +15,16 @@ var errMissingTWCCExtension = errors.New("missing transport layer cc header exte

// TODO(mathis): make types internal only?

type sentPacket struct {
sendTime time.Time
header rtp.Header
}

// PacketResult holds information about a packet and if/when it has been
// sent/received.
type PacketResult struct {
SentPacket sentPacket
receiveTime time.Time
Received bool
}

// FeedbackAdapter converts incoming feedback from the wireformat to a
// PacketResult
type FeedbackAdapter struct {
history map[uint16]sentPacket
history map[uint16]types.SentPacket
}

// NewFeedbackAdapter returns a new FeedbackAdapter
func NewFeedbackAdapter() *FeedbackAdapter {
return &FeedbackAdapter{
history: make(map[uint16]sentPacket),
history: make(map[uint16]types.SentPacket),
}
}

Expand All @@ -55,17 +43,17 @@ func (f *FeedbackAdapter) OnSent(ts time.Time, pkt *rtp.Packet, attributes inter
return err
}

f.history[tccExt.TransportSequence] = sentPacket{
sendTime: ts,
header: pkt.Header,
f.history[tccExt.TransportSequence] = types.SentPacket{
SendTime: ts,
Header: pkt.Header,
}
return nil
}

// OnIncomingTransportCC converts the incoming rtcp.TransportLayerCC to a
// []PacketResult
func (f *FeedbackAdapter) OnIncomingTransportCC(ts time.Time, feedback *rtcp.TransportLayerCC) []PacketResult {
result := []PacketResult{}
func (f *FeedbackAdapter) OnIncomingTransportCC(ts time.Time, feedback *rtcp.TransportLayerCC) []types.PacketResult {
result := []types.PacketResult{}

baseSequenceNr := feedback.BaseSequenceNumber
sequenceNr := baseSequenceNr
Expand Down Expand Up @@ -109,9 +97,9 @@ func (f *FeedbackAdapter) OnIncomingTransportCC(ts time.Time, feedback *rtcp.Tra
receiveTime := referenceTime.Add(delta)
referenceTime = receiveTime

result = append(result, PacketResult{
result = append(result, types.PacketResult{
SentPacket: sent,
receiveTime: receiveTime,
ReceiveTime: receiveTime,
Received: true,
})
delete(f.history, sequenceNr)
Expand Down Expand Up @@ -153,9 +141,9 @@ func (f *FeedbackAdapter) OnIncomingTransportCC(ts time.Time, feedback *rtcp.Tra
receiveTime := referenceTime.Add(delta)
referenceTime = receiveTime

result = append(result, PacketResult{
result = append(result, types.PacketResult{
SentPacket: sent,
receiveTime: receiveTime,
ReceiveTime: receiveTime,
Received: true,
})
delete(f.history, sequenceNr)
Expand All @@ -167,16 +155,16 @@ func (f *FeedbackAdapter) OnIncomingTransportCC(ts time.Time, feedback *rtcp.Tra
}

for _, v := range sortedKeysUint16(f.history) {
result = append(result, PacketResult{
result = append(result, types.PacketResult{
SentPacket: f.history[v],
receiveTime: time.Time{},
ReceiveTime: time.Time{},
Received: false,
})
}
return result
}

func sortedKeysUint16(m map[uint16]sentPacket) []uint16 {
func sortedKeysUint16(m map[uint16]types.SentPacket) []uint16 {
var result []uint16
for k := range m {
result = append(result, k)
Expand All @@ -188,6 +176,6 @@ func sortedKeysUint16(m map[uint16]sentPacket) []uint16 {
}

// OnIncomingRFC8888 converts the incoming RFC8888 packet to a []PacketResult
func (f *FeedbackAdapter) OnIncomingRFC8888(ts time.Time, feedback *rtcp.RawPacket) []PacketResult {
func (f *FeedbackAdapter) OnIncomingRFC8888(ts time.Time, feedback *rtcp.RawPacket) []types.PacketResult {
return nil
}
91 changes: 46 additions & 45 deletions pkg/cc/feedback_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/internal/types"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,12 +40,12 @@ func TestFeedbackAdapterTWCC(t *testing.T) {
assert.NoError(t, adapter.OnSent(time.Time{}, pkt, interceptor.Attributes{twccExtension: hdrExtID}))
result := adapter.OnIncomingTransportCC(time.Time{}, &rtcp.TransportLayerCC{})
assert.NotEmpty(t, result)
assert.Contains(t, result, PacketResult{
SentPacket: sentPacket{
sendTime: time.Time{},
header: pkt.Header,
assert.Contains(t, result, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: time.Time{},
Header: pkt.Header,
},
receiveTime: time.Time{},
ReceiveTime: time.Time{},
Received: false,
})
})
Expand Down Expand Up @@ -131,61 +132,61 @@ func TestFeedbackAdapterTWCC(t *testing.T) {
assert.NotEmpty(t, results)
assert.Len(t, results, 22)

assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[0],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[0],
},
receiveTime: t0.Add(time.Millisecond),
ReceiveTime: t0.Add(time.Millisecond),
Received: true,
})

assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[1],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[1],
},
receiveTime: t0.Add(101 * time.Millisecond),
ReceiveTime: t0.Add(101 * time.Millisecond),
Received: true,
})

for i := uint16(2); i < 7; i++ {
assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[i],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[i],
},
receiveTime: time.Time{},
ReceiveTime: time.Time{},
Received: false,
})
}

assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[7],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[7],
},
receiveTime: t0.Add(104 * time.Millisecond),
ReceiveTime: t0.Add(104 * time.Millisecond),
Received: true,
})

for i := uint16(8); i < 21; i++ {
assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[i],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[i],
},
receiveTime: time.Time{},
ReceiveTime: time.Time{},
Received: false,
})
}

assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: headers[21],
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: headers[21],
},
receiveTime: t0.Add(105 * time.Millisecond),
ReceiveTime: t0.Add(105 * time.Millisecond),
Received: true,
})
})
Expand Down Expand Up @@ -274,20 +275,20 @@ func TestFeedbackAdapterTWCC(t *testing.T) {

assert.NotEmpty(t, results)
assert.Len(t, results, 2)
assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: pkt65535.Header,
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: pkt65535.Header,
},
receiveTime: t0.Add(1 * time.Millisecond),
ReceiveTime: t0.Add(1 * time.Millisecond),
Received: true,
})
assert.Contains(t, results, PacketResult{
SentPacket: sentPacket{
sendTime: t0,
header: pkt0.Header,
assert.Contains(t, results, types.PacketResult{
SentPacket: types.SentPacket{
SendTime: t0,
Header: pkt0.Header,
},
receiveTime: t0.Add(2 * time.Millisecond),
ReceiveTime: t0.Add(2 * time.Millisecond),
Received: true,
})
})
Expand Down
Loading

0 comments on commit 57e9724

Please sign in to comment.