Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: InitSegment.TweakSingleTrakLive to tweak init segments to #323

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet
### Added

- InitSegment.TweakSingleTrakLive changes an init segment to fit live streaming

## [0.42.0] - 2024-01-26

Expand Down
23 changes: 23 additions & 0 deletions mp4/initsegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,29 @@ func (s *InitSegment) GetMediaType() string {
}
}

// TweakSingleTrakLive assures that there is only one track and removes any mehd box.
func (s *InitSegment) TweakSingleTrakLive() error {
if len(s.Moov.Traks) != 1 {
return fmt.Errorf("only one track allowed for live")
}
mvex := s.Moov.Mvex
if mvex == nil {
return fmt.Errorf("no mvex box found")

}
mehd := mvex.Mehd
if mehd != nil {
for i, c := range mvex.Children {
if c == mehd {
mvex.Children = append(mvex.Children[:i], mvex.Children[i+1:]...)
mvex.Mehd = nil
break
}
}
}
return nil
}

// SetAACDescriptor - Modify a TrakBox by adding AAC SampleDescriptor
// objType is one of AAClc, HEAACv1, HEAACv2
// For HEAAC, the samplingFrequency is the base frequency (normally 24000)
Expand Down
34 changes: 34 additions & 0 deletions mp4/initsegment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,37 @@ func TestGenerateInitSegment(t *testing.T) {
t.Errorf("Generated init segment different from %s", goldenAssetPath)
}
}

func TestInitTweakSingleTrakLive(t *testing.T) {
// Check that mehd box is removed and that multiple tracks is not allowed.
goldenAssetPath := "testdata/golden_init_video.mp4"
r, err := os.Open(goldenAssetPath)
if err != nil {
t.Error(err)
}
f, err := DecodeFile(r)
if err != nil {
t.Error(err)
}
moov := f.Init.Moov
mvex := moov.Mvex
mehd := MehdBox{
FragmentDuration: 1000,
}
mvex.AddChild(&mehd)
err = f.Init.TweakSingleTrakLive()
if err != nil {
t.Error(err)
}
if mvex.Mehd != nil {
t.Errorf("Mehd not removed")
}
f.Init.AddEmptyTrack(180000, "video", "und")
err = f.Init.TweakSingleTrakLive()
wantedErrMsg := "only one track allowed for live"
if err == nil {
t.Errorf("Did not get error")
} else if err.Error() != wantedErrMsg {
t.Errorf(`Did not get error %q but %q"`, wantedErrMsg, err)
}
}
Loading