diff --git a/client_read_test.go b/client_read_test.go index 93966c9e..12b8a4af 100644 --- a/client_read_test.go +++ b/client_read_test.go @@ -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" @@ -29,25 +30,29 @@ func TestClientReadTracks(t *testing.T) { } track2 := &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 44100, - ChannelCount: 2, - AOTSpecificConfig: nil, - SizeLength: 13, - IndexLength: 3, - IndexDeltaLength: 3, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 44100, + ChannelCount: 2, + AOTSpecificConfig: nil, + }, + SizeLength: 13, + IndexLength: 3, + IndexDeltaLength: 3, } track3 := &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 96000, - ChannelCount: 2, - AOTSpecificConfig: nil, - SizeLength: 13, - IndexLength: 3, - IndexDeltaLength: 3, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 96000, + ChannelCount: 2, + AOTSpecificConfig: nil, + }, + SizeLength: 13, + IndexLength: 3, + IndexDeltaLength: 3, } l, err := net.Listen("tcp", "localhost:8554") diff --git a/examples/client-publish-aac/main.go b/examples/client-publish-aac/main.go index f0d8fbf7..179205bc 100644 --- a/examples/client-publish-aac/main.go +++ b/examples/client-publish-aac/main.go @@ -5,6 +5,7 @@ import ( "net" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/aac" "github.com/pion/rtp" ) @@ -35,10 +36,12 @@ func main() { // create an AAC track track := &gortsplib.TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + }, SizeLength: 13, IndexLength: 3, IndexDeltaLength: 3, diff --git a/examples/client-read-aac/main.go b/examples/client-read-aac/main.go index 47307de8..38ec96a2 100644 --- a/examples/client-read-aac/main.go +++ b/examples/client-read-aac/main.go @@ -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, diff --git a/track_aac.go b/track_aac.go index 5dabb5d2..1faa78a0 100644 --- a/track_aac.go +++ b/track_aac.go @@ -13,14 +13,11 @@ import ( // TrackAAC is an AAC track. type TrackAAC struct { - PayloadType uint8 - Type int - SampleRate int - ChannelCount int - AOTSpecificConfig []byte - SizeLength int - IndexLength int - IndexDeltaLength int + PayloadType uint8 + Config *aac.MPEG4AudioConfig + SizeLength int + IndexLength int + IndexDeltaLength int trackBase } @@ -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,31 +105,23 @@ 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, - SizeLength: t.SizeLength, - IndexLength: t.IndexLength, - IndexDeltaLength: t.IndexDeltaLength, - trackBase: t.trackBase, + PayloadType: t.PayloadType, + Config: t.Config, + SizeLength: t.SizeLength, + IndexLength: t.IndexLength, + IndexDeltaLength: t.IndexDeltaLength, + trackBase: t.trackBase, } } // 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", diff --git a/track_aac_test.go b/track_aac_test.go index a6a39d8f..7e9974a3 100644 --- a/track_aac_test.go +++ b/track_aac_test.go @@ -5,18 +5,22 @@ 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, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, - AOTSpecificConfig: []byte{0x01, 0x02}, - SizeLength: 13, - IndexLength: 3, - IndexDeltaLength: 3, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + AOTSpecificConfig: []byte{0x01, 0x02}, + }, + SizeLength: 13, + IndexLength: 3, + IndexDeltaLength: 3, } clone := track.clone() @@ -26,10 +30,12 @@ func TestTrackAACClone(t *testing.T) { func TestTrackAACMediaDescription(t *testing.T) { track := &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + }, SizeLength: 13, IndexLength: 3, IndexDeltaLength: 3, diff --git a/track_test.go b/track_test.go index 8838ab6b..7d89dbe5 100644 --- a/track_test.go +++ b/track_test.go @@ -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" ) @@ -72,14 +73,16 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, }, &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, - AOTSpecificConfig: []byte{0x01, 0x02}, - SizeLength: 13, - IndexLength: 3, - IndexDeltaLength: 3, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + AOTSpecificConfig: []byte{0x01, 0x02}, + }, + SizeLength: 13, + IndexLength: 3, + IndexDeltaLength: 3, }, }, { @@ -102,10 +105,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, }, &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + }, SizeLength: 13, IndexLength: 3, IndexDeltaLength: 3, @@ -131,10 +136,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, }, &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + }, SizeLength: 13, IndexLength: 3, IndexDeltaLength: 3, @@ -164,10 +171,12 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, }, &TrackAAC{ - PayloadType: 96, - Type: 2, - SampleRate: 48000, - ChannelCount: 2, + PayloadType: 96, + Config: &aac.MPEG4AudioConfig{ + Type: 2, + SampleRate: 48000, + ChannelCount: 2, + }, SizeLength: 13, IndexLength: 0, IndexDeltaLength: 0,