From 96f3434e616247caaffeff12866f64d674ec3a2b Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 4 Oct 2020 18:30:25 +0200 Subject: [PATCH] rename SetupMode into TransportMode and move into headers --- connclient.go | 34 +++++++------------------ connclientdial.go | 8 +++--- headers/transport.go | 53 ++++++++++++++++++++++++++++++++++----- headers/transport_test.go | 4 +-- utils.go | 24 +++++------------- 5 files changed, 68 insertions(+), 55 deletions(-) diff --git a/connclient.go b/connclient.go index 61123427..ebf4e1e2 100644 --- a/connclient.go +++ b/connclient.go @@ -390,10 +390,10 @@ func (c *ConnClient) Describe(u *url.URL) (Tracks, *base.Response, error) { } // build an URL by merging baseUrl with the control attribute from track.Media -func (c *ConnClient) urlForTrack(baseUrl *url.URL, mode SetupMode, track *Track) *url.URL { +func (c *ConnClient) urlForTrack(baseUrl *url.URL, mode TransportMode, track *Track) *url.URL { control := func() string { // if we're recording, get control from track ID - if mode == SetupModeRecord { + if mode == TransportModeRecord { return "trackID=" + strconv.FormatInt(int64(track.Id), 10) } @@ -450,7 +450,7 @@ func (c *ConnClient) urlForTrack(baseUrl *url.URL, mode SetupMode, track *Track) return u } -func (c *ConnClient) setup(u *url.URL, mode SetupMode, track *Track, ht *headers.Transport) (*base.Response, error) { +func (c *ConnClient) setup(u *url.URL, mode TransportMode, track *Track, ht *headers.Transport) (*base.Response, error) { res, err := c.Do(&base.Request{ Method: base.SETUP, Url: c.urlForTrack(u, mode, track), @@ -471,7 +471,7 @@ func (c *ConnClient) setup(u *url.URL, mode SetupMode, track *Track, ht *headers // SetupUDP writes a SETUP request and reads a Response. // If rtpPort and rtcpPort are zero, they are be chosen automatically. -func (c *ConnClient) SetupUDP(u *url.URL, mode SetupMode, track *Track, rtpPort int, +func (c *ConnClient) SetupUDP(u *url.URL, mode TransportMode, track *Track, rtpPort int, rtcpPort int) (*base.Response, error) { if c.state != connClientStateInitial { return nil, fmt.Errorf("can't be called when reading or publishing") @@ -542,15 +542,7 @@ func (c *ConnClient) SetupUDP(u *url.URL, mode SetupMode, track *Track, rtpPort return &ret }(), ClientPorts: &[2]int{rtpPort, rtcpPort}, - Mode: func() *string { - var v string - if mode == SetupModeRecord { - v = "record" - } else { - v = "play" - } - return &v - }(), + Mode: &mode, }) if err != nil { rtpListener.close() @@ -575,7 +567,7 @@ func (c *ConnClient) SetupUDP(u *url.URL, mode SetupMode, track *Track, rtpPort streamProtocol := StreamProtocolUDP c.streamProtocol = &streamProtocol - if mode == SetupModePlay { + if mode == TransportModePlay { c.rtcpReceivers[track.Id] = rtcpreceiver.New() v := time.Now().Unix() @@ -596,7 +588,7 @@ func (c *ConnClient) SetupUDP(u *url.URL, mode SetupMode, track *Track, rtpPort } // SetupTCP writes a SETUP request and reads a Response. -func (c *ConnClient) SetupTCP(u *url.URL, mode SetupMode, track *Track) (*base.Response, error) { +func (c *ConnClient) SetupTCP(u *url.URL, mode TransportMode, track *Track) (*base.Response, error) { if c.state != connClientStateInitial { return nil, fmt.Errorf("can't be called when reading or publishing") } @@ -617,15 +609,7 @@ func (c *ConnClient) SetupTCP(u *url.URL, mode SetupMode, track *Track) (*base.R return &ret }(), InterleavedIds: &interleavedIds, - Mode: func() *string { - var v string - if mode == SetupModeRecord { - v = "record" - } else { - v = "play" - } - return &v - }(), + Mode: &mode, }) if err != nil { return nil, err @@ -647,7 +631,7 @@ func (c *ConnClient) SetupTCP(u *url.URL, mode SetupMode, track *Track) (*base.R streamProtocol := StreamProtocolTCP c.streamProtocol = &streamProtocol - if mode == SetupModePlay { + if mode == TransportModePlay { c.rtcpReceivers[track.Id] = rtcpreceiver.New() } diff --git a/connclientdial.go b/connclientdial.go index 20f67556..9c9bfc40 100644 --- a/connclientdial.go +++ b/connclientdial.go @@ -30,7 +30,7 @@ func DialRead(address string, proto StreamProtocol) (*ConnClient, Tracks, error) if proto == StreamProtocolUDP { for _, track := range tracks { - _, err := conn.SetupUDP(u, SetupModePlay, track, 0, 0) + _, err := conn.SetupUDP(u, TransportModePlay, track, 0, 0) if err != nil { return nil, nil, err } @@ -38,7 +38,7 @@ func DialRead(address string, proto StreamProtocol) (*ConnClient, Tracks, error) } else { for _, track := range tracks { - _, err := conn.SetupTCP(u, SetupModePlay, track) + _, err := conn.SetupTCP(u, TransportModePlay, track) if err != nil { conn.Close() return nil, nil, err @@ -81,7 +81,7 @@ func DialPublish(address string, proto StreamProtocol, tracks Tracks) (*ConnClie if proto == StreamProtocolUDP { for _, track := range tracks { - _, err = conn.SetupUDP(u, SetupModeRecord, track, 0, 0) + _, err = conn.SetupUDP(u, TransportModeRecord, track, 0, 0) if err != nil { conn.Close() return nil, err @@ -90,7 +90,7 @@ func DialPublish(address string, proto StreamProtocol, tracks Tracks) (*ConnClie } else { for _, track := range tracks { - _, err = conn.SetupTCP(u, SetupModeRecord, track) + _, err = conn.SetupTCP(u, TransportModeRecord, track) if err != nil { conn.Close() return nil, err diff --git a/headers/transport.go b/headers/transport.go index a0f0d107..edad7f7c 100644 --- a/headers/transport.go +++ b/headers/transport.go @@ -54,6 +54,29 @@ func (sc StreamCast) String() string { return "unknown" } +// TransportMode is a transport mode. +type TransportMode int + +const ( + // TransportModePlay is the "play" transport mode + TransportModePlay TransportMode = iota + + // TransportModeRecord is the "record" transport mode + TransportModeRecord +) + +// String implements fmt.Stringer. +func (sm TransportMode) String() string { + switch sm { + case TransportModePlay: + return "play" + + case TransportModeRecord: + return "record" + } + return "unknown" +} + // Transport is a Transport header. type Transport struct { // protocol of the stream @@ -81,7 +104,7 @@ type Transport struct { InterleavedIds *[2]int // (optional) mode - Mode *string + Mode *TransportMode } func parsePorts(val string) (*[2]int, error) { @@ -142,6 +165,8 @@ func ReadTransport(v base.HeaderValue) (*Transport, error) { v := StreamMulticast ht.Cast = &v parts = parts[1:] + + // cast is optional, do not return any error } for _, t := range parts { @@ -186,10 +211,22 @@ func ReadTransport(v base.HeaderValue) (*Transport, error) { ht.InterleavedIds = ports } else if strings.HasPrefix(t, "mode=") { - v := strings.ToLower(t[len("mode="):]) - v = strings.TrimPrefix(v, "\"") - v = strings.TrimSuffix(v, "\"") - ht.Mode = &v + str := strings.ToLower(t[len("mode="):]) + str = strings.TrimPrefix(str, "\"") + str = strings.TrimSuffix(str, "\"") + + switch str { + case "play": + v := TransportModePlay + ht.Mode = &v + + case "record": + v := TransportModeRecord + ht.Mode = &v + + default: + return nil, fmt.Errorf("unrecognized transport mode: '%s'", str) + } } // ignore non-standard keys @@ -232,7 +269,11 @@ func (ht *Transport) Write() base.HeaderValue { } if ht.Mode != nil { - vals = append(vals, "mode="+*ht.Mode) + if *ht.Mode == TransportModePlay { + vals = append(vals, "mode=play") + } else { + vals = append(vals, "mode=record") + } } return base.HeaderValue{strings.Join(vals, ";")} diff --git a/headers/transport_test.go b/headers/transport_test.go index 08329018..68750771 100644 --- a/headers/transport_test.go +++ b/headers/transport_test.go @@ -25,8 +25,8 @@ var casesTransport = []struct { return &v }(), ClientPorts: &[2]int{3456, 3457}, - Mode: func() *string { - v := "play" + Mode: func() *TransportMode { + v := TransportModePlay return &v }(), }, diff --git a/utils.go b/utils.go index a3d05b15..25c632df 100644 --- a/utils.go +++ b/utils.go @@ -38,25 +38,13 @@ const ( StreamTypeRtcp = base.StreamTypeRtcp ) -// SetupMode is the setup mode. -type SetupMode int +// TransportMode is a transport mode. +type TransportMode = headers.TransportMode const ( - // SetupModePlay is the "play" setup mode - SetupModePlay SetupMode = iota + // TransportModePlay is the "play" transport mode + TransportModePlay TransportMode = headers.TransportModePlay - // SetupModeRecord is the "record" setup mode - SetupModeRecord + // TransportModeRecord is the "record" transport mode + TransportModeRecord TransportMode = headers.TransportModeRecord ) - -// String implements fmt.Stringer. -func (sm SetupMode) String() string { - switch sm { - case SetupModePlay: - return "play" - - case SetupModeRecord: - return "record" - } - return "unknown" -}