@hiroosy/baileys-lite 0.0.3 → 0.3.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/LICENSE +21 -0
- package/README.md +660 -12
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +702 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { createNoopLogger } from '../shim/core.js';
|
|
3
|
+
import { isLidJid } from '../shim/protocol.js';
|
|
4
|
+
import { hasNodeChild } from '../shim/transport.js';
|
|
5
|
+
import { resolvePositive, toError } from '../shim/util.js';
|
|
6
|
+
import { generateCallKey } from '../crypto/encryption.js';
|
|
7
|
+
import { WaAudioEngine } from '../media/WaAudioEngine.js';
|
|
8
|
+
import { parseRelayFromAck } from '../relay/relay-ack.js';
|
|
9
|
+
import { buildOfferStanza, decryptCallKey, extractNodeInfo, generateCallId } from '../signaling/signaling.js';
|
|
10
|
+
import { CallDirection, CallMediaType, CallState, EndCallReason } from '../types.js';
|
|
11
|
+
import { CallInfo } from './call-state.js';
|
|
12
|
+
import { WaCallMediaSession } from './WaCallMediaSession.js';
|
|
13
|
+
const DEFAULT_MAX_CONCURRENT_CALLS = 1;
|
|
14
|
+
export class WaCallManager extends EventEmitter {
|
|
15
|
+
deps;
|
|
16
|
+
stores;
|
|
17
|
+
logger;
|
|
18
|
+
maxConcurrentCalls;
|
|
19
|
+
calls = new Map();
|
|
20
|
+
constructor(config) {
|
|
21
|
+
super();
|
|
22
|
+
this.deps = config.deps;
|
|
23
|
+
this.stores = config.stores;
|
|
24
|
+
this.logger = config.logger ?? createNoopLogger();
|
|
25
|
+
this.maxConcurrentCalls = resolvePositive(config.maxConcurrentCalls, DEFAULT_MAX_CONCURRENT_CALLS, 'maxConcurrentCalls');
|
|
26
|
+
}
|
|
27
|
+
async startCall(options) {
|
|
28
|
+
if (this.activeCallCount >= this.maxConcurrentCalls) {
|
|
29
|
+
throw new Error(`max concurrent calls reached (${this.maxConcurrentCalls})`);
|
|
30
|
+
}
|
|
31
|
+
const callId = generateCallId();
|
|
32
|
+
const mediaType = options.isVideo ? CallMediaType.Video : CallMediaType.Audio;
|
|
33
|
+
const creds = this.deps.authClient.getCurrentCredentials();
|
|
34
|
+
const callCreator = creds?.meLid || creds?.meJid || '';
|
|
35
|
+
const peerJid = await this.resolvePeerLid(options.peerJid);
|
|
36
|
+
const info = CallInfo.newOutgoing(callId, peerJid, callCreator, mediaType);
|
|
37
|
+
const callKey = generateCallKey();
|
|
38
|
+
info.encryptionKey = callKey;
|
|
39
|
+
const session = this.createSession(info, { videoConfig: options.videoConfig });
|
|
40
|
+
try {
|
|
41
|
+
session.resetOutgoingFlags();
|
|
42
|
+
const selfLid = creds?.meLid || creds?.meJid || '';
|
|
43
|
+
await session.initMedia(selfLid, peerJid);
|
|
44
|
+
const offerStanza = await buildOfferStanza(this.deps, this.stores, callId, callKey, peerJid, options.isVideo ?? false, this.logger.child({ component: 'signaling' }));
|
|
45
|
+
await this.deps.lowLevelCoordinator.sendNode(offerStanza);
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
session.cleanup();
|
|
49
|
+
this.calls.delete(callId);
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
info.applyTransition({ type: 'offer_sent' });
|
|
53
|
+
this.emitState(info);
|
|
54
|
+
this.logger.debug('outgoing offer sent', { callId, peerJid });
|
|
55
|
+
return callId;
|
|
56
|
+
}
|
|
57
|
+
async acceptCall(callId) {
|
|
58
|
+
const session = this.getSessionOrThrow(callId);
|
|
59
|
+
if (!session.info.canAccept) {
|
|
60
|
+
throw new Error(`Call ${callId} cannot be accepted in state ${session.info.stateData.state}`);
|
|
61
|
+
}
|
|
62
|
+
await session.acceptCall();
|
|
63
|
+
}
|
|
64
|
+
async rejectCall(callId, reason = EndCallReason.Declined) {
|
|
65
|
+
const session = this.getSessionOrThrow(callId);
|
|
66
|
+
await session.rejectCall(reason);
|
|
67
|
+
this.calls.delete(callId);
|
|
68
|
+
await this.maybeUnblockWaitingCalls();
|
|
69
|
+
}
|
|
70
|
+
async endCall(callId, reason = EndCallReason.UserEnded) {
|
|
71
|
+
const session = this.calls.get(callId);
|
|
72
|
+
if (!session || session.info.isEnded)
|
|
73
|
+
return;
|
|
74
|
+
await session.endCall(reason);
|
|
75
|
+
this.calls.delete(callId);
|
|
76
|
+
await this.maybeUnblockWaitingCalls();
|
|
77
|
+
}
|
|
78
|
+
setMute(callId, muted) {
|
|
79
|
+
const session = this.calls.get(callId);
|
|
80
|
+
session?.setMute(muted);
|
|
81
|
+
}
|
|
82
|
+
async setHandRaised(callId, raised) {
|
|
83
|
+
const session = this.getSessionOrThrow(callId);
|
|
84
|
+
await session.setHandRaised(raised);
|
|
85
|
+
}
|
|
86
|
+
async setScreenShare(callId, sharing) {
|
|
87
|
+
const session = this.getSessionOrThrow(callId);
|
|
88
|
+
await session.setScreenShare(sharing);
|
|
89
|
+
}
|
|
90
|
+
sendReaction(callId, reaction) {
|
|
91
|
+
const session = this.getSessionOrThrow(callId);
|
|
92
|
+
return session.sendReaction(reaction);
|
|
93
|
+
}
|
|
94
|
+
async loadAudio(callId, audioPath) {
|
|
95
|
+
const session = this.getSessionOrThrow(callId);
|
|
96
|
+
await session.loadAudio(audioPath);
|
|
97
|
+
}
|
|
98
|
+
async loadVideo(callId, videoPath) {
|
|
99
|
+
const session = this.getSessionOrThrow(callId);
|
|
100
|
+
await session.loadVideo(videoPath);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async enableVideoMidCall(callId) {
|
|
104
|
+
const session = this.getSessionOrThrow(callId);
|
|
105
|
+
await session.enableVideoMidCall();
|
|
106
|
+
}
|
|
107
|
+
async disableVideoMidCall(callId, options) {
|
|
108
|
+
const session = this.getSessionOrThrow(callId);
|
|
109
|
+
session.disableVideoMidCall(options);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async startVideoMidCall(callId) {
|
|
113
|
+
const session = this.getSessionOrThrow(callId);
|
|
114
|
+
await session.startVideoMidCall();
|
|
115
|
+
}
|
|
116
|
+
async stopVideoMidCall(callId, options) {
|
|
117
|
+
const session = this.getSessionOrThrow(callId);
|
|
118
|
+
await session.stopVideoMidCall(options);
|
|
119
|
+
}
|
|
120
|
+
async swapVideoSource(callId, videoPath) {
|
|
121
|
+
const session = this.getSessionOrThrow(callId);
|
|
122
|
+
await session.swapVideoSource(videoPath);
|
|
123
|
+
}
|
|
124
|
+
setExternalAudioMode(callId, enabled) {
|
|
125
|
+
const session = this.getSessionOrThrow(callId);
|
|
126
|
+
session.setExternalAudioMode(enabled);
|
|
127
|
+
}
|
|
128
|
+
feedLiveAudio(callId, data) {
|
|
129
|
+
const session = this.calls.get(callId);
|
|
130
|
+
return session?.feedLiveAudio(data) ?? 0;
|
|
131
|
+
}
|
|
132
|
+
getLiveBufferMs(callId) {
|
|
133
|
+
const session = this.calls.get(callId);
|
|
134
|
+
return session?.getLiveBufferMs() ?? 0;
|
|
135
|
+
}
|
|
136
|
+
getFeedWatermarksMs() {
|
|
137
|
+
return WaAudioEngine.feedWatermarksMs();
|
|
138
|
+
}
|
|
139
|
+
getCall(callId) {
|
|
140
|
+
return this.calls.get(callId)?.info ?? null;
|
|
141
|
+
}
|
|
142
|
+
getCalls() {
|
|
143
|
+
const result = [];
|
|
144
|
+
for (const session of this.calls.values()) {
|
|
145
|
+
result.push(session.info);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
async handleCallOffer(node, peerJid) {
|
|
150
|
+
const nodeInfo = extractNodeInfo(node);
|
|
151
|
+
if (!nodeInfo?.callId)
|
|
152
|
+
return;
|
|
153
|
+
const callId = nodeInfo.callId;
|
|
154
|
+
const existing = this.calls.get(callId);
|
|
155
|
+
if (existing) {
|
|
156
|
+
if (!existing.info.isEnded) {
|
|
157
|
+
this.logger.debug('duplicate offer for active call, ignoring', { callId });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
existing.cleanup();
|
|
161
|
+
this.calls.delete(callId);
|
|
162
|
+
}
|
|
163
|
+
const callCreator = nodeInfo.innerNode.attrs?.['call-creator'] || peerJid;
|
|
164
|
+
const isVideo = hasNodeChild(nodeInfo.innerNode, 'video');
|
|
165
|
+
const callKey = await decryptCallKey(this.deps, nodeInfo.innerNode, peerJid, this.logger.child({ component: 'signaling' }));
|
|
166
|
+
const { relays, participantJids, uuid, selfPid, peerPid, hbhKey } = parseRelayFromAck(nodeInfo.innerNode);
|
|
167
|
+
const mediaType = isVideo ? CallMediaType.Video : CallMediaType.Audio;
|
|
168
|
+
const info = CallInfo.newIncoming(callId, peerJid, callCreator, undefined, mediaType);
|
|
169
|
+
if (callKey) {
|
|
170
|
+
info.encryptionKey = callKey;
|
|
171
|
+
}
|
|
172
|
+
if (relays.length > 0) {
|
|
173
|
+
info.relayData = {
|
|
174
|
+
endpoints: relays,
|
|
175
|
+
participantJids,
|
|
176
|
+
uuid,
|
|
177
|
+
selfPid,
|
|
178
|
+
peerPid,
|
|
179
|
+
hbhKey
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const atCapacity = this.activeCallCount >= this.maxConcurrentCalls;
|
|
183
|
+
const session = this.createSession(info, { acceptBlocked: atCapacity });
|
|
184
|
+
if (!atCapacity) {
|
|
185
|
+
try {
|
|
186
|
+
const creds = this.deps.authClient.getCurrentCredentials();
|
|
187
|
+
const selfLid = creds?.meLid || creds?.meJid || '';
|
|
188
|
+
await session.initMedia(selfLid, peerJid);
|
|
189
|
+
await session.sendIncomingPreaccept(peerJid);
|
|
190
|
+
await session.sendIncomingRelayLatency();
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
this.logger.error('incoming call activation failed', {
|
|
194
|
+
callId,
|
|
195
|
+
message: toError(err).message
|
|
196
|
+
});
|
|
197
|
+
try {
|
|
198
|
+
info.applyTransition({ type: 'terminated', reason: EndCallReason.Failed });
|
|
199
|
+
}
|
|
200
|
+
catch (transitionErr) {
|
|
201
|
+
this.logger.trace('failed-activation transition skipped', {
|
|
202
|
+
message: toError(transitionErr).message
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
this.emit('call_ended', info);
|
|
206
|
+
this.emitState(info);
|
|
207
|
+
session.cleanup();
|
|
208
|
+
this.calls.delete(callId);
|
|
209
|
+
await this.maybeUnblockWaitingCalls();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
this.logger.debug('incoming call waiting, at capacity', {
|
|
215
|
+
callId,
|
|
216
|
+
peerJid,
|
|
217
|
+
maxConcurrentCalls: this.maxConcurrentCalls
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
this.emit('call_incoming', info);
|
|
221
|
+
this.emitState(info);
|
|
222
|
+
this.logger.debug('incoming call', {
|
|
223
|
+
callId,
|
|
224
|
+
peerJid,
|
|
225
|
+
callCreator,
|
|
226
|
+
isVideo,
|
|
227
|
+
relayCount: relays.length,
|
|
228
|
+
acceptBlocked: atCapacity
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
async handleCallAccept(node, peerJid) {
|
|
232
|
+
const session = this.resolveSessionFromNode(node);
|
|
233
|
+
if (!session)
|
|
234
|
+
return;
|
|
235
|
+
await session.handleCallAccept(node, peerJid);
|
|
236
|
+
}
|
|
237
|
+
async handleCallPreaccept(node, peerJid) {
|
|
238
|
+
const session = this.resolveSessionFromNode(node);
|
|
239
|
+
if (!session)
|
|
240
|
+
return;
|
|
241
|
+
await session.handleCallPreaccept(node, peerJid);
|
|
242
|
+
}
|
|
243
|
+
async handleCallTransport(node, peerJid) {
|
|
244
|
+
const session = this.resolveSessionFromNode(node);
|
|
245
|
+
if (!session)
|
|
246
|
+
return;
|
|
247
|
+
await session.handleCallTransport(node);
|
|
248
|
+
}
|
|
249
|
+
async handleCallAck(node) {
|
|
250
|
+
const session = this.resolveSessionForOfferAck(node);
|
|
251
|
+
if (!session)
|
|
252
|
+
return;
|
|
253
|
+
await session.handleCallAck(node);
|
|
254
|
+
}
|
|
255
|
+
async handleCallRelaylatency(node, peerJid) {
|
|
256
|
+
const session = this.resolveSessionFromNode(node);
|
|
257
|
+
if (!session)
|
|
258
|
+
return;
|
|
259
|
+
await session.handleCallRelaylatency(node, peerJid);
|
|
260
|
+
}
|
|
261
|
+
handleRelayElection(node) {
|
|
262
|
+
const session = this.resolveSessionFromNode(node);
|
|
263
|
+
if (!session)
|
|
264
|
+
return;
|
|
265
|
+
session.handleRelayElection(node);
|
|
266
|
+
}
|
|
267
|
+
async handleCallMuteV2(node, peerJid) {
|
|
268
|
+
const session = this.resolveSessionFromNode(node);
|
|
269
|
+
if (!session)
|
|
270
|
+
return;
|
|
271
|
+
await session.handleCallMuteV2(node, peerJid);
|
|
272
|
+
}
|
|
273
|
+
handleCallUserAction(node, peerJid) {
|
|
274
|
+
const session = this.resolveSessionFromNode(node);
|
|
275
|
+
if (!session)
|
|
276
|
+
return;
|
|
277
|
+
session.handleCallUserAction(node, peerJid);
|
|
278
|
+
}
|
|
279
|
+
handleCallRaiseHand(node, peerJid) {
|
|
280
|
+
const session = this.resolveSessionFromNode(node);
|
|
281
|
+
if (!session)
|
|
282
|
+
return;
|
|
283
|
+
session.handleCallRaiseHand(node, peerJid);
|
|
284
|
+
}
|
|
285
|
+
handleCallScreenShare(node) {
|
|
286
|
+
const session = this.resolveSessionFromNode(node);
|
|
287
|
+
if (!session)
|
|
288
|
+
return;
|
|
289
|
+
session.handleCallScreenShare(node);
|
|
290
|
+
}
|
|
291
|
+
async handleCallVideo(node) {
|
|
292
|
+
const session = this.resolveSessionFromNode(node);
|
|
293
|
+
if (!session)
|
|
294
|
+
return;
|
|
295
|
+
await session.handleCallVideo(node);
|
|
296
|
+
}
|
|
297
|
+
async handleCallTerminate(node) {
|
|
298
|
+
const session = this.resolveSessionFromNode(node);
|
|
299
|
+
if (!session)
|
|
300
|
+
return;
|
|
301
|
+
session.handleCallTerminate();
|
|
302
|
+
this.calls.delete(session.callId);
|
|
303
|
+
await this.maybeUnblockWaitingCalls();
|
|
304
|
+
}
|
|
305
|
+
destroy() {
|
|
306
|
+
for (const session of this.calls.values()) {
|
|
307
|
+
session.cleanup();
|
|
308
|
+
}
|
|
309
|
+
this.calls.clear();
|
|
310
|
+
this.removeAllListeners();
|
|
311
|
+
}
|
|
312
|
+
get activeCallCount() {
|
|
313
|
+
let count = 0;
|
|
314
|
+
for (const session of this.calls.values()) {
|
|
315
|
+
if (!session.info.isEnded && !session.info.isAcceptBlocked)
|
|
316
|
+
count++;
|
|
317
|
+
}
|
|
318
|
+
return count;
|
|
319
|
+
}
|
|
320
|
+
createSession(info, options = {}) {
|
|
321
|
+
const prior = this.calls.get(info.callId);
|
|
322
|
+
if (prior) {
|
|
323
|
+
if (!prior.info.isEnded) {
|
|
324
|
+
throw new Error(`call ${info.callId} already exists`);
|
|
325
|
+
}
|
|
326
|
+
prior.cleanup();
|
|
327
|
+
this.calls.delete(info.callId);
|
|
328
|
+
}
|
|
329
|
+
const acceptBlocked = options.acceptBlocked ?? false;
|
|
330
|
+
if (!acceptBlocked && this.activeCallCount >= this.maxConcurrentCalls) {
|
|
331
|
+
throw new Error(`max concurrent calls reached (${this.maxConcurrentCalls})`);
|
|
332
|
+
}
|
|
333
|
+
if (acceptBlocked) {
|
|
334
|
+
info.stateData.acceptBlocked = true;
|
|
335
|
+
}
|
|
336
|
+
const sessionLogger = this.logger.child({ callId: info.callId });
|
|
337
|
+
const session = new WaCallMediaSession({
|
|
338
|
+
deps: this.deps,
|
|
339
|
+
logger: sessionLogger,
|
|
340
|
+
info,
|
|
341
|
+
videoConfig: options.videoConfig,
|
|
342
|
+
delegate: {
|
|
343
|
+
emitState: (call) => this.emitState(call),
|
|
344
|
+
emitIncoming: (call) => this.emit('call_incoming', call),
|
|
345
|
+
emitEnded: (call) => this.emit('call_ended', call),
|
|
346
|
+
emitInboundAudio: (call, pcm) => this.emit('call_inbound_audio', call, pcm),
|
|
347
|
+
emitPeerMute: (call, muted) => this.emit('call_peer_mute', call, muted),
|
|
348
|
+
emitHandRaise: (call, participantJid, raised) => this.emit('call_hand_raise', call, participantJid, raised),
|
|
349
|
+
emitCallReaction: (call, reaction) => this.emit('call_reaction', call, reaction),
|
|
350
|
+
emitScreenShare: (call, share) => this.emit('call_screen_share', call, share),
|
|
351
|
+
emitOutboundAudioFinished: (call) => this.emit('call_outbound_audio_finished', call),
|
|
352
|
+
|
|
353
|
+
emitVideoState: (call, videoState) => this.emit('call_video_state', call, videoState),
|
|
354
|
+
|
|
355
|
+
emitInboundVideo: (call, au) => this.emit('call_inbound_video', call, au)
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
this.calls.set(info.callId, session);
|
|
359
|
+
return session;
|
|
360
|
+
}
|
|
361
|
+
getSessionOrThrow(callId) {
|
|
362
|
+
const session = this.calls.get(callId);
|
|
363
|
+
if (!session) {
|
|
364
|
+
throw new Error(`No call with id ${callId}`);
|
|
365
|
+
}
|
|
366
|
+
return session;
|
|
367
|
+
}
|
|
368
|
+
resolveSessionFromNode(node) {
|
|
369
|
+
const nodeInfo = extractNodeInfo(node);
|
|
370
|
+
if (!nodeInfo?.callId) {
|
|
371
|
+
this.logger.debug('stanza missing call-id, ignored');
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
const session = this.calls.get(nodeInfo.callId);
|
|
375
|
+
if (!session) {
|
|
376
|
+
this.logger.debug('no session for call-id', { callId: nodeInfo.callId });
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
return session;
|
|
380
|
+
}
|
|
381
|
+
resolveSessionForOfferAck(node) {
|
|
382
|
+
const callId = node.attrs?.['call-id'];
|
|
383
|
+
if (callId) {
|
|
384
|
+
const session = this.calls.get(callId);
|
|
385
|
+
if (session)
|
|
386
|
+
return session;
|
|
387
|
+
}
|
|
388
|
+
const outgoing = [];
|
|
389
|
+
for (const session of this.calls.values()) {
|
|
390
|
+
if (session.info.isInitiator && !session.info.isEnded) {
|
|
391
|
+
const state = session.info.stateData.state;
|
|
392
|
+
if (state === CallState.Initiating || state === CallState.Ringing) {
|
|
393
|
+
outgoing.push(session);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (outgoing.length === 1)
|
|
398
|
+
return outgoing[0];
|
|
399
|
+
|
|
400
|
+
this.logger.trace('offer ack could not be routed', {
|
|
401
|
+
callId: callId ?? null,
|
|
402
|
+
candidateCount: outgoing.length
|
|
403
|
+
});
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
emitState(call) {
|
|
407
|
+
this.emit('call_state', call);
|
|
408
|
+
}
|
|
409
|
+
async resolvePeerLid(peerJid) {
|
|
410
|
+
if (isLidJid(peerJid))
|
|
411
|
+
return peerJid;
|
|
412
|
+
try {
|
|
413
|
+
const [mapped] = await this.deps.signalDeviceSync.queryLidsByPhoneJids([peerJid]);
|
|
414
|
+
if (mapped?.lidJid)
|
|
415
|
+
return mapped.lidJid;
|
|
416
|
+
}
|
|
417
|
+
catch (err) {
|
|
418
|
+
this.logger.trace('lid resolution failed', { message: toError(err).message });
|
|
419
|
+
}
|
|
420
|
+
return peerJid;
|
|
421
|
+
}
|
|
422
|
+
async maybeUnblockWaitingCalls() {
|
|
423
|
+
while (this.activeCallCount < this.maxConcurrentCalls) {
|
|
424
|
+
const waiting = [...this.calls.values()].find((session) => session.info.direction === CallDirection.Incoming &&
|
|
425
|
+
session.info.isRinging &&
|
|
426
|
+
session.info.isAcceptBlocked);
|
|
427
|
+
if (!waiting)
|
|
428
|
+
break;
|
|
429
|
+
await this.activateWaitingIncoming(waiting);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
async activateWaitingIncoming(session) {
|
|
433
|
+
session.info.stateData.acceptBlocked = false;
|
|
434
|
+
const creds = this.deps.authClient.getCurrentCredentials();
|
|
435
|
+
const selfLid = creds?.meLid || creds?.meJid || '';
|
|
436
|
+
await session.initMedia(selfLid, session.info.peerJid);
|
|
437
|
+
await session.sendIncomingPreaccept(session.info.peerJid);
|
|
438
|
+
await session.sendIncomingRelayLatency();
|
|
439
|
+
this.emitState(session.info);
|
|
440
|
+
this.logger.debug('waiting incoming call unblocked', { callId: session.callId });
|
|
441
|
+
}
|
|
442
|
+
}
|