@hiroosy/baileys-lite 0.3.7 → 0.3.8
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/package.json +1 -1
- package/src/voip/app-data/WaAppDataStream.js +0 -39
- package/src/voip/call/WaCallManager.js +4 -32
- package/src/voip/call/WaCallMediaSession.js +35 -309
- package/src/voip/index.js +75 -18
- package/src/voip/media/WaAudioEngine.js +150 -122
- package/src/voip/media/WaVideoEngine.js +7 -22
- package/src/voip/media/mlow-codec.js +7 -141
- package/src/voip/relay/WaManualRelay.js +91 -94
- package/src/voip/relay/dtls/handshake.js +4 -42
- package/src/voip/relay/sctp/association.js +89 -54
- package/src/voip/relay/stun.js +6 -6
- package/src/voip/types.js +1 -1
- package/src/voip/voipClient.js +3 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { execFile, spawn } from 'node:child_process';
|
|
2
2
|
import { access } from 'node:fs/promises';
|
|
3
|
+
import { setPriority } from 'node:os';
|
|
3
4
|
import { createNoopLogger } from '../shim/core.js';
|
|
4
5
|
import { toBytesView, toError } from '../shim/util.js';
|
|
5
6
|
import { TEXT_DECODER } from '../bytes.js';
|
|
@@ -79,7 +80,6 @@ export class WaVideoEngine {
|
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
async loadVideoFile(videoPath) {
|
|
82
|
-
this.logger.debug('loading video file', { videoPath });
|
|
83
83
|
try {
|
|
84
84
|
await access(videoPath);
|
|
85
85
|
}
|
|
@@ -140,14 +140,15 @@ export class WaVideoEngine {
|
|
|
140
140
|
];
|
|
141
141
|
const proc = spawn(FFMPEG_BIN, args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
142
142
|
this.proc = proc;
|
|
143
|
+
try {
|
|
144
|
+
setPriority(proc.pid, 10);
|
|
145
|
+
}
|
|
146
|
+
catch { }
|
|
143
147
|
this.pending = new Uint8Array(0);
|
|
144
148
|
this.accessUnitsSent = 0;
|
|
145
149
|
this.waitingForKeyFrame = true;
|
|
146
150
|
this.running = true;
|
|
147
151
|
this._deliberateStop = false;
|
|
148
|
-
this.logger.warn('[DIAG] ffmpeg video process started', {
|
|
149
|
-
pid: proc.pid, frameRate: this.frameRate, width: outW, height: outH, level, bitrateKbps: targetBitrateKbps
|
|
150
|
-
});
|
|
151
152
|
let stderr = '';
|
|
152
153
|
proc.stdout?.on('data', (chunk) => {
|
|
153
154
|
try {
|
|
@@ -190,9 +191,6 @@ export class WaVideoEngine {
|
|
|
190
191
|
this.start();
|
|
191
192
|
}
|
|
192
193
|
});
|
|
193
|
-
this.logger.media('video source starting', {
|
|
194
|
-
source: this.sourceKind, videoPath: this.videoPath, width: this.width, height: this.height, fps: this.frameRate
|
|
195
|
-
});
|
|
196
194
|
}
|
|
197
195
|
|
|
198
196
|
onData(chunk) {
|
|
@@ -201,9 +199,6 @@ export class WaVideoEngine {
|
|
|
201
199
|
merged.set(chunk, this.pending.length);
|
|
202
200
|
this.pending = merged;
|
|
203
201
|
if (this.pending.length > MAX_PENDING_BYTES) {
|
|
204
|
-
this.logger.debug('video pending buffer exceeded cap without a second AUD, dropping', {
|
|
205
|
-
bytes: this.pending.length
|
|
206
|
-
});
|
|
207
202
|
this.pending = new Uint8Array(0);
|
|
208
203
|
return;
|
|
209
204
|
}
|
|
@@ -242,14 +237,7 @@ export class WaVideoEngine {
|
|
|
242
237
|
try {
|
|
243
238
|
this.videoSender.sendCapturedVideoAU(au, this.frameDurationMs);
|
|
244
239
|
}
|
|
245
|
-
catch
|
|
246
|
-
this.logger.trace('captured video send failed', { message: toError(err).message });
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (this.accessUnitsSent === 1 || this.accessUnitsSent % 300 === 0) {
|
|
250
|
-
this.logger.trace('video access unit emitted', {
|
|
251
|
-
count: this.accessUnitsSent, bytes: au.length
|
|
252
|
-
});
|
|
240
|
+
catch { }
|
|
253
241
|
}
|
|
254
242
|
}
|
|
255
243
|
|
|
@@ -257,13 +245,10 @@ export class WaVideoEngine {
|
|
|
257
245
|
this._deliberateStop = true;
|
|
258
246
|
this.running = false;
|
|
259
247
|
if (this.proc) {
|
|
260
|
-
this.logger.warn('[DIAG] ffmpeg video process stopping', { pid: this.proc.pid });
|
|
261
248
|
try {
|
|
262
249
|
this.proc.kill('SIGKILL');
|
|
263
250
|
}
|
|
264
|
-
catch
|
|
265
|
-
this.logger.trace('ffmpeg video kill failed', { message: toError(err).message });
|
|
266
|
-
}
|
|
251
|
+
catch { }
|
|
267
252
|
this.proc = null;
|
|
268
253
|
}
|
|
269
254
|
this.pending = new Uint8Array(0);
|
|
@@ -7,15 +7,10 @@ const MAX_FRAME_SIZE = 1_920;
|
|
|
7
7
|
const APPLICATION_VOIP = 2048;
|
|
8
8
|
const SIGNAL_VOICE = 3001;
|
|
9
9
|
const CONCEAL_QUANTUM = MLOW_SAMPLE_RATE / 400;
|
|
10
|
-
const DEFAULT_BITRATE =
|
|
11
|
-
const DEFAULT_COMPLEXITY =
|
|
10
|
+
const DEFAULT_BITRATE = 24_000;
|
|
11
|
+
const DEFAULT_COMPLEXITY = 9;
|
|
12
12
|
const DEFAULT_MAX_CONCEAL_FRAMES = 5;
|
|
13
|
-
const MAX_LOSS_HINT =
|
|
14
|
-
const SPEECH_TOC_MASK = 0xf8;
|
|
15
|
-
const SPEECH_TOC_60MS = 0x50;
|
|
16
|
-
const TINY_FRAME_BYTES = 18;
|
|
17
|
-
const RENEW_MIN_RMS = 120;
|
|
18
|
-
const RENEW_MIN_GAP_FRAMES = 4;
|
|
13
|
+
const MAX_LOSS_HINT = 20;
|
|
19
14
|
const SEQ_SPACE = 0x1_0000;
|
|
20
15
|
const SEQ_HALF = 0x8000;
|
|
21
16
|
export const MLOW_ENCODER_CTL = Object.freeze({
|
|
@@ -59,12 +54,6 @@ export class MLowCodec {
|
|
|
59
54
|
pcmScratch = new Int16Array(FRAME_SIZE);
|
|
60
55
|
lib = null;
|
|
61
56
|
encoderOptions = null;
|
|
62
|
-
spareEncoder = null;
|
|
63
|
-
spareBuilding = false;
|
|
64
|
-
generation = 0;
|
|
65
|
-
frameIndex = 0;
|
|
66
|
-
lastRenewFrame = -1000;
|
|
67
|
-
renewCount = 0;
|
|
68
57
|
silenceScratch = new Float32Array(MAX_FRAME_SIZE);
|
|
69
58
|
constructor() { }
|
|
70
59
|
static async create(opts = {}) {
|
|
@@ -112,83 +101,6 @@ export class MLowCodec {
|
|
|
112
101
|
if (opts.tunables) {
|
|
113
102
|
this.applyEncoderTunables(opts.tunables);
|
|
114
103
|
}
|
|
115
|
-
this.frameIndex = 0;
|
|
116
|
-
this.lastRenewFrame = -1000;
|
|
117
|
-
this.prepareSpare();
|
|
118
|
-
this.logger.warn('[MLOW] encoder ready', {
|
|
119
|
-
vadRenew: true,
|
|
120
|
-
bitrate: this.encoderOptions.bitrate,
|
|
121
|
-
lossHintCap: MAX_LOSS_HINT
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
prepareSpare() {
|
|
125
|
-
if (this.spareEncoder || this.spareBuilding || !this.lib || !this.encoderOptions) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const generation = this.generation;
|
|
129
|
-
this.spareBuilding = true;
|
|
130
|
-
const options = { ...this.encoderOptions, packetLossPercent: this.packetLossPercent };
|
|
131
|
-
this.lib.createEncoder(options).then((enc) => {
|
|
132
|
-
this.spareBuilding = false;
|
|
133
|
-
if (generation !== this.generation) {
|
|
134
|
-
enc.free();
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const tunables = this.opts?.tunables;
|
|
138
|
-
if (tunables) {
|
|
139
|
-
this.writeCtlOn(enc, MLOW_ENCODER_CTL.SUBFRAME_IMPORTANCE, tunables.subframeImportance);
|
|
140
|
-
this.writeCtlOn(enc, MLOW_ENCODER_CTL.USE_SPEECH_ACTIVITY_FLATNESS, tunables.useSpeechActivityFlatness);
|
|
141
|
-
this.writeCtlOn(enc, MLOW_ENCODER_CTL.VAD_NON_BINARY, tunables.vadNonBinary);
|
|
142
|
-
this.writeCtlOn(enc, MLOW_ENCODER_CTL.VAD_HIGHPASS_SHARPNESS, tunables.vadHighpassSharpness);
|
|
143
|
-
}
|
|
144
|
-
this.spareEncoder = enc;
|
|
145
|
-
}, (err) => {
|
|
146
|
-
this.spareBuilding = false;
|
|
147
|
-
this.logger.warn('mlow spare encoder unavailable', { message: toError(err).message });
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
renewEncoder(pcm, original) {
|
|
151
|
-
const spare = this.spareEncoder;
|
|
152
|
-
this.spareEncoder = null;
|
|
153
|
-
const kept = Uint8Array.from(original);
|
|
154
|
-
try {
|
|
155
|
-
const alt = spare.encode(pcm, { frameSize: this.frameSize });
|
|
156
|
-
if (alt.length > 0 && (alt[0] & SPEECH_TOC_MASK) === SPEECH_TOC_60MS) {
|
|
157
|
-
const out = Uint8Array.from(alt);
|
|
158
|
-
const old = this.encoder;
|
|
159
|
-
this.encoder = spare;
|
|
160
|
-
try {
|
|
161
|
-
spare.setPacketLossPercent(this.packetLossPercent);
|
|
162
|
-
}
|
|
163
|
-
catch { }
|
|
164
|
-
try {
|
|
165
|
-
old.free();
|
|
166
|
-
}
|
|
167
|
-
catch { }
|
|
168
|
-
this.renewCount++;
|
|
169
|
-
this.lastRenewFrame = this.frameIndex;
|
|
170
|
-
if (this.renewCount === 1 || this.renewCount % 20 === 0) {
|
|
171
|
-
this.logger.warn('[MLOW] encoder renewed: frame came out with VAD off', {
|
|
172
|
-
renewCount: this.renewCount,
|
|
173
|
-
frame: this.frameIndex,
|
|
174
|
-
tocBefore: kept[0],
|
|
175
|
-
tocAfter: out[0]
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
this.prepareSpare();
|
|
179
|
-
return out;
|
|
180
|
-
}
|
|
181
|
-
spare.free();
|
|
182
|
-
}
|
|
183
|
-
catch (err) {
|
|
184
|
-
this.logger.warn('mlow encoder renew failed', { message: toError(err).message });
|
|
185
|
-
try {
|
|
186
|
-
spare.free();
|
|
187
|
-
}
|
|
188
|
-
catch { }
|
|
189
|
-
}
|
|
190
|
-
this.prepareSpare();
|
|
191
|
-
return kept;
|
|
192
104
|
}
|
|
193
105
|
encode(float32Audio) {
|
|
194
106
|
if (!this.encoder) {
|
|
@@ -198,23 +110,11 @@ export class MLowCodec {
|
|
|
198
110
|
throw new Error(`[MLowCodec] encode expects ${this.frameSize} samples, got ${float32Audio.length}`);
|
|
199
111
|
}
|
|
200
112
|
const pcm = this.pcmScratch;
|
|
201
|
-
let sumSq = 0;
|
|
202
113
|
for (let i = 0; i < pcm.length; i++) {
|
|
203
114
|
const sample = Math.max(-1, Math.min(1, float32Audio[i]));
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
const packet = this.encoder.encode(pcm, { frameSize: this.frameSize });
|
|
209
|
-
this.frameIndex++;
|
|
210
|
-
if (packet.length > TINY_FRAME_BYTES &&
|
|
211
|
-
(packet[0] & SPEECH_TOC_MASK) !== SPEECH_TOC_60MS &&
|
|
212
|
-
this.spareEncoder &&
|
|
213
|
-
this.frameIndex - this.lastRenewFrame >= RENEW_MIN_GAP_FRAMES &&
|
|
214
|
-
Math.sqrt(sumSq / pcm.length) >= RENEW_MIN_RMS) {
|
|
215
|
-
return this.renewEncoder(pcm, packet);
|
|
216
|
-
}
|
|
217
|
-
return packet;
|
|
115
|
+
pcm[i] = Math.round(sample * 32_767);
|
|
116
|
+
}
|
|
117
|
+
return this.encoder.encode(pcm, { frameSize: this.frameSize });
|
|
218
118
|
}
|
|
219
119
|
decode(mlowFrame) {
|
|
220
120
|
if (!this.decoder) {
|
|
@@ -238,10 +138,6 @@ export class MLowCodec {
|
|
|
238
138
|
const delta = (current - this.lastSeq + SEQ_SPACE) % SEQ_SPACE;
|
|
239
139
|
if (delta === 0 || delta >= SEQ_HALF) {
|
|
240
140
|
this.lateFrames++;
|
|
241
|
-
this.logger.trace('mlow packet arrived out of order', {
|
|
242
|
-
seq: current,
|
|
243
|
-
lastSeq: this.lastSeq
|
|
244
|
-
});
|
|
245
141
|
return;
|
|
246
142
|
}
|
|
247
143
|
if (delta > 1) {
|
|
@@ -299,8 +195,7 @@ export class MLowCodec {
|
|
|
299
195
|
plc: this.plcFrames,
|
|
300
196
|
fec: this.fecFrames,
|
|
301
197
|
late: this.lateFrames,
|
|
302
|
-
concealCapped: this.concealCapped
|
|
303
|
-
vadRenew: this.renewCount
|
|
198
|
+
concealCapped: this.concealCapped
|
|
304
199
|
};
|
|
305
200
|
}
|
|
306
201
|
getFrameSize() {
|
|
@@ -324,21 +219,9 @@ export class MLowCodec {
|
|
|
324
219
|
this.fecFrames = 0;
|
|
325
220
|
this.lateFrames = 0;
|
|
326
221
|
this.concealCapped = 0;
|
|
327
|
-
this.renewCount = 0;
|
|
328
222
|
this.resetSequence();
|
|
329
223
|
}
|
|
330
224
|
destroy() {
|
|
331
|
-
if (this.frameIndex > 0) {
|
|
332
|
-
this.logger.warn('[MLOW] call summary', {
|
|
333
|
-
frames: this.frameIndex,
|
|
334
|
-
renewed: this.renewCount,
|
|
335
|
-
renewedPct: Math.round((1000 * this.renewCount) / this.frameIndex) / 10
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
this.generation++;
|
|
339
|
-
this.spareBuilding = false;
|
|
340
|
-
this.spareEncoder?.free();
|
|
341
|
-
this.spareEncoder = null;
|
|
342
225
|
this.encoder?.free();
|
|
343
226
|
this.decoder?.free();
|
|
344
227
|
this.encoder = null;
|
|
@@ -349,7 +232,6 @@ export class MLowCodec {
|
|
|
349
232
|
const frames = missing > limit ? limit : missing;
|
|
350
233
|
if (frames < missing) {
|
|
351
234
|
this.concealCapped++;
|
|
352
|
-
this.logger.trace('mlow concealment capped', { missing, frames });
|
|
353
235
|
}
|
|
354
236
|
if (frames <= 0) {
|
|
355
237
|
return;
|
|
@@ -374,7 +256,6 @@ export class MLowCodec {
|
|
|
374
256
|
}
|
|
375
257
|
catch (err) {
|
|
376
258
|
this.decodeErrors++;
|
|
377
|
-
this.logger.trace('mlow decode failed', { message: toError(err).message });
|
|
378
259
|
return null;
|
|
379
260
|
}
|
|
380
261
|
}
|
|
@@ -391,9 +272,6 @@ export class MLowCodec {
|
|
|
391
272
|
return recovered;
|
|
392
273
|
}
|
|
393
274
|
catch (err) {
|
|
394
|
-
this.logger.trace('mlow fec decode failed, concealing instead', {
|
|
395
|
-
message: toError(err).message
|
|
396
|
-
});
|
|
397
275
|
return this.conceal(frameSize);
|
|
398
276
|
}
|
|
399
277
|
}
|
|
@@ -407,7 +285,6 @@ export class MLowCodec {
|
|
|
407
285
|
return concealed;
|
|
408
286
|
}
|
|
409
287
|
catch (err) {
|
|
410
|
-
this.logger.trace('mlow concealment failed', { message: toError(err).message });
|
|
411
288
|
return this.silence(frameSize);
|
|
412
289
|
}
|
|
413
290
|
}
|
|
@@ -426,17 +303,6 @@ export class MLowCodec {
|
|
|
426
303
|
const size = frameSize > MAX_FRAME_SIZE ? MAX_FRAME_SIZE : frameSize;
|
|
427
304
|
return this.silenceScratch.subarray(0, size);
|
|
428
305
|
}
|
|
429
|
-
writeCtlOn(enc, request, value) {
|
|
430
|
-
if (value === undefined || !enc) {
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
try {
|
|
434
|
-
enc.encoderCtl(request, value);
|
|
435
|
-
}
|
|
436
|
-
catch (err) {
|
|
437
|
-
this.logger.warn('mlow spare ctl rejected', { request, message: toError(err).message });
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
306
|
writeCtl(request, value) {
|
|
441
307
|
if (value === undefined || !this.encoder) {
|
|
442
308
|
return;
|