| 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 | "fmt" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // A ReceiverReport (RR) packet provides reception quality feedback for an RTP stream. |
| 13 | type ReceiverReport struct { |
| 14 | // The synchronization source identifier for the originator of this RR packet. |
| 15 | SSRC uint32 |
| 16 | // Zero or more reception report blocks depending on the number of other |
| 17 | // sources heard by this sender since the last report. Each reception report |
| 18 | // block conveys statistics on the reception of RTP packets from a |
| 19 | // single synchronization source. |
| 20 | Reports []ReceptionReport |
| 21 | // Extension contains additional, payload-specific information that needs to |
| 22 | // be reported regularly about the receiver. |
| 23 | ProfileExtensions []byte |
| 24 | } |
| 25 | |
| 26 | const ( |
| 27 | ssrcLength = 4 |
| 28 | rrSSRCOffset = headerLength |
| 29 | rrReportOffset = rrSSRCOffset + ssrcLength |
| 30 | ) |
| 31 | |
| 32 | // Marshal encodes the ReceiverReport in binary. |
| 33 | func (r ReceiverReport) Marshal() ([]byte, error) { |
| 34 | /* |
| 35 | * 0 1 2 3 |
| 36 | * 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 |
| 37 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | * header |V=2|P| RC | PT=RR=201 | length | |
| 39 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | * | SSRC of packet sender | |
| 41 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 42 | * report | SSRC_1 (SSRC of first source) | |
| 43 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | * 1 | fraction lost | cumulative number of packets lost | |
| 45 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | * | extended highest sequence number received | |
| 47 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | * | interarrival jitter | |
| 49 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | * | last SR (LSR) | |
| 51 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | * | delay since last SR (DLSR) | |
| 53 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 54 | * report | SSRC_2 (SSRC of second source) | |
| 55 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 56 | * 2 : ... : |
| 57 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 58 | * | profile-specific extensions | |
| 59 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | */ |
| 61 | |
| 62 | rawPacket := make([]byte, r.MarshalSize()) |
| 63 | packetBody := rawPacket[headerLength:] |
| 64 | |
| 65 | binary.BigEndian.PutUint32(packetBody, r.SSRC) |
| 66 | |
| 67 | for i, rp := range r.Reports { |
| 68 | data, err := rp.Marshal() |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | offset := ssrcLength + receptionReportLength*i |
| 73 | copy(packetBody[offset:], data) |
| 74 | } |
| 75 | |
| 76 | if len(r.Reports) > countMax { |
| 77 | return nil, errTooManyReports |
| 78 | } |
| 79 | |
| 80 | pe := make([]byte, len(r.ProfileExtensions)) |
| 81 | copy(pe, r.ProfileExtensions) |
| 82 | |
| 83 | // if the length of the profile extensions isn't devisible |
| 84 | // by 4, we need to pad the end. |
| 85 | for (len(pe) & 0x3) != 0 { |
| 86 | pe = append(pe, 0) //nolint:makezero |
| 87 | } |
| 88 | |
| 89 | rawPacket = append(rawPacket, pe...) //nolint:makezero |
| 90 | |
| 91 | hData, err := r.Header().Marshal() |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | copy(rawPacket, hData) |
| 96 | |
| 97 | return rawPacket, nil |
| 98 | } |
| 99 | |
| 100 | // Unmarshal decodes the ReceiverReport from binary. |
| 101 | func (r *ReceiverReport) Unmarshal(rawPacket []byte) error { |
| 102 | /* |
| 103 | * 0 1 2 3 |
| 104 | * 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 |
| 105 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 106 | * header |V=2|P| RC | PT=RR=201 | length | |
| 107 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 108 | * | SSRC of packet sender | |
| 109 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 110 | * report | SSRC_1 (SSRC of first source) | |
| 111 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 112 | * 1 | fraction lost | cumulative number of packets lost | |
| 113 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 114 | * | extended highest sequence number received | |
| 115 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 116 | * | interarrival jitter | |
| 117 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 118 | * | last SR (LSR) | |
| 119 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 120 | * | delay since last SR (DLSR) | |
| 121 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 122 | * report | SSRC_2 (SSRC of second source) | |
| 123 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 124 | * 2 : ... : |
| 125 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 126 | * | profile-specific extensions | |
| 127 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 128 | */ |
| 129 | |
| 130 | if len(rawPacket) < (headerLength + ssrcLength) { |
| 131 | return errPacketTooShort |
| 132 | } |
| 133 | |
| 134 | var header Header |
| 135 | if err := header.Unmarshal(rawPacket); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | if header.Type != TypeReceiverReport { |
| 140 | return errWrongType |
| 141 | } |
| 142 | |
| 143 | r.SSRC = binary.BigEndian.Uint32(rawPacket[rrSSRCOffset:]) |
| 144 | |
| 145 | for i := rrReportOffset; i < len(rawPacket) && len(r.Reports) < int(header.Count); i += receptionReportLength { |
| 146 | var rr ReceptionReport |
| 147 | if err := rr.Unmarshal(rawPacket[i:]); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | r.Reports = append(r.Reports, rr) |
| 151 | } |
| 152 | r.ProfileExtensions = rawPacket[rrReportOffset+(len(r.Reports)*receptionReportLength):] |
| 153 | |
| 154 | //nolint:gosec // G115 |
| 155 | if uint8(len(r.Reports)) != header.Count { |
| 156 | return errInvalidHeader |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // MarshalSize returns the size of the packet once marshaled. |
| 163 | func (r *ReceiverReport) MarshalSize() int { |
| 164 | repsLength := 0 |
| 165 | for _, rep := range r.Reports { |
| 166 | repsLength += rep.len() |
| 167 | } |
| 168 | |
| 169 | return headerLength + ssrcLength + repsLength |
| 170 | } |
| 171 | |
| 172 | // Header returns the Header associated with this packet. |
| 173 | func (r *ReceiverReport) Header() Header { |
| 174 | return Header{ |
| 175 | Count: uint8(len(r.Reports)), //nolint:gosec // G115 |
| 176 | Type: TypeReceiverReport, |
| 177 | Length: uint16((r.MarshalSize()/4)-1) + uint16(getPadding(len(r.ProfileExtensions))), //nolint:gosec // G115 |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 182 | func (r *ReceiverReport) DestinationSSRC() []uint32 { |
| 183 | out := make([]uint32, len(r.Reports)) |
| 184 | for i, v := range r.Reports { |
| 185 | out[i] = v.SSRC |
| 186 | } |
| 187 | |
| 188 | return out |
| 189 | } |
| 190 | |
| 191 | func (r ReceiverReport) String() string { |
| 192 | var out strings.Builder |
| 193 | fmt.Fprintf(&out, "ReceiverReport from %x\n", r.SSRC) |
| 194 | out.WriteString("\tSSRC \tLost\tLastSequence\n") |
| 195 | for _, i := range r.Reports { |
| 196 | fmt.Fprintf(&out, "\t%x\t%d/%d\t%d\n", i.SSRC, i.FractionLost, i.TotalLost, i.LastSequenceNumber) |
| 197 | } |
| 198 | fmt.Fprintf(&out, "\tProfile Extension Data: %v\n", r.ProfileExtensions) |
| 199 | |
| 200 | return out.String() |
| 201 | } |