mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 07:06:58 +08:00
add partial client read example; fix #16
This commit is contained in:
@@ -10,9 +10,10 @@ RTSP 1.0 client and server library for the Go programming language, written for
|
|||||||
Features:
|
Features:
|
||||||
|
|
||||||
* Client
|
* Client
|
||||||
* Query servers about published streams
|
* Query servers about published tracks
|
||||||
* Read streams from servers with UDP or TCP
|
* Read tracks from servers with UDP or TCP
|
||||||
* Publish streams to 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
|
* Pause reading or publishing without disconnecting from the server
|
||||||
* Server
|
* Server
|
||||||
* Handle server-side connections
|
* Handle server-side connections
|
||||||
@@ -21,6 +22,7 @@ Features:
|
|||||||
|
|
||||||
* [client-query](examples/client-query.go)
|
* [client-query](examples/client-query.go)
|
||||||
* [client-read](examples/client-read.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-options](examples/client-read-options.go)
|
||||||
* [client-read-pause](examples/client-read-pause.go)
|
* [client-read-pause](examples/client-read-pause.go)
|
||||||
* [client-publish](examples/client-publish.go)
|
* [client-publish](examples/client-publish.go)
|
||||||
|
@@ -12,10 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * set additional client options
|
// 1. set additional client options
|
||||||
// * generate RTP/H264 frames from a file with Gstreamer
|
// 2. generate RTP/H264 frames from a file with Gstreamer
|
||||||
// * connect to a RTSP server, announce a H264 track
|
// 3. connect to a RTSP server, announce a H264 track
|
||||||
// * write the frames of the track
|
// 4. write the frames of the track
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// open a listener to receive RTP/H264 frames
|
// open a listener to receive RTP/H264 frames
|
||||||
@@ -66,14 +66,13 @@ func main() {
|
|||||||
// read frames from the source
|
// read frames from the source
|
||||||
n, _, err := pc.ReadFrom(buf)
|
n, _, err := pc.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write track frames
|
// write track frames
|
||||||
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
|
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("connection is closed (%s)\n", err)
|
panic(err)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,11 +12,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * generate RTP/H264 frames from a file with Gstreamer
|
// 1. generate RTP/H264 frames from a file with Gstreamer
|
||||||
// * connect to a RTSP server, announce a H264 track
|
// 2. connect to a RTSP server, announce a H264 track
|
||||||
// * write the frames of the track for 5 seconds
|
// 3. write the frames of the track for 5 seconds
|
||||||
// * pause for 5 seconds
|
// 4. pause for 5 seconds
|
||||||
// * repeat
|
// 5. repeat
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// open a listener to receive RTP/H264 frames
|
// open a listener to receive RTP/H264 frames
|
||||||
|
@@ -11,9 +11,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * generate RTP/H264 frames from a file with Gstreamer
|
// 1. generate RTP/H264 frames from a file with Gstreamer
|
||||||
// * connect to a RTSP server, announce a H264 track
|
// 2. connect to a RTSP server, announce a H264 track
|
||||||
// * write the frames of the track
|
// 3. write the frames of the track
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// open a listener to receive RTP/H264 frames
|
// open a listener to receive RTP/H264 frames
|
||||||
@@ -54,14 +54,13 @@ func main() {
|
|||||||
// read frames from the source
|
// read frames from the source
|
||||||
n, _, err := pc.ReadFrom(buf)
|
n, _, err := pc.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write track frames
|
// write track frames
|
||||||
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
|
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("connection is closed (%s)\n", err)
|
panic(err)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * connect to a RTSP server
|
// 1. connect to a RTSP server
|
||||||
// * query and print informations about tracks published on a path.
|
// 2. get 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")
|
||||||
@@ -30,14 +30,10 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tracks, res, err := conn.Describe(u)
|
tracks, _, err := conn.Describe(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != base.StatusOK {
|
|
||||||
panic(fmt.Errorf("server returned status %d", res.StatusCode))
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("available tracks: %v\n", tracks)
|
fmt.Println("available tracks: %v\n", tracks)
|
||||||
}
|
}
|
||||||
|
@@ -10,9 +10,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * set additional client options
|
// 1. set additional client options
|
||||||
// * connect to a RTSP server
|
// 2. connect to a RTSP server
|
||||||
// * read all tracks on a path
|
// 3. read all tracks on a path
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Dialer allows to set additional client options
|
// 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)
|
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
|
||||||
})
|
})
|
||||||
|
|
||||||
<-readerDone
|
// catch any error
|
||||||
|
err = <-readerDone
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
63
examples/client-read-partial.go
Normal file
63
examples/client-read-partial.go
Normal 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)
|
||||||
|
}
|
@@ -10,10 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * connect to a RTSP server
|
// 1. connect to a RTSP server
|
||||||
// * read all tracks for 5 seconds
|
// 2. read all tracks for 5 seconds
|
||||||
// * pause for 5 seconds
|
// 3. pause for 5 seconds
|
||||||
// * repeat
|
// 4. repeat
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// connect to the server and start reading all tracks
|
// connect to the server and start reading all tracks
|
||||||
|
@@ -9,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// This example shows how to
|
||||||
// * connect to a RTSP server
|
// 1. connect to a RTSP server
|
||||||
// * read all tracks on a path
|
// 2. read all tracks on a path
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// connect to the server and start reading all tracks
|
// 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)
|
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
|
||||||
})
|
})
|
||||||
|
|
||||||
<-readerDone
|
// catch any error
|
||||||
|
err = <-readerDone
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user