| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | // Packet represents an RTCP packet, a protocol used for out-of-band statistics |
| 7 | // and control information for an RTP session. |
| 8 | type Packet interface { |
| 9 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 10 | DestinationSSRC() []uint32 |
| 11 | |
| 12 | Marshal() ([]byte, error) |
| 13 | Unmarshal(rawPacket []byte) error |
| 14 | MarshalSize() int |
| 15 | } |
| 16 | |
| 17 | // Unmarshal takes an entire udp datagram (which may consist of multiple RTCP packets) and |
| 18 | // returns the unmarshaled packets it contains. |
| 19 | // |
| 20 | // If this is a reduced-size RTCP packet a feedback packet (Goodbye, SliceLossIndication, etc) |
| 21 | // will be returned. Otherwise, the underlying type of the returned packet will be |
| 22 | // CompoundPacket. |
| 23 | func Unmarshal(rawData []byte) ([]Packet, error) { |
| 24 | var packets []Packet |
| 25 | for len(rawData) != 0 { |
| 26 | p, processed, err := unmarshal(rawData) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | packets = append(packets, p) |
| 32 | rawData = rawData[processed:] |
| 33 | } |
| 34 | |
| 35 | switch len(packets) { |
| 36 | // Empty packet |
| 37 | case 0: |
| 38 | return nil, errInvalidHeader |
| 39 | // Multiple Packets |
| 40 | default: |
| 41 | return packets, nil |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Marshal takes an array of Packets and serializes them to a single buffer. |
| 46 | func Marshal(packets []Packet) ([]byte, error) { |
| 47 | out := make([]byte, 0) |
| 48 | for _, p := range packets { |
| 49 | data, err := p.Marshal() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | out = append(out, data...) |
| 54 | } |
| 55 | |
| 56 | return out, nil |
| 57 | } |
| 58 | |
| 59 | // unmarshal is a factory which pulls the first RTCP packet from a bytestream, |
| 60 | // and returns it's parsed representation, and the amount of data that was processed. |
| 61 | // |
| 62 | //nolint:cyclop |
| 63 | func unmarshal(rawData []byte) (packet Packet, bytesprocessed int, err error) { |
| 64 | var header Header |
| 65 | |
| 66 | err = header.Unmarshal(rawData) |
| 67 | if err != nil { |
| 68 | return nil, 0, err |
| 69 | } |
| 70 | |
| 71 | bytesprocessed = (int(header.Length) + 1) * 4 |
| 72 | if bytesprocessed > len(rawData) { |
| 73 | return nil, 0, errPacketTooShort |
| 74 | } |
| 75 | inPacket := rawData[:bytesprocessed] |
| 76 | |
| 77 | switch header.Type { |
| 78 | case TypeSenderReport: |
| 79 | packet = new(SenderReport) |
| 80 | |
| 81 | case TypeReceiverReport: |
| 82 | packet = new(ReceiverReport) |
| 83 | |
| 84 | case TypeSourceDescription: |
| 85 | packet = new(SourceDescription) |
| 86 | |
| 87 | case TypeGoodbye: |
| 88 | packet = new(Goodbye) |
| 89 | |
| 90 | case TypeTransportSpecificFeedback: |
| 91 | switch header.Count { |
| 92 | case FormatTLN: |
| 93 | packet = new(TransportLayerNack) |
| 94 | case FormatRRR: |
| 95 | packet = new(RapidResynchronizationRequest) |
| 96 | case FormatTCC: |
| 97 | packet = new(TransportLayerCC) |
| 98 | case FormatCCFB: |
| 99 | packet = new(CCFeedbackReport) |
| 100 | default: |
| 101 | packet = new(RawPacket) |
| 102 | } |
| 103 | |
| 104 | case TypePayloadSpecificFeedback: |
| 105 | switch header.Count { |
| 106 | case FormatPLI: |
| 107 | packet = new(PictureLossIndication) |
| 108 | case FormatSLI: |
| 109 | packet = new(SliceLossIndication) |
| 110 | case FormatREMB: |
| 111 | packet = new(ReceiverEstimatedMaximumBitrate) |
| 112 | case FormatFIR: |
| 113 | packet = new(FullIntraRequest) |
| 114 | default: |
| 115 | packet = new(RawPacket) |
| 116 | } |
| 117 | |
| 118 | case TypeExtendedReport: |
| 119 | packet = new(ExtendedReport) |
| 120 | |
| 121 | case TypeApplicationDefined: |
| 122 | packet = new(ApplicationDefined) |
| 123 | |
| 124 | default: |
| 125 | packet = new(RawPacket) |
| 126 | } |
| 127 | |
| 128 | err = packet.Unmarshal(inPacket) |
| 129 | |
| 130 | return packet, bytesprocessed, err |
| 131 | } |