diff --git a/README.md b/README.md index 686e28c6..dc66cf8d 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Features: * [client-read-partial](examples/client-read-partial/main.go) * [client-read-options](examples/client-read-options/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-options](examples/client-publish-options/main.go) * [client-publish-pause](examples/client-publish-pause/main.go) diff --git a/examples/client-read-h264/main.go b/examples/client-read-h264/main.go new file mode 100644 index 00000000..e617f238 --- /dev/null +++ b/examples/client-read-h264/main.go @@ -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) +} diff --git a/examples/client-read-options/main.go b/examples/client-read-options/main.go index 46ace7c9..e29fd2da 100644 --- a/examples/client-read-options/main.go +++ b/examples/client-read-options/main.go @@ -9,8 +9,7 @@ import ( // This example shows how to // 1. set additional client options -// 2. connect to a RTSP server -// 3. read all tracks on a path +// 2. connect to a RTSP server and read all tracks on a path func main() { // ClientConf allows to set additional client options @@ -32,7 +31,7 @@ func main() { // read track frames 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) } diff --git a/examples/client-read-partial/main.go b/examples/client-read-partial/main.go index d87ce360..27dcc059 100644 --- a/examples/client-read-partial/main.go +++ b/examples/client-read-partial/main.go @@ -52,7 +52,7 @@ func main() { // read track frames 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) } diff --git a/examples/client-read-pause/main.go b/examples/client-read-pause/main.go index 6624ccb7..c69da2f6 100644 --- a/examples/client-read-pause/main.go +++ b/examples/client-read-pause/main.go @@ -8,8 +8,8 @@ import ( ) // This example shows how to -// 1. connect to a RTSP server -// 2. read all tracks for 5 seconds +// 1. connect to a RTSP server and read all tracks on a path +// 2. wait for 5 seconds // 3. pause for 5 seconds // 4. repeat @@ -24,7 +24,7 @@ func main() { for { // read track frames 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 diff --git a/examples/client-read/main.go b/examples/client-read/main.go index 41d95235..6e3ba648 100644 --- a/examples/client-read/main.go +++ b/examples/client-read/main.go @@ -7,8 +7,7 @@ import ( ) // This example shows how to -// 1. connect to a RTSP server -// 2. read all tracks on a path +// 1. connect to a RTSP server and read all tracks on a path func main() { // connect to the server and start reading all tracks @@ -20,7 +19,7 @@ func main() { // read track frames 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) }