Skip to content

Commit

Permalink
hls: use fields for each flag values
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Nov 2, 2022
1 parent f0514b3 commit a4363df
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
18 changes: 11 additions & 7 deletions internal/hls/fmp4/part.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
trunFlagSampleSizePresent = 0x200
trunFlagSampleFlagsPresent = 0x400
trunFlagSampleCompositionTimeOffsetPresentOrV1 = 0x800

sampleFlagIsNonSyncSample = 1 << 16
)

// Part is a FMP4 part file.
Expand Down Expand Up @@ -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")
}

Expand All @@ -126,22 +128,24 @@ 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
}

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
Expand Down
8 changes: 4 additions & 4 deletions internal/hls/fmp4/part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func TestPartMarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
{
Duration: 1 * 90000,
Payload: []byte{
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
}

Expand Down Expand Up @@ -214,15 +214,15 @@ func TestPartUnmarshal(t *testing.T) {
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
{
Duration: 1 * 90000,
Payload: []byte{
0x00, 0x00, 0x00, 0x01,
0x01, // non-IDR
},
Flags: 1 << 16,
IsNonSyncSample: true,
},
},
},
Expand Down
15 changes: 10 additions & 5 deletions internal/hls/fmp4/part_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/hls/muxer_variant_fmp4_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 3 additions & 8 deletions internal/hls/muxer_variant_fmp4_segmenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit a4363df

Please sign in to comment.