| 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 SenderReport (SR) packet provides reception quality feedback for an RTP stream. |
| 13 | type SenderReport struct { |
| 14 | // The synchronization source identifier for the originator of this SR packet. |
| 15 | SSRC uint32 |
| 16 | // The wallclock time when this report was sent so that it may be used in |
| 17 | // combination with timestamps returned in reception reports from other |
| 18 | // receivers to measure round-trip propagation to those receivers. |
| 19 | NTPTime uint64 |
| 20 | // Corresponds to the same time as the NTP timestamp (above), but in |
| 21 | // the same units and with the same random offset as the RTP |
| 22 | // timestamps in data packets. This correspondence may be used for |
| 23 | // intra- and inter-media synchronization for sources whose NTP |
| 24 | // timestamps are synchronized, and may be used by media-independent |
| 25 | // receivers to estimate the nominal RTP clock frequency. |
| 26 | RTPTime uint32 |
| 27 | // The total number of RTP data packets transmitted by the sender |
| 28 | // since starting transmission up until the time this SR packet was |
| 29 | // generated. |
| 30 | PacketCount uint32 |
| 31 | // The total number of payload octets (i.e., not including header or |
| 32 | // padding) transmitted in RTP data packets by the sender since |
| 33 | // starting transmission up until the time this SR packet was |
| 34 | // generated. |
| 35 | OctetCount uint32 |
| 36 | // Zero or more reception report blocks depending on the number of other |
| 37 | // sources heard by this sender since the last report. Each reception report |
| 38 | // block conveys statistics on the reception of RTP packets from a |
| 39 | // single synchronization source. |
| 40 | Reports []ReceptionReport |
| 41 | // ProfileExtensions contains additional, payload-specific information that needs to |
| 42 | // be reported regularly about the sender. |
| 43 | ProfileExtensions []byte |
| 44 | } |
| 45 | |
| 46 | const ( |
| 47 | srHeaderLength = 24 |
| 48 | srSSRCOffset = 0 |
| 49 | srNTPOffset = srSSRCOffset + ssrcLength |
| 50 | ntpTimeLength = 8 |
| 51 | srRTPOffset = srNTPOffset + ntpTimeLength |
| 52 | rtpTimeLength = 4 |
| 53 | srPacketCountOffset = srRTPOffset + rtpTimeLength |
| 54 | srPacketCountLength = 4 |
| 55 | srOctetCountOffset = srPacketCountOffset + srPacketCountLength |
| 56 | srOctetCountLength = 4 |
| 57 | srReportOffset = srOctetCountOffset + srOctetCountLength |
| 58 | ) |
| 59 | |
| 60 | // Marshal encodes the SenderReport in binary. |
| 61 | func (r SenderReport) Marshal() ([]byte, error) { |
| 62 | /* |
| 63 | * 0 1 2 3 |
| 64 | * 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 |
| 65 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | * header |V=2|P| RC | PT=SR=200 | length | |
| 67 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | * | SSRC of sender | |
| 69 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 70 | * sender | NTP timestamp, most significant word | |
| 71 | * info +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | * | NTP timestamp, least significant word | |
| 73 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 74 | * | RTP timestamp | |
| 75 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 76 | * | sender's packet count | |
| 77 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 78 | * | sender's octet count | |
| 79 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 80 | * report | SSRC_1 (SSRC of first source) | |
| 81 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 82 | * 1 | fraction lost | cumulative number of packets lost | |
| 83 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 84 | * | extended highest sequence number received | |
| 85 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 86 | * | interarrival jitter | |
| 87 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 88 | * | last SR (LSR) | |
| 89 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 90 | * | delay since last SR (DLSR) | |
| 91 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 92 | * report | SSRC_2 (SSRC of second source) | |
| 93 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 94 | * 2 : ... : |
| 95 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 96 | * | profile-specific extensions | |
| 97 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 98 | */ |
| 99 | |
| 100 | rawPacket := make([]byte, r.MarshalSize()) |
| 101 | packetBody := rawPacket[headerLength:] |
| 102 | |
| 103 | binary.BigEndian.PutUint32(packetBody[srSSRCOffset:], r.SSRC) |
| 104 | binary.BigEndian.PutUint64(packetBody[srNTPOffset:], r.NTPTime) |
| 105 | binary.BigEndian.PutUint32(packetBody[srRTPOffset:], r.RTPTime) |
| 106 | binary.BigEndian.PutUint32(packetBody[srPacketCountOffset:], r.PacketCount) |
| 107 | binary.BigEndian.PutUint32(packetBody[srOctetCountOffset:], r.OctetCount) |
| 108 | |
| 109 | offset := srHeaderLength |
| 110 | for _, rp := range r.Reports { |
| 111 | data, err := rp.Marshal() |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | copy(packetBody[offset:], data) |
| 116 | offset += receptionReportLength |
| 117 | } |
| 118 | |
| 119 | if len(r.Reports) > countMax { |
| 120 | return nil, errTooManyReports |
| 121 | } |
| 122 | |
| 123 | copy(packetBody[offset:], r.ProfileExtensions) |
| 124 | |
| 125 | hData, err := r.Header().Marshal() |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | copy(rawPacket, hData) |
| 130 | |
| 131 | return rawPacket, nil |
| 132 | } |
| 133 | |
| 134 | // Unmarshal decodes the SenderReport from binary. |
| 135 | func (r *SenderReport) Unmarshal(rawPacket []byte) error { |
| 136 | /* |
| 137 | * 0 1 2 3 |
| 138 | * 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 |
| 139 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 140 | * header |V=2|P| RC | PT=SR=200 | length | |
| 141 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 142 | * | SSRC of sender | |
| 143 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 144 | * sender | NTP timestamp, most significant word | |
| 145 | * info +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 146 | * | NTP timestamp, least significant word | |
| 147 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 148 | * | RTP timestamp | |
| 149 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 150 | * | sender's packet count | |
| 151 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 152 | * | sender's octet count | |
| 153 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 154 | * report | SSRC_1 (SSRC of first source) | |
| 155 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 156 | * 1 | fraction lost | cumulative number of packets lost | |
| 157 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 158 | * | extended highest sequence number received | |
| 159 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 160 | * | interarrival jitter | |
| 161 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 162 | * | last SR (LSR) | |
| 163 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 164 | * | delay since last SR (DLSR) | |
| 165 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 166 | * report | SSRC_2 (SSRC of second source) | |
| 167 | * block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 168 | * 2 : ... : |
| 169 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 170 | * | profile-specific extensions | |
| 171 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 172 | */ |
| 173 | |
| 174 | if len(rawPacket) < (headerLength + srHeaderLength) { |
| 175 | return errPacketTooShort |
| 176 | } |
| 177 | |
| 178 | var header Header |
| 179 | if err := header.Unmarshal(rawPacket); err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | if header.Type != TypeSenderReport { |
| 184 | return errWrongType |
| 185 | } |
| 186 | |
| 187 | packetBody := rawPacket[headerLength:] |
| 188 | |
| 189 | r.SSRC = binary.BigEndian.Uint32(packetBody[srSSRCOffset:]) |
| 190 | r.NTPTime = binary.BigEndian.Uint64(packetBody[srNTPOffset:]) |
| 191 | r.RTPTime = binary.BigEndian.Uint32(packetBody[srRTPOffset:]) |
| 192 | r.PacketCount = binary.BigEndian.Uint32(packetBody[srPacketCountOffset:]) |
| 193 | r.OctetCount = binary.BigEndian.Uint32(packetBody[srOctetCountOffset:]) |
| 194 | |
| 195 | offset := srReportOffset |
| 196 | for i := 0; i < int(header.Count); i++ { |
| 197 | rrEnd := offset + receptionReportLength |
| 198 | if rrEnd > len(packetBody) { |
| 199 | return errPacketTooShort |
| 200 | } |
| 201 | rrBody := packetBody[offset : offset+receptionReportLength] |
| 202 | offset = rrEnd |
| 203 | |
| 204 | var rr ReceptionReport |
| 205 | if err := rr.Unmarshal(rrBody); err != nil { |
| 206 | return err |
| 207 | } |
| 208 | r.Reports = append(r.Reports, rr) |
| 209 | } |
| 210 | |
| 211 | if offset < len(packetBody) { |
| 212 | r.ProfileExtensions = packetBody[offset:] |
| 213 | } |
| 214 | |
| 215 | if uint8(len(r.Reports)) != header.Count { //nolint:gosec // G115 |
| 216 | return errInvalidHeader |
| 217 | } |
| 218 | |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 223 | func (r *SenderReport) DestinationSSRC() []uint32 { |
| 224 | out := make([]uint32, len(r.Reports)+1) |
| 225 | for i, v := range r.Reports { |
| 226 | out[i] = v.SSRC |
| 227 | } |
| 228 | out[len(r.Reports)] = r.SSRC |
| 229 | |
| 230 | return out |
| 231 | } |
| 232 | |
| 233 | // MarshalSize returns the size of the packet once marshaled. |
| 234 | func (r *SenderReport) MarshalSize() int { |
| 235 | repsLength := 0 |
| 236 | for _, rep := range r.Reports { |
| 237 | repsLength += rep.len() |
| 238 | } |
| 239 | |
| 240 | return headerLength + srHeaderLength + repsLength + len(r.ProfileExtensions) |
| 241 | } |
| 242 | |
| 243 | // Header returns the Header associated with this packet. |
| 244 | func (r *SenderReport) Header() Header { |
| 245 | return Header{ |
| 246 | Count: uint8(len(r.Reports)), //nolint:gosec // G115 |
| 247 | Type: TypeSenderReport, |
| 248 | Length: uint16((r.MarshalSize() / 4) - 1), //nolint:gosec // G115 |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func (r SenderReport) String() string { |
| 253 | var out strings.Builder |
| 254 | fmt.Fprintf(&out, "SenderReport from %x\n", r.SSRC) |
| 255 | fmt.Fprintf(&out, "\tNTPTime:\t%d\n", r.NTPTime) |
| 256 | fmt.Fprintf(&out, "\tRTPTIme:\t%d\n", r.RTPTime) |
| 257 | fmt.Fprintf(&out, "\tPacketCount:\t%d\n", r.PacketCount) |
| 258 | fmt.Fprintf(&out, "\tOctetCount:\t%d\n", r.OctetCount) |
| 259 | |
| 260 | out.WriteString("\tSSRC \tLost\tLastSequence\n") |
| 261 | for _, i := range r.Reports { |
| 262 | fmt.Fprintf(&out, "\t%x\t%d/%d\t%d\n", i.SSRC, i.FractionLost, i.TotalLost, i.LastSequenceNumber) |
| 263 | } |
| 264 | fmt.Fprintf(&out, "\tProfile Extension Data: %v\n", r.ProfileExtensions) |
| 265 | |
| 266 | return out.String() |
| 267 | } |