/
rfc8888.go
276 lines (238 loc) · 8.1 KB
/
rfc8888.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package rtcp
import (
"encoding/binary"
"errors"
)
// https://www.rfc-editor.org/rfc/rfc8888.html#name-rtcp-congestion-control-fee
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |V=2|P| FMT=11 | PT = 205 | length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | SSRC of RTCP packet sender |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | SSRC of 1st RTP Stream |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | begin_seq | num_reports |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |R|ECN| Arrival time offset | ... .
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// . .
// . .
// . .
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | SSRC of nth RTP Stream |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | begin_seq | num_reports |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |R|ECN| Arrival time offset | ... |
// . .
// . .
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Report Timestamp (32 bits) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
var (
errReportBlockLength = errors.New("feedback report blocks must be at least 8 bytes")
errIncorrectNumReports = errors.New("feedback report block contains less reports than num_reports")
errMetricBlockLength = errors.New("feedback report metric blocks must be exactly 2 bytes")
)
// ECN represents the two ECN bits
type ECN uint8
const (
//nolint:misspell
// ECNNonECT signals Non ECN-Capable Transport, Non-ECT
ECNNonECT ECN = iota // 00
//nolint:misspell
// ECNECT1 signals ECN Capable Transport, ECT(0)
ECNECT1 // 01
//nolint:misspell
// ECNECT0 signals ECN Capable Transport, ECT(1)
ECNECT0 // 10
// ECNCE signals ECN Congestion Encountered, CE
ECNCE // 11
)
const (
reportTimestampLength = 4
reportBlockOffset = 8
)
// CCFeedbackReport is a Congestion Control Feedback Report as defined in
// https://www.rfc-editor.org/rfc/rfc8888.html#name-rtcp-congestion-control-fee
type CCFeedbackReport struct {
// header
Header Header
// SSRC of sender
SenderSSRC uint32
// Report Blocks
ReportBlocks []CCFeedbackReportBlock
// Basetime
ReportTimestamp uint32
}
// DestinationSSRC returns an array of SSRC values that this packet refers to.
func (b CCFeedbackReport) DestinationSSRC() []uint32 {
ssrcs := make([]uint32, len(b.ReportBlocks))
for i, block := range b.ReportBlocks {
ssrcs[i] = block.MediaSSRC
}
return ssrcs
}
// Len returns the length of the report in bytes
func (b *CCFeedbackReport) Len() uint16 {
n := uint16(0)
for _, block := range b.ReportBlocks {
n += block.Len()
}
return reportBlockOffset + n + reportTimestampLength
}
// Marshal encodes the Congestion Control Feedback Report in binary
func (b CCFeedbackReport) Marshal() ([]byte, error) {
header, err := b.Header.Marshal()
if err != nil {
return nil, err
}
buf := make([]byte, b.Len())
copy(buf[:headerLength], header)
binary.BigEndian.PutUint32(buf[headerLength:], b.SenderSSRC)
offset := uint16(reportBlockOffset)
for _, block := range b.ReportBlocks {
b, err := block.marshal()
if err != nil {
return nil, err
}
copy(buf[offset:], b)
offset += block.Len()
}
binary.BigEndian.PutUint32(buf[offset:], b.ReportTimestamp)
return buf, nil
}
// Unmarshal decodes the Congestion Control Feedback Report from binary
func (b *CCFeedbackReport) Unmarshal(rawPacket []byte) error {
if len(rawPacket) < headerLength+ssrcLength+reportTimestampLength {
return errPacketTooShort
}
if err := b.Header.Unmarshal(rawPacket); err != nil {
return err
}
b.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:])
reportTimestampOffset := uint16(len(rawPacket) - reportTimestampLength)
b.ReportTimestamp = binary.BigEndian.Uint32(rawPacket[reportTimestampOffset:])
offset := uint16(reportBlockOffset)
b.ReportBlocks = []CCFeedbackReportBlock{}
for offset < reportTimestampOffset {
var block CCFeedbackReportBlock
if err := block.unmarshal(rawPacket[offset:]); err != nil {
return err
}
b.ReportBlocks = append(b.ReportBlocks, block)
offset += block.Len()
}
return nil
}
const (
ssrcOffset = 0
beginSequenceOffset = 4
numReportsOffset = 6
reportsOffset = 8
maxMetricBlocks = 16384
)
// CCFeedbackReportBlock is a Feedback Report Block
type CCFeedbackReportBlock struct {
// SSRC of the RTP stream on which this block is reporting
MediaSSRC uint32
BeginSequence uint16
MetricBlocks []CCFeedbackMetricBlock
}
// Len returns the length of the report block in bytes
func (b *CCFeedbackReportBlock) Len() uint16 {
n := len(b.MetricBlocks)
if n%2 != 0 {
n++
}
return reportsOffset + 2*uint16(n)
}
// marshal encodes the Congestion Control Feedback Report Block in binary
func (b CCFeedbackReportBlock) marshal() ([]byte, error) {
if len(b.MetricBlocks) > maxMetricBlocks {
return nil, errTooManyReports
}
buf := make([]byte, b.Len())
binary.BigEndian.PutUint32(buf[ssrcOffset:], b.MediaSSRC)
binary.BigEndian.PutUint16(buf[beginSequenceOffset:], b.BeginSequence)
binary.BigEndian.PutUint16(buf[numReportsOffset:], uint16(len(b.MetricBlocks)))
for i, block := range b.MetricBlocks {
b, err := block.marshal()
if err != nil {
return nil, err
}
copy(buf[reportsOffset+i*2:], b)
}
return buf, nil
}
// Unmarshal decodes the Congestion Control Feedback Report Block from binary
func (b *CCFeedbackReportBlock) unmarshal(rawPacket []byte) error {
if len(rawPacket) < reportsOffset {
return errReportBlockLength
}
b.MediaSSRC = binary.BigEndian.Uint32(rawPacket[:beginSequenceOffset])
b.BeginSequence = binary.BigEndian.Uint16(rawPacket[beginSequenceOffset:numReportsOffset])
numReports := binary.BigEndian.Uint16(rawPacket[numReportsOffset:])
if len(rawPacket) < int(reportsOffset+numReports*2) {
return errIncorrectNumReports
}
b.MetricBlocks = make([]CCFeedbackMetricBlock, numReports)
for i := uint16(0); i < numReports; i++ {
var mb CCFeedbackMetricBlock
offset := reportsOffset + 2*i
if err := mb.unmarshal(rawPacket[offset : offset+2]); err != nil {
return err
}
b.MetricBlocks[i] = mb
}
return nil
}
const (
metricBlockLength = 2
)
// CCFeedbackMetricBlock is a Feedback Metric Block
type CCFeedbackMetricBlock struct {
Received bool
ECN ECN
// Offset in 1/1024 seconds before Report Timestamp
ArrivalTimeOffset uint16
}
// Marshal encodes the Congestion Control Feedback Metric Block in binary
func (b CCFeedbackMetricBlock) marshal() ([]byte, error) {
buf := make([]byte, 2)
r := uint16(0)
if b.Received {
r = 1
}
dst, err := setNBitsOfUint16(0, 1, 0, r)
if err != nil {
return nil, err
}
dst, err = setNBitsOfUint16(dst, 2, 1, uint16(b.ECN))
if err != nil {
return nil, err
}
dst, err = setNBitsOfUint16(dst, 13, 3, b.ArrivalTimeOffset)
if err != nil {
return nil, err
}
binary.BigEndian.PutUint16(buf, dst)
return buf, nil
}
// Unmarshal decodes the Congestion Control Feedback Metric Block from binary
func (b *CCFeedbackMetricBlock) unmarshal(rawPacket []byte) error {
if len(rawPacket) != metricBlockLength {
return errMetricBlockLength
}
b.Received = rawPacket[0]&0x80 != 0
if !b.Received {
b.ECN = ECNNonECT
b.ArrivalTimeOffset = 0
return nil
}
b.ECN = ECN(rawPacket[0] >> 5 & 0x03)
b.ArrivalTimeOffset = binary.BigEndian.Uint16(rawPacket) & 0x1FFF
return nil
}