| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "encoding/binary" |
| 9 | "fmt" |
| 10 | "math" |
| 11 | ) |
| 12 | |
| 13 | // ReceiverEstimatedMaximumBitrate contains the receiver's estimated maximum bitrate. |
| 14 | // see: https://tools.ietf.org/html/draft-alvestrand-rmcat-remb-03 |
| 15 | type ReceiverEstimatedMaximumBitrate struct { |
| 16 | // SSRC of sender |
| 17 | SenderSSRC uint32 |
| 18 | |
| 19 | // Estimated maximum bitrate |
| 20 | Bitrate float32 |
| 21 | |
| 22 | // SSRC entries which this packet applies to |
| 23 | SSRCs []uint32 |
| 24 | } |
| 25 | |
| 26 | // Marshal serializes the packet and returns a byte slice. |
| 27 | func (p ReceiverEstimatedMaximumBitrate) Marshal() (buf []byte, err error) { |
| 28 | // Allocate a buffer of the exact output size. |
| 29 | buf = make([]byte, p.MarshalSize()) |
| 30 | |
| 31 | // Write to our buffer. |
| 32 | n, err := p.MarshalTo(buf) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | // This will always be true but just to be safe. |
| 38 | if n != len(buf) { |
| 39 | return nil, errWrongMarshalSize |
| 40 | } |
| 41 | |
| 42 | return buf, nil |
| 43 | } |
| 44 | |
| 45 | // MarshalSize returns the size of the packet once marshaled. |
| 46 | func (p ReceiverEstimatedMaximumBitrate) MarshalSize() int { |
| 47 | return 20 + 4*len(p.SSRCs) |
| 48 | } |
| 49 | |
| 50 | // MarshalTo serializes the packet to the given byte slice. |
| 51 | func (p ReceiverEstimatedMaximumBitrate) MarshalTo(buf []byte) (n int, err error) { |
| 52 | const bitratemax = 0x3FFFFp+63 |
| 53 | /* |
| 54 | 0 1 2 3 |
| 55 | 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 |
| 56 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 | |V=2|P| FMT=15 | PT=206 | length | |
| 58 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 | | SSRC of packet sender | |
| 60 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 61 | | SSRC of media source | |
| 62 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 63 | | Unique identifier 'R' 'E' 'M' 'B' | |
| 64 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 | | Num SSRC | BR Exp | BR Mantissa | |
| 66 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 | | SSRC feedback | |
| 68 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 69 | | ... | |
| 70 | */ |
| 71 | |
| 72 | size := p.MarshalSize() |
| 73 | if len(buf) < size { |
| 74 | return 0, errPacketTooShort |
| 75 | } |
| 76 | |
| 77 | buf[0] = 143 // v=2, p=0, fmt=15 |
| 78 | buf[1] = 206 |
| 79 | |
| 80 | // Length of this packet in 32-bit words minus one. |
| 81 | length := uint16((p.MarshalSize() / 4) - 1) //nolint:gosec // G115 |
| 82 | binary.BigEndian.PutUint16(buf[2:4], length) |
| 83 | |
| 84 | binary.BigEndian.PutUint32(buf[4:8], p.SenderSSRC) |
| 85 | binary.BigEndian.PutUint32(buf[8:12], 0) // always zero |
| 86 | |
| 87 | // ALL HAIL REMB |
| 88 | buf[12] = 'R' |
| 89 | buf[13] = 'E' |
| 90 | buf[14] = 'M' |
| 91 | buf[15] = 'B' |
| 92 | |
| 93 | // Write the length of the ssrcs to follow at the end |
| 94 | if len(p.SSRCs) > math.MaxUint8 { |
| 95 | return 0, errTooManySSRCs |
| 96 | } |
| 97 | |
| 98 | buf[16] = byte(len(p.SSRCs)) //nolint:gosec // length validated above |
| 99 | |
| 100 | exp := 0 |
| 101 | bitrate := p.Bitrate |
| 102 | |
| 103 | if bitrate >= bitratemax { |
| 104 | bitrate = bitratemax |
| 105 | } |
| 106 | |
| 107 | if bitrate < 0 { |
| 108 | return 0, errInvalidBitrate |
| 109 | } |
| 110 | |
| 111 | for bitrate >= (1 << 18) { |
| 112 | bitrate /= 2.0 |
| 113 | exp++ |
| 114 | } |
| 115 | |
| 116 | if exp >= (1 << 6) { |
| 117 | return 0, errInvalidBitrate |
| 118 | } |
| 119 | |
| 120 | mantissa := uint(math.Floor(float64(bitrate))) |
| 121 | |
| 122 | // We can't quite use the binary package because |
| 123 | // a) it's a uint24 and b) the exponent is only 6-bits |
| 124 | // Just trust me; this is big-endian encoding. |
| 125 | buf[17] = byte(exp<<2) | byte(mantissa>>16) //nolint: gosec // mantissa is limited to 18 bits |
| 126 | buf[18] = byte(mantissa >> 8) //nolint: gosec // mantissa is limited to 18 bits |
| 127 | buf[19] = byte(mantissa) // nolint: gosec // mantissa is limited to 18 bits |
| 128 | |
| 129 | // Write the SSRCs at the very end. |
| 130 | n = 20 |
| 131 | for _, ssrc := range p.SSRCs { |
| 132 | binary.BigEndian.PutUint32(buf[n:n+4], ssrc) |
| 133 | n += 4 |
| 134 | } |
| 135 | |
| 136 | return n, nil |
| 137 | } |
| 138 | |
| 139 | // Unmarshal reads a REMB packet from the given byte slice. |
| 140 | // |
| 141 | //nolint:cyclop |
| 142 | func (p *ReceiverEstimatedMaximumBitrate) Unmarshal(buf []byte) (err error) { |
| 143 | const mantissamax = 0x7FFFFF |
| 144 | /* |
| 145 | 0 1 2 3 |
| 146 | 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 |
| 147 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 148 | |V=2|P| FMT=15 | PT=206 | length | |
| 149 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 150 | | SSRC of packet sender | |
| 151 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 152 | | SSRC of media source | |
| 153 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 154 | | Unique identifier 'R' 'E' 'M' 'B' | |
| 155 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 156 | | Num SSRC | BR Exp | BR Mantissa | |
| 157 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 158 | | SSRC feedback | |
| 159 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 160 | | ... | |
| 161 | */ |
| 162 | |
| 163 | // 20 bytes is the size of the packet with no SSRCs |
| 164 | if len(buf) < 20 { |
| 165 | return errPacketTooShort |
| 166 | } |
| 167 | |
| 168 | // version must be 2 |
| 169 | version := buf[0] >> 6 |
| 170 | if version != 2 { |
| 171 | return fmt.Errorf("%w expected(2) actual(%d)", errBadVersion, version) |
| 172 | } |
| 173 | |
| 174 | // padding must be unset |
| 175 | padding := (buf[0] >> 5) & 1 |
| 176 | if padding != 0 { |
| 177 | return fmt.Errorf("%w expected(0) actual(%d)", errWrongPadding, padding) |
| 178 | } |
| 179 | |
| 180 | // fmt must be 15 |
| 181 | fmtVal := buf[0] & 31 |
| 182 | if fmtVal != 15 { |
| 183 | return fmt.Errorf("%w expected(15) actual(%d)", errWrongFeedbackType, fmtVal) |
| 184 | } |
| 185 | |
| 186 | // Must be payload specific feedback |
| 187 | if buf[1] != 206 { |
| 188 | return fmt.Errorf("%w expected(206) actual(%d)", errWrongPayloadType, buf[1]) |
| 189 | } |
| 190 | |
| 191 | // length is the number of 32-bit words, minus 1 |
| 192 | length := binary.BigEndian.Uint16(buf[2:4]) |
| 193 | size := int((length + 1) * 4) |
| 194 | |
| 195 | // There's not way this could be legit |
| 196 | if size < 20 { |
| 197 | return errHeaderTooSmall |
| 198 | } |
| 199 | |
| 200 | // Make sure the buffer is large enough. |
| 201 | if len(buf) < size { |
| 202 | return errPacketTooShort |
| 203 | } |
| 204 | |
| 205 | // The sender SSRC is 32-bits |
| 206 | p.SenderSSRC = binary.BigEndian.Uint32(buf[4:8]) |
| 207 | |
| 208 | // The destination SSRC must be 0 |
| 209 | media := binary.BigEndian.Uint32(buf[8:12]) |
| 210 | if media != 0 { |
| 211 | return errSSRCMustBeZero |
| 212 | } |
| 213 | |
| 214 | // REMB rules all around me |
| 215 | if !bytes.Equal(buf[12:16], []byte{'R', 'E', 'M', 'B'}) { |
| 216 | return errMissingREMBidentifier |
| 217 | } |
| 218 | |
| 219 | // The next byte is the number of SSRC entries at the end. |
| 220 | num := int(buf[16]) |
| 221 | |
| 222 | // Now we know the expected size, make sure they match. |
| 223 | if size != 20+4*num { |
| 224 | return errSSRCNumAndLengthMismatch |
| 225 | } |
| 226 | |
| 227 | // Get the 6-bit exponent value. |
| 228 | exp := buf[17] >> 2 |
| 229 | exp += 127 // bias for IEEE754 |
| 230 | exp += 23 // IEEE754 biases the decimal to the left, abs-send-time biases it to the right |
| 231 | |
| 232 | // The remaining 2-bits plus the next 16-bits are the mantissa. |
| 233 | mantissa := uint32(buf[17]&3)<<16 | uint32(buf[18])<<8 | uint32(buf[19]) |
| 234 | |
| 235 | if mantissa != 0 { |
| 236 | // ieee754 requires an implicit leading bit |
| 237 | for (mantissa & (mantissamax + 1)) == 0 { |
| 238 | exp-- |
| 239 | mantissa *= 2 |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // bitrate = mantissa * 2^exp |
| 244 | p.Bitrate = math.Float32frombits((uint32(exp) << 23) | (mantissa & mantissamax)) |
| 245 | |
| 246 | // Clear any existing SSRCs |
| 247 | p.SSRCs = nil |
| 248 | |
| 249 | // Loop over and parse the SSRC entires at the end. |
| 250 | // We already verified that size == num * 4 |
| 251 | for n := 20; n < size; n += 4 { |
| 252 | ssrc := binary.BigEndian.Uint32(buf[n : n+4]) |
| 253 | p.SSRCs = append(p.SSRCs, ssrc) |
| 254 | } |
| 255 | |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | // Header returns the Header associated with this packet. |
| 260 | func (p *ReceiverEstimatedMaximumBitrate) Header() Header { |
| 261 | return Header{ |
| 262 | Count: FormatREMB, |
| 263 | Type: TypePayloadSpecificFeedback, |
| 264 | Length: uint16((p.MarshalSize() / 4) - 1), //nolint:gosec // G115 |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // String prints the REMB packet in a human-readable format. |
| 269 | func (p *ReceiverEstimatedMaximumBitrate) String() string { |
| 270 | // Keep a table of powers to units for fast conversion. |
| 271 | bitUnits := []string{"b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb"} |
| 272 | |
| 273 | // Do some unit conversions because b/s is far too difficult to read. |
| 274 | bitrate := p.Bitrate |
| 275 | powers := 0 |
| 276 | |
| 277 | // Keep dividing the bitrate until it's under 1000 |
| 278 | for bitrate >= 1000.0 && powers < len(bitUnits) { |
| 279 | bitrate /= 1000.0 |
| 280 | powers++ |
| 281 | } |
| 282 | |
| 283 | unit := bitUnits[powers] //nolint:gosec // powers is bounded by loop condition |
| 284 | |
| 285 | return fmt.Sprintf("ReceiverEstimatedMaximumBitrate %x %.2f %s/s", p.SenderSSRC, bitrate, unit) |
| 286 | } |
| 287 | |
| 288 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 289 | func (p *ReceiverEstimatedMaximumBitrate) DestinationSSRC() []uint32 { |
| 290 | return p.SSRCs |
| 291 | } |