From a4d581d02e56d9f5c2908c74419989f44fcb1f36 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 17 May 2020 16:49:21 +0200 Subject: [PATCH] move sdp functions into gortsplib --- go.mod | 4 +-- go.sum | 12 ++++---- server-client.go | 73 ++---------------------------------------------- 3 files changed, 10 insertions(+), 79 deletions(-) diff --git a/go.mod b/go.mod index 504c13d3..297e9c89 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.13 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect - github.com/aler9/gortsplib v0.0.0-20200517142010-db3770c8f3a0 + github.com/aler9/gortsplib v0.0.0-20200517144705-37bd9a1b98ce github.com/stretchr/testify v1.4.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 - gortc.io/sdp v0.17.0 + gortc.io/sdp v0.18.2 ) diff --git a/go.sum b/go.sum index 92a849d6..5788092c 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,12 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -github.com/aler9/gortsplib v0.0.0-20200517142010-db3770c8f3a0 h1:/v4nyRb/wrhEkx+oKL4iDelWzYVJfcX4oa2MG0Yo9pc= -github.com/aler9/gortsplib v0.0.0-20200517142010-db3770c8f3a0/go.mod h1:YiIgmmv0ELkWUy11Jj2h5AgfqLCpy8sIX/l9MmS8+uw= +github.com/aler9/gortsplib v0.0.0-20200517144705-37bd9a1b98ce h1:YBes3GDLTF/UonS+v9s478lfCqg6HGRAaaiR9iMtygc= +github.com/aler9/gortsplib v0.0.0-20200517144705-37bd9a1b98ce/go.mod h1:sL64nUkmrTVhlT/GCaxRXyI2Xk7m8XSdw5Uv8xKGPdc= 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.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +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= @@ -19,5 +19,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.17.0 h1:gPmGXqyszHplnlMeF2X0eCOK+G8aAK4PP6puglIcofc= -gortc.io/sdp v0.17.0/go.mod h1:iwG4LtzGX1MLglrl5x7AL7cfLndFPMfJ02koQl5KIgE= +gortc.io/sdp v0.18.2 h1:w2L1h9rgMZwGQz/bYMxTQ36POQ114ETpYUX1pFfSsLg= +gortc.io/sdp v0.18.2/go.mod h1:Oj8tpRIx+Zx6lyrQR9+HHegByfbaz92A9wPSWrQhTI4= diff --git a/server-client.go b/server-client.go index 3be418c0..a35ca101 100644 --- a/server-client.go +++ b/server-client.go @@ -7,81 +7,12 @@ import ( "log" "net" "os/exec" - "strconv" "strings" "github.com/aler9/gortsplib" "gortc.io/sdp" ) -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 in SDP") - } - - return m, nil -} - -// remove everything from SDP 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 -} - func interleavedChannelToTrack(channel uint8) (int, trackFlow) { if (channel % 2) == 0 { return int(channel / 2), _TRACK_FLOW_RTP @@ -396,13 +327,13 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) bool { return false } - sdpParsed, err := sdpParse(req.Content) + sdpParsed, err := gortsplib.SDPParse(req.Content) if err != nil { c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("invalid SDP: %s", err)) return false } - sdpParsed, req.Content = sdpFilter(sdpParsed, req.Content) + sdpParsed, req.Content = gortsplib.SDPFilter(sdpParsed, req.Content) err = func() error { c.p.tcpl.mutex.Lock()