amiudmodz 6.2.7 → 6.2.9
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/Utils/baileys-event-stream.js +0 -0
- 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 +813 -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/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,874 @@ 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
|
|
222
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
|
+
}
|
|
223
429
|
return call;
|
|
224
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;
|
|
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
|
+
}
|
|
573
|
+
return call;
|
|
574
|
+
};
|
|
575
|
+
this.acceptCall = (isMicEnabled = true) => this._engine?.acceptCall(isMicEnabled, false);
|
|
225
576
|
this.disconnect = () => {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
577
|
+
this._activeCall?._forceEnd("disconnect");
|
|
578
|
+
this._activeCall = null;
|
|
579
|
+
this._captureStartRequested = false;
|
|
580
|
+
try {
|
|
581
|
+
this._feeder?.stop();
|
|
582
|
+
}
|
|
583
|
+
catch { }
|
|
584
|
+
this._feeder = null;
|
|
585
|
+
try {
|
|
586
|
+
this._videoFeeder?.stop();
|
|
587
|
+
}
|
|
588
|
+
catch { }
|
|
589
|
+
this._videoFeeder = null;
|
|
590
|
+
if (this._audioStartTimer) {
|
|
591
|
+
clearTimeout(this._audioStartTimer);
|
|
592
|
+
this._audioStartTimer = null;
|
|
593
|
+
}
|
|
594
|
+
try {
|
|
595
|
+
this._signaling?.attachEngine?.(null);
|
|
596
|
+
}
|
|
597
|
+
catch { }
|
|
598
|
+
this._detachSocketHandlers();
|
|
599
|
+
this._relay?.closeAll();
|
|
600
|
+
this._engine?.destroy();
|
|
601
|
+
if (!this._externalSocket) {
|
|
602
|
+
this._sock?.end?.();
|
|
603
|
+
}
|
|
604
|
+
this._engine = null;
|
|
605
|
+
this._relay = null;
|
|
606
|
+
this._signaling = null;
|
|
607
|
+
if (!this._externalSocket) {
|
|
608
|
+
this._sock = null;
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
this.resetCallState = () => {
|
|
612
|
+
try {
|
|
613
|
+
this._activeCall?._forceEnd("reset");
|
|
614
|
+
}
|
|
615
|
+
catch { }
|
|
616
|
+
try {
|
|
617
|
+
this._engine?.endCall?.(0, true);
|
|
618
|
+
}
|
|
619
|
+
catch { }
|
|
620
|
+
try {
|
|
621
|
+
this._feeder?.stop();
|
|
622
|
+
}
|
|
623
|
+
catch { }
|
|
624
|
+
this._feeder = null;
|
|
625
|
+
try {
|
|
626
|
+
this._videoFeeder?.stop();
|
|
627
|
+
}
|
|
628
|
+
catch { }
|
|
629
|
+
this._videoFeeder = null;
|
|
630
|
+
if (this._audioStartTimer) {
|
|
631
|
+
clearTimeout(this._audioStartTimer);
|
|
632
|
+
this._audioStartTimer = null;
|
|
633
|
+
}
|
|
634
|
+
this._captureStartRequested = false;
|
|
635
|
+
this._activeCall = null;
|
|
636
|
+
};
|
|
637
|
+
this._initVoipStack = async () => {
|
|
638
|
+
if (!this._sock) {
|
|
639
|
+
throw new Error("Socket Baileys belum tersedia.");
|
|
640
|
+
}
|
|
641
|
+
this._signaling = new SignalingBridge({
|
|
642
|
+
sock: this._sock,
|
|
643
|
+
logger: this._config.logger,
|
|
644
|
+
debug: this._config.debug
|
|
645
|
+
});
|
|
646
|
+
await this._signaling.init();
|
|
647
|
+
this._relay = new RelayRtcTransport({
|
|
648
|
+
onTransportMessage: (data, ip, port) => this._engine?.handleOnTransportMessage(data, ip, port),
|
|
649
|
+
onIceRtt: (rttMs, ip, port) => this._engine?.updateIceRtt(rttMs, ip, port)
|
|
650
|
+
});
|
|
651
|
+
this._engine = new WasmEngine({
|
|
652
|
+
callbacks: {
|
|
653
|
+
onSignalingXmpp: (peerJid, callId, xmlPayload) => {
|
|
654
|
+
this._log("[RTC WASM SIGNALING XMPP]", {
|
|
655
|
+
peerJid,
|
|
656
|
+
callId,
|
|
657
|
+
payloadLength: xmlPayload?.length || 0
|
|
658
|
+
});
|
|
659
|
+
return this._signaling.sendSignaling(peerJid, callId, xmlPayload);
|
|
660
|
+
},
|
|
661
|
+
onCallEvent: (eventType, eventData) => this._handleCallEvent(eventType, eventData),
|
|
662
|
+
sendDataToRelay: (data, ip, port) => this._relay.send(data, ip, port),
|
|
663
|
+
onAudioCaptureInit: config => this._handleAudioCaptureInit(config),
|
|
664
|
+
onAudioCaptureStart: () => this._handleAudioCaptureStart(),
|
|
665
|
+
onAudioCaptureStop: () => this._handleAudioCaptureStop(),
|
|
666
|
+
onAudioPlaybackData: audioData => this._activeCall?._emitAudio(audioData),
|
|
667
|
+
onVideoCaptureStart: config => this._handleVideoCaptureStart(config),
|
|
668
|
+
onVideoCaptureStop: () => this._handleVideoCaptureStop(),
|
|
669
|
+
onDesktopCaptureStart: config => this._handleDesktopCaptureStart(config),
|
|
670
|
+
onDesktopCaptureStop: () => this._handleDesktopCaptureStop(),
|
|
671
|
+
cryptoHkdf: computeHkdf,
|
|
672
|
+
hmacSha256: computeHmacSha256
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
await this._engine.initialize();
|
|
676
|
+
this._signaling.attachEngine(this._engine);
|
|
677
|
+
const selfPnJidRaw = this._sock.authState?.creds?.me?.id ||
|
|
678
|
+
this._sock.authState?.creds?.me?.jid ||
|
|
679
|
+
this._sock.user?.id ||
|
|
680
|
+
this._sock.user?.jid;
|
|
681
|
+
const selfPnJid = toBareJid(selfPnJidRaw);
|
|
682
|
+
const selfLidJid = this._sock.authState?.creds?.me?.lid ||
|
|
683
|
+
this._sock.authState?.creds?.me?.lidJid ||
|
|
684
|
+
"";
|
|
685
|
+
if (!selfPnJid) {
|
|
686
|
+
throw new Error("Tidak bisa membaca JID bot dari socket utama.");
|
|
687
|
+
}
|
|
688
|
+
this._log("[RTC SELF JID]", { selfPnJid, selfLidJid });
|
|
689
|
+
this._engine.initVoipStack(selfPnJid, selfPnJid, selfLidJid);
|
|
690
|
+
await this._engine.waitForVoipStackReady();
|
|
691
|
+
try {
|
|
692
|
+
this._engine.updateNetworkMedium(2, 0);
|
|
693
|
+
}
|
|
694
|
+
catch { }
|
|
695
|
+
this._bindSocketHandlers();
|
|
696
|
+
};
|
|
697
|
+
this._detachSocketHandlers = () => {
|
|
698
|
+
const ws = this._sock?.ws;
|
|
699
|
+
if (ws?.off) {
|
|
700
|
+
try {
|
|
701
|
+
if (this._wsCallHandler)
|
|
702
|
+
ws.off("CB:call", this._wsCallHandler);
|
|
703
|
+
}
|
|
704
|
+
catch { }
|
|
705
|
+
try {
|
|
706
|
+
if (this._wsReceiptHandler)
|
|
707
|
+
ws.off("CB:receipt", this._wsReceiptHandler);
|
|
708
|
+
}
|
|
709
|
+
catch { }
|
|
710
|
+
}
|
|
711
|
+
this._wsCallHandler = null;
|
|
712
|
+
this._wsReceiptHandler = null;
|
|
713
|
+
};
|
|
714
|
+
this._bindSocketHandlers = () => {
|
|
715
|
+
if (!this._sock?.ws?.on) {
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
this._detachSocketHandlers();
|
|
719
|
+
this._wsCallHandler = node => {
|
|
720
|
+
const signaling = this._signaling;
|
|
721
|
+
const engine = this._engine;
|
|
722
|
+
if (!signaling || !engine)
|
|
723
|
+
return;
|
|
724
|
+
if (typeof engine.isInitialized === "function" && !engine.isInitialized())
|
|
725
|
+
return;
|
|
726
|
+
signaling.processIncomingCall(node, engine, this._activeCall?.callId ?? "");
|
|
727
|
+
};
|
|
728
|
+
this._wsReceiptHandler = node => {
|
|
729
|
+
if (!isCallReceiptNode(node))
|
|
730
|
+
return;
|
|
731
|
+
const signaling = this._signaling;
|
|
732
|
+
const engine = this._engine;
|
|
733
|
+
if (!signaling || !engine)
|
|
734
|
+
return;
|
|
735
|
+
if (typeof engine.isInitialized === "function" && !engine.isInitialized())
|
|
736
|
+
return;
|
|
737
|
+
signaling.processIncomingReceipt(node, engine, this._activeCall?.callId ?? "");
|
|
738
|
+
};
|
|
739
|
+
this._sock.ws.on("CB:call", this._wsCallHandler);
|
|
740
|
+
this._sock.ws.on("CB:receipt", this._wsReceiptHandler);
|
|
741
|
+
};
|
|
742
|
+
this._handleCallEvent = (eventType, eventData) => {
|
|
743
|
+
this._log("[RTC CALL EVENT]", { eventType, hasData: !!eventData, data: eventData });
|
|
236
744
|
if (eventType === 16 && eventData) {
|
|
237
745
|
try {
|
|
238
746
|
const parsed = JSON.parse(eventData);
|
|
239
747
|
const info = parsed.call_info ?? parsed.callInfo ?? {};
|
|
240
748
|
const callState = Number(info.call_state ?? info.callState ?? 0);
|
|
241
|
-
|
|
749
|
+
this._log("[RTC CALL STATE]", callState);
|
|
750
|
+
this._activeCall?._updateState(callState);
|
|
751
|
+
if (callState === CallState.Active) {
|
|
752
|
+
this._captureStartRequested = true;
|
|
753
|
+
setTimeout(() => {
|
|
754
|
+
try {
|
|
755
|
+
this._startAudioFeeder?.();
|
|
756
|
+
}
|
|
757
|
+
catch (err) {
|
|
758
|
+
this._log("[RTC AUDIO START ON ACTIVE ERROR]", err?.message || err);
|
|
759
|
+
}
|
|
760
|
+
}, 300);
|
|
761
|
+
}
|
|
242
762
|
}
|
|
243
763
|
catch { }
|
|
244
764
|
}
|
|
245
765
|
else if (eventType === 156 && eventData) {
|
|
246
766
|
try {
|
|
247
767
|
const update = JSON.parse(eventData);
|
|
248
|
-
|
|
768
|
+
this._relay?.updateRelayList(update);
|
|
249
769
|
}
|
|
250
770
|
catch { }
|
|
251
771
|
}
|
|
252
772
|
else if (eventType === 2) {
|
|
253
|
-
|
|
773
|
+
this._activeCall?._forceEnd("remote_end");
|
|
254
774
|
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (!
|
|
775
|
+
};
|
|
776
|
+
this._handleAudioCaptureInit = config => {
|
|
777
|
+
if (!this._engine)
|
|
258
778
|
return;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const chunkSamples =
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
779
|
+
this._captureSampleRate = config.sampleRate || 16000;
|
|
780
|
+
this._captureChannels = config.channels || 1;
|
|
781
|
+
this._captureFramesPerChunk = config.framesPerChunk || 320;
|
|
782
|
+
const chunkSamples = this._captureFramesPerChunk * this._captureChannels;
|
|
783
|
+
this._captureChunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
784
|
+
this._capturePtr = this._engine.malloc(this._captureChunkBytes);
|
|
785
|
+
if (this._captureStartRequested && this._activeCall?.state === CallState.Active) {
|
|
786
|
+
setTimeout(() => {
|
|
787
|
+
try {
|
|
788
|
+
this._startAudioFeeder?.();
|
|
789
|
+
}
|
|
790
|
+
catch { }
|
|
791
|
+
}, 100);
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
this._startAudioFeeder = () => {
|
|
795
|
+
this._captureStartRequested = true;
|
|
796
|
+
if (this._activeCall?.state !== CallState.Active) {
|
|
797
|
+
this._log("[RTC AUDIO] capture siap, tunggu call active sebelum audio start");
|
|
268
798
|
return;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
799
|
+
}
|
|
800
|
+
if (!this._engine || !this._capturePtr) {
|
|
801
|
+
this._log("[RTC AUDIO] capturePtr belum siap, audio feeder batal start");
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
if (this._feeder)
|
|
805
|
+
return;
|
|
806
|
+
const audioDelayMs = Number(this._activeCall?._audioStartDelayMs ?? 0) || 0;
|
|
807
|
+
if (audioDelayMs > 0 && !this._audioStartTimer) {
|
|
808
|
+
this._log("[RTC AUDIO] call active, audio ditunda:", audioDelayMs);
|
|
809
|
+
this._audioStartTimer = setTimeout(() => {
|
|
810
|
+
this._audioStartTimer = null;
|
|
811
|
+
if (this._activeCall?.state === CallState.Active) {
|
|
812
|
+
this._activeCall._audioStartDelayMs = 0;
|
|
813
|
+
this._startAudioFeeder();
|
|
814
|
+
}
|
|
815
|
+
}, audioDelayMs);
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
if (this._audioStartTimer)
|
|
819
|
+
return;
|
|
820
|
+
const call = this._activeCall;
|
|
821
|
+
const playlist = call?._playlist?.length ? [...call._playlist] : [call?._audioSource ?? "silence"];
|
|
822
|
+
this._log("[RTC AUDIO] start feeder:", {
|
|
823
|
+
playlist,
|
|
824
|
+
sampleRate: this._captureSampleRate,
|
|
825
|
+
channels: this._captureChannels,
|
|
826
|
+
framesPerChunk: this._captureFramesPerChunk
|
|
827
|
+
});
|
|
828
|
+
this._feeder = new AudioFeeder(this._captureSampleRate, this._captureChannels, this._captureFramesPerChunk, chunk => {
|
|
829
|
+
if (this._engine && this._capturePtr && (!this._engine.isInitialized || this._engine.isInitialized())) {
|
|
830
|
+
this._engine.sendAudioData(chunk, this._capturePtr);
|
|
831
|
+
}
|
|
832
|
+
}, undefined, {
|
|
833
|
+
onTrackStart: track => call?._onTrackStart(track),
|
|
834
|
+
onTrackEnd: track => call?._onTrackEnd(track),
|
|
835
|
+
onIdle: () => call?._onPlaylistIdle(),
|
|
836
|
+
ffmpegPath: this._config.ffmpegPath
|
|
837
|
+
});
|
|
838
|
+
call?._attachFeeder(this._feeder);
|
|
839
|
+
this._feeder.start(playlist);
|
|
840
|
+
};
|
|
841
|
+
this._startVideoFeeder = (config, useDesktopCapture) => {
|
|
842
|
+
const call = this._activeCall;
|
|
843
|
+
const label = useDesktopCapture ? "SCREEN" : "VIDEO";
|
|
844
|
+
if (!call?._video) {
|
|
845
|
+
this._log(`[RTC ${label}] capture diminta tapi call ini bukan video, diabaikan`);
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
if (this._videoFeeder) {
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const width = config?.width ?? 640;
|
|
852
|
+
const height = config?.height ?? 480;
|
|
853
|
+
const fps = config?.fps ?? 24;
|
|
854
|
+
const playlist = call._videoPlaylist?.length ? [...call._videoPlaylist] : [call._videoSource ?? "black"];
|
|
855
|
+
this._log(`[RTC ${label}] start feeder:`, { width, height, fps, playlist });
|
|
856
|
+
this._videoFeeder = new VideoFeeder(width, height, fps, (frame, w, h, rate) => {
|
|
857
|
+
if (this._engine && (!this._engine.isInitialized || this._engine.isInitialized())) {
|
|
858
|
+
this._engine.sendVideoFrame(frame, w, h, rate, VIDEO_FORMAT_I420, 0, useDesktopCapture);
|
|
859
|
+
}
|
|
860
|
+
}, undefined, {
|
|
861
|
+
onTrackStart: track => call._onVideoTrackStart(track),
|
|
862
|
+
onTrackEnd: track => call._onVideoTrackEnd(track),
|
|
863
|
+
ffmpegPath: this._config.ffmpegPath
|
|
864
|
+
});
|
|
865
|
+
call._attachVideoFeeder(this._videoFeeder);
|
|
866
|
+
this._videoFeeder.start(playlist);
|
|
867
|
+
};
|
|
868
|
+
this._stopVideoFeeder = label => {
|
|
869
|
+
this._log(`[RTC ${label}] stop feeder`);
|
|
870
|
+
try {
|
|
871
|
+
this._videoFeeder?.stop();
|
|
872
|
+
}
|
|
873
|
+
catch { }
|
|
874
|
+
this._videoFeeder = null;
|
|
875
|
+
try {
|
|
876
|
+
this._engine?.releaseVideoFrameBuffer?.();
|
|
877
|
+
}
|
|
878
|
+
catch { }
|
|
879
|
+
};
|
|
880
|
+
this._handleVideoCaptureStart = config => this._startVideoFeeder(config, false);
|
|
881
|
+
this._handleVideoCaptureStop = () => this._stopVideoFeeder("VIDEO");
|
|
882
|
+
this._handleDesktopCaptureStart = config => this._startVideoFeeder(config, true);
|
|
883
|
+
this._handleDesktopCaptureStop = () => this._stopVideoFeeder("SCREEN");
|
|
884
|
+
this._handleAudioCaptureStart = () => {
|
|
885
|
+
this._captureStartRequested = true;
|
|
886
|
+
if (this._activeCall?.state === CallState.Active) {
|
|
887
|
+
this._startAudioFeeder();
|
|
888
|
+
}
|
|
889
|
+
else {
|
|
890
|
+
this._log("[RTC AUDIO] startCaptureJS diterima sebelum active, audio ditahan dulu");
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
this._handleAudioCaptureStop = () => {
|
|
894
|
+
this._captureStartRequested = false;
|
|
895
|
+
this._feeder?.stop();
|
|
896
|
+
this._feeder = null;
|
|
897
|
+
try {
|
|
898
|
+
this._videoFeeder?.stop();
|
|
899
|
+
}
|
|
900
|
+
catch { }
|
|
901
|
+
this._videoFeeder = null;
|
|
902
|
+
if (this._engine && this._capturePtr) {
|
|
280
903
|
try {
|
|
281
|
-
|
|
904
|
+
this._engine.free(this._capturePtr);
|
|
282
905
|
}
|
|
283
906
|
catch { }
|
|
284
|
-
|
|
907
|
+
this._capturePtr = 0;
|
|
285
908
|
}
|
|
286
|
-
}
|
|
287
|
-
__classPrivateFieldSet(this, _VoipClient_config, config, "f");
|
|
909
|
+
};
|
|
288
910
|
}
|
|
289
911
|
}
|
|
290
|
-
|
|
912
|
+
export { VideoFeeder, VIDEO_FORMAT_I420 } from './video-feeder.js';
|
|
913
|
+
export { AudioFeeder } from './audio-feeder.js';
|
|
914
|
+
export const makeVoipClient = async (socket, config = {}) => {
|
|
915
|
+
const client = new VoipClient({ ...config, socket });
|
|
916
|
+
await client.connect();
|
|
917
|
+
return client;
|
|
918
|
+
};
|
|
919
|
+
export default VoipClient;
|
|
291
920
|
//# sourceMappingURL=index.js.map
|