rtpaac: make SizeLength, IndexLength, IndexDeltaLength mandatory

This commit is contained in:
aler9
2022-04-15 16:55:06 +02:00
parent 430b594283
commit efe7b064c3
4 changed files with 55 additions and 83 deletions

View File

@@ -49,14 +49,11 @@ func main() {
}
// setup decoder
v1 := aacTrack.SizeLength()
v2 := aacTrack.IndexLength()
v3 := aacTrack.IndexDeltaLength()
dec := &rtpaac.Decoder{
SampleRate: aacTrack.ClockRate(),
SizeLength: &v1,
IndexLength: &v2,
IndexDeltaLength: &v3,
SizeLength: aacTrack.SizeLength(),
IndexLength: aacTrack.IndexLength(),
IndexDeltaLength: aacTrack.IndexDeltaLength(),
}
dec.Init()

View File

@@ -21,17 +21,14 @@ type Decoder struct {
// sample rate of input packets.
SampleRate int
// The number of bits on which the AU-size field is encoded in the AU-header (optional).
// It defaults to 13.
SizeLength *int
// The number of bits on which the AU-size field is encoded in the AU-header.
SizeLength int
// The number of bits on which the AU-Index is encoded in the first AU-header (optional).
// It defaults to 3.
IndexLength *int
// The number of bits on which the AU-Index is encoded in the first AU-header.
IndexLength int
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header (optional).
// It defaults to 3.
IndexDeltaLength *int
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header.
IndexDeltaLength int
timeDecoder *rtptimedec.Decoder
fragmentedMode bool
@@ -41,19 +38,6 @@ type Decoder struct {
// Init initializes the decoder
func (d *Decoder) Init() {
if d.SizeLength == nil {
v := 13
d.SizeLength = &v
}
if d.IndexLength == nil {
v := 3
d.IndexLength = &v
}
if d.IndexDeltaLength == nil {
v := 3
d.IndexDeltaLength = &v
}
d.timeDecoder = rtptimedec.New(d.SampleRate)
}
@@ -154,11 +138,11 @@ func (d *Decoder) readAUHeaders(payload []byte, headersLen int) ([]uint64, error
count := 0
for i := 0; i < headersLen; {
if i == 0 {
i += *d.SizeLength
i += *d.IndexLength
i += d.SizeLength
i += d.IndexLength
} else {
i += *d.SizeLength
i += *d.IndexDeltaLength
i += d.SizeLength
i += d.IndexDeltaLength
}
count++
}
@@ -167,31 +151,31 @@ func (d *Decoder) readAUHeaders(payload []byte, headersLen int) ([]uint64, error
i := 0
for headersLen > 0 {
dataLen, err := br.ReadBits(uint8(*d.SizeLength))
dataLen, err := br.ReadBits(uint8(d.SizeLength))
if err != nil {
return nil, err
}
headersLen -= *d.SizeLength
headersLen -= d.SizeLength
if !firstRead {
firstRead = true
if *d.IndexLength > 0 {
auIndex, err := br.ReadBits(uint8(*d.IndexLength))
if d.IndexLength > 0 {
auIndex, err := br.ReadBits(uint8(d.IndexLength))
if err != nil {
return nil, err
}
headersLen -= *d.IndexLength
headersLen -= d.IndexLength
if auIndex != 0 {
return nil, fmt.Errorf("AU-index different than zero is not supported")
}
}
} else if *d.IndexDeltaLength > 0 {
auIndexDelta, err := br.ReadBits(uint8(*d.IndexDeltaLength))
} else if d.IndexDeltaLength > 0 {
auIndexDelta, err := br.ReadBits(uint8(d.IndexDeltaLength))
if err != nil {
return nil, err
}
headersLen -= *d.IndexDeltaLength
headersLen -= d.IndexDeltaLength
if auIndexDelta != 0 {
return nil, fmt.Errorf("AU-index-delta different than zero is not supported")

View File

@@ -40,17 +40,14 @@ type Encoder struct {
// sample rate of packets.
SampleRate int
// The number of bits on which the AU-size field is encoded in the AU-header (optional).
// It defaults to 13.
SizeLength *int
// The number of bits on which the AU-size field is encoded in the AU-header.
SizeLength int
// The number of bits on which the AU-Index is encoded in the first AU-header (optional).
// It defaults to 3.
IndexLength *int
// The number of bits on which the AU-Index is encoded in the first AU-header.
IndexLength int
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header (optional).
// It defaults to 3.
IndexDeltaLength *int
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header.
IndexDeltaLength int
sequenceNumber uint16
}
@@ -72,18 +69,6 @@ func (e *Encoder) Init() {
if e.PayloadMaxSize == 0 {
e.PayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
}
if e.SizeLength == nil {
v := 13
e.SizeLength = &v
}
if e.IndexLength == nil {
v := 3
e.IndexLength = &v
}
if e.IndexDeltaLength == nil {
v := 3
e.IndexDeltaLength = &v
}
e.sequenceNumber = *e.InitialSequenceNumber
}
@@ -145,7 +130,7 @@ func (e *Encoder) writeBatch(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packe
}
func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet, error) {
auHeaderLen := *e.SizeLength + *e.IndexLength
auHeaderLen := e.SizeLength + e.IndexLength
auMaxSize := e.PayloadMaxSize - 2 - auHeaderLen/8
packetCount := len(au) / auMaxSize
lastPacketSize := len(au) % auMaxSize
@@ -171,8 +156,8 @@ func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet,
// AU-headers
bw := bitio.NewWriter(bytes.NewBuffer(byts[2:2]))
bw.WriteBits(uint64(le), uint8(*e.SizeLength))
bw.WriteBits(0, uint8(*e.IndexLength))
bw.WriteBits(uint64(le), uint8(e.SizeLength))
bw.WriteBits(0, uint8(e.IndexLength))
bw.Close()
// AU
@@ -204,9 +189,9 @@ func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
for _, au := range aus {
// AU-header
if i == 0 {
ret += (*e.SizeLength + *e.IndexLength) / 8
ret += (e.SizeLength + e.IndexLength) / 8
} else {
ret += (*e.SizeLength + *e.IndexDeltaLength) / 8
ret += (e.SizeLength + e.IndexDeltaLength) / 8
}
ret += len(au) // AU
i++
@@ -215,9 +200,9 @@ func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
if addAU != nil {
// AU-header
if i == 0 {
ret += (*e.SizeLength + *e.IndexLength) / 8
ret += (e.SizeLength + e.IndexLength) / 8
} else {
ret += (*e.SizeLength + *e.IndexDeltaLength) / 8
ret += (e.SizeLength + e.IndexDeltaLength) / 8
}
ret += len(addAU) // AU
}
@@ -232,14 +217,14 @@ func (e *Encoder) writeAggregated(aus [][]byte, firstPTS time.Duration) ([]*rtp.
written := 0
bw := bitio.NewWriter(bytes.NewBuffer(payload[2:2]))
for i, au := range aus {
bw.WriteBits(uint64(len(au)), uint8(*e.SizeLength))
written += *e.SizeLength
bw.WriteBits(uint64(len(au)), uint8(e.SizeLength))
written += e.SizeLength
if i == 0 {
bw.WriteBits(0, uint8(*e.IndexLength))
written += *e.IndexLength
bw.WriteBits(0, uint8(e.IndexLength))
written += e.IndexLength
} else {
bw.WriteBits(0, uint8(*e.IndexDeltaLength))
written += *e.IndexDeltaLength
bw.WriteBits(0, uint8(e.IndexDeltaLength))
written += e.IndexDeltaLength
}
}
bw.Close()

View File

@@ -378,9 +378,9 @@ func TestDecode(t *testing.T) {
t.Run(ca.name, func(t *testing.T) {
d := &Decoder{
SampleRate: 48000,
SizeLength: &ca.sizeLength,
IndexLength: &ca.indexLength,
IndexDeltaLength: &ca.indexDeltaLength,
SizeLength: ca.sizeLength,
IndexLength: ca.indexLength,
IndexDeltaLength: ca.indexDeltaLength,
}
d.Init()
@@ -662,7 +662,10 @@ func TestDecodeErrors(t *testing.T) {
} {
t.Run(ca.name, func(t *testing.T) {
d := &Decoder{
SampleRate: 48000,
SampleRate: 48000,
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
d.Init()
@@ -693,9 +696,9 @@ func TestEncode(t *testing.T) {
v := uint32(0x88776655)
return &v
}(),
SizeLength: &ca.sizeLength,
IndexLength: &ca.indexLength,
IndexDeltaLength: &ca.indexDeltaLength,
SizeLength: ca.sizeLength,
IndexLength: ca.indexLength,
IndexDeltaLength: ca.indexDeltaLength,
}
e.Init()
@@ -708,8 +711,11 @@ func TestEncode(t *testing.T) {
func TestEncodeRandomInitialState(t *testing.T) {
e := &Encoder{
PayloadType: 96,
SampleRate: 48000,
PayloadType: 96,
SampleRate: 48000,
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
e.Init()
require.NotEqual(t, nil, e.SSRC)