mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 23:52:46 +08:00
rename SetupMode into TransportMode and move into headers
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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, "\"")
|
||||
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, ";")}
|
||||
|
@@ -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
|
||||
}(),
|
||||
},
|
||||
|
24
utils.go
24
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"
|
||||
}
|
||||
|
Reference in New Issue
Block a user