diff --git a/internal/hls/fmp4/part.go b/internal/hls/fmp4/part.go index 7ff3de59..d82facdf 100644 --- a/internal/hls/fmp4/part.go +++ b/internal/hls/fmp4/part.go @@ -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) } } diff --git a/internal/hls/fmp4/videosample.go b/internal/hls/fmp4/videosample.go index a28e0205..4f7416da 100644 --- a/internal/hls/fmp4/videosample.go +++ b/internal/hls/fmp4/videosample.go @@ -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. diff --git a/internal/hls/muxer_variant_fmp4_segment.go b/internal/hls/muxer_variant_fmp4_segment.go index c53baa63..ebfa8569 100644 --- a/internal/hls/muxer_variant_fmp4_segment.go +++ b/internal/hls/muxer_variant_fmp4_segment.go @@ -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 { diff --git a/internal/hls/muxer_variant_fmp4_segmenter.go b/internal/hls/muxer_variant_fmp4_segmenter.go index a241ba76..4f809f46 100644 --- a/internal/hls/muxer_variant_fmp4_segmenter.go +++ b/internal/hls/muxer_variant_fmp4_segmenter.go @@ -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, }) }