@alessmicrosystems/mpegts.js 1.8.1
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/LICENSE +202 -0
- package/README.md +158 -0
- package/README_ja.md +153 -0
- package/README_zh.md +157 -0
- package/d.ts/mpegts.d.ts +524 -0
- package/d.ts/src/core/mse-events.d.ts +9 -0
- package/d.ts/src/core/transmuxing-events.d.ts +24 -0
- package/d.ts/src/demux/aac.d.ts +44 -0
- package/d.ts/src/demux/ac3.d.ts +70 -0
- package/d.ts/src/demux/av1-parser.d.ts +77 -0
- package/d.ts/src/demux/av1.d.ts +11 -0
- package/d.ts/src/demux/base-demuxer.d.ts +55 -0
- package/d.ts/src/demux/h264.d.ts +40 -0
- package/d.ts/src/demux/h265.d.ts +65 -0
- package/d.ts/src/demux/klv.d.ts +17 -0
- package/d.ts/src/demux/mp3.d.ts +6 -0
- package/d.ts/src/demux/mpeg4-audio.d.ts +28 -0
- package/d.ts/src/demux/pat-pmt-pes.d.ts +106 -0
- package/d.ts/src/demux/patpmt.d.ts +40 -0
- package/d.ts/src/demux/pes-private-data.d.ts +14 -0
- package/d.ts/src/demux/pgs-data.d.ts +9 -0
- package/d.ts/src/demux/scte35.d.ts +250 -0
- package/d.ts/src/demux/sei.d.ts +8 -0
- package/d.ts/src/demux/smpte2038.d.ts +22 -0
- package/d.ts/src/demux/ts-demuxer.d.ts +124 -0
- package/d.ts/src/player/live-latency-chaser.d.ts +10 -0
- package/d.ts/src/player/live-latency-synchronizer.d.ts +10 -0
- package/d.ts/src/player/loading-controller.d.ts +19 -0
- package/d.ts/src/player/mse-player.d.ts +30 -0
- package/d.ts/src/player/player-engine-dedicated-thread-worker.d.ts +2 -0
- package/d.ts/src/player/player-engine-dedicated-thread.d.ts +48 -0
- package/d.ts/src/player/player-engine-main-thread.d.ts +50 -0
- package/d.ts/src/player/player-engine-worker-cmd-def.d.ts +25 -0
- package/d.ts/src/player/player-engine-worker-msg-def.d.ts +54 -0
- package/d.ts/src/player/player-engine-worker.d.ts +2 -0
- package/d.ts/src/player/player-engine.d.ts +16 -0
- package/d.ts/src/player/player-events.d.ts +21 -0
- package/d.ts/src/player/seeking-handler.d.ts +22 -0
- package/d.ts/src/player/startup-stall-jumper.d.ts +14 -0
- package/d.ts/src/utils/typedarray-equality.d.ts +2 -0
- package/dist/mpegts.js +3 -0
- package/dist/mpegts.js.LICENSE.txt +7 -0
- package/dist/mpegts.js.map +1 -0
- package/package.json +53 -0
- package/src/config.js +67 -0
- package/src/core/features.js +88 -0
- package/src/core/media-info.js +127 -0
- package/src/core/media-segment-info.js +230 -0
- package/src/core/mse-controller.js +599 -0
- package/src/core/mse-events.ts +28 -0
- package/src/core/transmuxer.js +346 -0
- package/src/core/transmuxing-controller.js +628 -0
- package/src/core/transmuxing-events.ts +43 -0
- package/src/core/transmuxing-worker.js +286 -0
- package/src/demux/aac.ts +397 -0
- package/src/demux/ac3.ts +335 -0
- package/src/demux/amf-parser.js +243 -0
- package/src/demux/av1-parser.ts +629 -0
- package/src/demux/av1.ts +103 -0
- package/src/demux/base-demuxer.ts +69 -0
- package/src/demux/demux-errors.js +26 -0
- package/src/demux/exp-golomb.js +116 -0
- package/src/demux/flv-demuxer.js +1854 -0
- package/src/demux/h264.ts +187 -0
- package/src/demux/h265-parser.js +501 -0
- package/src/demux/h265.ts +214 -0
- package/src/demux/klv.ts +40 -0
- package/src/demux/mp3.ts +7 -0
- package/src/demux/mpeg4-audio.ts +45 -0
- package/src/demux/pat-pmt-pes.ts +132 -0
- package/src/demux/pes-private-data.ts +16 -0
- package/src/demux/pgs-data.ts +11 -0
- package/src/demux/scte35.ts +723 -0
- package/src/demux/sei.ts +99 -0
- package/src/demux/smpte2038.ts +89 -0
- package/src/demux/sps-parser.js +298 -0
- package/src/demux/ts-demuxer.ts +2405 -0
- package/src/index.js +4 -0
- package/src/io/fetch-stream-loader.js +266 -0
- package/src/io/io-controller.js +647 -0
- package/src/io/loader.js +134 -0
- package/src/io/param-seek-handler.js +85 -0
- package/src/io/range-seek-handler.js +52 -0
- package/src/io/speed-sampler.js +93 -0
- package/src/io/websocket-loader.js +151 -0
- package/src/io/xhr-moz-chunked-loader.js +211 -0
- package/src/io/xhr-msstream-loader.js +307 -0
- package/src/io/xhr-range-loader.js +366 -0
- package/src/mpegts.js +95 -0
- package/src/player/live-latency-chaser.ts +66 -0
- package/src/player/live-latency-synchronizer.ts +79 -0
- package/src/player/loading-controller.ts +142 -0
- package/src/player/mse-player.ts +150 -0
- package/src/player/native-player.js +262 -0
- package/src/player/player-engine-dedicated-thread.ts +479 -0
- package/src/player/player-engine-main-thread.ts +463 -0
- package/src/player/player-engine-worker-cmd-def.ts +62 -0
- package/src/player/player-engine-worker-msg-def.ts +102 -0
- package/src/player/player-engine-worker.ts +370 -0
- package/src/player/player-engine.ts +35 -0
- package/src/player/player-errors.js +39 -0
- package/src/player/player-events.ts +40 -0
- package/src/player/seeking-handler.ts +205 -0
- package/src/player/startup-stall-jumper.ts +86 -0
- package/src/remux/aac-silent.js +56 -0
- package/src/remux/mp4-generator.js +866 -0
- package/src/remux/mp4-remuxer.js +778 -0
- package/src/utils/browser.js +128 -0
- package/src/utils/exception.js +73 -0
- package/src/utils/logger.js +140 -0
- package/src/utils/logging-control.js +165 -0
- package/src/utils/polyfill.js +68 -0
- package/src/utils/typedarray-equality.ts +69 -0
- package/src/utils/utf8-conv.js +84 -0
- package/src/utils/webworkify-webpack.js +202 -0
- package/tsconfig.json +16 -0
- package/tslint.json +1 -0
- package/types/index.d.ts +3 -0
- package/types/test-flv.ts +8 -0
- package/types/tsconfig.json +24 -0
- package/webpack.config.js +55 -0
package/src/io/loader.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2016 Bilibili. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* @author zheng qian <xqq@xqq.im>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {NotImplementedException} from '../utils/exception.js';
|
|
20
|
+
|
|
21
|
+
export const LoaderStatus = {
|
|
22
|
+
kIdle: 0,
|
|
23
|
+
kConnecting: 1,
|
|
24
|
+
kBuffering: 2,
|
|
25
|
+
kError: 3,
|
|
26
|
+
kComplete: 4
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const LoaderErrors = {
|
|
30
|
+
OK: 'OK',
|
|
31
|
+
EXCEPTION: 'Exception',
|
|
32
|
+
HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid',
|
|
33
|
+
CONNECTING_TIMEOUT: 'ConnectingTimeout',
|
|
34
|
+
EARLY_EOF: 'EarlyEof',
|
|
35
|
+
UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof'
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/* Loader has callbacks which have following prototypes:
|
|
39
|
+
* function onContentLengthKnown(contentLength: number): void
|
|
40
|
+
* function onURLRedirect(url: string): void
|
|
41
|
+
* function onDataArrival(chunk: ArrayBuffer, byteStart: number, receivedLength: number): void
|
|
42
|
+
* function onError(errorType: number, errorInfo: {code: number, msg: string}): void
|
|
43
|
+
* function onComplete(rangeFrom: number, rangeTo: number): void
|
|
44
|
+
*/
|
|
45
|
+
export class BaseLoader {
|
|
46
|
+
|
|
47
|
+
constructor(typeName) {
|
|
48
|
+
this._type = typeName || 'undefined';
|
|
49
|
+
this._status = LoaderStatus.kIdle;
|
|
50
|
+
this._needStash = false;
|
|
51
|
+
// callbacks
|
|
52
|
+
this._onContentLengthKnown = null;
|
|
53
|
+
this._onURLRedirect = null;
|
|
54
|
+
this._onDataArrival = null;
|
|
55
|
+
this._onError = null;
|
|
56
|
+
this._onComplete = null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
destroy() {
|
|
60
|
+
this._status = LoaderStatus.kIdle;
|
|
61
|
+
this._onContentLengthKnown = null;
|
|
62
|
+
this._onURLRedirect = null;
|
|
63
|
+
this._onDataArrival = null;
|
|
64
|
+
this._onError = null;
|
|
65
|
+
this._onComplete = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
isWorking() {
|
|
69
|
+
return this._status === LoaderStatus.kConnecting || this._status === LoaderStatus.kBuffering;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get type() {
|
|
73
|
+
return this._type;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get status() {
|
|
77
|
+
return this._status;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get needStashBuffer() {
|
|
81
|
+
return this._needStash;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get onContentLengthKnown() {
|
|
85
|
+
return this._onContentLengthKnown;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
set onContentLengthKnown(callback) {
|
|
89
|
+
this._onContentLengthKnown = callback;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get onURLRedirect() {
|
|
93
|
+
return this._onURLRedirect;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
set onURLRedirect(callback) {
|
|
97
|
+
this._onURLRedirect = callback;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get onDataArrival() {
|
|
101
|
+
return this._onDataArrival;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
set onDataArrival(callback) {
|
|
105
|
+
this._onDataArrival = callback;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get onError() {
|
|
109
|
+
return this._onError;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
set onError(callback) {
|
|
113
|
+
this._onError = callback;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get onComplete() {
|
|
117
|
+
return this._onComplete;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
set onComplete(callback) {
|
|
121
|
+
this._onComplete = callback;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// pure virtual
|
|
125
|
+
open(dataSource, range) {
|
|
126
|
+
throw new NotImplementedException('Unimplemented abstract function!');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
abort() {
|
|
130
|
+
throw new NotImplementedException('Unimplemented abstract function!');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2016 Bilibili. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* @author zheng qian <xqq@xqq.im>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class ParamSeekHandler {
|
|
20
|
+
|
|
21
|
+
constructor(paramStart, paramEnd) {
|
|
22
|
+
this._startName = paramStart;
|
|
23
|
+
this._endName = paramEnd;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getConfig(baseUrl, range) {
|
|
27
|
+
let url = baseUrl;
|
|
28
|
+
|
|
29
|
+
if (range.from !== 0 || range.to !== -1) {
|
|
30
|
+
let needAnd = true;
|
|
31
|
+
if (url.indexOf('?') === -1) {
|
|
32
|
+
url += '?';
|
|
33
|
+
needAnd = false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (needAnd) {
|
|
37
|
+
url += '&';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
url += `${this._startName}=${range.from.toString()}`;
|
|
41
|
+
|
|
42
|
+
if (range.to !== -1) {
|
|
43
|
+
url += `&${this._endName}=${range.to.toString()}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
url: url,
|
|
49
|
+
headers: {}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
removeURLParameters(seekedURL) {
|
|
54
|
+
let baseURL = seekedURL.split('?')[0];
|
|
55
|
+
let params = undefined;
|
|
56
|
+
|
|
57
|
+
let queryIndex = seekedURL.indexOf('?');
|
|
58
|
+
if (queryIndex !== -1) {
|
|
59
|
+
params = seekedURL.substring(queryIndex + 1);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let resultParams = '';
|
|
63
|
+
|
|
64
|
+
if (params != undefined && params.length > 0) {
|
|
65
|
+
let pairs = params.split('&');
|
|
66
|
+
|
|
67
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
68
|
+
let pair = pairs[i].split('=');
|
|
69
|
+
let requireAnd = (i > 0);
|
|
70
|
+
|
|
71
|
+
if (pair[0] !== this._startName && pair[0] !== this._endName) {
|
|
72
|
+
if (requireAnd) {
|
|
73
|
+
resultParams += '&';
|
|
74
|
+
}
|
|
75
|
+
resultParams += pairs[i];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return (resultParams.length === 0) ? baseURL : baseURL + '?' + resultParams;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default ParamSeekHandler;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2016 Bilibili. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* @author zheng qian <xqq@xqq.im>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class RangeSeekHandler {
|
|
20
|
+
|
|
21
|
+
constructor(zeroStart) {
|
|
22
|
+
this._zeroStart = zeroStart || false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getConfig(url, range) {
|
|
26
|
+
let headers = {};
|
|
27
|
+
|
|
28
|
+
if (range.from !== 0 || range.to !== -1) {
|
|
29
|
+
let param;
|
|
30
|
+
if (range.to !== -1) {
|
|
31
|
+
param = `bytes=${range.from.toString()}-${range.to.toString()}`;
|
|
32
|
+
} else {
|
|
33
|
+
param = `bytes=${range.from.toString()}-`;
|
|
34
|
+
}
|
|
35
|
+
headers['Range'] = param;
|
|
36
|
+
} else if (this._zeroStart) {
|
|
37
|
+
headers['Range'] = 'bytes=0-';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
url: url,
|
|
42
|
+
headers: headers
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
removeURLParameters(seekedURL) {
|
|
47
|
+
return seekedURL;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default RangeSeekHandler;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2016 Bilibili. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* @author zheng qian <xqq@xqq.im>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Utility class to calculate realtime network I/O speed
|
|
20
|
+
class SpeedSampler {
|
|
21
|
+
|
|
22
|
+
constructor() {
|
|
23
|
+
// milliseconds
|
|
24
|
+
this._firstCheckpoint = 0;
|
|
25
|
+
this._lastCheckpoint = 0;
|
|
26
|
+
this._intervalBytes = 0;
|
|
27
|
+
this._totalBytes = 0;
|
|
28
|
+
this._lastSecondBytes = 0;
|
|
29
|
+
|
|
30
|
+
// compatibility detection
|
|
31
|
+
if (self.performance && self.performance.now) {
|
|
32
|
+
this._now = self.performance.now.bind(self.performance);
|
|
33
|
+
} else {
|
|
34
|
+
this._now = Date.now;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
reset() {
|
|
39
|
+
this._firstCheckpoint = this._lastCheckpoint = 0;
|
|
40
|
+
this._totalBytes = this._intervalBytes = 0;
|
|
41
|
+
this._lastSecondBytes = 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
addBytes(bytes) {
|
|
45
|
+
if (this._firstCheckpoint === 0) {
|
|
46
|
+
this._firstCheckpoint = this._now();
|
|
47
|
+
this._lastCheckpoint = this._firstCheckpoint;
|
|
48
|
+
this._intervalBytes += bytes;
|
|
49
|
+
this._totalBytes += bytes;
|
|
50
|
+
} else if (this._now() - this._lastCheckpoint < 1000) {
|
|
51
|
+
this._intervalBytes += bytes;
|
|
52
|
+
this._totalBytes += bytes;
|
|
53
|
+
} else { // duration >= 1000
|
|
54
|
+
this._lastSecondBytes = this._intervalBytes;
|
|
55
|
+
this._intervalBytes = bytes;
|
|
56
|
+
this._totalBytes += bytes;
|
|
57
|
+
this._lastCheckpoint = this._now();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get currentKBps() {
|
|
62
|
+
this.addBytes(0);
|
|
63
|
+
|
|
64
|
+
let durationSeconds = (this._now() - this._lastCheckpoint) / 1000;
|
|
65
|
+
if (durationSeconds == 0) durationSeconds = 1;
|
|
66
|
+
return (this._intervalBytes / durationSeconds) / 1024;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get lastSecondKBps() {
|
|
70
|
+
this.addBytes(0);
|
|
71
|
+
|
|
72
|
+
if (this._lastSecondBytes !== 0) {
|
|
73
|
+
return this._lastSecondBytes / 1024;
|
|
74
|
+
} else { // lastSecondBytes === 0
|
|
75
|
+
if (this._now() - this._lastCheckpoint >= 500) {
|
|
76
|
+
// if time interval since last checkpoint has exceeded 500ms
|
|
77
|
+
// the speed is nearly accurate
|
|
78
|
+
return this.currentKBps;
|
|
79
|
+
} else {
|
|
80
|
+
// We don't know
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get averageKBps() {
|
|
87
|
+
let durationSeconds = (this._now() - this._firstCheckpoint) / 1000;
|
|
88
|
+
return (this._totalBytes / durationSeconds) / 1024;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export default SpeedSampler;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2016 Bilibili. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* @author zheng qian <xqq@xqq.im>
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import Log from '../utils/logger.js';
|
|
20
|
+
import {BaseLoader, LoaderStatus, LoaderErrors} from './loader.js';
|
|
21
|
+
import {RuntimeException} from '../utils/exception.js';
|
|
22
|
+
|
|
23
|
+
// For MPEG-TS/FLV over WebSocket live stream
|
|
24
|
+
class WebSocketLoader extends BaseLoader {
|
|
25
|
+
|
|
26
|
+
static isSupported() {
|
|
27
|
+
try {
|
|
28
|
+
return (typeof self.WebSocket !== 'undefined');
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
constructor() {
|
|
35
|
+
super('websocket-loader');
|
|
36
|
+
this.TAG = 'WebSocketLoader';
|
|
37
|
+
|
|
38
|
+
this._needStash = true;
|
|
39
|
+
|
|
40
|
+
this._ws = null;
|
|
41
|
+
this._requestAbort = false;
|
|
42
|
+
this._receivedLength = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
destroy() {
|
|
46
|
+
if (this._ws) {
|
|
47
|
+
this.abort();
|
|
48
|
+
}
|
|
49
|
+
super.destroy();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
open(dataSource) {
|
|
53
|
+
try {
|
|
54
|
+
let ws = this._ws = new self.WebSocket(dataSource.url);
|
|
55
|
+
ws.binaryType = 'arraybuffer';
|
|
56
|
+
ws.onopen = this._onWebSocketOpen.bind(this);
|
|
57
|
+
ws.onclose = this._onWebSocketClose.bind(this);
|
|
58
|
+
ws.onmessage = this._onWebSocketMessage.bind(this);
|
|
59
|
+
ws.onerror = this._onWebSocketError.bind(this);
|
|
60
|
+
|
|
61
|
+
this._status = LoaderStatus.kConnecting;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
this._status = LoaderStatus.kError;
|
|
64
|
+
|
|
65
|
+
let info = {code: e.code, msg: e.message};
|
|
66
|
+
|
|
67
|
+
if (this._onError) {
|
|
68
|
+
this._onError(LoaderErrors.EXCEPTION, info);
|
|
69
|
+
} else {
|
|
70
|
+
throw new RuntimeException(info.msg);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
abort() {
|
|
76
|
+
let ws = this._ws;
|
|
77
|
+
if (ws && (ws.readyState === 0 || ws.readyState === 1)) { // CONNECTING || OPEN
|
|
78
|
+
this._requestAbort = true;
|
|
79
|
+
ws.close();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this._ws = null;
|
|
83
|
+
this._status = LoaderStatus.kComplete;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_onWebSocketOpen(e) {
|
|
87
|
+
this._status = LoaderStatus.kBuffering;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
_onWebSocketClose(e) {
|
|
91
|
+
if (this._requestAbort === true) {
|
|
92
|
+
this._requestAbort = false;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this._status = LoaderStatus.kComplete;
|
|
97
|
+
|
|
98
|
+
if (this._onComplete) {
|
|
99
|
+
this._onComplete(0, this._receivedLength - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
_onWebSocketMessage(e) {
|
|
104
|
+
if (e.data instanceof ArrayBuffer) {
|
|
105
|
+
this._dispatchArrayBuffer(e.data);
|
|
106
|
+
} else if (e.data instanceof Blob) {
|
|
107
|
+
let reader = new FileReader();
|
|
108
|
+
reader.onload = () => {
|
|
109
|
+
this._dispatchArrayBuffer(reader.result);
|
|
110
|
+
};
|
|
111
|
+
reader.readAsArrayBuffer(e.data);
|
|
112
|
+
} else {
|
|
113
|
+
this._status = LoaderStatus.kError;
|
|
114
|
+
let info = {code: -1, msg: 'Unsupported WebSocket message type: ' + e.data.constructor.name};
|
|
115
|
+
|
|
116
|
+
if (this._onError) {
|
|
117
|
+
this._onError(LoaderErrors.EXCEPTION, info);
|
|
118
|
+
} else {
|
|
119
|
+
throw new RuntimeException(info.msg);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_dispatchArrayBuffer(arraybuffer) {
|
|
125
|
+
let chunk = arraybuffer;
|
|
126
|
+
let byteStart = this._receivedLength;
|
|
127
|
+
this._receivedLength += chunk.byteLength;
|
|
128
|
+
|
|
129
|
+
if (this._onDataArrival) {
|
|
130
|
+
this._onDataArrival(chunk, byteStart, this._receivedLength);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
_onWebSocketError(e) {
|
|
135
|
+
this._status = LoaderStatus.kError;
|
|
136
|
+
|
|
137
|
+
let info = {
|
|
138
|
+
code: e.code,
|
|
139
|
+
msg: e.message
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (this._onError) {
|
|
143
|
+
this._onError(LoaderErrors.EXCEPTION, info);
|
|
144
|
+
} else {
|
|
145
|
+
throw new RuntimeException(info.msg);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default WebSocketLoader;
|