@hiroosy/baileys-lite 0.3.9 → 0.4.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/README.md +97 -6
- package/package.json +1 -1
- package/src/voip/WaVoipCoordinator.js +57 -1
- package/src/voip/call/WaCallManager.js +265 -6
- package/src/voip/call/WaCallMediaSession.js +865 -10
- package/src/voip/call/call-state.js +45 -0
- package/src/voip/crypto/rtcp.js +6 -0
- package/src/voip/crypto/srtp.js +5 -0
- package/src/voip/crypto/ssrc.js +10 -0
- package/src/voip/index.js +212 -16
- package/src/voip/media/WaAudioEngine.js +25 -16
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/mlow-codec.js +12 -8
- package/src/voip/relay/WaManualRelay.js +45 -6
- package/src/voip/relay/sctp/association.js +14 -1
- package/src/voip/relay/stun.js +52 -1
- package/src/voip/signaling/bridge.js +15 -1
- package/src/voip/signaling/signaling.js +432 -1
- package/src/voip/types.js +1 -0
- package/src/voip/voip-deps.js +7 -0
- package/src/voip/voipClient.js +152 -17
package/src/voip/voipClient.js
CHANGED
|
@@ -25,15 +25,16 @@ function createVoipCtx(sock, deps, stores, logger, emitter) {
|
|
|
25
25
|
});
|
|
26
26
|
};
|
|
27
27
|
const key = `CB:${tag}`;
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
const ws = sock.ws;
|
|
29
|
+
if (prepend && typeof ws.prependListener === 'function') {
|
|
30
|
+
ws.prependListener(key, listener);
|
|
30
31
|
}
|
|
31
32
|
else {
|
|
32
|
-
|
|
33
|
+
ws.on(key, listener);
|
|
33
34
|
}
|
|
34
35
|
return () => {
|
|
35
36
|
try {
|
|
36
|
-
|
|
37
|
+
ws.off(key, listener);
|
|
37
38
|
}
|
|
38
39
|
catch { }
|
|
39
40
|
};
|
|
@@ -82,6 +83,20 @@ export class ActiveCall extends EventEmitter {
|
|
|
82
83
|
shareScreen = (sharing = true) => this.#coordinator.setScreenShare(this.callId, !!sharing);
|
|
83
84
|
react = (emoji) => this.#coordinator.sendReaction(this.callId, emoji);
|
|
84
85
|
upgradeToVideo = () => this.#coordinator.startVideoMidCall(this.callId);
|
|
86
|
+
addParticipant = (target) => this.#coordinator.addGroupParticipant(this.callId, target);
|
|
87
|
+
ringParticipant = (target) => this.#coordinator.ringGroupParticipant(this.callId, target);
|
|
88
|
+
setApprovalRequired = (enabled) => this.#coordinator.setApprovalRequired(this.callId, !!enabled);
|
|
89
|
+
admit = (jid) => this.#coordinator.admitParticipant(this.callId, jid);
|
|
90
|
+
deny = (jid) => this.#coordinator.denyParticipant(this.callId, jid);
|
|
91
|
+
get isGroup() {
|
|
92
|
+
return !!this.#coordinator.getCall(this.callId)?.isGroup;
|
|
93
|
+
}
|
|
94
|
+
get participants() {
|
|
95
|
+
return this.#coordinator.getCall(this.callId)?.groupState?.participants ?? [];
|
|
96
|
+
}
|
|
97
|
+
get waitingRoom() {
|
|
98
|
+
return this.#coordinator.getCall(this.callId)?.waitingRoom ?? null;
|
|
99
|
+
}
|
|
85
100
|
end = async () => {
|
|
86
101
|
if (this.#ended)
|
|
87
102
|
return;
|
|
@@ -123,6 +138,11 @@ export class VoipClient {
|
|
|
123
138
|
#activeCalls = new Map();
|
|
124
139
|
#offSocketClose = null;
|
|
125
140
|
#connecting = null;
|
|
141
|
+
#boundEv = null;
|
|
142
|
+
#logger = null;
|
|
143
|
+
#signalingDown = false;
|
|
144
|
+
#watchTimer = null;
|
|
145
|
+
#deadlineTimer = null;
|
|
126
146
|
constructor(config) {
|
|
127
147
|
this.#config = config;
|
|
128
148
|
if (!config?.existingSocket) {
|
|
@@ -136,6 +156,8 @@ export class VoipClient {
|
|
|
136
156
|
return [...this.#activeCalls.values()];
|
|
137
157
|
}
|
|
138
158
|
connect = () => {
|
|
159
|
+
if (this.#signalingDown)
|
|
160
|
+
return Promise.reject(new Error('Connection is reconnecting, try again once it is restored.'));
|
|
139
161
|
if (this.#coordinator && this.#sock === this.#config.existingSocket) {
|
|
140
162
|
return Promise.resolve();
|
|
141
163
|
}
|
|
@@ -150,34 +172,81 @@ export class VoipClient {
|
|
|
150
172
|
this.#sock = this.#config.existingSocket;
|
|
151
173
|
const { deps, stores } = await createVoipDeps(this.#sock);
|
|
152
174
|
const logger = createConsoleLogger(this.#config.voipLogLevel ?? 'warn');
|
|
175
|
+
this.#logger = logger;
|
|
153
176
|
const emitter = new EventEmitter();
|
|
154
177
|
const ctx = createVoipCtx(this.#sock, deps, stores, logger, emitter);
|
|
155
178
|
this.#coordinator = new WaVoipCoordinator(ctx, {
|
|
156
179
|
maxConcurrentCalls: this.#config.maxConcurrentCalls ?? 1,
|
|
157
180
|
logLevel: this.#config.voipLogLevel ?? 'warn'
|
|
158
181
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
this.#teardown('connection_closed');
|
|
167
|
-
};
|
|
168
|
-
sock.ev?.on?.('connection.update', onUpdate);
|
|
182
|
+
this.#listenForSocket();
|
|
183
|
+
};
|
|
184
|
+
#listenForSocket() {
|
|
185
|
+
const ev = this.#sock.ev;
|
|
186
|
+
const onUpdate = (update) => this.#onConnectionUpdate(update);
|
|
187
|
+
ev?.on?.('connection.update', onUpdate);
|
|
188
|
+
this.#boundEv = ev;
|
|
169
189
|
this.#offSocketClose = () => {
|
|
170
190
|
try {
|
|
171
|
-
|
|
191
|
+
ev?.off?.('connection.update', onUpdate);
|
|
172
192
|
}
|
|
173
193
|
catch { }
|
|
174
194
|
};
|
|
175
|
-
}
|
|
195
|
+
}
|
|
196
|
+
#onConnectionUpdate(update) {
|
|
197
|
+
if (update?.connection === 'open') {
|
|
198
|
+
this.#restoreSignaling();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (update?.connection !== 'close')
|
|
202
|
+
return;
|
|
203
|
+
const statusCode = update?.lastDisconnect?.error?.output?.statusCode
|
|
204
|
+
?? update?.lastDisconnect?.error?.output?.payload?.statusCode;
|
|
205
|
+
if ([401, 403, 411, 440, 500].includes(statusCode) && !(statusCode === 500 && /^WebSocket Error/i.test(String(update?.lastDisconnect?.error?.message || '')))) {
|
|
206
|
+
this.#logger?.warn('voip: socket closed for good, tearing down active calls', { statusCode });
|
|
207
|
+
this.#teardown('connection_closed');
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (this.#signalingDown)
|
|
211
|
+
return;
|
|
212
|
+
this.#logger?.warn('voip: socket closed, keeping active calls until it reconnects');
|
|
213
|
+
this.#suspendSignaling();
|
|
214
|
+
}
|
|
215
|
+
#suspendSignaling() {
|
|
216
|
+
this.#signalingDown = true;
|
|
217
|
+
this.#offSocketClose?.();
|
|
218
|
+
this.#offSocketClose = null;
|
|
219
|
+
this.#coordinator?.suspendIncoming();
|
|
220
|
+
this.#coordinator?.notifyReconnecting();
|
|
221
|
+
this.#watchTimer = setInterval(() => this.#watchSocket(), 50);
|
|
222
|
+
this.#deadlineTimer = setTimeout(() => this.#teardown('connection_closed'), 120_000);
|
|
223
|
+
}
|
|
224
|
+
#watchSocket() {
|
|
225
|
+
if (!this.#signalingDown || !this.#sock || this.#sock.ev === this.#boundEv)
|
|
226
|
+
return;
|
|
227
|
+
this.#listenForSocket();
|
|
228
|
+
this.#coordinator?.suspendIncoming();
|
|
229
|
+
this.#coordinator?.resumeIncoming();
|
|
230
|
+
}
|
|
231
|
+
#restoreSignaling() {
|
|
232
|
+
if (!this.#signalingDown)
|
|
233
|
+
return;
|
|
234
|
+
this.#clearSignalingState();
|
|
235
|
+
this.#coordinator?.notifyReconnected();
|
|
236
|
+
}
|
|
237
|
+
#clearSignalingState() {
|
|
238
|
+
this.#signalingDown = false;
|
|
239
|
+
clearInterval(this.#watchTimer);
|
|
240
|
+
clearTimeout(this.#deadlineTimer);
|
|
241
|
+
this.#watchTimer = null;
|
|
242
|
+
this.#deadlineTimer = null;
|
|
243
|
+
}
|
|
176
244
|
#teardown(reason) {
|
|
177
245
|
const calls = [...this.#activeCalls.values()];
|
|
178
246
|
this.#activeCalls.clear();
|
|
179
247
|
this.#offSocketClose?.();
|
|
180
248
|
this.#offSocketClose = null;
|
|
249
|
+
this.#clearSignalingState();
|
|
181
250
|
for (const call of calls) {
|
|
182
251
|
try {
|
|
183
252
|
call._forceEnd(reason);
|
|
@@ -190,6 +259,7 @@ export class VoipClient {
|
|
|
190
259
|
catch { }
|
|
191
260
|
this.#sock = null;
|
|
192
261
|
this.#coordinator = null;
|
|
262
|
+
this.#boundEv = null;
|
|
193
263
|
}
|
|
194
264
|
call = async (phoneNumber, opts = {}) => {
|
|
195
265
|
const coordinator = this.#coordinator;
|
|
@@ -226,6 +296,68 @@ export class VoipClient {
|
|
|
226
296
|
}
|
|
227
297
|
return this.#track(coordinator, callId, durationMs);
|
|
228
298
|
};
|
|
299
|
+
#loadGroupMedia = async (coordinator, callId, opts) => {
|
|
300
|
+
const audioFile = opts.audioSource && opts.audioSource !== 'silence' ? opts.audioSource : undefined;
|
|
301
|
+
if (audioFile) {
|
|
302
|
+
try {
|
|
303
|
+
await coordinator.loadAudio(callId, audioFile);
|
|
304
|
+
} catch (e) {
|
|
305
|
+
console.error(`[ VOIP ] Failed to load audio "${audioFile}" for call ${callId}:`, e?.message || e);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (opts.videoSource) {
|
|
309
|
+
try {
|
|
310
|
+
await coordinator.loadVideo(callId, opts.videoSource);
|
|
311
|
+
} catch (e) {
|
|
312
|
+
console.error(`[ VOIP ] Failed to load video "${opts.videoSource}" for call ${callId}:`, e?.message || e);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (this.#coordinator !== coordinator) {
|
|
316
|
+
throw new Error('Connection closed while placing the call.');
|
|
317
|
+
}
|
|
318
|
+
const call = this.#track(coordinator, callId, opts.durationMs ?? 0);
|
|
319
|
+
setImmediate(() => {
|
|
320
|
+
const info = coordinator.getCall(callId);
|
|
321
|
+
if (info && !call.ended) {
|
|
322
|
+
call._onState(info);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
return call;
|
|
326
|
+
};
|
|
327
|
+
groupCall = async (targets, opts = {}) => {
|
|
328
|
+
const coordinator = this.#coordinator;
|
|
329
|
+
if (!this.#sock || !coordinator)
|
|
330
|
+
throw new Error('Not connected. Call connect() first.');
|
|
331
|
+
const callId = await coordinator.startGroupCall({
|
|
332
|
+
targets,
|
|
333
|
+
groupJid: opts.groupJid,
|
|
334
|
+
isVideo: !!opts.isVideo,
|
|
335
|
+
videoConfig: opts.videoConfig
|
|
336
|
+
});
|
|
337
|
+
return this.#loadGroupMedia(coordinator, callId, opts);
|
|
338
|
+
};
|
|
339
|
+
createLink = async (opts = {}) => {
|
|
340
|
+
const coordinator = this.#coordinator;
|
|
341
|
+
if (!this.#sock || !coordinator)
|
|
342
|
+
throw new Error('Not connected. Call connect() first.');
|
|
343
|
+
return coordinator.createCallLink({ isVideo: !!opts.isVideo });
|
|
344
|
+
};
|
|
345
|
+
queryLink = async (linkOrToken, opts = {}) => {
|
|
346
|
+
const coordinator = this.#coordinator;
|
|
347
|
+
if (!this.#sock || !coordinator)
|
|
348
|
+
throw new Error('Not connected. Call connect() first.');
|
|
349
|
+
return coordinator.queryCallLink(linkOrToken, { isVideo: opts.isVideo });
|
|
350
|
+
};
|
|
351
|
+
joinLink = async (linkOrToken, opts = {}) => {
|
|
352
|
+
const coordinator = this.#coordinator;
|
|
353
|
+
if (!this.#sock || !coordinator)
|
|
354
|
+
throw new Error('Not connected. Call connect() first.');
|
|
355
|
+
const callId = await coordinator.joinCallLink(linkOrToken, {
|
|
356
|
+
isVideo: opts.isVideo,
|
|
357
|
+
videoConfig: opts.videoConfig
|
|
358
|
+
});
|
|
359
|
+
return this.#loadGroupMedia(coordinator, callId, opts);
|
|
360
|
+
};
|
|
229
361
|
#track = (coordinator, callId, durationMs) => {
|
|
230
362
|
const call = new ActiveCall(coordinator, callId, durationMs);
|
|
231
363
|
this.#activeCalls.set(callId, call);
|
|
@@ -247,7 +379,10 @@ export class VoipClient {
|
|
|
247
379
|
['call_screen_share', 'screen_share', (c, share) => [share]],
|
|
248
380
|
['call_video_state', 'peer_video', (c, change) => [change]],
|
|
249
381
|
['call_inbound_audio', 'inbound_audio', (c, pcm) => [pcm]],
|
|
250
|
-
['call_inbound_video', 'inbound_video', (c, frame) => [frame]],
|
|
382
|
+
['call_inbound_video', 'inbound_video', (c, frame, participantJid) => [frame, participantJid]],
|
|
383
|
+
['call_group_state', 'group_state', (c, state) => [state]],
|
|
384
|
+
['call_waiting_room', 'waiting_room', (c, room) => [room]],
|
|
385
|
+
['call_participant_audio', 'participant_audio', (c, participantJid, pcm) => [participantJid, pcm]],
|
|
251
386
|
['call_outbound_audio_finished', 'audio_finished', () => []],
|
|
252
387
|
['call_reconnecting', 'reconnecting', (c, detail) => [detail]],
|
|
253
388
|
['call_reconnected', 'reconnected', () => []]
|