-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from Dash-Industry-Forum/patch
MPD Patch support
- Loading branch information
Showing
24 changed files
with
1,412 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ denylist: | |
allowlist: | ||
- Apache-2.0 | ||
- MIT | ||
- BSD-2-Clause | ||
- BSD-3-Clause | ||
- CC0-1.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package app | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Dash-Industry-Forum/livesim2/pkg/patch" | ||
) | ||
|
||
type rec struct { | ||
body []byte | ||
status int | ||
} | ||
|
||
func (r *rec) Write(b []byte) (int, error) { | ||
r.body = append(r.body, b...) | ||
return len(b), nil | ||
} | ||
|
||
func (r *rec) WriteHeader(status int) { | ||
r.status = status | ||
} | ||
|
||
func (r *rec) Header() http.Header { | ||
return 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{} | ||
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") | ||
r.URL.RawQuery = newQuery | ||
s.livesimHandlerFunc(new, r) | ||
|
||
doc, err := patch.MPDDiff(old.body, new.body) | ||
switch { | ||
case errors.Is(err, patch.ErrPatchSamePublishTime): | ||
http.Error(w, err.Error(), http.StatusTooEarly) | ||
return | ||
case errors.Is(err, patch.ErrPatchTooLate): | ||
http.Error(w, err.Error(), http.StatusGone) | ||
return | ||
case err != nil: | ||
slog.Error("MPDDiff", "err", err) | ||
http.Error(w, "MPDDiff", http.StatusInternalServerError) | ||
return | ||
} | ||
doc.Indent(2) | ||
b, err := doc.WriteToBytes() | ||
if err != nil { | ||
slog.Error("WriteToBytes", "err", err) | ||
http.Error(w, "WriteToBytes", http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/dash-patch+xml") | ||
w.Header().Set("Content-Length", strconv.Itoa(len(b))) | ||
_, err = w.Write(b) | ||
if err != nil { | ||
slog.Error("Write", "err", err) | ||
} | ||
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 { | ||
if strings.HasPrefix(kv, key+"=") { | ||
q = append(q[:i], q[i+1:]...) | ||
break | ||
} | ||
} | ||
return strings.Join(q, "&") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/Dash-Industry-Forum/livesim2/pkg/logging" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
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]"> | ||
<PatchLocation ttl="60">/patch/livesim2/patch_60/segtimeline_1/testpic_2s/Manifest.mpp?publishTime=2024-04-02T15%3A52%3A40Z</PatchLocation> | ||
</replace> | ||
<remove sel="/MPD/Period[@id='P0']/AdaptationSet[@id='1']/SegmentTemplate/SegmentTimeline/S[1]"/> | ||
<add sel="/MPD/Period[@id='P0']/AdaptationSet[@id='1']/SegmentTemplate/SegmentTimeline" pos="prepend"> | ||
<S t="82179508704256" d="96256" r="1"/> | ||
</add> | ||
<remove sel="/MPD/Period[@id='P0']/AdaptationSet[@id='2']/SegmentTemplate/SegmentTimeline/S[1]"/> | ||
<add sel="/MPD/Period[@id='P0']/AdaptationSet[@id='2']/SegmentTemplate/SegmentTimeline" pos="prepend"> | ||
<S t="154086578820000" d="180000" r="30"/> | ||
</add> | ||
</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='P0']/AdaptationSet[@id='1']/SegmentTemplate/@startNumber">856626417</replace> | ||
<remove sel="/MPD/Period[@id='P0']/AdaptationSet[@id='1']/SegmentTemplate/SegmentTimeline/S[1]"/> | ||
<add sel="/MPD/Period[@id='P0']/AdaptationSet[@id='1']/SegmentTemplate/SegmentTimeline" pos="prepend"> | ||
<S t="82236136032256" d="96256" r="1"/> | ||
</add> | ||
<add sel="/MPD/Period[@id='P0']/AdaptationSet[@id='1']/SegmentTemplate/SegmentTimeline/S[15]" pos="after"> | ||
<S d="95232"/> | ||
</add> | ||
<replace sel="/MPD/Period[@id='P0']/AdaptationSet[@id='2']/SegmentTemplate/@startNumber">856626417</replace> | ||
<remove sel="/MPD/Period[@id='P0']/AdaptationSet[@id='2']/SegmentTemplate/SegmentTimeline/S[1]"/> | ||
<add sel="/MPD/Period[@id='P0']/AdaptationSet[@id='2']/SegmentTemplate/SegmentTimeline" pos="prepend"> | ||
<S t="154192755060000" d="180000" r="30"/> | ||
</add> | ||
</Patch> | ||
` | ||
|
||
func TestPatchHandler(t *testing.T) { | ||
cfg := ServerConfig{ | ||
VodRoot: "testdata/assets", | ||
TimeoutS: 0, | ||
LogFormat: logging.LogDiscard, | ||
} | ||
err := logging.InitSlog(cfg.LogLevel, cfg.LogFormat) | ||
require.NoError(t, err) | ||
server, err := SetupServer(context.Background(), &cfg) | ||
require.NoError(t, err) | ||
ts := httptest.NewServer(server.Router) | ||
defer ts.Close() | ||
testCases := []struct { | ||
desc string | ||
url string | ||
wantedStatusCode int | ||
wantedContentType string | ||
wantedBody string | ||
}{ | ||
{ | ||
desc: "segTimeline no update yet", | ||
url: "/patch/livesim2/patch_60/segtimeline_1/testpic_2s/Manifest.mpp?publishTime=2024-04-16T07:34:38Z&nowDate=2024-04-16T07:34:39Z", | ||
wantedStatusCode: http.StatusTooEarly, | ||
wantedContentType: "text/plain; charset=utf-8", | ||
wantedBody: "", | ||
}, | ||
{ | ||
desc: "segTimeline too late", | ||
url: "/patch/livesim2/patch_60/segtimeline_1/testpic_2s/Manifest.mpp?publishTime=2024-04-16T07:34:38Z&nowDate=2024-04-16T07:44:39Z", | ||
wantedStatusCode: http.StatusGone, | ||
wantedContentType: "text/plain; charset=utf-8", | ||
wantedBody: "", | ||
}, | ||
{ | ||
desc: "segTimeline", | ||
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: wantedPatchSegTimelineTime, | ||
}, | ||
{ | ||
desc: "segTimeline with Number", | ||
url: "/patch/livesim2/patch_60/segtimelinenr_1/testpic_2s/Manifest.mpp?publishTime=2024-04-16T07:34:38Z&nowDate=2024-04-16T07:34:57Z", | ||
wantedStatusCode: http.StatusOK, | ||
wantedContentType: "application/dash-patch+xml", | ||
wantedBody: wantedPatchSegTimelineNumberWithAddAtEnd, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
resp, body := testFullRequest(t, ts, "GET", tc.url, nil) | ||
require.Equal(t, tc.wantedStatusCode, resp.StatusCode) | ||
require.Equal(t, tc.wantedContentType, resp.Header.Get("Content-Type")) | ||
if tc.wantedStatusCode != http.StatusOK { | ||
return | ||
} | ||
bodyStr := string(body) | ||
require.Equal(t, tc.wantedBody, bodyStr) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.