xgplayer-mp4-loader 0.0.1 → 3.0.0

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