Browse Source

hls: use fields for each flag values

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

8
internal/hls/fmp4/part_test.go

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

15
internal/hls/fmp4/part_track.go

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

2
internal/hls/muxer_variant_fmp4_part.go

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

11
internal/hls/muxer_variant_fmp4_segmenter.go

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

Loading…
Cancel
Save