diff --git a/README.md b/README.md index f64367b3..042eacc9 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ RTSP 1.0 client and server library for the Go programming language, written for Features: * Client - * Query servers about published streams - * Read streams from servers with UDP or TCP - * Publish streams to servers with UDP or TCP + * Query servers about published tracks + * Read tracks from servers with UDP or TCP + * Read only selected tracks + * Publish tracks to servers with UDP or TCP * Pause reading or publishing without disconnecting from the server * Server * Handle server-side connections @@ -21,6 +22,7 @@ Features: * [client-query](examples/client-query.go) * [client-read](examples/client-read.go) +* [client-read-partial](examples/client-read-partial.go) * [client-read-options](examples/client-read-options.go) * [client-read-pause](examples/client-read-pause.go) * [client-publish](examples/client-publish.go) diff --git a/examples/client-publish-options.go b/examples/client-publish-options.go index 2448ba9b..5343897f 100644 --- a/examples/client-publish-options.go +++ b/examples/client-publish-options.go @@ -12,10 +12,10 @@ import ( ) // This example shows how to -// * set additional client options -// * generate RTP/H264 frames from a file with Gstreamer -// * connect to a RTSP server, announce a H264 track -// * write the frames of the track +// 1. set additional client options +// 2. generate RTP/H264 frames from a file with Gstreamer +// 3. connect to a RTSP server, announce a H264 track +// 4. write the frames of the track func main() { // open a listener to receive RTP/H264 frames @@ -66,14 +66,13 @@ func main() { // read frames from the source n, _, err := pc.ReadFrom(buf) if err != nil { - break + panic(err) } // write track frames err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n]) if err != nil { - fmt.Printf("connection is closed (%s)\n", err) - break + panic(err) } } } diff --git a/examples/client-publish-pause.go b/examples/client-publish-pause.go index 8356ee69..c7c1866f 100644 --- a/examples/client-publish-pause.go +++ b/examples/client-publish-pause.go @@ -12,11 +12,11 @@ import ( ) // This example shows how to -// * generate RTP/H264 frames from a file with Gstreamer -// * connect to a RTSP server, announce a H264 track -// * write the frames of the track for 5 seconds -// * pause for 5 seconds -// * repeat +// 1. generate RTP/H264 frames from a file with Gstreamer +// 2. connect to a RTSP server, announce a H264 track +// 3. write the frames of the track for 5 seconds +// 4. pause for 5 seconds +// 5. repeat func main() { // open a listener to receive RTP/H264 frames diff --git a/examples/client-publish.go b/examples/client-publish.go index b60e8ba1..87f71e54 100644 --- a/examples/client-publish.go +++ b/examples/client-publish.go @@ -11,9 +11,9 @@ import ( ) // This example shows how to -// * generate RTP/H264 frames from a file with Gstreamer -// * connect to a RTSP server, announce a H264 track -// * write the frames of the track +// 1. generate RTP/H264 frames from a file with Gstreamer +// 2. connect to a RTSP server, announce a H264 track +// 3. write the frames of the track func main() { // open a listener to receive RTP/H264 frames @@ -54,14 +54,13 @@ func main() { // read frames from the source n, _, err := pc.ReadFrom(buf) if err != nil { - break + panic(err) } // write track frames err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n]) if err != nil { - fmt.Printf("connection is closed (%s)\n", err) - break + panic(err) } } } diff --git a/examples/client-query.go b/examples/client-query.go index ef6ad05f..9d11abb9 100644 --- a/examples/client-query.go +++ b/examples/client-query.go @@ -10,8 +10,8 @@ import ( ) // This example shows how to -// * connect to a RTSP server -// * query and print informations about tracks published on a path. +// 1. connect to a RTSP server +// 2. get and print informations about tracks published on a path. func main() { u, err := base.ParseURL("rtsp://myserver/mypath") @@ -30,14 +30,10 @@ func main() { panic(err) } - tracks, res, err := conn.Describe(u) + tracks, _, err := conn.Describe(u) if err != nil { panic(err) } - if res.StatusCode != base.StatusOK { - panic(fmt.Errorf("server returned status %d", res.StatusCode)) - } - fmt.Println("available tracks: %v\n", tracks) } diff --git a/examples/client-read-options.go b/examples/client-read-options.go index 15bf6618..9baead6b 100644 --- a/examples/client-read-options.go +++ b/examples/client-read-options.go @@ -10,9 +10,9 @@ import ( ) // This example shows how to -// * set additional client options -// * connect to a RTSP server -// * read all tracks on a path +// 1. set additional client options +// 2. connect to a RTSP server +// 3. read all tracks on a path func main() { // Dialer allows to set additional client options @@ -37,5 +37,7 @@ func main() { fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) }) - <-readerDone + // catch any error + err = <-readerDone + panic(err) } diff --git a/examples/client-read-partial.go b/examples/client-read-partial.go new file mode 100644 index 00000000..ca03b4b3 --- /dev/null +++ b/examples/client-read-partial.go @@ -0,0 +1,63 @@ +// +build ignore + +package main + +import ( + "fmt" + + "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/base" + "github.com/aler9/gortsplib/pkg/headers" +) + +// This example shows how to +// 1. connect to a RTSP server +// 2. get tracks published on a path +// 3. read only selected tracks + +func main() { + u, err := base.ParseURL("rtsp://myserver/mypath") + if err != nil { + panic(err) + } + + conn, err := gortsplib.Dial(u.Host) + if err != nil { + panic(err) + } + defer conn.Close() + + _, err = conn.Options(u) + if err != nil { + panic(err) + } + + tracks, _, err := conn.Describe(u) + if err != nil { + panic(err) + } + + for _, t := range tracks { + // start reading only video tracks, skipping audio or application tracks + if t.Media.MediaName.Media == "video" { + _, err := conn.Setup(headers.TransportModePlay, t, 0, 0) + if err != nil { + panic(err) + } + } + } + + _, err = conn.Play() + if err != nil { + panic(err) + } + + // read track frames + readerDone := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) { + fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) + }) + + // catch any error + err = <-readerDone + panic(err) +} diff --git a/examples/client-read-pause.go b/examples/client-read-pause.go index 2293b6f1..9bf4572c 100644 --- a/examples/client-read-pause.go +++ b/examples/client-read-pause.go @@ -10,10 +10,10 @@ import ( ) // This example shows how to -// * connect to a RTSP server -// * read all tracks for 5 seconds -// * pause for 5 seconds -// * repeat +// 1. connect to a RTSP server +// 2. read all tracks for 5 seconds +// 3. pause for 5 seconds +// 4. repeat func main() { // connect to the server and start reading all tracks diff --git a/examples/client-read.go b/examples/client-read.go index c297f280..e80b6dba 100644 --- a/examples/client-read.go +++ b/examples/client-read.go @@ -9,8 +9,8 @@ import ( ) // This example shows how to -// * connect to a RTSP server -// * read all tracks on a path +// 1. connect to a RTSP server +// 2. read all tracks on a path func main() { // connect to the server and start reading all tracks @@ -25,5 +25,7 @@ func main() { fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) }) - <-readerDone + // catch any error + err = <-readerDone + panic(err) }