Skip to content

Commit

Permalink
fix: correct offset for insertion after start
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbee committed Apr 16, 2024
1 parent 6ceccc4 commit 15bc550
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- MPD Patch functionality with new `/patch_ttl` URL configuration
- nowDate query parameter as an alternative to nowMS for MPD and patch

### Fixed

Expand Down
15 changes: 12 additions & 3 deletions cmd/livesim2/app/handler_livesim.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,18 @@ func (s *Server) livesimHandlerFunc(w http.ResponseWriter, r *http.Request) {
return
}

nowDate := q.Get("nowDate")
if nowDate != "" {
nowMS, err = getMSFromDate(nowDate)
if err != nil {
http.Error(w, "bad date query", http.StatusBadRequest)
return
}
}

publishTime := q.Get("publishTime")
if publishTime != "" {
nowMS, err = getMSFromPublishTime(publishTime)
nowMS, err = getMSFromDate(publishTime)
if err != nil {
http.Error(w, "bad publishTime query", http.StatusBadRequest)
return
Expand Down Expand Up @@ -155,10 +164,10 @@ func getNowMS(nowMSValue string) (nowMS int, err error) {
return int(time.Now().UnixMilli()), nil
}

// getMSFromPublishTime returns a nowMS value based on publishTime (+1ms).
// getMSFromDate returns a nowMS value based on date (+1ms).
// The extra millisecond is there to ensure that the corresponding manifest
// can be generated
func getMSFromPublishTime(publishTimeValue string) (nowMS int, err error) {
func getMSFromDate(publishTimeValue string) (nowMS int, err error) {
t, err := time.Parse(time.RFC3339, publishTimeValue)
if err != nil {
return -1, err
Expand Down
22 changes: 13 additions & 9 deletions cmd/livesim2/app/handler_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@ func (r *rec) Header() http.Header {

// patchHandlerFunc returns an MPD patch
func (s *Server) patchHandlerFunc(w http.ResponseWriter, r *http.Request) {
origQuery := r.URL.RawQuery
q := r.URL.Query()
publishTime := q.Get("publishTime")
if publishTime == "" {
slog.Warn("publishTime query is required, but not provided in patch request")
http.Error(w, "publishTime query is required", http.StatusBadRequest)
}
old := &rec{}
origPath := r.URL.Path
origQuery := r.URL.RawQuery

newPath := strings.Replace(origPath, ".mpp", ".mpd", 1)
newPath = strings.TrimPrefix(newPath, "/patch")
r.URL.Path = newPath
newQuery := removeQuery(origQuery, "nowMS")
r.URL.RawQuery = newQuery
oldQuery := removeQuery(origQuery, "nowMS")
oldQuery = removeQuery(oldQuery, "nowDate")
mpdPath := mpdPathFromPatchPath(r.URL.Path)
r.URL.Path = mpdPath
r.URL.RawQuery = oldQuery
s.livesimHandlerFunc(old, r)

new := &rec{}
newQuery = removeQuery(origQuery, "publishTime")
newQuery := removeQuery(origQuery, "publishTime")
r.URL.RawQuery = newQuery
s.livesimHandlerFunc(new, r)

Expand All @@ -73,6 +71,12 @@ func (s *Server) patchHandlerFunc(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func mpdPathFromPatchPath(patchPath string) string {
mpdPath := strings.Replace(patchPath, ".mpp", ".mpd", 1)
//TODO. Handle a possible set prefix before patch
return strings.TrimPrefix(mpdPath, "/patch")
}

func removeQuery(query, key string) string {
q := strings.Split(query, "&")
for i, kv := range q {
Expand Down
33 changes: 31 additions & 2 deletions cmd/livesim2/app/handler_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

var wantedPatch = `<?xml version="1.0" encoding="UTF-8"?>
var wantedPatchSegTimelineTime = `<?xml version="1.0" encoding="UTF-8"?>
<Patch xmlns="urn:mpeg:dash:schema:mpd-patch:2020" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:dash:schema:mpd-patch:2020 DASH-MPD-PATCH.xsd" mpdId="base" originalPublishTime="2024-04-02T15:50:56Z" publishTime="2024-04-02T15:52:40Z">
<replace sel="/MPD/@publishTime">2024-04-02T15:52:40Z</replace>
<replace sel="/MPD/PatchLocation[1]">
Expand All @@ -27,6 +27,28 @@ var wantedPatch = `<?xml version="1.0" encoding="UTF-8"?>
</Patch>
`

var wantedPatchSegTimelineNumberWithAddAtEnd = `<?xml version="1.0" encoding="UTF-8"?>
<Patch xmlns="urn:mpeg:dash:schema:mpd-patch:2020" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:dash:schema:mpd-patch:2020 DASH-MPD-PATCH.xsd" mpdId="base" originalPublishTime="2024-04-16T07:34:38Z" publishTime="2024-04-16T07:34:56Z">
<replace sel="/MPD/@publishTime">2024-04-16T07:34:56Z</replace>
<replace sel="/MPD/PatchLocation[1]">
<PatchLocation ttl="60">/patch/livesim2/patch_60/segtimelinenr_1/testpic_2s/Manifest.mpp?publishTime=2024-04-16T07%3A34%3A56Z</PatchLocation>
</replace>
<replace sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;1&apos;]/SegmentTemplate/@startNumber">856626417</replace>
<remove sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;1&apos;]/SegmentTemplate/SegmentTimeline/S[1]"/>
<add sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;1&apos;]/SegmentTemplate/SegmentTimeline" pos="prepend">
<S t="82236136032256" d="96256" r="1"/>
</add>
<add sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;1&apos;]/SegmentTemplate/SegmentTimeline/S[15]" pos="after">
<S d="95232"/>
</add>
<replace sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;2&apos;]/SegmentTemplate/@startNumber">856626417</replace>
<remove sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;2&apos;]/SegmentTemplate/SegmentTimeline/S[1]"/>
<add sel="/MPD/Period[@id=&apos;P0&apos;]/AdaptationSet[@id=&apos;2&apos;]/SegmentTemplate/SegmentTimeline" pos="prepend">
<S t="154192755060000" d="180000" r="30"/>
</add>
</Patch>
`

func TestPatchHandler(t *testing.T) {
cfg := ServerConfig{
VodRoot: "testdata/assets",
Expand All @@ -51,7 +73,14 @@ func TestPatchHandler(t *testing.T) {
url: "/patch/livesim2/patch_60/segtimeline_1/testpic_2s/Manifest.mpp?publishTime=2024-04-02T15:50:56Z&nowMS=1712073160000",
wantedStatusCode: http.StatusOK,
wantedContentType: "application/dash-patch+xml",
wantedBody: wantedPatch,
wantedBody: wantedPatchSegTimelineTime,
},
{
desc: "segTimeline",
url: "/patch/livesim2/patch_60/segtimelinenr_1/testpic_2s/Manifest.mpp?publishTime=2024-04-16T07:34:38Z&nowDate=2024-04-16T07:34:56Z",
wantedStatusCode: http.StatusOK,
wantedContentType: "application/dash-patch+xml",
wantedBody: wantedPatchSegTimelineNumberWithAddAtEnd,
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func addLeafListChanges(patchRoot, old, new *etree.Element, elemPath string) err
e.CreateAttr("pos", "prepend")
e.AddChild(newElem.Copy())
} else {
addr := calcAddr(newElem, newPos)
addr := calcAddr(newElem, newPos-1)
e.CreateAttr("sel", fmt.Sprintf("%s/%s", elemPath, addr))
e.CreateAttr("pos", "after")
e.AddChild(newElem.Copy())
Expand Down

0 comments on commit 15bc550

Please sign in to comment.