| 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 | ) |
| 9 | |
| 10 | // PacketType specifies the type of an RTCP packet. |
| 11 | type PacketType uint8 |
| 12 | |
| 13 | // RTCP packet types registered with IANA. See: |
| 14 | // |
| 15 | // https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-4 |
| 16 | const ( |
| 17 | TypeSenderReport PacketType = 200 // RFC 3550, 6.4.1 |
| 18 | TypeReceiverReport PacketType = 201 // RFC 3550, 6.4.2 |
| 19 | TypeSourceDescription PacketType = 202 // RFC 3550, 6.5 |
| 20 | TypeGoodbye PacketType = 203 // RFC 3550, 6.6 |
| 21 | TypeApplicationDefined PacketType = 204 // RFC 3550, 6.7 (unimplemented) |
| 22 | TypeTransportSpecificFeedback PacketType = 205 // RFC 4585, 6051 |
| 23 | TypePayloadSpecificFeedback PacketType = 206 // RFC 4585, 6.3 |
| 24 | TypeExtendedReport PacketType = 207 // RFC 3611 |
| 25 | |
| 26 | ) |
| 27 | |
| 28 | // Transport and Payload specific feedback messages overload the count field to act as a message type. |
| 29 | // those are listed here. |
| 30 | const ( |
| 31 | FormatSLI uint8 = 2 |
| 32 | FormatPLI uint8 = 1 |
| 33 | FormatFIR uint8 = 4 |
| 34 | FormatTLN uint8 = 1 |
| 35 | FormatRRR uint8 = 5 |
| 36 | FormatCCFB uint8 = 11 |
| 37 | FormatREMB uint8 = 15 |
| 38 | |
| 39 | // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01#page-5 |
| 40 | FormatTCC uint8 = 15 |
| 41 | ) |
| 42 | |
| 43 | func (p PacketType) String() string { |
| 44 | switch p { |
| 45 | case TypeSenderReport: |
| 46 | return "SR" |
| 47 | case TypeReceiverReport: |
| 48 | return "RR" |
| 49 | case TypeSourceDescription: |
| 50 | return "SDES" |
| 51 | case TypeGoodbye: |
| 52 | return "BYE" |
| 53 | case TypeApplicationDefined: |
| 54 | return "APP" |
| 55 | case TypeTransportSpecificFeedback: |
| 56 | return "TSFB" |
| 57 | case TypePayloadSpecificFeedback: |
| 58 | return "PSFB" |
| 59 | case TypeExtendedReport: |
| 60 | return "XR" |
| 61 | default: |
| 62 | return string(p) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const rtpVersion = 2 |
| 67 | |
| 68 | // A Header is the common header shared by all RTCP packets. |
| 69 | type Header struct { |
| 70 | // If the padding bit is set, this individual RTCP packet contains |
| 71 | // some additional padding octets at the end which are not part of |
| 72 | // the control information but are included in the length field. |
| 73 | Padding bool |
| 74 | // The number of reception reports, sources contained or FMT in this packet (depending on the Type) |
| 75 | Count uint8 |
| 76 | // The RTCP packet type for this packet |
| 77 | Type PacketType |
| 78 | // The length of this RTCP packet in 32-bit words minus one, |
| 79 | // including the header and any padding. |
| 80 | Length uint16 |
| 81 | } |
| 82 | |
| 83 | const ( |
| 84 | headerLength = 4 |
| 85 | versionShift = 6 |
| 86 | versionMask = 0x3 |
| 87 | paddingShift = 5 |
| 88 | paddingMask = 0x1 |
| 89 | countShift = 0 |
| 90 | countMask = 0x1f |
| 91 | countMax = (1 << 5) - 1 |
| 92 | ) |
| 93 | |
| 94 | // Marshal encodes the Header in binary. |
| 95 | func (h Header) Marshal() ([]byte, error) { |
| 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 | * |V=2|P| RC | PT=SR=200 | length | |
| 101 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 | */ |
| 103 | rawPacket := make([]byte, headerLength) |
| 104 | |
| 105 | rawPacket[0] |= rtpVersion << versionShift |
| 106 | |
| 107 | if h.Padding { |
| 108 | rawPacket[0] |= 1 << paddingShift |
| 109 | } |
| 110 | |
| 111 | if h.Count > 31 { |
| 112 | return nil, errInvalidHeader |
| 113 | } |
| 114 | rawPacket[0] |= h.Count << countShift //nolint:gosec // rawPacket is created with length headerLength (4) |
| 115 | |
| 116 | rawPacket[1] = uint8(h.Type) //nolint:gosec // rawPacket is created with length headerLength (4) |
| 117 | |
| 118 | binary.BigEndian.PutUint16(rawPacket[2:], h.Length) |
| 119 | |
| 120 | return rawPacket, nil |
| 121 | } |
| 122 | |
| 123 | // Unmarshal decodes the Header from binary. |
| 124 | func (h *Header) Unmarshal(rawPacket []byte) error { |
| 125 | if len(rawPacket) < headerLength { |
| 126 | return errPacketTooShort |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * 0 1 2 3 |
| 131 | * 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 |
| 132 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 133 | * |V=2|P| RC | PT | length | |
| 134 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 135 | */ |
| 136 | |
| 137 | version := rawPacket[0] >> versionShift & versionMask |
| 138 | if version != rtpVersion { |
| 139 | return errBadVersion |
| 140 | } |
| 141 | |
| 142 | h.Padding = (rawPacket[0] >> paddingShift & paddingMask) > 0 |
| 143 | h.Count = rawPacket[0] >> countShift & countMask |
| 144 | |
| 145 | h.Type = PacketType(rawPacket[1]) |
| 146 | |
| 147 | h.Length = binary.BigEndian.Uint16(rawPacket[2:]) |
| 148 | |
| 149 | return nil |
| 150 | } |