| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcpscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ |
| 5 | |
| 6 | import ( |
| 7 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/" |
| 8 | ) |
| 9 | |
| 10 | // PacketType specifies the type of an RTCP packet. |
| 11 | type PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 | TypeSenderReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeSenderReport. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 200 // RFC 3550, 6.4.1 |
| 18 | TypeReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeReceiverReport. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 201 // RFC 3550, 6.4.2 |
| 19 | TypeSourceDescriptionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeSourceDescription. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 202 // RFC 3550, 6.5 |
| 20 | TypeGoodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeGoodbye. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 203 // RFC 3550, 6.6 |
| 21 | TypeApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeApplicationDefined. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 204 // RFC 3550, 6.7 (unimplemented) |
| 22 | TypeTransportSpecificFeedbackscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeTransportSpecificFeedback. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 205 // RFC 4585, 6051 |
| 23 | TypePayloadSpecificFeedbackscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypePayloadSpecificFeedback. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# = 206 // RFC 4585, 6.3 |
| 24 | TypeExtendedReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeExtendedReport. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 | FormatSLIscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatSLI. uint8 = 2 |
| 32 | FormatPLIscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatPLI. uint8 = 1 |
| 33 | FormatFIRscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatFIR. uint8 = 4 |
| 34 | FormatTLNscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatTLN. uint8 = 1 |
| 35 | FormatRRRscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatRRR. uint8 = 5 |
| 36 | FormatCCFBscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatCCFB. uint8 = 11 |
| 37 | FormatREMBscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatREMB. uint8 = 15 |
| 38 | |
| 39 | // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01#page-5 |
| 40 | FormatTCCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatTCC. uint8 = 15 |
| 41 | ) |
| 42 | |
| 43 | func (p PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType#) Stringscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType#String().() string { |
| 44 | switch p { |
| 45 | case TypeSenderReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeSenderReport.: |
| 46 | return "SR" |
| 47 | case TypeReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeReceiverReport.: |
| 48 | return "RR" |
| 49 | case TypeSourceDescriptionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeSourceDescription.: |
| 50 | return "SDES" |
| 51 | case TypeGoodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeGoodbye.: |
| 52 | return "BYE" |
| 53 | case TypeApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeApplicationDefined.: |
| 54 | return "APP" |
| 55 | case TypeTransportSpecificFeedbackscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeTransportSpecificFeedback.: |
| 56 | return "TSFB" |
| 57 | case TypePayloadSpecificFeedbackscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypePayloadSpecificFeedback.: |
| 58 | return "PSFB" |
| 59 | case TypeExtendedReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeExtendedReport.: |
| 60 | return "XR" |
| 61 | default: |
| 62 | return string(p) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const rtpVersionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rtpVersion. = 2 |
| 67 | |
| 68 | // A Header is the common header shared by all RTCP packets. |
| 69 | type Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 | Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding. bool |
| 74 | // The number of reception reports, sources contained or FMT in this packet (depending on the Type) |
| 75 | Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count. uint8 |
| 76 | // The RTCP packet type for this packet |
| 77 | Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type. PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType# |
| 78 | // The length of this RTCP packet in 32-bit words minus one, |
| 79 | // including the header and any padding. |
| 80 | Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length. uint16 |
| 81 | } |
| 82 | |
| 83 | const ( |
| 84 | headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. = 4 |
| 85 | versionShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/versionShift. = 6 |
| 86 | versionMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/versionMask. = 0x3 |
| 87 | paddingShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/paddingShift. = 5 |
| 88 | paddingMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/paddingMask. = 0x1 |
| 89 | countShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countShift. = 0 |
| 90 | countMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countMask. = 0x1f |
| 91 | countMaxscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countMax. = (1 << 5) - 1 |
| 92 | ) |
| 93 | |
| 94 | // Marshal encodes the Header in binary. |
| 95 | func (h Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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, headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength.) |
| 104 | |
| 105 | rawPacket[0] |= rtpVersionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rtpVersion. << versionShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/versionShift. |
| 106 | |
| 107 | if h.Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding. { |
| 108 | rawPacket[0] |= 1 << paddingShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/paddingShift. |
| 109 | } |
| 110 | |
| 111 | if h.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count. > 31 { |
| 112 | return nil, errInvalidHeaderscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errInvalidHeader. |
| 113 | } |
| 114 | rawPacket[0] |= h.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count. << countShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countShift. //nolint:gosec // rawPacket is created with length headerLength (4) |
| 115 | |
| 116 | rawPacket[1] = uint8(h.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type.) //nolint:gosec // rawPacket is created with length headerLength (4) |
| 117 | |
| 118 | binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#PutUint16().(rawPacket[2:], h.Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.) |
| 119 | |
| 120 | return rawPacket, nil |
| 121 | } |
| 122 | |
| 123 | // Unmarshal decodes the Header from binary. |
| 124 | func (h *Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Unmarshal().(rawPacket []byte) error { |
| 125 | if len(rawPacket) < headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. { |
| 126 | return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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] >> versionShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/versionShift. & versionMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/versionMask. |
| 138 | if version != rtpVersionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rtpVersion. { |
| 139 | return errBadVersionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errBadVersion. |
| 140 | } |
| 141 | |
| 142 | h.Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding. = (rawPacket[0] >> paddingShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/paddingShift. & paddingMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/paddingMask.) > 0 |
| 143 | h.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count. = rawPacket[0] >> countShiftscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countShift. & countMaskscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countMask. |
| 144 | |
| 145 | h.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type. = PacketTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/PacketType#(rawPacket[1]) |
| 146 | |
| 147 | h.Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length. = binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#Uint16().(rawPacket[2:]) |
| 148 | |
| 149 | return nil |
| 150 | } |