improve examples (#703)

* add client-play-format-av1-to-jpeg
* improve client-play-format-av1 to decode frames
* improve speed of sample decoders by using pointers instead of copies
* improve client-record-format-h264 and client-record-format-h265 to encode frames
* add client-record-format-av1
This commit is contained in:
Alessandro Ros
2025-02-19 22:00:49 +01:00
committed by GitHub
parent 55556f1ecf
commit a17e1f776e
26 changed files with 1978 additions and 647 deletions

View File

@@ -9,13 +9,14 @@ import (
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtph264"
"github.com/bluenviron/mediacommon/v2/pkg/codecs/h264"
"github.com/pion/rtp"
)
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's an H264 format
// 3. decode the H264 format into RGBA frames
// 3. decode the H264 stream into RGBA frames
// This example requires the FFmpeg libraries, that can be installed with this command:
// apt install -y libavformat-dev libswscale-dev gcc pkg-config
@@ -55,20 +56,20 @@ func main() {
panic(err)
}
// setup H264 -> raw frames decoder
frameDec := &h264Decoder{}
err = frameDec.initialize()
// setup H264 -> RGBA decoder
h264Dec := &h264Decoder{}
err = h264Dec.initialize()
if err != nil {
panic(err)
}
defer frameDec.close()
defer h264Dec.close()
// if SPS and PPS are present into the SDP, send them to the decoder
if forma.SPS != nil {
frameDec.decode(forma.SPS)
h264Dec.decode([][]byte{forma.SPS})
}
if forma.PPS != nil {
frameDec.decode(forma.PPS)
h264Dec.decode([][]byte{forma.PPS})
}
// setup a single media
@@ -77,6 +78,8 @@ func main() {
panic(err)
}
firstRandomAccess := false
// called when a RTP packet arrives
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
// decode timestamp
@@ -95,20 +98,25 @@ func main() {
return
}
for _, nalu := range au {
// convert NALUs into RGBA frames
img, err := frameDec.decode(nalu)
if err != nil {
panic(err)
}
// wait for a frame
if img == nil {
continue
}
log.Printf("decoded frame with PTS %v and size %v", pts, img.Bounds().Max)
// wait for a random access unit
if !firstRandomAccess && !h264.IsRandomAccess(au) {
log.Printf("waiting for a random access unit")
return
}
firstRandomAccess = true
// convert H264 access units into RGBA frames
img, err := h264Dec.decode(au)
if err != nil {
panic(err)
}
// wait for a frame
if img == nil {
return
}
log.Printf("decoded frame with PTS %v and size %v", pts, img.Bounds().Max)
})
// start playing