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

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