mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
rtpaac: make SizeLength, IndexLength, IndexDeltaLength mandatory
This commit is contained in:
@@ -49,14 +49,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setup decoder
|
// setup decoder
|
||||||
v1 := aacTrack.SizeLength()
|
|
||||||
v2 := aacTrack.IndexLength()
|
|
||||||
v3 := aacTrack.IndexDeltaLength()
|
|
||||||
dec := &rtpaac.Decoder{
|
dec := &rtpaac.Decoder{
|
||||||
SampleRate: aacTrack.ClockRate(),
|
SampleRate: aacTrack.ClockRate(),
|
||||||
SizeLength: &v1,
|
SizeLength: aacTrack.SizeLength(),
|
||||||
IndexLength: &v2,
|
IndexLength: aacTrack.IndexLength(),
|
||||||
IndexDeltaLength: &v3,
|
IndexDeltaLength: aacTrack.IndexDeltaLength(),
|
||||||
}
|
}
|
||||||
dec.Init()
|
dec.Init()
|
||||||
|
|
||||||
|
@@ -21,17 +21,14 @@ type Decoder struct {
|
|||||||
// sample rate of input packets.
|
// sample rate of input packets.
|
||||||
SampleRate int
|
SampleRate int
|
||||||
|
|
||||||
// The number of bits on which the AU-size field is encoded in the AU-header (optional).
|
// The number of bits on which the AU-size field is encoded in the AU-header.
|
||||||
// It defaults to 13.
|
SizeLength int
|
||||||
SizeLength *int
|
|
||||||
|
|
||||||
// The number of bits on which the AU-Index is encoded in the first AU-header (optional).
|
// The number of bits on which the AU-Index is encoded in the first AU-header.
|
||||||
// It defaults to 3.
|
IndexLength int
|
||||||
IndexLength *int
|
|
||||||
|
|
||||||
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header (optional).
|
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header.
|
||||||
// It defaults to 3.
|
IndexDeltaLength int
|
||||||
IndexDeltaLength *int
|
|
||||||
|
|
||||||
timeDecoder *rtptimedec.Decoder
|
timeDecoder *rtptimedec.Decoder
|
||||||
fragmentedMode bool
|
fragmentedMode bool
|
||||||
@@ -41,19 +38,6 @@ type Decoder struct {
|
|||||||
|
|
||||||
// Init initializes the decoder
|
// Init initializes the decoder
|
||||||
func (d *Decoder) Init() {
|
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)
|
d.timeDecoder = rtptimedec.New(d.SampleRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,11 +138,11 @@ func (d *Decoder) readAUHeaders(payload []byte, headersLen int) ([]uint64, error
|
|||||||
count := 0
|
count := 0
|
||||||
for i := 0; i < headersLen; {
|
for i := 0; i < headersLen; {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
i += *d.SizeLength
|
i += d.SizeLength
|
||||||
i += *d.IndexLength
|
i += d.IndexLength
|
||||||
} else {
|
} else {
|
||||||
i += *d.SizeLength
|
i += d.SizeLength
|
||||||
i += *d.IndexDeltaLength
|
i += d.IndexDeltaLength
|
||||||
}
|
}
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
@@ -167,31 +151,31 @@ func (d *Decoder) readAUHeaders(payload []byte, headersLen int) ([]uint64, error
|
|||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for headersLen > 0 {
|
for headersLen > 0 {
|
||||||
dataLen, err := br.ReadBits(uint8(*d.SizeLength))
|
dataLen, err := br.ReadBits(uint8(d.SizeLength))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
headersLen -= *d.SizeLength
|
headersLen -= d.SizeLength
|
||||||
|
|
||||||
if !firstRead {
|
if !firstRead {
|
||||||
firstRead = true
|
firstRead = true
|
||||||
if *d.IndexLength > 0 {
|
if d.IndexLength > 0 {
|
||||||
auIndex, err := br.ReadBits(uint8(*d.IndexLength))
|
auIndex, err := br.ReadBits(uint8(d.IndexLength))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
headersLen -= *d.IndexLength
|
headersLen -= d.IndexLength
|
||||||
|
|
||||||
if auIndex != 0 {
|
if auIndex != 0 {
|
||||||
return nil, fmt.Errorf("AU-index different than zero is not supported")
|
return nil, fmt.Errorf("AU-index different than zero is not supported")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if *d.IndexDeltaLength > 0 {
|
} else if d.IndexDeltaLength > 0 {
|
||||||
auIndexDelta, err := br.ReadBits(uint8(*d.IndexDeltaLength))
|
auIndexDelta, err := br.ReadBits(uint8(d.IndexDeltaLength))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
headersLen -= *d.IndexDeltaLength
|
headersLen -= d.IndexDeltaLength
|
||||||
|
|
||||||
if auIndexDelta != 0 {
|
if auIndexDelta != 0 {
|
||||||
return nil, fmt.Errorf("AU-index-delta different than zero is not supported")
|
return nil, fmt.Errorf("AU-index-delta different than zero is not supported")
|
||||||
|
@@ -40,17 +40,14 @@ type Encoder struct {
|
|||||||
// sample rate of packets.
|
// sample rate of packets.
|
||||||
SampleRate int
|
SampleRate int
|
||||||
|
|
||||||
// The number of bits on which the AU-size field is encoded in the AU-header (optional).
|
// The number of bits on which the AU-size field is encoded in the AU-header.
|
||||||
// It defaults to 13.
|
SizeLength int
|
||||||
SizeLength *int
|
|
||||||
|
|
||||||
// The number of bits on which the AU-Index is encoded in the first AU-header (optional).
|
// The number of bits on which the AU-Index is encoded in the first AU-header.
|
||||||
// It defaults to 3.
|
IndexLength int
|
||||||
IndexLength *int
|
|
||||||
|
|
||||||
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header (optional).
|
// The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header.
|
||||||
// It defaults to 3.
|
IndexDeltaLength int
|
||||||
IndexDeltaLength *int
|
|
||||||
|
|
||||||
sequenceNumber uint16
|
sequenceNumber uint16
|
||||||
}
|
}
|
||||||
@@ -72,18 +69,6 @@ func (e *Encoder) Init() {
|
|||||||
if e.PayloadMaxSize == 0 {
|
if e.PayloadMaxSize == 0 {
|
||||||
e.PayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
|
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
|
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) {
|
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
|
auMaxSize := e.PayloadMaxSize - 2 - auHeaderLen/8
|
||||||
packetCount := len(au) / auMaxSize
|
packetCount := len(au) / auMaxSize
|
||||||
lastPacketSize := len(au) % auMaxSize
|
lastPacketSize := len(au) % auMaxSize
|
||||||
@@ -171,8 +156,8 @@ func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet,
|
|||||||
|
|
||||||
// AU-headers
|
// AU-headers
|
||||||
bw := bitio.NewWriter(bytes.NewBuffer(byts[2:2]))
|
bw := bitio.NewWriter(bytes.NewBuffer(byts[2:2]))
|
||||||
bw.WriteBits(uint64(le), uint8(*e.SizeLength))
|
bw.WriteBits(uint64(le), uint8(e.SizeLength))
|
||||||
bw.WriteBits(0, uint8(*e.IndexLength))
|
bw.WriteBits(0, uint8(e.IndexLength))
|
||||||
bw.Close()
|
bw.Close()
|
||||||
|
|
||||||
// AU
|
// AU
|
||||||
@@ -204,9 +189,9 @@ func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
|
|||||||
for _, au := range aus {
|
for _, au := range aus {
|
||||||
// AU-header
|
// AU-header
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
ret += (*e.SizeLength + *e.IndexLength) / 8
|
ret += (e.SizeLength + e.IndexLength) / 8
|
||||||
} else {
|
} else {
|
||||||
ret += (*e.SizeLength + *e.IndexDeltaLength) / 8
|
ret += (e.SizeLength + e.IndexDeltaLength) / 8
|
||||||
}
|
}
|
||||||
ret += len(au) // AU
|
ret += len(au) // AU
|
||||||
i++
|
i++
|
||||||
@@ -215,9 +200,9 @@ func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
|
|||||||
if addAU != nil {
|
if addAU != nil {
|
||||||
// AU-header
|
// AU-header
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
ret += (*e.SizeLength + *e.IndexLength) / 8
|
ret += (e.SizeLength + e.IndexLength) / 8
|
||||||
} else {
|
} else {
|
||||||
ret += (*e.SizeLength + *e.IndexDeltaLength) / 8
|
ret += (e.SizeLength + e.IndexDeltaLength) / 8
|
||||||
}
|
}
|
||||||
ret += len(addAU) // AU
|
ret += len(addAU) // AU
|
||||||
}
|
}
|
||||||
@@ -232,14 +217,14 @@ func (e *Encoder) writeAggregated(aus [][]byte, firstPTS time.Duration) ([]*rtp.
|
|||||||
written := 0
|
written := 0
|
||||||
bw := bitio.NewWriter(bytes.NewBuffer(payload[2:2]))
|
bw := bitio.NewWriter(bytes.NewBuffer(payload[2:2]))
|
||||||
for i, au := range aus {
|
for i, au := range aus {
|
||||||
bw.WriteBits(uint64(len(au)), uint8(*e.SizeLength))
|
bw.WriteBits(uint64(len(au)), uint8(e.SizeLength))
|
||||||
written += *e.SizeLength
|
written += e.SizeLength
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
bw.WriteBits(0, uint8(*e.IndexLength))
|
bw.WriteBits(0, uint8(e.IndexLength))
|
||||||
written += *e.IndexLength
|
written += e.IndexLength
|
||||||
} else {
|
} else {
|
||||||
bw.WriteBits(0, uint8(*e.IndexDeltaLength))
|
bw.WriteBits(0, uint8(e.IndexDeltaLength))
|
||||||
written += *e.IndexDeltaLength
|
written += e.IndexDeltaLength
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bw.Close()
|
bw.Close()
|
||||||
|
@@ -378,9 +378,9 @@ func TestDecode(t *testing.T) {
|
|||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
d := &Decoder{
|
d := &Decoder{
|
||||||
SampleRate: 48000,
|
SampleRate: 48000,
|
||||||
SizeLength: &ca.sizeLength,
|
SizeLength: ca.sizeLength,
|
||||||
IndexLength: &ca.indexLength,
|
IndexLength: ca.indexLength,
|
||||||
IndexDeltaLength: &ca.indexDeltaLength,
|
IndexDeltaLength: ca.indexDeltaLength,
|
||||||
}
|
}
|
||||||
d.Init()
|
d.Init()
|
||||||
|
|
||||||
@@ -662,7 +662,10 @@ func TestDecodeErrors(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
d := &Decoder{
|
d := &Decoder{
|
||||||
SampleRate: 48000,
|
SampleRate: 48000,
|
||||||
|
SizeLength: 13,
|
||||||
|
IndexLength: 3,
|
||||||
|
IndexDeltaLength: 3,
|
||||||
}
|
}
|
||||||
d.Init()
|
d.Init()
|
||||||
|
|
||||||
@@ -693,9 +696,9 @@ func TestEncode(t *testing.T) {
|
|||||||
v := uint32(0x88776655)
|
v := uint32(0x88776655)
|
||||||
return &v
|
return &v
|
||||||
}(),
|
}(),
|
||||||
SizeLength: &ca.sizeLength,
|
SizeLength: ca.sizeLength,
|
||||||
IndexLength: &ca.indexLength,
|
IndexLength: ca.indexLength,
|
||||||
IndexDeltaLength: &ca.indexDeltaLength,
|
IndexDeltaLength: ca.indexDeltaLength,
|
||||||
}
|
}
|
||||||
e.Init()
|
e.Init()
|
||||||
|
|
||||||
@@ -708,8 +711,11 @@ func TestEncode(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeRandomInitialState(t *testing.T) {
|
func TestEncodeRandomInitialState(t *testing.T) {
|
||||||
e := &Encoder{
|
e := &Encoder{
|
||||||
PayloadType: 96,
|
PayloadType: 96,
|
||||||
SampleRate: 48000,
|
SampleRate: 48000,
|
||||||
|
SizeLength: 13,
|
||||||
|
IndexLength: 3,
|
||||||
|
IndexDeltaLength: 3,
|
||||||
}
|
}
|
||||||
e.Init()
|
e.Init()
|
||||||
require.NotEqual(t, nil, e.SSRC)
|
require.NotEqual(t, nil, e.SSRC)
|
||||||
|
Reference in New Issue
Block a user