| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | ) |
| 9 | |
| 10 | // The ExtendedReport packet is an Implementation of RTCP Extended |
| 11 | // Reports defined in RFC 3611. It is used to convey detailed |
| 12 | // information about an RTP stream. Each packet contains one or |
| 13 | // more report blocks, each of which conveys a different kind of |
| 14 | // information. |
| 15 | // |
| 16 | // 0 1 2 3 |
| 17 | // 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 |
| 18 | // |
| 19 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 20 | // |V=2|P|reserved | PT=XR=207 | length | |
| 21 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 22 | // | SSRC | |
| 23 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 24 | // : report blocks : |
| 25 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 26 | // . |
| 27 | type ExtendedReport struct { |
| 28 | SenderSSRC uint32 `fmt:"0x%X"` |
| 29 | Reports []ReportBlock |
| 30 | } |
| 31 | |
| 32 | // ReportBlock represents a single report within an ExtendedReport |
| 33 | // packet. |
| 34 | type ReportBlock interface { |
| 35 | DestinationSSRC() []uint32 |
| 36 | setupBlockHeader() |
| 37 | unpackBlockHeader() |
| 38 | } |
| 39 | |
| 40 | // TypeSpecificField as described in RFC 3611 section 4.5. In typical |
| 41 | // cases, users of ExtendedReports shouldn't need to access this, |
| 42 | // and should instead use the corresponding fields in the actual |
| 43 | // report blocks themselves. |
| 44 | type TypeSpecificField uint8 |
| 45 | |
| 46 | // XRHeader defines the common fields that must appear at the start |
| 47 | // of each report block. In typical cases, users of ExtendedReports |
| 48 | // shouldn't need to access this. For locally-constructed report |
| 49 | // blocks, these values will not be accurate until the corresponding |
| 50 | // packet is marshaled. |
| 51 | type XRHeader struct { |
| 52 | BlockType BlockTypeType |
| 53 | TypeSpecific TypeSpecificField `fmt:"0x%X"` |
| 54 | BlockLength uint16 |
| 55 | } |
| 56 | |
| 57 | // BlockTypeType specifies the type of report in a report block. |
| 58 | type BlockTypeType uint8 |
| 59 | |
| 60 | // Extended Report block types from RFC 3611. |
| 61 | const ( |
| 62 | LossRLEReportBlockType = 1 // RFC 3611, section 4.1 |
| 63 | DuplicateRLEReportBlockType = 2 // RFC 3611, section 4.2 |
| 64 | PacketReceiptTimesReportBlockType = 3 // RFC 3611, section 4.3 |
| 65 | ReceiverReferenceTimeReportBlockType = 4 // RFC 3611, section 4.4 |
| 66 | DLRRReportBlockType = 5 // RFC 3611, section 4.5 |
| 67 | StatisticsSummaryReportBlockType = 6 // RFC 3611, section 4.6 |
| 68 | VoIPMetricsReportBlockType = 7 // RFC 3611, section 4.7 |
| 69 | ) |
| 70 | |
| 71 | // String converts the Extended report block types into readable strings. |
| 72 | func (t BlockTypeType) String() string { |
| 73 | switch t { |
| 74 | case LossRLEReportBlockType: |
| 75 | return "LossRLEReportBlockType" |
| 76 | case DuplicateRLEReportBlockType: |
| 77 | return "DuplicateRLEReportBlockType" |
| 78 | case PacketReceiptTimesReportBlockType: |
| 79 | return "PacketReceiptTimesReportBlockType" |
| 80 | case ReceiverReferenceTimeReportBlockType: |
| 81 | return "ReceiverReferenceTimeReportBlockType" |
| 82 | case DLRRReportBlockType: |
| 83 | return "DLRRReportBlockType" |
| 84 | case StatisticsSummaryReportBlockType: |
| 85 | return "StatisticsSummaryReportBlockType" |
| 86 | case VoIPMetricsReportBlockType: |
| 87 | return "VoIPMetricsReportBlockType" |
| 88 | } |
| 89 | |
| 90 | return fmt.Sprintf("invalid value %d", t) |
| 91 | } |
| 92 | |
| 93 | // rleReportBlock defines the common structure used by both |
| 94 | // Loss RLE report blocks (RFC 3611 §4.1) and Duplicate RLE |
| 95 | // report blocks (RFC 3611 §4.2). |
| 96 | // |
| 97 | // 0 1 2 3 |
| 98 | // 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 |
| 99 | // |
| 100 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 101 | // | BT = 1 or 2 | rsvd. | T | block length | |
| 102 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 103 | // | SSRC of source | |
| 104 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | // | begin_seq | end_seq | |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // | chunk 1 | chunk 2 | |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // : ... : |
| 110 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | // | chunk n-1 | chunk n | |
| 112 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | // . |
| 114 | type rleReportBlock struct { |
| 115 | XRHeader |
| 116 | T uint8 `encoding:"omit"` |
| 117 | SSRC uint32 `fmt:"0x%X"` |
| 118 | BeginSeq uint16 |
| 119 | EndSeq uint16 |
| 120 | Chunks []Chunk |
| 121 | } |
| 122 | |
| 123 | // Chunk as defined in RFC 3611, section 4.1. These represent information |
| 124 | // about packet losses and packet duplication. They have three representations: |
| 125 | // |
| 126 | // Run Length Chunk: |
| 127 | // |
| 128 | // 0 1 |
| 129 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 130 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 131 | // |C|R| run length | |
| 132 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 133 | // |
| 134 | // Bit Vector Chunk: |
| 135 | // |
| 136 | // 0 1 |
| 137 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 138 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 139 | // |C| bit vector | |
| 140 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 141 | // |
| 142 | // Terminating Null Chunk: |
| 143 | // |
| 144 | // 0 1 |
| 145 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 146 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 147 | // |0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0| |
| 148 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 149 | type Chunk uint16 |
| 150 | |
| 151 | // LossRLEReportBlock is used to report information about packet |
| 152 | // losses, as described in RFC 3611, section 4.1. |
| 153 | type LossRLEReportBlock rleReportBlock |
| 154 | |
| 155 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 156 | func (b *LossRLEReportBlock) DestinationSSRC() []uint32 { |
| 157 | return []uint32{b.SSRC} |
| 158 | } |
| 159 | |
| 160 | func (b *LossRLEReportBlock) setupBlockHeader() { |
| 161 | b.XRHeader.BlockType = LossRLEReportBlockType |
| 162 | b.XRHeader.TypeSpecific = TypeSpecificField(b.T & 0x0F) |
| 163 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 164 | } |
| 165 | |
| 166 | func (b *LossRLEReportBlock) unpackBlockHeader() { |
| 167 | b.T = uint8(b.XRHeader.TypeSpecific) & 0x0F |
| 168 | } |
| 169 | |
| 170 | // DuplicateRLEReportBlock is used to report information about packet |
| 171 | // duplication, as described in RFC 3611, section 4.1. |
| 172 | type DuplicateRLEReportBlock rleReportBlock |
| 173 | |
| 174 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 175 | func (b *DuplicateRLEReportBlock) DestinationSSRC() []uint32 { |
| 176 | return []uint32{b.SSRC} |
| 177 | } |
| 178 | |
| 179 | func (b *DuplicateRLEReportBlock) setupBlockHeader() { |
| 180 | b.XRHeader.BlockType = DuplicateRLEReportBlockType |
| 181 | b.XRHeader.TypeSpecific = TypeSpecificField(b.T & 0x0F) |
| 182 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 183 | } |
| 184 | |
| 185 | func (b *DuplicateRLEReportBlock) unpackBlockHeader() { |
| 186 | b.T = uint8(b.XRHeader.TypeSpecific) & 0x0F |
| 187 | } |
| 188 | |
| 189 | // ChunkType enumerates the three kinds of chunks described in RFC 3611 section 4.1. |
| 190 | type ChunkType uint8 |
| 191 | |
| 192 | // These are the valid values that ChunkType can assume. |
| 193 | const ( |
| 194 | RunLengthChunkType = 0 |
| 195 | BitVectorChunkType = 1 |
| 196 | TerminatingNullChunkType = 2 |
| 197 | ) |
| 198 | |
| 199 | func (c Chunk) String() string { |
| 200 | switch c.Type() { |
| 201 | case RunLengthChunkType: |
| 202 | runType, _ := c.RunType() |
| 203 | |
| 204 | return fmt.Sprintf("[RunLength type=%d, length=%d]", runType, c.Value()) |
| 205 | case BitVectorChunkType: |
| 206 | return fmt.Sprintf("[BitVector 0b%015b]", c.Value()) |
| 207 | case TerminatingNullChunkType: |
| 208 | return "[TerminatingNull]" |
| 209 | } |
| 210 | |
| 211 | return fmt.Sprintf("[0x%X]", uint16(c)) |
| 212 | } |
| 213 | |
| 214 | // Type returns the ChunkType that this Chunk represents. |
| 215 | func (c Chunk) Type() ChunkType { |
| 216 | if c == 0 { |
| 217 | return TerminatingNullChunkType |
| 218 | } |
| 219 | |
| 220 | return ChunkType(c >> 15) //nolint:gosec // G115 |
| 221 | } |
| 222 | |
| 223 | // RunType returns the RunType that this Chunk represents. It is |
| 224 | // only valid if ChunkType is RunLengthChunkType. |
| 225 | func (c Chunk) RunType() (uint, error) { |
| 226 | if c.Type() != RunLengthChunkType { |
| 227 | return 0, errWrongChunkType |
| 228 | } |
| 229 | |
| 230 | return uint((c >> 14) & 0x01), nil |
| 231 | } |
| 232 | |
| 233 | // Value returns the value represented in this Chunk. |
| 234 | func (c Chunk) Value() uint { |
| 235 | switch c.Type() { |
| 236 | case RunLengthChunkType: |
| 237 | return uint(c & 0x3FFF) |
| 238 | case BitVectorChunkType: |
| 239 | return uint(c & 0x7FFF) |
| 240 | case TerminatingNullChunkType: |
| 241 | return 0 |
| 242 | } |
| 243 | |
| 244 | return uint(c) |
| 245 | } |
| 246 | |
| 247 | // PacketReceiptTimesReportBlock represents a Packet Receipt Times |
| 248 | // report block, as described in RFC 3611 section 4.3. |
| 249 | // |
| 250 | // 0 1 2 3 |
| 251 | // 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 |
| 252 | // |
| 253 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 254 | // | BT=3 | rsvd. | T | block length | |
| 255 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 256 | // | SSRC of source | |
| 257 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 258 | // | begin_seq | end_seq | |
| 259 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 260 | // | Receipt time of packet begin_seq | |
| 261 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 262 | // | Receipt time of packet (begin_seq + 1) mod 65536 | |
| 263 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 264 | // : ... : |
| 265 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 266 | // | Receipt time of packet (end_seq - 1) mod 65536 | |
| 267 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 268 | // . |
| 269 | type PacketReceiptTimesReportBlock struct { |
| 270 | XRHeader |
| 271 | T uint8 `encoding:"omit"` |
| 272 | SSRC uint32 `fmt:"0x%X"` |
| 273 | BeginSeq uint16 |
| 274 | EndSeq uint16 |
| 275 | ReceiptTime []uint32 |
| 276 | } |
| 277 | |
| 278 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 279 | func (b *PacketReceiptTimesReportBlock) DestinationSSRC() []uint32 { |
| 280 | return []uint32{b.SSRC} |
| 281 | } |
| 282 | |
| 283 | func (b *PacketReceiptTimesReportBlock) setupBlockHeader() { |
| 284 | b.XRHeader.BlockType = PacketReceiptTimesReportBlockType |
| 285 | b.XRHeader.TypeSpecific = TypeSpecificField(b.T & 0x0F) |
| 286 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 287 | } |
| 288 | |
| 289 | func (b *PacketReceiptTimesReportBlock) unpackBlockHeader() { |
| 290 | b.T = uint8(b.XRHeader.TypeSpecific) & 0x0F |
| 291 | } |
| 292 | |
| 293 | // ReceiverReferenceTimeReportBlock encodes a Receiver Reference Time |
| 294 | // report block as described in RFC 3611 section 4.4. |
| 295 | // |
| 296 | // 0 1 2 3 |
| 297 | // 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 |
| 298 | // |
| 299 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 300 | // | BT=4 | reserved | block length = 2 | |
| 301 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 302 | // | NTP timestamp, most significant word | |
| 303 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 304 | // | NTP timestamp, least significant word | |
| 305 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 306 | // . |
| 307 | type ReceiverReferenceTimeReportBlock struct { |
| 308 | XRHeader |
| 309 | NTPTimestamp uint64 |
| 310 | } |
| 311 | |
| 312 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 313 | func (b *ReceiverReferenceTimeReportBlock) DestinationSSRC() []uint32 { |
| 314 | return []uint32{} |
| 315 | } |
| 316 | |
| 317 | func (b *ReceiverReferenceTimeReportBlock) setupBlockHeader() { |
| 318 | b.XRHeader.BlockType = ReceiverReferenceTimeReportBlockType |
| 319 | b.XRHeader.TypeSpecific = 0 |
| 320 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 321 | } |
| 322 | |
| 323 | func (b *ReceiverReferenceTimeReportBlock) unpackBlockHeader() { |
| 324 | } |
| 325 | |
| 326 | // DLRRReportBlock encodes a DLRR Report Block as described in |
| 327 | // RFC 3611 section 4.5. |
| 328 | // |
| 329 | // 0 1 2 3 |
| 330 | // 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 |
| 331 | // |
| 332 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 333 | // | BT=5 | reserved | block length | |
| 334 | // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 335 | // | SSRC_1 (SSRC of first receiver) | sub- |
| 336 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block |
| 337 | // | last RR (LRR) | 1 |
| 338 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 339 | // | delay since last RR (DLRR) | |
| 340 | // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 341 | // | SSRC_2 (SSRC of second receiver) | sub- |
| 342 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block |
| 343 | // : ... : 2 |
| 344 | // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 345 | // . |
| 346 | type DLRRReportBlock struct { |
| 347 | XRHeader |
| 348 | Reports []DLRRReport |
| 349 | } |
| 350 | |
| 351 | // DLRRReport encodes a single report inside a DLRRReportBlock. |
| 352 | type DLRRReport struct { |
| 353 | SSRC uint32 `fmt:"0x%X"` |
| 354 | LastRR uint32 |
| 355 | DLRR uint32 |
| 356 | } |
| 357 | |
| 358 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 359 | func (b *DLRRReportBlock) DestinationSSRC() []uint32 { |
| 360 | ssrc := make([]uint32, len(b.Reports)) |
| 361 | for i, r := range b.Reports { |
| 362 | ssrc[i] = r.SSRC |
| 363 | } |
| 364 | |
| 365 | return ssrc |
| 366 | } |
| 367 | |
| 368 | func (b *DLRRReportBlock) setupBlockHeader() { |
| 369 | b.XRHeader.BlockType = DLRRReportBlockType |
| 370 | b.XRHeader.TypeSpecific = 0 |
| 371 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 372 | } |
| 373 | |
| 374 | func (b *DLRRReportBlock) unpackBlockHeader() { |
| 375 | } |
| 376 | |
| 377 | // StatisticsSummaryReportBlock encodes a Statistics Summary Report |
| 378 | // Block as described in RFC 3611, section 4.6. |
| 379 | // |
| 380 | // 0 1 2 3 |
| 381 | // 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 |
| 382 | // |
| 383 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 384 | // | BT=6 |L|D|J|ToH|rsvd.| block length = 9 | |
| 385 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 386 | // | SSRC of source | |
| 387 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 388 | // | begin_seq | end_seq | |
| 389 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 390 | // | lost_packets | |
| 391 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 392 | // | dup_packets | |
| 393 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 394 | // | min_jitter | |
| 395 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 396 | // | max_jitter | |
| 397 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 398 | // | mean_jitter | |
| 399 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 400 | // | dev_jitter | |
| 401 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 402 | // | min_ttl_or_hl | max_ttl_or_hl |mean_ttl_or_hl | dev_ttl_or_hl | |
| 403 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 404 | // . |
| 405 | type StatisticsSummaryReportBlock struct { |
| 406 | XRHeader |
| 407 | LossReports bool `encoding:"omit"` |
| 408 | DuplicateReports bool `encoding:"omit"` |
| 409 | JitterReports bool `encoding:"omit"` |
| 410 | TTLorHopLimit TTLorHopLimitType `encoding:"omit"` |
| 411 | SSRC uint32 `fmt:"0x%X"` |
| 412 | BeginSeq uint16 |
| 413 | EndSeq uint16 |
| 414 | LostPackets uint32 |
| 415 | DupPackets uint32 |
| 416 | MinJitter uint32 |
| 417 | MaxJitter uint32 |
| 418 | MeanJitter uint32 |
| 419 | DevJitter uint32 |
| 420 | MinTTLOrHL uint8 |
| 421 | MaxTTLOrHL uint8 |
| 422 | MeanTTLOrHL uint8 |
| 423 | DevTTLOrHL uint8 |
| 424 | } |
| 425 | |
| 426 | // TTLorHopLimitType encodes values for the ToH field in |
| 427 | // a StatisticsSummaryReportBlock. |
| 428 | type TTLorHopLimitType uint8 |
| 429 | |
| 430 | // Values for TTLorHopLimitType. |
| 431 | const ( |
| 432 | ToHMissing = 0 |
| 433 | ToHIPv4 = 1 |
| 434 | ToHIPv6 = 2 |
| 435 | ) |
| 436 | |
| 437 | func (t TTLorHopLimitType) String() string { |
| 438 | switch t { |
| 439 | case ToHMissing: |
| 440 | return "[ToH Missing]" |
| 441 | case ToHIPv4: |
| 442 | return "[ToH = IPv4]" |
| 443 | case ToHIPv6: |
| 444 | return "[ToH = IPv6]" |
| 445 | } |
| 446 | |
| 447 | return "[ToH Flag is Invalid]" |
| 448 | } |
| 449 | |
| 450 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 451 | func (b *StatisticsSummaryReportBlock) DestinationSSRC() []uint32 { |
| 452 | return []uint32{b.SSRC} |
| 453 | } |
| 454 | |
| 455 | func (b *StatisticsSummaryReportBlock) setupBlockHeader() { |
| 456 | b.XRHeader.BlockType = StatisticsSummaryReportBlockType |
| 457 | b.XRHeader.TypeSpecific = 0x00 |
| 458 | if b.LossReports { |
| 459 | b.XRHeader.TypeSpecific |= 0x80 |
| 460 | } |
| 461 | if b.DuplicateReports { |
| 462 | b.XRHeader.TypeSpecific |= 0x40 |
| 463 | } |
| 464 | if b.JitterReports { |
| 465 | b.XRHeader.TypeSpecific |= 0x20 |
| 466 | } |
| 467 | b.XRHeader.TypeSpecific |= TypeSpecificField((b.TTLorHopLimit & 0x03) << 3) |
| 468 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 469 | } |
| 470 | |
| 471 | func (b *StatisticsSummaryReportBlock) unpackBlockHeader() { |
| 472 | b.LossReports = b.XRHeader.TypeSpecific&0x80 != 0 |
| 473 | b.DuplicateReports = b.XRHeader.TypeSpecific&0x40 != 0 |
| 474 | b.JitterReports = b.XRHeader.TypeSpecific&0x20 != 0 |
| 475 | b.TTLorHopLimit = TTLorHopLimitType((b.XRHeader.TypeSpecific & 0x18) >> 3) |
| 476 | } |
| 477 | |
| 478 | // VoIPMetricsReportBlock encodes a VoIP Metrics Report Block as described |
| 479 | // in RFC 3611, section 4.7. |
| 480 | // |
| 481 | // 0 1 2 3 |
| 482 | // 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 |
| 483 | // |
| 484 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 485 | // | BT=7 | reserved | block length = 8 | |
| 486 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 487 | // | SSRC of source | |
| 488 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 489 | // | loss rate | discard rate | burst density | gap density | |
| 490 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 491 | // | burst duration | gap duration | |
| 492 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 493 | // | round trip delay | end system delay | |
| 494 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 495 | // | signal level | noise level | RERL | Gmin | |
| 496 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 497 | // | R factor | ext. R factor | MOS-LQ | MOS-CQ | |
| 498 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 499 | // | RX config | reserved | JB nominal | |
| 500 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 501 | // | JB maximum | JB abs max | |
| 502 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 503 | // . |
| 504 | type VoIPMetricsReportBlock struct { |
| 505 | XRHeader |
| 506 | SSRC uint32 `fmt:"0x%X"` |
| 507 | LossRate uint8 |
| 508 | DiscardRate uint8 |
| 509 | BurstDensity uint8 |
| 510 | GapDensity uint8 |
| 511 | BurstDuration uint16 |
| 512 | GapDuration uint16 |
| 513 | RoundTripDelay uint16 |
| 514 | EndSystemDelay uint16 |
| 515 | SignalLevel uint8 |
| 516 | NoiseLevel uint8 |
| 517 | RERL uint8 |
| 518 | Gmin uint8 |
| 519 | RFactor uint8 |
| 520 | ExtRFactor uint8 |
| 521 | MOSLQ uint8 |
| 522 | MOSCQ uint8 |
| 523 | RXConfig uint8 |
| 524 | _ uint8 |
| 525 | JBNominal uint16 |
| 526 | JBMaximum uint16 |
| 527 | JBAbsMax uint16 |
| 528 | } |
| 529 | |
| 530 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 531 | func (b *VoIPMetricsReportBlock) DestinationSSRC() []uint32 { |
| 532 | return []uint32{b.SSRC} |
| 533 | } |
| 534 | |
| 535 | func (b *VoIPMetricsReportBlock) setupBlockHeader() { |
| 536 | b.XRHeader.BlockType = VoIPMetricsReportBlockType |
| 537 | b.XRHeader.TypeSpecific = 0 |
| 538 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 539 | } |
| 540 | |
| 541 | func (b *VoIPMetricsReportBlock) unpackBlockHeader() { |
| 542 | } |
| 543 | |
| 544 | // UnknownReportBlock is used to store bytes for any report block |
| 545 | // that has an unknown Report Block Type. |
| 546 | type UnknownReportBlock struct { |
| 547 | XRHeader |
| 548 | Bytes []byte |
| 549 | } |
| 550 | |
| 551 | // DestinationSSRC returns an array of SSRC values that this report block refers to. |
| 552 | func (b *UnknownReportBlock) DestinationSSRC() []uint32 { |
| 553 | return []uint32{} |
| 554 | } |
| 555 | |
| 556 | func (b *UnknownReportBlock) setupBlockHeader() { |
| 557 | b.XRHeader.BlockLength = uint16(wireSize(b)/4 - 1) //nolint:gosec // G115 |
| 558 | } |
| 559 | |
| 560 | func (b *UnknownReportBlock) unpackBlockHeader() { |
| 561 | } |
| 562 | |
| 563 | // MarshalSize returns the size of the packet once marshaled. |
| 564 | func (x ExtendedReport) MarshalSize() int { |
| 565 | return wireSize(x) |
| 566 | } |
| 567 | |
| 568 | // Marshal encodes the ExtendedReport in binary. |
| 569 | func (x ExtendedReport) Marshal() ([]byte, error) { |
| 570 | for _, p := range x.Reports { |
| 571 | p.setupBlockHeader() |
| 572 | } |
| 573 | |
| 574 | length := wireSize(x) |
| 575 | |
| 576 | // RTCP Header |
| 577 | header := Header{ |
| 578 | Type: TypeExtendedReport, |
| 579 | Length: uint16(length / 4), //nolint:gosec // G115 |
| 580 | } |
| 581 | headerBuffer, err := header.Marshal() |
| 582 | if err != nil { |
| 583 | return []byte{}, err |
| 584 | } |
| 585 | length += len(headerBuffer) |
| 586 | |
| 587 | rawPacket := make([]byte, length) |
| 588 | buffer := packetBuffer{bytes: rawPacket} |
| 589 | |
| 590 | err = buffer.write(headerBuffer) |
| 591 | if err != nil { |
| 592 | return []byte{}, err |
| 593 | } |
| 594 | err = buffer.write(x) |
| 595 | if err != nil { |
| 596 | return []byte{}, err |
| 597 | } |
| 598 | |
| 599 | return rawPacket, nil |
| 600 | } |
| 601 | |
| 602 | // Unmarshal decodes the ExtendedReport from binary. |
| 603 | // |
| 604 | //nolint:cyclop |
| 605 | func (x *ExtendedReport) Unmarshal(b []byte) error { |
| 606 | var header Header |
| 607 | if err := header.Unmarshal(b); err != nil { |
| 608 | return err |
| 609 | } |
| 610 | if header.Type != TypeExtendedReport { |
| 611 | return errWrongType |
| 612 | } |
| 613 | |
| 614 | buffer := packetBuffer{bytes: b[headerLength:]} |
| 615 | err := buffer.read(&x.SenderSSRC) |
| 616 | if err != nil { |
| 617 | return err |
| 618 | } |
| 619 | |
| 620 | for len(buffer.bytes) > 0 { |
| 621 | var block ReportBlock |
| 622 | |
| 623 | headerBuffer := buffer |
| 624 | xrHeader := XRHeader{} |
| 625 | err = headerBuffer.read(&xrHeader) |
| 626 | if err != nil { |
| 627 | return err |
| 628 | } |
| 629 | |
| 630 | switch xrHeader.BlockType { |
| 631 | case LossRLEReportBlockType: |
| 632 | block = new(LossRLEReportBlock) |
| 633 | case DuplicateRLEReportBlockType: |
| 634 | block = new(DuplicateRLEReportBlock) |
| 635 | case PacketReceiptTimesReportBlockType: |
| 636 | block = new(PacketReceiptTimesReportBlock) |
| 637 | case ReceiverReferenceTimeReportBlockType: |
| 638 | block = new(ReceiverReferenceTimeReportBlock) |
| 639 | case DLRRReportBlockType: |
| 640 | block = new(DLRRReportBlock) |
| 641 | case StatisticsSummaryReportBlockType: |
| 642 | block = new(StatisticsSummaryReportBlock) |
| 643 | case VoIPMetricsReportBlockType: |
| 644 | block = new(VoIPMetricsReportBlock) |
| 645 | default: |
| 646 | block = new(UnknownReportBlock) |
| 647 | } |
| 648 | |
| 649 | // We need to limit the amount of data available to |
| 650 | // this block to the actual length of the block |
| 651 | blockLength := (int(xrHeader.BlockLength) + 1) * 4 |
| 652 | blockBuffer := buffer.split(blockLength) |
| 653 | err = blockBuffer.read(block) |
| 654 | if err != nil { |
| 655 | return err |
| 656 | } |
| 657 | block.unpackBlockHeader() |
| 658 | x.Reports = append(x.Reports, block) |
| 659 | } |
| 660 | |
| 661 | return nil |
| 662 | } |
| 663 | |
| 664 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 665 | func (x *ExtendedReport) DestinationSSRC() []uint32 { |
| 666 | ssrc := make([]uint32, 0, len(x.Reports)+1) |
| 667 | ssrc = append(ssrc, x.SenderSSRC) |
| 668 | for _, p := range x.Reports { |
| 669 | ssrc = append(ssrc, p.DestinationSSRC()...) |
| 670 | } |
| 671 | |
| 672 | return ssrc |
| 673 | } |
| 674 | |
| 675 | func (x *ExtendedReport) String() string { |
| 676 | return stringify(x) |
| 677 | } |