2 lines
140 KiB
JavaScript
2 lines
140 KiB
JavaScript
|
require=function e(t,n,r){function i(o,a){if(!n[o]){if(!t[o]){var u="function"==typeof require&&require;if(!a&&u)return u(o,!0);if(s)return s(o,!0);var c=new Error("Cannot find module '"+o+"'");throw c.code="MODULE_NOT_FOUND",c}var h=n[o]={exports:{}};t[o][0].call(h.exports,function(e){return i(t[o][1][e]||e)},h,h.exports,e,t,n,r)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;o<r.length;o++)i(r[o]);return i}({1:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const r=1e3,i=6e4;class s{constructor(e,t){this.value=e,this.timeStamp=t}}n.BandwidthApproximator=class{constructor(){this.lastBytes=[],this.currentBytesSum=0,this.lastBandwidth=[]}addBytes(e,t){for(this.lastBytes.push(new s(e,t)),this.currentBytesSum+=e;t-this.lastBytes[0].timeStamp>r;)this.currentBytesSum-=this.lastBytes.shift().value;this.lastBandwidth.push(new s(this.currentBytesSum/r,t))}getBandwidth(e){for(;0!=this.lastBandwidth.length&&e-this.lastBandwidth[0].timeStamp>i;)this.lastBandwidth.shift();let t=0;for(const e of this.lastBandwidth)e.value>t&&(t=e.value);return t}getSmoothInterval(){return r}getMeasureInterval(){return i}}},{}],2:[function(e,t,n){window.p2pml||(window.p2pml={}),window.p2pml.core=e("./index")},{"./index":"p2p-media-loader-core"}],3:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const r=e("debug"),i=e("./stringly-typed-event-emitter");n.HttpMediaManager=class extends i.STEEmitter{constructor(e){super(),this.settings=e,this.xhrRequests=new Map,this.failedSegments=new Map,this.debug=r("p2pml:http-media-manager"),this.now=()=>performance.now()}download(e,t){if(this.isDownloading(e))return;this.cleanTimedOutFailedSegments();const n=this.settings.segmentUrlBuilder?this.settings.segmentUrlBuilder(e):e.url;this.debug("http segment download",n),e.requestUrl=n;const r=new XMLHttpRequest;if(r.open("GET",n,!0),r.responseType="arraybuffer",e.range)r.setRequestHeader("Range",e.range),t=void 0;else if(void 0!==t&&this.settings.httpUseRanges){let e=0;for(const n of t)e+=n.byteLength;r.setRequestHeader("Range",`bytes=${e}-`),this.debug("continue download from",e)}else t=void 0;this.setupXhrEvents(r,e,t),this.settings.xhrSetup&&this.settings.xhrSetup(r,n),this.xhrRequests.set(e.id,{xhr:r,segment:e}),r.send()}abort(e){const t=this.xhrRequests.get(e.id);t&&(t.xhr.abort(),this.xhrRequests.delete(e.id),this.debug("http segment abort",e.id))}isDownloading(e){return this.xhrRequests.has(e.id)}isFailed(e){const t=this.failedSegments.get(e.id);return void 0!==t&&t>this.now()}getActiveDownloads(){return this.xhrRequests}getActiveDownloadsCount(){return this.xhrRequests.size}destroy(){this.xhrRequests.forEach(e=>e.xhr.abort()),this.xhrRequests.clear()}setupXhrEvents(e,t,n){let r=0;e.addEventListener("progress",e=>{const t=e.loaded-r;this.emit("bytes-downloaded",t),r=e.loaded}),e.addEventListener("load",async r=>{if(r.target.status<200||r.target.status>=300)return void this.segmentFailure(t,r,e);let i=r.target.response;if(void 0!==n&&206===r.target.status){let e=0;for(const t of n)e+=t.byteLength;const t=new Uint8Array(e+i.byteLength);let r=0;for(const e of n)t.set(new Uint8Array(e),r),r+=e.byteLength;t.set(new Uint8Array(i),r),i=t.buffer}await this.segmentDownloadFinished(t,i,e)}),e.addEventListener("error",n=>{this.segmentFailure(t,n,e)}),e.addEventListener("timeout",n=>{this.segmentFailure(t,n,e)})}async segmentDownloadFinished(e,t,n){if(e.responseUrl=null===n.responseURL?void 0:n.responseURL,this.settings.segmentValidator)try{await this.settings.segmentValidator(Object.assign(Object.assign({},e),{data:t}),"http")}catch(t){return this.debug("segment validator failed",t),void this.segmentFailure(e,t,n)}this.xhrRequests.delete(e.id),this.emit("segment-loaded",e,t)}segmentFailure(e,t,n){e.responseUrl=null===n.responseURL?void 0:n.responseURL,this.xhrRequests.delete(e.id),this.failedSegments.set(e.id,this.now()+this.settings.httpFailedSegmentTimeout),this.emit("segment-error",e,t)}cleanTimedOutFailedSegments(){const e=this.now(),t=[];this.failedSegments.forEach((n,r)=>{n<e&&t.pus
|