| 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 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 10 | ) |
| 11 | |
| 12 | // A ReceiverReport (RR) packet provides reception quality feedback for an RTP stream. |
| 13 | type ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport# struct { |
| 14 | // The synchronization source identifier for the originator of this RR packet. |
| 15 | SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#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 | Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. []ReceptionReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport# |
| 21 | // Extension contains additional, payload-specific information that needs to |
| 22 | // be reported regularly about the receiver. |
| 23 | ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#ProfileExtensions. []byte |
| 24 | } |
| 25 | |
| 26 | const ( |
| 27 | ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. = 4 |
| 28 | rrSSRCOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrSSRCOffset. = headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. |
| 29 | rrReportOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrReportOffset. = rrSSRCOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrSSRCOffset. + ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. |
| 30 | ) |
| 31 | |
| 32 | // Marshal encodes the ReceiverReport in binary. |
| 33 | func (r ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#MarshalSize().()) |
| 63 | packetBody := rawPacket[headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength.:] |
| 64 | |
| 65 | 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..PutUint32scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#PutUint32().(packetBody, r.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#SSRC.) |
| 66 | |
| 67 | for i, rp := range r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. { |
| 68 | data, err := rp.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#Marshal().() |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | offset := ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. + receptionReportLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/receptionReportLength.*i |
| 73 | copy(packetBody[offset:], data) |
| 74 | } |
| 75 | |
| 76 | if len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.) > countMaxscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countMax. { |
| 77 | return nil, errTooManyReportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errTooManyReports. |
| 78 | } |
| 79 | |
| 80 | pe := make([]byte, len(r.ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#ProfileExtensions.)) |
| 81 | copy(pe, r.ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#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.Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Header().().Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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) < (headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. + ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength.) { |
| 131 | return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort. |
| 132 | } |
| 133 | |
| 134 | var header Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header# |
| 135 | if err := header.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Unmarshal().(rawPacket); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | if header.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type. != TypeReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeReceiverReport. { |
| 140 | return errWrongTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongType. |
| 141 | } |
| 142 | |
| 143 | r.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#SSRC. = 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..Uint32scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#Uint32().(rawPacket[rrSSRCOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrSSRCOffset.:]) |
| 144 | |
| 145 | for i := rrReportOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrReportOffset.; i < len(rawPacket) && len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.) < int(header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.); i += receptionReportLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/receptionReportLength. { |
| 146 | var rr ReceptionReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport# |
| 147 | if err := rr.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#Unmarshal().(rawPacket[i:]); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. = append(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports., rr) |
| 151 | } |
| 152 | r.ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#ProfileExtensions. = rawPacket[rrReportOffsetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/rrReportOffset.+(len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.)*receptionReportLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/receptionReportLength.):] |
| 153 | |
| 154 | //nolint:gosec // G115 |
| 155 | if uint8(len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.)) != header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count. { |
| 156 | return errInvalidHeaderscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errInvalidHeader. |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // MarshalSize returns the size of the packet once marshaled. |
| 163 | func (r *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#MarshalSize().() int { |
| 164 | repsLength := 0 |
| 165 | for _, rep := range r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. { |
| 166 | repsLength += rep.lenscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#len().() |
| 167 | } |
| 168 | |
| 169 | return headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. + ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. + repsLength |
| 170 | } |
| 171 | |
| 172 | // Header returns the Header associated with this packet. |
| 173 | func (r *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Header().() Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header# { |
| 174 | return Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#{ |
| 175 | Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.: uint8(len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.)), //nolint:gosec // G115 |
| 176 | Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type.: TypeReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeReceiverReport., |
| 177 | Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.: uint16((r.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#MarshalSize().()/4)-1) + uint16(getPaddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/getPadding().(len(r.ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#ProfileExtensions.))), //nolint:gosec // G115 |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 182 | func (r *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#DestinationSSRC().() []uint32 { |
| 183 | out := make([]uint32, len(r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports.)) |
| 184 | for i, v := range r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. { |
| 185 | out[i] = v.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#SSRC. |
| 186 | } |
| 187 | |
| 188 | return out |
| 189 | } |
| 190 | |
| 191 | func (r ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) Stringscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#String().() string { |
| 192 | var out stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder# |
| 193 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(&out, "ReceiverReport from %x\n", r.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#SSRC.) |
| 194 | out.WriteStringscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder#WriteString().("\tSSRC \tLost\tLastSequence\n") |
| 195 | for _, i := range r.Reportsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#Reports. { |
| 196 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(&out, "\t%x\t%d/%d\t%d\n", i.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#SSRC., i.FractionLostscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#FractionLost., i.TotalLostscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#TotalLost., i.LastSequenceNumberscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceptionReport#LastSequenceNumber.) |
| 197 | } |
| 198 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(&out, "\tProfile Extension Data: %v\n", r.ProfileExtensionsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#ProfileExtensions.) |
| 199 | |
| 200 | return out.Stringscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder#String().() |
| 201 | } |