Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ssadovski007/bb2b64b7ec3916569b0c216f11650198 to your computer and use it in GitHub Desktop.
Save ssadovski007/bb2b64b7ec3916569b0c216f11650198 to your computer and use it in GitHub Desktop.
// rule for AnyProxy tool (http://anyproxy.io/en/)
// support https://github.com/google/shaka-player/issues/1884 investigation
const moment = require('moment')
const parser = require('xml2json')
module.exports = {
summary: "manifest-period@start-resets-from-PT0S.js",
*beforeSendResponse(requestDetail, responseDetail) {
if (requestDetail.url.includes('.mpd')
&& requestDetail.requestOptions.method === "GET"
&& responseDetail.response.header['Content-Type'] === "application/dash+xml"
&& responseDetail.response.statusCode === 200) {
let newResponse = Object.assign({}, responseDetail.response)
let manifestJson = JSON.parse(parser.toJson(newResponse.body.toString(), {reversible: true})),
rootMPD = manifestJson['MPD'],
periods = [rootMPD.Period].flat()
if (rootMPD.type === "static"
&& rootMPD.profiles === "urn:mpeg:dash:profile:isoff-live:2011"
&& periods[0]) {
let firstPeriodStart = moment.duration(periods[0].start || "PT0S")
periods.forEach(p => {
p.start = moment.duration(p.start).subtract(firstPeriodStart).toString()
})
newResponse.header['X-Proxy-By'] = `AnyProxy: ${this.summary}`
newResponse.body = parser.toXml(manifestJson)
}
return {
response: newResponse
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment