improve H264/H265 examples (#355) (#375) (#398)

This commit is contained in:
Alessandro Ros
2023-09-02 14:49:50 +02:00
committed by GitHub
parent 67af5e3840
commit 4f31866b9c
16 changed files with 757 additions and 44 deletions

View File

@@ -12,16 +12,17 @@ import (
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtph264"
"github.com/bluenviron/gortsplib/v4/pkg/url"
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/pion/rtp"
)
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's a H264 media
// 3. decode H264 into RGBA frames
// 4. encode the frames into JPEG images and save them on disk
// 2. check if there's a H264 media stream
// 3. decode the H264 media stream into RGBA frames
// 4. convert frames to JPEG images and save them on disk
// This example requires the ffmpeg libraries, that can be installed in this way:
// This example requires the FFmpeg libraries, that can be installed with this command:
// apt install -y libavformat-dev libswscale-dev gcc pkg-config
func saveToFile(img image.Image) error {
@@ -77,18 +78,18 @@ func main() {
}
// setup H264 -> raw frames decoder
h264RawDec, err := newH264Decoder()
frameDec, err := newH264Decoder()
if err != nil {
panic(err)
}
defer h264RawDec.close()
defer frameDec.close()
// if SPS and PPS are present into the SDP, send them to the decoder
if forma.SPS != nil {
h264RawDec.decode(forma.SPS)
frameDec.decode(forma.SPS)
}
if forma.PPS != nil {
h264RawDec.decode(forma.PPS)
frameDec.decode(forma.PPS)
}
// setup a single media
@@ -97,6 +98,7 @@ func main() {
panic(err)
}
iframeReceived := false
saveCount := 0
// called when a RTP packet arrives
@@ -110,9 +112,18 @@ func main() {
return
}
// wait for an I-frame
if !iframeReceived {
if !h264.IDRPresent(au) {
log.Printf("waiting for an I-frame")
return
}
iframeReceived = true
}
for _, nalu := range au {
// convert NALUs into RGBA frames
img, err := h264RawDec.decode(nalu)
img, err := frameDec.decode(nalu)
if err != nil {
panic(err)
}