add client-read-h264 example

This commit is contained in:
aler9
2021-03-07 17:26:41 +01:00
parent 4419e8ac30
commit f5c2308431
6 changed files with 61 additions and 10 deletions

View File

@@ -38,6 +38,7 @@ Features:
* [client-read-partial](examples/client-read-partial/main.go) * [client-read-partial](examples/client-read-partial/main.go)
* [client-read-options](examples/client-read-options/main.go) * [client-read-options](examples/client-read-options/main.go)
* [client-read-pause](examples/client-read-pause/main.go) * [client-read-pause](examples/client-read-pause/main.go)
* [client-read-h264](examples/client-read-h264/main.go)
* [client-publish](examples/client-publish/main.go) * [client-publish](examples/client-publish/main.go)
* [client-publish-options](examples/client-publish-options/main.go) * [client-publish-options](examples/client-publish-options/main.go)
* [client-publish-pause](examples/client-publish-pause/main.go) * [client-publish-pause](examples/client-publish-pause/main.go)

View File

@@ -0,0 +1,52 @@
package main
import (
"fmt"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/rtph264"
)
// This example shows how to
// 1. connect to a RTSP server and read all tracks on a path
// 2. check whether there's a H264 track
// 3. get H264 NALUs of that track
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()
// check whether there's a H264 track
h264Track := func() int {
for _, track := range conn.Tracks() {
if track.IsH264() {
return track.ID
}
}
return -1
}()
if h264Track < 0 {
panic(fmt.Errorf("H264 track not found"))
}
fmt.Printf("H264 track is number %d\n", h264Track+1)
// instantiate a decoder
dec := rtph264.NewDecoder()
// get H264 NALUs of that track
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
if trackID == h264Track {
nt, err := dec.Decode(buf)
if err != nil {
return
}
fmt.Printf("received H264 NALU of size %d\n", len(nt.NALU))
}
})
panic(err)
}

View File

@@ -9,8 +9,7 @@ import (
// This example shows how to // This example shows how to
// 1. set additional client options // 1. set additional client options
// 2. connect to a RTSP server // 2. connect to a RTSP server and read all tracks on a path
// 3. read all tracks on a path
func main() { func main() {
// ClientConf allows to set additional client options // ClientConf allows to set additional client options
@@ -32,7 +31,7 @@ func main() {
// read track frames // read track frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", trackID, typ, buf) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)
} }

View File

@@ -52,7 +52,7 @@ func main() {
// read track frames // read track frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", trackID, typ, buf) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)
} }

View File

@@ -8,8 +8,8 @@ import (
) )
// This example shows how to // This example shows how to
// 1. connect to a RTSP server // 1. connect to a RTSP server and read all tracks on a path
// 2. read all tracks for 5 seconds // 2. wait for 5 seconds
// 3. pause for 5 seconds // 3. pause for 5 seconds
// 4. repeat // 4. repeat
@@ -24,7 +24,7 @@ func main() {
for { for {
// read track frames // read track frames
done := conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { done := conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", trackID, typ, buf) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
// wait // wait

View File

@@ -7,8 +7,7 @@ import (
) )
// This example shows how to // This example shows how to
// 1. connect to a RTSP server // 1. connect to a RTSP server and 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
@@ -20,7 +19,7 @@ func main() {
// read track frames // read track frames
err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) { err = <-conn.ReadFrames(func(trackID int, typ gortsplib.StreamType, buf []byte) {
fmt.Printf("frame from track %d, type %v: %v\n", trackID, typ, buf) fmt.Printf("frame from track %d, type %v, size %d\n", trackID, typ, len(buf))
}) })
panic(err) panic(err)
} }