implement automatic protocol switching; fix #13

This commit is contained in:
aler9
2020-11-21 13:11:53 +01:00
parent 13bcdbaebf
commit a6d0fc140b
9 changed files with 208 additions and 108 deletions

View File

@@ -228,8 +228,6 @@ func (c *ConnClient) Do(req *base.Request) (*base.Response, error) {
} }
// Options writes an OPTIONS request and reads a response. // Options writes an OPTIONS request and reads a response.
// Since this method is not implemented by every RTSP server, the function
// does not fail if the returned code is StatusNotFound.
func (c *ConnClient) Options(u *base.URL) (*base.Response, error) { func (c *ConnClient) Options(u *base.URL) (*base.Response, error) {
err := c.checkState(map[connClientState]struct{}{ err := c.checkState(map[connClientState]struct{}{
connClientStateInitial: {}, connClientStateInitial: {},
@@ -248,8 +246,8 @@ func (c *ConnClient) Options(u *base.URL) (*base.Response, error) {
return nil, err return nil, err
} }
if res.StatusCode != base.StatusOK && res.StatusCode != base.StatusNotFound { if res.StatusCode != base.StatusOK {
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage) return res, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
} }
c.getParameterSupported = func() bool { c.getParameterSupported = func() bool {
@@ -291,8 +289,10 @@ func (c *ConnClient) Describe(u *base.URL) (Tracks, *base.Response, error) {
return nil, nil, err return nil, nil, err
} }
switch res.StatusCode { if res.StatusCode != base.StatusOK {
case base.StatusOK: return nil, res, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
contentType, ok := res.Header["Content-Type"] contentType, ok := res.Header["Content-Type"]
if !ok || len(contentType) != 1 { if !ok || len(contentType) != 1 {
return nil, nil, fmt.Errorf("Content-Type not provided") return nil, nil, fmt.Errorf("Content-Type not provided")
@@ -308,19 +308,6 @@ func (c *ConnClient) Describe(u *base.URL) (Tracks, *base.Response, error) {
} }
return tracks, res, nil return tracks, res, nil
case base.StatusMovedPermanently, base.StatusFound,
base.StatusSeeOther, base.StatusNotModified, base.StatusUseProxy:
location, ok := res.Header["Location"]
if !ok || len(location) != 1 {
return nil, nil, fmt.Errorf("Location not provided")
}
return nil, res, nil
default:
return nil, nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
}
} }
// 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.
@@ -485,7 +472,7 @@ func (c *ConnClient) Setup(u *base.URL, mode headers.TransportMode, proto base.S
rtpListener.close() rtpListener.close()
rtcpListener.close() rtcpListener.close()
} }
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage) return res, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
} }
th, err := headers.ReadTransport(res.Header["Transport"]) th, err := headers.ReadTransport(res.Header["Transport"])
@@ -575,7 +562,7 @@ func (c *ConnClient) Pause() (*base.Response, error) {
} }
if res.StatusCode != base.StatusOK { if res.StatusCode != base.StatusOK {
return nil, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage) return res, fmt.Errorf("bad status code: %d (%s)", res.StatusCode, res.StatusMessage)
} }
switch c.state { switch c.state {

View File

@@ -33,9 +33,9 @@ func DialPublish(address string, tracks Tracks) (*ConnClient, error) {
// Dialer allows to initialize a ConnClient. // Dialer allows to initialize a ConnClient.
type Dialer struct { type Dialer struct {
// (optional) the stream protocol. // (optional) the stream protocol (UDP or TCP).
// It defaults to StreamProtocolUDP. // If nil, it is chosen automatically (first UDP, then, if it fails, TCP).
StreamProtocol StreamProtocol StreamProtocol *StreamProtocol
// (optional) timeout of read operations. // (optional) timeout of read operations.
// It defaults to 10 seconds // It defaults to 10 seconds
@@ -113,30 +113,54 @@ func (d Dialer) DialRead(address string) (*ConnClient, error) {
return nil, err return nil, err
} }
_, err = conn.Options(u) res, err := conn.Options(u)
if err != nil { if err != nil {
// since this method is not implemented by every RTSP server,
// return only if status code is not 404
if res == nil || res.StatusCode != base.StatusNotFound {
conn.Close() conn.Close()
return nil, err return nil, err
} }
}
tracks, res, err := conn.Describe(u) tracks, res, err := conn.Describe(u)
if err != nil { if err != nil {
conn.Close() // redirect
return nil, err if res != nil && res.StatusCode >= base.StatusMovedPermanently &&
} res.StatusCode <= base.StatusUseProxy &&
len(res.Header["Location"]) == 1 {
if res.StatusCode >= base.StatusMovedPermanently &&
res.StatusCode <= base.StatusUseProxy {
conn.Close() conn.Close()
return d.DialRead(res.Header["Location"][0]) return d.DialRead(res.Header["Location"][0])
} }
for _, track := range tracks { conn.Close()
_, err := conn.Setup(u, headers.TransportModePlay, d.StreamProtocol, track, 0, 0) 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)
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 { if err != nil {
conn.Close() conn.Close()
return nil, err return nil, err
} }
} else {
conn.Close()
return nil, err
}
}
} }
_, err = conn.Play() _, err = conn.Play()
@@ -160,11 +184,15 @@ func (d Dialer) DialPublish(address string, tracks Tracks) (*ConnClient, error)
return nil, err return nil, err
} }
_, err = conn.Options(u) res, err := conn.Options(u)
if err != nil { if err != nil {
// since this method is not implemented by every RTSP server,
// return only if status code is not 404
if res == nil || res.StatusCode != base.StatusNotFound {
conn.Close() conn.Close()
return nil, err return nil, err
} }
}
_, err = conn.Announce(u, tracks) _, err = conn.Announce(u, tracks)
if err != nil { if err != nil {
@@ -172,12 +200,30 @@ func (d Dialer) DialPublish(address string, tracks Tracks) (*ConnClient, error)
return nil, err return nil, err
} }
for _, track := range tracks { proto := func() StreamProtocol {
_, err = conn.Setup(u, headers.TransportModeRecord, d.StreamProtocol, track, 0, 0) 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)
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 { if err != nil {
conn.Close() conn.Close()
return nil, err return nil, err
} }
} else {
conn.Close()
return nil, err
}
}
} }
_, err = conn.Record() _, err = conn.Record()

View File

@@ -59,7 +59,7 @@ func (c *container) wait() int {
return int(code) return int(code)
} }
func TestDialReadParallel(t *testing.T) { func TestDialRead(t *testing.T) {
for _, proto := range []string{ for _, proto := range []string{
"udp", "udp",
"tcp", "tcp",
@@ -85,12 +85,16 @@ func TestDialReadParallel(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if proto == "udp" { if proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream") conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
require.NoError(t, err) require.NoError(t, err)
@@ -115,7 +119,48 @@ func TestDialReadParallel(t *testing.T) {
} }
} }
func TestDialReadRedirectParallel(t *testing.T) { func TestDialReadAutomaticProtocol(t *testing.T) {
cnt1, err := newContainer("rtsp-simple-server", "server", []string{
"protocols: [tcp]\n",
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
cnt2, err := newContainer("ffmpeg", "publish", []string{
"-re",
"-stream_loop", "-1",
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "tcp",
"rtsp://localhost:8554/teststream",
})
require.NoError(t, err)
defer cnt2.close()
time.Sleep(1 * time.Second)
dialer := Dialer{StreamProtocol: nil}
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
require.NoError(t, err)
var firstFrame int32
frameRecv := make(chan struct{})
done := conn.OnFrame(func(id int, typ StreamType, content []byte) {
if atomic.SwapInt32(&firstFrame, 1) == 0 {
close(frameRecv)
}
})
<-frameRecv
conn.Close()
<-done
}
func TestDialReadRedirect(t *testing.T) {
cnt1, err := newContainer("rtsp-simple-server", "server", []string{ cnt1, err := newContainer("rtsp-simple-server", "server", []string{
"paths:\n" + "paths:\n" +
" path1:\n" + " path1:\n" +
@@ -158,7 +203,7 @@ func TestDialReadRedirectParallel(t *testing.T) {
<-done <-done
} }
func TestDialReadPauseParallel(t *testing.T) { func TestDialReadPause(t *testing.T) {
for _, proto := range []string{ for _, proto := range []string{
"udp", "udp",
"tcp", "tcp",
@@ -184,12 +229,16 @@ func TestDialReadPauseParallel(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if proto == "udp" { if proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
conn, err := dialer.DialRead("rtsp://localhost:8554/teststream") conn, err := dialer.DialRead("rtsp://localhost:8554/teststream")
require.NoError(t, err) require.NoError(t, err)
@@ -255,12 +304,16 @@ func TestDialPublishSerial(t *testing.T) {
track, err := NewTrackH264(0, sps, pps) track, err := NewTrackH264(0, sps, pps)
require.NoError(t, err) require.NoError(t, err)
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if proto == "udp" { if proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream", conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream",
Tracks{track}) Tracks{track})
@@ -337,12 +390,16 @@ func TestDialPublishParallel(t *testing.T) {
var conn *ConnClient var conn *ConnClient
defer func() { conn.Close() }() defer func() { conn.Close() }()
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if ca.proto == "udp" { if ca.proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
go func() { go func() {
defer close(writerDone) defer close(writerDone)
@@ -421,12 +478,16 @@ func TestDialPublishPauseSerial(t *testing.T) {
track, err := NewTrackH264(0, sps, pps) track, err := NewTrackH264(0, sps, pps)
require.NoError(t, err) require.NoError(t, err)
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if proto == "udp" { if proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream", conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream",
Tracks{track}) Tracks{track})
@@ -489,12 +550,16 @@ func TestDialPublishPauseParallel(t *testing.T) {
track, err := NewTrackH264(0, sps, pps) track, err := NewTrackH264(0, sps, pps)
require.NoError(t, err) require.NoError(t, err)
dialer := func() Dialer { dialer := Dialer{
StreamProtocol: func() *StreamProtocol {
if proto == "udp" { if proto == "udp" {
return Dialer{} v := StreamProtocolUDP
return &v
}
v := StreamProtocolTCP
return &v
}(),
} }
return Dialer{StreamProtocol: StreamProtocolTCP}
}()
conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream", conn, err := dialer.DialPublish("rtsp://localhost:8554/teststream",
Tracks{track}) Tracks{track})

View File

@@ -12,6 +12,7 @@ import (
) )
// This example shows how to // This example shows how to
// * set additional client options
// * generate RTP/H264 frames from a file with Gstreamer // * generate RTP/H264 frames from a file with Gstreamer
// * connect to a RTSP server, announce a H264 track // * connect to a RTSP server, announce a H264 track
// * write the frames of the track // * write the frames of the track
@@ -42,10 +43,10 @@ func main() {
panic(err) panic(err)
} }
// Dialer allows to set additional options // Dialer allows to set additional client options
dialer := gortsplib.Dialer{ dialer := gortsplib.Dialer{
// the stream protocol // the stream protocol (UDP or TCP). If nil, it is chosen automatically
StreamProtocol: gortsplib.StreamProtocolUDP, StreamProtocol: nil,
// timeout of read operations // timeout of read operations
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
// timeout of write operations // timeout of write operations
@@ -72,7 +73,7 @@ func main() {
break break
} }
// write frames to the server // write track frames
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n]) err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil { if err != nil {
fmt.Printf("connection is closed (%s)\n", err) fmt.Printf("connection is closed (%s)\n", err)

View File

@@ -65,7 +65,7 @@ func main() {
break break
} }
// write frames to the server // write track frames
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n]) err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil { if err != nil {
break break

View File

@@ -57,7 +57,7 @@ func main() {
break break
} }
// write frames to the server // write track frames
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n]) err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil { if err != nil {
fmt.Printf("connection is closed (%s)\n", err) fmt.Printf("connection is closed (%s)\n", err)

View File

@@ -10,14 +10,15 @@ import (
) )
// This example shows how to // This example shows how to
// * set additional client options
// * connect to a RTSP server // * connect to a RTSP server
// * read all tracks on a path // * read all tracks on a path
func main() { func main() {
// Dialer allows to set additional options // Dialer allows to set additional client options
dialer := gortsplib.Dialer{ dialer := gortsplib.Dialer{
// the stream protocol // the stream protocol (UDP or TCP). If nil, it is chosen automatically
StreamProtocol: gortsplib.StreamProtocolUDP, StreamProtocol: nil,
// timeout of read operations // timeout of read operations
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
// timeout of write operations // timeout of write operations
@@ -35,7 +36,7 @@ func main() {
} }
defer conn.Close() defer conn.Close()
// read frames from the server // read track frames
readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) { readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
}) })

View File

@@ -24,7 +24,7 @@ func main() {
defer conn.Close() defer conn.Close()
for { for {
// read frames from the server // read track frames
readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) { readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
}) })

View File

@@ -20,7 +20,7 @@ func main() {
} }
defer conn.Close() defer conn.Close()
// read frames from the server // read track frames
readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) { readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
}) })