-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.mjs
5443 lines (4805 loc) · 179 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import _Object$getOwnPropertyDescriptor from 'babel-runtime/core-js/object/get-own-property-descriptor';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import { CustEvent, Log, deepAssign, throttle } from 'chimee-helper';
import _Object$assign from 'babel-runtime/core-js/object/assign';
import work from 'webworkify';
import _typeof from 'babel-runtime/helpers/typeof';
var MSEController = function (_CustEvent) {
_inherits(MSEController, _CustEvent);
/**
* mediasource 控制层
* @class mediasource
* @param {Element} videoElement
* @param {object} config
*/
function MSEController(videoElement, config) {
_classCallCheck(this, MSEController);
var _this = _possibleConstructorReturn(this, (MSEController.__proto__ || _Object$getPrototypeOf(MSEController)).call(this));
_this.video = videoElement;
_this.config = config;
_this.tag = 'mse-controller';
_this.e = {
onSourceOpen: _this.onSourceOpen.bind(_this),
onSourceEnded: _this.onSourceEnded.bind(_this),
onSourceClose: _this.onSourceClose.bind(_this),
onSourceBufferError: _this.onSourceBufferError.bind(_this)
};
_this.queue = [];
_this.removeRangesList = [];
_this.removeBucketing = false;
_this.mimeCodec = 'video/mp4; codecs="avc1.640020,mp4a.40.2"';
_this.init();
return _this;
}
/**
* 初始化 控制层
*/
_createClass(MSEController, [{
key: 'init',
value: function init() {
if (this.mediaSource) {
Log.Error(this.tag, 'MediaSource has been attached to an HTMLMediaElement!');
throw new Error('MediaSource has been attached to an HTMLMediaElement!');
}
var ms = this.mediaSource = new window.MediaSource();
ms.addEventListener('sourceopen', this.e.onSourceOpen);
ms.addEventListener('sourceended', this.e.onSourceEnded);
ms.addEventListener('sourceclose', this.e.onSourceClose);
}
/**
* mediaSource open
*/
}, {
key: 'onSourceOpen',
value: function onSourceOpen() {
var _this2 = this;
Log.verbose(this.tag, 'MediaSource onSourceOpen');
this.mediaSource.removeEventListener('sourceopen', this.e.onSourceOpen);
window.sou = this.sourceBuffer = this.mediaSource.addSourceBuffer(this.mimeCodec);
this.sourceBuffer.addEventListener('error', this.e.onSourceBufferError);
this.sourceBuffer.addEventListener('abort', function () {
return console.log('sourceBuffer: abort');
});
this.sourceBuffer.addEventListener('updateend', function () {
if (_this2.queue.length >= 1) {
if (!_this2.sourceBuffer.updating) {
if (_this2.needCleanupSourceBuffer()) {
_this2.doCleanupSourceBuffer();
} else {
var data = _this2.queue.shift();
_this2.appendBuffer(data);
}
}
}
_this2.emit('updateend');
});
this.emit('source_open');
this.sourceBufferEvent();
}
/**
* sourceBuffer 事件
*/
}, {
key: 'sourceBufferEvent',
value: function sourceBufferEvent() {
var _this3 = this;
this.on('mediaSegment', function (handler) {
var data = handler.data;
// if(this.needCleanupSourceBuffer()) {
// this.doCleanupSourceBuffer();
// }
if (_this3.sourceBuffer.updating || _this3.queue.length > 0) {
// console.log(new Uint8Array(data));
_this3.queue.push(data);
} else {
_this3.appendBuffer(data);
}
});
this.on('mediaSegmentInit', function (handler) {
var data = handler.data;
if (_this3.sourceBuffer.updating || _this3.queue.length > 0) {
_this3.queue.push(data);
} else {
_this3.appendBuffer(data);
}
});
}
/**
* 是否需要清除sourcebuffer 里的buffer
*/
}, {
key: 'needCleanupSourceBuffer',
value: function needCleanupSourceBuffer() {
var currentTime = this.video.currentTime;
var sb = this.sourceBuffer;
var buffered = sb.buffered;
if (buffered.length >= 1) {
if (currentTime - buffered.start(0) >= this.config.autoCleanupMaxBackwardDuration) {
return true;
}
}
return false;
}
/**
* 清除buffer
*/
}, {
key: 'doCleanupSourceBuffer',
value: function doCleanupSourceBuffer() {
Log.verbose(this.tag, 'docleanBuffer');
var currentTime = this.video.currentTime;
var sb = this.sourceBuffer;
var buffered = sb.buffered;
var doRemove = false;
for (var i = 0; i < buffered.length; i++) {
var start = buffered.start(i);
var end = buffered.end(i);
if (start <= currentTime && currentTime < end + 3) {
if (currentTime - start >= this.config.autoCleanupMaxBackwardDuration) {
doRemove = true;
var removeEnd = currentTime - this.config.autoCleanupMinBackwardDuration;
this.removeRangesList.push({ start: start, end: removeEnd });
}
} else if (end < currentTime) {
doRemove = true;
this.removeRangesList.push({ start: start, end: end });
}
}
if (doRemove && !this.sourceBuffer.updating) {
this.cleanRangesList();
}
}
/**
* 清除bufferlist
*/
}, {
key: 'cleanRangesList',
value: function cleanRangesList() {
if (this.sourceBuffer.updating) {
return;
}
var sb = this.sourceBuffer;
while (this.removeRangesList.length && !sb.updating) {
var ranges = this.removeRangesList.shift();
sb.remove(ranges.start, ranges.end);
}
}
/**
* 往sourcebuffer里添加数据
*/
}, {
key: 'appendBuffer',
value: function appendBuffer(data) {
try {
this.sourceBuffer.appendBuffer(data);
} catch (e) {
if (e.code === 22) {
// chrome 大概会有350M
Log.verbose(this.TAG, 'MediaSource bufferFull');
this.emit('bufferFull');
}
}
}
/**
* sourcebuffer 结束
*/
}, {
key: 'onSourceEnded',
value: function onSourceEnded() {
Log.verbose(this.TAG, 'MediaSource onSourceEnded');
}
/**
* sourcebuffer 关闭
*/
}, {
key: 'onSourceClose',
value: function onSourceClose() {
Log.verbose(this.tag, 'MediaSource onSourceClose');
if (this.mediaSource && this.e !== null) {
this.mediaSource.removeEventListener('sourceopen', this.e.onSourceOpen);
this.mediaSource.removeEventListener('sourceended', this.e.onSourceEnded);
this.mediaSource.removeEventListener('sourceclose', this.e.onSourceClose);
}
}
/**
* sourcebuffer 错误
*/
}, {
key: 'onSourceBufferError',
value: function onSourceBufferError(e) {
Log.Error(this.tag, 'SourceBuffer Error: ' + e);
}
/**
* seek
*/
}, {
key: 'seek',
value: function seek() {}
/**
* 销毁
*/
}, {
key: 'destroy',
value: function destroy() {
if (this.mediaSource) {
var ms = this.mediaSource;
// pending segments should be discard
this.queue = [];
// remove all sourcebuffers
var sb = this.sourceBuffer;
if (sb) {
if (ms.readyState !== 'closed') {
ms.removeSourceBuffer(sb);
sb.removeEventListener('error', this.e.onSourceBufferError);
sb.removeEventListener('updateend', this.e.onSourceBufferUpdateEnd);
}
this.sourceBuffer = null;
}
if (ms.readyState === 'open') {
try {
ms.endOfStream();
} catch (error) {
Log.e(this.TAG, error.message);
}
}
ms.removeEventListener('sourceopen', this.e.onSourceOpen);
ms.removeEventListener('sourceended', this.e.onSourceEnded);
ms.removeEventListener('sourceclose', this.e.onSourceClose);
this.mediaSource = null;
}
if (this._mediaElement) {
this._mediaElement.src = '';
this._mediaElement.removeAttribute('src');
this._mediaElement = null;
}
if (this._mediaSourceObjectURL) {
window.URL.revokeObjectURL(this._mediaSourceObjectURL);
this._mediaSourceObjectURL = null;
}
}
}]);
return MSEController;
}(CustEvent);
/**
* 处理range的静态函数
* author songguangyu
* emil 522963130@qq.com
*/
var handleRange = function (range) {
var headers = {};
var param = void 0;
if (range.to !== -1) {
param = 'bytes=' + range.from.toString() + '-' + range.to.toString();
} else {
param = 'bytes=' + range.from.toString() + '-';
}
headers['Range'] = param;
return {
headers: headers
};
};
/**
* fetch firfox 直播 点播
* author songguangyu
* emil 522963130@qq.com
*/
/**
* FetchLoader
* @class FetchLoader
* @param {string} video url
* @param {object} range.from range.to
*/
var FetchLoader = function (_CustEvent) {
_inherits(FetchLoader, _CustEvent);
_createClass(FetchLoader, null, [{
key: 'isSupport',
/**
* broswer is support moz-chunk
*/
value: function isSupport() {
if (window.fetch && window.ReadableStream) {
return true;
} else {
return false;
}
}
}]);
function FetchLoader(src, config) {
_classCallCheck(this, FetchLoader);
var _this = _possibleConstructorReturn(this, (FetchLoader.__proto__ || _Object$getPrototypeOf(FetchLoader)).call(this));
_this.tag = 'fetch';
_this.fetching = false;
_this.config = config;
_this.range = {
from: 0,
to: 500000
};
_this.src = src;
_this.totalRange = null;
_this.block = 500000;
_this.reader = null;
_this.requestAbort = false;
_this.arrivalDataCallback = null;
_this.bytesStart = 0;
return _this;
}
/**
* if don't need range don't set
* @param {object} range.from range.to
*/
_createClass(FetchLoader, [{
key: 'open',
value: function open(range, keyframePoint) {
var _this2 = this;
this.requestAbort = false;
var reqHeaders = new Headers();
var r = range || { from: 0, to: -1 };
if (!this.config.isLive) {
this.range.from = r.from;
this.range.to = r.to;
var headers = handleRange(r).headers;
for (var i in headers) {
reqHeaders.append(i, headers[i]);
}
}
if (keyframePoint) {
this.bytesStart = 0;
}
this.req = new Request(this.src, { headers: reqHeaders });
fetch(this.req).then(function (res) {
if (res.ok) {
var reader = res.body.getReader();
return _this2.pump(reader, keyframePoint);
}
});
}
/**
* pause video
*/
}, {
key: 'pause',
value: function pause() {
this.requestAbort = true;
}
/**
* pump data
*/
}, {
key: 'pump',
value: function pump(reader, keyframePoint) {
var _this3 = this;
// ReadableStreamReader
return reader.read().then(function (result) {
if (result.done) {
Log.verbose(_this3.tag, 'play end');
// trigger complete
} else {
if (_this3.requestAbort === true) {
return reader.cancel();
}
var chunk = result.value.buffer;
if (_this3.arrivalDataCallback) {
_this3.arrivalDataCallback(chunk, _this3.bytesStart, keyframePoint);
_this3.bytesStart += chunk.byteLength;
}
return _this3.pump(reader);
}
});
}
}]);
return FetchLoader;
}(CustEvent);
/**
* XHR 点播
* author songguangyu
* emil 522963130@qq.com
*/
// import Log from 'helper/log';
/**
* MozChunkLoader
* @class MozChunkLoader
* @param {string} video url
* @param {object} range.from range.to
*/
var RangeLoader = function (_CustEvent) {
_inherits(RangeLoader, _CustEvent);
_createClass(RangeLoader, null, [{
key: 'isSupport',
/**
* broswer is support XMLHttpRequest
*/
value: function isSupport() {
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com', true);
xhr.responseType = 'arraybuffer';
return xhr.responseType === 'arraybuffer';
} catch (e) {
return false;
}
}
}]);
function RangeLoader(src, config) {
_classCallCheck(this, RangeLoader);
var _this = _possibleConstructorReturn(this, (RangeLoader.__proto__ || _Object$getPrototypeOf(RangeLoader)).call(this));
_this.tag = 'RangeLoader';
_this.xhr = null;
_this.src = src;
_this.totalLength = null;
_this.chunkSizeKB = 393216;
_this.range = {};
_this.bytesStart = 0;
return _this;
}
/**
* if don't need range don't set
* @param {object} range.from range.to
*/
_createClass(RangeLoader, [{
key: 'open',
value: function open(range) {
var xhr = this.xhr = new XMLHttpRequest();
xhr.open('GET', this.src, true);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = this.onReadyStateChange.bind(this);
xhr.onprogress = this.onProgress.bind(this);
xhr.onload = this.onLoad.bind(this);
xhr.onerror = this.onXhrError.bind(this);
var r = range || { from: 0, to: -1 };
this.range.from = r.from;
this.range.to = r.to;
var headers = handleRange(r).headers;
for (var i in headers) {
xhr.setRequestHeader(i, headers[i]);
}
xhr.send();
}
/**
* abort request
*/
}, {
key: 'abort',
value: function abort() {
this.xhr.onreadystatechange = null;
this.xhr.onprogress = null;
this.xhr.onload = null;
this.xhr.onerror = null;
this.xhr.abort();
this.xhr = null;
}
/**
* destroy xhr Object clean cache
*/
}, {
key: 'destroy',
value: function destroy() {
if (this.xhr) {
this.abort();
this.xhr.onreadystatechange = null;
this.xhr.onprogress = null;
this.xhr.onload = null;
this.xhr.onerror = null;
this.xhr = null;
}
this.totalLength = null;
this.bytesStart = null;
this.range = {};
}
/**
* xhr onReadyStateChange
*/
}, {
key: 'onReadyStateChange',
value: function onReadyStateChange(e) {
var xhr = this.xhr;
if (xhr.readyState === 2) {
if (xhr.status < 200 && xhr.status > 299) {
var info = {
from: this.range.from,
to: this.range.to,
url: this.src,
msg: 'http Error: http code ' + xhr.status
};
this.emit(this.tag, info);
}
}
}
/**
* xhr onProgress
*/
}, {
key: 'onProgress',
value: function onProgress(e) {
if (!this.totalLength) {
this.totalLength = e.total;
this.abort();
this.open({ from: 0, to: this.chunkSizeKB });
}
}
/**
* xhr onLoad
*/
}, {
key: 'onLoad',
value: function onLoad(e) {
if (!this.totalLength) {
return;
}
if (this.range.to < this.totalLength) {
// this.open({from: this.range.to + 1, to: this.range.to + 1 + this.chunkSizeKB});
}
if (this.arrivalDataCallback) {
var chunk = e.target.response;
this.arrivalDataCallback(chunk, this.bytesStart);
this.bytesStart += chunk.byteLength;
}
}
/**
* xhr onXhrError
*/
}, {
key: 'onXhrError',
value: function onXhrError(e) {
var info = {
from: this.range.from,
to: this.range.to,
url: this.src,
msg: e.constructor.name + ' ' + e.type
};
this.emit(this.tag, info);
}
}]);
return RangeLoader;
}(CustEvent);
/**
* XHR firfox 直播 点播
* author songguangyu
* emil 522963130@qq.com
*/
// import Log from 'helper/log';
/**
* MozChunkLoader
* @class MozChunkLoader
* @param {string} video url
* @param {object} range.from range.to
*/
var MozChunkLoader = function (_CustEvent) {
_inherits(MozChunkLoader, _CustEvent);
_createClass(MozChunkLoader, null, [{
key: 'isSupport',
/**
* broswer is support moz-chunk
*/
value: function isSupport() {
try {
var xhr = new XMLHttpRequest();
// Firefox 37- requires .open() to be called before setting responseType
xhr.open('GET', 'https://example.com', true);
xhr.responseType = 'moz-chunked-arraybuffer';
return xhr.responseType === 'moz-chunked-arraybuffer';
} catch (e) {
return false;
}
}
}]);
function MozChunkLoader(src, config) {
_classCallCheck(this, MozChunkLoader);
var _this = _possibleConstructorReturn(this, (MozChunkLoader.__proto__ || _Object$getPrototypeOf(MozChunkLoader)).call(this));
_this.tag = 'mozChunkLoader';
_this.xhr = null;
_this.src = src;
_this.config = config;
_this.totalLength = null;
_this.chunkSizeKB = 393216;
_this.range = {};
_this.bytesStart = 0;
return _this;
}
/**
* if don't need range don't set
* @param {object} range.from range.to
*/
_createClass(MozChunkLoader, [{
key: 'open',
value: function open(range) {
var xhr = this.xhr = new XMLHttpRequest();
xhr.open('GET', this.src, true);
xhr.responseType = 'moz-chunked-arraybuffer';
xhr.onreadystatechange = this.onReadyStateChange.bind(this);
xhr.onprogress = this.onProgress.bind(this);
xhr.onload = this.onLoadEnd.bind(this);
xhr.onerror = this.onXhrError.bind(this);
if (!this.config.isLive) {
var r = range || { from: 0, to: -1 };
this.range.from = r.from;
this.range.to = r.to;
var headers = handleRange(r).headers;
for (var i in headers) {
xhr.setRequestHeader(i, headers[i]);
}
}
xhr.send();
}
/**
* abort request
*/
}, {
key: 'abort',
value: function abort() {
this.xhr.onreadystatechange = null;
this.xhr.onprogress = null;
this.xhr.onload = null;
this.xhr.onerror = null;
this.xhr.abort();
this.xhr = null;
}
/**
* destroy xhr Object clean cache
*/
}, {
key: 'destroy',
value: function destroy() {
if (this.xhr) {
this.abort();
this.xhr.onreadystatechange = null;
this.xhr.onprogress = null;
this.xhr.onload = null;
this.xhr.onerror = null;
this.xhr = null;
}
this.totalLength = null;
this.bytesStart = null;
this.range = {};
}
/**
* xhr onReadyStateChange
*/
}, {
key: 'onReadyStateChange',
value: function onReadyStateChange(e) {
var xhr = this.xhr;
if (xhr.readyState === 2) {
if (xhr.status < 200 && xhr.status > 299) {
var info = {
from: this.range.from,
to: this.range.to,
url: this.src,
msg: 'http Error: http code ' + xhr.status
};
this.emit(this.tag, info);
}
}
}
/**
* xhr onProgress
*/
}, {
key: 'onProgress',
value: function onProgress(e) {
if (!this.totalLength) {
this.totalLength = e.total;
if (e.total !== null && e.total !== 0) {
this.totalLength = e.total;
}
}
var chunk = e.target.response;
this.arrivalDataCallback(chunk, this.bytesStart);
this.bytesStart += chunk.byteLength;
}
/**
* xhr onLoadEnd
*/
}, {
key: 'onLoadEnd',
value: function onLoadEnd(e) {
this.emit(this.tag, 'video load end');
}
/**
* xhr onXhrError
*/
}, {
key: 'onXhrError',
value: function onXhrError(e) {
var info = {
from: this.range.from,
to: this.range.to,
url: this.src,
msg: e.constructor.name + ' ' + e.type
};
this.emit(this.tag, info);
}
}]);
return MozChunkLoader;
}(CustEvent);
/**
* 处理range的静态函数
* author songguangyu
* emil 522963130@qq.com
*/
/**
* Ioloader 处理io的调用器 缓存多余数据
* @class Ioloader
* @param {object} video config
*/
var Ioloader = function (_CustEvent) {
_inherits(Ioloader, _CustEvent);
function Ioloader(config) {
_classCallCheck(this, Ioloader);
var _this = _possibleConstructorReturn(this, (Ioloader.__proto__ || _Object$getPrototypeOf(Ioloader)).call(this));
_this.loader = null;
_this.config = {};
_Object$assign(_this.config, config);
_this.selectLoader();
_this.bufferSize = 1024 * 1024 * 3; // initial size: 3MB
_this.cacheBuffer = new ArrayBuffer(_this.bufferSize);
_this.cacheRemain = 0;
_this.stashByteStart = 0;
_this.enableStash = true;
_this.stashSize = 1024 * 384;
_this.resumeFrom = 0;
_this.currentRange = {};
_this.totalReceive = 0;
_this.seekPonit = 0;
_this.timer = null;
return _this;
}
/**
* 自动选择io处理器
*/
_createClass(Ioloader, [{
key: 'selectLoader',
value: function selectLoader() {
var config = this.config;
var url = this.config.src;
if (FetchLoader.isSupport()) {
this.loader = new FetchLoader(url, config);
} else if (MozChunkLoader.isSupport()) {
this.loader = new MozChunkLoader(url, config);
} else if (RangeLoader.isSupport()) {
this.loader = new RangeLoader(url, config);
}
this.loader.arrivalDataCallback = this.onLoaderChunkArrival.bind(this);
}
/**
* 数据接收器
* @param {arrayBuffer} chunk data
* @param {number} chunk byte postion
*/
}, {
key: 'onLoaderChunkArrival',
value: function onLoaderChunkArrival(chunk, byteStart, keyframePoint) {
if (keyframePoint) {
this.seekPonit = keyframePoint;
}
if (this.arrivalDataCallback) {
this.totalReceive += chunk.byteLength;
if (this.cacheRemain === 0 && this.stashByteStart === 0) {
// This is the first chunk after seek action
this.stashByteStart = byteStart;
}
if (this.cacheRemain + chunk.byteLength <= this.stashSize) {
// 小于cache大小 则看做数据太小 进行缓存 不进行下发
var stashArray = new Uint8Array(this.cacheBuffer, 0, this.stashSize);
stashArray.set(new Uint8Array(chunk), this.cacheRemain);
this.cacheRemain += chunk.byteLength;
} else {
// 大于cache大小的 则把数据放入播放器 溢出数据进行缓存
var _stashArray = new Uint8Array(this.cacheBuffer, 0, this.bufferSize);
if (this.cacheRemain > 0) {
// There're stash datas in buffer
// dispatch the whole stashBuffer, and stash remain data
// then append chunk to stashBuffer (stash)
var buffer = this.cacheBuffer.slice(0, this.cacheRemain);
var consumed = 0;
if (this.seekPonit) {
consumed = this.arrivalDataCallback(buffer, this.stashByteStart, this.seekPonit);
this.seekPonit = 0;
} else {
consumed = this.arrivalDataCallback(buffer, this.stashByteStart);
}
// const consumed = this.arrivalDataCallback(buffer, this.stashByteStart, keyframePoint);
if (consumed < buffer.byteLength) {
if (consumed > 0) {
var remainArray = new Uint8Array(buffer, consumed);
_stashArray.set(remainArray, 0);
this.cacheRemain = remainArray.byteLength;
this.stashByteStart += consumed;
}
} else {
this.cacheRemain = 0;
this.stashByteStart += consumed;
}
if (this.cacheRemain + chunk.byteLength > this.bufferSize) {
this.expandBuffer(this.cacheRemain + chunk.byteLength);
_stashArray = new Uint8Array(this.cacheBuffer, 0, this.bufferSize);
}
_stashArray.set(new Uint8Array(chunk), this.cacheRemain);
this.cacheRemain += chunk.byteLength;
} else {
// stash buffer empty, but chunkSize > stashSize (oh, holy shit)
// dispatch chunk directly and stash remain data
// const consumed = this.arrivalDataCallback(chunk, byteStart, keyframePoint);
var _consumed = 0;
if (this.seekPonit) {
_consumed = this.arrivalDataCallback(chunk, byteStart, this.seekPonit);
this.seekPonit = 0;
} else {
_consumed = this.arrivalDataCallback(chunk, byteStart);
}
if (_consumed < chunk.byteLength) {
var remain = chunk.byteLength - _consumed;
if (remain > this.bufferSize) {
this.expandBuffer(remain);
_stashArray = new Uint8Array(this.cacheBuffer, 0, this.bufferSize);
}
_stashArray.set(new Uint8Array(chunk, _consumed), 0);
this.cacheRemain += remain;
this.stashByteStart = byteStart + _consumed;
}
}
}
}
}
/**
* 清空缓存buffer
*/
}, {
key: 'initCacheBuffer',
value: function initCacheBuffer() {
this.cacheBuffer = new ArrayBuffer(this.bufferSize);
}
/**
* 动态扩展buffer存储器大小
* @param {number} chunk byte size
*/
}, {
key: 'expandBuffer',
value: function expandBuffer(expectedBytes) {
var bufferNewSize = this.bufferSize;
// while (bufferNewSize < expectedBytes) {
// bufferNewSize *= 2;
// }
if (bufferNewSize < expectedBytes) {
bufferNewSize = expectedBytes;
}
this.cacheBuffer = new ArrayBuffer(bufferNewSize);
this.bufferSize = bufferNewSize;
}
/**
* 暂停
*/