@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.
Files changed (121) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +158 -0
  3. package/README_ja.md +153 -0
  4. package/README_zh.md +157 -0
  5. package/d.ts/mpegts.d.ts +524 -0
  6. package/d.ts/src/core/mse-events.d.ts +9 -0
  7. package/d.ts/src/core/transmuxing-events.d.ts +24 -0
  8. package/d.ts/src/demux/aac.d.ts +44 -0
  9. package/d.ts/src/demux/ac3.d.ts +70 -0
  10. package/d.ts/src/demux/av1-parser.d.ts +77 -0
  11. package/d.ts/src/demux/av1.d.ts +11 -0
  12. package/d.ts/src/demux/base-demuxer.d.ts +55 -0
  13. package/d.ts/src/demux/h264.d.ts +40 -0
  14. package/d.ts/src/demux/h265.d.ts +65 -0
  15. package/d.ts/src/demux/klv.d.ts +17 -0
  16. package/d.ts/src/demux/mp3.d.ts +6 -0
  17. package/d.ts/src/demux/mpeg4-audio.d.ts +28 -0
  18. package/d.ts/src/demux/pat-pmt-pes.d.ts +106 -0
  19. package/d.ts/src/demux/patpmt.d.ts +40 -0
  20. package/d.ts/src/demux/pes-private-data.d.ts +14 -0
  21. package/d.ts/src/demux/pgs-data.d.ts +9 -0
  22. package/d.ts/src/demux/scte35.d.ts +250 -0
  23. package/d.ts/src/demux/sei.d.ts +8 -0
  24. package/d.ts/src/demux/smpte2038.d.ts +22 -0
  25. package/d.ts/src/demux/ts-demuxer.d.ts +124 -0
  26. package/d.ts/src/player/live-latency-chaser.d.ts +10 -0
  27. package/d.ts/src/player/live-latency-synchronizer.d.ts +10 -0
  28. package/d.ts/src/player/loading-controller.d.ts +19 -0
  29. package/d.ts/src/player/mse-player.d.ts +30 -0
  30. package/d.ts/src/player/player-engine-dedicated-thread-worker.d.ts +2 -0
  31. package/d.ts/src/player/player-engine-dedicated-thread.d.ts +48 -0
  32. package/d.ts/src/player/player-engine-main-thread.d.ts +50 -0
  33. package/d.ts/src/player/player-engine-worker-cmd-def.d.ts +25 -0
  34. package/d.ts/src/player/player-engine-worker-msg-def.d.ts +54 -0
  35. package/d.ts/src/player/player-engine-worker.d.ts +2 -0
  36. package/d.ts/src/player/player-engine.d.ts +16 -0
  37. package/d.ts/src/player/player-events.d.ts +21 -0
  38. package/d.ts/src/player/seeking-handler.d.ts +22 -0
  39. package/d.ts/src/player/startup-stall-jumper.d.ts +14 -0
  40. package/d.ts/src/utils/typedarray-equality.d.ts +2 -0
  41. package/dist/mpegts.js +3 -0
  42. package/dist/mpegts.js.LICENSE.txt +7 -0
  43. package/dist/mpegts.js.map +1 -0
  44. package/package.json +53 -0
  45. package/src/config.js +67 -0
  46. package/src/core/features.js +88 -0
  47. package/src/core/media-info.js +127 -0
  48. package/src/core/media-segment-info.js +230 -0
  49. package/src/core/mse-controller.js +599 -0
  50. package/src/core/mse-events.ts +28 -0
  51. package/src/core/transmuxer.js +346 -0
  52. package/src/core/transmuxing-controller.js +628 -0
  53. package/src/core/transmuxing-events.ts +43 -0
  54. package/src/core/transmuxing-worker.js +286 -0
  55. package/src/demux/aac.ts +397 -0
  56. package/src/demux/ac3.ts +335 -0
  57. package/src/demux/amf-parser.js +243 -0
  58. package/src/demux/av1-parser.ts +629 -0
  59. package/src/demux/av1.ts +103 -0
  60. package/src/demux/base-demuxer.ts +69 -0
  61. package/src/demux/demux-errors.js +26 -0
  62. package/src/demux/exp-golomb.js +116 -0
  63. package/src/demux/flv-demuxer.js +1854 -0
  64. package/src/demux/h264.ts +187 -0
  65. package/src/demux/h265-parser.js +501 -0
  66. package/src/demux/h265.ts +214 -0
  67. package/src/demux/klv.ts +40 -0
  68. package/src/demux/mp3.ts +7 -0
  69. package/src/demux/mpeg4-audio.ts +45 -0
  70. package/src/demux/pat-pmt-pes.ts +132 -0
  71. package/src/demux/pes-private-data.ts +16 -0
  72. package/src/demux/pgs-data.ts +11 -0
  73. package/src/demux/scte35.ts +723 -0
  74. package/src/demux/sei.ts +99 -0
  75. package/src/demux/smpte2038.ts +89 -0
  76. package/src/demux/sps-parser.js +298 -0
  77. package/src/demux/ts-demuxer.ts +2405 -0
  78. package/src/index.js +4 -0
  79. package/src/io/fetch-stream-loader.js +266 -0
  80. package/src/io/io-controller.js +647 -0
  81. package/src/io/loader.js +134 -0
  82. package/src/io/param-seek-handler.js +85 -0
  83. package/src/io/range-seek-handler.js +52 -0
  84. package/src/io/speed-sampler.js +93 -0
  85. package/src/io/websocket-loader.js +151 -0
  86. package/src/io/xhr-moz-chunked-loader.js +211 -0
  87. package/src/io/xhr-msstream-loader.js +307 -0
  88. package/src/io/xhr-range-loader.js +366 -0
  89. package/src/mpegts.js +95 -0
  90. package/src/player/live-latency-chaser.ts +66 -0
  91. package/src/player/live-latency-synchronizer.ts +79 -0
  92. package/src/player/loading-controller.ts +142 -0
  93. package/src/player/mse-player.ts +150 -0
  94. package/src/player/native-player.js +262 -0
  95. package/src/player/player-engine-dedicated-thread.ts +479 -0
  96. package/src/player/player-engine-main-thread.ts +463 -0
  97. package/src/player/player-engine-worker-cmd-def.ts +62 -0
  98. package/src/player/player-engine-worker-msg-def.ts +102 -0
  99. package/src/player/player-engine-worker.ts +370 -0
  100. package/src/player/player-engine.ts +35 -0
  101. package/src/player/player-errors.js +39 -0
  102. package/src/player/player-events.ts +40 -0
  103. package/src/player/seeking-handler.ts +205 -0
  104. package/src/player/startup-stall-jumper.ts +86 -0
  105. package/src/remux/aac-silent.js +56 -0
  106. package/src/remux/mp4-generator.js +866 -0
  107. package/src/remux/mp4-remuxer.js +778 -0
  108. package/src/utils/browser.js +128 -0
  109. package/src/utils/exception.js +73 -0
  110. package/src/utils/logger.js +140 -0
  111. package/src/utils/logging-control.js +165 -0
  112. package/src/utils/polyfill.js +68 -0
  113. package/src/utils/typedarray-equality.ts +69 -0
  114. package/src/utils/utf8-conv.js +84 -0
  115. package/src/utils/webworkify-webpack.js +202 -0
  116. package/tsconfig.json +16 -0
  117. package/tslint.json +1 -0
  118. package/types/index.d.ts +3 -0
  119. package/types/test-flv.ts +8 -0
  120. package/types/tsconfig.json +24 -0
  121. package/webpack.config.js +55 -0
@@ -0,0 +1,103 @@
1
+ /*
2
+ * Copyright (C) 2023 もにょてっく. All Rights Reserved.
3
+ *
4
+ * @author もにょ〜ん <monyone.teihen@gmail.com>
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";
20
+
21
+ export class AV1OBUInMpegTsParser {
22
+
23
+ private readonly TAG: string = "AV1OBUInMpegTsParser";
24
+
25
+ private data_: Uint8Array;
26
+ private current_startcode_offset_: number = 0;
27
+ private eof_flag_: boolean = false;
28
+
29
+ static _ebsp2rbsp(uint8array: Uint8Array) {
30
+ let src = uint8array;
31
+ let src_length = src.byteLength;
32
+ let dst = new Uint8Array(src_length);
33
+ let dst_idx = 0;
34
+
35
+ for (let i = 0; i < src_length; i++) {
36
+ if (i >= 2) {
37
+ // Unescape: Skip 0x03 after 00 00
38
+ if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
39
+ continue;
40
+ }
41
+ }
42
+ dst[dst_idx] = src[i];
43
+ dst_idx++;
44
+ }
45
+
46
+ return new Uint8Array(dst.buffer, 0, dst_idx);
47
+ }
48
+
49
+ public constructor(data: Uint8Array) {
50
+ this.data_ = data;
51
+ this.current_startcode_offset_ = this.findNextStartCodeOffset(0);
52
+ if (this.eof_flag_) {
53
+ Log.e(this.TAG, "Could not find AV1 startcode until payload end!");
54
+ }
55
+ }
56
+
57
+ private findNextStartCodeOffset(start_offset: number) {
58
+ let i = start_offset;
59
+ let data = this.data_;
60
+
61
+ while (true) {
62
+ if (i + 2 >= data.byteLength) {
63
+ this.eof_flag_ = true;
64
+ return data.byteLength;
65
+ }
66
+
67
+ // search 00 00 01
68
+ let uint24 = (data[i + 0] << 16)
69
+ | (data[i + 1] << 8)
70
+ | (data[i + 2]);
71
+ if (uint24 === 0x000001) {
72
+ return i;
73
+ } else {
74
+ i++;
75
+ }
76
+ }
77
+ }
78
+
79
+ public readNextOBUPayload(): Uint8Array | null {
80
+ let data = this.data_;
81
+ let payload: Uint8Array | null = null;
82
+
83
+ while (payload == null) {
84
+ if (this.eof_flag_) {
85
+ break;
86
+ }
87
+ // offset pointed to start code
88
+ let startcode_offset = this.current_startcode_offset_;
89
+
90
+ // nalu payload start offset
91
+ let offset = startcode_offset + 3;
92
+ let next_startcode_offset = this.findNextStartCodeOffset(offset);
93
+ this.current_startcode_offset_ = next_startcode_offset;
94
+
95
+ payload = AV1OBUInMpegTsParser._ebsp2rbsp(data.subarray(offset, next_startcode_offset));
96
+ }
97
+
98
+ return payload;
99
+ }
100
+
101
+ }
102
+
103
+ export default AV1OBUInMpegTsParser;
@@ -0,0 +1,69 @@
1
+ import MediaInfo from '../core/media-info';
2
+ import { PESPrivateData, PESPrivateDataDescriptor } from './pes-private-data';
3
+ import { SMPTE2038Data } from './smpte2038';
4
+ import { SCTE35Data } from './scte35';
5
+ import { KLVData } from './klv';
6
+ import { PGSData } from './pgs-data';
7
+ import { SEIData } from './sei';
8
+
9
+ type OnErrorCallback = (type: string, info: string) => void;
10
+ type OnMediaInfoCallback = (mediaInfo: MediaInfo) => void;
11
+ type OnMetaDataArrivedCallback = (metadata: any) => void;
12
+ type OnTrackMetadataCallback = (type: string, metadata: any) => void;
13
+ type OnDataAvailableCallback = (audioTrack: any, videoTrack: any) => void;
14
+ type OnTimedID3MetadataCallback = (timed_id3_data: PESPrivateData) => void;
15
+ type onPGSSubitleDataCallback = (pgs_data: PGSData) => void;
16
+ type OnSynchronousKLVMetadataCallback = (synchronous_klv_data: KLVData) => void;
17
+ type OnAsynchronousKLVMetadataCallback = (asynchronous_klv_data: PESPrivateData) => void;
18
+ type OnSMPTE2038MetadataCallback = (smpte2038_data: SMPTE2038Data) => void;
19
+ type OnSEICallback = (sei_data: SEIData) => void;
20
+ type OnSCTE35MetadataCallback = (scte35_data: SCTE35Data) => void;
21
+ type OnPESPrivateDataCallback = (private_data: PESPrivateData) => void;
22
+ type OnPESPrivateDataDescriptorCallback = (private_data_descriptor: PESPrivateDataDescriptor) => void;
23
+ export type TracksUpdatedInfo = {
24
+ audioTracks: Array<{pid: number, codec: string, lang?: string}>,
25
+ subtitleTracks: Array<{pid: number, type: string, lang?: string}>,
26
+ };
27
+ type OnTracksUpdatedCallback = (info: TracksUpdatedInfo) => void;
28
+
29
+ export default abstract class BaseDemuxer {
30
+
31
+ public onError: OnErrorCallback;
32
+ public onMediaInfo: OnMediaInfoCallback;
33
+ public onMetaDataArrived: OnMetaDataArrivedCallback;
34
+ public onTrackMetadata: OnTrackMetadataCallback;
35
+ public onDataAvailable: OnDataAvailableCallback;
36
+ public onTimedID3Metadata: OnTimedID3MetadataCallback;
37
+ public onPGSSubtitleData: onPGSSubitleDataCallback;
38
+ public onSynchronousKLVMetadata: OnSynchronousKLVMetadataCallback
39
+ public onAsynchronousKLVMetadata: OnAsynchronousKLVMetadataCallback;
40
+ public onSMPTE2038Metadata: OnSMPTE2038MetadataCallback;
41
+ public onSEI: OnSEICallback;
42
+ public onSCTE35Metadata: OnSCTE35MetadataCallback;
43
+ public onPESPrivateData: OnPESPrivateDataCallback;
44
+ public onPESPrivateDataDescriptor: OnPESPrivateDataDescriptorCallback;
45
+ public onTracksUpdated: OnTracksUpdatedCallback;
46
+
47
+ public constructor() {}
48
+
49
+ public destroy(): void {
50
+ this.onError = null;
51
+ this.onMediaInfo = null;
52
+ this.onMetaDataArrived = null;
53
+ this.onTrackMetadata = null;
54
+ this.onDataAvailable = null;
55
+ this.onTimedID3Metadata = null;
56
+ this.onPGSSubtitleData = null;
57
+ this.onSynchronousKLVMetadata = null;
58
+ this.onAsynchronousKLVMetadata = null;
59
+ this.onSMPTE2038Metadata = null;
60
+ this.onSEI = null;
61
+ this.onSCTE35Metadata = null;
62
+ this.onPESPrivateData = null;
63
+ this.onPESPrivateDataDescriptor = null;
64
+ this.onTracksUpdated = null;
65
+ }
66
+
67
+ abstract parseChunks(chunk: ArrayBuffer, byteStart: number): number;
68
+
69
+ }
@@ -0,0 +1,26 @@
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
+ const DemuxErrors = {
20
+ OK: 'OK',
21
+ FORMAT_ERROR: 'FormatError',
22
+ FORMAT_UNSUPPORTED: 'FormatUnsupported',
23
+ CODEC_UNSUPPORTED: 'CodecUnsupported'
24
+ };
25
+
26
+ export default DemuxErrors;
@@ -0,0 +1,116 @@
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 {IllegalStateException, InvalidArgumentException} from '../utils/exception.js';
20
+
21
+ // Exponential-Golomb buffer decoder
22
+ class ExpGolomb {
23
+
24
+ constructor(uint8array) {
25
+ this.TAG = 'ExpGolomb';
26
+
27
+ this._buffer = uint8array;
28
+ this._buffer_index = 0;
29
+ this._total_bytes = uint8array.byteLength;
30
+ this._total_bits = uint8array.byteLength * 8;
31
+ this._current_word = 0;
32
+ this._current_word_bits_left = 0;
33
+ }
34
+
35
+ destroy() {
36
+ this._buffer = null;
37
+ }
38
+
39
+ _fillCurrentWord() {
40
+ let buffer_bytes_left = this._total_bytes - this._buffer_index;
41
+ if (buffer_bytes_left <= 0)
42
+ throw new IllegalStateException('ExpGolomb: _fillCurrentWord() but no bytes available');
43
+
44
+ let bytes_read = Math.min(4, buffer_bytes_left);
45
+ let word = new Uint8Array(4);
46
+ word.set(this._buffer.subarray(this._buffer_index, this._buffer_index + bytes_read));
47
+ this._current_word = new DataView(word.buffer).getUint32(0, false);
48
+
49
+ this._buffer_index += bytes_read;
50
+ this._current_word_bits_left = bytes_read * 8;
51
+ }
52
+
53
+ readBits(bits) {
54
+ if (bits > 32)
55
+ throw new InvalidArgumentException('ExpGolomb: readBits() bits exceeded max 32bits!');
56
+
57
+ if (bits <= this._current_word_bits_left) {
58
+ let result = this._current_word >>> (32 - bits);
59
+ this._current_word <<= bits;
60
+ this._current_word_bits_left -= bits;
61
+ return result;
62
+ }
63
+
64
+ let result = this._current_word_bits_left ? this._current_word : 0;
65
+ result = result >>> (32 - this._current_word_bits_left);
66
+ let bits_need_left = bits - this._current_word_bits_left;
67
+
68
+ this._fillCurrentWord();
69
+ let bits_read_next = Math.min(bits_need_left, this._current_word_bits_left);
70
+
71
+ let result2 = this._current_word >>> (32 - bits_read_next);
72
+ this._current_word <<= bits_read_next;
73
+ this._current_word_bits_left -= bits_read_next;
74
+
75
+ result = (result << bits_read_next) | result2;
76
+ return result;
77
+ }
78
+
79
+ readBool() {
80
+ return this.readBits(1) === 1;
81
+ }
82
+
83
+ readByte() {
84
+ return this.readBits(8);
85
+ }
86
+
87
+ _skipLeadingZero() {
88
+ let zero_count;
89
+ for (zero_count = 0; zero_count < this._current_word_bits_left; zero_count++) {
90
+ if (0 !== (this._current_word & (0x80000000 >>> zero_count))) {
91
+ this._current_word <<= zero_count;
92
+ this._current_word_bits_left -= zero_count;
93
+ return zero_count;
94
+ }
95
+ }
96
+ this._fillCurrentWord();
97
+ return zero_count + this._skipLeadingZero();
98
+ }
99
+
100
+ readUEG() { // unsigned exponential golomb
101
+ let leading_zeros = this._skipLeadingZero();
102
+ return this.readBits(leading_zeros + 1) - 1;
103
+ }
104
+
105
+ readSEG() { // signed exponential golomb
106
+ let value = this.readUEG();
107
+ if (value & 0x01) {
108
+ return (value + 1) >>> 1;
109
+ } else {
110
+ return -1 * (value >>> 1);
111
+ }
112
+ }
113
+
114
+ }
115
+
116
+ export default ExpGolomb;