xgplayer-streaming-shared
Advanced tools
Comparing version 3.0.0-next.14 to 3.0.0-next.15
@@ -557,2 +557,27 @@ function ownKeys(object, enumerableOnly) { | ||
} | ||
function _superPropBase(object, property) { | ||
while (!Object.prototype.hasOwnProperty.call(object, property)) { | ||
object = _getPrototypeOf(object); | ||
if (object === null) | ||
break; | ||
} | ||
return object; | ||
} | ||
function _get() { | ||
if (typeof Reflect !== "undefined" && Reflect.get) { | ||
_get = Reflect.get.bind(); | ||
} else { | ||
_get = function _get2(target, property, receiver) { | ||
var base = _superPropBase(target, property); | ||
if (!base) | ||
return; | ||
var desc = Object.getOwnPropertyDescriptor(base, property); | ||
if (desc.get) { | ||
return desc.get.call(arguments.length < 3 ? target : receiver); | ||
} | ||
return desc.value; | ||
}; | ||
} | ||
return _get.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -632,2 +657,2 @@ if (!o) | ||
} | ||
export { _arrayLikeToArray as arrayLikeToArray, _assertThisInitialized as assertThisInitialized, _asyncToGenerator as asyncToGenerator, _classCallCheck as classCallCheck, _construct as construct, _createClass as createClass, _createForOfIteratorHelper as createForOfIteratorHelper, _createSuper as createSuper, _defineProperty as defineProperty, _getPrototypeOf as getPrototypeOf, _inherits as inherits, _isNativeFunction as isNativeFunction, _isNativeReflectConstruct as isNativeReflectConstruct, _objectSpread2 as objectSpread2, _objectWithoutProperties as objectWithoutProperties, _objectWithoutPropertiesLoose as objectWithoutPropertiesLoose, _possibleConstructorReturn as possibleConstructorReturn, _regeneratorRuntime as regeneratorRuntime, _setPrototypeOf as setPrototypeOf, _typeof as typeof, _unsupportedIterableToArray as unsupportedIterableToArray, _wrapNativeSuper as wrapNativeSuper }; | ||
export { _arrayLikeToArray as arrayLikeToArray, _assertThisInitialized as assertThisInitialized, _asyncToGenerator as asyncToGenerator, _classCallCheck as classCallCheck, _construct as construct, _createClass as createClass, _createForOfIteratorHelper as createForOfIteratorHelper, _createSuper as createSuper, _defineProperty as defineProperty, _get as get, _getPrototypeOf as getPrototypeOf, _inherits as inherits, _isNativeFunction as isNativeFunction, _isNativeReflectConstruct as isNativeReflectConstruct, _objectSpread2 as objectSpread2, _objectWithoutProperties as objectWithoutProperties, _objectWithoutPropertiesLoose as objectWithoutPropertiesLoose, _possibleConstructorReturn as possibleConstructorReturn, _regeneratorRuntime as regeneratorRuntime, _setPrototypeOf as setPrototypeOf, _superPropBase as superPropBase, _typeof as typeof, _unsupportedIterableToArray as unsupportedIterableToArray, _wrapNativeSuper as wrapNativeSuper }; |
@@ -30,2 +30,3 @@ export namespace EVENT { | ||
const MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT: string; | ||
const REAL_TIME_SPEED: string; | ||
} |
@@ -29,4 +29,5 @@ var EVENT = { | ||
AUDIO_OVERLAP_DETECT: "AUDIO_OVERLAP_DETECT", | ||
MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT: "MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT" | ||
MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT: "MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT", | ||
REAL_TIME_SPEED: "real_time_speed" | ||
}; | ||
export { EVENT }; |
@@ -47,2 +47,3 @@ declare var _default: { | ||
MAX_DTS_DELTA_WITH_NEXT_SEGMENT_DETECT: string; | ||
REAL_TIME_SPEED: string; | ||
}; | ||
@@ -49,0 +50,0 @@ ERR: { |
@@ -29,5 +29,6 @@ import { objectSpread2 as _objectSpread2 } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
referrerPolicy: void 0, | ||
integrity: void 0 | ||
integrity: void 0, | ||
onProcessMinLen: 0 | ||
}, cfg); | ||
} | ||
export { getConfig }; |
@@ -14,3 +14,4 @@ export class FetchLoader { | ||
_vid: string; | ||
load({ url, vid, timeout, responseType, onProgress, index, onTimeout, range, transformResponse, request, params, logger, method, headers, body, mode, credentials, cache, redirect, referrer, referrerPolicy }: { | ||
_onProcessMinLen: number; | ||
load({ url, vid, timeout, responseType, onProgress, index, onTimeout, range, transformResponse, request, params, logger, method, headers, body, mode, credentials, cache, redirect, referrer, referrerPolicy, onProcessMinLen }: { | ||
url: any; | ||
@@ -37,2 +38,3 @@ vid: any; | ||
referrerPolicy: any; | ||
onProcessMinLen: any; | ||
}): any; | ||
@@ -43,2 +45,4 @@ resolve: any; | ||
_loadChunk(response: any, onProgress: any, st: any, firstByteTime: any): void; | ||
_cache: any; | ||
_writeIdx: number; | ||
get receiveLen(): number; | ||
@@ -45,0 +49,0 @@ set running(arg: boolean); |
@@ -1,2 +0,2 @@ | ||
import { createClass as _createClass, classCallCheck as _classCallCheck, defineProperty as _defineProperty, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import { inherits as _inherits, createSuper as _createSuper, classCallCheck as _classCallCheck, defineProperty as _defineProperty, assertThisInitialized as _assertThisInitialized, createClass as _createClass, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime, get as _get, getPrototypeOf as _getPrototypeOf } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import "core-js/modules/es.object.to-string.js"; | ||
@@ -32,20 +32,29 @@ import "core-js/modules/es.promise.js"; | ||
import "core-js/modules/es.typed-array.to-string.js"; | ||
import "core-js/modules/es.array.slice.js"; | ||
import { NetError } from "./error.js"; | ||
import { setUrlParams, getRangeValue, createResponse } from "./helper.js"; | ||
import { setUrlParams, getRangeValue, createResponse, calculateSpeed } from "./helper.js"; | ||
import { ResponseType } from "./types.js"; | ||
import SpeedSampler from "./speed-sampler.js"; | ||
var FetchLoader = /* @__PURE__ */ function() { | ||
import { EVENT } from "../event.js"; | ||
import EventEmitter from "eventemitter3"; | ||
var CACHESIZE = 2 * 1024 * 1024; | ||
var FetchLoader = /* @__PURE__ */ function(_EventEmitter) { | ||
_inherits(FetchLoader2, _EventEmitter); | ||
var _super = _createSuper(FetchLoader2); | ||
function FetchLoader2() { | ||
var _this; | ||
_classCallCheck(this, FetchLoader2); | ||
_defineProperty(this, "_abortController", null); | ||
_defineProperty(this, "_timeoutTimer", null); | ||
_defineProperty(this, "_reader", null); | ||
_defineProperty(this, "_response", null); | ||
_defineProperty(this, "_aborted", false); | ||
_defineProperty(this, "_index", -1); | ||
_defineProperty(this, "_range", null); | ||
_defineProperty(this, "_receivedLength", 0); | ||
_defineProperty(this, "_running", false); | ||
_defineProperty(this, "_logger", null); | ||
_defineProperty(this, "_vid", ""); | ||
_this = _super.call(this); | ||
_defineProperty(_assertThisInitialized(_this), "_abortController", null); | ||
_defineProperty(_assertThisInitialized(_this), "_timeoutTimer", null); | ||
_defineProperty(_assertThisInitialized(_this), "_reader", null); | ||
_defineProperty(_assertThisInitialized(_this), "_response", null); | ||
_defineProperty(_assertThisInitialized(_this), "_aborted", false); | ||
_defineProperty(_assertThisInitialized(_this), "_index", -1); | ||
_defineProperty(_assertThisInitialized(_this), "_range", null); | ||
_defineProperty(_assertThisInitialized(_this), "_receivedLength", 0); | ||
_defineProperty(_assertThisInitialized(_this), "_running", false); | ||
_defineProperty(_assertThisInitialized(_this), "_logger", null); | ||
_defineProperty(_assertThisInitialized(_this), "_vid", ""); | ||
_defineProperty(_assertThisInitialized(_this), "_onProcessMinLen", 0); | ||
return _this; | ||
} | ||
@@ -55,6 +64,7 @@ _createClass(FetchLoader2, [{ | ||
value: function load(_ref) { | ||
var _this$_abortControlle, _this = this; | ||
var url = _ref.url, vid = _ref.vid, timeout = _ref.timeout, responseType = _ref.responseType, onProgress = _ref.onProgress, index = _ref.index, onTimeout = _ref.onTimeout, range = _ref.range, transformResponse = _ref.transformResponse, request = _ref.request, params = _ref.params, logger = _ref.logger, method = _ref.method, headers = _ref.headers, body = _ref.body, mode = _ref.mode, credentials = _ref.credentials, cache = _ref.cache, redirect = _ref.redirect, referrer = _ref.referrer, referrerPolicy = _ref.referrerPolicy; | ||
var _this$_abortControlle, _this2 = this; | ||
var url = _ref.url, vid = _ref.vid, timeout = _ref.timeout, responseType = _ref.responseType, onProgress = _ref.onProgress, index = _ref.index, onTimeout = _ref.onTimeout, range = _ref.range, transformResponse = _ref.transformResponse, request = _ref.request, params = _ref.params, logger = _ref.logger, method = _ref.method, headers = _ref.headers, body = _ref.body, mode = _ref.mode, credentials = _ref.credentials, cache = _ref.cache, redirect = _ref.redirect, referrer = _ref.referrer, referrerPolicy = _ref.referrerPolicy, onProcessMinLen = _ref.onProcessMinLen; | ||
this._logger = logger; | ||
this._aborted = false; | ||
this._onProcessMinLen = onProcessMinLen; | ||
this._abortController = typeof AbortController !== "undefined" && new AbortController(); | ||
@@ -96,3 +106,3 @@ this._running = true; | ||
isTimeout = true; | ||
_this.cancel(); | ||
_this2.cancel(); | ||
if (onTimeout) { | ||
@@ -110,3 +120,3 @@ var error = new NetError(url, init); | ||
var _ref2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee(response) { | ||
var firstByteTime, data, costTime; | ||
var firstByteTime, data, costTime, speed; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
@@ -116,5 +126,5 @@ while (1) { | ||
case 0: | ||
clearTimeout(_this._timeoutTimer); | ||
_this._response = response; | ||
if (!(_this._aborted || !_this._running)) { | ||
clearTimeout(_this2._timeoutTimer); | ||
_this2._response = response; | ||
if (!(_this2._aborted || !_this2._running)) { | ||
_context.next = 4; | ||
@@ -143,4 +153,4 @@ break; | ||
data = _context.sent; | ||
_this._running = false; | ||
_context.next = 36; | ||
_this2._running = false; | ||
_context.next = 37; | ||
break; | ||
@@ -156,4 +166,4 @@ case 15: | ||
data = _context.sent; | ||
_this._running = false; | ||
_context.next = 36; | ||
_this2._running = false; | ||
_context.next = 37; | ||
break; | ||
@@ -165,5 +175,5 @@ case 22: | ||
} | ||
_this.resolve = resolve; | ||
_this.reject = reject; | ||
_this._loadChunk(response, onProgress, startTime, firstByteTime); | ||
_this2.resolve = resolve; | ||
_this2.reject = reject; | ||
_this2._loadChunk(response, onProgress, startTime, firstByteTime); | ||
return _context.abrupt("return"); | ||
@@ -176,14 +186,12 @@ case 29: | ||
data = new Uint8Array(data); | ||
_this._running = false; | ||
_this2._running = false; | ||
costTime = Date.now() - startTime; | ||
SpeedSampler.netSampling({ | ||
mTime: costTime, | ||
mBytes: data.byteLength, | ||
mSpeedInbPS: data.byteLength * 8 / (costTime / 1e3), | ||
mRTT: firstByteTime - startTime | ||
speed = calculateSpeed(data.byteLength, costTime); | ||
_this2.emit(EVENT.REAL_TIME_SPEED, { | ||
speed | ||
}); | ||
case 36: | ||
_this._logger.debug("[fetch load end], index,", index, ",range,", range); | ||
resolve(createResponse(data, true, response, response.headers.get("Content-Length"), response.headers.get("age"), startTime, firstByteTime, index, range, _this._vid)); | ||
case 38: | ||
case 37: | ||
_this2._logger.debug("[fetch load end], index,", index, ",range,", range); | ||
resolve(createResponse(data, true, response, response.headers.get("Content-Length"), response.headers.get("age"), startTime, firstByteTime, index, range, _this2._vid)); | ||
case 39: | ||
case "end": | ||
@@ -199,5 +207,5 @@ return _context.stop(); | ||
}()).catch(function(error) { | ||
clearTimeout(_this._timeoutTimer); | ||
_this._running = false; | ||
if (_this._aborted && !isTimeout) | ||
clearTimeout(_this2._timeoutTimer); | ||
_this2._running = false; | ||
if (_this2._aborted && !isTimeout) | ||
return; | ||
@@ -228,22 +236,23 @@ error = error instanceof NetError ? error : new NetError(url, init); | ||
this._running = false; | ||
_get(_getPrototypeOf(FetchLoader2.prototype), "removeAllListeners", this).call(this); | ||
if (!this._response) { | ||
_context2.next = 14; | ||
_context2.next = 15; | ||
break; | ||
} | ||
_context2.prev = 5; | ||
_context2.prev = 6; | ||
if (!this._reader) { | ||
_context2.next = 9; | ||
_context2.next = 10; | ||
break; | ||
} | ||
_context2.next = 9; | ||
_context2.next = 10; | ||
return this._reader.cancel(); | ||
case 9: | ||
_context2.next = 13; | ||
case 10: | ||
_context2.next = 14; | ||
break; | ||
case 11: | ||
_context2.prev = 11; | ||
_context2.t0 = _context2["catch"](5); | ||
case 13: | ||
case 12: | ||
_context2.prev = 12; | ||
_context2.t0 = _context2["catch"](6); | ||
case 14: | ||
this._response = this._reader = null; | ||
case 14: | ||
case 15: | ||
if (this._abortController) { | ||
@@ -256,3 +265,3 @@ try { | ||
} | ||
case 15: | ||
case 16: | ||
case "end": | ||
@@ -262,3 +271,3 @@ return _context2.stop(); | ||
} | ||
}, _callee2, this, [[5, 11]]); | ||
}, _callee2, this, [[6, 12]]); | ||
})); | ||
@@ -273,3 +282,3 @@ function cancel() { | ||
value: function _loadChunk(response, onProgress, st, firstByteTime) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
if (!response.body || !response.body.getReader) { | ||
@@ -280,2 +289,6 @@ this._running = false; | ||
} | ||
if (this._onProcessMinLen > 0) { | ||
this._cache = new Uint8Array(CACHESIZE); | ||
this._writeIdx = 0; | ||
} | ||
var reader = this._reader = response.body.getReader(); | ||
@@ -287,3 +300,3 @@ var data; | ||
var _ref3 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3() { | ||
var startByte, costTime; | ||
var startByte, curLen, retData, temp, costTime, speed; | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
@@ -306,18 +319,18 @@ while (1) { | ||
endTime = Date.now(); | ||
if (!_this2._aborted) { | ||
_this2._running = false; | ||
_this2.reject(_context3.t0); | ||
if (!_this3._aborted) { | ||
_this3._running = false; | ||
_this3.reject(_context3.t0); | ||
} | ||
return _context3.abrupt("return"); | ||
case 13: | ||
startByte = _this2._range[0] + _this2._receivedLength; | ||
if (!_this2._aborted) { | ||
startByte = _this3._range[0] + _this3._receivedLength; | ||
if (!_this3._aborted) { | ||
_context3.next = 18; | ||
break; | ||
} | ||
_this2._running = false; | ||
_this3._running = false; | ||
onProgress(void 0, false, { | ||
range: [startByte, startByte], | ||
vid: _this2._vid, | ||
index: _this2._index, | ||
vid: _this3._vid, | ||
index: _this3._index, | ||
startTime, | ||
@@ -328,18 +341,44 @@ endTime | ||
case 18: | ||
_this2._receivedLength += data.value ? data.value.byteLength : 0; | ||
_this2._logger.debug("\u3010fetchLoader,onProgress call\u3011,task,", _this2._range, ", start,", startByte, ", end,", _this2._range[0] + _this2._receivedLength, ", done,", data.done); | ||
onProgress(data.value, data.done, { | ||
range: [startByte, _this2._range[0] + _this2._receivedLength], | ||
vid: _this2._vid, | ||
index: _this2._index, | ||
startTime, | ||
endTime | ||
}, response); | ||
curLen = data.value ? data.value.byteLength : 0; | ||
_this3._receivedLength += curLen; | ||
_this3._logger.debug("\u3010fetchLoader,onProgress call\u3011,task,", _this3._range, ", start,", startByte, ", end,", _this3._range[0] + _this3._receivedLength, ", done,", data.done); | ||
if (_this3._onProcessMinLen > 0) { | ||
if (_this3._writeIdx + curLen >= _this3._onProcessMinLen || data.done) { | ||
retData = new Uint8Array(_this3._writeIdx + curLen); | ||
retData.set(_this3._cache.slice(0, _this3._writeIdx), 0); | ||
curLen > 0 && retData.set(data.value, _this3._writeIdx); | ||
_this3._writeIdx = 0; | ||
_this3._logger.debug("\u3010fetchLoader,onProgress enough\u3011,done,", data.done, ",len,", retData.byteLength, ", writeIdx,", _this3._writeIdx); | ||
} else { | ||
if (curLen > 0 && _this3._writeIdx + curLen < CACHESIZE) { | ||
_this3._cache.set(data.value, _this3._writeIdx); | ||
_this3._writeIdx += curLen; | ||
_this3._logger.debug("\u3010fetchLoader,onProgress cache\u3011,len,", curLen, ", writeIdx,", _this3._writeIdx); | ||
} else if (curLen > 0) { | ||
temp = new Uint8Array(_this3._writeIdx + curLen + 2048); | ||
_this3._logger.debug("\u3010fetchLoader,onProgress extra start\u3011,size,", _this3._writeIdx + curLen + 2048, ", datalen,", curLen, ", writeIdx,", _this3._writeIdx); | ||
temp.set(_this3._cache.slice(0, _this3._writeIdx), 0); | ||
curLen > 0 && temp.set(data.value, _this3._writeIdx); | ||
_this3._writeIdx += curLen; | ||
delete _this3._cache; | ||
_this3._cache = temp; | ||
_this3._logger.debug("\u3010fetchLoader,onProgress extra end\u3011,len,", curLen, ", writeIdx,", _this3._writeIdx); | ||
} | ||
} | ||
} else { | ||
retData = data.value; | ||
} | ||
if (retData && retData.byteLength > 0 || data.done) { | ||
onProgress(retData, data.done, { | ||
range: [_this3._range[0] + _this3._receivedLength - (retData ? retData.byteLength : 0), _this3._range[0] + _this3._receivedLength], | ||
vid: _this3._vid, | ||
index: _this3._index, | ||
startTime, | ||
endTime | ||
}, response); | ||
} | ||
costTime = Date.now() - st; | ||
_this2._logger.debug("[fetchLoader SPEED], fetch speed(b/s),", _this2._receivedLength * 8 / (costTime / 1e3)); | ||
SpeedSampler.netSampling({ | ||
mTime: costTime, | ||
mBytes: _this2._receivedLength, | ||
mSpeedInbPS: _this2._receivedLength * 8 / (costTime / 1e3), | ||
mRTT: firstByteTime - st | ||
speed = calculateSpeed(_this3._receivedLength, costTime); | ||
_this3.emit(EVENT.REAL_TIME_SPEED, { | ||
speed | ||
}); | ||
@@ -349,7 +388,7 @@ if (!data.done) { | ||
} else { | ||
_this2._running = false; | ||
_this2._logger.debug("[fetchLoader onProgress end],task,", _this2._range, ",done,", data.done); | ||
_this2.resolve(createResponse(data, true, response, response.headers.get("Content-Length"), response.headers.get("age"), startTime, firstByteTime, _this2._index, _this2._range, _this2._vid)); | ||
_this3._running = false; | ||
_this3._logger.debug("[fetchLoader onProgress end],task,", _this3._range, ",done,", data.done); | ||
_this3.resolve(createResponse(data, true, response, response.headers.get("Content-Length"), response.headers.get("age"), startTime, firstByteTime, _this3._index, _this3._range, _this3._vid)); | ||
} | ||
case 25: | ||
case 27: | ||
case "end": | ||
@@ -387,3 +426,3 @@ return _context3.stop(); | ||
return FetchLoader2; | ||
}(); | ||
}(EventEmitter); | ||
export { FetchLoader }; |
@@ -18,1 +18,2 @@ export function getRangeValue(value: any): string; | ||
}; | ||
export function calculateSpeed(byteLen: any, millisec: any): any; |
@@ -81,2 +81,5 @@ import "core-js/modules/es.regexp.exec.js"; | ||
} | ||
export { createResponse, getRangeValue, setUrlParams }; | ||
function calculateSpeed(byteLen, millisec) { | ||
return Math.round(byteLen * 8 * 1e3 / millisec / 1024); | ||
} | ||
export { calculateSpeed, createResponse, getRangeValue, setUrlParams }; |
export class NetLoader { | ||
static isFetchSupport(): boolean; | ||
static getSpeed(): number; | ||
static getRealTimeSpeed(): number; | ||
static getRTT(): any; | ||
constructor(cfg: any); | ||
@@ -7,0 +4,0 @@ type: string; |
@@ -1,2 +0,2 @@ | ||
import { createClass as _createClass, classCallCheck as _classCallCheck, defineProperty as _defineProperty, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import { inherits as _inherits, createSuper as _createSuper, createClass as _createClass, classCallCheck as _classCallCheck, defineProperty as _defineProperty, assertThisInitialized as _assertThisInitialized, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime, get as _get, getPrototypeOf as _getPrototypeOf } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import "core-js/modules/es.object.assign.js"; | ||
@@ -19,15 +19,22 @@ import "core-js/modules/es.array.concat.js"; | ||
import { sleep } from "../streaming-helper.js"; | ||
import SpeedSampler from "./speed-sampler.js"; | ||
var NetLoader = /* @__PURE__ */ function() { | ||
import { EVENT } from "../event.js"; | ||
import EventEmitter from "eventemitter3"; | ||
var NetLoader = /* @__PURE__ */ function(_EventEmitter) { | ||
_inherits(NetLoader2, _EventEmitter); | ||
var _super = _createSuper(NetLoader2); | ||
function NetLoader2(cfg) { | ||
var _this; | ||
_classCallCheck(this, NetLoader2); | ||
_defineProperty(this, "type", LoaderType.FETCH); | ||
_defineProperty(this, "_queue", []); | ||
_defineProperty(this, "_alive", []); | ||
_defineProperty(this, "_currentTask", null); | ||
this._config = getConfig(cfg); | ||
if (this._config.loaderType === LoaderType.XHR || !FetchLoader.isSupported()) { | ||
this.type = LoaderType.XHR; | ||
_this = _super.call(this, cfg); | ||
_defineProperty(_assertThisInitialized(_this), "type", LoaderType.FETCH); | ||
_defineProperty(_assertThisInitialized(_this), "_queue", []); | ||
_defineProperty(_assertThisInitialized(_this), "_alive", []); | ||
_defineProperty(_assertThisInitialized(_this), "_currentTask", null); | ||
_defineProperty(_assertThisInitialized(_this), "_config", void 0); | ||
_this._config = getConfig(cfg); | ||
if (_this._config.loaderType === LoaderType.XHR || !FetchLoader.isSupported()) { | ||
_this.type = LoaderType.XHR; | ||
} | ||
this.log = cfg.logger; | ||
_this.log = cfg.logger; | ||
return _this; | ||
} | ||
@@ -42,2 +49,3 @@ _createClass(NetLoader2, [{ | ||
value: function load(url) { | ||
var _this2 = this; | ||
var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; | ||
@@ -61,2 +69,5 @@ if (typeof url === "string" || !url) { | ||
var task = new Task(this.type, config); | ||
task.loader.on(EVENT.REAL_TIME_SPEED, function(data) { | ||
_this2.emit(EVENT.REAL_TIME_SPEED, data); | ||
}); | ||
this._queue.push(task); | ||
@@ -77,2 +88,3 @@ if (this._queue.length === 1 && (!this._currentTask || !this._currentTask.running)) { | ||
case 0: | ||
_get(_getPrototypeOf(NetLoader2.prototype), "removeAllListeners", this).call(this); | ||
cancels = this._queue.map(function(t) { | ||
@@ -88,8 +100,8 @@ return t.cancel(); | ||
this._alive = []; | ||
_context.next = 6; | ||
_context.next = 7; | ||
return Promise.all(cancels); | ||
case 6: | ||
_context.next = 8; | ||
case 7: | ||
_context.next = 9; | ||
return sleep(); | ||
case 8: | ||
case 9: | ||
case "end": | ||
@@ -109,3 +121,3 @@ return _context.stop(); | ||
value: function _processTask() { | ||
var _this = this; | ||
var _this3 = this; | ||
this._currentTask = this._queue.shift(); | ||
@@ -119,3 +131,3 @@ if (!this._currentTask) | ||
}).finally(function() { | ||
_this._processTask(); | ||
_this3._processTask(); | ||
}); | ||
@@ -128,20 +140,5 @@ } | ||
} | ||
}, { | ||
key: "getSpeed", | ||
value: function getSpeed() { | ||
return SpeedSampler.getSpeed(); | ||
} | ||
}, { | ||
key: "getRealTimeSpeed", | ||
value: function getRealTimeSpeed() { | ||
return SpeedSampler.getRealTimeSpeed(); | ||
} | ||
}, { | ||
key: "getRTT", | ||
value: function getRTT() { | ||
return SpeedSampler.getRTT(); | ||
} | ||
}]); | ||
return NetLoader2; | ||
}(); | ||
}(EventEmitter); | ||
export { NetLoader }; |
@@ -15,4 +15,5 @@ export class Task { | ||
get running(): any; | ||
get loader(): FetchLoader | XhrLoader; | ||
} | ||
import { FetchLoader } from "./fetch"; | ||
import { XhrLoader } from "./xhr"; |
@@ -122,2 +122,7 @@ import { createClass as _createClass, objectWithoutProperties as _objectWithoutProperties, classCallCheck as _classCallCheck, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
} | ||
}, { | ||
key: "loader", | ||
get: function get() { | ||
return this._loader; | ||
} | ||
}]); | ||
@@ -124,0 +129,0 @@ return Task2; |
@@ -12,3 +12,2 @@ export class XhrLoader { | ||
_headers: any; | ||
_chunkSizeKBList: number[]; | ||
_currentChunkSizeKB: number; | ||
@@ -20,3 +19,2 @@ _currentSpeedNormalized: number; | ||
_startTime: number; | ||
_speedSampler: SpeedSampler; | ||
_loadCompleteResolve: any; | ||
@@ -41,3 +39,2 @@ _loadCompleteReject: any; | ||
_onLoad(e: any): any; | ||
_normalizeSpeed(input: any): any; | ||
cancel(): any; | ||
@@ -49,2 +46,1 @@ get receiveLen(): number; | ||
} | ||
import SpeedSampler from "./speed-sampler"; |
@@ -1,2 +0,2 @@ | ||
import { createClass as _createClass, objectSpread2 as _objectSpread2, createForOfIteratorHelper as _createForOfIteratorHelper, classCallCheck as _classCallCheck, defineProperty as _defineProperty } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import { inherits as _inherits, createSuper as _createSuper, classCallCheck as _classCallCheck, defineProperty as _defineProperty, assertThisInitialized as _assertThisInitialized, createClass as _createClass, objectSpread2 as _objectSpread2, get as _get, getPrototypeOf as _getPrototypeOf, createForOfIteratorHelper as _createForOfIteratorHelper } from "../_virtual/_rollupPluginBabelHelpers.js"; | ||
import "core-js/modules/es.object.to-string.js"; | ||
@@ -38,37 +38,41 @@ import "core-js/modules/es.promise.js"; | ||
import { NetError } from "./error.js"; | ||
import { setUrlParams, getRangeValue, createResponse } from "./helper.js"; | ||
import { setUrlParams, getRangeValue, calculateSpeed, createResponse } from "./helper.js"; | ||
import { ResponseType } from "./types.js"; | ||
import SpeedSampler from "./speed-sampler.js"; | ||
var XhrLoader = /* @__PURE__ */ function() { | ||
import { EVENT } from "../event.js"; | ||
import EventEmitter from "eventemitter3"; | ||
var XhrLoader = /* @__PURE__ */ function(_EventEmitter) { | ||
_inherits(XhrLoader2, _EventEmitter); | ||
var _super = _createSuper(XhrLoader2); | ||
function XhrLoader2() { | ||
var _this; | ||
_classCallCheck(this, XhrLoader2); | ||
_defineProperty(this, "_xhr", null); | ||
_defineProperty(this, "_aborted", false); | ||
_defineProperty(this, "_timeoutTimer", null); | ||
_defineProperty(this, "_range", null); | ||
_defineProperty(this, "_receivedLength", 0); | ||
_defineProperty(this, "_url", null); | ||
_defineProperty(this, "_onProgress", null); | ||
_defineProperty(this, "_index", -1); | ||
_defineProperty(this, "_headers", null); | ||
_defineProperty(this, "_chunkSizeKBList", [128, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 5120, 6144, 7168, 8192]); | ||
_defineProperty(this, "_currentChunkSizeKB", 384); | ||
_defineProperty(this, "_currentSpeedNormalized", 0); | ||
_defineProperty(this, "_zeroSpeedChunkCount", 0); | ||
_defineProperty(this, "_timeout", null); | ||
_defineProperty(this, "_xhr", null); | ||
_defineProperty(this, "_withCredentials", null); | ||
_defineProperty(this, "_startTime", -1); | ||
_defineProperty(this, "_speedSampler", new SpeedSampler()); | ||
_defineProperty(this, "_loadCompleteResolve", null); | ||
_defineProperty(this, "_loadCompleteReject", null); | ||
_defineProperty(this, "_runing", false); | ||
_defineProperty(this, "_logger", false); | ||
_defineProperty(this, "_vid", ""); | ||
_defineProperty(this, "_responseType", void 0); | ||
_defineProperty(this, "_credentials", void 0); | ||
_defineProperty(this, "_method", void 0); | ||
_defineProperty(this, "_transformResponse", void 0); | ||
_defineProperty(this, "_firstRtt", void 0); | ||
_defineProperty(this, "_subRangeStartTime", void 0); | ||
_this = _super.call(this); | ||
_defineProperty(_assertThisInitialized(_this), "_xhr", null); | ||
_defineProperty(_assertThisInitialized(_this), "_aborted", false); | ||
_defineProperty(_assertThisInitialized(_this), "_timeoutTimer", null); | ||
_defineProperty(_assertThisInitialized(_this), "_range", null); | ||
_defineProperty(_assertThisInitialized(_this), "_receivedLength", 0); | ||
_defineProperty(_assertThisInitialized(_this), "_url", null); | ||
_defineProperty(_assertThisInitialized(_this), "_onProgress", null); | ||
_defineProperty(_assertThisInitialized(_this), "_index", -1); | ||
_defineProperty(_assertThisInitialized(_this), "_headers", null); | ||
_defineProperty(_assertThisInitialized(_this), "_currentChunkSizeKB", 384); | ||
_defineProperty(_assertThisInitialized(_this), "_currentSpeedNormalized", 0); | ||
_defineProperty(_assertThisInitialized(_this), "_zeroSpeedChunkCount", 0); | ||
_defineProperty(_assertThisInitialized(_this), "_timeout", null); | ||
_defineProperty(_assertThisInitialized(_this), "_xhr", null); | ||
_defineProperty(_assertThisInitialized(_this), "_withCredentials", null); | ||
_defineProperty(_assertThisInitialized(_this), "_startTime", -1); | ||
_defineProperty(_assertThisInitialized(_this), "_loadCompleteResolve", null); | ||
_defineProperty(_assertThisInitialized(_this), "_loadCompleteReject", null); | ||
_defineProperty(_assertThisInitialized(_this), "_runing", false); | ||
_defineProperty(_assertThisInitialized(_this), "_logger", false); | ||
_defineProperty(_assertThisInitialized(_this), "_vid", ""); | ||
_defineProperty(_assertThisInitialized(_this), "_responseType", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "_credentials", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "_method", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "_transformResponse", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "_firstRtt", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "_subRangeStartTime", void 0); | ||
return _this; | ||
} | ||
@@ -78,3 +82,3 @@ _createClass(XhrLoader2, [{ | ||
value: function load(req) { | ||
var _this = this; | ||
var _this2 = this; | ||
clearTimeout(this._timeoutTimer); | ||
@@ -99,11 +103,11 @@ this._logger = req.logger; | ||
return new Promise(function(resolve, reject) { | ||
_this._loadCompleteResolve = resolve; | ||
_this._loadCompleteReject = reject; | ||
_this._startLoad(); | ||
_this2._loadCompleteResolve = resolve; | ||
_this2._loadCompleteReject = reject; | ||
_this2._startLoad(); | ||
}).catch(function(error) { | ||
clearTimeout(_this._timeoutTimer); | ||
_this._runing = false; | ||
if (_this._aborted) | ||
clearTimeout(_this2._timeoutTimer); | ||
_this2._runing = false; | ||
if (_this2._aborted) | ||
return; | ||
error = error instanceof NetError ? error : new NetError(_this._url, _this._request); | ||
error = error instanceof NetError ? error : new NetError(_this2._url, _this2._request); | ||
error.startTime = startTime; | ||
@@ -140,3 +144,3 @@ error.endTime = Date.now(); | ||
value: function _internalOpen(range) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
try { | ||
@@ -152,13 +156,13 @@ this._startTime = Date.now(); | ||
xhr.onerror = function(event) { | ||
_this2._running = false; | ||
_this2._loadCompleteReject(event); | ||
_this3._running = false; | ||
_this3._loadCompleteReject(event); | ||
}; | ||
xhr.ontimeout = function(event) { | ||
_this2.cancel(); | ||
if (_this2._onTimeout) { | ||
var error = new NetError(_this2._url, _this2._request); | ||
_this3.cancel(); | ||
if (_this3._onTimeout) { | ||
var error = new NetError(_this3._url, _this3._request); | ||
error.isTimeout = true; | ||
_this2._onTimeout(error); | ||
_this3._onTimeout(error); | ||
} | ||
_this2._loadCompleteReject(event); | ||
_this3._loadCompleteReject(event); | ||
}; | ||
@@ -199,16 +203,2 @@ var rangeValue = getRangeValue(range); | ||
} | ||
var KBps = this._speedSampler.lastSecondKBps; | ||
if (KBps === 0) { | ||
this._zeroSpeedChunkCount++; | ||
if (this._zeroSpeedChunkCount >= 3) { | ||
KBps = this._speedSampler.currentKBps; | ||
} | ||
} | ||
if (KBps !== 0) { | ||
var normalized = this._normalizeSpeed(KBps); | ||
if (this._currentSpeedNormalized !== normalized) { | ||
this._currentSpeedNormalized = normalized; | ||
this._currentChunkSizeKB = normalized; | ||
} | ||
} | ||
var data = null; | ||
@@ -223,8 +213,5 @@ var done = false; | ||
var costTime = Date.now() - this._subRangeStartTime; | ||
this._speedSampler.addBytes(chunk.byteLength); | ||
SpeedSampler.netSampling({ | ||
mTime: costTime, | ||
mBytes: this._receivedLength, | ||
mSpeedInbPS: this._receivedLength * 8 / (costTime / 1e3), | ||
mRTT: this._firstRtt - this._subRangeStartTime | ||
var speed = calculateSpeed(this._receivedLength, costTime); | ||
this.emit(EVENT.REAL_TIME_SPEED, { | ||
speed | ||
}); | ||
@@ -271,24 +258,2 @@ } | ||
}, { | ||
key: "_normalizeSpeed", | ||
value: function _normalizeSpeed(input) { | ||
var list = this._chunkSizeKBList; | ||
var last = list.length - 1; | ||
var mid = 0; | ||
var lbound = 0; | ||
var ubound = last; | ||
if (input < list[0]) { | ||
return list[0]; | ||
} | ||
while (lbound <= ubound) { | ||
mid = lbound + Math.floor((ubound - lbound) / 2); | ||
if (mid === last || input >= list[mid] && input < list[mid + 1]) { | ||
return list[mid]; | ||
} else if (list[mid] < input) { | ||
lbound = mid + 1; | ||
} else { | ||
ubound = mid - 1; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "cancel", | ||
@@ -300,2 +265,3 @@ value: function cancel() { | ||
this._runing = false; | ||
_get(_getPrototypeOf(XhrLoader2.prototype), "removeAllListeners", this).call(this); | ||
if (this._xhr) { | ||
@@ -344,3 +310,3 @@ return this._xhr.abort(); | ||
return XhrLoader2; | ||
}(); | ||
}(EventEmitter); | ||
export { XhrLoader }; |
{ | ||
"name": "xgplayer-streaming-shared", | ||
"version": "3.0.0-next.14", | ||
"version": "3.0.0-next.15", | ||
"main": "dist/index.min.js", | ||
@@ -21,2 +21,5 @@ "module": "es/index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"eventemitter3": "^4.0.7" | ||
}, | ||
"unpkgFiles": [ | ||
@@ -23,0 +26,0 @@ "dist" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
753253
1
48
3920
93
+ Addedeventemitter3@^4.0.7
+ Addedeventemitter3@4.0.7(transitive)