| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import "encoding/binary" |
| 7 | |
| 8 | // A ReceptionReport block conveys statistics on the reception of RTP packets |
| 9 | // from a single synchronization source. |
| 10 | type ReceptionReport struct { |
| 11 | // The SSRC identifier of the source to which the information in this |
| 12 | // reception report block pertains. |
| 13 | SSRC uint32 |
| 14 | // The fraction of RTP data packets from source SSRC lost since the |
| 15 | // previous SR or RR packet was sent, expressed as a fixed point |
| 16 | // number with the binary point at the left edge of the field. |
| 17 | FractionLost uint8 |
| 18 | // The total number of RTP data packets from source SSRC that have |
| 19 | // been lost since the beginning of reception. |
| 20 | TotalLost uint32 |
| 21 | // The low 16 bits contain the highest sequence number received in an |
| 22 | // RTP data packet from source SSRC, and the most significant 16 |
| 23 | // bits extend that sequence number with the corresponding count of |
| 24 | // sequence number cycles. |
| 25 | LastSequenceNumber uint32 |
| 26 | // An estimate of the statistical variance of the RTP data packet |
| 27 | // interarrival time, measured in timestamp units and expressed as an |
| 28 | // unsigned integer. |
| 29 | Jitter uint32 |
| 30 | // The middle 32 bits out of 64 in the NTP timestamp received as part of |
| 31 | // the most recent RTCP sender report (SR) packet from source SSRC. If no |
| 32 | // SR has been received yet, the field is set to zero. |
| 33 | LastSenderReport uint32 |
| 34 | // The delay, expressed in units of 1/65536 seconds, between receiving the |
| 35 | // last SR packet from source SSRC and sending this reception report block. |
| 36 | // If no SR packet has been received yet from SSRC, the field is set to zero. |
| 37 | Delay uint32 |
| 38 | } |
| 39 | |
| 40 | const ( |
| 41 | receptionReportLength = 24 |
| 42 | fractionLostOffset = 4 |
| 43 | totalLostOffset = 5 |
| 44 | lastSeqOffset = 8 |
| 45 | jitterOffset = 12 |
| 46 | lastSROffset = 16 |
| 47 | delayOffset = 20 |
| 48 | ) |
| 49 | |
| 50 | // Marshal encodes the ReceptionReport in binary. |
| 51 | func (r ReceptionReport) Marshal() ([]byte, error) { |
| 52 | /* |
| 53 | * 0 1 2 3 |
| 54 | * 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 |
| 55 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 56 | * | SSRC | |
| 57 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 58 | * | fraction lost | cumulative number of packets lost | |
| 59 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | * | extended highest sequence number received | |
| 61 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | * | interarrival jitter | |
| 63 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | * | last SR (LSR) | |
| 65 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | * | delay since last SR (DLSR) | |
| 67 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 68 | */ |
| 69 | |
| 70 | rawPacket := make([]byte, receptionReportLength) |
| 71 | |
| 72 | binary.BigEndian.PutUint32(rawPacket, r.SSRC) |
| 73 | |
| 74 | rawPacket[fractionLostOffset] = r.FractionLost |
| 75 | |
| 76 | // pack TotalLost into 24 bits |
| 77 | if r.TotalLost >= (1 << 25) { |
| 78 | return nil, errInvalidTotalLost |
| 79 | } |
| 80 | tlBytes := rawPacket[totalLostOffset:] |
| 81 | tlBytes[0] = byte(r.TotalLost >> 16) //nolint:gosec // rawPacket is created with length receptionReportLength (24) |
| 82 | tlBytes[1] = byte(r.TotalLost >> 8) //nolint:gosec // rawPacket is created with length receptionReportLength (24) |
| 83 | tlBytes[2] = byte(r.TotalLost) //nolint:gosec // rawPacket is created with length receptionReportLength (24) |
| 84 | |
| 85 | binary.BigEndian.PutUint32(rawPacket[lastSeqOffset:], r.LastSequenceNumber) |
| 86 | binary.BigEndian.PutUint32(rawPacket[jitterOffset:], r.Jitter) |
| 87 | binary.BigEndian.PutUint32(rawPacket[lastSROffset:], r.LastSenderReport) |
| 88 | binary.BigEndian.PutUint32(rawPacket[delayOffset:], r.Delay) |
| 89 | |
| 90 | return rawPacket, nil |
| 91 | } |
| 92 | |
| 93 | // Unmarshal decodes the ReceptionReport from binary. |
| 94 | func (r *ReceptionReport) Unmarshal(rawPacket []byte) error { |
| 95 | if len(rawPacket) < receptionReportLength { |
| 96 | return errPacketTooShort |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * 0 1 2 3 |
| 101 | * 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 |
| 102 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 103 | * | SSRC | |
| 104 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | * | fraction lost | cumulative number of packets lost | |
| 106 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | * | extended highest sequence number received | |
| 108 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | * | interarrival jitter | |
| 110 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | * | last SR (LSR) | |
| 112 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | * | delay since last SR (DLSR) | |
| 114 | * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 115 | */ |
| 116 | |
| 117 | r.SSRC = binary.BigEndian.Uint32(rawPacket) |
| 118 | r.FractionLost = rawPacket[fractionLostOffset] |
| 119 | |
| 120 | tlBytes := rawPacket[totalLostOffset:] |
| 121 | r.TotalLost = uint32(tlBytes[2]) | uint32(tlBytes[1])<<8 | uint32(tlBytes[0])<<16 |
| 122 | |
| 123 | r.LastSequenceNumber = binary.BigEndian.Uint32(rawPacket[lastSeqOffset:]) |
| 124 | r.Jitter = binary.BigEndian.Uint32(rawPacket[jitterOffset:]) |
| 125 | r.LastSenderReport = binary.BigEndian.Uint32(rawPacket[lastSROffset:]) |
| 126 | r.Delay = binary.BigEndian.Uint32(rawPacket[delayOffset:]) |
| 127 | |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | func (r *ReceptionReport) len() int { |
| 132 | return receptionReportLength |
| 133 | } |