Debian Codegraph @

rfc8888.go

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package rtcp
5
6 import (
7 "encoding/binary"
8 "errors"
9 "fmt"
10 "math"
11 "strings"
12 )
13
14 // https://www.rfc-editor.org/rfc/rfc8888.html#name-rtcp-congestion-control-fee
15 // 0 1 2 3
16 // 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
17 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18 // |V=2|P| FMT=11 | PT = 205 | length |
19 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 // | SSRC of RTCP packet sender |
21 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 // | SSRC of 1st RTP Stream |
23 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 // | begin_seq | num_reports |
25 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 // |R|ECN| Arrival time offset | ... .
27 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 // . .
29 // . .
30 // . .
31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 // | SSRC of nth RTP Stream |
33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 // | begin_seq | num_reports |
35 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 // |R|ECN| Arrival time offset | ... |
37 // . .
38 // . .
39 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 // | Report Timestamp (32 bits) |
41 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42
43 var (
44 errReportBlockLength = errors.New("feedback report blocks must be at least 8 bytes")
45 errIncorrectNumReports = errors.New("feedback report block contains less reports than num_reports")
46 errMetricBlockLength = errors.New("feedback report metric blocks must be exactly 2 bytes")
47 )
48
49 // ECN represents the two ECN bits.
50 type ECN uint8
51
52 const (
53 //nolint:misspell
54 // ECNNonECT signals Non ECN-Capable Transport, Non-ECT.
55 ECNNonECT ECN = iota // 00
56
57 //nolint:misspell
58 // ECNECT1 signals ECN Capable Transport, ECT(0).
59 ECNECT1 // 01
60
61 //nolint:misspell
62 // ECNECT0 signals ECN Capable Transport, ECT(1).
63 ECNECT0 // 10
64
65 // ECNCE signals ECN Congestion Encountered, CE.
66 ECNCE // 11
67 )
68
69 func (e ECN) String() string {
70 switch e {
71 case ECNNonECT:
72 //nolint:misspell
73 return "Non-ECT (00)"
74 case ECNECT0:
75 //nolint:misspell
76 return "ECT(0) (01)"
77 case ECNECT1:
78 //nolint:misspell
79 return "ECT(1) (10)"
80 case ECNCE:
81 //nolint:misspell
82 return "CE (11)"
83 }
84
85 return "invalid ECN value"
86 }
87
88 const (
89 reportTimestampLength = 4
90 reportBlockOffset = 8
91 )
92
93 // CCFeedbackReport is a Congestion Control Feedback Report as defined in
94 // https://www.rfc-editor.org/rfc/rfc8888.html#name-rtcp-congestion-control-fee
95 type CCFeedbackReport struct {
96 // SSRC of sender
97 SenderSSRC uint32
98
99 // Report Blocks
100 ReportBlocks []CCFeedbackReportBlock
101
102 // Basetime
103 ReportTimestamp uint32
104 }
105
106 // DestinationSSRC returns an array of SSRC values that this packet refers to.
107 func (b CCFeedbackReport) DestinationSSRC() []uint32 {
108 ssrcs := make([]uint32, len(b.ReportBlocks))
109 for i, block := range b.ReportBlocks {
110 ssrcs[i] = block.MediaSSRC
111 }
112
113 return ssrcs
114 }
115
116 // Len returns the length of the report in bytes.
117 func (b *CCFeedbackReport) Len() int {
118 return b.MarshalSize()
119 }
120
121 // MarshalSize returns the size of the packet once marshaled.
122 func (b *CCFeedbackReport) MarshalSize() int {
123 n := 0
124 for _, block := range b.ReportBlocks {
125 n += block.len()
126 }
127
128 return reportBlockOffset + n + reportTimestampLength
129 }
130
131 // Header returns the Header associated with this packet.
132 func (b *CCFeedbackReport) Header() Header {
133 return Header{
134 Padding: false,
135 Count: FormatCCFB,
136 Type: TypeTransportSpecificFeedback,
137 Length: uint16(b.MarshalSize()/4 - 1), //nolint:gosec // G115
138 }
139 }
140
141 // Marshal encodes the Congestion Control Feedback Report in binary.
142 func (b CCFeedbackReport) Marshal() ([]byte, error) {
143 header := b.Header()
144 headerBuf, err := header.Marshal()
145 if err != nil {
146 return nil, err
147 }
148 length := 4 * (int(header.Length) + 1)
149 buf := make([]byte, length)
150 copy(buf[:headerLength], headerBuf)
151 binary.BigEndian.PutUint32(buf[headerLength:], b.SenderSSRC)
152 offset := reportBlockOffset
153 for _, block := range b.ReportBlocks {
154 b, err := block.marshal()
155 if err != nil {
156 return nil, err
157 }
158 copy(buf[offset:], b)
159 offset += block.len()
160 }
161
162 binary.BigEndian.PutUint32(buf[offset:], b.ReportTimestamp)
163
164 return buf, nil
165 }
166
167 func (b CCFeedbackReport) String() string {
168 var out strings.Builder
169 fmt.Fprintf(&out, "CCFB:\n\tHeader %v\n", b.Header())
170 fmt.Fprintf(&out, "CCFB:\n\tSender SSRC %d\n", b.SenderSSRC)
171 fmt.Fprintf(&out, "\tReport Timestamp %d\n", b.ReportTimestamp)
172 out.WriteString("\tFeedback Reports \n")
173 for _, report := range b.ReportBlocks {
174 fmt.Fprintf(&out, "%v ", report)
175 }
176 out.WriteString("\n")
177
178 return out.String()
179 }
180
181 // Unmarshal decodes the Congestion Control Feedback Report from binary.
182 func (b *CCFeedbackReport) Unmarshal(rawPacket []byte) error {
183 if len(rawPacket) < headerLength+ssrcLength+reportTimestampLength {
184 return errPacketTooShort
185 }
186
187 var h Header
188 if err := h.Unmarshal(rawPacket); err != nil {
189 return err
190 }
191 if h.Type != TypeTransportSpecificFeedback {
192 return errWrongType
193 }
194
195 b.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:])
196
197 reportTimestampOffset := len(rawPacket) - reportTimestampLength
198 b.ReportTimestamp = binary.BigEndian.Uint32(rawPacket[reportTimestampOffset:])
199
200 offset := reportBlockOffset
201 b.ReportBlocks = []CCFeedbackReportBlock{}
202 for offset < reportTimestampOffset {
203 var block CCFeedbackReportBlock
204 if err := block.unmarshal(rawPacket[offset:]); err != nil {
205 return err
206 }
207 b.ReportBlocks = append(b.ReportBlocks, block)
208 offset += block.len()
209 }
210
211 return nil
212 }
213
214 const (
215 ssrcOffset = 0
216 beginSequenceOffset = 4
217 numReportsOffset = 6
218 reportsOffset = 8
219
220 maxMetricBlocks = 16384
221 )
222
223 // CCFeedbackReportBlock is a Feedback Report Block.
224 type CCFeedbackReportBlock struct {
225 // SSRC of the RTP stream on which this block is reporting
226 MediaSSRC uint32
227 BeginSequence uint16
228 MetricBlocks []CCFeedbackMetricBlock
229 }
230
231 // len returns the length of the report block in bytes.
232 func (b *CCFeedbackReportBlock) len() int {
233 n := len(b.MetricBlocks)
234 if n%2 != 0 {
235 n++
236 }
237
238 return reportsOffset + 2*n
239 }
240
241 func (b CCFeedbackReportBlock) String() string {
242 var out strings.Builder
243 fmt.Fprintf(&out, "\tReport Block Media SSRC %d\n", b.MediaSSRC)
244 fmt.Fprintf(&out, "\tReport Begin Sequence Nr %d\n", b.BeginSequence)
245 fmt.Fprintf(&out, "\tReport length %d\n\t", len(b.MetricBlocks))
246 for i, block := range b.MetricBlocks {
247 //nolint:gosec // G115
248 fmt.Fprintf(&out, "{nr: %d, rx: %v, ts: %v, ecn: %v} ",
249 b.BeginSequence+uint16(i),
250 block.Received,
251 block.ArrivalTimeOffset,
252 block.ECN)
253 }
254 out.WriteString("\n")
255
256 return out.String()
257 }
258
259 // marshal encodes the Congestion Control Feedback Report Block in binary.
260 func (b CCFeedbackReportBlock) marshal() ([]byte, error) {
261 if len(b.MetricBlocks) > maxMetricBlocks {
262 return nil, errTooManyReports
263 }
264
265 buf := make([]byte, b.len())
266 binary.BigEndian.PutUint32(buf[ssrcOffset:], b.MediaSSRC)
267 binary.BigEndian.PutUint16(buf[beginSequenceOffset:], b.BeginSequence)
268
269 length := uint16(len(b.MetricBlocks)) //nolint:gosec // G115
270
271 binary.BigEndian.PutUint16(buf[numReportsOffset:], length)
272
273 for i, block := range b.MetricBlocks {
274 b, err := block.marshal()
275 if err != nil {
276 return nil, err
277 }
278 copy(buf[reportsOffset+i*2:], b)
279 }
280
281 return buf, nil
282 }
283
284 // Unmarshal decodes the Congestion Control Feedback Report Block from binary.
285 func (b *CCFeedbackReportBlock) unmarshal(rawPacket []byte) error {
286 if len(rawPacket) < reportsOffset {
287 return errReportBlockLength
288 }
289 b.MediaSSRC = binary.BigEndian.Uint32(rawPacket[:beginSequenceOffset])
290 b.BeginSequence = binary.BigEndian.Uint16(rawPacket[beginSequenceOffset:numReportsOffset])
291 numReports := int(binary.BigEndian.Uint16(rawPacket[numReportsOffset:]))
292 if numReports == 0 {
293 return nil
294 }
295
296 if numReports > math.MaxUint16 {
297 return errIncorrectNumReports
298 }
299
300 if len(rawPacket) < reportsOffset+numReports*2 {
301 return errIncorrectNumReports
302 }
303
304 b.MetricBlocks = make([]CCFeedbackMetricBlock, numReports)
305 for i := range numReports {
306 var mb CCFeedbackMetricBlock
307 offset := reportsOffset + 2*i
308 if err := mb.unmarshal(rawPacket[offset : offset+2]); err != nil {
309 return err
310 }
311 b.MetricBlocks[i] = mb
312 }
313
314 return nil
315 }
316
317 const (
318 metricBlockLength = 2
319 )
320
321 // CCFeedbackMetricBlock is a Feedback Metric Block.
322 type CCFeedbackMetricBlock struct {
323 Received bool
324 ECN ECN
325
326 // Offset in 1/1024 seconds before Report Timestamp
327 ArrivalTimeOffset uint16
328 }
329
330 // Marshal encodes the Congestion Control Feedback Metric Block in binary.
331 func (b CCFeedbackMetricBlock) marshal() ([]byte, error) {
332 buf := make([]byte, 2)
333 r := uint16(0)
334 if b.Received {
335 r = 1
336 }
337 dst, err := setNBitsOfUint16(0, 1, 0, r)
338 if err != nil {
339 return nil, err
340 }
341 dst, err = setNBitsOfUint16(dst, 2, 1, uint16(b.ECN))
342 if err != nil {
343 return nil, err
344 }
345 dst, err = setNBitsOfUint16(dst, 13, 3, b.ArrivalTimeOffset)
346 if err != nil {
347 return nil, err
348 }
349
350 binary.BigEndian.PutUint16(buf, dst)
351
352 return buf, nil
353 }
354
355 // Unmarshal decodes the Congestion Control Feedback Metric Block from binary.
356 func (b *CCFeedbackMetricBlock) unmarshal(rawPacket []byte) error {
357 if len(rawPacket) != metricBlockLength {
358 return errMetricBlockLength
359 }
360 b.Received = rawPacket[0]&0x80 != 0
361 if !b.Received {
362 b.ECN = ECNNonECT
363 b.ArrivalTimeOffset = 0
364
365 return nil
366 }
367 b.ECN = ECN(rawPacket[0] >> 5 & 0x03)
368 b.ArrivalTimeOffset = binary.BigEndian.Uint16(rawPacket) & 0x1FFF
369
370 return nil
371 }