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