diff --git a/client_read_test.go b/client_read_test.go index d9552f83..03f5d060 100644 --- a/client_read_test.go +++ b/client_read_test.go @@ -15,10 +15,10 @@ 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" + "github.com/aler9/gortsplib/pkg/mpeg4audio" "github.com/aler9/gortsplib/pkg/url" ) @@ -57,7 +57,7 @@ func TestClientReadTracks(t *testing.T) { track2 := &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 44100, ChannelCount: 2, @@ -69,7 +69,7 @@ func TestClientReadTracks(t *testing.T) { track3 := &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 96000, ChannelCount: 2, diff --git a/examples/client-publish-aac/main.go b/examples/client-publish-aac/main.go index 179205bc..ef5306f7 100644 --- a/examples/client-publish-aac/main.go +++ b/examples/client-publish-aac/main.go @@ -5,7 +5,7 @@ import ( "net" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/aac" + "github.com/aler9/gortsplib/pkg/mpeg4audio" "github.com/pion/rtp" ) @@ -37,7 +37,7 @@ func main() { // create an AAC track track := &gortsplib.TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, diff --git a/pkg/aac/audiotype.go b/pkg/aac/audiotype.go deleted file mode 100644 index b5954609..00000000 --- a/pkg/aac/audiotype.go +++ /dev/null @@ -1,9 +0,0 @@ -package aac - -// MPEG4AudioType is the type of a MPEG-4 Audio stream. -type MPEG4AudioType int - -// MPEG-4 Audio types. -const ( - MPEG4AudioTypeAACLC MPEG4AudioType = 2 -) diff --git a/pkg/aac/aac.go b/pkg/mpeg4audio/aac.go similarity index 69% rename from pkg/aac/aac.go rename to pkg/mpeg4audio/aac.go index c605170f..62261185 100644 --- a/pkg/aac/aac.go +++ b/pkg/mpeg4audio/aac.go @@ -1,5 +1,5 @@ -// Package aac contains utilities to work with the AAC codec. -package aac +// Package mpeg4audio contains utilities to work with MPEG-4 audio codecs. +package mpeg4audio const ( // MaxAccessUnitSize is the maximum size of an Access Unit (AU). diff --git a/pkg/aac/adts.go b/pkg/mpeg4audio/adts.go similarity index 96% rename from pkg/aac/adts.go rename to pkg/mpeg4audio/adts.go index 33d02b6a..8922991e 100644 --- a/pkg/aac/adts.go +++ b/pkg/mpeg4audio/adts.go @@ -1,4 +1,4 @@ -package aac +package mpeg4audio import ( "fmt" @@ -6,7 +6,7 @@ import ( // ADTSPacket is an ADTS packet. type ADTSPacket struct { - Type MPEG4AudioType + Type ObjectType SampleRate int ChannelCount int AU []byte @@ -39,9 +39,9 @@ func (ps *ADTSPackets) Unmarshal(buf []byte) error { pkt := &ADTSPacket{} - pkt.Type = MPEG4AudioType((buf[pos+2] >> 6) + 1) + pkt.Type = ObjectType((buf[pos+2] >> 6) + 1) switch pkt.Type { - case MPEG4AudioTypeAACLC: + case ObjectTypeAACLC: default: return fmt.Errorf("unsupported audio type: %d", pkt.Type) } diff --git a/pkg/aac/adts_test.go b/pkg/mpeg4audio/adts_test.go similarity index 99% rename from pkg/aac/adts_test.go rename to pkg/mpeg4audio/adts_test.go index 53c85bd3..166c8997 100644 --- a/pkg/aac/adts_test.go +++ b/pkg/mpeg4audio/adts_test.go @@ -1,4 +1,4 @@ -package aac +package mpeg4audio import ( "testing" diff --git a/pkg/aac/mpeg4audioconfig.go b/pkg/mpeg4audio/config.go similarity index 86% rename from pkg/aac/mpeg4audioconfig.go rename to pkg/mpeg4audio/config.go index 682fa4f2..8d19cd45 100644 --- a/pkg/aac/mpeg4audioconfig.go +++ b/pkg/mpeg4audio/config.go @@ -1,4 +1,4 @@ -package aac +package mpeg4audio import ( "fmt" @@ -6,9 +6,9 @@ import ( "github.com/aler9/gortsplib/pkg/bits" ) -// MPEG4AudioConfig is a MPEG-4 Audio configuration. -type MPEG4AudioConfig struct { - Type MPEG4AudioType +// Config is a MPEG-4 Audio configuration. +type Config struct { + Type ObjectType SampleRate int ChannelCount int @@ -18,8 +18,8 @@ type MPEG4AudioConfig struct { CoreCoderDelay uint16 } -// Unmarshal decodes an MPEG4AudioConfig. -func (c *MPEG4AudioConfig) Unmarshal(buf []byte) error { +// Unmarshal decodes a Config. +func (c *Config) Unmarshal(buf []byte) error { // ref: ISO 14496-3 pos := 0 @@ -28,10 +28,10 @@ func (c *MPEG4AudioConfig) Unmarshal(buf []byte) error { if err != nil { return err } - c.Type = MPEG4AudioType(tmp) + c.Type = ObjectType(tmp) switch c.Type { - case MPEG4AudioTypeAACLC: + case ObjectTypeAACLC: default: return fmt.Errorf("unsupported type: %d", c.Type) } @@ -105,7 +105,7 @@ func (c *MPEG4AudioConfig) Unmarshal(buf []byte) error { return nil } -func (c MPEG4AudioConfig) marshalSize() int { +func (c Config) marshalSize() int { n := 5 + 4 + 3 _, ok := reverseSampleRates[c.SampleRate] @@ -127,8 +127,8 @@ func (c MPEG4AudioConfig) marshalSize() int { return ret } -// Marshal encodes an MPEG4AudioConfig. -func (c MPEG4AudioConfig) Marshal() ([]byte, error) { +// Marshal encodes a Config. +func (c Config) Marshal() ([]byte, error) { buf := make([]byte, c.marshalSize()) pos := 0 diff --git a/pkg/aac/mpeg4audioconfig_test.go b/pkg/mpeg4audio/config_test.go similarity index 78% rename from pkg/aac/mpeg4audioconfig_test.go rename to pkg/mpeg4audio/config_test.go index 259aa9d4..40ee5af8 100644 --- a/pkg/aac/mpeg4audioconfig_test.go +++ b/pkg/mpeg4audio/config_test.go @@ -1,4 +1,4 @@ -package aac +package mpeg4audio import ( "testing" @@ -9,13 +9,13 @@ import ( var configCases = []struct { name string enc []byte - dec MPEG4AudioConfig + dec Config }{ { "aac-lc 16khz mono", []byte{0x14, 0x08}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 16000, ChannelCount: 1, }, @@ -23,8 +23,8 @@ var configCases = []struct { { "aac-lc 44.1khz mono", []byte{0x12, 0x08}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 44100, ChannelCount: 1, }, @@ -32,8 +32,8 @@ var configCases = []struct { { "aac-lc 44.1khz 5.1", []byte{0x12, 0x30}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 44100, ChannelCount: 6, }, @@ -41,8 +41,8 @@ var configCases = []struct { { "aac-lc 48khz stereo", []byte{17, 144}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 48000, ChannelCount: 2, }, @@ -50,8 +50,8 @@ var configCases = []struct { { "aac-lc 53khz stereo", []byte{0x17, 0x80, 0x67, 0x84, 0x10}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 53000, ChannelCount: 2, }, @@ -59,8 +59,8 @@ var configCases = []struct { { "aac-lc 96khz stereo delay", []byte{0x10, 0x12, 0x0c, 0x08}, - MPEG4AudioConfig{ - Type: MPEG4AudioTypeAACLC, + Config{ + Type: ObjectTypeAACLC, SampleRate: 96000, ChannelCount: 2, DependsOnCoreCoder: true, @@ -72,7 +72,7 @@ var configCases = []struct { func TestConfigUnmarshal(t *testing.T) { for _, ca := range configCases { t.Run(ca.name, func(t *testing.T) { - var dec MPEG4AudioConfig + var dec Config err := dec.Unmarshal(ca.enc) require.NoError(t, err) require.Equal(t, ca.dec, dec) @@ -93,12 +93,12 @@ func TestConfigMarshal(t *testing.T) { func TestConfigMarshalErrors(t *testing.T) { for _, ca := range []struct { name string - conf MPEG4AudioConfig + conf Config err string }{ { "invalid channel config", - MPEG4AudioConfig{ + Config{ Type: 2, SampleRate: 44100, ChannelCount: 0, diff --git a/pkg/mpeg4audio/objecttype.go b/pkg/mpeg4audio/objecttype.go new file mode 100644 index 00000000..0d84b0b3 --- /dev/null +++ b/pkg/mpeg4audio/objecttype.go @@ -0,0 +1,9 @@ +package mpeg4audio + +// ObjectType is a MPEG-4 Audio object type. +type ObjectType int + +// supported types. +const ( + ObjectTypeAACLC ObjectType = 2 +) diff --git a/pkg/aac/samplerates.go b/pkg/mpeg4audio/samplerates.go similarity index 94% rename from pkg/aac/samplerates.go rename to pkg/mpeg4audio/samplerates.go index 96a42876..660b86e2 100644 --- a/pkg/aac/samplerates.go +++ b/pkg/mpeg4audio/samplerates.go @@ -1,4 +1,4 @@ -package aac +package mpeg4audio var sampleRates = []int{ 96000, diff --git a/pkg/rtpaac/decoder.go b/pkg/rtpaac/decoder.go index 4bf803c4..1a6ca685 100644 --- a/pkg/rtpaac/decoder.go +++ b/pkg/rtpaac/decoder.go @@ -7,8 +7,8 @@ import ( "github.com/pion/rtp" - "github.com/aler9/gortsplib/pkg/aac" "github.com/aler9/gortsplib/pkg/bits" + "github.com/aler9/gortsplib/pkg/mpeg4audio" "github.com/aler9/gortsplib/pkg/rtptimedec" ) @@ -44,7 +44,7 @@ func (d *Decoder) Init() { // Decode decodes AUs from a RTP/AAC packet. // It returns the AUs and the PTS of the first AU. -// The PTS of subsequent AUs can be calculated by adding time.Second*aac.SamplesPerAccessUnit/clockRate. +// The PTS of subsequent AUs can be calculated by adding time.Second*mpeg4audio.SamplesPerAccessUnit/clockRate. func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, time.Duration, error) { if len(pkt.Payload) < 2 { d.fragmentedParts = d.fragmentedParts[:0] @@ -118,10 +118,10 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, time.Duration, error) { } d.fragmentedSize += int(dataLens[0]) - if d.fragmentedSize > aac.MaxAccessUnitSize { + if d.fragmentedSize > mpeg4audio.MaxAccessUnitSize { d.fragmentedParts = d.fragmentedParts[:0] d.fragmentedMode = false - return nil, 0, fmt.Errorf("AU size (%d) is too big (maximum is %d)", d.fragmentedSize, aac.MaxAccessUnitSize) + return nil, 0, fmt.Errorf("AU size (%d) is too big (maximum is %d)", d.fragmentedSize, mpeg4audio.MaxAccessUnitSize) } d.fragmentedParts = append(d.fragmentedParts, payload[:dataLens[0]]) @@ -214,7 +214,7 @@ func (d *Decoder) finalize(aus [][]byte) ([][]byte, error) { if len(aus) == 1 && len(aus[0]) >= 2 { if aus[0][0] == 0xFF && (aus[0][1]&0xF0) == 0xF0 { - var pkts aac.ADTSPackets + var pkts mpeg4audio.ADTSPackets err := pkts.Unmarshal(aus[0]) if err == nil && len(pkts) == 1 { d.adtsMode = true @@ -227,7 +227,7 @@ func (d *Decoder) finalize(aus [][]byte) ([][]byte, error) { return nil, fmt.Errorf("multiple AUs in ADTS mode are not supported") } - var pkts aac.ADTSPackets + var pkts mpeg4audio.ADTSPackets err := pkts.Unmarshal(aus[0]) if err != nil { return nil, fmt.Errorf("unable to decode ADTS: %s", err) diff --git a/pkg/rtpaac/encoder.go b/pkg/rtpaac/encoder.go index b776569e..dcfe6fd4 100644 --- a/pkg/rtpaac/encoder.go +++ b/pkg/rtpaac/encoder.go @@ -6,8 +6,8 @@ import ( "github.com/pion/rtp" - "github.com/aler9/gortsplib/pkg/aac" "github.com/aler9/gortsplib/pkg/bits" + "github.com/aler9/gortsplib/pkg/mpeg4audio" ) func randUint32() uint32 { @@ -97,7 +97,7 @@ func (e *Encoder) Encode(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, e return nil, err } rets = append(rets, pkts...) - pts += time.Duration(len(batch)) * aac.SamplesPerAccessUnit * time.Second / time.Duration(e.SampleRate) + pts += time.Duration(len(batch)) * mpeg4audio.SamplesPerAccessUnit * time.Second / time.Duration(e.SampleRate) } // initialize new batch diff --git a/pkg/rtpaac/rtpaac_test.go b/pkg/rtpaac/rtpaac_test.go index 4b8f6189..2459b500 100644 --- a/pkg/rtpaac/rtpaac_test.go +++ b/pkg/rtpaac/rtpaac_test.go @@ -8,7 +8,7 @@ import ( "github.com/pion/rtp" "github.com/stretchr/testify/require" - "github.com/aler9/gortsplib/pkg/aac" + "github.com/aler9/gortsplib/pkg/mpeg4audio" ) func mergeBytes(vals ...[]byte) []byte { @@ -539,7 +539,7 @@ func TestDecode(t *testing.T) { require.NoError(t, err) require.Equal(t, expPTS, pts) aus = append(aus, addAUs...) - expPTS += time.Duration(len(aus)) * aac.SamplesPerAccessUnit * time.Second / 48000 + expPTS += time.Duration(len(aus)) * mpeg4audio.SamplesPerAccessUnit * time.Second / 48000 // test input integrity require.Equal(t, clone, pkt) diff --git a/track_aac.go b/track_aac.go index e328c3b4..651cf2f7 100644 --- a/track_aac.go +++ b/track_aac.go @@ -8,13 +8,13 @@ import ( psdp "github.com/pion/sdp/v3" - "github.com/aler9/gortsplib/pkg/aac" + "github.com/aler9/gortsplib/pkg/mpeg4audio" ) // TrackAAC is an AAC track. type TrackAAC struct { PayloadType uint8 - Config *aac.MPEG4AudioConfig + Config *mpeg4audio.Config SizeLength int IndexLength int IndexDeltaLength int @@ -63,7 +63,7 @@ func newTrackAACFromMediaDescription( return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1]) } - t.Config = &aac.MPEG4AudioConfig{} + t.Config = &mpeg4audio.Config{} err = t.Config.Unmarshal(enc) if err != nil { return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1]) diff --git a/track_aac_test.go b/track_aac_test.go index bc88ae18..0d17f9b4 100644 --- a/track_aac_test.go +++ b/track_aac_test.go @@ -6,13 +6,13 @@ import ( psdp "github.com/pion/sdp/v3" "github.com/stretchr/testify/require" - "github.com/aler9/gortsplib/pkg/aac" + "github.com/aler9/gortsplib/pkg/mpeg4audio" ) func TestTrackAACClone(t *testing.T) { track := &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, @@ -30,7 +30,7 @@ func TestTrackAACClone(t *testing.T) { func TestTrackAACMediaDescription(t *testing.T) { track := &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, diff --git a/track_test.go b/track_test.go index 7a9f342d..6c40e97a 100644 --- a/track_test.go +++ b/track_test.go @@ -6,7 +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/mpeg4audio" "github.com/aler9/gortsplib/pkg/url" ) @@ -74,7 +74,7 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, @@ -105,7 +105,7 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, @@ -136,7 +136,7 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2, @@ -171,7 +171,7 @@ func TestTrackNewFromMediaDescription(t *testing.T) { }, &TrackAAC{ PayloadType: 96, - Config: &aac.MPEG4AudioConfig{ + Config: &mpeg4audio.Config{ Type: 2, SampleRate: 48000, ChannelCount: 2,