From 6db4c3b85daceeb0e8420912c19398418f132d6f Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 29 Nov 2020 13:05:33 +0100 Subject: [PATCH] move automatic protocol selection into Setup() --- connclient.go | 33 ++++++++++++++++--- dialer.go | 52 +++++------------------------- examples/client-publish-options.go | 4 --- examples/client-read-options.go | 4 --- 4 files changed, 36 insertions(+), 57 deletions(-) diff --git a/connclient.go b/connclient.go index 6e254428..eff8f270 100644 --- a/connclient.go +++ b/connclient.go @@ -356,7 +356,7 @@ func (c *ConnClient) urlForTrack(baseUrl *base.URL, mode headers.TransportMode, // Setup writes a SETUP request and reads a Response. // rtpPort and rtcpPort are used only if protocol is UDP. // if rtpPort and rtcpPort are zero, they are chosen automatically. -func (c *ConnClient) Setup(u *base.URL, mode headers.TransportMode, proto base.StreamProtocol, +func (c *ConnClient) Setup(u *base.URL, mode headers.TransportMode, track *Track, rtpPort int, rtcpPort int) (*base.Response, error) { err := c.checkState(map[connClientState]struct{}{ connClientStateInitial: {}, @@ -380,13 +380,24 @@ func (c *ConnClient) Setup(u *base.URL, mode headers.TransportMode, proto base.S return nil, fmt.Errorf("setup has already begun with another url") } - if c.streamProtocol != nil && *c.streamProtocol != proto { - return nil, fmt.Errorf("cannot setup tracks with different protocols") - } - var rtpListener *connClientUDPListener var rtcpListener *connClientUDPListener + proto := func() StreamProtocol { + // protocol set by previous Setup() + if c.streamProtocol != nil { + return *c.streamProtocol + } + + // protocol set by dialer + if c.d.StreamProtocol != nil { + return *c.d.StreamProtocol + } + + // try udp + return StreamProtocolUDP + }() + transport := &headers.Transport{ Protocol: proto, Delivery: func() *base.StreamDelivery { @@ -474,6 +485,18 @@ func (c *ConnClient) Setup(u *base.URL, mode headers.TransportMode, proto base.S rtpListener.close() rtcpListener.close() } + + // switch protocol automatically + if res.StatusCode == base.StatusUnsupportedTransport && + c.streamProtocol == nil && + c.d.StreamProtocol == nil { + + v := StreamProtocolTCP + c.streamProtocol = &v + + return c.Setup(u, headers.TransportModePlay, track, 0, 0) + } + return res, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage) } diff --git a/dialer.go b/dialer.go index 2e9f3fcf..8991c3a6 100644 --- a/dialer.go +++ b/dialer.go @@ -139,29 +139,11 @@ func (d Dialer) DialRead(address string) (*ConnClient, error) { return nil, err } - proto := func() StreamProtocol { - if d.StreamProtocol != nil { - return *d.StreamProtocol - } - return StreamProtocolUDP - }() - - for i, track := range tracks { - res, err := conn.Setup(u, headers.TransportModePlay, proto, track, 0, 0) + for _, track := range tracks { + _, err := conn.Setup(u, headers.TransportModePlay, track, 0, 0) if err != nil { - // switch protocol automatically - if i == 0 && d.StreamProtocol == nil && res != nil && - res.StatusCode == base.StatusUnsupportedTransport { - proto = StreamProtocolTCP - _, err := conn.Setup(u, headers.TransportModePlay, proto, track, 0, 0) - if err != nil { - conn.Close() - return nil, err - } - } else { - conn.Close() - return nil, err - } + conn.Close() + return nil, err } } @@ -202,29 +184,11 @@ func (d Dialer) DialPublish(address string, tracks Tracks) (*ConnClient, error) return nil, err } - proto := func() StreamProtocol { - if d.StreamProtocol != nil { - return *d.StreamProtocol - } - return StreamProtocolUDP - }() - - for i, track := range tracks { - res, err := conn.Setup(u, headers.TransportModeRecord, proto, track, 0, 0) + for _, track := range tracks { + _, err := conn.Setup(u, headers.TransportModeRecord, track, 0, 0) if err != nil { - // switch protocol automatically - if i == 0 && d.StreamProtocol == nil && res != nil && - res.StatusCode == base.StatusUnsupportedTransport { - proto = StreamProtocolTCP - _, err := conn.Setup(u, headers.TransportModePlay, proto, track, 0, 0) - if err != nil { - conn.Close() - return nil, err - } - } else { - conn.Close() - return nil, err - } + conn.Close() + return nil, err } } diff --git a/examples/client-publish-options.go b/examples/client-publish-options.go index 6a975337..36ad772a 100644 --- a/examples/client-publish-options.go +++ b/examples/client-publish-options.go @@ -51,10 +51,6 @@ func main() { ReadTimeout: 10 * time.Second, // timeout of write operations WriteTimeout: 10 * time.Second, - // read buffer count. - // If greater than 1, allows to pass buffers to routines different than the one - // that is reading frames - ReadBufferCount: 1, } // connect to the server and start publishing the track diff --git a/examples/client-read-options.go b/examples/client-read-options.go index d72c4e7e..15bf6618 100644 --- a/examples/client-read-options.go +++ b/examples/client-read-options.go @@ -23,10 +23,6 @@ func main() { ReadTimeout: 10 * time.Second, // timeout of write operations WriteTimeout: 10 * time.Second, - // read buffer count. - // If greater than 1, allows to pass buffers to routines different than the one - // that is reading frames - ReadBufferCount: 1, } // connect to the server and start reading all tracks