Skip to content

Commit 3414414

Browse files
authored
improve tests (#743)
1 parent d59d7c4 commit 3414414

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

pkg/format/rtpav1/decoder.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func joinFragments(fragments [][]byte, size int) []byte {
2828
return ret
2929
}
3030

31+
func tuSize(tu [][]byte) int {
32+
s := 0
33+
for _, obu := range tu {
34+
s += len(obu)
35+
}
36+
return s
37+
}
38+
3139
// Decoder is a RTP/AV1 decoder.
3240
// Specification: https://aomediacodec.github.io/av1-rtp-spec/
3341
type Decoder struct {
@@ -107,15 +115,7 @@ func (d *Decoder) decodeOBUs(pkt *rtp.Packet) ([][]byte, error) {
107115
if av1header.Y {
108116
elementCount := len(av1header.OBUElements)
109117

110-
d.fragmentsSize += len(av1header.OBUElements[elementCount-1])
111-
112-
if d.fragmentsSize > av1.MaxTemporalUnitSize {
113-
errSize := d.fragmentsSize
114-
d.resetFragments()
115-
return nil, fmt.Errorf("temporal unit size (%d) is too big, maximum is %d",
116-
errSize, av1.MaxTemporalUnitSize)
117-
}
118-
118+
d.fragmentsSize = len(av1header.OBUElements[elementCount-1])
119119
d.fragments = append(d.fragments, av1header.OBUElements[elementCount-1])
120120
av1header.OBUElements = av1header.OBUElements[:elementCount-1]
121121
d.fragmentNextSeqNum = pkt.SequenceNumber + 1
@@ -151,11 +151,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
151151
errCount, av1.MaxOBUsPerTemporalUnit)
152152
}
153153

154-
addSize := 0
155-
156-
for _, obu := range obus {
157-
addSize += len(obu)
158-
}
154+
addSize := tuSize(obus)
159155

160156
if (d.frameBufferSize + addSize) > av1.MaxTemporalUnitSize {
161157
errSize := d.frameBufferSize + addSize

pkg/format/rtpav1/decoder_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rtpav1
22

33
import (
4+
"bytes"
45
"errors"
56
"testing"
67

@@ -33,6 +34,48 @@ func TestDecode(t *testing.T) {
3334
}
3435
}
3536

37+
func TestDecoderErrorTUSize(t *testing.T) {
38+
d := &Decoder{}
39+
err := d.Init()
40+
require.NoError(t, err)
41+
42+
size := 0
43+
i := uint16(0)
44+
45+
for size < av1.MaxTemporalUnitSize {
46+
var header byte
47+
if i == 0 {
48+
header = 0b01000000
49+
} else {
50+
header = 0b11000000
51+
}
52+
53+
fragmentLenLEB := av1.LEB128(1400)
54+
buf := make([]byte, fragmentLenLEB.MarshalSize())
55+
fragmentLenLEB.MarshalTo(buf)
56+
57+
payload := append([]byte{header}, buf...)
58+
payload = append(payload, bytes.Repeat([]byte{1, 2, 3, 4}, 1400/4)...)
59+
60+
_, err = d.Decode(&rtp.Packet{
61+
Header: rtp.Header{
62+
Version: 2,
63+
Marker: false,
64+
PayloadType: 96,
65+
SequenceNumber: 17645 + i,
66+
Timestamp: 2289527317,
67+
SSRC: 0x9dbb7812,
68+
},
69+
Payload: payload,
70+
})
71+
72+
size += 1400
73+
i++
74+
}
75+
76+
require.EqualError(t, err, "temporal unit size (3145800) is too big, maximum is 3145728")
77+
}
78+
3679
func TestDecoderErrorOBUCount(t *testing.T) {
3780
d := &Decoder{}
3881
err := d.Init()

pkg/format/rtph264/decoder.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ func isAllZero(buf []byte) bool {
3838
return true
3939
}
4040

41+
func auSize(au [][]byte) int {
42+
s := 0
43+
for _, nalu := range au {
44+
s += len(nalu)
45+
}
46+
return s
47+
}
48+
4149
// Decoder is a RTP/H264 decoder.
4250
// Specification: https://datatracker.ietf.org/doc/html/rfc6184
4351
type Decoder struct {
@@ -223,11 +231,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
223231
errCount, h264.MaxNALUsPerAccessUnit)
224232
}
225233

226-
addSize := 0
227-
228-
for _, nalu := range nalus {
229-
addSize += len(nalu)
230-
}
234+
addSize := auSize(nalus)
231235

232236
if (d.frameBufferSize + addSize) > h264.MaxAccessUnitSize {
233237
errSize := d.frameBufferSize + addSize

pkg/format/rtph265/decoder.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func joinFragments(fragments [][]byte, size int) []byte {
2828
return ret
2929
}
3030

31+
func auSize(au [][]byte) int {
32+
s := 0
33+
for _, nalu := range au {
34+
s += len(nalu)
35+
}
36+
return s
37+
}
38+
3139
// Decoder is a RTP/H265 decoder.
3240
// Specification: https://datatracker.ietf.org/doc/html/rfc7798
3341
type Decoder struct {
@@ -182,11 +190,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
182190
errCount, h265.MaxNALUsPerAccessUnit)
183191
}
184192

185-
addSize := 0
186-
187-
for _, nalu := range nalus {
188-
addSize += len(nalu)
189-
}
193+
addSize := auSize(nalus)
190194

191195
if (d.frameBufferSize + addSize) > h265.MaxAccessUnitSize {
192196
errSize := d.frameBufferSize + addSize

0 commit comments

Comments
 (0)