Skip to content

Commit

Permalink
Merge pull request #158 from Dash-Industry-Forum/fix-mehd-removal
Browse files Browse the repository at this point in the history
fix: remove mehd box from init segments
  • Loading branch information
tobbee authored Feb 13, 2024
2 parents a6fba5f + f158914 commit 0557d4f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make HTTP OPTIONS method work for all URLs
- Make --playurl work for general paths
- Derive and insert contentType if missing
- Remove any mehd box from init segment

## [1.1.1] - 2024-01-19

Expand Down
10 changes: 9 additions & 1 deletion cmd/livesim2/app/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,15 @@ func getInitSeg(data []byte) (*mp4.InitSegment, error) {
if err != nil {
return nil, fmt.Errorf("decode init: %w", err)
}
return initFile.Init, nil
initSeg := initFile.Init
if initSeg == nil {
return nil, fmt.Errorf("no init segment found")
}
err = initSeg.TweakSingleTrakLive()
if err != nil {
return nil, fmt.Errorf("tweak single trak live: %w", err)
}
return initSeg, nil
}

func getInitBytes(initSeg *mp4.InitSegment) ([]byte, error) {
Expand Down
49 changes: 34 additions & 15 deletions cmd/livesim2/app/livesegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,37 +253,56 @@ func findSegMetaFromNr(a *asset, rep *RepData, nr uint32, cfg *ResponseConfig, n
}, nil
}

type initMatch struct {
isInit bool
init []byte
rep *RepData
}

func writeInitSegment(w http.ResponseWriter, cfg *ResponseConfig, vodFS fs.FS, a *asset, segmentPart string) (isInit bool, err error) {
isTimeSubsInit, err := writeTimeSubsInitSegment(w, cfg, a, segmentPart)
if isTimeSubsInit {
return true, err
}
match, err := matchInit(segmentPart, cfg, a)
if err != nil {
return false, fmt.Errorf("getInitBytes: %w", err)
}
if !match.isInit {
return false, nil
}

w.Header().Set("Content-Length", strconv.Itoa(len(match.init)))
w.Header().Set("Content-Type", match.rep.SegmentType())
_, err = w.Write(match.init)
if err != nil {
slog.Error("writing response", "error", err)
return false, err
}
return true, nil
}

func matchInit(segmentPart string, cfg *ResponseConfig, a *asset) (initMatch, error) {
var im initMatch
for _, rep := range a.Reps {
if segmentPart == rep.InitURI {
var initBytes []byte
initBytes = rep.initBytes
im.init = rep.initBytes
if rep.encData != nil && cfg.DashIFECCP != "" {
switch cfg.DashIFECCP {
case "cbcs":
initBytes = rep.encData.cbcsInitBytes
im.init = rep.encData.cbcsInitBytes
case "cenc":
initBytes = rep.encData.cencInitBytes
im.init = rep.encData.cencInitBytes
default:
return false, fmt.Errorf("unknown DashIFECCP %s", cfg.DashIFECCP)
return im, fmt.Errorf("unknown DashIFECCP %s", cfg.DashIFECCP)
}
}
w.Header().Set("Content-Length", strconv.Itoa(len(initBytes)))

w.Header().Set("Content-Type", rep.SegmentType())
_, err := w.Write(initBytes)
if err != nil {
slog.Error("writing response", "error", err)
return false, err
}
return true, nil
im.rep = rep
im.isInit = true
return im, nil
}
}
return false, nil
return im, nil
}

func writeLiveSegment(w http.ResponseWriter, cfg *ResponseConfig, vodFS fs.FS, a *asset, segmentPart string, nowMS int, tt *template.Template) error {
Expand Down
19 changes: 19 additions & 0 deletions cmd/livesim2/app/livesegment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,22 @@ func TestSegmentStatusCodeResponse(t *testing.T) {
})
}
}

func TestMehdBoxRemovedFromInitSegment(t *testing.T) {
vodFS := os.DirFS("testdata/assets")
am := newAssetMgr(vodFS, "", false)
err := am.discoverAssets()
require.NoError(t, err)
asset, ok := am.findAsset("testpic_8s")
require.True(t, ok)
cfg := NewResponseConfig()
initV300 := "V300/init.mp4"
match, err := matchInit(initV300, cfg, asset)
require.NoError(t, err)
sr := bits.NewFixedSliceReader(match.init)
mp4File, err := mp4.DecodeFileSR(sr)
require.NoError(t, err)
initSeg := mp4File.Init
require.NotNil(t, initSeg)
require.Nil(t, initSeg.Moov.Mvex.Mehd)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.1
require (
github.com/Comcast/gots v1.0.4
github.com/Eyevinn/dash-mpd v0.11.1
github.com/Eyevinn/mp4ff v0.42.0
github.com/Eyevinn/mp4ff v0.42.1-0.20240212112335-795b1455e438
github.com/caddyserver/certmagic v0.20.0
github.com/dusted-go/logging v1.1.3
github.com/go-chi/chi/v5 v5.0.11
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/Comcast/gots v1.0.4 h1:OBxaTTM2kEjZZ9QTYsEVIUkaY3hAmy4spJOjpcgJk+A=
github.com/Comcast/gots v1.0.4/go.mod h1:/EieJXsI8HTyAAGPoardzq/OY+jL+RiyG+sq0cZgkyk=
github.com/Eyevinn/dash-mpd v0.11.1 h1:p9r31p7+YNp9E548m+sLIdohvQhQt+TmLoOxU8eGqns=
github.com/Eyevinn/dash-mpd v0.11.1/go.mod h1:loc8wzf1XW4NIWI4M7U6TAPk+bx2H8wGpjFTvxepmcI=
github.com/Eyevinn/mp4ff v0.42.0 h1:I85b/EDTkP77GsoBL8nRV6sfFKZhAXoP6oJHU8fv6kM=
github.com/Eyevinn/mp4ff v0.42.0/go.mod h1:w/6GSa5ghZ1VavzJK6McQ2/flx8mKtcrKDr11SsEweA=
github.com/Eyevinn/mp4ff v0.42.1-0.20240212112335-795b1455e438 h1:TojV4JliMS3FLLeDrcCPe+097UQ3EfyqqIPO4VAGsr4=
github.com/Eyevinn/mp4ff v0.42.1-0.20240212112335-795b1455e438/go.mod h1:w/6GSa5ghZ1VavzJK6McQ2/flx8mKtcrKDr11SsEweA=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down

0 comments on commit 0557d4f

Please sign in to comment.