mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 15:46:51 +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
|
// 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 {
|
control := func() string {
|
||||||
// if we're recording, get control from track ID
|
// if we're recording, get control from track ID
|
||||||
if mode == SetupModeRecord {
|
if mode == TransportModeRecord {
|
||||||
return "trackID=" + strconv.FormatInt(int64(track.Id), 10)
|
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
|
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{
|
res, err := c.Do(&base.Request{
|
||||||
Method: base.SETUP,
|
Method: base.SETUP,
|
||||||
Url: c.urlForTrack(u, mode, track),
|
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.
|
// SetupUDP writes a SETUP request and reads a Response.
|
||||||
// If rtpPort and rtcpPort are zero, they are be chosen automatically.
|
// 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) {
|
rtcpPort int) (*base.Response, error) {
|
||||||
if c.state != connClientStateInitial {
|
if c.state != connClientStateInitial {
|
||||||
return nil, fmt.Errorf("can't be called when reading or publishing")
|
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
|
return &ret
|
||||||
}(),
|
}(),
|
||||||
ClientPorts: &[2]int{rtpPort, rtcpPort},
|
ClientPorts: &[2]int{rtpPort, rtcpPort},
|
||||||
Mode: func() *string {
|
Mode: &mode,
|
||||||
var v string
|
|
||||||
if mode == SetupModeRecord {
|
|
||||||
v = "record"
|
|
||||||
} else {
|
|
||||||
v = "play"
|
|
||||||
}
|
|
||||||
return &v
|
|
||||||
}(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rtpListener.close()
|
rtpListener.close()
|
||||||
@@ -575,7 +567,7 @@ func (c *ConnClient) SetupUDP(u *url.URL, mode SetupMode, track *Track, rtpPort
|
|||||||
streamProtocol := StreamProtocolUDP
|
streamProtocol := StreamProtocolUDP
|
||||||
c.streamProtocol = &streamProtocol
|
c.streamProtocol = &streamProtocol
|
||||||
|
|
||||||
if mode == SetupModePlay {
|
if mode == TransportModePlay {
|
||||||
c.rtcpReceivers[track.Id] = rtcpreceiver.New()
|
c.rtcpReceivers[track.Id] = rtcpreceiver.New()
|
||||||
|
|
||||||
v := time.Now().Unix()
|
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.
|
// 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 {
|
if c.state != connClientStateInitial {
|
||||||
return nil, fmt.Errorf("can't be called when reading or publishing")
|
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
|
return &ret
|
||||||
}(),
|
}(),
|
||||||
InterleavedIds: &interleavedIds,
|
InterleavedIds: &interleavedIds,
|
||||||
Mode: func() *string {
|
Mode: &mode,
|
||||||
var v string
|
|
||||||
if mode == SetupModeRecord {
|
|
||||||
v = "record"
|
|
||||||
} else {
|
|
||||||
v = "play"
|
|
||||||
}
|
|
||||||
return &v
|
|
||||||
}(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -647,7 +631,7 @@ func (c *ConnClient) SetupTCP(u *url.URL, mode SetupMode, track *Track) (*base.R
|
|||||||
streamProtocol := StreamProtocolTCP
|
streamProtocol := StreamProtocolTCP
|
||||||
c.streamProtocol = &streamProtocol
|
c.streamProtocol = &streamProtocol
|
||||||
|
|
||||||
if mode == SetupModePlay {
|
if mode == TransportModePlay {
|
||||||
c.rtcpReceivers[track.Id] = rtcpreceiver.New()
|
c.rtcpReceivers[track.Id] = rtcpreceiver.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@ func DialRead(address string, proto StreamProtocol) (*ConnClient, Tracks, error)
|
|||||||
|
|
||||||
if proto == StreamProtocolUDP {
|
if proto == StreamProtocolUDP {
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
_, err := conn.SetupUDP(u, SetupModePlay, track, 0, 0)
|
_, err := conn.SetupUDP(u, TransportModePlay, track, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ func DialRead(address string, proto StreamProtocol) (*ConnClient, Tracks, error)
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
_, err := conn.SetupTCP(u, SetupModePlay, track)
|
_, err := conn.SetupTCP(u, TransportModePlay, track)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -81,7 +81,7 @@ func DialPublish(address string, proto StreamProtocol, tracks Tracks) (*ConnClie
|
|||||||
|
|
||||||
if proto == StreamProtocolUDP {
|
if proto == StreamProtocolUDP {
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
_, err = conn.SetupUDP(u, SetupModeRecord, track, 0, 0)
|
_, err = conn.SetupUDP(u, TransportModeRecord, track, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -90,7 +90,7 @@ func DialPublish(address string, proto StreamProtocol, tracks Tracks) (*ConnClie
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
for _, track := range tracks {
|
for _, track := range tracks {
|
||||||
_, err = conn.SetupTCP(u, SetupModeRecord, track)
|
_, err = conn.SetupTCP(u, TransportModeRecord, track)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -54,6 +54,29 @@ func (sc StreamCast) String() string {
|
|||||||
return "unknown"
|
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.
|
// Transport is a Transport header.
|
||||||
type Transport struct {
|
type Transport struct {
|
||||||
// protocol of the stream
|
// protocol of the stream
|
||||||
@@ -81,7 +104,7 @@ type Transport struct {
|
|||||||
InterleavedIds *[2]int
|
InterleavedIds *[2]int
|
||||||
|
|
||||||
// (optional) mode
|
// (optional) mode
|
||||||
Mode *string
|
Mode *TransportMode
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePorts(val string) (*[2]int, error) {
|
func parsePorts(val string) (*[2]int, error) {
|
||||||
@@ -142,6 +165,8 @@ func ReadTransport(v base.HeaderValue) (*Transport, error) {
|
|||||||
v := StreamMulticast
|
v := StreamMulticast
|
||||||
ht.Cast = &v
|
ht.Cast = &v
|
||||||
parts = parts[1:]
|
parts = parts[1:]
|
||||||
|
|
||||||
|
// cast is optional, do not return any error
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range parts {
|
for _, t := range parts {
|
||||||
@@ -186,10 +211,22 @@ func ReadTransport(v base.HeaderValue) (*Transport, error) {
|
|||||||
ht.InterleavedIds = ports
|
ht.InterleavedIds = ports
|
||||||
|
|
||||||
} else if strings.HasPrefix(t, "mode=") {
|
} else if strings.HasPrefix(t, "mode=") {
|
||||||
v := strings.ToLower(t[len("mode="):])
|
str := strings.ToLower(t[len("mode="):])
|
||||||
v = strings.TrimPrefix(v, "\"")
|
str = strings.TrimPrefix(str, "\"")
|
||||||
v = strings.TrimSuffix(v, "\"")
|
str = strings.TrimSuffix(str, "\"")
|
||||||
ht.Mode = &v
|
|
||||||
|
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
|
// ignore non-standard keys
|
||||||
@@ -232,7 +269,11 @@ func (ht *Transport) Write() base.HeaderValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ht.Mode != nil {
|
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, ";")}
|
return base.HeaderValue{strings.Join(vals, ";")}
|
||||||
|
@@ -25,8 +25,8 @@ var casesTransport = []struct {
|
|||||||
return &v
|
return &v
|
||||||
}(),
|
}(),
|
||||||
ClientPorts: &[2]int{3456, 3457},
|
ClientPorts: &[2]int{3456, 3457},
|
||||||
Mode: func() *string {
|
Mode: func() *TransportMode {
|
||||||
v := "play"
|
v := TransportModePlay
|
||||||
return &v
|
return &v
|
||||||
}(),
|
}(),
|
||||||
},
|
},
|
||||||
|
24
utils.go
24
utils.go
@@ -38,25 +38,13 @@ const (
|
|||||||
StreamTypeRtcp = base.StreamTypeRtcp
|
StreamTypeRtcp = base.StreamTypeRtcp
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetupMode is the setup mode.
|
// TransportMode is a transport mode.
|
||||||
type SetupMode int
|
type TransportMode = headers.TransportMode
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// SetupModePlay is the "play" setup mode
|
// TransportModePlay is the "play" transport mode
|
||||||
SetupModePlay SetupMode = iota
|
TransportModePlay TransportMode = headers.TransportModePlay
|
||||||
|
|
||||||
// SetupModeRecord is the "record" setup mode
|
// TransportModeRecord is the "record" transport mode
|
||||||
SetupModeRecord
|
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