Browse Source

hls: use fields for each flag values

pull/1219/head
aler9 3 years ago
parent
commit
a4363dfaa3
  1. 18
      internal/hls/fmp4/part.go
  2. 8
      internal/hls/fmp4/part_test.go
  3. 15
      internal/hls/fmp4/part_track.go
  4. 2
      internal/hls/muxer_variant_fmp4_part.go
  5. 11
      internal/hls/muxer_variant_fmp4_segmenter.go

18
internal/hls/fmp4/part.go

@ -13,6 +13,8 @@ const ( @@ -13,6 +13,8 @@ const (
trunFlagSampleSizePresent = 0x200
trunFlagSampleFlagsPresent = 0x400
trunFlagSampleCompositionTimeOffsetPresentOrV1 = 0x800
sampleFlagIsNonSyncSample = 1 << 16
)
// Part is a FMP4 part file.
@ -111,8 +113,8 @@ func (ps *Parts) Unmarshal(byts []byte) error { @@ -111,8 +113,8 @@ func (ps *Parts) Unmarshal(byts []byte) error {
}
trun := box.(*gomp4.Trun)
flags := uint16(trun.Flags[1])<<8 | uint16(trun.Flags[2])
if (flags & trunFlagDataOffsetPreset) == 0 {
trunFlags := uint16(trun.Flags[1])<<8 | uint16(trun.Flags[2])
if (trunFlags & trunFlagDataOffsetPreset) == 0 {
return nil, fmt.Errorf("unsupported flags")
}
@ -126,7 +128,7 @@ func (ps *Parts) Unmarshal(byts []byte) error { @@ -126,7 +128,7 @@ func (ps *Parts) Unmarshal(byts []byte) error {
for i, e := range trun.Entries {
s := &PartSample{}
if (flags & trunFlagSampleDurationPresent) != 0 {
if (trunFlags & trunFlagSampleDurationPresent) != 0 {
s.Duration = e.SampleDuration
} else {
s.Duration = tfhd.DefaultSampleDuration
@ -134,14 +136,16 @@ func (ps *Parts) Unmarshal(byts []byte) error { @@ -134,14 +136,16 @@ func (ps *Parts) Unmarshal(byts []byte) error {
s.PTSOffset = e.SampleCompositionTimeOffsetV1
if (flags & trunFlagSampleFlagsPresent) != 0 {
s.Flags = e.SampleFlags
var sampleFlags uint32
if (trunFlags & trunFlagSampleFlagsPresent) != 0 {
sampleFlags = e.SampleFlags
} else {
s.Flags = tfhd.DefaultSampleFlags
sampleFlags = tfhd.DefaultSampleFlags
}
s.IsNonSyncSample = ((sampleFlags & sampleFlagIsNonSyncSample) != 0)
var size uint32
if (flags & trunFlagSampleSizePresent) != 0 {
if (trunFlags & trunFlagSampleSizePresent) != 0 {
size = e.SampleSize
} else {
size = tfhd.DefaultSampleSize

8
internal/hls/fmp4/part_test.go

@ -37,7 +37,7 @@ func TestPartMarshal(t *testing.T) { @@ -37,7 +37,7 @@ func TestPartMarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
{
Duration: 1 * 90000,
@ -45,7 +45,7 @@ func TestPartMarshal(t *testing.T) { @@ -45,7 +45,7 @@ func TestPartMarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
}
@ -214,7 +214,7 @@ func TestPartUnmarshal(t *testing.T) { @@ -214,7 +214,7 @@ func TestPartUnmarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
{
Duration: 1 * 90000,
@ -222,7 +222,7 @@ func TestPartUnmarshal(t *testing.T) { @@ -222,7 +222,7 @@ func TestPartUnmarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
},
},

15
internal/hls/fmp4/part_track.go

@ -6,10 +6,10 @@ import ( @@ -6,10 +6,10 @@ import (
// PartSample is a sample of a PartTrack.
type PartSample struct {
Duration uint32
PTSOffset int32
Flags uint32
Payload []byte
Duration uint32
PTSOffset int32
IsNonSyncSample bool
Payload []byte
}
// PartTrack is a track of Part.
@ -78,10 +78,15 @@ func (pt *PartTrack) marshal(w *mp4Writer) (*gomp4.Trun, int, error) { @@ -78,10 +78,15 @@ func (pt *PartTrack) marshal(w *mp4Writer) (*gomp4.Trun, int, error) {
for _, sample := range pt.Samples {
if pt.IsVideo {
var flags uint32
if sample.IsNonSyncSample {
flags |= sampleFlagIsNonSyncSample
}
trun.Entries = append(trun.Entries, gomp4.TrunEntry{
SampleDuration: sample.Duration,
SampleSize: uint32(len(sample.Payload)),
SampleFlags: sample.Flags,
SampleFlags: flags,
SampleCompositionTimeOffsetV1: sample.PTSOffset,
})
} else {

2
internal/hls/muxer_variant_fmp4_part.go

@ -123,7 +123,7 @@ func (p *muxerVariantFMP4Part) writeH264(sample *augmentedVideoSample) { @@ -123,7 +123,7 @@ func (p *muxerVariantFMP4Part) writeH264(sample *augmentedVideoSample) {
p.videoStartDTS = sample.dts
}
if (sample.Flags & (1 << 16)) == 0 {
if !sample.IsNonSyncSample {
p.isIndependent = true
}

11
internal/hls/muxer_variant_fmp4_segmenter.go

@ -203,16 +203,11 @@ func (m *muxerVariantFMP4Segmenter) writeH264Entry( @@ -203,16 +203,11 @@ func (m *muxerVariantFMP4Segmenter) writeH264Entry(
return err
}
var flags uint32
if !idrPresent {
flags |= 1 << 16
}
sample := &augmentedVideoSample{
PartSample: fmp4.PartSample{
PTSOffset: int32(durationGoToMp4(pts-dts, 90000)),
Flags: flags,
Payload: avcc,
PTSOffset: int32(durationGoToMp4(pts-dts, 90000)),
IsNonSyncSample: !idrPresent,
Payload: avcc,
},
dts: dts,
}

Loading…
Cancel
Save