UNPKG

xgplayer-dash

Version:
299 lines (298 loc) 11 kB
import { inherits as _inherits, createSuper as _createSuper, classCallCheck as _classCallCheck, createClass as _createClass } from "../_virtual/_rollupPluginBabelHelpers.js"; import EventEmitter from "eventemitter3"; import util from "../util/index.js"; import XHR from "../util/xhr.js"; import Xml2Json from "./xml2json.js"; function parseMPD(ctx) { var xmlString; if (typeof ctx === "string") { xmlString = ctx; } if (ctx instanceof ArrayBuffer) { var uint8arr = new Uint8Array(ctx); xmlString = String.fromCharCode.apply(null, uint8arr); } if (!xmlString) { return null; } xmlString = xmlString.replace(/<(\/)?(\w+:)+(\w+)/g, "<$1$3"); return Xml2Json.parse(xmlString); } var MPD = /* @__PURE__ */ function(_EventEmitter) { _inherits(MPD2, _EventEmitter); var _super = _createSuper(MPD2); function MPD2(url) { var _this; _classCallCheck(this, MPD2); _this = _super.call(this); _this.url = url; _this.mediaList = { video: [], audio: [] }; _this.retryMax = 10; _this.retry = 0; _this.type = "live"; _this.isEnd = false; _this.duration = 0; _this.init(url); return _this; } _createClass(MPD2, [{ key: "fetch", value: function fetch(url) { var self = this; var meta = { TYPE: "LIVE", ENDLIST: "" }; var segments = []; return new Promise(function(resolve, reject) { new XHR({ type: "", url }).then(function(res) { var ctx = res.responseText; if (ctx) { var repID = []; var result = parseMPD(ctx); if (result.type === "static") { meta.TYPE = "VOD"; } if (result.minBufferTime) { meta.minBufferTime = util.durationConvert(result.minBufferTime); } if (result.maxSegmentDuration) { meta.maxSegmentDuration = util.durationConvert(result.maxSegmentDuration); } if (result.mediaPresentationDuration) { self.duration = util.durationConvert(result.mediaPresentationDuration); } var MpdBaseURL = ""; if (result.BaseURL) { MpdBaseURL = result.BaseURL[0]; } var Period = result.Period[0]; if (!self.duration && Period && Period.duration) { self.duration = util.durationConvert(Period.duration); } meta.duration = self.duration; Period.AdaptationSet.forEach(function(asItem, asIndex) { var mimeType = "video/mp4"; var codecs = "avc1.64001E"; var width = "640"; var height = "360"; var maxWidth = "640"; var maxHeight = "360"; var frameRate = "25"; var sar = "1:1"; var startWithSAP = "1"; var bandwidth = "588633"; if (asItem.mimeType) { mimeType = asItem.mimeType; if (mimeType === "video/mp4") { codecs = asItem.codecs; width = parseFloat(asItem.width); height = parseFloat(asItem.height); maxWidth = parseFloat(asItem.maxWidth); maxHeight = parseFloat(asItem.maxHeight); frameRate = parseFloat(asItem.frameRate); sar = asItem.sar; startWithSAP = asItem.startWithSAP; bandwidth = parseFloat(asItem.bandwidth); } else if (mimeType === "audio/mp4") { codecs = asItem.codecs; startWithSAP = asItem.startWithSAP; bandwidth = parseFloat(asItem.bandwidth); } } asItem.Representation.forEach(function(rItem, rIndex) { if (repID.indexOf(rItem.id) > -1) { rItem.id = (parseInt(repID[repID.length - 1]) + 1).toString(); } repID.push(rItem.id); var initSegment = ""; var mediaSegments = []; var timescale = 0; var duration = 0; var baseURL = url.slice(0, url.lastIndexOf("/") + 1) + MpdBaseURL; if (rItem.mimeType) { mimeType = rItem.mimeType; } if (rItem.mimeType) { mimeType = rItem.mimeType; } if (mimeType === "video/mp4") { if (rItem.codecs) { codecs = rItem.codecs; } if (rItem.width) { width = parseFloat(rItem.width); } if (rItem.height) { height = parseFloat(rItem.height); } if (rItem.maxWidth) { maxWidth = parseFloat(rItem.maxWidth); } if (rItem.maxHeight) { maxHeight = parseFloat(rItem.maxHeight); } if (rItem.frameRate) { frameRate = parseFloat(rItem.frameRate); } if (rItem.sar) { sar = rItem.sar; } if (rItem.startWithSAP) { startWithSAP = rItem.startWithSAP; } if (rItem.bandwidth) { bandwidth = parseFloat(rItem.bandwidth); } } else if (mimeType === "audio/mp4") { if (rItem.codecs) { codecs = rItem.codecs; } if (rItem.startWithSAP) { startWithSAP = rItem.startWithSAP; } if (rItem.bandwidth) { bandwidth = parseFloat(rItem.bandwidth); } } if (rItem.BaseURL && rItem.BaseURL.length > 0) { baseURL += rItem.BaseURL[0]; } var ST; if (asItem.SegmentTemplate && asItem.SegmentTemplate.length > 0) { ST = asItem.SegmentTemplate[0]; } if (rItem.SegmentTemplate && rItem.SegmentTemplate.length > 0) { ST = rItem.SegmentTemplate[0]; } var encrypted = false; if (asItem.ContentProtection || rItem.ContentProtection) { encrypted = true; } if (ST) { initSegment = ST.initialization; timescale = parseFloat(ST.timescale); duration = parseFloat(ST.duration); var segmentDuration = duration / timescale; var start = parseInt(ST.startNumber); var end = start + Math.ceil(self.duration / segmentDuration) - 1; for (var i = start; i <= end; i++) { var startTime = segmentDuration * (i - start); var endTime = segmentDuration * (i - start + 1); if (i === end) { segmentDuration = self.duration - segmentDuration * (end - start); endTime = self.duration; } mediaSegments.push({ idx: i, start: startTime, end: endTime, url: baseURL + ST.media.replace("$RepresentationID$", rItem.id).replace("$Number%04d$", util.preFixInterge(i, 4)), downloaded: false, segmentDuration }); } } if (mimeType === "video/mp4") { self.mediaList.video.push({ id: rItem.id, baseURL, initSegment: baseURL + initSegment.replace("$RepresentationID$", rItem.id), inited: false, mediaSegments, mimeType, codecs, width, height, maxWidth, maxHeight, frameRate, sar, startWithSAP, bandwidth, timescale, duration, encrypted }); } else if (mimeType === "audio/mp4") { self.mediaList.audio.push({ id: rItem.id, baseURL, initSegment: baseURL + initSegment.replace("$RepresentationID$", rItem.id), inited: false, mediaSegments, mimeType, codecs, startWithSAP, bandwidth, timescale, duration, encrypted }); } }); }); ["video", "audio"].forEach(function(mediaType) { self.mediaList[mediaType].selectedIdx = 0; self.mediaList[mediaType].sort(function(a, b) { return a.bandwidth - b.bandwidth; }); }); resolve({ meta, segments }); } else { reject(new Error("parse error")); } }).catch(function(err) { console.error(err); reject(err); }); }); } }, { key: "init", value: function init(url) { var self = this; this.fetch(url).then(function(res) { self.type = res.meta.TYPE.toLocaleLowerCase(); self.emit("ready"); }, function() { console.log("mpd init reject"); ["video", "audio"].forEach(function(mediaType) { self.mediaList[mediaType] = []; }); self.retry++; if (self.retry < self.retryMax) { self.init(url); } }); } }, { key: "seek", value: function seek(time) { var self = this; var mediaResults = {}; if (this.type === "vod" || time !== void 0) { ["video", "audio"].forEach(function(mediaType) { var mo = self.mediaList[mediaType][self.mediaList[mediaType].selectedIdx]; if (mo) { mediaResults[mediaType] = mo.mediaSegments.filter(function(item) { return (item.start <= time && time < item.end || item.start <= time - item.segmentDuration && time - item.segmentDuration < item.end || item.start <= time + item.segmentDuration && time + item.segmentDuration < item.end) && !item.downloaded; }); } }); } return mediaResults; } }]); return MPD2; }(EventEmitter); export { MPD as default };