Skip to content

Commit

Permalink
change sdp library
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 12, 2020
1 parent f01bb96 commit 7dc904a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 43 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ 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-20200710091324-fb7d7b008e68
github.com/aler9/gortsplib v0.0.0-20200712140456-c87fdcdbff66
github.com/pion/rtcp v1.2.3
github.com/pion/sdp v1.3.0
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.5.1
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v2 v2.2.2
gortc.io/sdp v0.18.2
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ 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-20200710091324-fb7d7b008e68 h1:apyYugiG/luHl0Xyc2xJtGkL2HbF1umxjCNP2sX9iyw=
github.com/aler9/gortsplib v0.0.0-20200710091324-fb7d7b008e68/go.mod h1:sL64nUkmrTVhlT/GCaxRXyI2Xk7m8XSdw5Uv8xKGPdc=
github.com/aler9/gortsplib v0.0.0-20200712140456-c87fdcdbff66 h1:YK6Fjwn5aEnvGjRPodjKmpkQ8VZbA58F5OvAecw0eJM=
github.com/aler9/gortsplib v0.0.0-20200712140456-c87fdcdbff66/go.mod h1:YiIgmmv0ELkWUy11Jj2h5AgfqLCpy8sIX/l9MmS8+uw=
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/pion/rtcp v1.2.3 h1:2wrhKnqgSz91Q5nzYTO07mQXztYPtxL8a0XOss4rJqA=
github.com/pion/rtcp v1.2.3/go.mod h1:zGhIv0RPRF0Z1Wiij22pUt5W/c9fevqSzT4jje/oK7I=
github.com/pion/sdp v1.3.0 h1:21lpgEILHyolpsIrbCBagZaAPj4o057cFjzaFebkVOs=
github.com/pion/sdp v1.3.0/go.mod h1:ceA2lTyftydQTuCIbUNoH77aAt6CiQJaRpssA4Gee8I=
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=
Expand All @@ -23,5 +25,3 @@ 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=
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"os"

"github.com/aler9/gortsplib"
"github.com/pion/sdp"
"gopkg.in/alecthomas/kingpin.v2"
"gortc.io/sdp"
)

var Version = "v0.0.0"
Expand Down Expand Up @@ -155,7 +155,7 @@ func (programEventTerminate) isProgramEvent() {}
type publisher interface {
publisherIsReady() bool
publisherSdpText() []byte
publisherSdpParsed() *sdp.Message
publisherSdpParsed() *sdp.SessionDescription
}

type program struct {
Expand Down Expand Up @@ -332,7 +332,7 @@ outer:

sdpParsed := pub.publisherSdpParsed()

if len(evt.client.streamTracks) >= len(sdpParsed.Medias) {
if len(evt.client.streamTracks) >= len(sdpParsed.MediaDescriptions) {
evt.res <- fmt.Errorf("all the tracks have already been setup")
continue
}
Expand Down Expand Up @@ -364,7 +364,7 @@ outer:

sdpParsed := pub.publisherSdpParsed()

if len(evt.client.streamTracks) != len(sdpParsed.Medias) {
if len(evt.client.streamTracks) != len(sdpParsed.MediaDescriptions) {
evt.res <- fmt.Errorf("not all tracks have been setup")
continue
}
Expand Down
28 changes: 17 additions & 11 deletions server-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/aler9/gortsplib"
"gortc.io/sdp"
"github.com/pion/sdp"
)

const (
Expand Down Expand Up @@ -71,8 +71,8 @@ type serverClient struct {
authPass string
authHelper *gortsplib.AuthServer
authFailures int
streamSdpText []byte // only if publisher
streamSdpParsed *sdp.Message // only if publisher
streamSdpText []byte // only if publisher
streamSdpParsed *sdp.SessionDescription // only if publisher
streamProtocol streamProtocol
streamTracks []*track
rtcpReceivers []*rtcpReceiver
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *serverClient) publisherSdpText() []byte {
return c.streamSdpText
}

func (c *serverClient) publisherSdpParsed() *sdp.Message {
func (c *serverClient) publisherSdpParsed() *sdp.SessionDescription {
return c.streamSdpParsed
}

Expand Down Expand Up @@ -631,6 +631,11 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) error {
return errClientTerminate
}

if len(path) == 0 {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path can't be empty"))
return errClientTerminate
}

pconf := c.findConfForPath(path)
if pconf == nil {
c.writeResError(req, gortsplib.StatusBadRequest,
Expand All @@ -657,15 +662,16 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) error {
return errClientTerminate
}

sdpParsed, err := gortsplib.SDPParse(req.Content)
sdpParsed := &sdp.SessionDescription{}
err = sdpParsed.Unmarshal(string(req.Content))
if err != nil {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("invalid SDP: %s", err))
return errClientTerminate
}
sdpParsed, req.Content = gortsplib.SDPFilter(sdpParsed, req.Content)
sdpParsed, req.Content = sdpForServer(sdpParsed, req.Content)

if len(path) == 0 {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path can't be empty"))
if len(sdpParsed.MediaDescriptions) == 0 {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("no tracks defined"))
return errClientTerminate
}

Expand Down Expand Up @@ -862,7 +868,7 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) error {
return errClientTerminate
}

if len(c.streamTracks) >= len(c.streamSdpParsed.Medias) {
if len(c.streamTracks) >= len(c.streamSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("all the tracks have already been setup"))
return errClientTerminate
}
Expand Down Expand Up @@ -914,7 +920,7 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) error {
return errClientTerminate
}

if len(c.streamTracks) >= len(c.streamSdpParsed.Medias) {
if len(c.streamTracks) >= len(c.streamSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("all the tracks have already been setup"))
return errClientTerminate
}
Expand Down Expand Up @@ -1014,7 +1020,7 @@ func (c *serverClient) handleRequest(req *gortsplib.Request) error {
return errClientTerminate
}

if len(c.streamTracks) != len(c.streamSdpParsed.Medias) {
if len(c.streamTracks) != len(c.streamSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("not all tracks have been setup"))
return errClientTerminate
}
Expand Down
45 changes: 23 additions & 22 deletions streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/aler9/gortsplib"
"gortc.io/sdp"
"github.com/pion/sdp"
)

const (
Expand All @@ -31,9 +31,9 @@ type streamer struct {
ur *url.URL
proto streamProtocol
ready bool
clientSdpParsed *sdp.Message
clientSdpParsed *sdp.SessionDescription
serverSdpText []byte
serverSdpParsed *sdp.Message
serverSdpParsed *sdp.SessionDescription
rtcpReceivers []*rtcpReceiver
readBuf *doubleBuffer

Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *streamer) publisherSdpText() []byte {
return s.serverSdpText
}

func (s *streamer) publisherSdpParsed() *sdp.Message {
func (s *streamer) publisherSdpParsed() *sdp.SessionDescription {
return s.serverSdpParsed
}

Expand Down Expand Up @@ -217,14 +217,15 @@ func (s *streamer) do() bool {
return true
}

clientSdpParsed, err := gortsplib.SDPParse(res.Content)
clientSdpParsed := &sdp.SessionDescription{}
err = clientSdpParsed.Unmarshal(string(res.Content))
if err != nil {
s.log("ERR: invalid SDP: %s", err)
return true
}

// create a filtered SDP that is used by the server (not by the client)
serverSdpParsed, serverSdpText := gortsplib.SDPFilter(clientSdpParsed, res.Content)
serverSdpParsed, serverSdpText := sdpForServer(clientSdpParsed, res.Content)

s.clientSdpParsed = clientSdpParsed
s.serverSdpText = serverSdpText
Expand All @@ -249,7 +250,7 @@ func (s *streamer) runUdp(conn *gortsplib.ConnClient) bool {
}
}()

for i, media := range s.clientSdpParsed.Medias {
for i, media := range s.clientSdpParsed.MediaDescriptions {
var rtpPort int
var rtcpPort int
var rtpl *streamerUdpListener
Expand Down Expand Up @@ -282,7 +283,7 @@ func (s *streamer) runUdp(conn *gortsplib.ConnClient) bool {
res, err := conn.WriteRequest(&gortsplib.Request{
Method: gortsplib.SETUP,
Url: func() *url.URL {
control := media.Attributes.Value("control")
control := sdpFindAttribute(media.Attributes, "control")

// no control attribute
if control == "" {
Expand All @@ -309,7 +310,7 @@ func (s *streamer) runUdp(conn *gortsplib.ConnClient) bool {
ret += "/"
}

control := media.Attributes.Value("control")
control := sdpFindAttribute(media.Attributes, "control")
if control != "" {
ret += control
} else {
Expand Down Expand Up @@ -388,8 +389,8 @@ func (s *streamer) runUdp(conn *gortsplib.ConnClient) bool {
return true
}

s.rtcpReceivers = make([]*rtcpReceiver, len(s.clientSdpParsed.Medias))
for trackId := range s.clientSdpParsed.Medias {
s.rtcpReceivers = make([]*rtcpReceiver, len(s.clientSdpParsed.MediaDescriptions))
for trackId := range s.clientSdpParsed.MediaDescriptions {
s.rtcpReceivers[trackId] = newRtcpReceiver()
}

Expand Down Expand Up @@ -429,7 +430,7 @@ outer:
}

case <-checkStreamTicker.C:
for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
if time.Since(s.rtcpReceivers[trackId].lastFrameTime()) >= s.p.conf.StreamDeadAfter {
s.log("ERR: stream is dead")
ret = true
Expand All @@ -438,7 +439,7 @@ outer:
}

case <-receiverReportTicker.C:
for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
frame := s.rtcpReceivers[trackId].report()
streamerUdpListenerPairs[trackId].rtcpl.writeChan <- &udpAddrBufPair{
addr: &net.UDPAddr{
Expand All @@ -463,21 +464,21 @@ outer:
pair.rtcpl.stop()
}

for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
s.rtcpReceivers[trackId].close()
}

return ret
}

func (s *streamer) runTcp(conn *gortsplib.ConnClient) bool {
for i, media := range s.clientSdpParsed.Medias {
for i, media := range s.clientSdpParsed.MediaDescriptions {
interleaved := fmt.Sprintf("interleaved=%d-%d", (i * 2), (i*2)+1)

res, err := conn.WriteRequest(&gortsplib.Request{
Method: gortsplib.SETUP,
Url: func() *url.URL {
control := media.Attributes.Value("control")
control := sdpFindAttribute(media.Attributes, "control")

// no control attribute
if control == "" {
Expand All @@ -504,7 +505,7 @@ func (s *streamer) runTcp(conn *gortsplib.ConnClient) bool {
ret += "/"
}

control := media.Attributes.Value("control")
control := sdpFindAttribute(media.Attributes, "control")
if control != "" {
ret += control
} else {
Expand Down Expand Up @@ -589,8 +590,8 @@ outer1:
}
}

s.rtcpReceivers = make([]*rtcpReceiver, len(s.clientSdpParsed.Medias))
for trackId := range s.clientSdpParsed.Medias {
s.rtcpReceivers = make([]*rtcpReceiver, len(s.clientSdpParsed.MediaDescriptions))
for trackId := range s.clientSdpParsed.MediaDescriptions {
s.rtcpReceivers[trackId] = newRtcpReceiver()
}

Expand Down Expand Up @@ -632,7 +633,7 @@ outer2:
break outer2

case <-checkStreamTicker.C:
for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
if time.Since(s.rtcpReceivers[trackId].lastFrameTime()) >= s.p.conf.StreamDeadAfter {
s.log("ERR: stream is dead")
ret = true
Expand All @@ -641,7 +642,7 @@ outer2:
}

case <-receiverReportTicker.C:
for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
frame := s.rtcpReceivers[trackId].report()

channel := trackFlowTypeToInterleavedChannel(trackId, _TRACK_FLOW_TYPE_RTCP)
Expand All @@ -659,7 +660,7 @@ outer2:

s.p.events <- programEventStreamerNotReady{s}

for trackId := range s.clientSdpParsed.Medias {
for trackId := range s.clientSdpParsed.MediaDescriptions {
s.rtcpReceivers[trackId].close()
}

Expand Down
Loading

0 comments on commit 7dc904a

Please sign in to comment.