Browse Source

fmp4: move avcc encoding into writer

pull/1128/head
aler9 3 years ago
parent
commit
b5dd658d29
  1. 15
      internal/hls/fmp4/part.go
  2. 3
      internal/hls/fmp4/videosample.go
  3. 13
      internal/hls/muxer_variant_fmp4_segment.go
  4. 6
      internal/hls/muxer_variant_fmp4_segmenter.go

15
internal/hls/fmp4/part.go

@ -6,6 +6,7 @@ import ( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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)
}
}

3
internal/hls/fmp4/videosample.go

@ -13,9 +13,10 @@ type VideoSample struct { @@ -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.

13
internal/hls/muxer_variant_fmp4_segment.go

@ -131,16 +131,17 @@ func (s *muxerVariantFMP4Segment) finalize( @@ -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 @@ -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 {

6
internal/hls/muxer_variant_fmp4_segmenter.go

@ -141,15 +141,9 @@ func (m *muxerVariantFMP4Segmenter) writeH264(pts time.Duration, nalus [][]byte) @@ -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,
})
}

Loading…
Cancel
Save