fmp4: move avcc encoding into writer

This commit is contained in:
aler9 2022-08-27 12:11:00 +02:00
parent 6793920d36
commit b5dd658d29
4 changed files with 20 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import (
gomp4 "github.com/abema/go-mp4"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/h264"
)
func durationGoToMp4(v time.Duration, timescale time.Duration) int64 {
@ -77,7 +78,7 @@ func generatePartVideoTraf(
trun.Entries = append(trun.Entries, gomp4.TrunEntry{
SampleDuration: uint32(durationGoToMp4(e.Duration(), videoTimescale)),
SampleSize: uint32(len(e.AVCC)),
SampleSize: uint32(len(e.avcc)),
SampleFlags: flags,
SampleCompositionTimeOffsetV1: int32(durationGoToMp4(off, videoTimescale)),
})
@ -208,6 +209,14 @@ func GeneratePart(
var videoTrun *gomp4.Trun
var videoTrunOffset int
if videoTrack != nil {
for _, e := range videoSamples {
var err error
e.avcc, err = h264.AVCCMarshal(e.NALUs)
if err != nil {
return nil, err
}
}
var err error
videoTrun, videoTrunOffset, err = generatePartVideoTraf(
w, trackID, videoSamples)
@ -240,7 +249,7 @@ func GeneratePart(
if videoTrack != nil {
for _, e := range videoSamples {
dataSize += len(e.AVCC)
dataSize += len(e.avcc)
}
videoDataSize = dataSize
}
@ -256,7 +265,7 @@ func GeneratePart(
if videoTrack != nil {
for _, e := range videoSamples {
pos += copy(mdat.Data[pos:], e.AVCC)
pos += copy(mdat.Data[pos:], e.avcc)
}
}

View File

@ -13,9 +13,10 @@ type VideoSample struct {
NALUs [][]byte
PTS time.Duration
DTS time.Duration
AVCC []byte
IDRPresent bool
Next *VideoSample
avcc []byte
}
// Duration returns the sample duration.

View File

@ -131,16 +131,17 @@ func (s *muxerVariantFMP4Segment) finalize(
}
func (s *muxerVariantFMP4Segment) writeH264(sample *fmp4.VideoSample, adjustedPartDuration time.Duration) error {
size := uint64(len(sample.AVCC))
size := uint64(0)
for _, nalu := range sample.NALUs {
size += uint64(len(nalu))
}
if (s.size + size) > s.segmentMaxSize {
return fmt.Errorf("reached maximum segment size")
}
s.size += size
s.currentPart.writeH264(sample)
s.size += size
// switch part
if s.lowLatency &&
s.currentPart.duration() >= adjustedPartDuration {
@ -164,15 +165,13 @@ func (s *muxerVariantFMP4Segment) writeH264(sample *fmp4.VideoSample, adjustedPa
func (s *muxerVariantFMP4Segment) writeAAC(sample *fmp4.AudioSample, adjustedPartDuration time.Duration) error {
size := uint64(len(sample.AU))
if (s.size + size) > s.segmentMaxSize {
return fmt.Errorf("reached maximum segment size")
}
s.size += size
s.currentPart.writeAAC(sample)
s.size += size
// switch part
if s.lowLatency && s.videoTrack == nil &&
s.currentPart.duration() >= adjustedPartDuration {

View File

@ -141,15 +141,9 @@ func (m *muxerVariantFMP4Segmenter) writeH264(pts time.Duration, nalus [][]byte)
return nil
}
avcc, err := h264.AVCCMarshal(nalus)
if err != nil {
return err
}
return m.writeH264Entry(&fmp4.VideoSample{
PTS: pts,
NALUs: nalus,
AVCC: avcc,
IDRPresent: idrPresent,
})
}