Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
use control attribute in SETUP when available
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Apr 10, 2020
1 parent 734454a commit fa01182
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
8 changes: 4 additions & 4 deletions server_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return nil, fmt.Errorf("stream '%s' is not ready yet", path)
}

return str.sdpText, nil
return str.serverSdpText, nil
}()
if err != nil {
c.writeResError(req, err)
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return fmt.Errorf("client want to send tracks with different protocols")
}

if len(c.streamTracks) >= len(str.sdpParsed.Medias) {
if len(c.streamTracks) >= len(str.serverSdpParsed.Medias) {
return fmt.Errorf("all the tracks have already been setup")
}

Expand Down Expand Up @@ -357,7 +357,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return fmt.Errorf("client want to send tracks with different protocols")
}

if len(c.streamTracks) >= len(str.sdpParsed.Medias) {
if len(c.streamTracks) >= len(str.serverSdpParsed.Medias) {
return fmt.Errorf("all the tracks have already been setup")
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return fmt.Errorf("no one is streaming on path '%s'", c.path)
}

if len(c.streamTracks) != len(str.sdpParsed.Medias) {
if len(c.streamTracks) != len(str.serverSdpParsed.Medias) {
return fmt.Errorf("not all tracks have been setup")
}

Expand Down
72 changes: 53 additions & 19 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ func sdpFilter(msgIn *sdp.Message, byteIn []byte) (*sdp.Message, []byte) {
attributes = append(attributes, attr)
}
}
// control attribute is needed by gstreamer

// 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: "streamid=" + strconv.FormatInt(int64(i), 10),
Value: "trackID=" + strconv.FormatInt(int64(i), 10),
})

msgOut.Medias = append(msgOut.Medias, sdp.Media{
Expand Down Expand Up @@ -109,14 +111,15 @@ const (
)

type stream struct {
p *program
state streamState
path string
conf streamConf
ur *url.URL
proto streamProtocol
sdpText []byte
sdpParsed *sdp.Message
p *program
state streamState
path string
conf streamConf
ur *url.URL
proto streamProtocol
clientSdpParsed *sdp.Message
serverSdpText []byte
serverSdpParsed *sdp.Message
}

func newStream(p *program, path string, conf streamConf) (*stream, error) {
Expand Down Expand Up @@ -261,20 +264,22 @@ func (s *stream) run() {
return
}

sdpParsed, err := sdpParse(res.Content)
clientSdpParsed, err := sdpParse(res.Content)
if err != nil {
s.log("ERR: invalid SDP: %s", err)
return
}

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

func() {
s.p.mutex.Lock()
defer s.p.mutex.Unlock()

s.sdpText = res.Content
s.sdpParsed = sdpParsed
s.clientSdpParsed = clientSdpParsed
s.serverSdpText = serverSdpText
s.serverSdpParsed = serverSdpParsed
}()

if s.proto == _STREAM_PROTOCOL_UDP {
Expand Down Expand Up @@ -302,7 +307,7 @@ func (s *stream) runUdp(conn *gortsplib.ConnClient) {
}
}()

for i := 0; i < len(s.sdpParsed.Medias); i++ {
for i, media := range s.clientSdpParsed.Medias {
var rtpPort int
var rtcpPort int
var rtpl *streamUdpListener
Expand Down Expand Up @@ -337,10 +342,25 @@ func (s *stream) runUdp(conn *gortsplib.ConnClient) {
Method: "SETUP",
Url: "rtsp://" + s.ur.Host + func() string {
ret := s.ur.Path

if len(ret) == 0 || ret[len(ret)-1] != '/' {
ret += "/"
}
ret += "trackID=" + strconv.FormatInt(int64(i+1), 10)

control := func() string {
for _, attr := range media.Attributes {
if attr.Key == "control" {
return attr.Value
}
}
return ""
}()
if control != "" {
ret += control
} else {
ret += "trackID=" + strconv.FormatInt(int64(i+1), 10)
}

return ret
}() + func() string {
if s.ur.RawQuery != "" {
Expand Down Expand Up @@ -494,18 +514,32 @@ func (s *stream) runUdp(conn *gortsplib.ConnClient) {
}

func (s *stream) runTcp(conn *gortsplib.ConnClient) {

for i := 0; i < len(s.sdpParsed.Medias); i++ {
for i, media := range s.clientSdpParsed.Medias {
interleaved := fmt.Sprintf("interleaved=%d-%d", (i * 2), (i*2)+1)

res, err := writeReqReadRes(conn, &gortsplib.Request{
Method: "SETUP",
Url: "rtsp://" + s.ur.Host + func() string {
ret := s.ur.Path

if len(ret) == 0 || ret[len(ret)-1] != '/' {
ret += "/"
}
ret += "trackID=" + strconv.FormatInt(int64(i+1), 10)

control := func() string {
for _, attr := range media.Attributes {
if attr.Key == "control" {
return attr.Value
}
}
return ""
}()
if control != "" {
ret += control
} else {
ret += "trackID=" + strconv.FormatInt(int64(i+1), 10)
}

return ret
}() + func() string {
if s.ur.RawQuery != "" {
Expand Down

0 comments on commit fa01182

Please sign in to comment.