diff --git a/README.md b/README.md index 708631a7..05e5908a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Features: * [client-read-tcp](examples/client-read-tcp.go) * [client-publish-udp](examples/client-publish-udp.go) * [client-publish-tcp](examples/client-publish-tcp.go) +* [client-read-pause](examples/client-read-pause.go) ## Documentation diff --git a/examples/client-publish-tcp.go b/examples/client-publish-tcp.go index 93fe6809..c224f49a 100644 --- a/examples/client-publish-tcp.go +++ b/examples/client-publish-tcp.go @@ -10,9 +10,10 @@ import ( "github.com/aler9/gortsplib/pkg/rtph264" ) -// This example shows how to generate RTP/H264 frames from a file with Gstreamer, -// create a RTSP client, connect to a server, announce a H264 track and write -// the frames with the TCP protocol. +// 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 with the TCP protocol func main() { // open a listener to receive RTP/H264 frames diff --git a/examples/client-publish-udp.go b/examples/client-publish-udp.go index a8789480..cc68050a 100644 --- a/examples/client-publish-udp.go +++ b/examples/client-publish-udp.go @@ -10,9 +10,10 @@ import ( "github.com/aler9/gortsplib/pkg/rtph264" ) -// This example shows how to generate RTP/H264 frames from a file with Gstreamer, -// create a RTSP client, connect to a server, announce a H264 track and write -// the frames with the UDP protocol. +// 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 with the UDP protocol func main() { // open a listener to receive RTP/H264 frames diff --git a/examples/client-query.go b/examples/client-query.go index 096ad816..ef6ad05f 100644 --- a/examples/client-query.go +++ b/examples/client-query.go @@ -9,8 +9,9 @@ import ( "github.com/aler9/gortsplib/pkg/base" ) -// This example shows how to connect to a server and print informations about -// tracks published on a path. +// This example shows how to +// * connect to a RTSP server +// * query and print informations about tracks published on a path. func main() { u, err := base.ParseURL("rtsp://myserver/mypath") diff --git a/examples/client-read-pause.go b/examples/client-read-pause.go new file mode 100644 index 00000000..7611d59f --- /dev/null +++ b/examples/client-read-pause.go @@ -0,0 +1,58 @@ +// +build ignore + +package main + +import ( + "fmt" + + "github.com/aler9/gortsplib" +) + +// This example shows how to +// * connect to a RTSP server +// * read all tracks with the UDP protocol for 5 seconds +// * pause for 5 seconds +// * repeat + +func main() { + // connect to the server and start reading all tracks + conn, err := gortsplib.DialRead("rtsp://localhost:8554/mystream") + if err != nil { + panic(err) + } + defer conn.Close() + + for { + // read frames from the server + readerDone := make(chan struct{}) + conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) { + if err != nil { + close(readerDone) + return + } + + fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) + }) + + // wait + time.Sleep(5 * time.Second) + + // pause + _, err := conn.Pause() + if err != nil { + panic(err) + } + + // join reader + <-readerDone + + // wait + time.Sleep(5 * time.Second) + + // play again + _, err := conn.Play() + if err != nil { + panic(err) + } + } +} diff --git a/examples/client-read-tcp.go b/examples/client-read-tcp.go index 9e97b17f..a3203ede 100644 --- a/examples/client-read-tcp.go +++ b/examples/client-read-tcp.go @@ -8,8 +8,9 @@ import ( "github.com/aler9/gortsplib" ) -// This example shows how to create a RTSP client, connect to a server and -// read all tracks with the TCP protocol. +// This example shows how to +// * connect to a RTSP server +// * read all tracks with the TCP protocol func main() { // connect to the server and start reading all tracks @@ -23,7 +24,6 @@ func main() { defer conn.Close() readerDone := make(chan struct{}) - defer func() { <-readerDone }() // read frames from the server conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) { @@ -35,4 +35,6 @@ func main() { fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) }) + + <-readerDone } diff --git a/examples/client-read-udp.go b/examples/client-read-udp.go index 789516dd..778f95e9 100644 --- a/examples/client-read-udp.go +++ b/examples/client-read-udp.go @@ -8,8 +8,9 @@ import ( "github.com/aler9/gortsplib" ) -// This example shows how to create a RTSP client, connect to a server and -// read all tracks with the UDP protocol. +// This example shows how to +// * connect to a RTSP server +// * read all tracks with the UDP protocol func main() { // connect to the server and start reading all tracks @@ -20,7 +21,6 @@ func main() { defer conn.Close() readerDone := make(chan struct{}) - defer func() { <-readerDone }() // read frames from the server conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) { @@ -32,4 +32,6 @@ func main() { fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf) }) + + <-readerDone }