mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 07:06:58 +08:00
add client-read-h264 example
This commit is contained in:
52
examples/client-read-h264/main.go
Normal file
52
examples/client-read-h264/main.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user