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/utils.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export function moovToSegments(moov: any, duration: any): {
2
+ videoSegments: any[];
3
+ audioSegments: any[];
4
+ };
5
+ export function moovToMeta(moov: any): {
6
+ kid: any;
7
+ videoCodec: string;
8
+ audioCodec: string;
9
+ width: number;
10
+ height: number;
11
+ videoTimescale: number;
12
+ audioChannelCount: number;
13
+ audioSampleRate: number;
14
+ duration: number;
15
+ audioTimescale: number;
16
+ moov: any;
17
+ };
18
+ export function isNumber(n: any): boolean;
package/es/utils.js ADDED
@@ -0,0 +1,267 @@
1
+ import { toConsumableArray as _toConsumableArray } 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.map.js";
5
+ import "core-js/modules/es.array.iterator.js";
6
+ import "core-js/modules/web.dom-collections.iterator.js";
7
+ import "core-js/modules/web.dom-collections.for-each.js";
8
+ import "core-js/modules/es.array.join.js";
9
+ import "core-js/modules/es.number.is-nan.js";
10
+ import "core-js/modules/es.number.constructor.js";
11
+ function moovToSegments(moov, duration) {
12
+ var tracks = moov.trak;
13
+ if (!tracks || !tracks.length)
14
+ return;
15
+ var videoTrack = tracks.find(function(t) {
16
+ var _t$mdia, _t$mdia$hdlr;
17
+ return ((_t$mdia = t.mdia) === null || _t$mdia === void 0 ? void 0 : (_t$mdia$hdlr = _t$mdia.hdlr) === null || _t$mdia$hdlr === void 0 ? void 0 : _t$mdia$hdlr.handlerType) === "vide";
18
+ });
19
+ var audioTrack = tracks.find(function(t) {
20
+ var _t$mdia2, _t$mdia2$hdlr;
21
+ return ((_t$mdia2 = t.mdia) === null || _t$mdia2 === void 0 ? void 0 : (_t$mdia2$hdlr = _t$mdia2.hdlr) === null || _t$mdia2$hdlr === void 0 ? void 0 : _t$mdia2$hdlr.handlerType) === "soun";
22
+ });
23
+ if (!videoTrack && !audioTrack)
24
+ return;
25
+ var videoSegments = [];
26
+ var audioSegments = [];
27
+ var segmentDurations;
28
+ if (videoTrack) {
29
+ var _videoTrack$mdia, _videoTrack$mdia$minf, _videoTrack$mdia$mdhd;
30
+ var videoStbl = (_videoTrack$mdia = videoTrack.mdia) === null || _videoTrack$mdia === void 0 ? void 0 : (_videoTrack$mdia$minf = _videoTrack$mdia.minf) === null || _videoTrack$mdia$minf === void 0 ? void 0 : _videoTrack$mdia$minf.stbl;
31
+ if (!videoStbl)
32
+ return;
33
+ var timescale = (_videoTrack$mdia$mdhd = videoTrack.mdia.mdhd) === null || _videoTrack$mdia$mdhd === void 0 ? void 0 : _videoTrack$mdia$mdhd.timescale;
34
+ var stts = videoStbl.stts, stsc = videoStbl.stsc, stsz = videoStbl.stsz, stco = videoStbl.stco, stss = videoStbl.stss, ctts = videoStbl.ctts;
35
+ if (!timescale || !stts || !stsc || !stsz || !stco || !stss)
36
+ return;
37
+ videoSegments = getSegments(duration, timescale, stts, stsc, stsz, stco, stss, ctts);
38
+ segmentDurations = videoSegments.map(function(x) {
39
+ return x.duration;
40
+ });
41
+ }
42
+ if (audioTrack) {
43
+ var _audioTrack$mdia, _audioTrack$mdia$minf, _audioTrack$mdia$mdhd;
44
+ var audioStbl = (_audioTrack$mdia = audioTrack.mdia) === null || _audioTrack$mdia === void 0 ? void 0 : (_audioTrack$mdia$minf = _audioTrack$mdia.minf) === null || _audioTrack$mdia$minf === void 0 ? void 0 : _audioTrack$mdia$minf.stbl;
45
+ if (!audioStbl)
46
+ return;
47
+ var _timescale = (_audioTrack$mdia$mdhd = audioTrack.mdia.mdhd) === null || _audioTrack$mdia$mdhd === void 0 ? void 0 : _audioTrack$mdia$mdhd.timescale;
48
+ var _stts = audioStbl.stts, _stsc = audioStbl.stsc, _stsz = audioStbl.stsz, _stco = audioStbl.stco;
49
+ if (!_timescale || !_stts || !_stsc || !_stsz || !_stco)
50
+ return;
51
+ audioSegments = getSegments(duration, _timescale, _stts, _stsc, _stsz, _stco, null, null, segmentDurations);
52
+ }
53
+ return {
54
+ videoSegments,
55
+ audioSegments
56
+ };
57
+ }
58
+ function getSegments(segDuration, timescale, stts, stsc, stsz, stco, stss, ctts, segmentDurations) {
59
+ var frames = [];
60
+ var gop = [];
61
+ var gopDuration = [];
62
+ var stscEntries = stsc.entries;
63
+ var stcoEntries = stco.entries;
64
+ var stszEntrySizes = stsz.entrySizes;
65
+ var stssEntries = stss === null || stss === void 0 ? void 0 : stss.entries;
66
+ var cttsEntries = ctts === null || ctts === void 0 ? void 0 : ctts.entries;
67
+ var cttsArr;
68
+ if (cttsEntries) {
69
+ cttsArr = [];
70
+ cttsEntries.forEach(function(_ref) {
71
+ var count = _ref.count, offset = _ref.offset;
72
+ for (var i2 = 0; i2 < count; i2++) {
73
+ cttsArr.push(offset);
74
+ }
75
+ });
76
+ }
77
+ var keyframeMap;
78
+ if (stssEntries) {
79
+ keyframeMap = {};
80
+ stssEntries.forEach(function(x) {
81
+ keyframeMap[x - 1] = true;
82
+ });
83
+ }
84
+ var frame;
85
+ var duration;
86
+ var startTime = 0;
87
+ var pos = 0;
88
+ var chunkIndex = 0;
89
+ var chunkRunIndex = 0;
90
+ var offsetInChunk = 0;
91
+ var lastSampleInChunk = stscEntries[0].samplesPerChunk;
92
+ var lastChunkInRun = stscEntries[1] ? stscEntries[1].firstChunk - 1 : Infinity;
93
+ var dts = 0;
94
+ var gopId = -1;
95
+ stts.entries.forEach(function(_ref2) {
96
+ var count = _ref2.count, delta = _ref2.delta;
97
+ duration = delta;
98
+ for (var i2 = 0; i2 < count; i2++) {
99
+ frame = {
100
+ dts,
101
+ startTime,
102
+ duration,
103
+ size: stszEntrySizes[pos] || stsz.sampleSize,
104
+ offset: stcoEntries[chunkIndex] + offsetInChunk,
105
+ index: pos
106
+ };
107
+ if (stssEntries) {
108
+ frame.keyframe = keyframeMap[pos];
109
+ if (frame.keyframe) {
110
+ gopId++;
111
+ gop.push([frame]);
112
+ gopDuration.push(frame.duration);
113
+ } else {
114
+ gop[gop.length - 1].push(frame);
115
+ gopDuration[gop.length - 1] += frame.duration;
116
+ }
117
+ frame.gopId = gopId;
118
+ }
119
+ if (cttsArr && pos < cttsArr.length) {
120
+ frame.pts = dts + cttsArr[pos];
121
+ }
122
+ if (pos === 0) {
123
+ frame.pts = 0;
124
+ }
125
+ frames.push(frame);
126
+ startTime += duration;
127
+ dts += delta;
128
+ pos++;
129
+ if (pos < lastSampleInChunk) {
130
+ offsetInChunk += frame.size;
131
+ } else {
132
+ chunkIndex++;
133
+ offsetInChunk = 0;
134
+ if (chunkIndex >= lastChunkInRun) {
135
+ chunkRunIndex++;
136
+ lastChunkInRun = stscEntries[chunkRunIndex + 1] ? stscEntries[chunkRunIndex + 1].firstChunk - 1 : Infinity;
137
+ }
138
+ lastSampleInChunk += stscEntries[chunkRunIndex].samplesPerChunk;
139
+ }
140
+ }
141
+ });
142
+ var l = frames.length;
143
+ if (!l || stss && !frames[0].keyframe)
144
+ return;
145
+ var segments = [];
146
+ var segFrames = [];
147
+ var time = 0;
148
+ var lastFrame;
149
+ var adjust = 0;
150
+ var pushSegment = function pushSegment2(duration2) {
151
+ var _segments;
152
+ lastFrame = segFrames[segFrames.length - 1];
153
+ segments.push({
154
+ index: segments.length,
155
+ startTime: ((_segments = segments[segments.length - 1]) === null || _segments === void 0 ? void 0 : _segments.endTime) || segFrames[0].startTime / timescale,
156
+ endTime: (lastFrame.startTime + lastFrame.duration) / timescale,
157
+ duration: duration2,
158
+ range: [segFrames[0].offset, lastFrame.offset + lastFrame.size],
159
+ frames: segFrames
160
+ });
161
+ time = 0;
162
+ segFrames = [];
163
+ };
164
+ if (stss) {
165
+ var _duration = segDuration * timescale;
166
+ for (var i = 0, _l = gop.length; i < _l; i++) {
167
+ var _segFrames;
168
+ time += gopDuration[i];
169
+ (_segFrames = segFrames).push.apply(_segFrames, _toConsumableArray(gop[i]));
170
+ if (i + 1 < _l) {
171
+ if (i === 0 || time > _duration) {
172
+ pushSegment(time / timescale);
173
+ }
174
+ } else {
175
+ pushSegment(time / timescale);
176
+ }
177
+ }
178
+ } else {
179
+ segmentDurations = segmentDurations || [];
180
+ var _duration2 = segmentDurations[0] || segDuration;
181
+ for (var _i = 0; _i < l; _i++) {
182
+ segFrames.push(frames[_i]);
183
+ time += frames[_i].duration;
184
+ var curTime = time / timescale;
185
+ if (_i + 1 >= l || curTime + adjust >= _duration2) {
186
+ adjust += curTime - _duration2;
187
+ pushSegment(curTime);
188
+ _duration2 = segmentDurations[segments.length] || segDuration;
189
+ }
190
+ }
191
+ }
192
+ return segments;
193
+ }
194
+ function moovToMeta(moov) {
195
+ var videoCodec = "";
196
+ var audioCodec = "";
197
+ var width = 0;
198
+ var height = 0;
199
+ var audioChannelCount = 0;
200
+ var audioSampleRate = 0;
201
+ var duration = 0;
202
+ var videoTimescale = 0;
203
+ var audioTimescale = 0;
204
+ if (moov.mvhd) {
205
+ duration = moov.mvhd.duration / moov.mvhd.timescale;
206
+ }
207
+ var tracks = moov.trak;
208
+ if (tracks) {
209
+ var videoTrack = tracks.find(function(t) {
210
+ var _t$mdia3, _t$mdia3$hdlr;
211
+ return ((_t$mdia3 = t.mdia) === null || _t$mdia3 === void 0 ? void 0 : (_t$mdia3$hdlr = _t$mdia3.hdlr) === null || _t$mdia3$hdlr === void 0 ? void 0 : _t$mdia3$hdlr.handlerType) === "vide";
212
+ });
213
+ var audioTrack = tracks.find(function(t) {
214
+ var _t$mdia4, _t$mdia4$hdlr;
215
+ return ((_t$mdia4 = t.mdia) === null || _t$mdia4 === void 0 ? void 0 : (_t$mdia4$hdlr = _t$mdia4.hdlr) === null || _t$mdia4$hdlr === void 0 ? void 0 : _t$mdia4$hdlr.handlerType) === "soun";
216
+ });
217
+ var e1 = null;
218
+ var defaultKID;
219
+ if (videoTrack) {
220
+ var _videoTrack$mdia2, _videoTrack$mdia2$min, _videoTrack$mdia2$min2;
221
+ e1 = (_videoTrack$mdia2 = videoTrack.mdia) === null || _videoTrack$mdia2 === void 0 ? void 0 : (_videoTrack$mdia2$min = _videoTrack$mdia2.minf) === null || _videoTrack$mdia2$min === void 0 ? void 0 : (_videoTrack$mdia2$min2 = _videoTrack$mdia2$min.stbl) === null || _videoTrack$mdia2$min2 === void 0 ? void 0 : _videoTrack$mdia2$min2.stsd.entries[0];
222
+ if (e1) {
223
+ var _videoTrack$mdia3, _videoTrack$mdia3$mdh, _ref3;
224
+ width = e1.width;
225
+ height = e1.height;
226
+ videoTimescale = (_videoTrack$mdia3 = videoTrack.mdia) === null || _videoTrack$mdia3 === void 0 ? void 0 : (_videoTrack$mdia3$mdh = _videoTrack$mdia3.mdhd) === null || _videoTrack$mdia3$mdh === void 0 ? void 0 : _videoTrack$mdia3$mdh.timescale;
227
+ videoCodec = (_ref3 = e1.avcC || e1.hvcC) === null || _ref3 === void 0 ? void 0 : _ref3.codec;
228
+ if (e1.type === "encv") {
229
+ var _e1$sinf, _e1$sinf$schi;
230
+ defaultKID = (_e1$sinf = e1.sinf) === null || _e1$sinf === void 0 ? void 0 : (_e1$sinf$schi = _e1$sinf.schi) === null || _e1$sinf$schi === void 0 ? void 0 : _e1$sinf$schi.tenc.default_KID;
231
+ }
232
+ }
233
+ }
234
+ if (audioTrack) {
235
+ var _audioTrack$mdia2, _audioTrack$mdia2$min, _audioTrack$mdia2$min2;
236
+ e1 = (_audioTrack$mdia2 = audioTrack.mdia) === null || _audioTrack$mdia2 === void 0 ? void 0 : (_audioTrack$mdia2$min = _audioTrack$mdia2.minf) === null || _audioTrack$mdia2$min === void 0 ? void 0 : (_audioTrack$mdia2$min2 = _audioTrack$mdia2$min.stbl) === null || _audioTrack$mdia2$min2 === void 0 ? void 0 : _audioTrack$mdia2$min2.stsd.entries[0];
237
+ if (e1) {
238
+ var _e1$esds, _audioTrack$mdia3, _audioTrack$mdia3$mdh;
239
+ audioChannelCount = e1.channelCount;
240
+ audioSampleRate = e1.sampleRate;
241
+ audioCodec = (_e1$esds = e1.esds) === null || _e1$esds === void 0 ? void 0 : _e1$esds.codec;
242
+ audioTimescale = (_audioTrack$mdia3 = audioTrack.mdia) === null || _audioTrack$mdia3 === void 0 ? void 0 : (_audioTrack$mdia3$mdh = _audioTrack$mdia3.mdhd) === null || _audioTrack$mdia3$mdh === void 0 ? void 0 : _audioTrack$mdia3$mdh.timescale;
243
+ if (e1.type === "enca") {
244
+ var _e1$sinf2, _e1$sinf2$schi;
245
+ defaultKID = defaultKID || ((_e1$sinf2 = e1.sinf) === null || _e1$sinf2 === void 0 ? void 0 : (_e1$sinf2$schi = _e1$sinf2.schi) === null || _e1$sinf2$schi === void 0 ? void 0 : _e1$sinf2$schi.tenc.default_KID);
246
+ }
247
+ }
248
+ }
249
+ return {
250
+ kid: defaultKID ? defaultKID.join("") : null,
251
+ videoCodec,
252
+ audioCodec,
253
+ width,
254
+ height,
255
+ videoTimescale,
256
+ audioChannelCount,
257
+ audioSampleRate,
258
+ duration,
259
+ audioTimescale,
260
+ moov
261
+ };
262
+ }
263
+ }
264
+ function isNumber(n) {
265
+ return typeof n === "number" && !Number.isNaN(n);
266
+ }
267
+ export { isNumber, moovToMeta, moovToSegments };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "xgplayer-mp4-loader",
3
+ "version": "0.0.1-alpha.10",
4
+ "main": "dist/index.min.js",
5
+ "module": "es/index.js",
6
+ "typings": "es/index.d.ts",
7
+ "sideEffects": false,
8
+ "libd": {
9
+ "umdName": "MP4Loader"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "es"
14
+ ],
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org/",
17
+ "access": "public",
18
+ "tag": "alpha"
19
+ },
20
+ "license": "MIT",
21
+ "unpkgFiles": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "xgplayer-transmuxer": "3.0.0-next.26",
26
+ "xgplayer-streaming-shared": "3.0.0-next.34",
27
+ "eventemitter3": "^4.0.7"
28
+ },
29
+ "peerDependencies": {
30
+ "core-js": ">=3.12.1"
31
+ }
32
+ }