| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "math" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | var _ Packet = (*ReceiverEstimatedMaximumBitrate)(nil) // assert is a Packet |
| 14 | |
| 15 | func TestReceiverEstimatedMaximumBitrateMarshal(t *testing.T) { |
| 16 | assert := assert.New(t) |
| 17 | |
| 18 | input := ReceiverEstimatedMaximumBitrate{ |
| 19 | SenderSSRC: 1, |
| 20 | Bitrate: 8927168.0, |
| 21 | 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.Marshal() |
| 27 | assert.NoError(err) |
| 28 | assert.Equal(expected, output) |
| 29 | } |
| 30 | |
| 31 | func TestReceiverEstimatedMaximumBitrateUnmarshal(t *testing.T) { |
| 32 | assert := 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 := ReceiverEstimatedMaximumBitrate{ |
| 41 | SenderSSRC: 1, |
| 42 | Bitrate: 8927168, |
| 43 | SSRCs: []uint32{1215622422}, |
| 44 | } |
| 45 | |
| 46 | packet := ReceiverEstimatedMaximumBitrate{} |
| 47 | err := packet.Unmarshal(input) |
| 48 | assert.NoError(err) |
| 49 | assert.Equal(expected, packet) |
| 50 | } |
| 51 | |
| 52 | func TestReceiverEstimatedMaximumBitrateTruncate(t *testing.T) { |
| 53 | assert := 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 := ReceiverEstimatedMaximumBitrate{} |
| 65 | err := packet.Unmarshal(input) |
| 66 | assert.NoError(err) |
| 67 | assert.Equal(float32(8927168), packet.Bitrate) |
| 68 | |
| 69 | // Just verify marshal produces the same input. |
| 70 | output, err := packet.Marshal() |
| 71 | assert.NoError(err) |
| 72 | assert.Equal(input, output) |
| 73 | |
| 74 | // If we subtract the bitrate by 1, we'll round down a lower mantissa |
| 75 | packet.Bitrate-- |
| 76 | |
| 77 | // bitrate = 8927167 |
| 78 | // mantissa = 139486 |
| 79 | // exp = 6 |
| 80 | |
| 81 | output, err = packet.Marshal() |
| 82 | assert.NoError(err) |
| 83 | assert.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.Unmarshal(output) |
| 91 | assert.NoError(err) |
| 92 | assert.Equal(float32(8927104), packet.Bitrate) |
| 93 | } |
| 94 | |
| 95 | func TestReceiverEstimatedMaximumBitrateOverflow(t *testing.T) { |
| 96 | assert := assert.New(t) |
| 97 | |
| 98 | // Marshal a packet with the maximum possible bitrate. |
| 99 | packet := ReceiverEstimatedMaximumBitrate{ |
| 100 | Bitrate: 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.Marshal() |
| 109 | assert.NoError(err) |
| 110 | assert.Equal(expected, output) |
| 111 | |
| 112 | // mantissa = 262143 |
| 113 | // exp = 63 |
| 114 | // bitrate = 0xFFFFC00000000000 |
| 115 | |
| 116 | err = packet.Unmarshal(output) |
| 117 | assert.NoError(err) |
| 118 | assert.Equal(math.Float32frombits(0x67FFFFC0), packet.Bitrate) |
| 119 | |
| 120 | // Make sure we marshal to the same result again. |
| 121 | output, err = packet.Marshal() |
| 122 | assert.NoError(err) |
| 123 | assert.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.Unmarshal(input) |
| 128 | assert.NoError(err) |
| 129 | assert.Equal(math.Float32frombits(0x62800000), packet.Bitrate) |
| 130 | } |