xgplayer-mp4-loader 0.0.1-alpha.10

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