add new example client-read-format-mpeg4audio-save-to-disk (#235)

This commit is contained in:
Alessandro Ros
2023-04-09 19:18:28 +02:00
committed by GitHub
parent b6727c07ef
commit c0e3ba2a8d
9 changed files with 207 additions and 31 deletions

View File

@@ -13,7 +13,7 @@ import (
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's a H264 media
// 3. save the content of the H264 media into a file in MPEG-TS format
// 3. save the content of the media into a file in MPEG-TS format
func main() {
c := gortsplib.Client{}
@@ -44,10 +44,10 @@ func main() {
panic("media not found")
}
// setup RTP/H264->H264 decoder
// setup RTP/H264 -> H264 decoder
rtpDec := forma.CreateDecoder()
// setup H264->MPEGTS muxer
// setup H264 -> MPEG-TS muxer
mpegtsMuxer, err := newMPEGTSMuxer(forma.SPS, forma.PPS)
if err != nil {
panic(err)
@@ -61,9 +61,9 @@ func main() {
// called when a RTP packet arrives
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
// extract NALUs from RTP packets
// extract access unit from RTP packets
// DecodeUntilMarker is necessary for the DTS extractor to work
nalus, pts, err := rtpDec.DecodeUntilMarker(pkt)
au, pts, err := rtpDec.DecodeUntilMarker(pkt)
if err != nil {
if err != rtph264.ErrNonStartingPacketAndNoPrevious && err != rtph264.ErrMorePacketsNeeded {
log.Printf("ERR: %v", err)
@@ -71,8 +71,8 @@ func main() {
return
}
// encode H264 NALUs into MPEG-TS
mpegtsMuxer.encode(nalus, pts)
// encode the access unit into MPEG-TS
mpegtsMuxer.encode(au, pts)
})
// start playing

View File

@@ -54,8 +54,8 @@ func (e *mpegtsMuxer) close() {
e.f.Close()
}
// encode encodes H264 NALUs into MPEG-TS.
func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
// encode encodes a H264 access unit into MPEG-TS.
func (e *mpegtsMuxer) encode(au [][]byte, pts time.Duration) error {
// prepend an AUD. This is required by some players
filteredNALUs := [][]byte{
{byte(h264.NALUTypeAccessUnitDelimiter), 240},
@@ -64,7 +64,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
nonIDRPresent := false
idrPresent := false
for _, nalu := range nalus {
for _, nalu := range au {
typ := h264.NALUType(nalu[0] & 0x1F)
switch typ {
case h264.NALUTypeSPS:
@@ -88,7 +88,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
filteredNALUs = append(filteredNALUs, nalu)
}
nalus = filteredNALUs
au = filteredNALUs
if !nonIDRPresent && !idrPresent {
return nil
@@ -96,7 +96,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
// add SPS and PPS before every group that contains an IDR
if idrPresent {
nalus = append([][]byte{e.sps, e.pps}, nalus...)
au = append([][]byte{e.sps, e.pps}, au...)
}
var dts time.Duration
@@ -111,7 +111,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
e.dtsExtractor = h264.NewDTSExtractor()
var err error
dts, err = e.dtsExtractor.Extract(nalus, pts)
dts, err = e.dtsExtractor.Extract(au, pts)
if err != nil {
return err
}
@@ -122,7 +122,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
} else {
var err error
dts, err = e.dtsExtractor.Extract(nalus, pts)
dts, err = e.dtsExtractor.Extract(au, pts)
if err != nil {
return err
}
@@ -145,7 +145,7 @@ func (e *mpegtsMuxer) encode(nalus [][]byte, pts time.Duration) error {
}
// encode into Annex-B
annexb, err := h264.AnnexBMarshal(nalus)
annexb, err := h264.AnnexBMarshal(au)
if err != nil {
return err
}