@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.
Files changed (57) hide show
  1. package/README.md +8 -17
  2. package/package.json +1 -1
  3. package/src/ffmpeg/LICENSE +502 -0
  4. package/src/ffmpeg/index.js +64 -0
  5. package/src/ffmpeg/lib/bits.js +150 -0
  6. package/src/ffmpeg/lib/bsf.js +242 -0
  7. package/src/ffmpeg/lib/codec/flac.js +801 -0
  8. package/src/ffmpeg/lib/codec/index.js +108 -0
  9. package/src/ffmpeg/lib/codec/mp3.js +768 -0
  10. package/src/ffmpeg/lib/codec/mpa.js +65 -0
  11. package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
  12. package/src/ffmpeg/lib/codec/opus.js +140 -0
  13. package/src/ffmpeg/lib/codec/pcm.js +198 -0
  14. package/src/ffmpeg/lib/compat.js +42 -0
  15. package/src/ffmpeg/lib/crc.js +60 -0
  16. package/src/ffmpeg/lib/demux/adts.js +151 -0
  17. package/src/ffmpeg/lib/demux/flac.js +150 -0
  18. package/src/ffmpeg/lib/demux/image.js +47 -0
  19. package/src/ffmpeg/lib/demux/index.js +60 -0
  20. package/src/ffmpeg/lib/demux/matroska.js +525 -0
  21. package/src/ffmpeg/lib/demux/merge.js +106 -0
  22. package/src/ffmpeg/lib/demux/mp3.js +199 -0
  23. package/src/ffmpeg/lib/demux/mp4.js +622 -0
  24. package/src/ffmpeg/lib/demux/ogg.js +250 -0
  25. package/src/ffmpeg/lib/demux/ts.js +354 -0
  26. package/src/ffmpeg/lib/demux/wav.js +156 -0
  27. package/src/ffmpeg/lib/errors.js +27 -0
  28. package/src/ffmpeg/lib/filter/gain.js +69 -0
  29. package/src/ffmpeg/lib/filter/mix.js +95 -0
  30. package/src/ffmpeg/lib/filter/resample.js +187 -0
  31. package/src/ffmpeg/lib/filter/video.js +575 -0
  32. package/src/ffmpeg/lib/image/bmp.js +123 -0
  33. package/src/ffmpeg/lib/image/frame.js +216 -0
  34. package/src/ffmpeg/lib/image/gif.js +215 -0
  35. package/src/ffmpeg/lib/image/index.js +70 -0
  36. package/src/ffmpeg/lib/image/jpeg.js +964 -0
  37. package/src/ffmpeg/lib/image/png.js +256 -0
  38. package/src/ffmpeg/lib/image/run.js +67 -0
  39. package/src/ffmpeg/lib/io.js +298 -0
  40. package/src/ffmpeg/lib/mux/adts.js +75 -0
  41. package/src/ffmpeg/lib/mux/flac.js +79 -0
  42. package/src/ffmpeg/lib/mux/mp3.js +85 -0
  43. package/src/ffmpeg/lib/mux/mp4.js +450 -0
  44. package/src/ffmpeg/lib/mux/ogg.js +146 -0
  45. package/src/ffmpeg/lib/mux/wav.js +94 -0
  46. package/src/ffmpeg/lib/net.js +270 -0
  47. package/src/ffmpeg/lib/pipeline.js +579 -0
  48. package/src/ffmpeg/lib/probe.js +45 -0
  49. package/src/ffmpeg/lib/tags.js +307 -0
  50. package/src/ffmpeg/lib/util.js +104 -0
  51. package/src/foundation/image-thumb.js +4 -56
  52. package/src/socket/messages-recv.js +5 -6
  53. package/src/utils/media.js +8 -85
  54. package/src/utils/message-compose.js +19 -11
  55. package/src/voip/index.js +16 -50
  56. package/src/voip/media/WaAudioEngine.js +8 -216
  57. package/src/voip/media/WaVideoEngine.js +8 -237
@@ -1,67 +1,13 @@
1
- import { execFile, spawn } from 'node:child_process';
2
- import { access } from 'node:fs/promises';
3
- import { setPriority } from 'node:os';
4
1
  import { createNoopLogger } from '../shim/core.js';
5
- import { toBytesView, toError } from '../shim/util.js';
6
- import { TEXT_DECODER } from '../bytes.js';
7
2
  import { DEFAULT_VIDEO_CONFIG } from '../types.js';
8
- import { auHasIDR } from './h264.js';
9
- const isUrlSource = source => /^[a-z][a-z0-9+.-]+:\/\//i.test(String(source || ''));
10
- const FFMPEG_BIN = 'ffmpeg';
11
- const MAX_STDERR_CHARS = 16 * 1024;
12
-
13
- const MAX_PENDING_BYTES = 8 * 1024 * 1024;
14
- const ffmpegProbeCache = new Map();
15
- function probeBinary(bin) {
16
- return new Promise((resolve) => {
17
- execFile(bin, ['-version'], { timeout: 5_000 }, (err) => resolve(!err));
18
- });
19
- }
20
- async function hasFfmpeg(bin) {
21
- let available = ffmpegProbeCache.get(bin);
22
- if (available === undefined) {
23
- available = await probeBinary(bin);
24
- if (available)
25
- ffmpegProbeCache.set(bin, available);
26
- }
27
- return available;
28
- }
29
- function startCodeLen(data, offset) {
30
- if (offset + 3 < data.length &&
31
- data[offset] === 0 && data[offset + 1] === 0 && data[offset + 2] === 0 && data[offset + 3] === 1) {
32
- return 4;
33
- }
34
- if (offset + 2 < data.length && data[offset] === 0 && data[offset + 1] === 0 && data[offset + 2] === 1) {
35
- return 3;
36
- }
37
- return 0;
38
- }
39
-
40
- function pickH264Level(width, height, frameRate) {
41
- const mbs = Math.ceil(width / 16) * Math.ceil(height / 16);
42
- const mbps = mbs * frameRate;
43
- if (mbs <= 1620 && mbps <= 40500) return '3.0';
44
- if (mbs <= 3600 && mbps <= 108000) return '3.1';
45
- if (mbs <= 5120 && mbps <= 216000) return '3.2';
46
- return '4.0';
47
- }
48
- const align16 = (n) => Math.max(16, Math.ceil(n / 16) * 16);
49
-
3
+ const UNSUPPORTED_MESSAGE = 'Video is not supported by the built-in media engine.';
50
4
  export class WaVideoEngine {
51
5
  logger;
52
6
  videoSender = null;
53
- proc = null;
54
7
  width;
55
8
  height;
56
9
  frameRate;
57
10
  frameDurationMs;
58
- running = false;
59
- pending = new Uint8Array(0);
60
- accessUnitsSent = 0;
61
- videoPath = null;
62
- waitingForKeyFrame = true;
63
-
64
- sourceKind = null;
65
11
  constructor(config = {}) {
66
12
  const c = { ...DEFAULT_VIDEO_CONFIG, ...config };
67
13
  this.logger = config.logger ?? createNoopLogger();
@@ -74,192 +20,17 @@ export class WaVideoEngine {
74
20
  this.videoSender = sender;
75
21
  }
76
22
  isRunning() {
77
- return this.running;
23
+ return false;
78
24
  }
79
25
  hasSource() {
80
- return this.sourceKind !== null;
26
+ return false;
81
27
  }
82
-
83
- async loadVideoFile(videoPath) {
84
- if (!isUrlSource(videoPath)) {
85
- try {
86
- await access(videoPath);
87
- }
88
- catch {
89
- throw new Error(`File not found: ${videoPath}`);
90
- }
91
- }
92
- if (!(await hasFfmpeg(FFMPEG_BIN))) {
93
- throw new Error('ffmpeg not found on PATH (install ffmpeg to load video files)');
94
- }
95
- this.videoPath = videoPath;
96
- this.sourceKind = 'file';
28
+ async loadVideoFile() {
29
+ throw new Error(UNSUPPORTED_MESSAGE);
97
30
  }
98
-
99
31
  async loadBlankSource() {
100
- if (!(await hasFfmpeg(FFMPEG_BIN))) {
101
- throw new Error('ffmpeg not found on PATH (install ffmpeg for the black-screen video fallback)');
102
- }
103
- this.sourceKind = 'blank';
104
- }
105
-
106
- start() {
107
- if (!this.sourceKind || this.proc)
108
- return;
109
- const outW = align16(this.width);
110
- const outH = align16(this.height);
111
- const scaleFilter = `scale=${outW}:${outH}:force_original_aspect_ratio=decrease,pad=${outW}:${outH}:(ow-iw)/2:(oh-ih)/2,setsar=1`;
112
-
113
- const keyframeIntervalFrames = Math.max(1, Math.round(this.frameRate));
114
- const kbpsCap = 600;
115
- const targetBitrateKbps = Math.max(200, Math.min(kbpsCap, Math.round((outW * outH * this.frameRate) / 1000 * 0.08)));
116
- const bufsizeKbps = Math.max(60, Math.round(targetBitrateKbps * 0.25));
117
- const level = pickH264Level(outW, outH, this.frameRate);
118
- const inputArgs = this.sourceKind === 'blank'
119
-
120
- ? ['-f', 'lavfi', '-re', '-i', `color=c=black:s=${outW}x${outH}:r=${this.frameRate}`]
121
- : isUrlSource(this.videoPath)
122
- ? ['-re', '-i', this.videoPath]
123
- : ['-stream_loop', '-1', '-re', '-i', this.videoPath];
124
-
125
- const args = [
126
- '-hide_banner', '-loglevel', 'error',
127
- ...inputArgs,
128
- '-an',
129
- '-vf', scaleFilter,
130
- '-r', String(this.frameRate),
131
- '-c:v', 'libx264',
132
- '-threads', '1',
133
- '-profile:v', 'baseline',
134
- '-level', level,
135
- '-preset', 'veryfast',
136
- '-tune', 'zerolatency',
137
- '-pix_fmt', 'yuv420p',
138
- '-bf', '0',
139
- '-b:v', `${targetBitrateKbps}k`,
140
- '-maxrate', `${targetBitrateKbps}k`,
141
- '-bufsize', `${bufsizeKbps}k`,
142
- '-x264-params', `aud=1:repeat-headers=1:keyint=${keyframeIntervalFrames}:min-keyint=${keyframeIntervalFrames}:scenecut=0:rc-lookahead=0:sync-lookahead=0:open-gop=0:ref=1`,
143
- '-f', 'h264',
144
- 'pipe:1'
145
- ];
146
- const proc = spawn(FFMPEG_BIN, args, { stdio: ['ignore', 'pipe', 'pipe'] });
147
- this.proc = proc;
148
- try {
149
- setPriority(proc.pid, 10);
150
- }
151
- catch { }
152
- this.pending = new Uint8Array(0);
153
- this.accessUnitsSent = 0;
154
- this.waitingForKeyFrame = true;
155
- this.running = true;
156
- this._deliberateStop = false;
157
- let stderr = '';
158
- proc.stdout?.on('data', (chunk) => {
159
- try {
160
- this.onData(toBytesView(chunk));
161
- }
162
- catch (err) {
163
- this.logger.error('video stream parse error', { message: toError(err).message });
164
- }
165
- });
166
- proc.stderr?.on('data', (chunk) => {
167
- stderr = (stderr + TEXT_DECODER.decode(chunk)).slice(-MAX_STDERR_CHARS);
168
- });
169
- proc.on('error', (err) => {
170
- this.logger.error('ffmpeg video process error', { message: err.message });
171
- this.running = false;
172
- });
173
- proc.on('close', (code) => {
174
- if (this.proc === proc)
175
- this.proc = null;
176
- this.running = false;
177
- if (code !== 0 && code !== null) {
178
- this.logger.error('ffmpeg video process exited unexpectedly', { code, stderr: stderr.trim() });
179
- }
180
- if (!this._deliberateStop && this.sourceKind) {
181
- const now = Date.now();
182
- if (!this._restartWindowStartedAt || now - this._restartWindowStartedAt > 60_000) {
183
- this._restartWindowStartedAt = now;
184
- this._restartCount = 0;
185
- }
186
- this._restartCount = (this._restartCount ?? 0) + 1;
187
- if (this._restartCount > 5) {
188
- this.logger.error('video encoder crash-looped, giving up on auto-restart', {
189
- source: this.sourceKind, videoPath: this.videoPath, restartCount: this._restartCount
190
- });
191
- return;
192
- }
193
- this.logger.error('restarting video encoder after unexpected exit', {
194
- source: this.sourceKind, videoPath: this.videoPath, restartCount: this._restartCount
195
- });
196
- this.start();
197
- }
198
- });
199
- }
200
-
201
- onData(chunk) {
202
- const merged = new Uint8Array(this.pending.length + chunk.length);
203
- merged.set(this.pending, 0);
204
- merged.set(chunk, this.pending.length);
205
- this.pending = merged;
206
- if (this.pending.length > MAX_PENDING_BYTES) {
207
- this.pending = new Uint8Array(0);
208
- return;
209
- }
210
- const audPositions = [];
211
- let i = 0;
212
- const data = this.pending;
213
- while (i < data.length) {
214
- const sc = startCodeLen(data, i);
215
- if (sc > 0) {
216
- const naluStart = i + sc;
217
- if (naluStart < data.length && (data[naluStart] & 0x1f) === 9) {
218
- audPositions.push(i);
219
- }
220
- i += sc;
221
- continue;
222
- }
223
- i++;
224
- }
225
- if (audPositions.length < 2)
226
- return;
227
- for (let k = 0; k < audPositions.length - 1; k++) {
228
- const au = data.subarray(audPositions[k], audPositions[k + 1]);
229
- this.emitAccessUnit(au);
230
- }
231
- this.pending = data.slice(audPositions[audPositions.length - 1]);
232
- }
233
-
234
- emitAccessUnit(au) {
235
- if (this.waitingForKeyFrame) {
236
- if (!auHasIDR(au))
237
- return;
238
- this.waitingForKeyFrame = false;
239
- }
240
- this.accessUnitsSent++;
241
- if (this.videoSender) {
242
- try {
243
- this.videoSender.sendCapturedVideoAU(au, this.frameDurationMs);
244
- }
245
- catch { }
246
- }
247
- }
248
-
249
- stop({ keepSource = false } = {}) {
250
- this._deliberateStop = true;
251
- this.running = false;
252
- if (this.proc) {
253
- try {
254
- this.proc.kill('SIGKILL');
255
- }
256
- catch { }
257
- this.proc = null;
258
- }
259
- this.pending = new Uint8Array(0);
260
- if (!keepSource) {
261
- this.videoPath = null;
262
- this.sourceKind = null;
263
- }
32
+ throw new Error(UNSUPPORTED_MESSAGE);
264
33
  }
34
+ start() { }
35
+ stop() { }
265
36
  }