add partial client read example; fix #16

This commit is contained in:
aler9
2020-11-30 14:42:32 +01:00
parent 544784a539
commit b77433bc9d
9 changed files with 102 additions and 39 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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