store MPEG4AudioConfig inside TrackAAC instead of storing single fields

This commit is contained in:
aler9
2022-06-24 13:25:53 +02:00
parent 61726e534c
commit e82968442d
6 changed files with 95 additions and 91 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/net/ipv4"
"github.com/aler9/gortsplib/pkg/aac"
"github.com/aler9/gortsplib/pkg/auth"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/headers"
@@ -30,10 +31,12 @@ func TestClientReadTracks(t *testing.T) {
track2 := &TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
AOTSpecificConfig: nil,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
@@ -41,10 +44,12 @@ func TestClientReadTracks(t *testing.T) {
track3 := &TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 96000,
ChannelCount: 2,
AOTSpecificConfig: nil,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,

View File

@@ -5,6 +5,7 @@ import (
"net"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/aac"
"github.com/pion/rtp"
)
@@ -36,9 +37,11 @@ func main() {
// create an AAC track
track := &gortsplib.TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,

View File

@@ -50,7 +50,7 @@ func main() {
// setup decoder
dec := &rtpaac.Decoder{
SampleRate: aacTrack.SampleRate,
SampleRate: aacTrack.Config.SampleRate,
SizeLength: aacTrack.SizeLength,
IndexLength: aacTrack.IndexLength,
IndexDeltaLength: aacTrack.IndexDeltaLength,

View File

@@ -14,10 +14,7 @@ import (
// TrackAAC is an AAC track.
type TrackAAC struct {
PayloadType uint8
Type int
SampleRate int
ChannelCount int
AOTSpecificConfig []byte
Config *aac.MPEG4AudioConfig
SizeLength int
IndexLength int
IndexDeltaLength int
@@ -47,8 +44,6 @@ func newTrackAACFromMediaDescription(
},
}
configFound := false
for _, kv := range strings.Split(tmp[1], ";") {
kv = strings.Trim(kv, " ")
@@ -68,18 +63,12 @@ func newTrackAACFromMediaDescription(
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
var MPEGConf aac.MPEG4AudioConfig
err = MPEGConf.Decode(enc)
t.Config = &aac.MPEG4AudioConfig{}
err = t.Config.Decode(enc)
if err != nil {
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
t.Type = int(MPEGConf.Type)
t.SampleRate = MPEGConf.SampleRate
t.ChannelCount = MPEGConf.ChannelCount
t.AOTSpecificConfig = MPEGConf.AOTSpecificConfig
configFound = true
case "sizelength":
val, err := strconv.ParseUint(tmp[1], 10, 64)
if err != nil {
@@ -103,7 +92,7 @@ func newTrackAACFromMediaDescription(
}
}
if !configFound {
if t.Config == nil {
return nil, fmt.Errorf("config is missing (%v)", v)
}
@@ -116,16 +105,13 @@ func newTrackAACFromMediaDescription(
// ClockRate returns the track clock rate.
func (t *TrackAAC) ClockRate() int {
return t.SampleRate
return t.Config.SampleRate
}
func (t *TrackAAC) clone() Track {
return &TrackAAC{
PayloadType: t.PayloadType,
Type: t.Type,
SampleRate: t.SampleRate,
ChannelCount: t.ChannelCount,
AOTSpecificConfig: t.AOTSpecificConfig,
Config: t.Config,
SizeLength: t.SizeLength,
IndexLength: t.IndexLength,
IndexDeltaLength: t.IndexDeltaLength,
@@ -135,12 +121,7 @@ func (t *TrackAAC) clone() Track {
// MediaDescription returns the track media description in SDP format.
func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
mpegConf, err := aac.MPEG4AudioConfig{
Type: aac.MPEG4AudioType(t.Type),
SampleRate: t.SampleRate,
ChannelCount: t.ChannelCount,
AOTSpecificConfig: t.AOTSpecificConfig,
}.Encode()
enc, err := t.Config.Encode()
if err != nil {
return nil
}
@@ -156,8 +137,8 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: typ + " mpeg4-generic/" + strconv.FormatInt(int64(t.SampleRate), 10) +
"/" + strconv.FormatInt(int64(t.ChannelCount), 10),
Value: typ + " mpeg4-generic/" + strconv.FormatInt(int64(t.Config.SampleRate), 10) +
"/" + strconv.FormatInt(int64(t.Config.ChannelCount), 10),
},
{
Key: "fmtp",
@@ -181,7 +162,7 @@ func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
}
return ""
}() +
"config=" + hex.EncodeToString(mpegConf),
"config=" + hex.EncodeToString(enc),
},
{
Key: "control",

View File

@@ -5,15 +5,19 @@ import (
psdp "github.com/pion/sdp/v3"
"github.com/stretchr/testify/require"
"github.com/aler9/gortsplib/pkg/aac"
)
func TestTrackAACClone(t *testing.T) {
track := &TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
AOTSpecificConfig: []byte{0x01, 0x02},
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
@@ -27,9 +31,11 @@ func TestTrackAACClone(t *testing.T) {
func TestTrackAACMediaDescription(t *testing.T) {
track := &TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,

View File

@@ -6,6 +6,7 @@ import (
psdp "github.com/pion/sdp/v3"
"github.com/stretchr/testify/require"
"github.com/aler9/gortsplib/pkg/aac"
"github.com/aler9/gortsplib/pkg/url"
)
@@ -73,10 +74,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
},
&TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
AOTSpecificConfig: []byte{0x01, 0x02},
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
@@ -103,9 +106,11 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
},
&TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
@@ -132,9 +137,11 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
},
&TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
@@ -165,9 +172,11 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
},
&TrackAAC{
PayloadType: 96,
Config: &aac.MPEG4AudioConfig{
Type: 2,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 0,
IndexDeltaLength: 0,