Debian Codegraph @

receiver_estimated_maximum_bitrate.go

raw symbols SCIP
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 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
8 "encoding/binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/"
9 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
10 "mathscip-go gomod github.com/golang/go/src go1.24.0 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 ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate# struct {
16 // SSRC of sender
17 SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC. uint32
18
19 // Estimated maximum bitrate
20 Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate. float32
21
22 // SSRC entries which this packet applies to
23 SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs. []uint32
24 }
25
26 // Marshal serializes the packet and returns a byte slice.
27 func (p ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() (buf []byte, err error) {
28 // Allocate a buffer of the exact output size.
29 buf = make([]byte, p.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#MarshalSize().())
30
31 // Write to our buffer.
32 n, err := p.MarshalToscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#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, errWrongMarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongMarshalSize.
40 }
41
42 return buf, nil
43 }
44
45 // MarshalSize returns the size of the packet once marshaled.
46 func (p ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#MarshalSize().() int {
47 return 20 + 4*len(p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.)
48 }
49
50 // MarshalTo serializes the packet to the given byte slice.
51 func (p ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) MarshalToscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#MarshalSize().()
73 if len(buf) < size {
74 return 0, errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#MarshalSize().() / 4) - 1) //nolint:gosec // G115
82 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..PutUint16scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#PutUint16().(buf[2:4], length)
83
84 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().(buf[4:8], p.SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC.)
85 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().(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.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.) > mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint8scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint8. {
95 return 0, errTooManySSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errTooManySSRCs.
96 }
97
98 buf[16] = byte(len(p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.)) //nolint:gosec // length validated above
99
100 exp := 0
101 bitrate := p.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.
102
103 if bitrate >= bitratemax {
104 bitrate = bitratemax
105 }
106
107 if bitrate < 0 {
108 return 0, errInvalidBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errInvalidBitrate.
109 }
110
111 for bitrate >= (1 << 18) {
112 bitrate /= 2.0
113 exp++
114 }
115
116 if exp >= (1 << 6) {
117 return 0, errInvalidBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errInvalidBitrate.
118 }
119
120 mantissa := uint(mathscip-go gomod github.com/golang/go/src go1.24.0 math/.Floorscip-go gomod github.com/golang/go/src go1.24.0 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.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs. {
132 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().(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 *ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort.
166 }
167
168 // version must be 2
169 version := buf[0] >> 6
170 if version != 2 {
171 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("%w expected(2) actual(%d)", errBadVersionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errBadVersion., version)
172 }
173
174 // padding must be unset
175 padding := (buf[0] >> 5) & 1
176 if padding != 0 {
177 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("%w expected(0) actual(%d)", errWrongPaddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongPadding., padding)
178 }
179
180 // fmt must be 15
181 fmtVal := buf[0] & 31
182 if fmtVal != 15 {
183 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("%w expected(15) actual(%d)", errWrongFeedbackTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongFeedbackType., fmtVal)
184 }
185
186 // Must be payload specific feedback
187 if buf[1] != 206 {
188 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("%w expected(206) actual(%d)", errWrongPayloadTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongPayloadType., buf[1])
189 }
190
191 // length is the number of 32-bit words, minus 1
192 length := 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..Uint16scip-go gomod github.com/golang/go/src go1.24.0 `encoding/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 errHeaderTooSmallscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errHeaderTooSmall.
198 }
199
200 // Make sure the buffer is large enough.
201 if len(buf) < size {
202 return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort.
203 }
204
205 // The sender SSRC is 32-bits
206 p.SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC. = 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().(buf[4:8])
207
208 // The destination SSRC must be 0
209 media := 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().(buf[8:12])
210 if media != 0 {
211 return errSSRCMustBeZeroscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errSSRCMustBeZero.
212 }
213
214 // REMB rules all around me
215 if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(buf[12:16], []byte{'R', 'E', 'M', 'B'}) {
216 return errMissingREMBidentifierscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 errSSRCNumAndLengthMismatchscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate. = mathscip-go gomod github.com/golang/go/src go1.24.0 math/.Float32frombitsscip-go gomod github.com/golang/go/src go1.24.0 math/Float32frombits().((uint32(exp) << 23) | (mantissa & mantissamax))
245
246 // Clear any existing SSRCs
247 p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#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 := 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().(buf[n : n+4])
253 p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs. = append(p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs., ssrc)
254 }
255
256 return nil
257 }
258
259 // Header returns the Header associated with this packet.
260 func (p *ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Header().() Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header# {
261 return Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#{
262 Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.: FormatREMBscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/FormatREMB.,
263 Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type.: TypePayloadSpecificFeedbackscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypePayloadSpecificFeedback.,
264 Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.: uint16((p.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#MarshalSize().() / 4) - 1), //nolint:gosec // G115
265 }
266 }
267
268 // String prints the REMB packet in a human-readable format.
269 func (p *ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) Stringscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#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 fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("ReceiverEstimatedMaximumBitrate %x %.2f %s/s", p.SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC., bitrate, unit)
286 }
287
288 // DestinationSSRC returns an array of SSRC values that this packet refers to.
289 func (p *ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#) DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#DestinationSSRC().() []uint32 {
290 return p.SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.
291 }