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" "github.com/stretchr/testify/require"
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
"github.com/aler9/gortsplib/pkg/aac"
"github.com/aler9/gortsplib/pkg/auth" "github.com/aler9/gortsplib/pkg/auth"
"github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/headers"
@@ -29,25 +30,29 @@ func TestClientReadTracks(t *testing.T) {
} }
track2 := &TrackAAC{ track2 := &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 44100, Type: 2,
ChannelCount: 2, SampleRate: 44100,
AOTSpecificConfig: nil, ChannelCount: 2,
SizeLength: 13, AOTSpecificConfig: nil,
IndexLength: 3, },
IndexDeltaLength: 3, SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
} }
track3 := &TrackAAC{ track3 := &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 96000, Type: 2,
ChannelCount: 2, SampleRate: 96000,
AOTSpecificConfig: nil, ChannelCount: 2,
SizeLength: 13, AOTSpecificConfig: nil,
IndexLength: 3, },
IndexDeltaLength: 3, SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
} }
l, err := net.Listen("tcp", "localhost:8554") l, err := net.Listen("tcp", "localhost:8554")

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,6 +6,7 @@ import (
psdp "github.com/pion/sdp/v3" psdp "github.com/pion/sdp/v3"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/aler9/gortsplib/pkg/aac"
"github.com/aler9/gortsplib/pkg/url" "github.com/aler9/gortsplib/pkg/url"
) )
@@ -72,14 +73,16 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
}, },
}, },
&TrackAAC{ &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 48000, Type: 2,
ChannelCount: 2, SampleRate: 48000,
AOTSpecificConfig: []byte{0x01, 0x02}, ChannelCount: 2,
SizeLength: 13, AOTSpecificConfig: []byte{0x01, 0x02},
IndexLength: 3, },
IndexDeltaLength: 3, SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}, },
}, },
{ {
@@ -102,10 +105,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
}, },
}, },
&TrackAAC{ &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 48000, Type: 2,
ChannelCount: 2, SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13, SizeLength: 13,
IndexLength: 3, IndexLength: 3,
IndexDeltaLength: 3, IndexDeltaLength: 3,
@@ -131,10 +136,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
}, },
}, },
&TrackAAC{ &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 48000, Type: 2,
ChannelCount: 2, SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13, SizeLength: 13,
IndexLength: 3, IndexLength: 3,
IndexDeltaLength: 3, IndexDeltaLength: 3,
@@ -164,10 +171,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) {
}, },
}, },
&TrackAAC{ &TrackAAC{
PayloadType: 96, PayloadType: 96,
Type: 2, Config: &aac.MPEG4AudioConfig{
SampleRate: 48000, Type: 2,
ChannelCount: 2, SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13, SizeLength: 13,
IndexLength: 0, IndexLength: 0,
IndexDeltaLength: 0, IndexDeltaLength: 0,