Skip to content

Commit 1961dc7

Browse files
committed
Add report reception timestamps
1 parent 3566f46 commit 1961dc7

File tree

7 files changed

+51
-51
lines changed

7 files changed

+51
-51
lines changed

pkg/ccfb/ccfb_receiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ type acknowledgementList struct {
1919
acks []acknowledgement
2020
}
2121

22-
func convertCCFB(ts time.Time, feedback *rtcp.CCFeedbackReport) map[uint32]acknowledgementList {
22+
func convertCCFB(ts time.Time, feedback *rtcp.CCFeedbackReport) (time.Time, map[uint32]acknowledgementList) {
2323
if feedback == nil {
24-
return nil
24+
return time.Time{}, nil
2525
}
2626
result := map[uint32]acknowledgementList{}
2727
referenceTime := ntp.ToTime32(feedback.ReportTimestamp, ts)
2828
for _, rb := range feedback.ReportBlocks {
2929
result[rb.MediaSSRC] = convertMetricBlock(ts, referenceTime, rb.BeginSequence, rb.MetricBlocks)
3030
}
31-
return result
31+
return referenceTime, result
3232
}
3333

3434
func convertMetricBlock(ts time.Time, reference time.Time, seqNrOffset uint16, blocks []rtcp.CCFeedbackMetricBlock) acknowledgementList {

pkg/ccfb/ccfb_receiver_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestConvertCCFB(t *testing.T) {
1616
ts time.Time
1717
feedback *rtcp.CCFeedbackReport
1818
expect map[uint32]acknowledgementList
19+
expectTS time.Time
1920
}{
2021
{},
2122
{
@@ -50,11 +51,14 @@ func TestConvertCCFB(t *testing.T) {
5051
},
5152
},
5253
},
54+
expectTS: timeZero.Add(time.Second),
5355
},
5456
}
5557
for i, tc := range cases {
5658
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
57-
res := convertCCFB(tc.ts, tc.feedback)
59+
resTS, res := convertCCFB(tc.ts, tc.feedback)
60+
61+
assert.InDelta(t, tc.expectTS.UnixNano(), resTS.UnixNano(), float64(time.Millisecond.Nanoseconds()))
5862

5963
// Can't directly check equality since arrival timestamp conversions
6064
// may be slightly off due to ntp conversions.

pkg/ccfb/history.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
type PacketReportList struct {
14-
Timestamp time.Time
14+
Arrival time.Time
15+
Departure time.Time
1516
Reports []PacketReport
1617
}
1718

@@ -109,7 +110,7 @@ func (h *history) getReportForAck(al acknowledgementList) PacketReportList {
109110
}
110111

111112
return PacketReportList{
112-
Timestamp: al.ts,
113-
Reports: reports,
113+
Arrival: al.ts,
114+
Reports: reports,
114115
}
115116
}

pkg/ccfb/history_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestHistory(t *testing.T) {
7272
},
7373
},
7474
expectedReport: PacketReportList{
75-
Timestamp: time.Time{}.Add(time.Second),
75+
Arrival: time.Time{}.Add(time.Second),
7676
Reports: []PacketReport{
7777
{1, 1200, time.Time{}.Add(2 * time.Millisecond), true, time.Time{}.Add(3 * time.Millisecond), 0},
7878
{2, 1200, time.Time{}.Add(3 * time.Millisecond), false, time.Time{}, 0},

pkg/ccfb/interceptor.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ func (i *Interceptor) BindLocalStream(info *interceptor.StreamInfo, writer inter
9393
// BindRTCPReader implements interceptor.Interceptor.
9494
func (i *Interceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.RTCPReader {
9595
return interceptor.RTCPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
96-
now := i.timestamp()
97-
9896
n, attr, err := reader.Read(b, a)
9997
if err != nil {
10098
return n, attr, err
10199
}
100+
now := i.timestamp()
101+
102102
buf := make([]byte, n)
103103
copy(buf, b[:n])
104104

@@ -111,14 +111,16 @@ func (i *Interceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.
111111
pkts, err := attr.GetRTCPPackets(buf)
112112
for _, pkt := range pkts {
113113
var reportLists map[uint32]acknowledgementList
114+
var reportDeparture time.Time
114115
switch fb := pkt.(type) {
115116
case *rtcp.CCFeedbackReport:
116-
reportLists = convertCCFB(now, fb)
117+
reportDeparture, reportLists = convertCCFB(now, fb)
117118
case *rtcp.TransportLayerCC:
118-
reportLists = convertTWCC(now, fb)
119+
reportDeparture, reportLists = convertTWCC(now, fb)
119120
}
120121
for ssrc, reportList := range reportLists {
121122
prl := i.ssrcToHistory[ssrc].getReportForAck(reportList)
123+
prl.Departure = reportDeparture
122124
if l, ok := pktReportLists[ssrc]; !ok {
123125
pktReportLists[ssrc] = &prl
124126
} else {

pkg/ccfb/twcc_receiver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"github.com/pion/rtcp"
88
)
99

10-
func convertTWCC(ts time.Time, feedback *rtcp.TransportLayerCC) map[uint32]acknowledgementList {
10+
func convertTWCC(ts time.Time, feedback *rtcp.TransportLayerCC) (time.Time, map[uint32]acknowledgementList) {
1111
log.Printf("got twcc report: %v", feedback)
1212
if feedback == nil {
13-
return nil
13+
return time.Time{}, nil
1414
}
1515
var acks []acknowledgement
1616

1717
nextTimestamp := time.Time{}.Add(time.Duration(feedback.ReferenceTime) * 64 * time.Millisecond)
18+
reportDeparture := nextTimestamp
1819
recvDeltaIndex := 0
1920

2021
offset := 0
@@ -85,7 +86,7 @@ func convertTWCC(ts time.Time, feedback *rtcp.TransportLayerCC) map[uint32]ackno
8586
}
8687
}
8788

88-
return map[uint32]acknowledgementList{
89+
return reportDeparture, map[uint32]acknowledgementList{
8990
0: {
9091
ts: ts,
9192
acks: acks,

pkg/ccfb/twcc_receiver_test.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestConvertTWCC(t *testing.T) {
1515
ts time.Time
1616
feedback *rtcp.TransportLayerCC
1717
expect map[uint32]acknowledgementList
18+
expectTS time.Time
1819
}{
1920
{},
2021
{
@@ -24,26 +25,27 @@ func TestConvertTWCC(t *testing.T) {
2425
MediaSSRC: 2,
2526
BaseSequenceNumber: 178,
2627
PacketStatusCount: 0,
27-
ReferenceTime: 0,
28+
ReferenceTime: 3,
2829
FbPktCount: 0,
2930
PacketChunks: []rtcp.PacketStatusChunk{},
3031
RecvDeltas: []*rtcp.RecvDelta{},
3132
},
3233
expect: map[uint32]acknowledgementList{
33-
2: {
34+
0: {
3435
ts: timeZero.Add(2 * time.Second),
35-
acks: []acknowledgement{},
36+
acks: nil,
3637
},
3738
},
39+
expectTS: time.Time{}.Add(3 * 64 * time.Millisecond),
3840
},
3941
{
4042
ts: timeZero.Add(2 * time.Second),
4143
feedback: &rtcp.TransportLayerCC{
4244
SenderSSRC: 1,
4345
MediaSSRC: 2,
4446
BaseSequenceNumber: 178,
45-
PacketStatusCount: 3,
46-
ReferenceTime: 0,
47+
PacketStatusCount: 18,
48+
ReferenceTime: 3,
4749
FbPktCount: 0,
4850
PacketChunks: []rtcp.PacketStatusChunk{
4951
&rtcp.RunLengthChunk{
@@ -77,38 +79,38 @@ func TestConvertTWCC(t *testing.T) {
7779
},
7880
},
7981
RecvDeltas: []*rtcp.RecvDelta{
80-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
81-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
82-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
83-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
84-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
85-
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 0},
86-
{Type: rtcp.TypeTCCPacketReceivedLargeDelta, Delta: 0},
87-
{Type: rtcp.TypeTCCPacketReceivedLargeDelta, Delta: 0},
82+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
83+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
84+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
85+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
86+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
87+
{Type: rtcp.TypeTCCPacketReceivedSmallDelta, Delta: 1000},
88+
{Type: rtcp.TypeTCCPacketReceivedLargeDelta, Delta: 1000},
89+
{Type: rtcp.TypeTCCPacketReceivedLargeDelta, Delta: 1000},
8890
},
8991
},
9092
expect: map[uint32]acknowledgementList{
91-
2: {
93+
0: {
9294
ts: timeZero.Add(2 * time.Second),
9395
acks: []acknowledgement{
9496
// first run length chunk
95-
{seqNr: 178, arrived: true, arrival: time.Time{}, ecn: 0},
96-
{seqNr: 179, arrived: true, arrival: time.Time{}, ecn: 0},
97-
{seqNr: 180, arrived: true, arrival: time.Time{}, ecn: 0},
97+
{seqNr: 178, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 1*time.Millisecond), ecn: 0},
98+
{seqNr: 179, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 2*time.Millisecond), ecn: 0},
99+
{seqNr: 180, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 3*time.Millisecond), ecn: 0},
98100

99101
// first status vector chunk
100-
{seqNr: 181, arrived: true, arrival: time.Time{}, ecn: 0},
101-
{seqNr: 182, arrived: true, arrival: time.Time{}, ecn: 0},
102-
{seqNr: 183, arrived: true, arrival: time.Time{}, ecn: 0},
102+
{seqNr: 181, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 4*time.Millisecond), ecn: 0},
103+
{seqNr: 182, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 5*time.Millisecond), ecn: 0},
104+
{seqNr: 183, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 6*time.Millisecond), ecn: 0},
103105
{seqNr: 184, arrived: false, arrival: time.Time{}, ecn: 0},
104106
{seqNr: 185, arrived: false, arrival: time.Time{}, ecn: 0},
105107
{seqNr: 186, arrived: false, arrival: time.Time{}, ecn: 0},
106108
{seqNr: 187, arrived: false, arrival: time.Time{}, ecn: 0},
107109
{seqNr: 188, arrived: false, arrival: time.Time{}, ecn: 0},
108110

109111
// second status vector chunk
110-
{seqNr: 189, arrived: true, arrival: time.Time{}, ecn: 0},
111-
{seqNr: 190, arrived: true, arrival: time.Time{}, ecn: 0},
112+
{seqNr: 189, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 7*time.Millisecond), ecn: 0},
113+
{seqNr: 190, arrived: true, arrival: time.Time{}.Add(3*64*time.Millisecond + 8*time.Millisecond), ecn: 0},
112114
{seqNr: 191, arrived: false, arrival: time.Time{}, ecn: 0},
113115
{seqNr: 192, arrived: false, arrival: time.Time{}, ecn: 0},
114116
{seqNr: 193, arrived: false, arrival: time.Time{}, ecn: 0},
@@ -117,24 +119,14 @@ func TestConvertTWCC(t *testing.T) {
117119
},
118120
},
119121
},
122+
expectTS: time.Time{}.Add(3 * 64 * time.Millisecond),
120123
},
121124
}
122125
for i, tc := range cases {
123126
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
124-
res := convertTWCC(tc.ts, tc.feedback)
125-
126-
// Can't directly check equality since arrival timestamp conversions
127-
// may be slightly off due to ntp conversions.
128-
assert.Equal(t, len(tc.expect), len(res))
129-
for i, ee := range tc.expect {
130-
assert.Equal(t, ee.ts, res[i].ts)
131-
for j, ack := range ee.acks {
132-
assert.Equal(t, ack.seqNr, res[i].acks[j].seqNr)
133-
assert.Equal(t, ack.arrived, res[i].acks[j].arrived)
134-
assert.Equal(t, ack.ecn, res[i].acks[j].ecn)
135-
assert.InDelta(t, ack.arrival.UnixNano(), res[i].acks[j].arrival.UnixNano(), float64(time.Millisecond.Nanoseconds()))
136-
}
137-
}
127+
resTS, res := convertTWCC(tc.ts, tc.feedback)
128+
assert.Equal(t, tc.expect, res)
129+
assert.Equal(t, tc.expectTS, resTS)
138130
})
139131
}
140132

0 commit comments

Comments
 (0)