change ConnClient.Describe() to return tracks

This commit is contained in:
aler9
2020-07-14 19:35:43 +02:00
parent 67368c8ec5
commit 32d4cc1593
3 changed files with 35 additions and 18 deletions

View File

@@ -24,6 +24,15 @@ const (
clientWriteBufferSize = 4096 clientWriteBufferSize = 4096
) )
// Track is a track available in a certain URL.
type Track struct {
// track id
Id int
// track codec and info in SDP
Media *sdp.MediaDescription
}
// ConnClientConf allows to configure a ConnClient. // ConnClientConf allows to configure a ConnClient.
type ConnClientConf struct { type ConnClientConf struct {
// pre-existing TCP connection that will be wrapped // pre-existing TCP connection that will be wrapped
@@ -197,9 +206,9 @@ func (c *ConnClient) Options(u *url.URL) (*Response, error) {
} }
// Describe writes a DESCRIBE request, that means that we want to obtain the SDP // Describe writes a DESCRIBE request, that means that we want to obtain the SDP
// document that describes the stream available in the given url. It then // document that describes the tracks available in the given URL. It then
// reads a Response. // reads a Response.
func (c *ConnClient) Describe(u *url.URL) (*sdp.SessionDescription, *Response, error) { func (c *ConnClient) Describe(u *url.URL) ([]*Track, *Response, error) {
res, err := c.Do(&Request{ res, err := c.Do(&Request{
Method: DESCRIBE, Method: DESCRIBE,
Url: u, Url: u,
@@ -227,7 +236,15 @@ func (c *ConnClient) Describe(u *url.URL) (*sdp.SessionDescription, *Response, e
return nil, nil, err return nil, nil, err
} }
return sdpd, res, nil tracks := make([]*Track, len(sdpd.MediaDescriptions))
for i, media := range sdpd.MediaDescriptions {
tracks[i] = &Track{
Id: i,
Media: media,
}
}
return tracks, res, nil
} }
func (c *ConnClient) setup(u *url.URL, media *sdp.MediaDescription, transport []string) (*Response, error) { func (c *ConnClient) setup(u *url.URL, media *sdp.MediaDescription, transport []string) (*Response, error) {
@@ -242,7 +259,7 @@ func (c *ConnClient) setup(u *url.URL, media *sdp.MediaDescription, transport []
return "" return ""
}() }()
// no control attribute, use original url // no control attribute, use original URL
if control == "" { if control == "" {
return u return u
} }
@@ -300,10 +317,10 @@ func (c *ConnClient) setup(u *url.URL, media *sdp.MediaDescription, transport []
// SetupUdp writes a SETUP request, that means that we want to read // SetupUdp writes a SETUP request, that means that we want to read
// a track with given media, id and the UDP transport. It then reads a Response. // a track with given media, id and the UDP transport. It then reads a Response.
func (c *ConnClient) SetupUdp(u *url.URL, media *sdp.MediaDescription, func (c *ConnClient) SetupUdp(u *url.URL, track *Track, rtpPort int,
rtpPort int, rtcpPort int) (int, int, *Response, error) { rtcpPort int) (int, int, *Response, error) {
res, err := c.setup(u, media, []string{ res, err := c.setup(u, track.Media, []string{
"RTP/AVP/UDP", "RTP/AVP/UDP",
"unicast", "unicast",
fmt.Sprintf("client_port=%d-%d", rtpPort, rtcpPort), fmt.Sprintf("client_port=%d-%d", rtpPort, rtcpPort),
@@ -328,10 +345,10 @@ func (c *ConnClient) SetupUdp(u *url.URL, media *sdp.MediaDescription,
// SetupTcp writes a SETUP request, that means that we want to read // SetupTcp writes a SETUP request, that means that we want to read
// a track with given media, given id and the TCP transport. It then reads a Response. // a track with given media, given id and the TCP transport. It then reads a Response.
func (c *ConnClient) SetupTcp(u *url.URL, media *sdp.MediaDescription, trackId int) (*Response, error) { func (c *ConnClient) SetupTcp(u *url.URL, track *Track) (*Response, error) {
interleaved := fmt.Sprintf("interleaved=%d-%d", (trackId * 2), (trackId*2)+1) interleaved := fmt.Sprintf("interleaved=%d-%d", (track.Id * 2), (track.Id*2)+1)
res, err := c.setup(u, media, []string{ res, err := c.setup(u, track.Media, []string{
"RTP/AVP/TCP", "RTP/AVP/TCP",
"unicast", "unicast",
interleaved, interleaved,

View File

@@ -30,13 +30,13 @@ func main() {
panic(err) panic(err)
} }
sdpd, _, err := rconn.Describe(u) tracks, _, err := rconn.Describe(u)
if err != nil { if err != nil {
panic(err) panic(err)
} }
for i, media := range sdpd.MediaDescriptions { for _, track := range tracks {
_, err := rconn.SetupTcp(u, media, i) _, err := rconn.SetupTcp(u, track)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -31,7 +31,7 @@ func main() {
panic(err) panic(err)
} }
sdpd, _, err := rconn.Describe(u) tracks, _, err := rconn.Describe(u)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -39,8 +39,8 @@ func main() {
var rtpListeners []net.PacketConn var rtpListeners []net.PacketConn
var rtcpListeners []net.PacketConn var rtcpListeners []net.PacketConn
for i, media := range sdpd.MediaDescriptions { for _, track := range tracks {
rtpPort := 9000 + i*2 rtpPort := 9000 + track.Id*2
rtpl, err := net.ListenPacket("udp", ":"+strconv.FormatInt(int64(rtpPort), 10)) rtpl, err := net.ListenPacket("udp", ":"+strconv.FormatInt(int64(rtpPort), 10))
if err != nil { if err != nil {
panic(err) panic(err)
@@ -48,7 +48,7 @@ func main() {
defer rtpl.Close() defer rtpl.Close()
rtpListeners = append(rtpListeners, rtpl) rtpListeners = append(rtpListeners, rtpl)
rtcpPort := 9001 + i*2 rtcpPort := rtpPort + 1
rtcpl, err := net.ListenPacket("udp", ":"+strconv.FormatInt(int64(rtcpPort), 10)) rtcpl, err := net.ListenPacket("udp", ":"+strconv.FormatInt(int64(rtcpPort), 10))
if err != nil { if err != nil {
panic(err) panic(err)
@@ -56,7 +56,7 @@ func main() {
defer rtcpl.Close() defer rtcpl.Close()
rtcpListeners = append(rtcpListeners, rtcpl) rtcpListeners = append(rtcpListeners, rtcpl)
_, _, _, err = rconn.SetupUdp(u, media, rtpPort, rtcpPort) _, _, _, err = rconn.SetupUdp(u, track, rtpPort, rtcpPort)
if err != nil { if err != nil {
panic(err) panic(err)
} }