diff --git a/client.go b/client.go index 5d9f37b2..91b1b987 100644 --- a/client.go +++ b/client.go @@ -381,38 +381,6 @@ func (c *Client) Start(scheme string, host string) error { return nil } -// StartReading connects to the address and starts reading all tracks. -func (c *Client) StartReading(address string) error { - u, err := url.Parse(address) - if err != nil { - return err - } - - err = c.Start(u.Scheme, u.Host) - if err != nil { - return err - } - - tracks, baseURL, _, err := c.Describe(u) - if err != nil { - c.Close() - return err - } - - return c.SetupAndPlay(tracks, baseURL) -} - -// StartReadingAndWait connects to the address, starts reading all tracks and waits -// until a read error. -func (c *Client) StartReadingAndWait(address string) error { - err := c.StartReading(address) - if err != nil { - return err - } - - return c.Wait() -} - // StartPublishing connects to the address and starts publishing the tracks. func (c *Client) StartPublishing(address string, tracks Tracks) error { u, err := url.Parse(address) diff --git a/client_publish_test.go b/client_publish_test.go index 17ff4679..d842c672 100644 --- a/client_publish_test.go +++ b/client_publish_test.go @@ -218,7 +218,7 @@ func TestClientPublishSerial(t *testing.T) { recvDone := make(chan struct{}) - c := &Client{ + c := Client{ TLSConfig: &tls.Config{ InsecureSkipVerify: true, }, @@ -380,7 +380,7 @@ func TestClientPublishParallel(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ TLSConfig: &tls.Config{ InsecureSkipVerify: true, }, @@ -549,7 +549,7 @@ func TestClientPublishPauseSerial(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ Transport: func() *Transport { if transport == "udp" { v := TransportUDP @@ -688,7 +688,7 @@ func TestClientPublishPauseParallel(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ Transport: func() *Transport { if transport == "udp" { v := TransportUDP @@ -986,7 +986,7 @@ func TestClientPublishRTCPReport(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ udpSenderReportPeriod: 1 * time.Second, } @@ -1122,7 +1122,7 @@ func TestClientPublishIgnoreTCPRTPPackets(t *testing.T) { rtcpReceived := make(chan struct{}) - c := &Client{ + c := Client{ Transport: func() *Transport { v := TransportTCP return &v diff --git a/client_read_test.go b/client_read_test.go index 62ecf7fd..d9552f83 100644 --- a/client_read_test.go +++ b/client_read_test.go @@ -22,6 +22,32 @@ import ( "github.com/aler9/gortsplib/pkg/url" ) +func startReading(c *Client, ur string) error { + u, err := url.Parse(ur) + if err != nil { + return err + } + + err = c.Start(u.Scheme, u.Host) + if err != nil { + return err + } + + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + c.Close() + return err + } + + err = c.SetupAndPlay(tracks, baseURL) + if err != nil { + c.Close() + return err + } + + return nil +} + func TestClientReadTracks(t *testing.T) { track1 := &TrackH264{ PayloadType: 96, @@ -158,7 +184,7 @@ func TestClientReadTracks(t *testing.T) { c := Client{} - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -403,7 +429,7 @@ func TestClientRead(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ TLSConfig: &tls.Config{ InsecureSkipVerify: true, }, @@ -432,7 +458,7 @@ func TestClientRead(t *testing.T) { require.NoError(t, err) } - err = c.StartReading(scheme + "://" + listenIP + ":8554/test/stream?param=value") + err = startReading(&c, scheme+"://"+listenIP+":8554/test/stream?param=value") require.NoError(t, err) defer c.Close() @@ -565,7 +591,7 @@ func TestClientReadPartial(t *testing.T) { packetRecv := make(chan struct{}) - c := &Client{ + c := Client{ Transport: func() *Transport { v := TransportTCP return &v @@ -728,7 +754,7 @@ func TestClientReadContentBase(t *testing.T) { c := Client{} - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) c.Close() }) @@ -883,7 +909,7 @@ func TestClientReadAnyPort(t *testing.T) { packetRecv := make(chan struct{}) - c := &Client{ + c := Client{ AnyPortEnable: true, OnPacketRTP: func(ctx *ClientOnPacketRTPCtx) { require.Equal(t, &testRTPPacket, ctx.Packet) @@ -891,7 +917,7 @@ func TestClientReadAnyPort(t *testing.T) { }, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -1023,7 +1049,7 @@ func TestClientReadAutomaticProtocol(t *testing.T) { }, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -1266,14 +1292,14 @@ func TestClientReadAutomaticProtocol(t *testing.T) { packetRecv := make(chan struct{}) - c := &Client{ + c := Client{ ReadTimeout: 1 * time.Second, OnPacketRTP: func(ctx *ClientOnPacketRTPCtx) { close(packetRecv) }, } - err = c.StartReading("rtsp://myuser:mypass@localhost:8554/teststream") + err = startReading(&c, "rtsp://myuser:mypass@localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -1397,7 +1423,7 @@ func TestClientReadDifferentInterleavedIDs(t *testing.T) { packetRecv := make(chan struct{}) - c := &Client{ + c := Client{ Transport: func() *Transport { v := TransportTCP return &v @@ -1408,7 +1434,7 @@ func TestClientReadDifferentInterleavedIDs(t *testing.T) { }, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -1606,7 +1632,7 @@ func TestClientReadRedirect(t *testing.T) { if withCredentials { ru = "rtsp://testusr:testpwd@localhost:8554/path1" } - err = c.StartReading(ru) + err = startReading(&c, ru) require.NoError(t, err) defer c.Close() @@ -1806,7 +1832,7 @@ func TestClientReadPause(t *testing.T) { firstFrame := int32(0) packetRecv := make(chan struct{}) - c := &Client{ + c := Client{ Transport: func() *Transport { if transport == "udp" { v := TransportUDP @@ -1822,7 +1848,7 @@ func TestClientReadPause(t *testing.T) { }, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -2013,11 +2039,11 @@ func TestClientReadRTCPReport(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ udpReceiverReportPeriod: 1 * time.Second, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -2154,7 +2180,7 @@ func TestClientReadErrorTimeout(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ Transport: func() *Transport { switch transport { case "udp": @@ -2171,7 +2197,7 @@ func TestClientReadErrorTimeout(t *testing.T) { ReadTimeout: 1 * time.Second, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) err = c.Wait() @@ -2306,7 +2332,7 @@ func TestClientReadIgnoreTCPInvalidTrack(t *testing.T) { recv := make(chan struct{}) - c := &Client{ + c := Client{ Transport: func() *Transport { v := TransportTCP return &v @@ -2316,7 +2342,7 @@ func TestClientReadIgnoreTCPInvalidTrack(t *testing.T) { }, } - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -2463,7 +2489,7 @@ func TestClientReadSeek(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ Transport: func() *Transport { v := TransportTCP return &v @@ -2623,9 +2649,9 @@ func TestClientReadKeepaliveFromSession(t *testing.T) { close(keepaliveOk) }() - c := &Client{} + c := Client{} - err = c.StartReading("rtsp://localhost:8554/teststream") + err = startReading(&c, "rtsp://localhost:8554/teststream") require.NoError(t, err) defer c.Close() @@ -2763,7 +2789,7 @@ func TestClientReadDifferentSource(t *testing.T) { require.NoError(t, err) }() - c := &Client{ + c := Client{ TLSConfig: &tls.Config{ InsecureSkipVerify: true, }, @@ -2779,7 +2805,7 @@ func TestClientReadDifferentSource(t *testing.T) { close(packetRecv) } - err = c.StartReading("rtsp://localhost:8554/test/stream?param=value") + err = startReading(&c, "rtsp://localhost:8554/test/stream?param=value") require.NoError(t, err) defer c.Close() diff --git a/examples/client-read-aac/main.go b/examples/client-read-aac/main.go index 38ec96a2..49f8cd15 100644 --- a/examples/client-read-aac/main.go +++ b/examples/client-read-aac/main.go @@ -75,7 +75,7 @@ func main() { } } - // start reading tracks + // setup and read all tracks err = c.SetupAndPlay(tracks, baseURL) if err != nil { panic(err) diff --git a/examples/client-read-h264-convert-to-jpeg/main.go b/examples/client-read-h264-convert-to-jpeg/main.go index b1a7fe35..8baa6baf 100644 --- a/examples/client-read-h264-convert-to-jpeg/main.go +++ b/examples/client-read-h264-convert-to-jpeg/main.go @@ -127,7 +127,7 @@ func main() { } } - // start reading tracks + // setup and read all tracks err = c.SetupAndPlay(tracks, baseURL) if err != nil { panic(err) diff --git a/examples/client-read-h264-save-to-disk/main.go b/examples/client-read-h264-save-to-disk/main.go index 93d7277c..7084d491 100644 --- a/examples/client-read-h264-save-to-disk/main.go +++ b/examples/client-read-h264-save-to-disk/main.go @@ -68,7 +68,7 @@ func main() { } } - // start reading tracks + // setup and read all tracks err = c.SetupAndPlay(tracks, baseURL) if err != nil { panic(err) diff --git a/examples/client-read-h264/main.go b/examples/client-read-h264/main.go index 2326b0b5..eb0a3608 100644 --- a/examples/client-read-h264/main.go +++ b/examples/client-read-h264/main.go @@ -93,7 +93,7 @@ func main() { } } - // start reading tracks + // setup and read all tracks err = c.SetupAndPlay(tracks, baseURL) if err != nil { panic(err) diff --git a/examples/client-read-options/main.go b/examples/client-read-options/main.go index 5868a77b..5a582d5a 100644 --- a/examples/client-read-options/main.go +++ b/examples/client-read-options/main.go @@ -5,6 +5,7 @@ import ( "time" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/url" ) // This example shows how to @@ -30,6 +31,31 @@ func main() { }, } - // connect to the server and start reading all tracks - panic(c.StartReadingAndWait("rtsp://localhost:8554/mystream")) + // parse URL + u, err := url.Parse("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = c.Start(u.Scheme, u.Host) + if err != nil { + panic(err) + } + defer c.Close() + + // find published tracks + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + panic(err) + } + + // setup and read all tracks + err = c.SetupAndPlay(tracks, baseURL) + if err != nil { + panic(err) + } + + // wait until a fatal error + panic(c.Wait()) } diff --git a/examples/client-read-pause/main.go b/examples/client-read-pause/main.go index a508461e..8bb7bf15 100644 --- a/examples/client-read-pause/main.go +++ b/examples/client-read-pause/main.go @@ -5,6 +5,7 @@ import ( "time" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/url" ) // This example shows how to @@ -25,13 +26,31 @@ func main() { }, } - // connect to the server and start reading all tracks - err := c.StartReading("rtsp://localhost:8554/mystream") + // parse URL + u, err := url.Parse("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = c.Start(u.Scheme, u.Host) if err != nil { panic(err) } defer c.Close() + // find published tracks + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + panic(err) + } + + // setup and read all tracks + err = c.SetupAndPlay(tracks, baseURL) + if err != nil { + panic(err) + } + for { // wait time.Sleep(5 * time.Second) diff --git a/examples/client-read-republish/main.go b/examples/client-read-republish/main.go index 4acf7a08..4c43b928 100644 --- a/examples/client-read-republish/main.go +++ b/examples/client-read-republish/main.go @@ -49,7 +49,7 @@ func main() { publisher.WritePacketRTP(ctx.TrackID, ctx.Packet, ctx.PTSEqualsDTS) } - // start reading tracks + // setup and read all tracks err = reader.SetupAndPlay(tracks, baseURL) if err != nil { panic(err) diff --git a/examples/client-read/main.go b/examples/client-read/main.go index 2d213732..823c336d 100644 --- a/examples/client-read/main.go +++ b/examples/client-read/main.go @@ -4,6 +4,7 @@ import ( "log" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/url" ) // This example shows how to connect to a RTSP server @@ -21,6 +22,31 @@ func main() { }, } - // connect to the server and start reading all tracks - panic(c.StartReadingAndWait("rtsp://localhost:8554/mystream")) + // parse URL + u, err := url.Parse("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + + // connect to the server + err = c.Start(u.Scheme, u.Host) + if err != nil { + panic(err) + } + defer c.Close() + + // find published tracks + tracks, baseURL, _, err := c.Describe(u) + if err != nil { + panic(err) + } + + // setup and read all tracks + err = c.SetupAndPlay(tracks, baseURL) + if err != nil { + panic(err) + } + + // wait until a fatal error + panic(c.Wait()) }