mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-26 00:22:16 +00:00
fmp4: move avcc encoding into writer
This commit is contained in:
parent
6793920d36
commit
b5dd658d29
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
gomp4 "github.com/abema/go-mp4"
|
gomp4 "github.com/abema/go-mp4"
|
||||||
"github.com/aler9/gortsplib"
|
"github.com/aler9/gortsplib"
|
||||||
|
"github.com/aler9/gortsplib/pkg/h264"
|
||||||
)
|
)
|
||||||
|
|
||||||
func durationGoToMp4(v time.Duration, timescale time.Duration) int64 {
|
func durationGoToMp4(v time.Duration, timescale time.Duration) int64 {
|
||||||
@ -77,7 +78,7 @@ func generatePartVideoTraf(
|
|||||||
|
|
||||||
trun.Entries = append(trun.Entries, gomp4.TrunEntry{
|
trun.Entries = append(trun.Entries, gomp4.TrunEntry{
|
||||||
SampleDuration: uint32(durationGoToMp4(e.Duration(), videoTimescale)),
|
SampleDuration: uint32(durationGoToMp4(e.Duration(), videoTimescale)),
|
||||||
SampleSize: uint32(len(e.AVCC)),
|
SampleSize: uint32(len(e.avcc)),
|
||||||
SampleFlags: flags,
|
SampleFlags: flags,
|
||||||
SampleCompositionTimeOffsetV1: int32(durationGoToMp4(off, videoTimescale)),
|
SampleCompositionTimeOffsetV1: int32(durationGoToMp4(off, videoTimescale)),
|
||||||
})
|
})
|
||||||
@ -208,6 +209,14 @@ func GeneratePart(
|
|||||||
var videoTrun *gomp4.Trun
|
var videoTrun *gomp4.Trun
|
||||||
var videoTrunOffset int
|
var videoTrunOffset int
|
||||||
if videoTrack != nil {
|
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
|
var err error
|
||||||
videoTrun, videoTrunOffset, err = generatePartVideoTraf(
|
videoTrun, videoTrunOffset, err = generatePartVideoTraf(
|
||||||
w, trackID, videoSamples)
|
w, trackID, videoSamples)
|
||||||
@ -240,7 +249,7 @@ func GeneratePart(
|
|||||||
|
|
||||||
if videoTrack != nil {
|
if videoTrack != nil {
|
||||||
for _, e := range videoSamples {
|
for _, e := range videoSamples {
|
||||||
dataSize += len(e.AVCC)
|
dataSize += len(e.avcc)
|
||||||
}
|
}
|
||||||
videoDataSize = dataSize
|
videoDataSize = dataSize
|
||||||
}
|
}
|
||||||
@ -256,7 +265,7 @@ func GeneratePart(
|
|||||||
|
|
||||||
if videoTrack != nil {
|
if videoTrack != nil {
|
||||||
for _, e := range videoSamples {
|
for _, e := range videoSamples {
|
||||||
pos += copy(mdat.Data[pos:], e.AVCC)
|
pos += copy(mdat.Data[pos:], e.avcc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,9 +13,10 @@ type VideoSample struct {
|
|||||||
NALUs [][]byte
|
NALUs [][]byte
|
||||||
PTS time.Duration
|
PTS time.Duration
|
||||||
DTS time.Duration
|
DTS time.Duration
|
||||||
AVCC []byte
|
|
||||||
IDRPresent bool
|
IDRPresent bool
|
||||||
Next *VideoSample
|
Next *VideoSample
|
||||||
|
|
||||||
|
avcc []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duration returns the sample duration.
|
// Duration returns the sample duration.
|
||||||
|
@ -131,16 +131,17 @@ func (s *muxerVariantFMP4Segment) finalize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *muxerVariantFMP4Segment) writeH264(sample *fmp4.VideoSample, adjustedPartDuration time.Duration) error {
|
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 {
|
if (s.size + size) > s.segmentMaxSize {
|
||||||
return fmt.Errorf("reached maximum segment size")
|
return fmt.Errorf("reached maximum segment size")
|
||||||
}
|
}
|
||||||
|
s.size += size
|
||||||
|
|
||||||
s.currentPart.writeH264(sample)
|
s.currentPart.writeH264(sample)
|
||||||
|
|
||||||
s.size += size
|
|
||||||
|
|
||||||
// switch part
|
// switch part
|
||||||
if s.lowLatency &&
|
if s.lowLatency &&
|
||||||
s.currentPart.duration() >= adjustedPartDuration {
|
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 {
|
func (s *muxerVariantFMP4Segment) writeAAC(sample *fmp4.AudioSample, adjustedPartDuration time.Duration) error {
|
||||||
size := uint64(len(sample.AU))
|
size := uint64(len(sample.AU))
|
||||||
|
|
||||||
if (s.size + size) > s.segmentMaxSize {
|
if (s.size + size) > s.segmentMaxSize {
|
||||||
return fmt.Errorf("reached maximum segment size")
|
return fmt.Errorf("reached maximum segment size")
|
||||||
}
|
}
|
||||||
|
s.size += size
|
||||||
|
|
||||||
s.currentPart.writeAAC(sample)
|
s.currentPart.writeAAC(sample)
|
||||||
|
|
||||||
s.size += size
|
|
||||||
|
|
||||||
// switch part
|
// switch part
|
||||||
if s.lowLatency && s.videoTrack == nil &&
|
if s.lowLatency && s.videoTrack == nil &&
|
||||||
s.currentPart.duration() >= adjustedPartDuration {
|
s.currentPart.duration() >= adjustedPartDuration {
|
||||||
|
@ -141,15 +141,9 @@ func (m *muxerVariantFMP4Segmenter) writeH264(pts time.Duration, nalus [][]byte)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
avcc, err := h264.AVCCMarshal(nalus)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.writeH264Entry(&fmp4.VideoSample{
|
return m.writeH264Entry(&fmp4.VideoSample{
|
||||||
PTS: pts,
|
PTS: pts,
|
||||||
NALUs: nalus,
|
NALUs: nalus,
|
||||||
AVCC: avcc,
|
|
||||||
IDRPresent: idrPresent,
|
IDRPresent: idrPresent,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user