update VP8/VP9 examples to decode/encode frames (#704)

This commit is contained in:
Alessandro Ros
2025-02-19 22:45:31 +01:00
committed by GitHub
parent a17e1f776e
commit 3829fef787
29 changed files with 1513 additions and 195 deletions

View File

@@ -1,3 +1,5 @@
//go:build cgo
package main
import (
@@ -12,8 +14,11 @@ import (
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's a VP9 format
// 3. get access units of that format
// 2. check if there's an VP9 format
// 3. decode the VP9 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,12 +49,20 @@ func main() {
panic("media not found")
}
// create decoder
// setup RTP -> VP9 decoder
rtpDec, err := forma.CreateDecoder()
if err != nil {
panic(err)
}
// setup VP9 -> RGBA decoder
vp9Dec := &vp9Decoder{}
err = vp9Dec.initialize()
if err != nil {
panic(err)
}
defer vp9Dec.close()
// setup a single media
_, err = c.Setup(desc.BaseURL, medi, 0, 0)
if err != nil {
@@ -65,8 +78,8 @@ func main() {
return
}
// extract VP9 frames from RTP packets
vf, err := rtpDec.Decode(pkt)
// extract access units from RTP packets
au, err := rtpDec.Decode(pkt)
if err != nil {
if err != rtpvp9.ErrNonStartingPacketAndNoPrevious && err != rtpvp9.ErrMorePacketsNeeded {
log.Printf("ERR: %v", err)
@@ -74,7 +87,18 @@ func main() {
return
}
log.Printf("received frame with PTS %v and size %d\n", pts, len(vf))
// convert VP9 access units into RGBA frames
img, err := vp9Dec.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