@hiroosy/baileys-lite 0.4.3 → 0.4.4
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/README.md +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { badData } from '../errors.js';
|
|
2
|
+
import { buildAvc1Entry, parseSps, parseAvcc, box } from '../bsf.js';
|
|
3
|
+
|
|
4
|
+
const ids = {
|
|
5
|
+
ebml: 0x1a45dfa3,
|
|
6
|
+
segment: 0x18538067,
|
|
7
|
+
info: 0x1549a966,
|
|
8
|
+
tracks: 0x1654ae6b,
|
|
9
|
+
cluster: 0x1f43b675,
|
|
10
|
+
cues: 0x1c53bb6b,
|
|
11
|
+
tags: 0x1254c367,
|
|
12
|
+
seekHead: 0x114d9b74,
|
|
13
|
+
chapters: 0x1043a770,
|
|
14
|
+
attachments: 0x1941a469,
|
|
15
|
+
timecodeScale: 0x2ad7b1,
|
|
16
|
+
duration: 0x4489,
|
|
17
|
+
title: 0x7ba9,
|
|
18
|
+
trackEntry: 0xae,
|
|
19
|
+
trackNumber: 0xd7,
|
|
20
|
+
trackType: 0x83,
|
|
21
|
+
codecId: 0x86,
|
|
22
|
+
codecPrivate: 0x63a2,
|
|
23
|
+
defaultDuration: 0x23e383,
|
|
24
|
+
video: 0xe0,
|
|
25
|
+
pixelWidth: 0xb0,
|
|
26
|
+
pixelHeight: 0xba,
|
|
27
|
+
audio: 0xe1,
|
|
28
|
+
samplingFrequency: 0xb5,
|
|
29
|
+
channels: 0x9f,
|
|
30
|
+
bitDepth: 0x6264,
|
|
31
|
+
codecDelay: 0x56aa,
|
|
32
|
+
contentEncodings: 0x6d80,
|
|
33
|
+
clusterTimecode: 0xe7,
|
|
34
|
+
simpleBlock: 0xa3,
|
|
35
|
+
blockGroup: 0xa0,
|
|
36
|
+
block: 0xa1,
|
|
37
|
+
blockDuration: 0x9b,
|
|
38
|
+
referenceBlock: 0xfb,
|
|
39
|
+
docType: 0x4282,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const topLevel = new Set([ids.cluster, ids.cues, ids.tags, ids.seekHead, ids.chapters, ids.attachments, ids.info, ids.tracks]);
|
|
43
|
+
|
|
44
|
+
function vintLength(first) {
|
|
45
|
+
for (let i = 0; i < 8; i++) if (first & (0x80 >> i)) return i + 1;
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function readId(buf, pos) {
|
|
50
|
+
const len = vintLength(buf[pos]);
|
|
51
|
+
if (!len || len > 4 || pos + len > buf.length) return null;
|
|
52
|
+
let v = 0;
|
|
53
|
+
for (let i = 0; i < len; i++) v = v * 256 + buf[pos + i];
|
|
54
|
+
return { id: v, len };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readSize(buf, pos) {
|
|
58
|
+
if (pos >= buf.length) return null;
|
|
59
|
+
const len = vintLength(buf[pos]);
|
|
60
|
+
if (!len || pos + len > buf.length) return null;
|
|
61
|
+
let v = buf[pos] & (0xff >> len);
|
|
62
|
+
let allOnes = v === (0xff >> len);
|
|
63
|
+
for (let i = 1; i < len; i++) {
|
|
64
|
+
v = v * 256 + buf[pos + i];
|
|
65
|
+
if (buf[pos + i] !== 0xff) allOnes = false;
|
|
66
|
+
}
|
|
67
|
+
return { size: allOnes ? -1 : v, len };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function readUint(buf) {
|
|
71
|
+
let v = 0;
|
|
72
|
+
for (let i = 0; i < buf.length; i++) v = v * 256 + buf[i];
|
|
73
|
+
return v;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function readFloat(buf) {
|
|
77
|
+
if (buf.length === 4) return buf.readFloatBE(0);
|
|
78
|
+
if (buf.length === 8) return buf.readDoubleBE(0);
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseChildren(buf, start, end, handler) {
|
|
83
|
+
let p = start;
|
|
84
|
+
while (p < end) {
|
|
85
|
+
const id = readId(buf, p);
|
|
86
|
+
if (!id) break;
|
|
87
|
+
const sz = readSize(buf, p + id.len);
|
|
88
|
+
if (!sz) break;
|
|
89
|
+
const dataStart = p + id.len + sz.len;
|
|
90
|
+
const dataEnd = sz.size < 0 ? end : Math.min(end, dataStart + sz.size);
|
|
91
|
+
handler(id.id, dataStart, dataEnd);
|
|
92
|
+
p = dataEnd;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function hevcEntry(width, height, hvcC) {
|
|
97
|
+
const head = Buffer.alloc(78);
|
|
98
|
+
head.writeUInt16BE(1, 6);
|
|
99
|
+
head.writeUInt16BE(width, 24);
|
|
100
|
+
head.writeUInt16BE(height, 26);
|
|
101
|
+
head.writeUInt32BE(0x00480000, 28);
|
|
102
|
+
head.writeUInt32BE(0x00480000, 32);
|
|
103
|
+
head.writeUInt16BE(1, 40);
|
|
104
|
+
head.writeUInt16BE(0x0018, 74);
|
|
105
|
+
head.writeInt16BE(-1, 76);
|
|
106
|
+
return box('hvc1', head, box('hvcC', hvcC));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function av1Entry(width, height, av1C) {
|
|
110
|
+
const head = Buffer.alloc(78);
|
|
111
|
+
head.writeUInt16BE(1, 6);
|
|
112
|
+
head.writeUInt16BE(width, 24);
|
|
113
|
+
head.writeUInt16BE(height, 26);
|
|
114
|
+
head.writeUInt32BE(0x00480000, 28);
|
|
115
|
+
head.writeUInt32BE(0x00480000, 32);
|
|
116
|
+
head.writeUInt16BE(1, 40);
|
|
117
|
+
head.writeUInt16BE(0x0018, 74);
|
|
118
|
+
head.writeInt16BE(-1, 76);
|
|
119
|
+
return box('av01', head, box('av1C', av1C));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
class Reorder {
|
|
123
|
+
constructor(depth) {
|
|
124
|
+
this.depth = depth;
|
|
125
|
+
this.packets = [];
|
|
126
|
+
this.heap = [];
|
|
127
|
+
this.shift = null;
|
|
128
|
+
this.warm = [];
|
|
129
|
+
this.out = [];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pushHeap(v) {
|
|
133
|
+
const h = this.heap;
|
|
134
|
+
h.push(v);
|
|
135
|
+
let i = h.length - 1;
|
|
136
|
+
while (i > 0) {
|
|
137
|
+
const p = (i - 1) >> 1;
|
|
138
|
+
if (h[p] <= h[i]) break;
|
|
139
|
+
[h[p], h[i]] = [h[i], h[p]];
|
|
140
|
+
i = p;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
popHeap() {
|
|
145
|
+
const h = this.heap;
|
|
146
|
+
const top = h[0];
|
|
147
|
+
const last = h.pop();
|
|
148
|
+
if (h.length) {
|
|
149
|
+
h[0] = last;
|
|
150
|
+
let i = 0;
|
|
151
|
+
for (;;) {
|
|
152
|
+
let m = i;
|
|
153
|
+
const l = 2 * i + 1;
|
|
154
|
+
const r = l + 1;
|
|
155
|
+
if (l < h.length && h[l] < h[m]) m = l;
|
|
156
|
+
if (r < h.length && h[r] < h[m]) m = r;
|
|
157
|
+
if (m === i) break;
|
|
158
|
+
[h[m], h[i]] = [h[i], h[m]];
|
|
159
|
+
i = m;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return top;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
push(pkt) {
|
|
166
|
+
this.packets.push(pkt);
|
|
167
|
+
this.pushHeap(pkt.pts);
|
|
168
|
+
if (this.packets.length > this.depth) this.release();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
release() {
|
|
172
|
+
const pkt = this.packets.shift();
|
|
173
|
+
pkt.dts = this.popHeap();
|
|
174
|
+
this.out.push(pkt);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
flush() {
|
|
178
|
+
while (this.packets.length) this.release();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
take() {
|
|
182
|
+
return this.out.length ? this.out.shift() : null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class MatroskaDemuxer {
|
|
187
|
+
static probe(head) {
|
|
188
|
+
return head.length >= 4 && head[0] === 0x1a && head[1] === 0x45 && head[2] === 0xdf && head[3] === 0xa3;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static async open(reader) {
|
|
192
|
+
const demuxer = new MatroskaDemuxer(reader);
|
|
193
|
+
await demuxer.init();
|
|
194
|
+
return demuxer;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
constructor(reader) {
|
|
198
|
+
this.reader = reader;
|
|
199
|
+
this.format = 'matroska';
|
|
200
|
+
this.tags = {};
|
|
201
|
+
this.streams = [];
|
|
202
|
+
this.duration = 0;
|
|
203
|
+
this.scale = 1000000;
|
|
204
|
+
this.tracks = new Map();
|
|
205
|
+
this.queue = [];
|
|
206
|
+
this.pos = 0;
|
|
207
|
+
this.cluster = null;
|
|
208
|
+
this.ended = false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async readHeader(pos) {
|
|
212
|
+
const r = this.reader;
|
|
213
|
+
r.seek(pos);
|
|
214
|
+
const head = await r.read(12);
|
|
215
|
+
if (head.length < 2) return null;
|
|
216
|
+
const id = readId(head, 0);
|
|
217
|
+
if (!id) return null;
|
|
218
|
+
const sz = readSize(head, id.len);
|
|
219
|
+
if (!sz) return null;
|
|
220
|
+
return { id: id.id, size: sz.size, dataStart: pos + id.len + sz.len };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async init() {
|
|
224
|
+
const r = this.reader;
|
|
225
|
+
let h = await this.readHeader(0);
|
|
226
|
+
if (!h || h.id !== ids.ebml) throw badData('matroska: missing EBML header');
|
|
227
|
+
r.seek(h.dataStart);
|
|
228
|
+
const ebml = await r.read(h.size);
|
|
229
|
+
parseChildren(ebml, 0, ebml.length, (id, s, e) => {
|
|
230
|
+
if (id === ids.docType) this.docType = ebml.toString('latin1', s, e);
|
|
231
|
+
});
|
|
232
|
+
this.format = this.docType === 'webm' ? 'webm' : 'matroska';
|
|
233
|
+
let pos = h.dataStart + h.size;
|
|
234
|
+
h = await this.readHeader(pos);
|
|
235
|
+
if (!h || h.id !== ids.segment) throw badData('matroska: missing segment');
|
|
236
|
+
this.segmentEnd = h.size < 0 ? r.size : Math.min(r.size, h.dataStart + h.size);
|
|
237
|
+
pos = h.dataStart;
|
|
238
|
+
let sawTracks = false;
|
|
239
|
+
while (pos < this.segmentEnd) {
|
|
240
|
+
h = await this.readHeader(pos);
|
|
241
|
+
if (!h) break;
|
|
242
|
+
if (h.id === ids.cluster) break;
|
|
243
|
+
const end = h.size < 0 ? this.segmentEnd : h.dataStart + h.size;
|
|
244
|
+
if (h.id === ids.info || h.id === ids.tracks) {
|
|
245
|
+
r.seek(h.dataStart);
|
|
246
|
+
const body = await r.read(end - h.dataStart);
|
|
247
|
+
if (h.id === ids.info) this.parseInfo(body);
|
|
248
|
+
else {
|
|
249
|
+
this.parseTracks(body);
|
|
250
|
+
sawTracks = true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
pos = end;
|
|
254
|
+
}
|
|
255
|
+
if (!sawTracks || !this.streams.length) throw badData('matroska: no supported tracks');
|
|
256
|
+
this.pos = pos;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
parseInfo(buf) {
|
|
260
|
+
let dur = 0;
|
|
261
|
+
parseChildren(buf, 0, buf.length, (id, s, e) => {
|
|
262
|
+
if (id === ids.timecodeScale) this.scale = readUint(buf.subarray(s, e)) || 1000000;
|
|
263
|
+
else if (id === ids.duration) dur = readFloat(buf.subarray(s, e));
|
|
264
|
+
else if (id === ids.title) this.tags.title = buf.toString('utf8', s, e);
|
|
265
|
+
});
|
|
266
|
+
this.duration = (dur * this.scale) / 1e9;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
parseTracks(buf) {
|
|
270
|
+
parseChildren(buf, 0, buf.length, (id, s, e) => {
|
|
271
|
+
if (id !== ids.trackEntry) return;
|
|
272
|
+
const t = { number: 0, type: 0, codecId: '', priv: null, width: 0, height: 0, rate: 0, channels: 0, bits: 0, delay: 0, defaultDuration: 0, compressed: false };
|
|
273
|
+
parseChildren(buf, s, e, (cid, cs, ce) => {
|
|
274
|
+
const d = buf.subarray(cs, ce);
|
|
275
|
+
if (cid === ids.trackNumber) t.number = readUint(d);
|
|
276
|
+
else if (cid === ids.trackType) t.type = readUint(d);
|
|
277
|
+
else if (cid === ids.codecId) t.codecId = d.toString('latin1');
|
|
278
|
+
else if (cid === ids.codecPrivate) t.priv = Buffer.from(d);
|
|
279
|
+
else if (cid === ids.defaultDuration) t.defaultDuration = readUint(d);
|
|
280
|
+
else if (cid === ids.codecDelay) t.delay = readUint(d);
|
|
281
|
+
else if (cid === ids.contentEncodings) t.compressed = true;
|
|
282
|
+
else if (cid === ids.video) {
|
|
283
|
+
parseChildren(buf, cs, ce, (vid, vs, ve) => {
|
|
284
|
+
if (vid === ids.pixelWidth) t.width = readUint(buf.subarray(vs, ve));
|
|
285
|
+
else if (vid === ids.pixelHeight) t.height = readUint(buf.subarray(vs, ve));
|
|
286
|
+
});
|
|
287
|
+
} else if (cid === ids.audio) {
|
|
288
|
+
parseChildren(buf, cs, ce, (aid, as, ae) => {
|
|
289
|
+
if (aid === ids.samplingFrequency) t.rate = readFloat(buf.subarray(as, ae));
|
|
290
|
+
else if (aid === ids.channels) t.channels = readUint(buf.subarray(as, ae));
|
|
291
|
+
else if (aid === ids.bitDepth) t.bits = readUint(buf.subarray(as, ae));
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
this.addTrack(t);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
addTrack(t) {
|
|
300
|
+
if (t.compressed) return;
|
|
301
|
+
let stream = null;
|
|
302
|
+
const c = t.codecId;
|
|
303
|
+
if (t.type === 1) {
|
|
304
|
+
stream = { index: 0, type: 'video', timeScale: 90000, width: t.width, height: t.height, duration: this.duration };
|
|
305
|
+
if (c === 'V_MPEG4/ISO/AVC' && t.priv) {
|
|
306
|
+
const avc = parseAvcc(t.priv);
|
|
307
|
+
const info = avc.sps.length ? parseSps(avc.sps[0]) : null;
|
|
308
|
+
stream.codec = 'h264';
|
|
309
|
+
stream.extradata = t.priv;
|
|
310
|
+
stream.nalLength = avc.lengthSize;
|
|
311
|
+
stream.h264 = info;
|
|
312
|
+
stream.mp4Entry = buildAvc1Entry(info ? info.width : t.width, info ? info.height : t.height, t.priv);
|
|
313
|
+
if (info) {
|
|
314
|
+
stream.width = info.width;
|
|
315
|
+
stream.height = info.height;
|
|
316
|
+
}
|
|
317
|
+
} else if (c === 'V_MPEGH/ISO/HEVC' && t.priv) {
|
|
318
|
+
stream.codec = 'hevc';
|
|
319
|
+
stream.extradata = t.priv;
|
|
320
|
+
stream.mp4Entry = hevcEntry(t.width, t.height, t.priv);
|
|
321
|
+
} else if (c === 'V_AV1' && t.priv) {
|
|
322
|
+
stream.codec = 'av1';
|
|
323
|
+
stream.extradata = t.priv;
|
|
324
|
+
stream.mp4Entry = av1Entry(t.width, t.height, t.priv);
|
|
325
|
+
} else if (c === 'V_VP9') stream.codec = 'vp9';
|
|
326
|
+
else if (c === 'V_VP8') stream.codec = 'vp8';
|
|
327
|
+
else return;
|
|
328
|
+
if (t.defaultDuration) stream.frameRate = 1e9 / t.defaultDuration;
|
|
329
|
+
stream.codecPrivate = t.priv;
|
|
330
|
+
stream.codecId = c;
|
|
331
|
+
stream.reorder = stream.codec === 'h264' || stream.codec === 'hevc';
|
|
332
|
+
} else if (t.type === 2) {
|
|
333
|
+
stream = { index: 0, type: 'audio', sampleRate: Math.round(t.rate) || 48000, channels: t.channels || 2, bitsPerSample: t.bits || 0, duration: this.duration, extradata: null };
|
|
334
|
+
if (c === 'A_AAC' || c.startsWith('A_AAC/')) {
|
|
335
|
+
stream.codec = 'aac';
|
|
336
|
+
stream.extradata = t.priv;
|
|
337
|
+
} else if (c === 'A_OPUS') {
|
|
338
|
+
stream.codec = 'opus';
|
|
339
|
+
stream.extradata = t.priv;
|
|
340
|
+
stream.sampleRate = 48000;
|
|
341
|
+
stream.preSkip = t.priv && t.priv.length >= 12 ? t.priv.readUInt16LE(10) : 0;
|
|
342
|
+
} else if (c === 'A_MPEG/L3') stream.codec = 'mp3';
|
|
343
|
+
else if (c === 'A_FLAC' && t.priv && t.priv.length >= 42) {
|
|
344
|
+
stream.codec = 'flac';
|
|
345
|
+
stream.extradata = Buffer.from(t.priv.subarray(8, 42));
|
|
346
|
+
} else if (c === 'A_VORBIS') stream.codec = 'vorbis';
|
|
347
|
+
else if (c === 'A_AC3') stream.codec = 'ac3';
|
|
348
|
+
else return;
|
|
349
|
+
stream.timeScale = stream.sampleRate;
|
|
350
|
+
stream.codecPrivate = t.priv;
|
|
351
|
+
stream.codecId = c;
|
|
352
|
+
if (t.delay) stream.codecDelayNs = t.delay;
|
|
353
|
+
} else return;
|
|
354
|
+
stream.index = this.streams.length;
|
|
355
|
+
this.streams.push(stream);
|
|
356
|
+
this.tracks.set(t.number, { stream, reorder: stream.reorder ? new Reorder(24) : null, lastDelta: 0, lastPts: null });
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
toUnits(track, ticks) {
|
|
360
|
+
const ns = ticks * this.scale;
|
|
361
|
+
return Math.round((ns * track.stream.timeScale) / 1e9);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
handleBlock(data, duration, keyFlag) {
|
|
365
|
+
let p = 0;
|
|
366
|
+
const n = readSize(data, 0);
|
|
367
|
+
if (!n) return;
|
|
368
|
+
const number = n.size;
|
|
369
|
+
p = n.len;
|
|
370
|
+
if (p + 3 > data.length) return;
|
|
371
|
+
const rel = data.readInt16BE(p);
|
|
372
|
+
const flags = data[p + 2];
|
|
373
|
+
p += 3;
|
|
374
|
+
const track = this.tracks.get(number);
|
|
375
|
+
if (!track) return;
|
|
376
|
+
const lacing = (flags >> 1) & 3;
|
|
377
|
+
const frames = [];
|
|
378
|
+
if (lacing === 0) frames.push(data.subarray(p));
|
|
379
|
+
else {
|
|
380
|
+
const count = data[p++] + 1;
|
|
381
|
+
const sizes = [];
|
|
382
|
+
if (lacing === 1) {
|
|
383
|
+
for (let i = 0; i < count - 1; i++) {
|
|
384
|
+
let s = 0;
|
|
385
|
+
while (data[p] === 255) {
|
|
386
|
+
s += 255;
|
|
387
|
+
p++;
|
|
388
|
+
}
|
|
389
|
+
s += data[p++];
|
|
390
|
+
sizes.push(s);
|
|
391
|
+
}
|
|
392
|
+
} else if (lacing === 3) {
|
|
393
|
+
const first = readSize(data, p);
|
|
394
|
+
sizes.push(first.size);
|
|
395
|
+
p += first.len;
|
|
396
|
+
for (let i = 1; i < count - 1; i++) {
|
|
397
|
+
const v = readSize(data, p);
|
|
398
|
+
const bias = Math.pow(2, 7 * v.len - 1) - 1;
|
|
399
|
+
sizes.push(sizes[i - 1] + v.size - bias);
|
|
400
|
+
p += v.len;
|
|
401
|
+
}
|
|
402
|
+
} else {
|
|
403
|
+
const each = Math.floor((data.length - p) / count);
|
|
404
|
+
for (let i = 0; i < count - 1; i++) sizes.push(each);
|
|
405
|
+
}
|
|
406
|
+
let used = 0;
|
|
407
|
+
for (const s of sizes) used += s;
|
|
408
|
+
sizes.push(data.length - p - used);
|
|
409
|
+
for (const s of sizes) {
|
|
410
|
+
frames.push(data.subarray(p, p + s));
|
|
411
|
+
p += s;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const ticks = this.cluster + rel;
|
|
415
|
+
const stream = track.stream;
|
|
416
|
+
const frameDur = duration !== undefined ? this.toUnits(track, duration) : 0;
|
|
417
|
+
frames.forEach((f, i) => {
|
|
418
|
+
let pts = this.toUnits(track, ticks);
|
|
419
|
+
if (frames.length > 1 && stream.type === 'audio' && stream.codec === 'mp3') pts += i * 1152;
|
|
420
|
+
const pkt = { stream: stream.index, pts, dts: pts, duration: frameDur, key: stream.type === 'audio' ? true : keyFlag, data: Buffer.from(f) };
|
|
421
|
+
if (track.reorder) track.reorder.push(pkt);
|
|
422
|
+
else this.finish(track, pkt);
|
|
423
|
+
});
|
|
424
|
+
if (track.reorder) {
|
|
425
|
+
let o;
|
|
426
|
+
while ((o = track.reorder.take())) this.finish(track, o);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
finish(track, pkt) {
|
|
431
|
+
const prev = track.prevPkt;
|
|
432
|
+
if (prev && prev.duration === 0) {
|
|
433
|
+
const delta = pkt.dts - prev.dts;
|
|
434
|
+
if (delta > 0) {
|
|
435
|
+
prev.duration = delta;
|
|
436
|
+
track.lastDelta = delta;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (prev && !prev.queued) {
|
|
440
|
+
prev.queued = true;
|
|
441
|
+
this.queue.push(prev);
|
|
442
|
+
}
|
|
443
|
+
track.prevPkt = pkt;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async fillQueue() {
|
|
447
|
+
const r = this.reader;
|
|
448
|
+
while (!this.queue.length) {
|
|
449
|
+
if (this.pos >= this.segmentEnd || this.ended) {
|
|
450
|
+
this.ended = true;
|
|
451
|
+
for (const t of this.tracks.values()) {
|
|
452
|
+
if (t.reorder) {
|
|
453
|
+
t.reorder.flush();
|
|
454
|
+
let o;
|
|
455
|
+
while ((o = t.reorder.take())) this.finish(t, o);
|
|
456
|
+
}
|
|
457
|
+
if (t.prevPkt && !t.prevPkt.queued) {
|
|
458
|
+
if (t.prevPkt.duration === 0) t.prevPkt.duration = t.lastDelta || 0;
|
|
459
|
+
t.prevPkt.queued = true;
|
|
460
|
+
this.queue.push(t.prevPkt);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const h = await this.readHeader(this.pos);
|
|
466
|
+
if (!h) {
|
|
467
|
+
this.pos = this.segmentEnd;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (h.id !== ids.cluster) {
|
|
471
|
+
this.pos = h.size < 0 ? this.segmentEnd : h.dataStart + h.size;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
const end = h.size < 0 ? this.segmentEnd : Math.min(this.segmentEnd, h.dataStart + h.size);
|
|
475
|
+
r.seek(h.dataStart);
|
|
476
|
+
const body = await r.read(end - h.dataStart);
|
|
477
|
+
let consumed = body.length;
|
|
478
|
+
this.cluster = 0;
|
|
479
|
+
let p = 0;
|
|
480
|
+
while (p < body.length) {
|
|
481
|
+
const id = readId(body, p);
|
|
482
|
+
if (!id) break;
|
|
483
|
+
const sz = readSize(body, p + id.len);
|
|
484
|
+
if (!sz) break;
|
|
485
|
+
const ds = p + id.len + sz.len;
|
|
486
|
+
if (h.size < 0 && topLevel.has(id.id)) {
|
|
487
|
+
consumed = p;
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
const de = sz.size < 0 ? body.length : Math.min(body.length, ds + sz.size);
|
|
491
|
+
if (id.id === ids.clusterTimecode) this.cluster = readUint(body.subarray(ds, de));
|
|
492
|
+
else if (id.id === ids.simpleBlock) this.handleBlock(body.subarray(ds, de), undefined, this.simpleKey(body, ds));
|
|
493
|
+
else if (id.id === ids.blockGroup) {
|
|
494
|
+
let blk = null;
|
|
495
|
+
let dur;
|
|
496
|
+
let ref = false;
|
|
497
|
+
parseChildren(body, ds, de, (cid, cs, ce) => {
|
|
498
|
+
if (cid === ids.block) blk = body.subarray(cs, ce);
|
|
499
|
+
else if (cid === ids.blockDuration) dur = readUint(body.subarray(cs, ce));
|
|
500
|
+
else if (cid === ids.referenceBlock) ref = true;
|
|
501
|
+
});
|
|
502
|
+
if (blk) this.handleBlock(blk, dur, !ref);
|
|
503
|
+
}
|
|
504
|
+
p = de;
|
|
505
|
+
}
|
|
506
|
+
this.pos = h.dataStart + consumed;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
simpleKey(body, ds) {
|
|
511
|
+
const n = readSize(body, ds);
|
|
512
|
+
return (body[ds + n.len + 2] & 0x80) !== 0;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
async readPacket() {
|
|
516
|
+
if (!this.queue.length) await this.fillQueue();
|
|
517
|
+
return this.queue.length ? this.queue.shift() : null;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async close() {
|
|
521
|
+
await this.reader.close();
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export { MatroskaDemuxer, ids as matroskaIds };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { badArg } from '../errors.js';
|
|
2
|
+
|
|
3
|
+
function matches(spec, stream, perType) {
|
|
4
|
+
if (spec.type && stream.type !== spec.type) return false;
|
|
5
|
+
if (spec.nth !== undefined && perType !== spec.nth) return false;
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class MergedDemuxer {
|
|
10
|
+
constructor(demuxers, maps, options = {}) {
|
|
11
|
+
this.demuxers = demuxers;
|
|
12
|
+
this.format = demuxers[0].format;
|
|
13
|
+
this.tags = demuxers[0].tags || {};
|
|
14
|
+
this.streams = [];
|
|
15
|
+
this.pending = demuxers.map(() => null);
|
|
16
|
+
this.finished = demuxers.map(() => false);
|
|
17
|
+
this.shortest = Boolean(options.shortest);
|
|
18
|
+
this.duration = Math.max(0, ...demuxers.map((d) => d.duration || 0));
|
|
19
|
+
this.routes = demuxers.map(() => new Map());
|
|
20
|
+
const picks = [];
|
|
21
|
+
if (maps && maps.length) {
|
|
22
|
+
for (const m of maps) {
|
|
23
|
+
const d = demuxers[m.input];
|
|
24
|
+
if (!d) throw badArg('map refers to missing input ' + m.input);
|
|
25
|
+
const counters = { audio: 0, video: 0 };
|
|
26
|
+
let found = false;
|
|
27
|
+
for (const s of d.streams) {
|
|
28
|
+
const n = counters[s.type]++;
|
|
29
|
+
if (matches(m, s, n)) {
|
|
30
|
+
picks.push({ input: m.input, stream: s });
|
|
31
|
+
found = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!found && !m.optional) throw badArg('map matches no streams: ' + m.text);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
for (const type of ['video', 'audio']) {
|
|
38
|
+
for (let k = 0; k < demuxers.length; k++) {
|
|
39
|
+
const s = demuxers[k].streams.find((x) => x.type === type);
|
|
40
|
+
if (s) {
|
|
41
|
+
picks.push({ input: k, stream: s });
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
picks.sort((a, b) => (a.stream.type === b.stream.type ? 0 : a.stream.type === 'video' ? -1 : 1));
|
|
48
|
+
picks.forEach((p, i) => {
|
|
49
|
+
const s = Object.assign({}, p.stream, { index: i });
|
|
50
|
+
this.streams.push(s);
|
|
51
|
+
this.routes[p.input].set(p.stream.index, i);
|
|
52
|
+
});
|
|
53
|
+
if (!this.streams.length) throw badArg('no streams selected');
|
|
54
|
+
this.timeOf = this.streams.map((s) => s.timeScale || 1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async peek(k) {
|
|
58
|
+
if (this.pending[k] || this.finished[k]) return this.pending[k];
|
|
59
|
+
for (;;) {
|
|
60
|
+
const pkt = await this.demuxers[k].readPacket();
|
|
61
|
+
if (!pkt) {
|
|
62
|
+
this.finished[k] = true;
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const out = this.routes[k].get(pkt.stream);
|
|
66
|
+
if (out === undefined) continue;
|
|
67
|
+
pkt.stream = out;
|
|
68
|
+
this.pending[k] = pkt;
|
|
69
|
+
return pkt;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async readPacket() {
|
|
74
|
+
let best = -1;
|
|
75
|
+
let bestTime = Infinity;
|
|
76
|
+
for (let k = 0; k < this.demuxers.length; k++) {
|
|
77
|
+
if (!this.routes[k].size) continue;
|
|
78
|
+
const p = await this.peek(k);
|
|
79
|
+
if (!p) {
|
|
80
|
+
if (this.shortest) return null;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const t = p.dts / this.timeOf[p.stream];
|
|
84
|
+
if (t < bestTime) {
|
|
85
|
+
bestTime = t;
|
|
86
|
+
best = k;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (best < 0) return null;
|
|
90
|
+
const pkt = this.pending[best];
|
|
91
|
+
this.pending[best] = null;
|
|
92
|
+
return pkt;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async close() {
|
|
96
|
+
for (const d of this.demuxers) {
|
|
97
|
+
try {
|
|
98
|
+
await d.close();
|
|
99
|
+
} catch (ignored) {
|
|
100
|
+
d.closed = true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { MergedDemuxer };
|