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

@@ -1,3 +1,5 @@
//go:build cgo
package main
import (
@@ -6,14 +8,18 @@ import (
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpvp9"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpav1"
"github.com/bluenviron/mediacommon/v2/pkg/codecs/av1"
"github.com/pion/rtp"
)
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's a AV1 format
// 3. get access units of that format
// 3. decode the AV1 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
func main() {
c := gortsplib.Client{}
@@ -44,18 +50,28 @@ func main() {
panic("media not found")
}
// create decoder
// setup RTP -> AV1 decoder
rtpDec, err := forma.CreateDecoder()
if err != nil {
panic(err)
}
// setup AV1 -> RGBA decoder
av1Dec := &av1Decoder{}
err = av1Dec.initialize()
if err != nil {
panic(err)
}
defer av1Dec.close()
// setup a single media
_, err = c.Setup(desc.BaseURL, medi, 0, 0)
if err != nil {
panic(err)
}
firstRandomReceived := false
// called when a RTP packet arrives
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
// decode timestamp
@@ -68,13 +84,31 @@ func main() {
// extract AV1 temporal units from RTP packets
tu, err := rtpDec.Decode(pkt)
if err != nil {
if err != rtpvp9.ErrNonStartingPacketAndNoPrevious && err != rtpvp9.ErrMorePacketsNeeded {
if err != rtpav1.ErrNonStartingPacketAndNoPrevious && err != rtpav1.ErrMorePacketsNeeded {
log.Printf("ERR: %v", err)
}
return
}
log.Printf("received temporal unit with PTS %v and size %d\n", pts, len(tu))
// wait for a random access unit
if !firstRandomReceived && !av1.IsRandomAccess2(tu) {
log.Printf("waiting for a random access unit")
return
}
firstRandomReceived = true
// convert AV1 temporal units into RGBA frames
img, err := av1Dec.decode(tu)
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