diff --git a/connclient_test.go b/connclient_test.go index 0a489745..c4efc3fb 100644 --- a/connclient_test.go +++ b/connclient_test.go @@ -238,7 +238,8 @@ func TestConnClientPublishUDP(t *testing.T) { _, err = conn.Options(u) require.NoError(t, err) - track := NewTrackH264(0, sps, pps) + track, err := NewTrackH264(0, sps, pps) + require.NoError(t, err) _, err = conn.Announce(u, Tracks{track}) require.NoError(t, err) @@ -316,7 +317,8 @@ func TestConnClientPublishTCP(t *testing.T) { _, err = conn.Options(u) require.NoError(t, err) - track := NewTrackH264(0, sps, pps) + track, err := NewTrackH264(0, sps, pps) + require.NoError(t, err) _, err = conn.Announce(u, Tracks{track}) require.NoError(t, err) diff --git a/examples/client-publish-tcp.go b/examples/client-publish-tcp.go index 419b0f3b..242691b9 100644 --- a/examples/client-publish-tcp.go +++ b/examples/client-publish-tcp.go @@ -94,7 +94,10 @@ func main() { } // create a H264 track - track := gortsplib.NewTrackH264(0, sps, pps) + track, err := gortsplib.NewTrackH264(0, sps, pps) + if err != nil { + panic(err) + } // announce the track _, err = conn.Announce(u, gortsplib.Tracks{track}) diff --git a/examples/client-publish-udp.go b/examples/client-publish-udp.go index 802fb496..8da65fdb 100644 --- a/examples/client-publish-udp.go +++ b/examples/client-publish-udp.go @@ -94,7 +94,10 @@ func main() { } // create a H264 track - track := gortsplib.NewTrackH264(0, sps, pps) + track, err := gortsplib.NewTrackH264(0, sps, pps) + if err != nil { + panic(err) + } // announce the track _, err = conn.Announce(u, gortsplib.Tracks{track}) diff --git a/go.mod b/go.mod index 5e15d084..60376477 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625 + github.com/notedit/rtmp v0.0.2 github.com/pion/rtcp v1.2.3 github.com/pion/rtp v1.6.0 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index fce70483..313073a8 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625 h1:A3upkpYzceQT github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625/go.mod h1:5bO/aUQr9m3OasDatNNcVqKAgs7r5hgGXmszWHaC6mI= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/notedit/rtmp v0.0.2 h1:5+to4yezKATiJgnrcETu9LbV5G/QsWkOV9Ts2M/p33w= +github.com/notedit/rtmp v0.0.2/go.mod h1:vzuE21rowz+lT1NGsWbreIvYulgBpCGnQyeTyFblUHc= github.com/pion/randutil v0.0.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA= github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= diff --git a/track.go b/track.go index 0631b2b3..64309790 100644 --- a/track.go +++ b/track.go @@ -3,10 +3,12 @@ package gortsplib import ( "encoding/base64" "encoding/hex" + "fmt" "strconv" "strings" "github.com/aler9/sdp-dirty/v3" + "github.com/notedit/rtmp/codec/aac" ) // Track is a track available in a certain URL. @@ -19,7 +21,7 @@ type Track struct { } // NewTrackH264 initializes an H264 track. -func NewTrackH264(id int, sps []byte, pps []byte) *Track { +func NewTrackH264(id int, sps []byte, pps []byte) (*Track, error) { spropParameterSets := base64.StdEncoding.EncodeToString(sps) + "," + base64.StdEncoding.EncodeToString(pps) profileLevelId := strings.ToUpper(hex.EncodeToString(sps[1:4])) @@ -45,11 +47,31 @@ func NewTrackH264(id int, sps []byte, pps []byte) *Track { }, }, }, - } + }, nil } // NewTrackAac initializes an AAC track. -func NewTrackAac(id int, sampleRate int, channelCount int, config []byte) *Track { +func NewTrackAac(id int, config []byte) (*Track, error) { + codec, err := aac.FromMPEG4AudioConfigBytes(config) + if err != nil { + return nil, err + } + + channelCount, err := func() (int, error) { + switch codec.Config.ChannelLayout { + case aac.CH_MONO: + return 1, nil + + case aac.CH_STEREO: + return 2, nil + } + + return 0, fmt.Errorf("unsupported channel count: %v", codec.Config.ChannelLayout) + }() + if err != nil { + return nil, err + } + return &Track{ Id: id, Media: &sdp.MediaDescription{ @@ -61,7 +83,7 @@ func NewTrackAac(id int, sampleRate int, channelCount int, config []byte) *Track Attributes: []sdp.Attribute{ { Key: "rtpmap", - Value: "97 MPEG4-GENERIC/" + strconv.FormatInt(int64(sampleRate), 10) + + Value: "97 MPEG4-GENERIC/" + strconv.FormatInt(int64(codec.Config.SampleRate), 10) + "/" + strconv.FormatInt(int64(channelCount), 10), }, { @@ -75,7 +97,7 @@ func NewTrackAac(id int, sampleRate int, channelCount int, config []byte) *Track }, }, }, - } + }, nil } // Tracks is a list of tracks.