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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
package codecs
import (
"errors"
"fmt"
"math/rand"
"reflect"
"testing"
)
func TestVP9Packet_Unmarshal(t *testing.T) {
cases := map[string]struct {
b []byte
pkt VP9Packet
err error
}{
"Nil": {
b: nil,
err: errNilPacket,
},
"Empty": {
b: []byte{},
err: errShortPacket,
},
"NonFlexible": {
b: []byte{0x00, 0xAA},
pkt: VP9Packet{
Payload: []byte{0xAA},
},
},
"NonFlexiblePictureID": {
b: []byte{0x80, 0x02, 0xAA},
pkt: VP9Packet{
I: true,
PictureID: 0x02,
Payload: []byte{0xAA},
},
},
"NonFlexiblePictureIDExt": {
b: []byte{0x80, 0x81, 0xFF, 0xAA},
pkt: VP9Packet{
I: true,
PictureID: 0x01FF,
Payload: []byte{0xAA},
},
},
"NonFlexiblePictureIDExt_ShortPacket0": {
b: []byte{0x80, 0x81},
err: errShortPacket,
},
"NonFlexiblePictureIDExt_ShortPacket1": {
b: []byte{0x80},
err: errShortPacket,
},
"NonFlexibleLayerIndicePictureID": {
b: []byte{0xA0, 0x02, 0x23, 0x01, 0xAA},
pkt: VP9Packet{
I: true,
L: true,
PictureID: 0x02,
TID: 0x01,
SID: 0x01,
D: true,
TL0PICIDX: 0x01,
Payload: []byte{0xAA},
},
},
"FlexibleLayerIndicePictureID": {
b: []byte{0xB0, 0x02, 0x23, 0x01, 0xAA},
pkt: VP9Packet{
F: true,
I: true,
L: true,
PictureID: 0x02,
TID: 0x01,
SID: 0x01,
D: true,
Payload: []byte{0x01, 0xAA},
},
},
"NonFlexibleLayerIndicePictureID_ShortPacket0": {
b: []byte{0xA0, 0x02, 0x23},
err: errShortPacket,
},
"NonFlexibleLayerIndicePictureID_ShortPacket1": {
b: []byte{0xA0, 0x02},
err: errShortPacket,
},
"FlexiblePictureIDRefIndex": {
b: []byte{0xD0, 0x02, 0x03, 0x04, 0xAA},
pkt: VP9Packet{
I: true,
P: true,
F: true,
PictureID: 0x02,
PDiff: []uint8{0x01, 0x02},
Payload: []byte{0xAA},
},
},
"FlexiblePictureIDRefIndex_TooManyPDiff": {
b: []byte{0xD0, 0x02, 0x03, 0x05, 0x07, 0x09, 0x10, 0xAA},
err: errTooManyPDiff,
},
"FlexiblePictureIDRefIndexNoPayload": {
b: []byte{0xD0, 0x02, 0x03, 0x04},
pkt: VP9Packet{
I: true,
P: true,
F: true,
PictureID: 0x02,
PDiff: []uint8{0x01, 0x02},
Payload: []byte{},
},
},
"FlexiblePictureIDRefIndex_ShortPacket0": {
b: []byte{0xD0, 0x02, 0x03},
err: errShortPacket,
},
"FlexiblePictureIDRefIndex_ShortPacket1": {
b: []byte{0xD0, 0x02},
err: errShortPacket,
},
"FlexiblePictureIDRefIndex_ShortPacket2": {
b: []byte{0xD0},
err: errShortPacket,
},
"ScalabilityStructureResolutionsNoPayload": {
b: []byte{
0x0A,
(1 << 5) | (1 << 4), // NS:1 Y:1 G:0
640 >> 8, 640 & 0xff,
360 >> 8, 360 & 0xff,
1280 >> 8, 1280 & 0xff,
720 >> 8, 720 & 0xff,
},
pkt: VP9Packet{
B: true,
V: true,
NS: 1,
Y: true,
G: false,
NG: 0,
Width: []uint16{640, 1280},
Height: []uint16{360, 720},
Payload: []byte{},
},
},
"ScalabilityStructureNoPayload": {
b: []byte{
0x0A,
(1 << 5) | (0 << 4) | (1 << 3), // NS:1 Y:0 G:1
2,
(0 << 5) | (1 << 4) | (0 << 2), // T:0 U:1 R:0 -
(2 << 5) | (0 << 4) | (1 << 2), // T:2 U:0 R:1 -
33,
},
pkt: VP9Packet{
B: true,
V: true,
NS: 1,
Y: false,
G: true,
NG: 2,
PGTID: []uint8{0, 2},
PGU: []bool{true, false},
PGPDiff: [][]uint8{{}, {33}},
Payload: []byte{},
},
},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
p := VP9Packet{}
raw, err := p.Unmarshal(c.b)
if c.err == nil {
if raw == nil {
t.Error("Result shouldn't be nil in case of success")
}
if err != nil {
t.Error("Error should be nil in case of success")
}
if !reflect.DeepEqual(c.pkt, p) {
t.Errorf("Unmarshalled packet expected to be:\n %v\ngot:\n %v", c.pkt, p)
}
} else {
if raw != nil {
t.Error("Result should be nil in case of error")
}
if !errors.Is(err, c.err) {
t.Errorf("Error should be '%v', got '%v'", c.err, err)
}
}
})
}
}
func TestVP9Payloader_Payload(t *testing.T) {
r0 := int(rand.New(rand.NewSource(0)).Int31n(0x7FFF)) //nolint:gosec
var rands [][2]byte
for i := 0; i < 10; i++ {
rands = append(rands, [2]byte{byte(r0>>8) | 0x80, byte(r0 & 0xFF)})
r0++
}
cases := map[string]struct {
b [][]byte
mtu uint16
res [][]byte
}{
"NilPayload": {
b: [][]byte{nil},
mtu: 100,
res: [][]byte{},
},
"SmallMTU": {
b: [][]byte{{0x00, 0x00}},
mtu: 1,
res: [][]byte{},
},
"OnePacket": {
b: [][]byte{{0x01, 0x02}},
mtu: 10,
res: [][]byte{
{0x9C, rands[0][0], rands[0][1], 0x01, 0x02},
},
},
"TwoPackets": {
b: [][]byte{{0x01, 0x02}},
mtu: 4,
res: [][]byte{
{0x98, rands[0][0], rands[0][1], 0x01},
{0x94, rands[0][0], rands[0][1], 0x02},
},
},
"ThreePackets": {
b: [][]byte{{0x01, 0x02, 0x03}},
mtu: 4,
res: [][]byte{
{0x98, rands[0][0], rands[0][1], 0x01},
{0x90, rands[0][0], rands[0][1], 0x02},
{0x94, rands[0][0], rands[0][1], 0x03},
},
},
"TwoFramesFourPackets": {
b: [][]byte{{0x01, 0x02, 0x03}, {0x04}},
mtu: 5,
res: [][]byte{
{0x98, rands[0][0], rands[0][1], 0x01, 0x02},
{0x94, rands[0][0], rands[0][1], 0x03},
{0x9C, rands[1][0], rands[1][1], 0x04},
},
},
}
for name, c := range cases {
pck := VP9Payloader{
InitialPictureIDFn: func() uint16 {
return uint16(rand.New(rand.NewSource(0)).Int31n(0x7FFF)) //nolint:gosec
},
}
c := c
t.Run(fmt.Sprintf("%s_MTU%d", name, c.mtu), func(t *testing.T) {
res := [][]byte{}
for _, b := range c.b {
res = append(res, pck.Payload(c.mtu, b)...)
}
if !reflect.DeepEqual(c.res, res) {
t.Errorf("Payloaded packet expected to be:\n %v\ngot:\n %v", c.res, res)
}
})
}
t.Run("PictureIDOverflow", func(t *testing.T) {
pck := VP9Payloader{
InitialPictureIDFn: func() uint16 {
return uint16(rand.New(rand.NewSource(0)).Int31n(0x7FFF)) //nolint:gosec
},
}
pPrev := VP9Packet{}
for i := 0; i < 0x8000; i++ {
res := pck.Payload(4, []byte{0x01})
p := VP9Packet{}
_, err := p.Unmarshal(res[0])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if i > 0 {
if pPrev.PictureID == 0x7FFF {
if p.PictureID != 0 {
t.Errorf("Picture ID next to 0x7FFF must be 0, got %d", p.PictureID)
}
} else if pPrev.PictureID+1 != p.PictureID {
t.Errorf("Picture ID next must be incremented by 1: %d -> %d", pPrev.PictureID, p.PictureID)
}
}
pPrev = p
}
})
}
func TestVP9IsPartitionHead(t *testing.T) {
vp9 := &VP9Packet{}
t.Run("SmallPacket", func(t *testing.T) {
if vp9.IsPartitionHead([]byte{}) {
t.Fatal("Small packet should not be the head of a new partition")
}
})
t.Run("NormalPacket", func(t *testing.T) {
if !vp9.IsPartitionHead([]byte{0x18, 0x00, 0x00}) {
t.Error("VP9 RTP packet with B flag should be head of a new partition")
}
if vp9.IsPartitionHead([]byte{0x10, 0x00, 0x00}) {
t.Error("VP9 RTP packet without B flag should not be head of a new partition")
}
})
}
|