From 37bd9a1b98ce5847e209958e8e25b545738a9664 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 17 May 2020 16:47:05 +0200 Subject: [PATCH] add sdp functions --- go.mod | 5 ++- go.sum | 4 +++ interleaved-frame.go | 2 +- sdp.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 sdp.go diff --git a/go.mod b/go.mod index f69a9976..2c496dcc 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/aler9/gortsplib go 1.13 -require github.com/stretchr/testify v1.4.0 +require ( + github.com/stretchr/testify v1.4.0 + gortc.io/sdp v0.18.2 +) diff --git a/go.sum b/go.sum index 8fdee585..e62fffec 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -9,3 +11,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gortc.io/sdp v0.18.2 h1:w2L1h9rgMZwGQz/bYMxTQ36POQ114ETpYUX1pFfSsLg= +gortc.io/sdp v0.18.2/go.mod h1:Oj8tpRIx+Zx6lyrQR9+HHegByfbaz92A9wPSWrQhTI4= diff --git a/interleaved-frame.go b/interleaved-frame.go index 63cd84a9..1da0bf97 100644 --- a/interleaved-frame.go +++ b/interleaved-frame.go @@ -8,7 +8,7 @@ import ( ) const ( - _INTERLEAVED_FRAME_MAX_SIZE = 512 * 1024 + _INTERLEAVED_FRAME_MAX_SIZE = 512 * 1024 _INTERLEAVED_FRAME_MAX_CONTENT_SIZE = (_INTERLEAVED_FRAME_MAX_SIZE - 4) ) diff --git a/sdp.go b/sdp.go new file mode 100644 index 00000000..43ade655 --- /dev/null +++ b/sdp.go @@ -0,0 +1,77 @@ +package gortsplib + +import ( + "fmt" + "strconv" + + "gortc.io/sdp" +) + +// SDPParse parses a SDP document. +func SDPParse(in []byte) (*sdp.Message, error) { + s, err := sdp.DecodeSession(in, nil) + if err != nil { + return nil, err + } + + m := &sdp.Message{} + d := sdp.NewDecoder(s) + err = d.Decode(m) + if err != nil { + // allow empty Origins + if err.Error() != "failed to decode message: DecodeError in section s: origin address not set" { + return nil, err + } + } + + if len(m.Medias) == 0 { + return nil, fmt.Errorf("no tracks defined") + } + + return m, nil +} + +// SDPFilter removes everything from a SDP document, except the bare minimum. +func SDPFilter(msgIn *sdp.Message, byteIn []byte) (*sdp.Message, []byte) { + msgOut := &sdp.Message{} + + msgOut.Name = "Stream" + msgOut.Origin = sdp.Origin{ + Username: "-", + NetworkType: "IN", + AddressType: "IP4", + Address: "127.0.0.1", + } + + for i, m := range msgIn.Medias { + var attributes []sdp.Attribute + for _, attr := range m.Attributes { + if attr.Key == "rtpmap" || attr.Key == "fmtp" { + attributes = append(attributes, attr) + } + } + + // control attribute is mandatory, and is the path that is appended + // to the stream path in SETUP + attributes = append(attributes, sdp.Attribute{ + Key: "control", + Value: "trackID=" + strconv.FormatInt(int64(i), 10), + }) + + msgOut.Medias = append(msgOut.Medias, sdp.Media{ + Bandwidths: m.Bandwidths, + Description: sdp.MediaDescription{ + Type: m.Description.Type, + Protocol: "RTP/AVP", // override protocol + Formats: m.Description.Formats, + }, + Attributes: attributes, + }) + } + + sdps := sdp.Session{} + sdps = msgOut.Append(sdps) + byteOut := sdps.AppendTo(nil) + + return msgOut, byteOut +}