xgplayer-mp4-loader 0.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/loader.js CHANGED
@@ -1,64 +1,49 @@
1
- import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
2
- import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
3
- import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
4
- import _createClass from '@babel/runtime/helpers/createClass';
5
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
6
- import 'core-js/modules/es.array.find.js';
7
- import 'core-js/modules/es.array.slice.js';
8
- import _regeneratorRuntime from '@babel/runtime/regenerator';
9
- import { concatUint8Array, NetLoader } from 'xgplayer-streaming-shared';
10
- import { MP4Parser } from 'xgplayer-transmuxer';
11
- import { getConfig } from './config.js';
12
- import { MediaError } from './error.js';
13
- import { Cache } from './cache.js';
14
- import { isNumber, moovToMeta, moovToSegments } from './utils.js';
15
-
16
- var _excluded = ["vid", "moovEnd", "segmentDuration", "cache", "maxDownloadInfoSize"];
17
- var MP4Loader = /*#__PURE__*/function () {
18
- function MP4Loader(config) {
19
- _classCallCheck(this, MP4Loader);
20
-
21
- _defineProperty(this, "vid", '');
22
-
23
- _defineProperty(this, "url", '');
24
-
25
- _defineProperty(this, "meta", {});
26
-
27
- _defineProperty(this, "downloadInfo", []);
28
-
29
- _defineProperty(this, "videoSegments", []);
30
-
31
- _defineProperty(this, "audioSegments", []);
32
-
33
- _defineProperty(this, "cache", null);
34
-
35
- _defineProperty(this, "_currentSegmentIndex", -1);
36
-
37
- _defineProperty(this, "_currentLoadingSegmentIndex", -1);
38
-
39
- _defineProperty(this, "_transformError", function (error) {
40
- error.type = 'network';
1
+ import { inherits as _inherits, createSuper as _createSuper, createClass as _createClass, classCallCheck as _classCallCheck, defineProperty as _defineProperty, assertThisInitialized as _assertThisInitialized, objectWithoutProperties as _objectWithoutProperties, asyncToGenerator as _asyncToGenerator, regeneratorRuntime as _regeneratorRuntime, slicedToArray as _slicedToArray, objectSpread2 as _objectSpread2 } from "./_virtual/_rollupPluginBabelHelpers.js";
2
+ import { Logger, NetLoader, EVENT, concatUint8Array } from "xgplayer-streaming-shared";
3
+ import { MP4Parser } from "xgplayer-transmuxer";
4
+ import { getConfig } from "./config.js";
5
+ import { MediaError } from "./error.js";
6
+ import { Cache } from "./cache.js";
7
+ import { isNumber, moovToSegments, moovToMeta } from "./utils.js";
8
+ import EventEmitter from "eventemitter3";
9
+ var _excluded = ["vid", "cache"];
10
+ var MP4Loader = /* @__PURE__ */ function(_EventEmitter) {
11
+ _inherits(MP4Loader2, _EventEmitter);
12
+ var _super = _createSuper(MP4Loader2);
13
+ function MP4Loader2(config) {
14
+ var _this;
15
+ _classCallCheck(this, MP4Loader2);
16
+ _this = _super.call(this);
17
+ _defineProperty(_assertThisInitialized(_this), "vid", "");
18
+ _defineProperty(_assertThisInitialized(_this), "url", "");
19
+ _defineProperty(_assertThisInitialized(_this), "meta", {});
20
+ _defineProperty(_assertThisInitialized(_this), "downloadInfo", []);
21
+ _defineProperty(_assertThisInitialized(_this), "videoSegments", []);
22
+ _defineProperty(_assertThisInitialized(_this), "audioSegments", []);
23
+ _defineProperty(_assertThisInitialized(_this), "cache", null);
24
+ _defineProperty(_assertThisInitialized(_this), "_currentSegmentIndex", -1);
25
+ _defineProperty(_assertThisInitialized(_this), "_currentLoadingSegmentIndex", -1);
26
+ _defineProperty(_assertThisInitialized(_this), "buffer", void 0);
27
+ _defineProperty(_assertThisInitialized(_this), "_error", void 0);
28
+ _defineProperty(_assertThisInitialized(_this), "_transformError", function(error) {
41
29
  return error;
42
30
  });
43
-
44
- this._config = getConfig(config);
45
-
46
- var _this$_config = this._config,
47
- vid = _this$_config.vid;
48
- _this$_config.moovEnd;
49
- _this$_config.segmentDuration;
50
- var cache = _this$_config.cache;
51
- _this$_config.maxDownloadInfoSize;
52
- var rest = _objectWithoutProperties(_this$_config, _excluded);
53
-
54
- this.cache = cache || new Cache();
55
- this.vid = vid || rest.url;
56
- this.url = rest.url;
57
- rest.transformError = this._transformError;
58
- this._loader = new NetLoader(rest);
31
+ _this._config = getConfig(config);
32
+ var _this$_config = _this._config, vid = _this$_config.vid, cache = _this$_config.cache, rest = _objectWithoutProperties(_this$_config, _excluded);
33
+ _this.cache = cache || new Cache();
34
+ _this.vid = vid || rest.url;
35
+ _this.url = rest.url;
36
+ rest.transformError = _this._transformError;
37
+ _this.logger = new Logger("MP4Loader_" + _this.vid);
38
+ !!config.openLog && Logger.enable();
39
+ rest.logger = _this.logger;
40
+ _this._loader = new NetLoader(rest);
41
+ _this._loader.on(EVENT.REAL_TIME_SPEED, function(data) {
42
+ _this.emit(EVENT.REAL_TIME_SPEED, data);
43
+ });
44
+ return _this;
59
45
  }
60
-
61
- _createClass(MP4Loader, [{
46
+ _createClass(MP4Loader2, [{
62
47
  key: "isMetaLoaded",
63
48
  get: function get() {
64
49
  return this.videoSegments.length || this.audioSegments.length;
@@ -75,11 +60,9 @@ var MP4Loader = /*#__PURE__*/function () {
75
60
  value: function isLastSegment(segIndex) {
76
61
  if (isNumber(segIndex)) {
77
62
  var _this$videoSegments, _this$audioSegments;
78
-
79
63
  var lastIndex = ((_this$videoSegments = this.videoSegments[this.videoSegments.length - 1]) === null || _this$videoSegments === void 0 ? void 0 : _this$videoSegments.index) || ((_this$audioSegments = this.audioSegments[this.audioSegments.length - 1]) === null || _this$audioSegments === void 0 ? void 0 : _this$audioSegments.index) || 0;
80
- return segIndex === lastIndex;
64
+ return segIndex >= lastIndex;
81
65
  }
82
-
83
66
  return false;
84
67
  }
85
68
  }, {
@@ -89,408 +72,477 @@ var MP4Loader = /*#__PURE__*/function () {
89
72
  }
90
73
  }, {
91
74
  key: "changeUrl",
92
- value: function () {
93
- var _changeUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
94
- var vid,
95
- moovEnd,
96
- _args = arguments;
97
- return _regeneratorRuntime.wrap(function _callee$(_context) {
98
- while (1) {
75
+ value: function() {
76
+ var _changeUrl = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee(url) {
77
+ var vid, moovEnd, notCancelLoader, _args = arguments;
78
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
79
+ while (1)
99
80
  switch (_context.prev = _context.next) {
100
81
  case 0:
101
- vid = _args.length > 1 && _args[1] !== undefined ? _args[1] : url;
102
- moovEnd = _args.length > 2 ? _args[2] : undefined;
103
- _context.next = 4;
104
- return this.reset();
105
-
106
- case 4:
107
- if (url) this.url = url;
108
- if (vid) this.vid = vid;
109
- if (moovEnd) this._config.moovEnd = moovEnd;
110
-
111
- case 7:
82
+ vid = _args.length > 1 && _args[1] !== void 0 ? _args[1] : url;
83
+ moovEnd = _args.length > 2 ? _args[2] : void 0;
84
+ notCancelLoader = _args.length > 3 ? _args[3] : void 0;
85
+ _context.next = 5;
86
+ return this.reset(notCancelLoader);
87
+ case 5:
88
+ if (url)
89
+ this.url = url;
90
+ if (vid)
91
+ this.vid = vid;
92
+ if (moovEnd)
93
+ this._config.moovEnd = moovEnd;
94
+ case 8:
112
95
  case "end":
113
96
  return _context.stop();
114
97
  }
115
- }
116
98
  }, _callee, this);
117
99
  }));
118
-
119
100
  function changeUrl(_x) {
120
101
  return _changeUrl.apply(this, arguments);
121
102
  }
122
-
123
103
  return changeUrl;
124
104
  }()
125
105
  }, {
126
106
  key: "getOrLoadMeta",
127
- value: function () {
128
- var _getOrLoadMeta = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(cache) {
129
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
130
- while (1) {
107
+ value: function() {
108
+ var _getOrLoadMeta = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee2(cache) {
109
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
110
+ while (1)
131
111
  switch (_context2.prev = _context2.next) {
132
112
  case 0:
133
113
  if (this.isMetaLoaded) {
134
114
  _context2.next = 3;
135
115
  break;
136
116
  }
137
-
138
117
  _context2.next = 3;
139
118
  return this.loadMeta(cache);
140
-
141
119
  case 3:
142
120
  return _context2.abrupt("return", this.meta);
143
-
144
121
  case 4:
145
122
  case "end":
146
123
  return _context2.stop();
147
124
  }
148
- }
149
125
  }, _callee2, this);
150
126
  }));
151
-
152
127
  function getOrLoadMeta(_x2) {
153
128
  return _getOrLoadMeta.apply(this, arguments);
154
129
  }
155
-
156
130
  return getOrLoadMeta;
157
131
  }()
132
+ }, {
133
+ key: "loadMetaProcess",
134
+ value: function() {
135
+ var _loadMetaProcess = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee4(cache, _ref, onProgress, config) {
136
+ var _this2 = this;
137
+ var _ref2, moovStart, moovEnd, OnProgressHandle;
138
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
139
+ while (1)
140
+ switch (_context4.prev = _context4.next) {
141
+ case 0:
142
+ _ref2 = _slicedToArray(_ref, 2), moovStart = _ref2[0], moovEnd = _ref2[1];
143
+ this._error = false;
144
+ this.logger.debug("[loadMetaProcess start], range,", [moovStart, moovEnd]);
145
+ OnProgressHandle = /* @__PURE__ */ function() {
146
+ var _ref3 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3(data, state, options) {
147
+ var moov, mdat, _moovStart, res, parsedMoov, segments, videoSegments, audioSegments;
148
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
149
+ while (1)
150
+ switch (_context3.prev = _context3.next) {
151
+ case 0:
152
+ if (_this2.meta && options !== null && options !== void 0 && options.range && options.range.length > 0 && options.range[1] >= moovEnd) {
153
+ state = true;
154
+ _this2.logger.debug("[loadMetaProcess],data done,setstate true,[", moovStart, moovEnd, "]");
155
+ }
156
+ if (state && options !== null && options !== void 0 && options.range && options.range.length > 0 && options.range[1] < moovEnd) {
157
+ state = false;
158
+ _this2.logger.debug("[loadMetaProcess],data not done,setstate false,[", moovStart, moovEnd, "]");
159
+ }
160
+ _this2.logger.debug("[loadMetaProcess],task,[", moovStart, moovEnd, "], range,", options.range, ",dataLen,", data ? data.byteLength : void 0, ", state,", state, ",err,", _this2._error);
161
+ !_this2._error && data && data.byteLength > 0 && onProgress(data, state, options);
162
+ if (!(_this2.meta.moov || _this2._error)) {
163
+ _context3.next = 6;
164
+ break;
165
+ }
166
+ return _context3.abrupt("return");
167
+ case 6:
168
+ if (!(data && data.byteLength > 0)) {
169
+ _context3.next = 44;
170
+ break;
171
+ }
172
+ _this2.buffer = concatUint8Array(_this2.buffer, data);
173
+ moov = MP4Parser.findBox(_this2.buffer, ["moov"])[0];
174
+ if (moov) {
175
+ _context3.next = 23;
176
+ break;
177
+ }
178
+ mdat = MP4Parser.findBox(_this2.buffer, ["mdat"])[0];
179
+ if (!state) {
180
+ _context3.next = 23;
181
+ break;
182
+ }
183
+ if (mdat) {
184
+ _context3.next = 18;
185
+ break;
186
+ }
187
+ _this2._error = true;
188
+ onProgress(null, state, options, {
189
+ err: "cannot find moov or mdat box"
190
+ });
191
+ return _context3.abrupt("return");
192
+ case 18:
193
+ _moovStart = mdat.start + mdat.size;
194
+ _context3.next = 21;
195
+ return _this2.loadData([_moovStart, ""], cache, config);
196
+ case 21:
197
+ res = _context3.sent;
198
+ if (res) {
199
+ moov = MP4Parser.findBox(res.data, ["moov"])[0];
200
+ }
201
+ case 23:
202
+ if (!(moov && state && moov.size > moov.data.length)) {
203
+ _context3.next = 27;
204
+ break;
205
+ }
206
+ _this2.logger.debug("[loadMetaProcess],moov not all, range,", options.range[1], ",dataLen,", _this2.buffer.byteLength, ", state,", state);
207
+ _context3.next = 27;
208
+ return _this2.loadMetaProcess(cache, [options.range[1], moov.start + moov.size - 1], onProgress);
209
+ case 27:
210
+ if (!(moov && moov.size <= moov.data.length && !_this2.meta.moov)) {
211
+ _context3.next = 44;
212
+ break;
213
+ }
214
+ parsedMoov = MP4Parser.moov(moov);
215
+ if (parsedMoov) {
216
+ _context3.next = 33;
217
+ break;
218
+ }
219
+ _this2._error = true;
220
+ onProgress(null, state, options, {
221
+ err: "cannot parse moov box"
222
+ });
223
+ return _context3.abrupt("return");
224
+ case 33:
225
+ segments = moovToSegments(parsedMoov, _this2._config.segmentDuration);
226
+ if (segments) {
227
+ _context3.next = 38;
228
+ break;
229
+ }
230
+ _this2._error = true;
231
+ onProgress(null, state, options, {
232
+ err: "cannot parse segments"
233
+ });
234
+ return _context3.abrupt("return");
235
+ case 38:
236
+ _this2.meta = moovToMeta(parsedMoov);
237
+ videoSegments = segments.videoSegments, audioSegments = segments.audioSegments;
238
+ _this2.videoSegments = videoSegments;
239
+ _this2.audioSegments = audioSegments;
240
+ _this2.logger.debug("[loadMetaProcess] moov ok");
241
+ onProgress(void 0, state, {
242
+ meta: {
243
+ meta: _this2.meta,
244
+ videoSegments,
245
+ audioSegments
246
+ }
247
+ });
248
+ case 44:
249
+ case "end":
250
+ return _context3.stop();
251
+ }
252
+ }, _callee3);
253
+ }));
254
+ return function OnProgressHandle2(_x7, _x8, _x9) {
255
+ return _ref3.apply(this, arguments);
256
+ };
257
+ }();
258
+ _context4.next = 6;
259
+ return this.loadData([moovStart, moovEnd || this._config.moovEnd], cache, _objectSpread2({
260
+ onProgress: OnProgressHandle
261
+ }, config));
262
+ case 6:
263
+ case "end":
264
+ return _context4.stop();
265
+ }
266
+ }, _callee4, this);
267
+ }));
268
+ function loadMetaProcess(_x3, _x4, _x5, _x6) {
269
+ return _loadMetaProcess.apply(this, arguments);
270
+ }
271
+ return loadMetaProcess;
272
+ }()
158
273
  }, {
159
274
  key: "loadMeta",
160
- value: function () {
161
- var _loadMeta = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(cache, moovEnd) {
275
+ value: function() {
276
+ var _loadMeta = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5(cache, moovEnd, config) {
162
277
  var responses, res, moov, mdat, moovStart, parsedMoov, segments, videoSegments, audioSegments;
163
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
164
- while (1) {
165
- switch (_context3.prev = _context3.next) {
278
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
279
+ while (1)
280
+ switch (_context5.prev = _context5.next) {
166
281
  case 0:
167
282
  responses = [];
168
- _context3.next = 3;
169
- return this._loadData([0, moovEnd || this._config.moovEnd], cache);
170
-
171
- case 3:
172
- res = _context3.sent;
173
-
283
+ this.logger.debug("[loadMeta start]");
284
+ _context5.next = 4;
285
+ return this.loadData([0, moovEnd || this._config.moovEnd], cache, config);
286
+ case 4:
287
+ res = _context5.sent;
174
288
  if (res) {
175
- _context3.next = 6;
289
+ _context5.next = 7;
176
290
  break;
177
291
  }
178
-
179
- return _context3.abrupt("return");
180
-
181
- case 6:
292
+ return _context5.abrupt("return");
293
+ case 7:
182
294
  responses.push(res);
183
- moov = MP4Parser.findBox(res.data, ['moov'])[0];
184
-
295
+ moov = MP4Parser.findBox(res.data, ["moov"])[0];
185
296
  if (moov) {
186
- _context3.next = 22;
297
+ _context5.next = 23;
187
298
  break;
188
299
  }
189
-
190
- mdat = MP4Parser.findBox(res.data, ['mdat'])[0];
191
-
300
+ mdat = MP4Parser.findBox(res.data, ["mdat"])[0];
192
301
  if (mdat) {
193
- _context3.next = 12;
302
+ _context5.next = 13;
194
303
  break;
195
304
  }
196
-
197
- throw new MediaError('cannot find moov or mdat box', res.data);
198
-
199
- case 12:
305
+ throw new MediaError("cannot find moov or mdat box", res.data);
306
+ case 13:
200
307
  moovStart = mdat.start + mdat.size;
201
- _context3.next = 15;
202
- return this._loadData([moovStart], cache);
203
-
204
- case 15:
205
- res = _context3.sent;
206
-
308
+ _context5.next = 16;
309
+ return this.loadData([moovStart], cache, config);
310
+ case 16:
311
+ res = _context5.sent;
207
312
  if (res) {
208
- _context3.next = 18;
313
+ _context5.next = 19;
209
314
  break;
210
315
  }
211
-
212
- return _context3.abrupt("return");
213
-
214
- case 18:
316
+ return _context5.abrupt("return");
317
+ case 19:
215
318
  responses.push(res);
216
- moov = MP4Parser.findBox(res.data, ['moov'], moovStart)[0];
217
-
319
+ moov = MP4Parser.findBox(res.data, ["moov"], moovStart)[0];
218
320
  if (moov) {
219
- _context3.next = 22;
321
+ _context5.next = 23;
220
322
  break;
221
323
  }
222
-
223
- throw new MediaError('cannot find moov box', res.data);
224
-
225
- case 22:
324
+ throw new MediaError("cannot find moov box", res.data);
325
+ case 23:
226
326
  if (!(moov.size > moov.data.length)) {
227
- _context3.next = 30;
327
+ _context5.next = 31;
228
328
  break;
229
329
  }
230
-
231
- _context3.next = 25;
232
- return this._loadData([res.data.length, moov.start + moov.size - 1], cache);
233
-
234
- case 25:
235
- res = _context3.sent;
236
-
330
+ _context5.next = 26;
331
+ return this.loadData([res.data.length, moov.start + moov.size - 1], cache, config);
332
+ case 26:
333
+ res = _context5.sent;
237
334
  if (res) {
238
- _context3.next = 28;
335
+ _context5.next = 29;
239
336
  break;
240
337
  }
241
-
242
- return _context3.abrupt("return");
243
-
244
- case 28:
338
+ return _context5.abrupt("return");
339
+ case 29:
245
340
  responses.push(res);
246
341
  moov.data = concatUint8Array(moov.data, res.data);
247
-
248
- case 30:
342
+ case 31:
249
343
  parsedMoov = MP4Parser.moov(moov);
250
-
251
344
  if (parsedMoov) {
252
- _context3.next = 33;
345
+ _context5.next = 34;
253
346
  break;
254
347
  }
255
-
256
- throw new MediaError('cannot parse moov box', moov.data);
257
-
258
- case 33:
348
+ throw new MediaError("cannot parse moov box", moov.data);
349
+ case 34:
259
350
  segments = moovToSegments(parsedMoov, this._config.segmentDuration);
260
-
261
351
  if (segments) {
262
- _context3.next = 36;
352
+ _context5.next = 37;
263
353
  break;
264
354
  }
265
-
266
- throw new MediaError('cannot parse segments', moov.data);
267
-
268
- case 36:
355
+ throw new MediaError("cannot parse segments", moov.data);
356
+ case 37:
269
357
  this.meta = moovToMeta(parsedMoov);
270
358
  videoSegments = segments.videoSegments, audioSegments = segments.audioSegments;
271
359
  this.videoSegments = videoSegments;
272
360
  this.audioSegments = audioSegments;
273
- return _context3.abrupt("return", {
361
+ this.logger.debug("[load moov end!!!!!]");
362
+ return _context5.abrupt("return", {
274
363
  meta: this.meta,
275
- videoSegments: videoSegments,
276
- audioSegments: audioSegments,
277
- responses: responses
364
+ videoSegments,
365
+ audioSegments,
366
+ responses
278
367
  });
279
-
280
- case 41:
368
+ case 43:
281
369
  case "end":
282
- return _context3.stop();
370
+ return _context5.stop();
283
371
  }
284
- }
285
- }, _callee3, this);
372
+ }, _callee5, this);
286
373
  }));
287
-
288
- function loadMeta(_x3, _x4) {
374
+ function loadMeta(_x10, _x11, _x12) {
289
375
  return _loadMeta.apply(this, arguments);
290
376
  }
291
-
292
377
  return loadMeta;
293
378
  }()
379
+ }, {
380
+ key: "loadCacheMeta",
381
+ value: function loadCacheMeta(meta, segmentIndex) {
382
+ var moov = meta.moov;
383
+ var segments = moovToSegments(moov, this._config.segmentDuration);
384
+ var videoSegments = segments.videoSegments, audioSegments = segments.audioSegments;
385
+ this.videoSegments = videoSegments;
386
+ this.audioSegments = audioSegments;
387
+ this._currentSegmentIndex = segmentIndex;
388
+ this.meta = meta;
389
+ }
294
390
  }, {
295
391
  key: "getSegmentByTime",
296
392
  value: function getSegmentByTime(time) {
297
393
  var video;
298
394
  var audio;
299
-
300
395
  if (!this.videoSegments.length) {
301
- audio = this.audioSegments.find(function (x) {
396
+ audio = this.audioSegments.find(function(x) {
302
397
  return x.startTime <= time && x.endTime > time;
303
398
  });
304
399
  } else {
305
- video = this.videoSegments.find(function (x) {
400
+ video = this.videoSegments.find(function(x) {
306
401
  return x.startTime <= time && x.endTime > time;
307
402
  });
308
-
309
403
  if (video) {
310
404
  audio = this.audioSegments[video.index];
311
405
  }
312
406
  }
313
-
314
407
  return {
315
- video: video,
316
- audio: audio
408
+ video,
409
+ audio
317
410
  };
318
411
  }
319
412
  }, {
320
413
  key: "loadSegmentByTime",
321
- value: function () {
322
- var _loadSegmentByTime = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(time, cache) {
323
- var changeCurrent,
324
- _this$getSegmentByTim,
325
- video,
326
- audio,
327
- _args4 = arguments;
328
-
329
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
330
- while (1) {
331
- switch (_context4.prev = _context4.next) {
414
+ value: function() {
415
+ var _loadSegmentByTime = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee6(time, cache) {
416
+ var changeCurrent, config, _this$getSegmentByTim, video, audio, _args6 = arguments;
417
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
418
+ while (1)
419
+ switch (_context6.prev = _context6.next) {
332
420
  case 0:
333
- changeCurrent = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : true;
334
-
421
+ changeCurrent = _args6.length > 2 && _args6[2] !== void 0 ? _args6[2] : true;
422
+ config = _args6.length > 3 && _args6[3] !== void 0 ? _args6[3] : {};
335
423
  if (this.isMetaLoaded) {
336
- _context4.next = 4;
424
+ _context6.next = 5;
337
425
  break;
338
426
  }
339
-
340
- _context4.next = 4;
427
+ _context6.next = 5;
341
428
  return this.loadMeta(cache);
342
-
343
- case 4:
429
+ case 5:
344
430
  _this$getSegmentByTim = this.getSegmentByTime(time), video = _this$getSegmentByTim.video, audio = _this$getSegmentByTim.audio;
345
- return _context4.abrupt("return", this._loadSegment(video, audio, cache, changeCurrent));
346
-
347
- case 6:
431
+ return _context6.abrupt("return", this._loadSegment(video, audio, cache, changeCurrent, config));
432
+ case 7:
348
433
  case "end":
349
- return _context4.stop();
434
+ return _context6.stop();
350
435
  }
351
- }
352
- }, _callee4, this);
436
+ }, _callee6, this);
353
437
  }));
354
-
355
- function loadSegmentByTime(_x5, _x6) {
438
+ function loadSegmentByTime(_x13, _x14) {
356
439
  return _loadSegmentByTime.apply(this, arguments);
357
440
  }
358
-
359
441
  return loadSegmentByTime;
360
442
  }()
361
443
  }, {
362
444
  key: "loadNextSegment",
363
- value: function () {
364
- var _loadNextSegment = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(cache) {
365
- var changeCurrent,
366
- video,
367
- audio,
368
- _args5 = arguments;
369
- return _regeneratorRuntime.wrap(function _callee5$(_context5) {
370
- while (1) {
371
- switch (_context5.prev = _context5.next) {
445
+ value: function() {
446
+ var _loadNextSegment = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee7(cache) {
447
+ var changeCurrent, config, video, audio, _args7 = arguments;
448
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
449
+ while (1)
450
+ switch (_context7.prev = _context7.next) {
372
451
  case 0:
373
- changeCurrent = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : true;
374
-
452
+ changeCurrent = _args7.length > 1 && _args7[1] !== void 0 ? _args7[1] : true;
453
+ config = _args7.length > 2 && _args7[2] !== void 0 ? _args7[2] : {};
375
454
  if (this.isMetaLoaded) {
376
- _context5.next = 4;
455
+ _context7.next = 5;
377
456
  break;
378
457
  }
379
-
380
- _context5.next = 4;
458
+ _context7.next = 5;
381
459
  return this.loadMeta();
382
-
383
- case 4:
460
+ case 5:
384
461
  video = this.videoSegments[this._currentSegmentIndex + 1];
385
462
  audio = this.audioSegments[this._currentSegmentIndex + 1];
386
- return _context5.abrupt("return", this._loadSegment(video, audio, cache, changeCurrent));
387
-
388
- case 7:
463
+ return _context7.abrupt("return", this._loadSegment(video, audio, cache, changeCurrent, config));
464
+ case 8:
389
465
  case "end":
390
- return _context5.stop();
466
+ return _context7.stop();
391
467
  }
392
- }
393
- }, _callee5, this);
468
+ }, _callee7, this);
394
469
  }));
395
-
396
- function loadNextSegment(_x7) {
470
+ function loadNextSegment(_x15) {
397
471
  return _loadNextSegment.apply(this, arguments);
398
472
  }
399
-
400
473
  return loadNextSegment;
401
474
  }()
402
475
  }, {
403
476
  key: "preload",
404
- value: function () {
405
- var _preload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(time) {
406
- var _this = this;
407
-
477
+ value: function() {
478
+ var _preload = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee9(time) {
479
+ var _this3 = this;
408
480
  var _this$getSegmentByTim2, video, audio, index, videos, audios, load;
409
-
410
- return _regeneratorRuntime.wrap(function _callee7$(_context7) {
411
- while (1) {
412
- switch (_context7.prev = _context7.next) {
481
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
482
+ while (1)
483
+ switch (_context9.prev = _context9.next) {
413
484
  case 0:
414
485
  if (this.isMetaLoaded) {
415
- _context7.next = 3;
486
+ _context9.next = 3;
416
487
  break;
417
488
  }
418
-
419
- _context7.next = 3;
489
+ _context9.next = 3;
420
490
  return this.loadMeta(true);
421
-
422
491
  case 3:
423
492
  if (!(!time || time < 0)) {
424
- _context7.next = 5;
493
+ _context9.next = 5;
425
494
  break;
426
495
  }
427
-
428
- return _context7.abrupt("return");
429
-
496
+ return _context9.abrupt("return");
430
497
  case 5:
431
498
  _this$getSegmentByTim2 = this.getSegmentByTime(time), video = _this$getSegmentByTim2.video, audio = _this$getSegmentByTim2.audio;
432
499
  index = Math.max((video === null || video === void 0 ? void 0 : video.index) || 0, (audio === null || audio === void 0 ? void 0 : audio.index) || 0);
433
-
434
500
  if (index) {
435
- _context7.next = 9;
501
+ _context9.next = 9;
436
502
  break;
437
503
  }
438
-
439
- return _context7.abrupt("return");
440
-
504
+ return _context9.abrupt("return");
441
505
  case 9:
442
506
  videos = this.videoSegments.slice(0, index);
443
507
  audios = this.audioSegments.slice(0, index);
444
-
445
- load = /*#__PURE__*/function () {
446
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(i) {
447
- return _regeneratorRuntime.wrap(function _callee6$(_context6) {
448
- while (1) {
449
- switch (_context6.prev = _context6.next) {
508
+ load = /* @__PURE__ */ function() {
509
+ var _ref4 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee8(i) {
510
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
511
+ while (1)
512
+ switch (_context8.prev = _context8.next) {
450
513
  case 0:
451
514
  if (!(i > index)) {
452
- _context6.next = 2;
515
+ _context8.next = 2;
453
516
  break;
454
517
  }
455
-
456
- return _context6.abrupt("return");
457
-
518
+ return _context8.abrupt("return");
458
519
  case 2:
459
- _context6.next = 4;
460
- return _this._loadSegment(videos[i], audios[i], true, false);
461
-
520
+ _context8.next = 4;
521
+ return _this3._loadSegment(videos[i], audios[i], true, false);
462
522
  case 4:
463
- _context6.next = 6;
523
+ _context8.next = 6;
464
524
  return load(i + 1);
465
-
466
525
  case 6:
467
526
  case "end":
468
- return _context6.stop();
527
+ return _context8.stop();
469
528
  }
470
- }
471
- }, _callee6);
529
+ }, _callee8);
472
530
  }));
473
-
474
- return function load(_x9) {
475
- return _ref.apply(this, arguments);
531
+ return function load2(_x17) {
532
+ return _ref4.apply(this, arguments);
476
533
  };
477
534
  }();
478
-
479
- _context7.next = 14;
535
+ _context9.next = 14;
480
536
  return load(0);
481
-
482
537
  case 14:
483
538
  case "end":
484
- return _context7.stop();
539
+ return _context9.stop();
485
540
  }
486
- }
487
- }, _callee7, this);
541
+ }, _callee9, this);
488
542
  }));
489
-
490
- function preload(_x8) {
543
+ function preload(_x16) {
491
544
  return _preload.apply(this, arguments);
492
545
  }
493
-
494
546
  return preload;
495
547
  }()
496
548
  }, {
@@ -500,216 +552,187 @@ var MP4Loader = /*#__PURE__*/function () {
500
552
  }
501
553
  }, {
502
554
  key: "reset",
503
- value: function () {
504
- var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
505
- return _regeneratorRuntime.wrap(function _callee8$(_context8) {
506
- while (1) {
507
- switch (_context8.prev = _context8.next) {
555
+ value: function() {
556
+ var _reset = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee10() {
557
+ var notCancelLoader, _args10 = arguments;
558
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
559
+ while (1)
560
+ switch (_context10.prev = _context10.next) {
508
561
  case 0:
509
- _context8.next = 2;
562
+ notCancelLoader = _args10.length > 0 && _args10[0] !== void 0 ? _args10[0] : false;
563
+ if (notCancelLoader) {
564
+ _context10.next = 5;
565
+ break;
566
+ }
567
+ this.logger.debug("[MP4loader reset func call loader.cancel]");
568
+ _context10.next = 5;
510
569
  return this._loader.cancel();
511
-
512
- case 2:
513
- this.vid = this.url = '';
570
+ case 5:
571
+ this.vid = this.url = "";
514
572
  this.meta = {};
515
573
  this.downloadInfo = [];
516
574
  this.videoSegments = [];
517
575
  this.audioSegments = [];
518
576
  this._currentSegmentIndex = -1;
519
577
  this._currentLoadingSegmentIndex = -1;
520
-
521
- case 9:
578
+ case 12:
522
579
  case "end":
523
- return _context8.stop();
580
+ return _context10.stop();
524
581
  }
525
- }
526
- }, _callee8, this);
582
+ }, _callee10, this);
527
583
  }));
528
-
529
584
  function reset() {
530
585
  return _reset.apply(this, arguments);
531
586
  }
532
-
533
587
  return reset;
534
588
  }()
535
589
  }, {
536
590
  key: "destroy",
537
- value: function () {
538
- var _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
539
- return _regeneratorRuntime.wrap(function _callee9$(_context9) {
540
- while (1) {
541
- switch (_context9.prev = _context9.next) {
591
+ value: function() {
592
+ var _destroy = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee11() {
593
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
594
+ while (1)
595
+ switch (_context11.prev = _context11.next) {
542
596
  case 0:
543
- _context9.next = 2;
544
- return this.cancel();
545
-
597
+ _context11.next = 2;
598
+ return this.reset();
546
599
  case 2:
547
600
  this.cache.clear();
548
-
549
601
  case 3:
550
602
  case "end":
551
- return _context9.stop();
603
+ return _context11.stop();
552
604
  }
553
- }
554
- }, _callee9, this);
605
+ }, _callee11, this);
555
606
  }));
556
-
557
607
  function destroy() {
558
608
  return _destroy.apply(this, arguments);
559
609
  }
560
-
561
610
  return destroy;
562
611
  }()
563
612
  }, {
564
613
  key: "_loadSegment",
565
- value: function () {
566
- var _loadSegment2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(video, audio, cache, changeCurrent) {
614
+ value: function() {
615
+ var _loadSegment2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee12(video, audio, cache, changeCurrent, config) {
567
616
  var segIndex, res;
568
- return _regeneratorRuntime.wrap(function _callee10$(_context10) {
569
- while (1) {
570
- switch (_context10.prev = _context10.next) {
617
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
618
+ while (1)
619
+ switch (_context12.prev = _context12.next) {
571
620
  case 0:
572
621
  if (!(!video && !audio)) {
573
- _context10.next = 2;
622
+ _context12.next = 2;
574
623
  break;
575
624
  }
576
-
577
- return _context10.abrupt("return");
578
-
625
+ return _context12.abrupt("return");
579
626
  case 2:
580
627
  segIndex = (video === null || video === void 0 ? void 0 : video.index) || (audio === null || audio === void 0 ? void 0 : audio.index) || 0;
581
628
  this._currentLoadingSegmentIndex = segIndex;
582
- _context10.prev = 4;
583
- _context10.next = 7;
584
- return this._loadData([Math.min((video === null || video === void 0 ? void 0 : video.range[0]) || Infinity, (audio === null || audio === void 0 ? void 0 : audio.range[0]) || Infinity), Math.max((video === null || video === void 0 ? void 0 : video.range[1]) || 0, (audio === null || audio === void 0 ? void 0 : audio.range[1]) || 0)], cache);
585
-
629
+ _context12.prev = 4;
630
+ _context12.next = 7;
631
+ return this.loadData([Math.min((video === null || video === void 0 ? void 0 : video.range[0]) || Infinity, (audio === null || audio === void 0 ? void 0 : audio.range[0]) || Infinity), Math.max((video === null || video === void 0 ? void 0 : video.range[1]) || 0, (audio === null || audio === void 0 ? void 0 : audio.range[1]) || 0)], cache, config);
586
632
  case 7:
587
- res = _context10.sent;
588
-
633
+ res = _context12.sent;
589
634
  case 8:
590
- _context10.prev = 8;
635
+ _context12.prev = 8;
591
636
  this._currentLoadingSegmentIndex = -1;
592
- return _context10.finish(8);
593
-
637
+ return _context12.finish(8);
594
638
  case 11:
595
639
  if (res) {
596
- _context10.next = 13;
640
+ _context12.next = 13;
597
641
  break;
598
642
  }
599
-
600
- return _context10.abrupt("return");
601
-
643
+ return _context12.abrupt("return");
602
644
  case 13:
603
645
  if (changeCurrent) {
604
646
  this._currentSegmentIndex = segIndex;
605
647
  }
606
-
607
648
  res.video = video;
608
649
  res.audio = audio;
609
- return _context10.abrupt("return", res);
610
-
650
+ return _context12.abrupt("return", res);
611
651
  case 17:
612
652
  case "end":
613
- return _context10.stop();
653
+ return _context12.stop();
614
654
  }
615
- }
616
- }, _callee10, this, [[4,, 8, 11]]);
655
+ }, _callee12, this, [[4, , 8, 11]]);
617
656
  }));
618
-
619
- function _loadSegment(_x10, _x11, _x12, _x13) {
657
+ function _loadSegment(_x18, _x19, _x20, _x21, _x22) {
620
658
  return _loadSegment2.apply(this, arguments);
621
659
  }
622
-
623
660
  return _loadSegment;
624
661
  }()
625
662
  }, {
626
- key: "_loadData",
627
- value: function () {
628
- var _loadData2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(range, cache) {
629
- var cacheKey, data, res;
630
- return _regeneratorRuntime.wrap(function _callee11$(_context11) {
631
- while (1) {
632
- switch (_context11.prev = _context11.next) {
663
+ key: "loadData",
664
+ value: function() {
665
+ var _loadData = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee13(range, cache, config) {
666
+ var cacheKey, data, res, url;
667
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
668
+ while (1)
669
+ switch (_context13.prev = _context13.next) {
633
670
  case 0:
634
671
  cacheKey = this._getCacheKey(range);
635
- _context11.next = 3;
672
+ _context13.next = 3;
636
673
  return this.cache.get(cacheKey);
637
-
638
674
  case 3:
639
- data = _context11.sent;
640
-
675
+ data = _context13.sent;
641
676
  if (data) {
642
- _context11.next = 10;
677
+ _context13.next = 11;
643
678
  break;
644
679
  }
645
-
646
- _context11.next = 7;
647
- return this._loader.load(this.url, {
648
- range: range
649
- });
650
-
651
- case 7:
652
- res = _context11.sent;
653
- _context11.next = 11;
680
+ url = config && config.url ? config.url : this.url;
681
+ _context13.next = 8;
682
+ return this._loader.load(url, _objectSpread2({
683
+ range,
684
+ vid: this.vid
685
+ }, config));
686
+ case 8:
687
+ res = _context13.sent;
688
+ _context13.next = 12;
654
689
  break;
655
-
656
- case 10:
690
+ case 11:
657
691
  res = {
658
- data: data,
659
- fromCache: true
692
+ data,
693
+ state: true,
694
+ options: {
695
+ fromCache: true,
696
+ range,
697
+ vid: this.vid
698
+ }
660
699
  };
661
-
662
- case 11:
700
+ case 12:
663
701
  if (res) {
664
- _context11.next = 13;
702
+ _context13.next = 14;
665
703
  break;
666
704
  }
667
-
668
- return _context11.abrupt("return");
669
-
670
- case 13:
705
+ return _context13.abrupt("return");
706
+ case 14:
671
707
  if (!data) {
672
- this.downloadInfo.push({
708
+ res.data && this.downloadInfo.push({
673
709
  startTime: res.startTime,
674
710
  endTime: res.endTime,
675
711
  size: res.data.byteLength,
676
- range: range
712
+ range
677
713
  });
678
-
679
- if (this._addDownloadInfo > this._config.maxDownloadInfoSize) {
680
- this._downloadInfos = this.downloadInfo.slice(-this._config.maxDownloadInfoSize);
714
+ if (this.downloadInfo && this.downloadInfo.length > this._config.maxDownloadInfoSize) {
715
+ this.downloadInfo = this.downloadInfo.slice(-this._config.maxDownloadInfoSize);
681
716
  }
682
717
  }
683
-
684
- if (!data && cache) {
685
- this.cache.set(cacheKey, res.data);
686
- }
687
-
688
- res.range = range;
689
- return _context11.abrupt("return", res);
690
-
718
+ return _context13.abrupt("return", res);
691
719
  case 17:
692
720
  case "end":
693
- return _context11.stop();
721
+ return _context13.stop();
694
722
  }
695
- }
696
- }, _callee11, this);
723
+ }, _callee13, this);
697
724
  }));
698
-
699
- function _loadData(_x14, _x15) {
700
- return _loadData2.apply(this, arguments);
725
+ function loadData(_x23, _x24, _x25) {
726
+ return _loadData.apply(this, arguments);
701
727
  }
702
-
703
- return _loadData;
728
+ return loadData;
704
729
  }()
705
730
  }, {
706
731
  key: "_getCacheKey",
707
732
  value: function _getCacheKey(range) {
708
- return (this.vid || this.url) + ':' + range;
733
+ return (this.vid || this.url) + ":" + range;
709
734
  }
710
735
  }]);
711
-
712
- return MP4Loader;
713
- }();
714
-
736
+ return MP4Loader2;
737
+ }(EventEmitter);
715
738
  export { MP4Loader };