| 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 | "mathscip-go gomod github.com/golang/go/src go1.24.0 math/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 9 | |
| 10 | "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/" |
| 11 | ) |
| 12 | |
| 13 | var _ Packetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Packet# = (*ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#)(nil) // assert is a Packet |
| 14 | |
| 15 | func TestReceiverEstimatedMaximumBitrateMarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TestReceiverEstimatedMaximumBitrateMarshal().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 16 | assert := assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Newscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/New().(t) |
| 17 | |
| 18 | input := ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#{ |
| 19 | SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC.: 1, |
| 20 | Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.: 8927168.0, |
| 21 | SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.: []uint32{1215622422}, |
| 22 | } |
| 23 | |
| 24 | expected := []byte{143, 206, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 82, 69, 77, 66, 1, 26, 32, 223, 72, 116, 237, 22} |
| 25 | |
| 26 | output, err := input.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() |
| 27 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 28 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(expected, output) |
| 29 | } |
| 30 | |
| 31 | func TestReceiverEstimatedMaximumBitrateUnmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TestReceiverEstimatedMaximumBitrateUnmarshal().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 32 | assert := assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Newscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/New().(t) |
| 33 | |
| 34 | // Real data sent by Chrome while watching a 6Mb/s stream |
| 35 | input := []byte{143, 206, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 82, 69, 77, 66, 1, 26, 32, 223, 72, 116, 237, 22} |
| 36 | |
| 37 | // mantissa = []byte{26 & 3, 32, 223} = []byte{2, 32, 223} = 139487 |
| 38 | // exp = 26 >> 2 = 6 |
| 39 | // bitrate = 139487 * 2^6 = 139487 * 64 = 8927168 = 8.9 Mb/s |
| 40 | expected := ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#{ |
| 41 | SenderSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SenderSSRC.: 1, |
| 42 | Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.: 8927168, |
| 43 | SSRCsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#SSRCs.: []uint32{1215622422}, |
| 44 | } |
| 45 | |
| 46 | packet := ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#{} |
| 47 | err := packet.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Unmarshal().(input) |
| 48 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 49 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(expected, packet) |
| 50 | } |
| 51 | |
| 52 | func TestReceiverEstimatedMaximumBitrateTruncatescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TestReceiverEstimatedMaximumBitrateTruncate().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 53 | assert := assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Newscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/New().(t) |
| 54 | |
| 55 | input := []byte{143, 206, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 82, 69, 77, 66, 1, 26, 32, 223, 72, 116, 237, 22} |
| 56 | |
| 57 | // Make sure that we're interpreting the bitrate correctly. |
| 58 | // For the above example, we have: |
| 59 | |
| 60 | // mantissa = 139487 |
| 61 | // exp = 6 |
| 62 | // bitrate = 8927168 |
| 63 | |
| 64 | packet := ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#{} |
| 65 | err := packet.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Unmarshal().(input) |
| 66 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 67 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(float32(8927168), packet.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.) |
| 68 | |
| 69 | // Just verify marshal produces the same input. |
| 70 | output, err := packet.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() |
| 71 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 72 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(input, output) |
| 73 | |
| 74 | // If we subtract the bitrate by 1, we'll round down a lower mantissa |
| 75 | packet.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.-- |
| 76 | |
| 77 | // bitrate = 8927167 |
| 78 | // mantissa = 139486 |
| 79 | // exp = 6 |
| 80 | |
| 81 | output, err = packet.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() |
| 82 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 83 | assert.NotEqualscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NotEqual().(input, output) |
| 84 | |
| 85 | // Which if we actually unmarshal again, we'll find that it's actually decreased by 63 (which is exp) |
| 86 | // mantissa = 139486 |
| 87 | // exp = 6 |
| 88 | // bitrate = 8927104 |
| 89 | |
| 90 | err = packet.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Unmarshal().(output) |
| 91 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 92 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(float32(8927104), packet.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.) |
| 93 | } |
| 94 | |
| 95 | func TestReceiverEstimatedMaximumBitrateOverflowscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TestReceiverEstimatedMaximumBitrateOverflow().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 96 | assert := assertscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/.Newscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/New().(t) |
| 97 | |
| 98 | // Marshal a packet with the maximum possible bitrate. |
| 99 | packet := ReceiverEstimatedMaximumBitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#{ |
| 100 | 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/.MaxFloat32scip-go gomod github.com/golang/go/src go1.24.0 math/MaxFloat32., |
| 101 | } |
| 102 | |
| 103 | // mantissa = 262143 = 0x3FFFF |
| 104 | // exp = 63 |
| 105 | |
| 106 | expected := []byte{143, 206, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 82, 69, 77, 66, 0, 255, 255, 255} |
| 107 | |
| 108 | output, err := packet.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() |
| 109 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 110 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(expected, output) |
| 111 | |
| 112 | // mantissa = 262143 |
| 113 | // exp = 63 |
| 114 | // bitrate = 0xFFFFC00000000000 |
| 115 | |
| 116 | err = packet.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Unmarshal().(output) |
| 117 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 118 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(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().(0x67FFFFC0), packet.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.) |
| 119 | |
| 120 | // Make sure we marshal to the same result again. |
| 121 | output, err = packet.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Marshal().() |
| 122 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 123 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(expected, output) |
| 124 | |
| 125 | // Finally, try unmarshalling one number higher than we used to be able to handle. |
| 126 | input := []byte{143, 206, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 82, 69, 77, 66, 0, 188, 0, 0} |
| 127 | err = packet.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Unmarshal().(input) |
| 128 | assert.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#NoError().(err) |
| 129 | assert.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/assert`/Assertions#Equal().(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().(0x62800000), packet.Bitratescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverEstimatedMaximumBitrate#Bitrate.) |
| 130 | } |