add client-read-pause example

This commit is contained in:
aler9
2020-11-19 11:20:23 +01:00
parent ca060556b0
commit 8a8a8d9b5a
7 changed files with 80 additions and 14 deletions

View File

@@ -24,6 +24,7 @@ Features:
* [client-read-tcp](examples/client-read-tcp.go) * [client-read-tcp](examples/client-read-tcp.go)
* [client-publish-udp](examples/client-publish-udp.go) * [client-publish-udp](examples/client-publish-udp.go)
* [client-publish-tcp](examples/client-publish-tcp.go) * [client-publish-tcp](examples/client-publish-tcp.go)
* [client-read-pause](examples/client-read-pause.go)
## Documentation ## Documentation

View File

@@ -10,9 +10,10 @@ import (
"github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/gortsplib/pkg/rtph264"
) )
// This example shows how to generate RTP/H264 frames from a file with Gstreamer, // This example shows how to
// create a RTSP client, connect to a server, announce a H264 track and write // * generate RTP/H264 frames from a file with Gstreamer
// the frames with the TCP protocol. // * connect to a RTSP server, announce a H264 track
// * write the frames with the TCP protocol
func main() { func main() {
// open a listener to receive RTP/H264 frames // open a listener to receive RTP/H264 frames

View File

@@ -10,9 +10,10 @@ import (
"github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/gortsplib/pkg/rtph264"
) )
// This example shows how to generate RTP/H264 frames from a file with Gstreamer, // This example shows how to
// create a RTSP client, connect to a server, announce a H264 track and write // * generate RTP/H264 frames from a file with Gstreamer
// the frames with the UDP protocol. // * connect to a RTSP server, announce a H264 track
// * write the frames with the UDP protocol
func main() { func main() {
// open a listener to receive RTP/H264 frames // open a listener to receive RTP/H264 frames

View File

@@ -9,8 +9,9 @@ import (
"github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/base"
) )
// This example shows how to connect to a server and print informations about // This example shows how to
// tracks published on a path. // * connect to a RTSP server
// * query and print informations about tracks published on a path.
func main() { func main() {
u, err := base.ParseURL("rtsp://myserver/mypath") u, err := base.ParseURL("rtsp://myserver/mypath")

View File

@@ -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)
}
}
}

View File

@@ -8,8 +8,9 @@ import (
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
) )
// This example shows how to create a RTSP client, connect to a server and // This example shows how to
// read all tracks with the TCP protocol. // * connect to a RTSP server
// * read all tracks with the TCP protocol
func main() { func main() {
// connect to the server and start reading all tracks // connect to the server and start reading all tracks
@@ -23,7 +24,6 @@ func main() {
defer conn.Close() defer conn.Close()
readerDone := make(chan struct{}) readerDone := make(chan struct{})
defer func() { <-readerDone }()
// read frames from the server // read frames from the server
conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) { 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) fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
}) })
<-readerDone
} }

View File

@@ -8,8 +8,9 @@ import (
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
) )
// This example shows how to create a RTSP client, connect to a server and // This example shows how to
// read all tracks with the UDP protocol. // * connect to a RTSP server
// * read all tracks with the UDP protocol
func main() { func main() {
// connect to the server and start reading all tracks // connect to the server and start reading all tracks
@@ -20,7 +21,6 @@ func main() {
defer conn.Close() defer conn.Close()
readerDone := make(chan struct{}) readerDone := make(chan struct{})
defer func() { <-readerDone }()
// read frames from the server // read frames from the server
conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte, err error) { 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) fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
}) })
<-readerDone
} }