@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,250 @@
|
|
|
1
|
+
import { badData } from '../errors.js';
|
|
2
|
+
import { parseVorbisComment } from '../tags.js';
|
|
3
|
+
import { opusPacketDuration, parseOpusHead } from '../codec/opus.js';
|
|
4
|
+
|
|
5
|
+
class OggDemuxer {
|
|
6
|
+
static probe(head) {
|
|
7
|
+
return head.length >= 4 && head.toString('latin1', 0, 4) === 'OggS';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static async open(reader) {
|
|
11
|
+
const demuxer = new OggDemuxer(reader);
|
|
12
|
+
await demuxer.init();
|
|
13
|
+
return demuxer;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
constructor(reader) {
|
|
17
|
+
this.reader = reader;
|
|
18
|
+
this.format = 'ogg';
|
|
19
|
+
this.tags = {};
|
|
20
|
+
this.streams = [];
|
|
21
|
+
this.logical = new Map();
|
|
22
|
+
this.queue = [];
|
|
23
|
+
this.ready = false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async readPage() {
|
|
27
|
+
const r = this.reader;
|
|
28
|
+
for (;;) {
|
|
29
|
+
if (r.pos + 27 > r.size) return null;
|
|
30
|
+
const h = await r.read(27);
|
|
31
|
+
if (h[0] !== 0x4f || h[1] !== 0x67 || h[2] !== 0x67 || h[3] !== 0x53) {
|
|
32
|
+
r.seek(r.pos - 26);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const nsegs = h[26];
|
|
36
|
+
const lacing = await r.read(nsegs);
|
|
37
|
+
if (lacing.length < nsegs) return null;
|
|
38
|
+
let size = 0;
|
|
39
|
+
for (let i = 0; i < nsegs; i++) size += lacing[i];
|
|
40
|
+
const body = await r.read(size);
|
|
41
|
+
if (body.length < size) return null;
|
|
42
|
+
const lo = h.readUInt32LE(6);
|
|
43
|
+
const hi = h.readUInt32LE(10);
|
|
44
|
+
return {
|
|
45
|
+
flags: h[5],
|
|
46
|
+
granule: lo === 0xffffffff && hi === 0xffffffff ? -1 : hi * 4294967296 + lo,
|
|
47
|
+
serial: h.readUInt32LE(14),
|
|
48
|
+
lacing,
|
|
49
|
+
body,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async init() {
|
|
55
|
+
let page;
|
|
56
|
+
let pages = 0;
|
|
57
|
+
while ((page = await this.readPage())) {
|
|
58
|
+
pages++;
|
|
59
|
+
this.handlePage(page);
|
|
60
|
+
if (this.allReady()) break;
|
|
61
|
+
if (pages > 4096) break;
|
|
62
|
+
}
|
|
63
|
+
if (!this.logical.size) throw badData('ogg: no logical streams');
|
|
64
|
+
const groups = [...this.logical.values()].filter((s) => s.kind);
|
|
65
|
+
if (!groups.length) throw badData('ogg: no supported streams');
|
|
66
|
+
await this.findEnd();
|
|
67
|
+
let index = 0;
|
|
68
|
+
for (const st of groups) {
|
|
69
|
+
st.index = index;
|
|
70
|
+
let duration;
|
|
71
|
+
if (st.endGranule > 0) {
|
|
72
|
+
duration = st.kind === 'opus' ? Math.max(0, st.endGranule - st.preSkip) / 48000 : st.endGranule / st.sampleRate;
|
|
73
|
+
}
|
|
74
|
+
st.duration = duration;
|
|
75
|
+
const stream = {
|
|
76
|
+
index: index++,
|
|
77
|
+
type: 'audio',
|
|
78
|
+
codec: st.kind,
|
|
79
|
+
timeScale: st.kind === 'opus' ? 48000 : st.sampleRate,
|
|
80
|
+
sampleRate: st.kind === 'opus' ? 48000 : st.sampleRate,
|
|
81
|
+
channels: st.channels,
|
|
82
|
+
bitRate: st.bitRate,
|
|
83
|
+
duration,
|
|
84
|
+
extradata: st.headers[0],
|
|
85
|
+
headers: st.headers,
|
|
86
|
+
preSkip: st.preSkip,
|
|
87
|
+
tags: st.tags,
|
|
88
|
+
};
|
|
89
|
+
if (st.kind === 'opus') stream.inputRate = st.inputRate;
|
|
90
|
+
this.streams.push(stream);
|
|
91
|
+
st.stream = stream;
|
|
92
|
+
Object.assign(this.tags, st.tags);
|
|
93
|
+
}
|
|
94
|
+
this.duration = Math.max(...this.streams.map((s) => s.duration || 0)) || undefined;
|
|
95
|
+
this.ready = true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
allReady() {
|
|
99
|
+
for (const st of this.logical.values()) {
|
|
100
|
+
if (st.kind && st.headers.length < st.need) return false;
|
|
101
|
+
}
|
|
102
|
+
return this.logical.size > 0 && [...this.logical.values()].every((s) => s.bos === false || !s.kind || s.headers.length >= s.need) && this.sawNonBos;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
handlePage(page) {
|
|
106
|
+
let st = this.logical.get(page.serial);
|
|
107
|
+
if (!st) {
|
|
108
|
+
if (this.ready || (this.sawNonBos && !(page.flags & 2))) return;
|
|
109
|
+
st = { serial: page.serial, partial: [], kind: null, headers: [], need: 0, lastGranule: 0, position: 0, tags: {}, bos: true };
|
|
110
|
+
this.logical.set(page.serial, st);
|
|
111
|
+
}
|
|
112
|
+
if (!(page.flags & 2)) this.sawNonBos = true;
|
|
113
|
+
const packets = [];
|
|
114
|
+
let off = 0;
|
|
115
|
+
let acc = [];
|
|
116
|
+
if (!(page.flags & 1)) st.partial = [];
|
|
117
|
+
for (let i = 0; i < page.lacing.length; i++) {
|
|
118
|
+
const len = page.lacing[i];
|
|
119
|
+
acc.push(page.body.subarray(off, off + len));
|
|
120
|
+
off += len;
|
|
121
|
+
if (len < 255) {
|
|
122
|
+
const cont = st.partial.length ? st.partial.concat(acc) : acc;
|
|
123
|
+
packets.push(cont.length === 1 ? cont[0] : Buffer.concat(cont));
|
|
124
|
+
st.partial = [];
|
|
125
|
+
acc = [];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (acc.length) st.partial = st.partial.concat(acc);
|
|
129
|
+
const audio = [];
|
|
130
|
+
for (const pkt of packets) {
|
|
131
|
+
if (!st.kind && st.headers.length === 0 && !st.probed) {
|
|
132
|
+
st.probed = true;
|
|
133
|
+
this.identify(st, pkt);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (!st.kind) continue;
|
|
137
|
+
if (st.headers.length < st.need) {
|
|
138
|
+
st.headers.push(pkt);
|
|
139
|
+
if (st.headers.length === 2) this.readTags(st, pkt);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
audio.push(pkt);
|
|
143
|
+
}
|
|
144
|
+
if (st.kind && audio.length) this.queueAudio(st, audio, page);
|
|
145
|
+
if (page.flags & 4 && page.granule >= 0 && st.kind) {
|
|
146
|
+
st.endGranule = Math.max(st.endGranule || 0, page.granule);
|
|
147
|
+
if (st.kind === 'opus' && audio.length) {
|
|
148
|
+
const excess = st.position - page.granule;
|
|
149
|
+
const last = this.queue[this.queue.length - 1];
|
|
150
|
+
if (excess > 0 && last && last.stream === st.index) {
|
|
151
|
+
const cut = Math.min(excess, last.duration);
|
|
152
|
+
last.duration -= cut;
|
|
153
|
+
st.position -= cut;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (page.granule >= 0) st.lastGranule = page.granule;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
identify(st, pkt) {
|
|
161
|
+
if (pkt.length >= 19 && pkt.toString('latin1', 0, 8) === 'OpusHead') {
|
|
162
|
+
const head = parseOpusHead(pkt);
|
|
163
|
+
st.kind = 'opus';
|
|
164
|
+
st.need = 2;
|
|
165
|
+
st.channels = head.channels;
|
|
166
|
+
st.preSkip = head.preSkip;
|
|
167
|
+
st.inputRate = head.inputRate;
|
|
168
|
+
st.sampleRate = 48000;
|
|
169
|
+
st.headers.push(pkt);
|
|
170
|
+
} else if (pkt.length >= 30 && pkt[0] === 1 && pkt.toString('latin1', 1, 7) === 'vorbis') {
|
|
171
|
+
st.kind = 'vorbis';
|
|
172
|
+
st.need = 3;
|
|
173
|
+
st.channels = pkt[11];
|
|
174
|
+
st.sampleRate = pkt.readUInt32LE(12);
|
|
175
|
+
const nominal = pkt.readInt32LE(20);
|
|
176
|
+
st.bitRate = nominal > 0 ? nominal : undefined;
|
|
177
|
+
st.preSkip = 0;
|
|
178
|
+
st.headers.push(pkt);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
readTags(st, pkt) {
|
|
183
|
+
try {
|
|
184
|
+
if (st.kind === 'opus' && pkt.toString('latin1', 0, 8) === 'OpusTags') st.tags = parseVorbisComment(pkt, 8);
|
|
185
|
+
else if (st.kind === 'vorbis' && pkt[0] === 3) st.tags = parseVorbisComment(pkt, 7);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
st.tags = {};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
queueAudio(st, audio, page) {
|
|
192
|
+
if (st.kind === 'opus') {
|
|
193
|
+
for (const data of audio) {
|
|
194
|
+
const duration = opusPacketDuration(data);
|
|
195
|
+
this.queue.push({ stream: st.index, pts: st.position, dts: st.position, duration, key: true, data });
|
|
196
|
+
st.position += duration;
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
let total = 0;
|
|
201
|
+
if (page.granule >= 0 && page.lacing.length > 0) total = Math.max(0, page.granule - st.position);
|
|
202
|
+
const count = audio.length;
|
|
203
|
+
const base = Math.floor(total / count);
|
|
204
|
+
for (let i = 0; i < count; i++) {
|
|
205
|
+
const duration = i === count - 1 ? total - base * (count - 1) : base;
|
|
206
|
+
this.queue.push({ stream: st.index, pts: st.position, dts: st.position, duration, key: true, data: audio[i] });
|
|
207
|
+
st.position += duration;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async findEnd() {
|
|
212
|
+
const r = this.reader;
|
|
213
|
+
const save = r.pos;
|
|
214
|
+
const span = Math.min(r.size, 131072);
|
|
215
|
+
r.seek(r.size - span);
|
|
216
|
+
const tail = await r.read(span);
|
|
217
|
+
r.seek(save);
|
|
218
|
+
for (let i = tail.length - 27; i >= 0; i--) {
|
|
219
|
+
if (tail[i] === 0x4f && tail[i + 1] === 0x67 && tail[i + 2] === 0x67 && tail[i + 3] === 0x53) {
|
|
220
|
+
const lo = tail.readUInt32LE(i + 6);
|
|
221
|
+
const hi = tail.readUInt32LE(i + 10);
|
|
222
|
+
if (lo === 0xffffffff && hi === 0xffffffff) continue;
|
|
223
|
+
const serial = tail.readUInt32LE(i + 14);
|
|
224
|
+
const st = this.logical.get(serial);
|
|
225
|
+
if (st && st.kind) {
|
|
226
|
+
st.endGranule = hi * 4294967296 + lo;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async readPacket() {
|
|
234
|
+
while (!this.queue.length) {
|
|
235
|
+
const page = await this.readPage();
|
|
236
|
+
if (!page) return null;
|
|
237
|
+
this.handlePage(page);
|
|
238
|
+
}
|
|
239
|
+
const pkt = this.queue.shift();
|
|
240
|
+
return pkt;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async close() {
|
|
244
|
+
await this.reader.close();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export {
|
|
249
|
+
OggDemuxer,
|
|
250
|
+
};
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { badData } from '../errors.js';
|
|
2
|
+
import { parseAdtsHeader } from './adts.js';
|
|
3
|
+
import { splitAnnexB, parseSps, buildAvcC, buildAvc1Entry, annexBToAvcc, buildAsc } from '../bsf.js';
|
|
4
|
+
|
|
5
|
+
const clock = 90000;
|
|
6
|
+
const wrap33 = 8589934592;
|
|
7
|
+
const sizes = [188, 192, 204];
|
|
8
|
+
|
|
9
|
+
function readTs(b, o) {
|
|
10
|
+
return (b[o] >> 1 & 7) * 1073741824 + b[o + 1] * 4194304 + (b[o + 2] >> 1) * 32768 + b[o + 3] * 128 + (b[o + 4] >> 1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function detectLayout(buf) {
|
|
14
|
+
for (const size of sizes) {
|
|
15
|
+
const lead = size === 192 ? 4 : 0;
|
|
16
|
+
for (let start = 0; start < Math.min(size, buf.length); start++) {
|
|
17
|
+
let ok = true;
|
|
18
|
+
for (let k = 0; k < 4 && ok; k++) {
|
|
19
|
+
const p = start + lead + k * size;
|
|
20
|
+
if (p >= buf.length) {
|
|
21
|
+
ok = k >= 2;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (buf[p] !== 0x47) ok = false;
|
|
25
|
+
}
|
|
26
|
+
if (ok) return { size, lead, start };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class PesParser {
|
|
33
|
+
constructor(layout, onPes) {
|
|
34
|
+
this.layout = layout;
|
|
35
|
+
this.onPes = onPes;
|
|
36
|
+
this.pat = new Map();
|
|
37
|
+
this.pmtPid = -1;
|
|
38
|
+
this.es = [];
|
|
39
|
+
this.pmtDone = false;
|
|
40
|
+
this.acc = new Map();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
feed(buf) {
|
|
44
|
+
const { size, lead } = this.layout;
|
|
45
|
+
for (let o = 0; o + size <= buf.length; o += size) {
|
|
46
|
+
const p = o + lead;
|
|
47
|
+
if (buf[p] !== 0x47) continue;
|
|
48
|
+
const pusi = (buf[p + 1] & 0x40) !== 0;
|
|
49
|
+
const pid = ((buf[p + 1] & 0x1f) << 8) | buf[p + 2];
|
|
50
|
+
const afc = (buf[p + 3] >> 4) & 3;
|
|
51
|
+
let start = p + 4;
|
|
52
|
+
if (afc & 2) start += 1 + buf[p + 4];
|
|
53
|
+
if (!(afc & 1) || start >= p + 188) continue;
|
|
54
|
+
const payload = buf.subarray(start, p + 188);
|
|
55
|
+
if (pid === 0) this.section(payload, pusi, 'pat');
|
|
56
|
+
else if (pid === this.pmtPid) this.section(payload, pusi, 'pmt');
|
|
57
|
+
else if (this.es.some((e) => e.pid === pid)) this.collect(pid, payload, pusi);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
section(payload, pusi, kind) {
|
|
62
|
+
if (!pusi || !payload.length) return;
|
|
63
|
+
const sec = payload.subarray(1 + payload[0]);
|
|
64
|
+
if (sec.length < 8) return;
|
|
65
|
+
const len = ((sec[1] & 0x0f) << 8) | sec[2];
|
|
66
|
+
const end = Math.min(sec.length, 3 + len - 4);
|
|
67
|
+
if (kind === 'pat') {
|
|
68
|
+
if (this.pmtPid >= 0) return;
|
|
69
|
+
for (let i = 8; i + 4 <= end; i += 4) {
|
|
70
|
+
const program = (sec[i] << 8) | sec[i + 1];
|
|
71
|
+
if (program !== 0) {
|
|
72
|
+
this.pmtPid = ((sec[i + 2] & 0x1f) << 8) | sec[i + 3];
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (this.pmtDone || sec[0] !== 2) return;
|
|
79
|
+
const infoLen = ((sec[10] & 0x0f) << 8) | sec[11];
|
|
80
|
+
let i = 12 + infoLen;
|
|
81
|
+
const es = [];
|
|
82
|
+
while (i + 5 <= end) {
|
|
83
|
+
es.push({ type: sec[i], pid: ((sec[i + 1] & 0x1f) << 8) | sec[i + 2] });
|
|
84
|
+
i += 5 + (((sec[i + 3] & 0x0f) << 8) | sec[i + 4]);
|
|
85
|
+
}
|
|
86
|
+
this.es = es.filter((e) => e.type === 0x1b || e.type === 0x0f);
|
|
87
|
+
this.pmtDone = true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
collect(pid, payload, pusi) {
|
|
91
|
+
let a = this.acc.get(pid);
|
|
92
|
+
if (pusi) {
|
|
93
|
+
if (a && a.parts.length) this.flushPid(pid);
|
|
94
|
+
a = { parts: [], bytes: 0 };
|
|
95
|
+
this.acc.set(pid, a);
|
|
96
|
+
}
|
|
97
|
+
if (!a) return;
|
|
98
|
+
a.parts.push(payload);
|
|
99
|
+
a.bytes += payload.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
flushPid(pid) {
|
|
103
|
+
const a = this.acc.get(pid);
|
|
104
|
+
if (!a || !a.parts.length) return;
|
|
105
|
+
const buf = a.parts.length === 1 ? a.parts[0] : Buffer.concat(a.parts);
|
|
106
|
+
a.parts = [];
|
|
107
|
+
a.bytes = 0;
|
|
108
|
+
if (buf.length < 9 || buf[0] !== 0 || buf[1] !== 0 || buf[2] !== 1) return;
|
|
109
|
+
const flags = buf[7] >> 6;
|
|
110
|
+
const hdr = buf[8];
|
|
111
|
+
let pts;
|
|
112
|
+
let dts;
|
|
113
|
+
if ((flags & 2) && buf.length >= 14) pts = readTs(buf, 9);
|
|
114
|
+
if (flags === 3 && buf.length >= 19) dts = readTs(buf, 14);
|
|
115
|
+
if (dts === undefined) dts = pts;
|
|
116
|
+
this.onPes(pid, pts, dts, buf.subarray(9 + hdr));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
flushAll() {
|
|
120
|
+
for (const pid of this.acc.keys()) this.flushPid(pid);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
class TsDemuxer {
|
|
125
|
+
static probe(head) {
|
|
126
|
+
return head.length >= 5 && (head[0] === 0x47 || (head[4] === 0x47 && head[0] !== 0x47));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static async open(reader) {
|
|
130
|
+
const demuxer = new TsDemuxer(reader);
|
|
131
|
+
await demuxer.init();
|
|
132
|
+
return demuxer;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
constructor(reader) {
|
|
136
|
+
this.reader = reader;
|
|
137
|
+
this.format = 'mpegts';
|
|
138
|
+
this.tags = {};
|
|
139
|
+
this.streams = [];
|
|
140
|
+
this.duration = 0;
|
|
141
|
+
this.queue = [];
|
|
142
|
+
this.byPid = new Map();
|
|
143
|
+
this.pending = new Map();
|
|
144
|
+
this.ended = false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
makeTrack(pid, type) {
|
|
148
|
+
let t = this.byPid.get(pid);
|
|
149
|
+
if (!t) {
|
|
150
|
+
t = { pid, type, stream: null, lastPts: -1, unwrap: 0, nextAudioPts: null, carry: null, sps: [], pps: [], firstTs: undefined, lastDelta: 3003, frames: [] };
|
|
151
|
+
this.byPid.set(pid, t);
|
|
152
|
+
}
|
|
153
|
+
return t;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
unwrapTs(t, v) {
|
|
157
|
+
if (v === undefined) return v;
|
|
158
|
+
if (t.lastRaw !== undefined && v - t.lastRaw < -wrap33 / 2) t.unwrap += wrap33;
|
|
159
|
+
else if (t.lastRaw !== undefined && v - t.lastRaw > wrap33 / 2) t.unwrap -= wrap33;
|
|
160
|
+
t.lastRaw = v;
|
|
161
|
+
return v + t.unwrap;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async init() {
|
|
165
|
+
const r = this.reader;
|
|
166
|
+
r.seek(0);
|
|
167
|
+
const head = await r.read(Math.min(r.size, 188 * 8));
|
|
168
|
+
const layout = detectLayout(head);
|
|
169
|
+
if (!layout) throw badData('mpegts: no sync pattern');
|
|
170
|
+
this.layout = layout;
|
|
171
|
+
this.startPos = layout.start;
|
|
172
|
+
const limit = Math.min(r.size, 6 * 1024 * 1024);
|
|
173
|
+
const parser = new PesParser(layout, (pid, pts, dts, payload) => this.probePes(pid, pts, dts, payload));
|
|
174
|
+
this.probing = true;
|
|
175
|
+
this.parserRef = parser;
|
|
176
|
+
let pos = layout.start;
|
|
177
|
+
const chunk = layout.size * 512;
|
|
178
|
+
while (pos < limit) {
|
|
179
|
+
r.seek(pos);
|
|
180
|
+
const data = await r.read(Math.min(chunk, limit - pos));
|
|
181
|
+
if (data.length < layout.size) break;
|
|
182
|
+
const usable = data.length - (data.length % layout.size);
|
|
183
|
+
parser.feed(data.subarray(0, usable));
|
|
184
|
+
pos += usable;
|
|
185
|
+
if (parser.pmtDone && this.ready(parser)) break;
|
|
186
|
+
}
|
|
187
|
+
parser.flushAll();
|
|
188
|
+
this.probing = false;
|
|
189
|
+
this.parserRef = null;
|
|
190
|
+
if (!parser.pmtDone) throw badData('mpegts: no program map table');
|
|
191
|
+
const tracks = parser.es.map((e) => this.byPid.get(e.pid)).filter((t) => t && t.stream);
|
|
192
|
+
if (!tracks.length) throw badData('mpegts: no supported streams');
|
|
193
|
+
let origin = Infinity;
|
|
194
|
+
for (const t of tracks) if (t.firstTs !== undefined && t.firstTs < origin) origin = t.firstTs;
|
|
195
|
+
this.origin = Number.isFinite(origin) ? origin : 0;
|
|
196
|
+
tracks.forEach((t, i) => {
|
|
197
|
+
t.stream.index = i;
|
|
198
|
+
this.streams.push(t.stream);
|
|
199
|
+
});
|
|
200
|
+
for (const t of this.byPid.values()) {
|
|
201
|
+
t.lastRaw = undefined;
|
|
202
|
+
t.unwrap = 0;
|
|
203
|
+
t.nextAudioPts = null;
|
|
204
|
+
t.carry = null;
|
|
205
|
+
}
|
|
206
|
+
this.pos = layout.start;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
ready(parser) {
|
|
210
|
+
if (!parser.es.length) return false;
|
|
211
|
+
for (const e of parser.es) {
|
|
212
|
+
const t = this.byPid.get(e.pid);
|
|
213
|
+
if (!t || !t.stream || t.firstTs === undefined) return false;
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
probePes(pid, pts, dts, payload) {
|
|
219
|
+
const es = this.currentEs(pid);
|
|
220
|
+
if (!es) return;
|
|
221
|
+
const t = this.makeTrack(pid, es === 0x1b ? 'video' : 'audio');
|
|
222
|
+
const stamp = this.unwrapTs(t, dts !== undefined ? dts : pts);
|
|
223
|
+
if (t.type === 'video') {
|
|
224
|
+
for (const nal of splitAnnexB(payload)) {
|
|
225
|
+
const type = nal[0] & 31;
|
|
226
|
+
if (type === 7 && !t.sps.some((s) => s.equals(nal))) t.sps.push(Buffer.from(nal));
|
|
227
|
+
else if (type === 8 && !t.pps.some((s) => s.equals(nal))) t.pps.push(Buffer.from(nal));
|
|
228
|
+
}
|
|
229
|
+
if (!t.stream && t.sps.length && t.pps.length) {
|
|
230
|
+
const info = parseSps(t.sps[0]);
|
|
231
|
+
const avcC = buildAvcC(t.sps, t.pps, info);
|
|
232
|
+
t.info = info;
|
|
233
|
+
t.stream = { index: 0, type: 'video', codec: 'h264', timeScale: clock, width: info.width, height: info.height, extradata: avcC, mp4Entry: buildAvc1Entry(info.width, info.height, avcC), h264: info, duration: 0 };
|
|
234
|
+
}
|
|
235
|
+
} else if (!t.stream) {
|
|
236
|
+
const h = this.findAdts(payload);
|
|
237
|
+
if (h) {
|
|
238
|
+
t.stream = { index: 0, type: 'audio', codec: 'aac', timeScale: h.sampleRate, sampleRate: h.sampleRate, channels: h.channels, extradata: buildAsc(h.profile, h.sfi, h.chan), duration: 0 };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (t.firstTs === undefined && stamp !== undefined) t.firstTs = stamp;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
currentEs(pid) {
|
|
245
|
+
const e = this.parserRef ? this.parserRef.es.find((x) => x.pid === pid) : null;
|
|
246
|
+
return e ? e.type : 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
findAdts(buf) {
|
|
250
|
+
for (let i = 0; i + 7 <= buf.length; i++) {
|
|
251
|
+
if (buf[i] !== 0xff) continue;
|
|
252
|
+
const h = parseAdtsHeader(buf, i);
|
|
253
|
+
if (h) return h;
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
emit(t, pkt) {
|
|
259
|
+
if (t.type === 'video') {
|
|
260
|
+
const prev = this.pending.get(t.pid);
|
|
261
|
+
if (prev) {
|
|
262
|
+
const delta = pkt.dts - prev.dts;
|
|
263
|
+
prev.duration = delta > 0 ? delta : t.lastDelta;
|
|
264
|
+
if (delta > 0) t.lastDelta = delta;
|
|
265
|
+
this.queue.push(prev);
|
|
266
|
+
}
|
|
267
|
+
this.pending.set(t.pid, pkt);
|
|
268
|
+
} else this.queue.push(pkt);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
onPes(pid, pts, dts, payload) {
|
|
272
|
+
const es = this.parser.es.find((x) => x.pid === pid);
|
|
273
|
+
const t = this.byPid.get(pid);
|
|
274
|
+
if (!es || !t || !t.stream) return;
|
|
275
|
+
const rawDts = dts !== undefined ? dts : pts;
|
|
276
|
+
if (rawDts === undefined && t.type === 'video') return;
|
|
277
|
+
const uDts = this.unwrapTs(t, rawDts);
|
|
278
|
+
const uPts = pts !== undefined && dts !== undefined && pts !== dts ? uDts + ((pts - dts + wrap33 * 2) % wrap33) : uDts;
|
|
279
|
+
if (t.type === 'video') {
|
|
280
|
+
const nals = splitAnnexB(payload);
|
|
281
|
+
const keep = [];
|
|
282
|
+
let key = false;
|
|
283
|
+
let vcl = false;
|
|
284
|
+
for (const nal of nals) {
|
|
285
|
+
const type = nal[0] & 31;
|
|
286
|
+
if (type === 9) continue;
|
|
287
|
+
if (type === 7 || type === 8) {
|
|
288
|
+
const known = (type === 7 ? t.sps : t.pps).some((s) => s.equals(nal));
|
|
289
|
+
if (known) continue;
|
|
290
|
+
}
|
|
291
|
+
if (type >= 1 && type <= 5) vcl = true;
|
|
292
|
+
if (type === 5) key = true;
|
|
293
|
+
keep.push(nal);
|
|
294
|
+
}
|
|
295
|
+
if (!vcl) return;
|
|
296
|
+
this.emit(t, { stream: t.stream.index, pts: uPts - this.origin, dts: uDts - this.origin, duration: t.lastDelta, key, data: annexBToAvcc(keep) });
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
let buf = payload;
|
|
300
|
+
if (t.carry && t.carry.length) {
|
|
301
|
+
buf = Buffer.concat([t.carry, payload]);
|
|
302
|
+
t.carry = null;
|
|
303
|
+
} else if (uDts !== undefined) t.nextAudioPts = ((uDts - this.origin) * t.stream.sampleRate) / clock;
|
|
304
|
+
let o = 0;
|
|
305
|
+
while (o + 7 <= buf.length) {
|
|
306
|
+
const h = buf[o] === 0xff ? parseAdtsHeader(buf, o) : null;
|
|
307
|
+
if (!h) {
|
|
308
|
+
o++;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (o + h.frameLen > buf.length) break;
|
|
312
|
+
const data = Buffer.from(buf.subarray(o + h.headerLen, o + h.frameLen));
|
|
313
|
+
const frames = 1024 * h.blocks;
|
|
314
|
+
const pos = Math.round(t.nextAudioPts === null ? 0 : t.nextAudioPts);
|
|
315
|
+
this.queue.push({ stream: t.stream.index, pts: pos, dts: pos, duration: frames, key: true, data });
|
|
316
|
+
t.nextAudioPts = (t.nextAudioPts === null ? 0 : t.nextAudioPts) + frames;
|
|
317
|
+
o += h.frameLen;
|
|
318
|
+
}
|
|
319
|
+
if (o < buf.length) t.carry = Buffer.from(buf.subarray(o));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async readPacket() {
|
|
323
|
+
const r = this.reader;
|
|
324
|
+
if (!this.parser) {
|
|
325
|
+
this.parser = new PesParser(this.layout, (pid, pts, dts, payload) => this.onPes(pid, pts, dts, payload));
|
|
326
|
+
this.parser.es = [...this.byPid.values()].map((t) => ({ pid: t.pid, type: t.type === 'video' ? 0x1b : 0x0f }));
|
|
327
|
+
this.parser.pmtDone = true;
|
|
328
|
+
this.parser.pmtPid = -2;
|
|
329
|
+
}
|
|
330
|
+
for (;;) {
|
|
331
|
+
if (this.queue.length) return this.queue.shift();
|
|
332
|
+
if (this.ended) return null;
|
|
333
|
+
r.seek(this.pos);
|
|
334
|
+
const want = this.layout.size * 340;
|
|
335
|
+
const data = await r.read(Math.min(want, r.size - this.pos));
|
|
336
|
+
const usable = data.length - (data.length % this.layout.size);
|
|
337
|
+
if (usable < this.layout.size) {
|
|
338
|
+
this.ended = true;
|
|
339
|
+
this.parser.flushAll();
|
|
340
|
+
for (const p of this.pending.values()) this.queue.push(p);
|
|
341
|
+
this.pending.clear();
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
this.pos += usable;
|
|
345
|
+
this.parser.feed(data.subarray(0, usable));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async close() {
|
|
350
|
+
await this.reader.close();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export { TsDemuxer };
|