amiudmodz 6.2.7 → 6.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/lib/Socket/business.d.ts +6 -1
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/communities.d.ts +6 -1
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +17 -4
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +6 -1
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-send.d.ts +6 -1
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +85 -21
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Socket/newsletter.d.ts.map +1 -1
- package/lib/Socket/newsletter.js +4 -5
- package/lib/Socket/newsletter.js.map +1 -1
- package/lib/Utils/baileys-event-stream.js +0 -0
- package/lib/Utils/crypto.d.ts.map +1 -1
- package/lib/Utils/crypto.js +6 -7
- package/lib/Utils/crypto.js.map +1 -1
- package/lib/VoIP/audio-feeder.d.ts +42 -4
- package/lib/VoIP/audio-feeder.d.ts.map +1 -1
- package/lib/VoIP/audio-feeder.js +252 -128
- package/lib/VoIP/audio-feeder.js.map +1 -1
- package/lib/VoIP/index.d.ts +91 -8
- package/lib/VoIP/index.d.ts.map +1 -1
- package/lib/VoIP/index.js +834 -184
- package/lib/VoIP/index.js.map +1 -1
- package/lib/VoIP/relay-transport.d.ts +26 -1
- package/lib/VoIP/relay-transport.d.ts.map +1 -1
- package/lib/VoIP/relay-transport.js +257 -181
- package/lib/VoIP/relay-transport.js.map +1 -1
- package/lib/VoIP/signaling.d.ts +57 -3
- package/lib/VoIP/signaling.d.ts.map +1 -1
- package/lib/VoIP/signaling.js +791 -328
- package/lib/VoIP/signaling.js.map +1 -1
- package/lib/VoIP/types.d.ts +12 -11
- package/lib/VoIP/types.d.ts.map +1 -1
- package/lib/VoIP/types.js +4 -9
- package/lib/VoIP/types.js.map +1 -1
- package/lib/VoIP/video-feeder.d.ts +53 -0
- package/lib/VoIP/video-feeder.d.ts.map +1 -0
- package/lib/VoIP/video-feeder.js +269 -0
- package/lib/VoIP/video-feeder.js.map +1 -0
- package/lib/VoIP/wasm-engine.d.ts +10 -2
- package/lib/VoIP/wasm-engine.d.ts.map +1 -1
- package/lib/VoIP/wasm-engine.js +179 -71
- package/lib/VoIP/wasm-engine.js.map +1 -1
- package/lib/VoIP/worker-bootstrap.js +29 -51
- package/lib/VoIP/worker-bootstrap.js.map +1 -1
- package/lib/WABinary/decode.d.ts.map +1 -1
- package/lib/WABinary/decode.js +10 -0
- package/lib/WABinary/decode.js.map +1 -1
- package/lib/WABinary/encode.d.ts.map +1 -1
- package/lib/WABinary/encode.js +10 -0
- package/lib/WABinary/encode.js.map +1 -1
- package/lib/WABinary/jid-utils.d.ts.map +1 -1
- package/lib/WABinary/jid-utils.js +18 -17
- package/lib/WABinary/jid-utils.js.map +1 -1
- package/lib/assets/wasm/README.md +76 -0
- package/lib/assets/wasm/loader.js +0 -1
- package/lib/assets/wasm/loader.js.map +1 -1
- package/lib/assets/wasm/worker-modules.js +0 -1
- package/lib/assets/wasm/worker-modules.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +146 -142
package/lib/VoIP/index.js
CHANGED
|
@@ -1,32 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _ActiveCall_state, _ActiveCall_endResolver, _ActiveCall_endPromise, _ActiveCall_endTimer, _ActiveCall_ended, _ActiveCall_hasBeenActive, _VoipClient_config, _VoipClient_engine, _VoipClient_relay, _VoipClient_signaling, _VoipClient_sock, _VoipClient_activeCall, _VoipClient_capturePtr, _VoipClient_captureChunkBytes, _VoipClient_captureSampleRate, _VoipClient_captureChannels, _VoipClient_captureFramesPerChunk, _VoipClient_feeder, _VoipClient_initVoipStack, _VoipClient_handleCallEvent, _VoipClient_handleAudioCaptureInit, _VoipClient_handleAudioCaptureStart, _VoipClient_handleAudioCaptureStop;
|
|
13
|
-
import { EventEmitter } from "node:events";
|
|
14
|
-
import { randomBytes, createHmac } from "node:crypto";
|
|
15
|
-
import { WasmEngine } from "./wasm-engine.js";
|
|
16
|
-
import { RelayRtcTransport } from "./relay-transport.js";
|
|
17
|
-
import { SignalingBridge } from "./signaling.js";
|
|
18
|
-
import { AudioFeeder } from "./audio-feeder.js";
|
|
19
|
-
import { CallState } from "./types.js";
|
|
20
|
-
export { CallState } from "./types.js";
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { createHmac, randomBytes } from 'node:crypto';
|
|
3
|
+
import { WasmEngine } from './wasm-engine.js';
|
|
4
|
+
import { RelayRtcTransport } from './relay-transport.js';
|
|
5
|
+
import { SignalingBridge } from './signaling.js';
|
|
6
|
+
import { AudioFeeder } from './audio-feeder.js';
|
|
7
|
+
import { VideoFeeder, VIDEO_FORMAT_I420 } from './video-feeder.js';
|
|
8
|
+
import { CallState } from './types.js';
|
|
9
|
+
export { CallState };
|
|
21
10
|
const SHA256_LEN = 32;
|
|
22
|
-
const toBareJid =
|
|
11
|
+
const toBareJid = jid => {
|
|
23
12
|
if (!jid)
|
|
24
13
|
return jid;
|
|
25
|
-
const
|
|
14
|
+
const value = String(jid);
|
|
15
|
+
const at = value.indexOf("@");
|
|
26
16
|
if (at < 0)
|
|
27
|
-
return
|
|
28
|
-
const user =
|
|
29
|
-
return `${user}@${
|
|
17
|
+
return value;
|
|
18
|
+
const user = value.slice(0, at).split(":")[0];
|
|
19
|
+
return `${user}@${value.slice(at + 1)}`;
|
|
30
20
|
};
|
|
31
21
|
const computeHkdf = (key, salt, info, length) => {
|
|
32
22
|
const effectiveSalt = salt && salt.length > 0 ? Buffer.from(salt) : Buffer.alloc(SHA256_LEN, 0);
|
|
@@ -48,7 +38,7 @@ const computeHmacSha256 = (data, key) => {
|
|
|
48
38
|
const result = createHmac("sha256", Buffer.from(key)).update(data).digest();
|
|
49
39
|
return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
|
|
50
40
|
};
|
|
51
|
-
const isCallReceiptNode =
|
|
41
|
+
const isCallReceiptNode = node => {
|
|
52
42
|
if (node?.tag !== "receipt")
|
|
53
43
|
return false;
|
|
54
44
|
const child = Array.isArray(node.content) ? node.content[0] : null;
|
|
@@ -57,235 +47,895 @@ const isCallReceiptNode = (node) => {
|
|
|
57
47
|
export class ActiveCall extends EventEmitter {
|
|
58
48
|
constructor(callId, engine, durationMs) {
|
|
59
49
|
super();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
this.callId = callId;
|
|
51
|
+
this.engine = engine;
|
|
52
|
+
this._state = CallState.Idle;
|
|
53
|
+
this._endResolver = undefined;
|
|
54
|
+
this._endPromise = undefined;
|
|
55
|
+
this._endTimer = null;
|
|
56
|
+
this._ended = false;
|
|
66
57
|
this._audioSource = "silence";
|
|
67
58
|
this.end = () => {
|
|
68
|
-
if (
|
|
59
|
+
if (this._ended)
|
|
69
60
|
return;
|
|
70
|
-
__classPrivateFieldSet(this, _ActiveCall_ended, true, "f");
|
|
71
|
-
if (__classPrivateFieldGet(this, _ActiveCall_endTimer, "f")) {
|
|
72
|
-
clearTimeout(__classPrivateFieldGet(this, _ActiveCall_endTimer, "f"));
|
|
73
|
-
__classPrivateFieldSet(this, _ActiveCall_endTimer, null, "f");
|
|
74
|
-
}
|
|
75
61
|
try {
|
|
76
62
|
this.engine.endCall(0, true);
|
|
77
63
|
}
|
|
78
64
|
catch { }
|
|
65
|
+
this._forceEnd("local_end");
|
|
79
66
|
};
|
|
80
|
-
this.mute =
|
|
67
|
+
this.mute = muted => {
|
|
81
68
|
try {
|
|
82
69
|
this.engine.setMute(muted);
|
|
83
70
|
}
|
|
84
71
|
catch { }
|
|
85
72
|
};
|
|
86
|
-
this.waitForEnd = () =>
|
|
87
|
-
this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
73
|
+
this.waitForEnd = () => this._endPromise;
|
|
74
|
+
this._playlist = [];
|
|
75
|
+
this._feeder = null;
|
|
76
|
+
this._idleTimer = null;
|
|
77
|
+
this._endWhenQueueEmpty = true;
|
|
78
|
+
this._idleGraceMs = 0;
|
|
79
|
+
this._attachFeeder = feeder => {
|
|
80
|
+
this._feeder = feeder;
|
|
81
|
+
};
|
|
82
|
+
this.play = source => {
|
|
83
|
+
this._playlist = [];
|
|
84
|
+
if (this._feeder) {
|
|
85
|
+
this._feeder.clearPlaylist();
|
|
86
|
+
this._feeder.enqueue(source);
|
|
87
|
+
this._feeder.skip();
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
this._playlist.push(source);
|
|
91
|
+
return false;
|
|
92
|
+
};
|
|
93
|
+
this.enqueue = source => {
|
|
94
|
+
this._clearIdleTimer();
|
|
95
|
+
if (this._feeder) {
|
|
96
|
+
return this._feeder.enqueue(source);
|
|
97
|
+
}
|
|
98
|
+
const tracks = Array.isArray(source) ? source : [source];
|
|
99
|
+
for (const track of tracks) {
|
|
100
|
+
if (track) {
|
|
101
|
+
this._playlist.push(track);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return this._playlist.length;
|
|
105
|
+
};
|
|
106
|
+
this.skip = () => this._feeder?.skip() ?? false;
|
|
107
|
+
this.queued = () => this._feeder?.playlistLength ?? this._playlist.length;
|
|
108
|
+
this.nowPlaying = () => this._feeder?.currentTrack ?? null;
|
|
109
|
+
this._video = false;
|
|
110
|
+
this._videoSource = "black";
|
|
111
|
+
this._videoPlaylist = [];
|
|
112
|
+
this._videoFeeder = null;
|
|
113
|
+
this._attachVideoFeeder = feeder => {
|
|
114
|
+
this._videoFeeder = feeder;
|
|
115
|
+
};
|
|
116
|
+
this.playVideo = source => {
|
|
117
|
+
this._videoPlaylist = [];
|
|
118
|
+
if (this._videoFeeder) {
|
|
119
|
+
this._videoFeeder.clearPlaylist();
|
|
120
|
+
this._videoFeeder.enqueue(source);
|
|
121
|
+
this._videoFeeder.skip();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
this._videoPlaylist.push(source);
|
|
125
|
+
return false;
|
|
126
|
+
};
|
|
127
|
+
this.enqueueVideo = source => {
|
|
128
|
+
if (this._videoFeeder) {
|
|
129
|
+
return this._videoFeeder.enqueue(source);
|
|
130
|
+
}
|
|
131
|
+
const tracks = Array.isArray(source) ? source : [source];
|
|
132
|
+
for (const track of tracks) {
|
|
133
|
+
if (track) {
|
|
134
|
+
this._videoPlaylist.push(track);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return this._videoPlaylist.length;
|
|
138
|
+
};
|
|
139
|
+
this.skipVideo = () => this._videoFeeder?.skip() ?? false;
|
|
140
|
+
this.queuedVideo = () => this._videoFeeder?.playlistLength ?? this._videoPlaylist.length;
|
|
141
|
+
this.nowPlayingVideo = () => this._videoFeeder?.currentTrack ?? null;
|
|
142
|
+
this.isVideo = () => this._video;
|
|
143
|
+
this._screenShare = false;
|
|
144
|
+
this.startScreenShare = () => {
|
|
145
|
+
if (!this._video) {
|
|
146
|
+
throw new Error("screen share needs a video call: pass video: true, or screenShare: true which implies it");
|
|
147
|
+
}
|
|
148
|
+
this._screenShare = true;
|
|
149
|
+
try {
|
|
150
|
+
return this.engine.startScreenShare();
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
this.stopScreenShare = () => {
|
|
157
|
+
this._screenShare = false;
|
|
158
|
+
try {
|
|
159
|
+
return this.engine.stopScreenShare();
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
this.isScreenShare = () => this._screenShare;
|
|
166
|
+
this._onVideoTrackStart = track => {
|
|
167
|
+
this.emit("videotrack", track);
|
|
168
|
+
};
|
|
169
|
+
this._onVideoTrackEnd = track => {
|
|
170
|
+
this.emit("videotrackend", track);
|
|
171
|
+
};
|
|
172
|
+
this._clearIdleTimer = () => {
|
|
173
|
+
if (this._idleTimer) {
|
|
174
|
+
clearTimeout(this._idleTimer);
|
|
175
|
+
this._idleTimer = null;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
this._onTrackStart = track => {
|
|
179
|
+
this._clearIdleTimer();
|
|
180
|
+
this.emit("track", track);
|
|
181
|
+
};
|
|
182
|
+
this._onTrackEnd = track => {
|
|
183
|
+
this.emit("trackend", track);
|
|
184
|
+
};
|
|
185
|
+
this._onPlaylistIdle = () => {
|
|
186
|
+
this.emit("idle");
|
|
187
|
+
if (!this._endWhenQueueEmpty) {
|
|
188
|
+
return;
|
|
91
189
|
}
|
|
92
|
-
|
|
190
|
+
this._clearIdleTimer();
|
|
191
|
+
this._idleTimer = setTimeout(() => {
|
|
192
|
+
this._idleTimer = null;
|
|
193
|
+
if (this._feeder?.playlistLength || this._feeder?.currentTrack) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
this.end();
|
|
197
|
+
}, this._idleGraceMs);
|
|
198
|
+
};
|
|
199
|
+
this._updateState = state => {
|
|
200
|
+
this._state = state;
|
|
201
|
+
if (state === CallState.PreacceptReceived) {
|
|
93
202
|
this.emit("ringing");
|
|
94
|
-
|
|
203
|
+
}
|
|
204
|
+
else if (state === CallState.Active) {
|
|
95
205
|
this.emit("connected");
|
|
96
|
-
|
|
206
|
+
if (this._screenShare) {
|
|
207
|
+
try {
|
|
208
|
+
this.engine.startScreenShare();
|
|
209
|
+
}
|
|
210
|
+
catch { }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else if (state === CallState.Idle || state === CallState.Ending) {
|
|
97
214
|
this._forceEnd("ended");
|
|
98
215
|
}
|
|
99
216
|
};
|
|
100
|
-
this._emitAudio =
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
217
|
+
this._emitAudio = pcm => {
|
|
218
|
+
this.emit("audio", pcm);
|
|
219
|
+
};
|
|
220
|
+
this._forceEnd = reason => {
|
|
221
|
+
if (this._ended)
|
|
104
222
|
return;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
223
|
+
this._ended = true;
|
|
224
|
+
this._clearIdleTimer();
|
|
225
|
+
if (this._endTimer) {
|
|
226
|
+
clearTimeout(this._endTimer);
|
|
227
|
+
this._endTimer = null;
|
|
109
228
|
}
|
|
110
229
|
this.emit("ended", reason);
|
|
111
|
-
|
|
230
|
+
this._endResolver(reason);
|
|
112
231
|
};
|
|
113
|
-
this.
|
|
114
|
-
|
|
115
|
-
|
|
232
|
+
this._endPromise = new Promise(resolve => {
|
|
233
|
+
this._endResolver = resolve;
|
|
234
|
+
});
|
|
116
235
|
if (durationMs > 0) {
|
|
117
|
-
|
|
236
|
+
this._endTimer = setTimeout(() => this.end(), durationMs);
|
|
118
237
|
}
|
|
119
238
|
}
|
|
120
|
-
get state() {
|
|
239
|
+
get state() {
|
|
240
|
+
return this._state;
|
|
241
|
+
}
|
|
121
242
|
}
|
|
122
|
-
_ActiveCall_state = new WeakMap(), _ActiveCall_endResolver = new WeakMap(), _ActiveCall_endPromise = new WeakMap(), _ActiveCall_endTimer = new WeakMap(), _ActiveCall_ended = new WeakMap(), _ActiveCall_hasBeenActive = new WeakMap();
|
|
123
243
|
export class VoipClient {
|
|
124
244
|
constructor(config = {}) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
this.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
resourcesPath: __classPrivateFieldGet(this, _VoipClient_config, "f").resourcesPath,
|
|
150
|
-
callbacks: {
|
|
151
|
-
onSignalingXmpp: (peerJid, callId, xmlPayload) => __classPrivateFieldGet(this, _VoipClient_signaling, "f").sendSignaling(peerJid, callId, xmlPayload),
|
|
152
|
-
onCallEvent: (eventType, eventData) => __classPrivateFieldGet(this, _VoipClient_handleCallEvent, "f").call(this, eventType, eventData),
|
|
153
|
-
sendDataToRelay: (data, ip, port) => __classPrivateFieldGet(this, _VoipClient_relay, "f").send(data, ip, port),
|
|
154
|
-
onAudioCaptureInit: (config) => __classPrivateFieldGet(this, _VoipClient_handleAudioCaptureInit, "f").call(this, config),
|
|
155
|
-
onAudioCaptureStart: () => __classPrivateFieldGet(this, _VoipClient_handleAudioCaptureStart, "f").call(this),
|
|
156
|
-
onAudioCaptureStop: () => __classPrivateFieldGet(this, _VoipClient_handleAudioCaptureStop, "f").call(this),
|
|
157
|
-
onAudioPlaybackData: (audioData) => __classPrivateFieldGet(this, _VoipClient_activeCall, "f")?._emitAudio(audioData),
|
|
158
|
-
cryptoHkdf: computeHkdf,
|
|
159
|
-
hmacSha256: computeHmacSha256,
|
|
160
|
-
},
|
|
161
|
-
}), "f");
|
|
162
|
-
await __classPrivateFieldGet(this, _VoipClient_engine, "f").initialize();
|
|
163
|
-
__classPrivateFieldGet(this, _VoipClient_signaling, "f").attachEngine(__classPrivateFieldGet(this, _VoipClient_engine, "f"));
|
|
164
|
-
const selfPnJid = __classPrivateFieldGet(this, _VoipClient_sock, "f").authState.creds.me?.id;
|
|
165
|
-
const selfLidJid = __classPrivateFieldGet(this, _VoipClient_sock, "f").authState.creds.me?.lid;
|
|
166
|
-
__classPrivateFieldGet(this, _VoipClient_engine, "f").initVoipStack(selfPnJid, toBareJid(selfPnJid), selfLidJid);
|
|
167
|
-
await __classPrivateFieldGet(this, _VoipClient_engine, "f").waitForVoipStackReady();
|
|
168
|
-
try {
|
|
169
|
-
__classPrivateFieldGet(this, _VoipClient_engine, "f").updateNetworkMedium(2, 0);
|
|
245
|
+
this._config = config;
|
|
246
|
+
this._log = typeof config.logger === 'function'
|
|
247
|
+
? config.logger
|
|
248
|
+
: config.debug ? (...args) => console.log(...args) : () => { };
|
|
249
|
+
this._engine = null;
|
|
250
|
+
this._relay = null;
|
|
251
|
+
this._signaling = null;
|
|
252
|
+
this._sock = config.socket || config.sock || config.conn || null;
|
|
253
|
+
this._activeCall = null;
|
|
254
|
+
this._externalSocket = !!this._sock;
|
|
255
|
+
this._capturePtr = 0;
|
|
256
|
+
this._captureChunkBytes = 0;
|
|
257
|
+
this._captureSampleRate = 16000;
|
|
258
|
+
this._captureChannels = 1;
|
|
259
|
+
this._captureFramesPerChunk = 320;
|
|
260
|
+
this._feeder = null;
|
|
261
|
+
this._videoFeeder = null;
|
|
262
|
+
this._wsCallHandler = null;
|
|
263
|
+
this._wsReceiptHandler = null;
|
|
264
|
+
this._captureStartRequested = false;
|
|
265
|
+
this._audioStartTimer = null;
|
|
266
|
+
this.connect = async () => {
|
|
267
|
+
if (!this._sock) {
|
|
268
|
+
throw new Error('VoipClient needs the socket you are already logged in with: new VoipClient({ socket: sock }). It never opens a session of its own, so there is no second pairing or QR scan.');
|
|
170
269
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
270
|
+
if (!this._sock.ws) {
|
|
271
|
+
throw new Error('the socket has no websocket yet; call connect() once connection.update reports "open"');
|
|
272
|
+
}
|
|
273
|
+
await this._initVoipStack();
|
|
274
|
+
};
|
|
275
|
+
this.attach = async (socket) => {
|
|
276
|
+
if (!socket) {
|
|
277
|
+
throw new Error('attach needs the reconnected socket');
|
|
278
|
+
}
|
|
279
|
+
this._detachSocketHandlers();
|
|
280
|
+
this._sock = socket;
|
|
281
|
+
this._externalSocket = true;
|
|
282
|
+
this._signaling?.setSocket?.(socket);
|
|
283
|
+
this._bindSocketHandlers();
|
|
284
|
+
};
|
|
285
|
+
this.connectWithSocket = async (socket) => {
|
|
286
|
+
if (!socket) {
|
|
287
|
+
throw new Error('connectWithSocket needs the socket you are already logged in with');
|
|
288
|
+
}
|
|
289
|
+
this._detachSocketHandlers();
|
|
290
|
+
this._sock = socket;
|
|
291
|
+
this._externalSocket = true;
|
|
292
|
+
this._signaling?.setSocket?.(socket);
|
|
293
|
+
await this.connect();
|
|
294
|
+
};
|
|
181
295
|
this.call = async (phoneNumber, opts = {}) => {
|
|
182
|
-
if (!
|
|
183
|
-
throw new Error("Not connected. Call
|
|
184
|
-
|
|
185
|
-
|
|
296
|
+
if (!this._engine || !this._signaling) {
|
|
297
|
+
throw new Error("Not connected. Call connect() first.");
|
|
298
|
+
}
|
|
299
|
+
if (this._activeCall) {
|
|
300
|
+
try {
|
|
301
|
+
this._activeCall._forceEnd("replaced");
|
|
302
|
+
}
|
|
303
|
+
catch { }
|
|
304
|
+
try {
|
|
305
|
+
this._engine?.endCall?.(0, true);
|
|
306
|
+
}
|
|
307
|
+
catch { }
|
|
308
|
+
try {
|
|
309
|
+
this._feeder?.stop();
|
|
310
|
+
}
|
|
311
|
+
catch { }
|
|
312
|
+
this._feeder = null;
|
|
313
|
+
try {
|
|
314
|
+
this._videoFeeder?.stop();
|
|
315
|
+
}
|
|
316
|
+
catch { }
|
|
317
|
+
this._videoFeeder = null;
|
|
318
|
+
this._activeCall = null;
|
|
319
|
+
}
|
|
186
320
|
const targetNumber = phoneNumber.replace(/\D/g, "");
|
|
321
|
+
if (!targetNumber) {
|
|
322
|
+
throw new Error("Nomor target tidak valid.");
|
|
323
|
+
}
|
|
187
324
|
const targetPnJid = `${targetNumber}@s.whatsapp.net`;
|
|
188
325
|
const durationMs = opts.durationMs ?? 120000;
|
|
189
326
|
const audioSource = opts.audioSource ?? "silence";
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
327
|
+
const peerLid = await this._signaling.resolveLid(targetPnJid);
|
|
328
|
+
if (!peerLid) {
|
|
329
|
+
throw new Error(`Tidak bisa resolve LID untuk ${targetPnJid}. Pastikan target valid dan sudah tersimpan mapping LID/PN atau USync LID bisa diakses.`);
|
|
330
|
+
}
|
|
331
|
+
const peerJid = peerLid;
|
|
332
|
+
const peerPn = targetPnJid;
|
|
333
|
+
const isLidCall = true;
|
|
194
334
|
for (const jid of [targetPnJid, peerLid]) {
|
|
195
335
|
try {
|
|
196
|
-
await
|
|
336
|
+
await this._sock.presenceSubscribe(jid);
|
|
197
337
|
}
|
|
198
338
|
catch { }
|
|
199
339
|
}
|
|
200
|
-
await new Promise(
|
|
201
|
-
|
|
340
|
+
await new Promise(resolve => setTimeout(resolve, 150));
|
|
341
|
+
let peerDeviceJids = [];
|
|
342
|
+
try {
|
|
343
|
+
peerDeviceJids = await this._signaling.discoverPeerDevices(peerLid);
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
this._log("[RTC DISCOVER DEVICES ERROR]", err?.message || err);
|
|
347
|
+
}
|
|
202
348
|
const deviceList = peerDeviceJids.length ? peerDeviceJids : [toBareJid(peerLid)];
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
349
|
+
try {
|
|
350
|
+
await this._signaling.ensureSessionsForPeers(deviceList);
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
this._log("[RTC ENSURE SESSIONS WARNING]", err?.message || err);
|
|
354
|
+
}
|
|
355
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
356
|
+
let tcToken = null;
|
|
357
|
+
try {
|
|
358
|
+
this._signaling.issueTcToken(peerLid).catch(err => {
|
|
359
|
+
this._log("[RTC ISSUE TC TOKEN ASYNC ERROR]", err?.message || err);
|
|
360
|
+
});
|
|
361
|
+
tcToken = await Promise.race([
|
|
362
|
+
this._signaling.ensureTcToken(peerLid, targetPnJid).catch(err => {
|
|
363
|
+
this._log("[RTC ENSURE TC TOKEN ERROR]", err?.message || err);
|
|
364
|
+
return null;
|
|
365
|
+
}),
|
|
366
|
+
new Promise(resolve => setTimeout(() => resolve(null), 1500))
|
|
367
|
+
]);
|
|
368
|
+
}
|
|
369
|
+
catch (err) {
|
|
370
|
+
this._log("[RTC TC TOKEN FAST PATH ERROR]", err?.message || err);
|
|
371
|
+
}
|
|
372
|
+
if (!tcToken || !tcToken.length) {
|
|
373
|
+
this._log("[RTC TC TOKEN WARNING] TC token kosong, lanjut startCall dengan token kosong.");
|
|
374
|
+
tcToken = undefined;
|
|
375
|
+
}
|
|
208
376
|
const callId = ("00" + randomBytes(16).toString("hex").slice(2)).toUpperCase();
|
|
209
|
-
const call = new ActiveCall(callId,
|
|
377
|
+
const call = new ActiveCall(callId, this._engine, durationMs);
|
|
210
378
|
call._audioSource = audioSource;
|
|
211
|
-
call.
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
379
|
+
call._audioStartDelayMs = Number(opts.audioStartDelayMs ?? opts.audioDelayMs ?? 1800) || 0;
|
|
380
|
+
call._playlist = Array.isArray(opts.playlist) ? opts.playlist.filter(Boolean) : [];
|
|
381
|
+
call._endWhenQueueEmpty = opts.endWhenQueueEmpty ?? true;
|
|
382
|
+
call._idleGraceMs = Number(opts.idleGraceMs ?? 0) || 0;
|
|
383
|
+
call._screenShare = !!opts.screenShare;
|
|
384
|
+
call._video = !!(opts.video ?? opts.isVideo) || call._screenShare;
|
|
385
|
+
call._videoSource = opts.videoSource ?? "black";
|
|
386
|
+
call._videoPlaylist = Array.isArray(opts.videoPlaylist) ? opts.videoPlaylist.filter(Boolean) : [];
|
|
387
|
+
this._activeCall = call;
|
|
388
|
+
call.once("ended", () => {
|
|
389
|
+
if (this._activeCall === call)
|
|
390
|
+
this._activeCall = null;
|
|
391
|
+
});
|
|
392
|
+
call.once("error", () => {
|
|
393
|
+
if (this._activeCall === call)
|
|
394
|
+
this._activeCall = null;
|
|
395
|
+
});
|
|
396
|
+
this._log("[RTC START CALL PARAMS]", {
|
|
397
|
+
peerJid,
|
|
398
|
+
peerPn,
|
|
216
399
|
peerList: deviceList,
|
|
217
400
|
callId,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
401
|
+
isLidCall,
|
|
402
|
+
targetPnJid,
|
|
403
|
+
peerLid,
|
|
404
|
+
hasTcToken: !!tcToken,
|
|
405
|
+
tcTokenLength: tcToken?.length || 0
|
|
406
|
+
});
|
|
407
|
+
try {
|
|
408
|
+
const startResult = this._engine.startCall({
|
|
409
|
+
peerJid,
|
|
410
|
+
peerPn,
|
|
411
|
+
peerList: deviceList,
|
|
412
|
+
callId,
|
|
413
|
+
isVideo: call._video,
|
|
414
|
+
isLidCall,
|
|
415
|
+
isFromDialer: false,
|
|
416
|
+
extraData: tcToken
|
|
417
|
+
});
|
|
418
|
+
this._log("[RTC START CALL RESULT]", startResult);
|
|
419
|
+
}
|
|
420
|
+
catch (err) {
|
|
421
|
+
if (this._activeCall === call)
|
|
422
|
+
this._activeCall = null;
|
|
423
|
+
try {
|
|
424
|
+
call._forceEnd("start_call_failed");
|
|
425
|
+
}
|
|
426
|
+
catch { }
|
|
427
|
+
throw err;
|
|
428
|
+
}
|
|
429
|
+
return call;
|
|
430
|
+
};
|
|
431
|
+
this.callGroup = async (groupJid, opts = {}) => {
|
|
432
|
+
if (!this._engine || !this._signaling) {
|
|
433
|
+
throw new Error("Not connected. Call connect() first.");
|
|
434
|
+
}
|
|
435
|
+
if (typeof groupJid !== "string" || !groupJid.endsWith("@g.us")) {
|
|
436
|
+
throw new Error(`${JSON.stringify(groupJid)} is not a group jid`);
|
|
437
|
+
}
|
|
438
|
+
const metadata = opts.metadata ?? await this._sock.groupMetadata(groupJid);
|
|
439
|
+
const selfPn = toBareJid(this._sock.authState?.creds?.me?.id ?? this._sock.user?.id ?? "");
|
|
440
|
+
const selfLid = toBareJid(this._sock.authState?.creds?.me?.lid ?? "");
|
|
441
|
+
const members = (metadata?.participants ?? [])
|
|
442
|
+
.map(participant => toBareJid(participant.id ?? participant.jid ?? ""))
|
|
443
|
+
.filter(jid => jid && jid !== selfPn && jid !== selfLid);
|
|
444
|
+
const wanted = Array.isArray(opts.participants) && opts.participants.length
|
|
445
|
+
? opts.participants.map(toBareJid)
|
|
446
|
+
: members;
|
|
447
|
+
if (!wanted.length) {
|
|
448
|
+
throw new Error(`no one to call in ${groupJid}`);
|
|
449
|
+
}
|
|
450
|
+
const pnUserJids = [];
|
|
451
|
+
const lidUserJids = [];
|
|
452
|
+
const deviceJidsCsv = [];
|
|
453
|
+
for (const member of wanted) {
|
|
454
|
+
const isLid = member.endsWith("@lid");
|
|
455
|
+
const lid = isLid ? member : await this._signaling.resolveLid(member).catch(() => null);
|
|
456
|
+
const pn = isLid ? "" : member;
|
|
457
|
+
if (pn) {
|
|
458
|
+
pnUserJids.push(pn);
|
|
459
|
+
}
|
|
460
|
+
if (lid) {
|
|
461
|
+
lidUserJids.push(lid);
|
|
462
|
+
let devices = [];
|
|
463
|
+
try {
|
|
464
|
+
devices = await this._signaling.discoverPeerDevices(lid);
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
this._log("[RTC GROUP DISCOVER DEVICES ERROR]", member, err?.message || err);
|
|
468
|
+
}
|
|
469
|
+
deviceJidsCsv.push((devices.length ? devices : [lid]).join(","));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (!lidUserJids.length) {
|
|
473
|
+
throw new Error(`could not resolve a LID for anyone in ${groupJid}`);
|
|
474
|
+
}
|
|
475
|
+
const flatDevices = deviceJidsCsv.join(",").split(",").filter(Boolean);
|
|
476
|
+
try {
|
|
477
|
+
await this._signaling.ensureSessionsForPeers(flatDevices);
|
|
478
|
+
}
|
|
479
|
+
catch (err) {
|
|
480
|
+
this._log("[RTC GROUP ENSURE SESSIONS WARNING]", err?.message || err);
|
|
481
|
+
}
|
|
482
|
+
const callId = ("00" + randomBytes(16).toString("hex").slice(2)).toUpperCase();
|
|
483
|
+
const call = new ActiveCall(callId, this._engine, opts.durationMs ?? 120000);
|
|
484
|
+
call._audioSource = opts.audioSource ?? "silence";
|
|
485
|
+
call._audioStartDelayMs = Number(opts.audioStartDelayMs ?? opts.audioDelayMs ?? 1800) || 0;
|
|
486
|
+
call._playlist = Array.isArray(opts.playlist) ? opts.playlist.filter(Boolean) : [];
|
|
487
|
+
call._endWhenQueueEmpty = opts.endWhenQueueEmpty ?? true;
|
|
488
|
+
call._idleGraceMs = Number(opts.idleGraceMs ?? 0) || 0;
|
|
489
|
+
call._screenShare = !!opts.screenShare;
|
|
490
|
+
call._video = !!(opts.video ?? opts.isVideo) || call._screenShare;
|
|
491
|
+
call._videoSource = opts.videoSource ?? "black";
|
|
492
|
+
call._videoPlaylist = Array.isArray(opts.videoPlaylist) ? opts.videoPlaylist.filter(Boolean) : [];
|
|
493
|
+
call.groupJid = groupJid;
|
|
494
|
+
this._activeCall = call;
|
|
495
|
+
call.once("ended", () => {
|
|
496
|
+
if (this._activeCall === call)
|
|
497
|
+
this._activeCall = null;
|
|
498
|
+
});
|
|
499
|
+
this._log("[RTC START GROUP CALL PARAMS]", { groupJid, callId, pnUserJids, lidUserJids, deviceJidsCsv });
|
|
500
|
+
try {
|
|
501
|
+
const startResult = this._engine.startGroupCall({
|
|
502
|
+
pnUserJids,
|
|
503
|
+
lidUserJids,
|
|
504
|
+
deviceJidsCsv,
|
|
505
|
+
callId,
|
|
506
|
+
isVideo: call._video,
|
|
507
|
+
groupJid,
|
|
508
|
+
chatName: metadata?.subject ?? ""
|
|
509
|
+
});
|
|
510
|
+
this._log("[RTC START GROUP CALL RESULT]", startResult);
|
|
511
|
+
}
|
|
512
|
+
catch (err) {
|
|
513
|
+
if (this._activeCall === call)
|
|
514
|
+
this._activeCall = null;
|
|
515
|
+
try {
|
|
516
|
+
call._forceEnd("start_group_call_failed");
|
|
517
|
+
}
|
|
518
|
+
catch { }
|
|
519
|
+
throw err;
|
|
520
|
+
}
|
|
521
|
+
return call;
|
|
522
|
+
};
|
|
523
|
+
this.joinGroupCall = async (invite, opts = {}) => {
|
|
524
|
+
if (!this._engine || !this._signaling) {
|
|
525
|
+
throw new Error("Not connected. Call connect() first.");
|
|
526
|
+
}
|
|
527
|
+
const callId = invite?.callId ?? invite?.id;
|
|
528
|
+
const groupJid = invite?.groupJid ?? invite?.from;
|
|
529
|
+
const callCreatorJid = invite?.callCreatorJid ?? invite?.creator ?? invite?.from;
|
|
530
|
+
if (!callId || !callCreatorJid) {
|
|
531
|
+
throw new Error("joinGroupCall needs at least { callId, callCreatorJid } from the incoming offer");
|
|
532
|
+
}
|
|
533
|
+
const call = new ActiveCall(callId, this._engine, opts.durationMs ?? 0);
|
|
534
|
+
call._audioSource = opts.audioSource ?? "silence";
|
|
535
|
+
call._audioStartDelayMs = Number(opts.audioStartDelayMs ?? opts.audioDelayMs ?? 1800) || 0;
|
|
536
|
+
call._playlist = Array.isArray(opts.playlist) ? opts.playlist.filter(Boolean) : [];
|
|
537
|
+
call._endWhenQueueEmpty = opts.endWhenQueueEmpty ?? true;
|
|
538
|
+
call._idleGraceMs = Number(opts.idleGraceMs ?? 0) || 0;
|
|
539
|
+
call._screenShare = !!opts.screenShare;
|
|
540
|
+
call._video = !!(opts.video ?? opts.isVideo) || call._screenShare;
|
|
541
|
+
call._videoSource = opts.videoSource ?? "black";
|
|
542
|
+
call._videoPlaylist = Array.isArray(opts.videoPlaylist) ? opts.videoPlaylist.filter(Boolean) : [];
|
|
543
|
+
call.groupJid = groupJid;
|
|
544
|
+
this._activeCall = call;
|
|
545
|
+
call.once("ended", () => {
|
|
546
|
+
if (this._activeCall === call)
|
|
547
|
+
this._activeCall = null;
|
|
222
548
|
});
|
|
549
|
+
try {
|
|
550
|
+
const result = this._engine.joinOngoingCall({
|
|
551
|
+
callId,
|
|
552
|
+
callCreatorJid,
|
|
553
|
+
initialPeerJid: invite?.initialPeerJid ?? callCreatorJid,
|
|
554
|
+
pnUserJids: invite?.pnUserJids ?? [],
|
|
555
|
+
lidUserJids: invite?.lidUserJids ?? [],
|
|
556
|
+
deviceJidsCsv: invite?.deviceJidsCsv ?? [],
|
|
557
|
+
groupJid: groupJid ?? "",
|
|
558
|
+
isVideo: call._video,
|
|
559
|
+
joinAndAccept: opts.joinAndAccept ?? true,
|
|
560
|
+
chatName: invite?.chatName ?? ""
|
|
561
|
+
});
|
|
562
|
+
this._log("[RTC JOIN CALL RESULT]", result);
|
|
563
|
+
}
|
|
564
|
+
catch (err) {
|
|
565
|
+
if (this._activeCall === call)
|
|
566
|
+
this._activeCall = null;
|
|
567
|
+
try {
|
|
568
|
+
call._forceEnd("join_call_failed");
|
|
569
|
+
}
|
|
570
|
+
catch { }
|
|
571
|
+
throw err;
|
|
572
|
+
}
|
|
223
573
|
return call;
|
|
224
574
|
};
|
|
575
|
+
this.acceptCall = (isMicEnabled = true, isCameraEnabled) => {
|
|
576
|
+
const isVideo = isCameraEnabled !== undefined ? isCameraEnabled : (this._activeCall?._video ?? false);
|
|
577
|
+
return this._engine?.acceptCall(isMicEnabled, isVideo);
|
|
578
|
+
};
|
|
225
579
|
this.disconnect = () => {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
580
|
+
this._activeCall?._forceEnd("disconnect");
|
|
581
|
+
this._activeCall = null;
|
|
582
|
+
this._captureStartRequested = false;
|
|
583
|
+
try {
|
|
584
|
+
this._feeder?.stop();
|
|
585
|
+
}
|
|
586
|
+
catch { }
|
|
587
|
+
this._feeder = null;
|
|
588
|
+
try {
|
|
589
|
+
this._videoFeeder?.stop();
|
|
590
|
+
}
|
|
591
|
+
catch { }
|
|
592
|
+
this._videoFeeder = null;
|
|
593
|
+
if (this._audioStartTimer) {
|
|
594
|
+
clearTimeout(this._audioStartTimer);
|
|
595
|
+
this._audioStartTimer = null;
|
|
596
|
+
}
|
|
597
|
+
try {
|
|
598
|
+
this._signaling?.attachEngine?.(null);
|
|
599
|
+
}
|
|
600
|
+
catch { }
|
|
601
|
+
this._detachSocketHandlers();
|
|
602
|
+
this._relay?.closeAll();
|
|
603
|
+
this._engine?.destroy();
|
|
604
|
+
if (!this._externalSocket) {
|
|
605
|
+
this._sock?.end?.();
|
|
606
|
+
}
|
|
607
|
+
this._engine = null;
|
|
608
|
+
this._relay = null;
|
|
609
|
+
this._signaling = null;
|
|
610
|
+
if (!this._externalSocket) {
|
|
611
|
+
this._sock = null;
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
this.resetCallState = () => {
|
|
615
|
+
try {
|
|
616
|
+
this._activeCall?._forceEnd("reset");
|
|
617
|
+
}
|
|
618
|
+
catch { }
|
|
619
|
+
try {
|
|
620
|
+
this._engine?.endCall?.(0, true);
|
|
621
|
+
}
|
|
622
|
+
catch { }
|
|
623
|
+
try {
|
|
624
|
+
this._feeder?.stop();
|
|
625
|
+
}
|
|
626
|
+
catch { }
|
|
627
|
+
this._feeder = null;
|
|
628
|
+
try {
|
|
629
|
+
this._videoFeeder?.stop();
|
|
630
|
+
}
|
|
631
|
+
catch { }
|
|
632
|
+
this._videoFeeder = null;
|
|
633
|
+
if (this._audioStartTimer) {
|
|
634
|
+
clearTimeout(this._audioStartTimer);
|
|
635
|
+
this._audioStartTimer = null;
|
|
636
|
+
}
|
|
637
|
+
this._captureStartRequested = false;
|
|
638
|
+
this._activeCall = null;
|
|
639
|
+
};
|
|
640
|
+
this._initVoipStack = async () => {
|
|
641
|
+
if (!this._sock) {
|
|
642
|
+
throw new Error("Socket Baileys belum tersedia.");
|
|
643
|
+
}
|
|
644
|
+
this._signaling = new SignalingBridge({
|
|
645
|
+
sock: this._sock,
|
|
646
|
+
logger: this._config.logger,
|
|
647
|
+
debug: this._config.debug
|
|
648
|
+
});
|
|
649
|
+
await this._signaling.init();
|
|
650
|
+
this._relay = new RelayRtcTransport({
|
|
651
|
+
onTransportMessage: (data, ip, port) => this._engine?.handleOnTransportMessage(data, ip, port),
|
|
652
|
+
onIceRtt: (rttMs, ip, port) => this._engine?.updateIceRtt(rttMs, ip, port)
|
|
653
|
+
});
|
|
654
|
+
this._engine = new WasmEngine({
|
|
655
|
+
callbacks: {
|
|
656
|
+
onSignalingXmpp: (peerJid, callId, xmlPayload) => {
|
|
657
|
+
this._log("[RTC WASM SIGNALING XMPP]", {
|
|
658
|
+
peerJid,
|
|
659
|
+
callId,
|
|
660
|
+
payloadLength: xmlPayload?.length || 0
|
|
661
|
+
});
|
|
662
|
+
return this._signaling.sendSignaling(peerJid, callId, xmlPayload);
|
|
663
|
+
},
|
|
664
|
+
onCallEvent: (eventType, eventData) => this._handleCallEvent(eventType, eventData),
|
|
665
|
+
sendDataToRelay: (data, ip, port) => this._relay.send(data, ip, port),
|
|
666
|
+
onAudioCaptureInit: config => this._handleAudioCaptureInit(config),
|
|
667
|
+
onAudioCaptureStart: () => this._handleAudioCaptureStart(),
|
|
668
|
+
onAudioCaptureStop: () => this._handleAudioCaptureStop(),
|
|
669
|
+
onAudioPlaybackData: audioData => this._activeCall?._emitAudio(audioData),
|
|
670
|
+
onVideoCaptureStart: config => this._handleVideoCaptureStart(config),
|
|
671
|
+
onVideoCaptureStop: () => this._handleVideoCaptureStop(),
|
|
672
|
+
onDesktopCaptureStart: config => this._handleDesktopCaptureStart(config),
|
|
673
|
+
onDesktopCaptureStop: () => this._handleDesktopCaptureStop(),
|
|
674
|
+
cryptoHkdf: computeHkdf,
|
|
675
|
+
hmacSha256: computeHmacSha256
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
await this._engine.initialize();
|
|
679
|
+
this._signaling.attachEngine(this._engine);
|
|
680
|
+
const selfPnJidRaw = this._sock.authState?.creds?.me?.id ||
|
|
681
|
+
this._sock.authState?.creds?.me?.jid ||
|
|
682
|
+
this._sock.user?.id ||
|
|
683
|
+
this._sock.user?.jid;
|
|
684
|
+
const selfPnJid = toBareJid(selfPnJidRaw);
|
|
685
|
+
const selfLidJid = this._sock.authState?.creds?.me?.lid ||
|
|
686
|
+
this._sock.authState?.creds?.me?.lidJid ||
|
|
687
|
+
"";
|
|
688
|
+
if (!selfPnJid) {
|
|
689
|
+
throw new Error("Tidak bisa membaca JID bot dari socket utama.");
|
|
690
|
+
}
|
|
691
|
+
this._log("[RTC SELF JID]", { selfPnJid, selfLidJid });
|
|
692
|
+
this._engine.initVoipStack(selfPnJid, selfPnJid, selfLidJid);
|
|
693
|
+
await this._engine.waitForVoipStackReady();
|
|
694
|
+
try {
|
|
695
|
+
this._engine.updateNetworkMedium(2, 0);
|
|
696
|
+
}
|
|
697
|
+
catch { }
|
|
698
|
+
this._bindSocketHandlers();
|
|
699
|
+
};
|
|
700
|
+
this._detachSocketHandlers = () => {
|
|
701
|
+
const ws = this._sock?.ws;
|
|
702
|
+
if (ws?.off) {
|
|
703
|
+
try {
|
|
704
|
+
if (this._wsCallHandler)
|
|
705
|
+
ws.off("CB:call", this._wsCallHandler);
|
|
706
|
+
}
|
|
707
|
+
catch { }
|
|
708
|
+
try {
|
|
709
|
+
if (this._wsReceiptHandler)
|
|
710
|
+
ws.off("CB:receipt", this._wsReceiptHandler);
|
|
711
|
+
}
|
|
712
|
+
catch { }
|
|
713
|
+
}
|
|
714
|
+
this._wsCallHandler = null;
|
|
715
|
+
this._wsReceiptHandler = null;
|
|
716
|
+
};
|
|
717
|
+
this._bindSocketHandlers = () => {
|
|
718
|
+
if (!this._sock?.ws?.on) {
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
this._detachSocketHandlers();
|
|
722
|
+
this._wsCallHandler = node => {
|
|
723
|
+
const signaling = this._signaling;
|
|
724
|
+
const engine = this._engine;
|
|
725
|
+
if (!signaling || !engine)
|
|
726
|
+
return;
|
|
727
|
+
if (typeof engine.isInitialized === "function" && !engine.isInitialized())
|
|
728
|
+
return;
|
|
729
|
+
signaling.processIncomingCall(node, engine, this._activeCall?.callId ?? "");
|
|
730
|
+
};
|
|
731
|
+
this._wsReceiptHandler = node => {
|
|
732
|
+
if (!isCallReceiptNode(node))
|
|
733
|
+
return;
|
|
734
|
+
const signaling = this._signaling;
|
|
735
|
+
const engine = this._engine;
|
|
736
|
+
if (!signaling || !engine)
|
|
737
|
+
return;
|
|
738
|
+
if (typeof engine.isInitialized === "function" && !engine.isInitialized())
|
|
739
|
+
return;
|
|
740
|
+
signaling.processIncomingReceipt(node, engine, this._activeCall?.callId ?? "");
|
|
741
|
+
};
|
|
742
|
+
this._sock.ws.on("CB:call", this._wsCallHandler);
|
|
743
|
+
this._sock.ws.on("CB:receipt", this._wsReceiptHandler);
|
|
744
|
+
};
|
|
745
|
+
this._handleCallEvent = (eventType, eventData) => {
|
|
746
|
+
this._log("[RTC CALL EVENT]", { eventType, hasData: !!eventData, data: eventData });
|
|
236
747
|
if (eventType === 16 && eventData) {
|
|
237
748
|
try {
|
|
238
749
|
const parsed = JSON.parse(eventData);
|
|
239
750
|
const info = parsed.call_info ?? parsed.callInfo ?? {};
|
|
240
751
|
const callState = Number(info.call_state ?? info.callState ?? 0);
|
|
241
|
-
|
|
752
|
+
const callId = info.call_id ?? info.callId ?? "";
|
|
753
|
+
const isVideo = !!(info.is_video ?? info.isVideo);
|
|
754
|
+
this._log("[RTC CALL STATE]", callState);
|
|
755
|
+
if (callState === CallState.ReceivedCall) {
|
|
756
|
+
if (!this._activeCall || (callId && this._activeCall.callId !== callId)) {
|
|
757
|
+
const call = new ActiveCall(callId, this._engine, 0);
|
|
758
|
+
call._video = isVideo;
|
|
759
|
+
call._state = CallState.ReceivedCall;
|
|
760
|
+
this._activeCall = call;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
this._activeCall?._updateState(callState);
|
|
764
|
+
if (callState === CallState.Active) {
|
|
765
|
+
this._captureStartRequested = true;
|
|
766
|
+
setTimeout(() => {
|
|
767
|
+
try {
|
|
768
|
+
this._startAudioFeeder?.();
|
|
769
|
+
}
|
|
770
|
+
catch (err) {
|
|
771
|
+
this._log("[RTC AUDIO START ON ACTIVE ERROR]", err?.message || err);
|
|
772
|
+
}
|
|
773
|
+
if (this._activeCall?._video && !this._videoFeeder) {
|
|
774
|
+
try {
|
|
775
|
+
this._startVideoFeeder?.({ width: 640, height: 480, fps: 24 }, false);
|
|
776
|
+
}
|
|
777
|
+
catch (err) {
|
|
778
|
+
this._log("[RTC VIDEO START ON ACTIVE ERROR]", err?.message || err);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}, 300);
|
|
782
|
+
}
|
|
242
783
|
}
|
|
243
784
|
catch { }
|
|
244
785
|
}
|
|
245
786
|
else if (eventType === 156 && eventData) {
|
|
246
787
|
try {
|
|
247
788
|
const update = JSON.parse(eventData);
|
|
248
|
-
|
|
789
|
+
this._relay?.updateRelayList(update);
|
|
249
790
|
}
|
|
250
791
|
catch { }
|
|
251
792
|
}
|
|
252
793
|
else if (eventType === 2) {
|
|
253
|
-
|
|
794
|
+
this._activeCall?._forceEnd("remote_end");
|
|
254
795
|
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (!
|
|
796
|
+
};
|
|
797
|
+
this._handleAudioCaptureInit = config => {
|
|
798
|
+
if (!this._engine)
|
|
258
799
|
return;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const chunkSamples =
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
800
|
+
this._captureSampleRate = config.sampleRate || 16000;
|
|
801
|
+
this._captureChannels = config.channels || 1;
|
|
802
|
+
this._captureFramesPerChunk = config.framesPerChunk || 320;
|
|
803
|
+
const chunkSamples = this._captureFramesPerChunk * this._captureChannels;
|
|
804
|
+
this._captureChunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
805
|
+
this._capturePtr = this._engine.malloc(this._captureChunkBytes);
|
|
806
|
+
if (this._captureStartRequested && this._activeCall?.state === CallState.Active) {
|
|
807
|
+
setTimeout(() => {
|
|
808
|
+
try {
|
|
809
|
+
this._startAudioFeeder?.();
|
|
810
|
+
}
|
|
811
|
+
catch { }
|
|
812
|
+
}, 100);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
this._startAudioFeeder = () => {
|
|
816
|
+
this._captureStartRequested = true;
|
|
817
|
+
if (this._activeCall?.state !== CallState.Active) {
|
|
818
|
+
this._log("[RTC AUDIO] capture siap, tunggu call active sebelum audio start");
|
|
268
819
|
return;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
820
|
+
}
|
|
821
|
+
if (!this._engine || !this._capturePtr) {
|
|
822
|
+
this._log("[RTC AUDIO] capturePtr belum siap, audio feeder batal start");
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (this._feeder)
|
|
826
|
+
return;
|
|
827
|
+
const audioDelayMs = Number(this._activeCall?._audioStartDelayMs ?? 0) || 0;
|
|
828
|
+
if (audioDelayMs > 0 && !this._audioStartTimer) {
|
|
829
|
+
this._log("[RTC AUDIO] call active, audio ditunda:", audioDelayMs);
|
|
830
|
+
this._audioStartTimer = setTimeout(() => {
|
|
831
|
+
this._audioStartTimer = null;
|
|
832
|
+
if (this._activeCall?.state === CallState.Active) {
|
|
833
|
+
this._activeCall._audioStartDelayMs = 0;
|
|
834
|
+
this._startAudioFeeder();
|
|
835
|
+
}
|
|
836
|
+
}, audioDelayMs);
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (this._audioStartTimer)
|
|
840
|
+
return;
|
|
841
|
+
const call = this._activeCall;
|
|
842
|
+
const playlist = call?._playlist?.length ? [...call._playlist] : [call?._audioSource ?? "silence"];
|
|
843
|
+
this._log("[RTC AUDIO] start feeder:", {
|
|
844
|
+
playlist,
|
|
845
|
+
sampleRate: this._captureSampleRate,
|
|
846
|
+
channels: this._captureChannels,
|
|
847
|
+
framesPerChunk: this._captureFramesPerChunk
|
|
848
|
+
});
|
|
849
|
+
this._feeder = new AudioFeeder(this._captureSampleRate, this._captureChannels, this._captureFramesPerChunk, chunk => {
|
|
850
|
+
if (this._engine && this._capturePtr && (!this._engine.isInitialized || this._engine.isInitialized())) {
|
|
851
|
+
this._engine.sendAudioData(chunk, this._capturePtr);
|
|
852
|
+
}
|
|
853
|
+
}, undefined, {
|
|
854
|
+
onTrackStart: track => call?._onTrackStart(track),
|
|
855
|
+
onTrackEnd: track => call?._onTrackEnd(track),
|
|
856
|
+
onIdle: () => call?._onPlaylistIdle(),
|
|
857
|
+
ffmpegPath: this._config.ffmpegPath
|
|
858
|
+
});
|
|
859
|
+
call?._attachFeeder(this._feeder);
|
|
860
|
+
this._feeder.start(playlist);
|
|
861
|
+
};
|
|
862
|
+
this._startVideoFeeder = (config, useDesktopCapture) => {
|
|
863
|
+
const call = this._activeCall;
|
|
864
|
+
const label = useDesktopCapture ? "SCREEN" : "VIDEO";
|
|
865
|
+
if (!call?._video) {
|
|
866
|
+
this._log(`[RTC ${label}] capture diminta tapi call ini bukan video, diabaikan`);
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
if (this._videoFeeder) {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const width = config?.width ?? 640;
|
|
873
|
+
const height = config?.height ?? 480;
|
|
874
|
+
const fps = config?.fps ?? 24;
|
|
875
|
+
const playlist = call._videoPlaylist?.length ? [...call._videoPlaylist] : [call._videoSource ?? "black"];
|
|
876
|
+
this._log(`[RTC ${label}] start feeder:`, { width, height, fps, playlist });
|
|
877
|
+
this._videoFeeder = new VideoFeeder(width, height, fps, (frame, w, h, rate) => {
|
|
878
|
+
if (this._engine && (!this._engine.isInitialized || this._engine.isInitialized())) {
|
|
879
|
+
this._engine.sendVideoFrame(frame, w, h, rate, VIDEO_FORMAT_I420, 0, useDesktopCapture);
|
|
880
|
+
}
|
|
881
|
+
}, undefined, {
|
|
882
|
+
onTrackStart: track => call._onVideoTrackStart(track),
|
|
883
|
+
onTrackEnd: track => call._onVideoTrackEnd(track),
|
|
884
|
+
ffmpegPath: this._config.ffmpegPath
|
|
885
|
+
});
|
|
886
|
+
call._attachVideoFeeder(this._videoFeeder);
|
|
887
|
+
this._videoFeeder.start(playlist);
|
|
888
|
+
};
|
|
889
|
+
this._stopVideoFeeder = label => {
|
|
890
|
+
this._log(`[RTC ${label}] stop feeder`);
|
|
891
|
+
try {
|
|
892
|
+
this._videoFeeder?.stop();
|
|
893
|
+
}
|
|
894
|
+
catch { }
|
|
895
|
+
this._videoFeeder = null;
|
|
896
|
+
try {
|
|
897
|
+
this._engine?.releaseVideoFrameBuffer?.();
|
|
898
|
+
}
|
|
899
|
+
catch { }
|
|
900
|
+
};
|
|
901
|
+
this._handleVideoCaptureStart = config => this._startVideoFeeder(config, false);
|
|
902
|
+
this._handleVideoCaptureStop = () => this._stopVideoFeeder("VIDEO");
|
|
903
|
+
this._handleDesktopCaptureStart = config => this._startVideoFeeder(config, true);
|
|
904
|
+
this._handleDesktopCaptureStop = () => this._stopVideoFeeder("SCREEN");
|
|
905
|
+
this._handleAudioCaptureStart = () => {
|
|
906
|
+
this._captureStartRequested = true;
|
|
907
|
+
if (this._activeCall?.state === CallState.Active) {
|
|
908
|
+
this._startAudioFeeder();
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
this._log("[RTC AUDIO] startCaptureJS diterima sebelum active, audio ditahan dulu");
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
this._handleAudioCaptureStop = () => {
|
|
915
|
+
this._captureStartRequested = false;
|
|
916
|
+
this._feeder?.stop();
|
|
917
|
+
this._feeder = null;
|
|
918
|
+
try {
|
|
919
|
+
this._videoFeeder?.stop();
|
|
920
|
+
}
|
|
921
|
+
catch { }
|
|
922
|
+
this._videoFeeder = null;
|
|
923
|
+
if (this._engine && this._capturePtr) {
|
|
280
924
|
try {
|
|
281
|
-
|
|
925
|
+
this._engine.free(this._capturePtr);
|
|
282
926
|
}
|
|
283
927
|
catch { }
|
|
284
|
-
|
|
928
|
+
this._capturePtr = 0;
|
|
285
929
|
}
|
|
286
|
-
}
|
|
287
|
-
__classPrivateFieldSet(this, _VoipClient_config, config, "f");
|
|
930
|
+
};
|
|
288
931
|
}
|
|
289
932
|
}
|
|
290
|
-
|
|
933
|
+
export { VideoFeeder, VIDEO_FORMAT_I420 } from './video-feeder.js';
|
|
934
|
+
export { AudioFeeder } from './audio-feeder.js';
|
|
935
|
+
export const makeVoipClient = async (socket, config = {}) => {
|
|
936
|
+
const client = new VoipClient({ ...config, socket });
|
|
937
|
+
await client.connect();
|
|
938
|
+
return client;
|
|
939
|
+
};
|
|
940
|
+
export default VoipClient;
|
|
291
941
|
//# sourceMappingURL=index.js.map
|